Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2014 Fraunhofer ITWM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Written by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Phoebe Buckheister <phoebe.buckheister@itwm.fraunhofer.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/ieee802154.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <net/mac802154.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <net/ieee802154_netdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) ieee802154_hdr_push_addr(u8 *buf, const struct ieee802154_addr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 			 bool omit_pan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	if (addr->mode == IEEE802154_ADDR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	if (!omit_pan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		memcpy(buf + pos, &addr->pan_id, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		pos += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	switch (addr->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	case IEEE802154_ADDR_SHORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		memcpy(buf + pos, &addr->short_addr, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		pos += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	case IEEE802154_ADDR_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		memcpy(buf + pos, &addr->extended_addr, IEEE802154_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		pos += IEEE802154_ADDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) ieee802154_hdr_push_sechdr(u8 *buf, const struct ieee802154_sechdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int pos = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	memcpy(buf, hdr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	memcpy(buf + 1, &hdr->frame_counter, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	switch (hdr->key_id_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	case IEEE802154_SCF_KEY_IMPLICIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	case IEEE802154_SCF_KEY_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	case IEEE802154_SCF_KEY_SHORT_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		memcpy(buf + pos, &hdr->short_src, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		pos += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	case IEEE802154_SCF_KEY_HW_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		memcpy(buf + pos, &hdr->extended_src, IEEE802154_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		pos += IEEE802154_ADDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	buf[pos++] = hdr->key_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) ieee802154_hdr_push(struct sk_buff *skb, struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u8 buf[IEEE802154_MAX_HEADER_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct ieee802154_hdr_fc *fc = &hdr->fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	buf[pos++] = hdr->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	fc->dest_addr_mode = hdr->dest.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	rc = ieee802154_hdr_push_addr(buf + pos, &hdr->dest, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	pos += rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	fc->source_addr_mode = hdr->source.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (hdr->source.pan_id == hdr->dest.pan_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	    hdr->dest.mode != IEEE802154_ADDR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		fc->intra_pan = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	rc = ieee802154_hdr_push_addr(buf + pos, &hdr->source, fc->intra_pan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	pos += rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (fc->security_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		fc->version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		rc = ieee802154_hdr_push_sechdr(buf + pos, &hdr->sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		pos += rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	memcpy(buf, fc, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	memcpy(skb_push(skb, pos), buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) EXPORT_SYMBOL_GPL(ieee802154_hdr_push);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ieee802154_hdr_get_addr(const u8 *buf, int mode, bool omit_pan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			struct ieee802154_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	addr->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (mode == IEEE802154_ADDR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (!omit_pan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		memcpy(&addr->pan_id, buf + pos, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		pos += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (mode == IEEE802154_ADDR_SHORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		memcpy(&addr->short_addr, buf + pos, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return pos + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		memcpy(&addr->extended_addr, buf + pos, IEEE802154_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return pos + IEEE802154_ADDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int ieee802154_hdr_addr_len(int mode, bool omit_pan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	int pan_len = omit_pan ? 0 : 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	case IEEE802154_ADDR_NONE: return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	case IEEE802154_ADDR_SHORT: return 2 + pan_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	case IEEE802154_ADDR_LONG: return IEEE802154_ADDR_LEN + pan_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	default: return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ieee802154_hdr_get_sechdr(const u8 *buf, struct ieee802154_sechdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int pos = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	memcpy(hdr, buf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	memcpy(&hdr->frame_counter, buf + 1, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	switch (hdr->key_id_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	case IEEE802154_SCF_KEY_IMPLICIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	case IEEE802154_SCF_KEY_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	case IEEE802154_SCF_KEY_SHORT_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		memcpy(&hdr->short_src, buf + pos, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		pos += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	case IEEE802154_SCF_KEY_HW_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		memcpy(&hdr->extended_src, buf + pos, IEEE802154_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		pos += IEEE802154_ADDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	hdr->key_id = buf[pos++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int ieee802154_sechdr_lengths[4] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	[IEEE802154_SCF_KEY_IMPLICIT] = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	[IEEE802154_SCF_KEY_INDEX] = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	[IEEE802154_SCF_KEY_SHORT_INDEX] = 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	[IEEE802154_SCF_KEY_HW_INDEX] = 14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int ieee802154_hdr_sechdr_len(u8 sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return ieee802154_sechdr_lengths[IEEE802154_SCF_KEY_ID_MODE(sc)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int ieee802154_hdr_minlen(const struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int dlen, slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	dlen = ieee802154_hdr_addr_len(hdr->fc.dest_addr_mode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	slen = ieee802154_hdr_addr_len(hdr->fc.source_addr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				       hdr->fc.intra_pan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (slen < 0 || dlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return 3 + dlen + slen + hdr->fc.security_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ieee802154_hdr_get_addrs(const u8 *buf, struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	pos += ieee802154_hdr_get_addr(buf + pos, hdr->fc.dest_addr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 				       false, &hdr->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	pos += ieee802154_hdr_get_addr(buf + pos, hdr->fc.source_addr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				       hdr->fc.intra_pan, &hdr->source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (hdr->fc.intra_pan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		hdr->source.pan_id = hdr->dest.pan_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	int pos = 3, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!pskb_may_pull(skb, 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	memcpy(hdr, skb->data, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	rc = ieee802154_hdr_minlen(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (rc < 0 || !pskb_may_pull(skb, rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	pos += ieee802154_hdr_get_addrs(skb->data + pos, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (hdr->fc.security_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		int want = pos + ieee802154_hdr_sechdr_len(skb->data[pos]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (!pskb_may_pull(skb, want))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		pos += ieee802154_hdr_get_sechdr(skb->data + pos, &hdr->sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	skb_pull(skb, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) EXPORT_SYMBOL_GPL(ieee802154_hdr_pull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ieee802154_hdr_peek_addrs(const struct sk_buff *skb, struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	const u8 *buf = skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int pos = 3, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (buf + 3 > skb_tail_pointer(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	memcpy(hdr, buf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	rc = ieee802154_hdr_minlen(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (rc < 0 || buf + rc > skb_tail_pointer(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	pos += ieee802154_hdr_get_addrs(buf + pos, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) EXPORT_SYMBOL_GPL(ieee802154_hdr_peek_addrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	const u8 *buf = skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	pos = ieee802154_hdr_peek_addrs(skb, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (pos < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (hdr->fc.security_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		u8 key_id_mode = IEEE802154_SCF_KEY_ID_MODE(*(buf + pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		int want = pos + ieee802154_sechdr_lengths[key_id_mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (buf + want > skb_tail_pointer(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		pos += ieee802154_hdr_get_sechdr(buf + pos, &hdr->sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) EXPORT_SYMBOL_GPL(ieee802154_hdr_peek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int ieee802154_max_payload(const struct ieee802154_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int hlen = ieee802154_hdr_minlen(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	if (hdr->fc.security_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		hlen += ieee802154_sechdr_lengths[hdr->sec.key_id_mode] - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		hlen += ieee802154_sechdr_authtag_len(&hdr->sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return IEEE802154_MTU - hlen - IEEE802154_MFR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) EXPORT_SYMBOL_GPL(ieee802154_max_payload);