2 * Copyright (C) 2014 Fraunhofer ITWM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 * Phoebe Buckheister <phoebe.buckheister@itwm.fraunhofer.de>
17 #include <linux/err.h>
18 #include <linux/bug.h>
19 #include <linux/completion.h>
20 #include <linux/ieee802154.h>
21 #include <linux/rculist.h>
23 #include <crypto/aead.h>
24 #include <crypto/skcipher.h>
26 #include "ieee802154_i.h"
29 static void llsec_key_put(struct mac802154_llsec_key
*key
);
30 static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id
*a
,
31 const struct ieee802154_llsec_key_id
*b
);
33 static void llsec_dev_free(struct mac802154_llsec_device
*dev
);
35 void mac802154_llsec_init(struct mac802154_llsec
*sec
)
37 memset(sec
, 0, sizeof(*sec
));
39 memset(&sec
->params
.default_key_source
, 0xFF, IEEE802154_ADDR_LEN
);
41 INIT_LIST_HEAD(&sec
->table
.security_levels
);
42 INIT_LIST_HEAD(&sec
->table
.devices
);
43 INIT_LIST_HEAD(&sec
->table
.keys
);
44 hash_init(sec
->devices_short
);
45 hash_init(sec
->devices_hw
);
46 rwlock_init(&sec
->lock
);
49 void mac802154_llsec_destroy(struct mac802154_llsec
*sec
)
51 struct ieee802154_llsec_seclevel
*sl
, *sn
;
52 struct ieee802154_llsec_device
*dev
, *dn
;
53 struct ieee802154_llsec_key_entry
*key
, *kn
;
55 list_for_each_entry_safe(sl
, sn
, &sec
->table
.security_levels
, list
) {
56 struct mac802154_llsec_seclevel
*msl
;
58 msl
= container_of(sl
, struct mac802154_llsec_seclevel
, level
);
63 list_for_each_entry_safe(dev
, dn
, &sec
->table
.devices
, list
) {
64 struct mac802154_llsec_device
*mdev
;
66 mdev
= container_of(dev
, struct mac802154_llsec_device
, dev
);
71 list_for_each_entry_safe(key
, kn
, &sec
->table
.keys
, list
) {
72 struct mac802154_llsec_key
*mkey
;
74 mkey
= container_of(key
->key
, struct mac802154_llsec_key
, key
);
81 int mac802154_llsec_get_params(struct mac802154_llsec
*sec
,
82 struct ieee802154_llsec_params
*params
)
84 read_lock_bh(&sec
->lock
);
85 *params
= sec
->params
;
86 read_unlock_bh(&sec
->lock
);
91 int mac802154_llsec_set_params(struct mac802154_llsec
*sec
,
92 const struct ieee802154_llsec_params
*params
,
95 write_lock_bh(&sec
->lock
);
97 if (changed
& IEEE802154_LLSEC_PARAM_ENABLED
)
98 sec
->params
.enabled
= params
->enabled
;
99 if (changed
& IEEE802154_LLSEC_PARAM_FRAME_COUNTER
)
100 sec
->params
.frame_counter
= params
->frame_counter
;
101 if (changed
& IEEE802154_LLSEC_PARAM_OUT_LEVEL
)
102 sec
->params
.out_level
= params
->out_level
;
103 if (changed
& IEEE802154_LLSEC_PARAM_OUT_KEY
)
104 sec
->params
.out_key
= params
->out_key
;
105 if (changed
& IEEE802154_LLSEC_PARAM_KEY_SOURCE
)
106 sec
->params
.default_key_source
= params
->default_key_source
;
107 if (changed
& IEEE802154_LLSEC_PARAM_PAN_ID
)
108 sec
->params
.pan_id
= params
->pan_id
;
109 if (changed
& IEEE802154_LLSEC_PARAM_HWADDR
)
110 sec
->params
.hwaddr
= params
->hwaddr
;
111 if (changed
& IEEE802154_LLSEC_PARAM_COORD_HWADDR
)
112 sec
->params
.coord_hwaddr
= params
->coord_hwaddr
;
113 if (changed
& IEEE802154_LLSEC_PARAM_COORD_SHORTADDR
)
114 sec
->params
.coord_shortaddr
= params
->coord_shortaddr
;
116 write_unlock_bh(&sec
->lock
);
121 static struct mac802154_llsec_key
*
122 llsec_key_alloc(const struct ieee802154_llsec_key
*template)
124 const int authsizes
[3] = { 4, 8, 16 };
125 struct mac802154_llsec_key
*key
;
128 key
= kzalloc(sizeof(*key
), GFP_KERNEL
);
132 kref_init(&key
->ref
);
133 key
->key
= *template;
135 BUILD_BUG_ON(ARRAY_SIZE(authsizes
) != ARRAY_SIZE(key
->tfm
));
137 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++) {
138 key
->tfm
[i
] = crypto_alloc_aead("ccm(aes)", 0,
140 if (IS_ERR(key
->tfm
[i
]))
142 if (crypto_aead_setkey(key
->tfm
[i
], template->key
,
143 IEEE802154_LLSEC_KEY_SIZE
))
145 if (crypto_aead_setauthsize(key
->tfm
[i
], authsizes
[i
]))
149 key
->tfm0
= crypto_alloc_skcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC
);
150 if (IS_ERR(key
->tfm0
))
153 if (crypto_skcipher_setkey(key
->tfm0
, template->key
,
154 IEEE802154_LLSEC_KEY_SIZE
))
160 crypto_free_skcipher(key
->tfm0
);
162 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++)
164 crypto_free_aead(key
->tfm
[i
]);
170 static void llsec_key_release(struct kref
*ref
)
172 struct mac802154_llsec_key
*key
;
175 key
= container_of(ref
, struct mac802154_llsec_key
, ref
);
177 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++)
178 crypto_free_aead(key
->tfm
[i
]);
180 crypto_free_skcipher(key
->tfm0
);
184 static struct mac802154_llsec_key
*
185 llsec_key_get(struct mac802154_llsec_key
*key
)
191 static void llsec_key_put(struct mac802154_llsec_key
*key
)
193 kref_put(&key
->ref
, llsec_key_release
);
196 static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id
*a
,
197 const struct ieee802154_llsec_key_id
*b
)
199 if (a
->mode
!= b
->mode
)
202 if (a
->mode
== IEEE802154_SCF_KEY_IMPLICIT
)
203 return ieee802154_addr_equal(&a
->device_addr
, &b
->device_addr
);
209 case IEEE802154_SCF_KEY_INDEX
:
211 case IEEE802154_SCF_KEY_SHORT_INDEX
:
212 return a
->short_source
== b
->short_source
;
213 case IEEE802154_SCF_KEY_HW_INDEX
:
214 return a
->extended_source
== b
->extended_source
;
220 int mac802154_llsec_key_add(struct mac802154_llsec
*sec
,
221 const struct ieee802154_llsec_key_id
*id
,
222 const struct ieee802154_llsec_key
*key
)
224 struct mac802154_llsec_key
*mkey
= NULL
;
225 struct ieee802154_llsec_key_entry
*pos
, *new;
227 if (!(key
->frame_types
& (1 << IEEE802154_FC_TYPE_MAC_CMD
)) &&
231 list_for_each_entry(pos
, &sec
->table
.keys
, list
) {
232 if (llsec_key_id_equal(&pos
->id
, id
))
235 if (memcmp(pos
->key
->key
, key
->key
,
236 IEEE802154_LLSEC_KEY_SIZE
))
239 mkey
= container_of(pos
->key
, struct mac802154_llsec_key
, key
);
241 /* Don't allow multiple instances of the same AES key to have
242 * different allowed frame types/command frame ids, as this is
243 * not possible in the 802.15.4 PIB.
245 if (pos
->key
->frame_types
!= key
->frame_types
||
246 pos
->key
->cmd_frame_ids
!= key
->cmd_frame_ids
)
252 new = kzalloc(sizeof(*new), GFP_KERNEL
);
257 mkey
= llsec_key_alloc(key
);
259 mkey
= llsec_key_get(mkey
);
265 new->key
= &mkey
->key
;
267 list_add_rcu(&new->list
, &sec
->table
.keys
);
276 int mac802154_llsec_key_del(struct mac802154_llsec
*sec
,
277 const struct ieee802154_llsec_key_id
*key
)
279 struct ieee802154_llsec_key_entry
*pos
;
281 list_for_each_entry(pos
, &sec
->table
.keys
, list
) {
282 struct mac802154_llsec_key
*mkey
;
284 mkey
= container_of(pos
->key
, struct mac802154_llsec_key
, key
);
286 if (llsec_key_id_equal(&pos
->id
, key
)) {
287 list_del_rcu(&pos
->list
);
296 static bool llsec_dev_use_shortaddr(__le16 short_addr
)
298 return short_addr
!= cpu_to_le16(IEEE802154_ADDR_UNDEF
) &&
299 short_addr
!= cpu_to_le16(0xffff);
302 static u32
llsec_dev_hash_short(__le16 short_addr
, __le16 pan_id
)
304 return ((__force u16
)short_addr
) << 16 | (__force u16
)pan_id
;
307 static u64
llsec_dev_hash_long(__le64 hwaddr
)
309 return (__force u64
)hwaddr
;
312 static struct mac802154_llsec_device
*
313 llsec_dev_find_short(struct mac802154_llsec
*sec
, __le16 short_addr
,
316 struct mac802154_llsec_device
*dev
;
317 u32 key
= llsec_dev_hash_short(short_addr
, pan_id
);
319 hash_for_each_possible_rcu(sec
->devices_short
, dev
, bucket_s
, key
) {
320 if (dev
->dev
.short_addr
== short_addr
&&
321 dev
->dev
.pan_id
== pan_id
)
328 static struct mac802154_llsec_device
*
329 llsec_dev_find_long(struct mac802154_llsec
*sec
, __le64 hwaddr
)
331 struct mac802154_llsec_device
*dev
;
332 u64 key
= llsec_dev_hash_long(hwaddr
);
334 hash_for_each_possible_rcu(sec
->devices_hw
, dev
, bucket_hw
, key
) {
335 if (dev
->dev
.hwaddr
== hwaddr
)
342 static void llsec_dev_free(struct mac802154_llsec_device
*dev
)
344 struct ieee802154_llsec_device_key
*pos
, *pn
;
345 struct mac802154_llsec_device_key
*devkey
;
347 list_for_each_entry_safe(pos
, pn
, &dev
->dev
.keys
, list
) {
348 devkey
= container_of(pos
, struct mac802154_llsec_device_key
,
351 list_del(&pos
->list
);
358 int mac802154_llsec_dev_add(struct mac802154_llsec
*sec
,
359 const struct ieee802154_llsec_device
*dev
)
361 struct mac802154_llsec_device
*entry
;
362 u32 skey
= llsec_dev_hash_short(dev
->short_addr
, dev
->pan_id
);
363 u64 hwkey
= llsec_dev_hash_long(dev
->hwaddr
);
365 BUILD_BUG_ON(sizeof(hwkey
) != IEEE802154_ADDR_LEN
);
367 if ((llsec_dev_use_shortaddr(dev
->short_addr
) &&
368 llsec_dev_find_short(sec
, dev
->short_addr
, dev
->pan_id
)) ||
369 llsec_dev_find_long(sec
, dev
->hwaddr
))
372 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
377 spin_lock_init(&entry
->lock
);
378 INIT_LIST_HEAD(&entry
->dev
.keys
);
380 if (llsec_dev_use_shortaddr(dev
->short_addr
))
381 hash_add_rcu(sec
->devices_short
, &entry
->bucket_s
, skey
);
383 INIT_HLIST_NODE(&entry
->bucket_s
);
385 hash_add_rcu(sec
->devices_hw
, &entry
->bucket_hw
, hwkey
);
386 list_add_tail_rcu(&entry
->dev
.list
, &sec
->table
.devices
);
391 static void llsec_dev_free_rcu(struct rcu_head
*rcu
)
393 llsec_dev_free(container_of(rcu
, struct mac802154_llsec_device
, rcu
));
396 int mac802154_llsec_dev_del(struct mac802154_llsec
*sec
, __le64 device_addr
)
398 struct mac802154_llsec_device
*pos
;
400 pos
= llsec_dev_find_long(sec
, device_addr
);
404 hash_del_rcu(&pos
->bucket_s
);
405 hash_del_rcu(&pos
->bucket_hw
);
406 list_del_rcu(&pos
->dev
.list
);
407 call_rcu(&pos
->rcu
, llsec_dev_free_rcu
);
412 static struct mac802154_llsec_device_key
*
413 llsec_devkey_find(struct mac802154_llsec_device
*dev
,
414 const struct ieee802154_llsec_key_id
*key
)
416 struct ieee802154_llsec_device_key
*devkey
;
418 list_for_each_entry_rcu(devkey
, &dev
->dev
.keys
, list
) {
419 if (!llsec_key_id_equal(key
, &devkey
->key_id
))
422 return container_of(devkey
, struct mac802154_llsec_device_key
,
429 int mac802154_llsec_devkey_add(struct mac802154_llsec
*sec
,
431 const struct ieee802154_llsec_device_key
*key
)
433 struct mac802154_llsec_device
*dev
;
434 struct mac802154_llsec_device_key
*devkey
;
436 dev
= llsec_dev_find_long(sec
, dev_addr
);
441 if (llsec_devkey_find(dev
, &key
->key_id
))
444 devkey
= kmalloc(sizeof(*devkey
), GFP_KERNEL
);
448 devkey
->devkey
= *key
;
449 list_add_tail_rcu(&devkey
->devkey
.list
, &dev
->dev
.keys
);
453 int mac802154_llsec_devkey_del(struct mac802154_llsec
*sec
,
455 const struct ieee802154_llsec_device_key
*key
)
457 struct mac802154_llsec_device
*dev
;
458 struct mac802154_llsec_device_key
*devkey
;
460 dev
= llsec_dev_find_long(sec
, dev_addr
);
465 devkey
= llsec_devkey_find(dev
, &key
->key_id
);
469 list_del_rcu(&devkey
->devkey
.list
);
470 kfree_rcu(devkey
, rcu
);
474 static struct mac802154_llsec_seclevel
*
475 llsec_find_seclevel(const struct mac802154_llsec
*sec
,
476 const struct ieee802154_llsec_seclevel
*sl
)
478 struct ieee802154_llsec_seclevel
*pos
;
480 list_for_each_entry(pos
, &sec
->table
.security_levels
, list
) {
481 if (pos
->frame_type
!= sl
->frame_type
||
482 (pos
->frame_type
== IEEE802154_FC_TYPE_MAC_CMD
&&
483 pos
->cmd_frame_id
!= sl
->cmd_frame_id
) ||
484 pos
->device_override
!= sl
->device_override
||
485 pos
->sec_levels
!= sl
->sec_levels
)
488 return container_of(pos
, struct mac802154_llsec_seclevel
,
495 int mac802154_llsec_seclevel_add(struct mac802154_llsec
*sec
,
496 const struct ieee802154_llsec_seclevel
*sl
)
498 struct mac802154_llsec_seclevel
*entry
;
500 if (llsec_find_seclevel(sec
, sl
))
503 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
509 list_add_tail_rcu(&entry
->level
.list
, &sec
->table
.security_levels
);
514 int mac802154_llsec_seclevel_del(struct mac802154_llsec
*sec
,
515 const struct ieee802154_llsec_seclevel
*sl
)
517 struct mac802154_llsec_seclevel
*pos
;
519 pos
= llsec_find_seclevel(sec
, sl
);
523 list_del_rcu(&pos
->level
.list
);
529 static int llsec_recover_addr(struct mac802154_llsec
*sec
,
530 struct ieee802154_addr
*addr
)
532 __le16 caddr
= sec
->params
.coord_shortaddr
;
534 addr
->pan_id
= sec
->params
.pan_id
;
536 if (caddr
== cpu_to_le16(IEEE802154_ADDR_BROADCAST
)) {
538 } else if (caddr
== cpu_to_le16(IEEE802154_ADDR_UNDEF
)) {
539 addr
->extended_addr
= sec
->params
.coord_hwaddr
;
540 addr
->mode
= IEEE802154_ADDR_LONG
;
542 addr
->short_addr
= sec
->params
.coord_shortaddr
;
543 addr
->mode
= IEEE802154_ADDR_SHORT
;
549 static struct mac802154_llsec_key
*
550 llsec_lookup_key(struct mac802154_llsec
*sec
,
551 const struct ieee802154_hdr
*hdr
,
552 const struct ieee802154_addr
*addr
,
553 struct ieee802154_llsec_key_id
*key_id
)
555 struct ieee802154_addr devaddr
= *addr
;
556 u8 key_id_mode
= hdr
->sec
.key_id_mode
;
557 struct ieee802154_llsec_key_entry
*key_entry
;
558 struct mac802154_llsec_key
*key
;
560 if (key_id_mode
== IEEE802154_SCF_KEY_IMPLICIT
&&
561 devaddr
.mode
== IEEE802154_ADDR_NONE
) {
562 if (hdr
->fc
.type
== IEEE802154_FC_TYPE_BEACON
) {
563 devaddr
.extended_addr
= sec
->params
.coord_hwaddr
;
564 devaddr
.mode
= IEEE802154_ADDR_LONG
;
565 } else if (llsec_recover_addr(sec
, &devaddr
) < 0) {
570 list_for_each_entry_rcu(key_entry
, &sec
->table
.keys
, list
) {
571 const struct ieee802154_llsec_key_id
*id
= &key_entry
->id
;
573 if (!(key_entry
->key
->frame_types
& BIT(hdr
->fc
.type
)))
576 if (id
->mode
!= key_id_mode
)
579 if (key_id_mode
== IEEE802154_SCF_KEY_IMPLICIT
) {
580 if (ieee802154_addr_equal(&devaddr
, &id
->device_addr
))
583 if (id
->id
!= hdr
->sec
.key_id
)
586 if ((key_id_mode
== IEEE802154_SCF_KEY_INDEX
) ||
587 (key_id_mode
== IEEE802154_SCF_KEY_SHORT_INDEX
&&
588 id
->short_source
== hdr
->sec
.short_src
) ||
589 (key_id_mode
== IEEE802154_SCF_KEY_HW_INDEX
&&
590 id
->extended_source
== hdr
->sec
.extended_src
))
598 key
= container_of(key_entry
->key
, struct mac802154_llsec_key
, key
);
600 *key_id
= key_entry
->id
;
601 return llsec_key_get(key
);
604 static void llsec_geniv(u8 iv
[16], __le64 addr
,
605 const struct ieee802154_sechdr
*sec
)
607 __be64 addr_bytes
= (__force __be64
) swab64((__force u64
) addr
);
608 __be32 frame_counter
= (__force __be32
) swab32((__force u32
) sec
->frame_counter
);
610 iv
[0] = 1; /* L' = L - 1 = 1 */
611 memcpy(iv
+ 1, &addr_bytes
, sizeof(addr_bytes
));
612 memcpy(iv
+ 9, &frame_counter
, sizeof(frame_counter
));
619 llsec_do_encrypt_unauth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
620 const struct ieee802154_hdr
*hdr
,
621 struct mac802154_llsec_key
*key
)
624 struct scatterlist src
;
625 SKCIPHER_REQUEST_ON_STACK(req
, key
->tfm0
);
629 llsec_geniv(iv
, sec
->params
.hwaddr
, &hdr
->sec
);
630 /* Compute data payload offset and data length */
631 data
= skb_mac_header(skb
) + skb
->mac_len
;
632 datalen
= skb_tail_pointer(skb
) - data
;
633 sg_init_one(&src
, data
, datalen
);
635 skcipher_request_set_tfm(req
, key
->tfm0
);
636 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
637 skcipher_request_set_crypt(req
, &src
, &src
, datalen
, iv
);
638 err
= crypto_skcipher_encrypt(req
);
639 skcipher_request_zero(req
);
643 static struct crypto_aead
*
644 llsec_tfm_by_len(struct mac802154_llsec_key
*key
, int authlen
)
648 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++)
649 if (crypto_aead_authsize(key
->tfm
[i
]) == authlen
)
656 llsec_do_encrypt_auth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
657 const struct ieee802154_hdr
*hdr
,
658 struct mac802154_llsec_key
*key
)
662 int authlen
, assoclen
, datalen
, rc
;
663 struct scatterlist sg
;
664 struct aead_request
*req
;
666 authlen
= ieee802154_sechdr_authtag_len(&hdr
->sec
);
667 llsec_geniv(iv
, sec
->params
.hwaddr
, &hdr
->sec
);
669 req
= aead_request_alloc(llsec_tfm_by_len(key
, authlen
), GFP_ATOMIC
);
673 assoclen
= skb
->mac_len
;
675 data
= skb_mac_header(skb
) + skb
->mac_len
;
676 datalen
= skb_tail_pointer(skb
) - data
;
678 skb_put(skb
, authlen
);
680 sg_init_one(&sg
, skb_mac_header(skb
), assoclen
+ datalen
+ authlen
);
682 if (!(hdr
->sec
.level
& IEEE802154_SCF_SECLEVEL_ENC
)) {
687 aead_request_set_callback(req
, 0, NULL
, NULL
);
688 aead_request_set_crypt(req
, &sg
, &sg
, datalen
, iv
);
689 aead_request_set_ad(req
, assoclen
);
691 rc
= crypto_aead_encrypt(req
);
698 static int llsec_do_encrypt(struct sk_buff
*skb
,
699 const struct mac802154_llsec
*sec
,
700 const struct ieee802154_hdr
*hdr
,
701 struct mac802154_llsec_key
*key
)
703 if (hdr
->sec
.level
== IEEE802154_SCF_SECLEVEL_ENC
)
704 return llsec_do_encrypt_unauth(skb
, sec
, hdr
, key
);
706 return llsec_do_encrypt_auth(skb
, sec
, hdr
, key
);
709 int mac802154_llsec_encrypt(struct mac802154_llsec
*sec
, struct sk_buff
*skb
)
711 struct ieee802154_hdr hdr
;
712 int rc
, authlen
, hlen
;
713 struct mac802154_llsec_key
*key
;
716 hlen
= ieee802154_hdr_pull(skb
, &hdr
);
718 if (hlen
< 0 || hdr
.fc
.type
!= IEEE802154_FC_TYPE_DATA
)
721 if (!hdr
.fc
.security_enabled
||
722 (hdr
.sec
.level
== IEEE802154_SCF_SECLEVEL_NONE
)) {
727 authlen
= ieee802154_sechdr_authtag_len(&hdr
.sec
);
729 if (skb
->len
+ hlen
+ authlen
+ IEEE802154_MFR_SIZE
> IEEE802154_MTU
)
734 read_lock_bh(&sec
->lock
);
736 if (!sec
->params
.enabled
) {
741 key
= llsec_lookup_key(sec
, &hdr
, &hdr
.dest
, NULL
);
747 read_unlock_bh(&sec
->lock
);
749 write_lock_bh(&sec
->lock
);
751 frame_ctr
= be32_to_cpu(sec
->params
.frame_counter
);
752 hdr
.sec
.frame_counter
= cpu_to_le32(frame_ctr
);
753 if (frame_ctr
== 0xFFFFFFFF) {
754 write_unlock_bh(&sec
->lock
);
760 sec
->params
.frame_counter
= cpu_to_be32(frame_ctr
+ 1);
762 write_unlock_bh(&sec
->lock
);
766 skb
->mac_len
= ieee802154_hdr_push(skb
, &hdr
);
767 skb_reset_mac_header(skb
);
769 rc
= llsec_do_encrypt(skb
, sec
, &hdr
, key
);
775 read_unlock_bh(&sec
->lock
);
781 static struct mac802154_llsec_device
*
782 llsec_lookup_dev(struct mac802154_llsec
*sec
,
783 const struct ieee802154_addr
*addr
)
785 struct ieee802154_addr devaddr
= *addr
;
786 struct mac802154_llsec_device
*dev
= NULL
;
788 if (devaddr
.mode
== IEEE802154_ADDR_NONE
&&
789 llsec_recover_addr(sec
, &devaddr
) < 0)
792 if (devaddr
.mode
== IEEE802154_ADDR_SHORT
) {
793 u32 key
= llsec_dev_hash_short(devaddr
.short_addr
,
796 hash_for_each_possible_rcu(sec
->devices_short
, dev
,
798 if (dev
->dev
.pan_id
== devaddr
.pan_id
&&
799 dev
->dev
.short_addr
== devaddr
.short_addr
)
803 u64 key
= llsec_dev_hash_long(devaddr
.extended_addr
);
805 hash_for_each_possible_rcu(sec
->devices_hw
, dev
,
807 if (dev
->dev
.hwaddr
== devaddr
.extended_addr
)
816 llsec_lookup_seclevel(const struct mac802154_llsec
*sec
,
817 u8 frame_type
, u8 cmd_frame_id
,
818 struct ieee802154_llsec_seclevel
*rlevel
)
820 struct ieee802154_llsec_seclevel
*level
;
822 list_for_each_entry_rcu(level
, &sec
->table
.security_levels
, list
) {
823 if (level
->frame_type
== frame_type
&&
824 (frame_type
!= IEEE802154_FC_TYPE_MAC_CMD
||
825 level
->cmd_frame_id
== cmd_frame_id
)) {
835 llsec_do_decrypt_unauth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
836 const struct ieee802154_hdr
*hdr
,
837 struct mac802154_llsec_key
*key
, __le64 dev_addr
)
842 struct scatterlist src
;
843 SKCIPHER_REQUEST_ON_STACK(req
, key
->tfm0
);
846 llsec_geniv(iv
, dev_addr
, &hdr
->sec
);
847 data
= skb_mac_header(skb
) + skb
->mac_len
;
848 datalen
= skb_tail_pointer(skb
) - data
;
850 sg_init_one(&src
, data
, datalen
);
852 skcipher_request_set_tfm(req
, key
->tfm0
);
853 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
854 skcipher_request_set_crypt(req
, &src
, &src
, datalen
, iv
);
856 err
= crypto_skcipher_decrypt(req
);
857 skcipher_request_zero(req
);
862 llsec_do_decrypt_auth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
863 const struct ieee802154_hdr
*hdr
,
864 struct mac802154_llsec_key
*key
, __le64 dev_addr
)
868 int authlen
, datalen
, assoclen
, rc
;
869 struct scatterlist sg
;
870 struct aead_request
*req
;
872 authlen
= ieee802154_sechdr_authtag_len(&hdr
->sec
);
873 llsec_geniv(iv
, dev_addr
, &hdr
->sec
);
875 req
= aead_request_alloc(llsec_tfm_by_len(key
, authlen
), GFP_ATOMIC
);
879 assoclen
= skb
->mac_len
;
881 data
= skb_mac_header(skb
) + skb
->mac_len
;
882 datalen
= skb_tail_pointer(skb
) - data
;
884 sg_init_one(&sg
, skb_mac_header(skb
), assoclen
+ datalen
);
886 if (!(hdr
->sec
.level
& IEEE802154_SCF_SECLEVEL_ENC
)) {
887 assoclen
+= datalen
- authlen
;
891 aead_request_set_callback(req
, 0, NULL
, NULL
);
892 aead_request_set_crypt(req
, &sg
, &sg
, datalen
, iv
);
893 aead_request_set_ad(req
, assoclen
);
895 rc
= crypto_aead_decrypt(req
);
898 skb_trim(skb
, skb
->len
- authlen
);
904 llsec_do_decrypt(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
905 const struct ieee802154_hdr
*hdr
,
906 struct mac802154_llsec_key
*key
, __le64 dev_addr
)
908 if (hdr
->sec
.level
== IEEE802154_SCF_SECLEVEL_ENC
)
909 return llsec_do_decrypt_unauth(skb
, sec
, hdr
, key
, dev_addr
);
911 return llsec_do_decrypt_auth(skb
, sec
, hdr
, key
, dev_addr
);
915 llsec_update_devkey_record(struct mac802154_llsec_device
*dev
,
916 const struct ieee802154_llsec_key_id
*in_key
)
918 struct mac802154_llsec_device_key
*devkey
;
920 devkey
= llsec_devkey_find(dev
, in_key
);
923 struct mac802154_llsec_device_key
*next
;
925 next
= kzalloc(sizeof(*devkey
), GFP_ATOMIC
);
929 next
->devkey
.key_id
= *in_key
;
931 spin_lock_bh(&dev
->lock
);
933 devkey
= llsec_devkey_find(dev
, in_key
);
935 list_add_rcu(&next
->devkey
.list
, &dev
->dev
.keys
);
939 spin_unlock_bh(&dev
->lock
);
946 llsec_update_devkey_info(struct mac802154_llsec_device
*dev
,
947 const struct ieee802154_llsec_key_id
*in_key
,
950 struct mac802154_llsec_device_key
*devkey
= NULL
;
952 if (dev
->dev
.key_mode
== IEEE802154_LLSEC_DEVKEY_RESTRICT
) {
953 devkey
= llsec_devkey_find(dev
, in_key
);
958 if (dev
->dev
.key_mode
== IEEE802154_LLSEC_DEVKEY_RECORD
) {
959 int rc
= llsec_update_devkey_record(dev
, in_key
);
965 spin_lock_bh(&dev
->lock
);
967 if ((!devkey
&& frame_counter
< dev
->dev
.frame_counter
) ||
968 (devkey
&& frame_counter
< devkey
->devkey
.frame_counter
)) {
969 spin_unlock_bh(&dev
->lock
);
974 devkey
->devkey
.frame_counter
= frame_counter
+ 1;
976 dev
->dev
.frame_counter
= frame_counter
+ 1;
978 spin_unlock_bh(&dev
->lock
);
983 int mac802154_llsec_decrypt(struct mac802154_llsec
*sec
, struct sk_buff
*skb
)
985 struct ieee802154_hdr hdr
;
986 struct mac802154_llsec_key
*key
;
987 struct ieee802154_llsec_key_id key_id
;
988 struct mac802154_llsec_device
*dev
;
989 struct ieee802154_llsec_seclevel seclevel
;
994 if (ieee802154_hdr_peek(skb
, &hdr
) < 0)
996 if (!hdr
.fc
.security_enabled
)
998 if (hdr
.fc
.version
== 0)
1001 read_lock_bh(&sec
->lock
);
1002 if (!sec
->params
.enabled
) {
1003 read_unlock_bh(&sec
->lock
);
1006 read_unlock_bh(&sec
->lock
);
1010 key
= llsec_lookup_key(sec
, &hdr
, &hdr
.source
, &key_id
);
1016 dev
= llsec_lookup_dev(sec
, &hdr
.source
);
1022 if (llsec_lookup_seclevel(sec
, hdr
.fc
.type
, 0, &seclevel
) < 0) {
1027 if (!(seclevel
.sec_levels
& BIT(hdr
.sec
.level
)) &&
1028 (hdr
.sec
.level
== 0 && seclevel
.device_override
&&
1029 !dev
->dev
.seclevel_exempt
)) {
1034 frame_ctr
= le32_to_cpu(hdr
.sec
.frame_counter
);
1036 if (frame_ctr
== 0xffffffff) {
1041 err
= llsec_update_devkey_info(dev
, &key_id
, frame_ctr
);
1045 dev_addr
= dev
->dev
.hwaddr
;
1049 err
= llsec_do_decrypt(skb
, sec
, &hdr
, key
, dev_addr
);