1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Fraunhofer ITWM
6 * Phoebe Buckheister <phoebe.buckheister@itwm.fraunhofer.de>
9 #include <linux/ieee802154.h>
11 #include <net/mac802154.h>
12 #include <net/ieee802154_netdev.h>
15 ieee802154_hdr_push_addr(u8
*buf
, const struct ieee802154_addr
*addr
,
20 if (addr
->mode
== IEEE802154_ADDR_NONE
)
24 memcpy(buf
+ pos
, &addr
->pan_id
, 2);
29 case IEEE802154_ADDR_SHORT
:
30 memcpy(buf
+ pos
, &addr
->short_addr
, 2);
34 case IEEE802154_ADDR_LONG
:
35 memcpy(buf
+ pos
, &addr
->extended_addr
, IEEE802154_ADDR_LEN
);
36 pos
+= IEEE802154_ADDR_LEN
;
47 ieee802154_hdr_push_sechdr(u8
*buf
, const struct ieee802154_sechdr
*hdr
)
52 memcpy(buf
+ 1, &hdr
->frame_counter
, 4);
54 switch (hdr
->key_id_mode
) {
55 case IEEE802154_SCF_KEY_IMPLICIT
:
58 case IEEE802154_SCF_KEY_INDEX
:
61 case IEEE802154_SCF_KEY_SHORT_INDEX
:
62 memcpy(buf
+ pos
, &hdr
->short_src
, 4);
66 case IEEE802154_SCF_KEY_HW_INDEX
:
67 memcpy(buf
+ pos
, &hdr
->extended_src
, IEEE802154_ADDR_LEN
);
68 pos
+= IEEE802154_ADDR_LEN
;
72 buf
[pos
++] = hdr
->key_id
;
78 ieee802154_hdr_push(struct sk_buff
*skb
, struct ieee802154_hdr
*hdr
)
80 u8 buf
[IEEE802154_MAX_HEADER_LEN
];
83 struct ieee802154_hdr_fc
*fc
= &hdr
->fc
;
85 buf
[pos
++] = hdr
->seq
;
87 fc
->dest_addr_mode
= hdr
->dest
.mode
;
89 rc
= ieee802154_hdr_push_addr(buf
+ pos
, &hdr
->dest
, false);
94 fc
->source_addr_mode
= hdr
->source
.mode
;
96 if (hdr
->source
.pan_id
== hdr
->dest
.pan_id
&&
97 hdr
->dest
.mode
!= IEEE802154_ADDR_NONE
)
100 rc
= ieee802154_hdr_push_addr(buf
+ pos
, &hdr
->source
, fc
->intra_pan
);
105 if (fc
->security_enabled
) {
108 rc
= ieee802154_hdr_push_sechdr(buf
+ pos
, &hdr
->sec
);
117 memcpy(skb_push(skb
, pos
), buf
, pos
);
121 EXPORT_SYMBOL_GPL(ieee802154_hdr_push
);
124 ieee802154_hdr_get_addr(const u8
*buf
, int mode
, bool omit_pan
,
125 struct ieee802154_addr
*addr
)
131 if (mode
== IEEE802154_ADDR_NONE
)
135 memcpy(&addr
->pan_id
, buf
+ pos
, 2);
139 if (mode
== IEEE802154_ADDR_SHORT
) {
140 memcpy(&addr
->short_addr
, buf
+ pos
, 2);
143 memcpy(&addr
->extended_addr
, buf
+ pos
, IEEE802154_ADDR_LEN
);
144 return pos
+ IEEE802154_ADDR_LEN
;
148 static int ieee802154_hdr_addr_len(int mode
, bool omit_pan
)
150 int pan_len
= omit_pan
? 0 : 2;
153 case IEEE802154_ADDR_NONE
: return 0;
154 case IEEE802154_ADDR_SHORT
: return 2 + pan_len
;
155 case IEEE802154_ADDR_LONG
: return IEEE802154_ADDR_LEN
+ pan_len
;
156 default: return -EINVAL
;
161 ieee802154_hdr_get_sechdr(const u8
*buf
, struct ieee802154_sechdr
*hdr
)
166 memcpy(&hdr
->frame_counter
, buf
+ 1, 4);
168 switch (hdr
->key_id_mode
) {
169 case IEEE802154_SCF_KEY_IMPLICIT
:
172 case IEEE802154_SCF_KEY_INDEX
:
175 case IEEE802154_SCF_KEY_SHORT_INDEX
:
176 memcpy(&hdr
->short_src
, buf
+ pos
, 4);
180 case IEEE802154_SCF_KEY_HW_INDEX
:
181 memcpy(&hdr
->extended_src
, buf
+ pos
, IEEE802154_ADDR_LEN
);
182 pos
+= IEEE802154_ADDR_LEN
;
186 hdr
->key_id
= buf
[pos
++];
191 static int ieee802154_sechdr_lengths
[4] = {
192 [IEEE802154_SCF_KEY_IMPLICIT
] = 5,
193 [IEEE802154_SCF_KEY_INDEX
] = 6,
194 [IEEE802154_SCF_KEY_SHORT_INDEX
] = 10,
195 [IEEE802154_SCF_KEY_HW_INDEX
] = 14,
198 static int ieee802154_hdr_sechdr_len(u8 sc
)
200 return ieee802154_sechdr_lengths
[IEEE802154_SCF_KEY_ID_MODE(sc
)];
203 static int ieee802154_hdr_minlen(const struct ieee802154_hdr
*hdr
)
207 dlen
= ieee802154_hdr_addr_len(hdr
->fc
.dest_addr_mode
, false);
208 slen
= ieee802154_hdr_addr_len(hdr
->fc
.source_addr_mode
,
211 if (slen
< 0 || dlen
< 0)
214 return 3 + dlen
+ slen
+ hdr
->fc
.security_enabled
;
218 ieee802154_hdr_get_addrs(const u8
*buf
, struct ieee802154_hdr
*hdr
)
222 pos
+= ieee802154_hdr_get_addr(buf
+ pos
, hdr
->fc
.dest_addr_mode
,
224 pos
+= ieee802154_hdr_get_addr(buf
+ pos
, hdr
->fc
.source_addr_mode
,
225 hdr
->fc
.intra_pan
, &hdr
->source
);
227 if (hdr
->fc
.intra_pan
)
228 hdr
->source
.pan_id
= hdr
->dest
.pan_id
;
234 ieee802154_hdr_pull(struct sk_buff
*skb
, struct ieee802154_hdr
*hdr
)
238 if (!pskb_may_pull(skb
, 3))
241 memcpy(hdr
, skb
->data
, 3);
243 rc
= ieee802154_hdr_minlen(hdr
);
244 if (rc
< 0 || !pskb_may_pull(skb
, rc
))
247 pos
+= ieee802154_hdr_get_addrs(skb
->data
+ pos
, hdr
);
249 if (hdr
->fc
.security_enabled
) {
250 int want
= pos
+ ieee802154_hdr_sechdr_len(skb
->data
[pos
]);
252 if (!pskb_may_pull(skb
, want
))
255 pos
+= ieee802154_hdr_get_sechdr(skb
->data
+ pos
, &hdr
->sec
);
261 EXPORT_SYMBOL_GPL(ieee802154_hdr_pull
);
264 ieee802154_hdr_peek_addrs(const struct sk_buff
*skb
, struct ieee802154_hdr
*hdr
)
266 const u8
*buf
= skb_mac_header(skb
);
269 if (buf
+ 3 > skb_tail_pointer(skb
))
274 rc
= ieee802154_hdr_minlen(hdr
);
275 if (rc
< 0 || buf
+ rc
> skb_tail_pointer(skb
))
278 pos
+= ieee802154_hdr_get_addrs(buf
+ pos
, hdr
);
281 EXPORT_SYMBOL_GPL(ieee802154_hdr_peek_addrs
);
284 ieee802154_hdr_peek(const struct sk_buff
*skb
, struct ieee802154_hdr
*hdr
)
286 const u8
*buf
= skb_mac_header(skb
);
289 pos
= ieee802154_hdr_peek_addrs(skb
, hdr
);
293 if (hdr
->fc
.security_enabled
) {
294 u8 key_id_mode
= IEEE802154_SCF_KEY_ID_MODE(*(buf
+ pos
));
295 int want
= pos
+ ieee802154_sechdr_lengths
[key_id_mode
];
297 if (buf
+ want
> skb_tail_pointer(skb
))
300 pos
+= ieee802154_hdr_get_sechdr(buf
+ pos
, &hdr
->sec
);
305 EXPORT_SYMBOL_GPL(ieee802154_hdr_peek
);
307 int ieee802154_max_payload(const struct ieee802154_hdr
*hdr
)
309 int hlen
= ieee802154_hdr_minlen(hdr
);
311 if (hdr
->fc
.security_enabled
) {
312 hlen
+= ieee802154_sechdr_lengths
[hdr
->sec
.key_id_mode
] - 1;
313 hlen
+= ieee802154_sechdr_authtag_len(&hdr
->sec
);
316 return IEEE802154_MTU
- hlen
- IEEE802154_MFR_SIZE
;
318 EXPORT_SYMBOL_GPL(ieee802154_max_payload
);