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 <net/ieee802154.h>
21 #include <crypto/algapi.h>
23 #include "mac802154.h"
26 static void llsec_key_put(struct mac802154_llsec_key
*key
);
27 static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id
*a
,
28 const struct ieee802154_llsec_key_id
*b
);
30 static void llsec_dev_free(struct mac802154_llsec_device
*dev
);
32 void mac802154_llsec_init(struct mac802154_llsec
*sec
)
34 memset(sec
, 0, sizeof(*sec
));
36 memset(&sec
->params
.default_key_source
, 0xFF, IEEE802154_ADDR_LEN
);
38 INIT_LIST_HEAD(&sec
->table
.security_levels
);
39 INIT_LIST_HEAD(&sec
->table
.devices
);
40 INIT_LIST_HEAD(&sec
->table
.keys
);
41 hash_init(sec
->devices_short
);
42 hash_init(sec
->devices_hw
);
43 rwlock_init(&sec
->lock
);
46 void mac802154_llsec_destroy(struct mac802154_llsec
*sec
)
48 struct ieee802154_llsec_seclevel
*sl
, *sn
;
49 struct ieee802154_llsec_device
*dev
, *dn
;
50 struct ieee802154_llsec_key_entry
*key
, *kn
;
52 list_for_each_entry_safe(sl
, sn
, &sec
->table
.security_levels
, list
) {
53 struct mac802154_llsec_seclevel
*msl
;
55 msl
= container_of(sl
, struct mac802154_llsec_seclevel
, level
);
60 list_for_each_entry_safe(dev
, dn
, &sec
->table
.devices
, list
) {
61 struct mac802154_llsec_device
*mdev
;
63 mdev
= container_of(dev
, struct mac802154_llsec_device
, dev
);
68 list_for_each_entry_safe(key
, kn
, &sec
->table
.keys
, list
) {
69 struct mac802154_llsec_key
*mkey
;
71 mkey
= container_of(key
->key
, struct mac802154_llsec_key
, key
);
80 int mac802154_llsec_get_params(struct mac802154_llsec
*sec
,
81 struct ieee802154_llsec_params
*params
)
83 read_lock_bh(&sec
->lock
);
84 *params
= sec
->params
;
85 read_unlock_bh(&sec
->lock
);
90 int mac802154_llsec_set_params(struct mac802154_llsec
*sec
,
91 const struct ieee802154_llsec_params
*params
,
94 write_lock_bh(&sec
->lock
);
96 if (changed
& IEEE802154_LLSEC_PARAM_ENABLED
)
97 sec
->params
.enabled
= params
->enabled
;
98 if (changed
& IEEE802154_LLSEC_PARAM_FRAME_COUNTER
)
99 sec
->params
.frame_counter
= params
->frame_counter
;
100 if (changed
& IEEE802154_LLSEC_PARAM_OUT_LEVEL
)
101 sec
->params
.out_level
= params
->out_level
;
102 if (changed
& IEEE802154_LLSEC_PARAM_OUT_KEY
)
103 sec
->params
.out_key
= params
->out_key
;
104 if (changed
& IEEE802154_LLSEC_PARAM_KEY_SOURCE
)
105 sec
->params
.default_key_source
= params
->default_key_source
;
106 if (changed
& IEEE802154_LLSEC_PARAM_PAN_ID
)
107 sec
->params
.pan_id
= params
->pan_id
;
108 if (changed
& IEEE802154_LLSEC_PARAM_HWADDR
)
109 sec
->params
.hwaddr
= params
->hwaddr
;
110 if (changed
& IEEE802154_LLSEC_PARAM_COORD_HWADDR
)
111 sec
->params
.coord_hwaddr
= params
->coord_hwaddr
;
112 if (changed
& IEEE802154_LLSEC_PARAM_COORD_SHORTADDR
)
113 sec
->params
.coord_shortaddr
= params
->coord_shortaddr
;
115 write_unlock_bh(&sec
->lock
);
122 static struct mac802154_llsec_key
*
123 llsec_key_alloc(const struct ieee802154_llsec_key
*template)
125 const int authsizes
[3] = { 4, 8, 16 };
126 struct mac802154_llsec_key
*key
;
129 key
= kzalloc(sizeof(*key
), GFP_KERNEL
);
133 kref_init(&key
->ref
);
134 key
->key
= *template;
136 BUILD_BUG_ON(ARRAY_SIZE(authsizes
) != ARRAY_SIZE(key
->tfm
));
138 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++) {
139 key
->tfm
[i
] = crypto_alloc_aead("ccm(aes)", 0,
143 if (crypto_aead_setkey(key
->tfm
[i
], template->key
,
144 IEEE802154_LLSEC_KEY_SIZE
))
146 if (crypto_aead_setauthsize(key
->tfm
[i
], authsizes
[i
]))
150 key
->tfm0
= crypto_alloc_blkcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC
);
154 if (crypto_blkcipher_setkey(key
->tfm0
, template->key
,
155 IEEE802154_LLSEC_KEY_SIZE
))
161 crypto_free_blkcipher(key
->tfm0
);
163 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++)
165 crypto_free_aead(key
->tfm
[i
]);
171 static void llsec_key_release(struct kref
*ref
)
173 struct mac802154_llsec_key
*key
;
176 key
= container_of(ref
, struct mac802154_llsec_key
, ref
);
178 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++)
179 crypto_free_aead(key
->tfm
[i
]);
181 crypto_free_blkcipher(key
->tfm0
);
185 static struct mac802154_llsec_key
*
186 llsec_key_get(struct mac802154_llsec_key
*key
)
192 static void llsec_key_put(struct mac802154_llsec_key
*key
)
194 kref_put(&key
->ref
, llsec_key_release
);
197 static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id
*a
,
198 const struct ieee802154_llsec_key_id
*b
)
200 if (a
->mode
!= b
->mode
)
203 if (a
->mode
== IEEE802154_SCF_KEY_IMPLICIT
)
204 return ieee802154_addr_equal(&a
->device_addr
, &b
->device_addr
);
210 case IEEE802154_SCF_KEY_INDEX
:
212 case IEEE802154_SCF_KEY_SHORT_INDEX
:
213 return a
->short_source
== b
->short_source
;
214 case IEEE802154_SCF_KEY_HW_INDEX
:
215 return a
->extended_source
== b
->extended_source
;
221 int mac802154_llsec_key_add(struct mac802154_llsec
*sec
,
222 const struct ieee802154_llsec_key_id
*id
,
223 const struct ieee802154_llsec_key
*key
)
225 struct mac802154_llsec_key
*mkey
= NULL
;
226 struct ieee802154_llsec_key_entry
*pos
, *new;
228 if (!(key
->frame_types
& (1 << IEEE802154_FC_TYPE_MAC_CMD
)) &&
232 list_for_each_entry(pos
, &sec
->table
.keys
, list
) {
233 if (llsec_key_id_equal(&pos
->id
, id
))
236 if (memcmp(pos
->key
->key
, key
->key
,
237 IEEE802154_LLSEC_KEY_SIZE
))
240 mkey
= container_of(pos
->key
, struct mac802154_llsec_key
, key
);
242 /* Don't allow multiple instances of the same AES key to have
243 * different allowed frame types/command frame ids, as this is
244 * not possible in the 802.15.4 PIB.
246 if (pos
->key
->frame_types
!= key
->frame_types
||
247 pos
->key
->cmd_frame_ids
!= key
->cmd_frame_ids
)
253 new = kzalloc(sizeof(*new), GFP_KERNEL
);
258 mkey
= llsec_key_alloc(key
);
260 mkey
= llsec_key_get(mkey
);
266 new->key
= &mkey
->key
;
268 list_add_rcu(&new->list
, &sec
->table
.keys
);
277 int mac802154_llsec_key_del(struct mac802154_llsec
*sec
,
278 const struct ieee802154_llsec_key_id
*key
)
280 struct ieee802154_llsec_key_entry
*pos
;
282 list_for_each_entry(pos
, &sec
->table
.keys
, list
) {
283 struct mac802154_llsec_key
*mkey
;
285 mkey
= container_of(pos
->key
, struct mac802154_llsec_key
, key
);
287 if (llsec_key_id_equal(&pos
->id
, key
)) {
288 list_del_rcu(&pos
->list
);
299 static bool llsec_dev_use_shortaddr(__le16 short_addr
)
301 return short_addr
!= cpu_to_le16(IEEE802154_ADDR_UNDEF
) &&
302 short_addr
!= cpu_to_le16(0xffff);
305 static u32
llsec_dev_hash_short(__le16 short_addr
, __le16 pan_id
)
307 return ((__force u16
) short_addr
) << 16 | (__force u16
) pan_id
;
310 static u64
llsec_dev_hash_long(__le64 hwaddr
)
312 return (__force u64
) hwaddr
;
315 static struct mac802154_llsec_device
*
316 llsec_dev_find_short(struct mac802154_llsec
*sec
, __le16 short_addr
,
319 struct mac802154_llsec_device
*dev
;
320 u32 key
= llsec_dev_hash_short(short_addr
, pan_id
);
322 hash_for_each_possible_rcu(sec
->devices_short
, dev
, bucket_s
, key
) {
323 if (dev
->dev
.short_addr
== short_addr
&&
324 dev
->dev
.pan_id
== pan_id
)
331 static struct mac802154_llsec_device
*
332 llsec_dev_find_long(struct mac802154_llsec
*sec
, __le64 hwaddr
)
334 struct mac802154_llsec_device
*dev
;
335 u64 key
= llsec_dev_hash_long(hwaddr
);
337 hash_for_each_possible_rcu(sec
->devices_hw
, dev
, bucket_hw
, key
) {
338 if (dev
->dev
.hwaddr
== hwaddr
)
345 static void llsec_dev_free(struct mac802154_llsec_device
*dev
)
347 struct ieee802154_llsec_device_key
*pos
, *pn
;
348 struct mac802154_llsec_device_key
*devkey
;
350 list_for_each_entry_safe(pos
, pn
, &dev
->dev
.keys
, list
) {
351 devkey
= container_of(pos
, struct mac802154_llsec_device_key
,
354 list_del(&pos
->list
);
361 int mac802154_llsec_dev_add(struct mac802154_llsec
*sec
,
362 const struct ieee802154_llsec_device
*dev
)
364 struct mac802154_llsec_device
*entry
;
365 u32 skey
= llsec_dev_hash_short(dev
->short_addr
, dev
->pan_id
);
366 u64 hwkey
= llsec_dev_hash_long(dev
->hwaddr
);
368 BUILD_BUG_ON(sizeof(hwkey
) != IEEE802154_ADDR_LEN
);
370 if ((llsec_dev_use_shortaddr(dev
->short_addr
) &&
371 llsec_dev_find_short(sec
, dev
->short_addr
, dev
->pan_id
)) ||
372 llsec_dev_find_long(sec
, dev
->hwaddr
))
375 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
380 spin_lock_init(&entry
->lock
);
381 INIT_LIST_HEAD(&entry
->dev
.keys
);
383 if (llsec_dev_use_shortaddr(dev
->short_addr
))
384 hash_add_rcu(sec
->devices_short
, &entry
->bucket_s
, skey
);
386 INIT_HLIST_NODE(&entry
->bucket_s
);
388 hash_add_rcu(sec
->devices_hw
, &entry
->bucket_hw
, hwkey
);
389 list_add_tail_rcu(&entry
->dev
.list
, &sec
->table
.devices
);
394 static void llsec_dev_free_rcu(struct rcu_head
*rcu
)
396 llsec_dev_free(container_of(rcu
, struct mac802154_llsec_device
, rcu
));
399 int mac802154_llsec_dev_del(struct mac802154_llsec
*sec
, __le64 device_addr
)
401 struct mac802154_llsec_device
*pos
;
403 pos
= llsec_dev_find_long(sec
, device_addr
);
407 hash_del_rcu(&pos
->bucket_s
);
408 hash_del_rcu(&pos
->bucket_hw
);
409 call_rcu(&pos
->rcu
, llsec_dev_free_rcu
);
416 static struct mac802154_llsec_device_key
*
417 llsec_devkey_find(struct mac802154_llsec_device
*dev
,
418 const struct ieee802154_llsec_key_id
*key
)
420 struct ieee802154_llsec_device_key
*devkey
;
422 list_for_each_entry_rcu(devkey
, &dev
->dev
.keys
, list
) {
423 if (!llsec_key_id_equal(key
, &devkey
->key_id
))
426 return container_of(devkey
, struct mac802154_llsec_device_key
,
433 int mac802154_llsec_devkey_add(struct mac802154_llsec
*sec
,
435 const struct ieee802154_llsec_device_key
*key
)
437 struct mac802154_llsec_device
*dev
;
438 struct mac802154_llsec_device_key
*devkey
;
440 dev
= llsec_dev_find_long(sec
, dev_addr
);
445 if (llsec_devkey_find(dev
, &key
->key_id
))
448 devkey
= kmalloc(sizeof(*devkey
), GFP_KERNEL
);
452 devkey
->devkey
= *key
;
453 list_add_tail_rcu(&devkey
->devkey
.list
, &dev
->dev
.keys
);
457 int mac802154_llsec_devkey_del(struct mac802154_llsec
*sec
,
459 const struct ieee802154_llsec_device_key
*key
)
461 struct mac802154_llsec_device
*dev
;
462 struct mac802154_llsec_device_key
*devkey
;
464 dev
= llsec_dev_find_long(sec
, dev_addr
);
469 devkey
= llsec_devkey_find(dev
, &key
->key_id
);
473 list_del_rcu(&devkey
->devkey
.list
);
474 kfree_rcu(devkey
, rcu
);
480 static struct mac802154_llsec_seclevel
*
481 llsec_find_seclevel(const struct mac802154_llsec
*sec
,
482 const struct ieee802154_llsec_seclevel
*sl
)
484 struct ieee802154_llsec_seclevel
*pos
;
486 list_for_each_entry(pos
, &sec
->table
.security_levels
, list
) {
487 if (pos
->frame_type
!= sl
->frame_type
||
488 (pos
->frame_type
== IEEE802154_FC_TYPE_MAC_CMD
&&
489 pos
->cmd_frame_id
!= sl
->cmd_frame_id
) ||
490 pos
->device_override
!= sl
->device_override
||
491 pos
->sec_levels
!= sl
->sec_levels
)
494 return container_of(pos
, struct mac802154_llsec_seclevel
,
501 int mac802154_llsec_seclevel_add(struct mac802154_llsec
*sec
,
502 const struct ieee802154_llsec_seclevel
*sl
)
504 struct mac802154_llsec_seclevel
*entry
;
506 if (llsec_find_seclevel(sec
, sl
))
509 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
515 list_add_tail_rcu(&entry
->level
.list
, &sec
->table
.security_levels
);
520 int mac802154_llsec_seclevel_del(struct mac802154_llsec
*sec
,
521 const struct ieee802154_llsec_seclevel
*sl
)
523 struct mac802154_llsec_seclevel
*pos
;
525 pos
= llsec_find_seclevel(sec
, sl
);
529 list_del_rcu(&pos
->level
.list
);
537 static int llsec_recover_addr(struct mac802154_llsec
*sec
,
538 struct ieee802154_addr
*addr
)
540 __le16 caddr
= sec
->params
.coord_shortaddr
;
542 addr
->pan_id
= sec
->params
.pan_id
;
544 if (caddr
== cpu_to_le16(IEEE802154_ADDR_BROADCAST
)) {
546 } else if (caddr
== cpu_to_le16(IEEE802154_ADDR_UNDEF
)) {
547 addr
->extended_addr
= sec
->params
.coord_hwaddr
;
548 addr
->mode
= IEEE802154_ADDR_LONG
;
550 addr
->short_addr
= sec
->params
.coord_shortaddr
;
551 addr
->mode
= IEEE802154_ADDR_SHORT
;
557 static struct mac802154_llsec_key
*
558 llsec_lookup_key(struct mac802154_llsec
*sec
,
559 const struct ieee802154_hdr
*hdr
,
560 const struct ieee802154_addr
*addr
,
561 struct ieee802154_llsec_key_id
*key_id
)
563 struct ieee802154_addr devaddr
= *addr
;
564 u8 key_id_mode
= hdr
->sec
.key_id_mode
;
565 struct ieee802154_llsec_key_entry
*key_entry
;
566 struct mac802154_llsec_key
*key
;
568 if (key_id_mode
== IEEE802154_SCF_KEY_IMPLICIT
&&
569 devaddr
.mode
== IEEE802154_ADDR_NONE
) {
570 if (hdr
->fc
.type
== IEEE802154_FC_TYPE_BEACON
) {
571 devaddr
.extended_addr
= sec
->params
.coord_hwaddr
;
572 devaddr
.mode
= IEEE802154_ADDR_LONG
;
573 } else if (llsec_recover_addr(sec
, &devaddr
) < 0) {
578 list_for_each_entry_rcu(key_entry
, &sec
->table
.keys
, list
) {
579 const struct ieee802154_llsec_key_id
*id
= &key_entry
->id
;
581 if (!(key_entry
->key
->frame_types
& BIT(hdr
->fc
.type
)))
584 if (id
->mode
!= key_id_mode
)
587 if (key_id_mode
== IEEE802154_SCF_KEY_IMPLICIT
) {
588 if (ieee802154_addr_equal(&devaddr
, &id
->device_addr
))
591 if (id
->id
!= hdr
->sec
.key_id
)
594 if ((key_id_mode
== IEEE802154_SCF_KEY_INDEX
) ||
595 (key_id_mode
== IEEE802154_SCF_KEY_SHORT_INDEX
&&
596 id
->short_source
== hdr
->sec
.short_src
) ||
597 (key_id_mode
== IEEE802154_SCF_KEY_HW_INDEX
&&
598 id
->extended_source
== hdr
->sec
.extended_src
))
606 key
= container_of(key_entry
->key
, struct mac802154_llsec_key
, key
);
608 *key_id
= key_entry
->id
;
609 return llsec_key_get(key
);
613 static void llsec_geniv(u8 iv
[16], __le64 addr
,
614 const struct ieee802154_sechdr
*sec
)
616 __be64 addr_bytes
= (__force __be64
) swab64((__force u64
) addr
);
617 __be32 frame_counter
= (__force __be32
) swab32((__force u32
) sec
->frame_counter
);
619 iv
[0] = 1; /* L' = L - 1 = 1 */
620 memcpy(iv
+ 1, &addr_bytes
, sizeof(addr_bytes
));
621 memcpy(iv
+ 9, &frame_counter
, sizeof(frame_counter
));
628 llsec_do_encrypt_unauth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
629 const struct ieee802154_hdr
*hdr
,
630 struct mac802154_llsec_key
*key
)
633 struct scatterlist src
;
634 struct blkcipher_desc req
= {
640 llsec_geniv(iv
, sec
->params
.hwaddr
, &hdr
->sec
);
641 sg_init_one(&src
, skb
->data
, skb
->len
);
642 return crypto_blkcipher_encrypt_iv(&req
, &src
, &src
, skb
->len
);
645 static struct crypto_aead
*
646 llsec_tfm_by_len(struct mac802154_llsec_key
*key
, int authlen
)
650 for (i
= 0; i
< ARRAY_SIZE(key
->tfm
); i
++)
651 if (crypto_aead_authsize(key
->tfm
[i
]) == authlen
)
658 llsec_do_encrypt_auth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
659 const struct ieee802154_hdr
*hdr
,
660 struct mac802154_llsec_key
*key
)
664 int authlen
, assoclen
, datalen
, rc
;
665 struct scatterlist src
, assoc
[2], dst
[2];
666 struct aead_request
*req
;
668 authlen
= ieee802154_sechdr_authtag_len(&hdr
->sec
);
669 llsec_geniv(iv
, sec
->params
.hwaddr
, &hdr
->sec
);
671 req
= aead_request_alloc(llsec_tfm_by_len(key
, authlen
), GFP_ATOMIC
);
675 sg_init_table(assoc
, 2);
676 sg_set_buf(&assoc
[0], skb_mac_header(skb
), skb
->mac_len
);
677 assoclen
= skb
->mac_len
;
679 data
= skb_mac_header(skb
) + skb
->mac_len
;
680 datalen
= skb_tail_pointer(skb
) - data
;
682 if (hdr
->sec
.level
& IEEE802154_SCF_SECLEVEL_ENC
) {
683 sg_set_buf(&assoc
[1], data
, 0);
685 sg_set_buf(&assoc
[1], data
, datalen
);
690 sg_init_one(&src
, data
, datalen
);
692 sg_init_table(dst
, 2);
693 sg_set_buf(&dst
[0], data
, datalen
);
694 sg_set_buf(&dst
[1], skb_put(skb
, authlen
), authlen
);
696 aead_request_set_callback(req
, 0, NULL
, NULL
);
697 aead_request_set_assoc(req
, assoc
, assoclen
);
698 aead_request_set_crypt(req
, &src
, dst
, datalen
, iv
);
700 rc
= crypto_aead_encrypt(req
);
707 static int llsec_do_encrypt(struct sk_buff
*skb
,
708 const struct mac802154_llsec
*sec
,
709 const struct ieee802154_hdr
*hdr
,
710 struct mac802154_llsec_key
*key
)
712 if (hdr
->sec
.level
== IEEE802154_SCF_SECLEVEL_ENC
)
713 return llsec_do_encrypt_unauth(skb
, sec
, hdr
, key
);
715 return llsec_do_encrypt_auth(skb
, sec
, hdr
, key
);
718 int mac802154_llsec_encrypt(struct mac802154_llsec
*sec
, struct sk_buff
*skb
)
720 struct ieee802154_hdr hdr
;
721 int rc
, authlen
, hlen
;
722 struct mac802154_llsec_key
*key
;
725 hlen
= ieee802154_hdr_pull(skb
, &hdr
);
727 if (hlen
< 0 || hdr
.fc
.type
!= IEEE802154_FC_TYPE_DATA
)
730 if (!hdr
.fc
.security_enabled
|| hdr
.sec
.level
== 0) {
735 authlen
= ieee802154_sechdr_authtag_len(&hdr
.sec
);
737 if (skb
->len
+ hlen
+ authlen
+ IEEE802154_MFR_SIZE
> IEEE802154_MTU
)
742 read_lock_bh(&sec
->lock
);
744 if (!sec
->params
.enabled
) {
749 key
= llsec_lookup_key(sec
, &hdr
, &hdr
.dest
, NULL
);
755 read_unlock_bh(&sec
->lock
);
757 write_lock_bh(&sec
->lock
);
759 frame_ctr
= be32_to_cpu(sec
->params
.frame_counter
);
760 hdr
.sec
.frame_counter
= cpu_to_le32(frame_ctr
);
761 if (frame_ctr
== 0xFFFFFFFF) {
762 write_unlock_bh(&sec
->lock
);
768 sec
->params
.frame_counter
= cpu_to_be32(frame_ctr
+ 1);
770 write_unlock_bh(&sec
->lock
);
774 skb
->mac_len
= ieee802154_hdr_push(skb
, &hdr
);
775 skb_reset_mac_header(skb
);
777 rc
= llsec_do_encrypt(skb
, sec
, &hdr
, key
);
783 read_unlock_bh(&sec
->lock
);
791 static struct mac802154_llsec_device
*
792 llsec_lookup_dev(struct mac802154_llsec
*sec
,
793 const struct ieee802154_addr
*addr
)
795 struct ieee802154_addr devaddr
= *addr
;
796 struct mac802154_llsec_device
*dev
= NULL
;
798 if (devaddr
.mode
== IEEE802154_ADDR_NONE
&&
799 llsec_recover_addr(sec
, &devaddr
) < 0)
802 if (devaddr
.mode
== IEEE802154_ADDR_SHORT
) {
803 u32 key
= llsec_dev_hash_short(devaddr
.short_addr
,
806 hash_for_each_possible_rcu(sec
->devices_short
, dev
,
808 if (dev
->dev
.pan_id
== devaddr
.pan_id
&&
809 dev
->dev
.short_addr
== devaddr
.short_addr
)
813 u64 key
= llsec_dev_hash_long(devaddr
.extended_addr
);
815 hash_for_each_possible_rcu(sec
->devices_hw
, dev
,
817 if (dev
->dev
.hwaddr
== devaddr
.extended_addr
)
826 llsec_lookup_seclevel(const struct mac802154_llsec
*sec
,
827 u8 frame_type
, u8 cmd_frame_id
,
828 struct ieee802154_llsec_seclevel
*rlevel
)
830 struct ieee802154_llsec_seclevel
*level
;
832 list_for_each_entry_rcu(level
, &sec
->table
.security_levels
, list
) {
833 if (level
->frame_type
== frame_type
&&
834 (frame_type
!= IEEE802154_FC_TYPE_MAC_CMD
||
835 level
->cmd_frame_id
== cmd_frame_id
)) {
845 llsec_do_decrypt_unauth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
846 const struct ieee802154_hdr
*hdr
,
847 struct mac802154_llsec_key
*key
, __le64 dev_addr
)
852 struct scatterlist src
;
853 struct blkcipher_desc req
= {
859 llsec_geniv(iv
, dev_addr
, &hdr
->sec
);
860 data
= skb_mac_header(skb
) + skb
->mac_len
;
861 datalen
= skb_tail_pointer(skb
) - data
;
863 sg_init_one(&src
, data
, datalen
);
865 return crypto_blkcipher_decrypt_iv(&req
, &src
, &src
, datalen
);
869 llsec_do_decrypt_auth(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
870 const struct ieee802154_hdr
*hdr
,
871 struct mac802154_llsec_key
*key
, __le64 dev_addr
)
875 int authlen
, datalen
, assoclen
, rc
;
876 struct scatterlist src
, assoc
[2];
877 struct aead_request
*req
;
879 authlen
= ieee802154_sechdr_authtag_len(&hdr
->sec
);
880 llsec_geniv(iv
, dev_addr
, &hdr
->sec
);
882 req
= aead_request_alloc(llsec_tfm_by_len(key
, authlen
), GFP_ATOMIC
);
886 sg_init_table(assoc
, 2);
887 sg_set_buf(&assoc
[0], skb_mac_header(skb
), skb
->mac_len
);
888 assoclen
= skb
->mac_len
;
890 data
= skb_mac_header(skb
) + skb
->mac_len
;
891 datalen
= skb_tail_pointer(skb
) - data
;
893 if (hdr
->sec
.level
& IEEE802154_SCF_SECLEVEL_ENC
) {
894 sg_set_buf(&assoc
[1], data
, 0);
896 sg_set_buf(&assoc
[1], data
, datalen
- authlen
);
897 assoclen
+= datalen
- authlen
;
898 data
+= datalen
- authlen
;
902 sg_init_one(&src
, data
, datalen
);
904 aead_request_set_callback(req
, 0, NULL
, NULL
);
905 aead_request_set_assoc(req
, assoc
, assoclen
);
906 aead_request_set_crypt(req
, &src
, &src
, datalen
, iv
);
908 rc
= crypto_aead_decrypt(req
);
911 skb_trim(skb
, skb
->len
- authlen
);
917 llsec_do_decrypt(struct sk_buff
*skb
, const struct mac802154_llsec
*sec
,
918 const struct ieee802154_hdr
*hdr
,
919 struct mac802154_llsec_key
*key
, __le64 dev_addr
)
921 if (hdr
->sec
.level
== IEEE802154_SCF_SECLEVEL_ENC
)
922 return llsec_do_decrypt_unauth(skb
, sec
, hdr
, key
, dev_addr
);
924 return llsec_do_decrypt_auth(skb
, sec
, hdr
, key
, dev_addr
);
928 llsec_update_devkey_record(struct mac802154_llsec_device
*dev
,
929 const struct ieee802154_llsec_key_id
*in_key
)
931 struct mac802154_llsec_device_key
*devkey
;
933 devkey
= llsec_devkey_find(dev
, in_key
);
936 struct mac802154_llsec_device_key
*next
;
938 next
= kzalloc(sizeof(*devkey
), GFP_ATOMIC
);
942 next
->devkey
.key_id
= *in_key
;
944 spin_lock_bh(&dev
->lock
);
946 devkey
= llsec_devkey_find(dev
, in_key
);
948 list_add_rcu(&next
->devkey
.list
, &dev
->dev
.keys
);
952 spin_unlock_bh(&dev
->lock
);
959 llsec_update_devkey_info(struct mac802154_llsec_device
*dev
,
960 const struct ieee802154_llsec_key_id
*in_key
,
963 struct mac802154_llsec_device_key
*devkey
= NULL
;
965 if (dev
->dev
.key_mode
== IEEE802154_LLSEC_DEVKEY_RESTRICT
) {
966 devkey
= llsec_devkey_find(dev
, in_key
);
971 if (dev
->dev
.key_mode
== IEEE802154_LLSEC_DEVKEY_RECORD
) {
972 int rc
= llsec_update_devkey_record(dev
, in_key
);
978 spin_lock_bh(&dev
->lock
);
980 if ((!devkey
&& frame_counter
< dev
->dev
.frame_counter
) ||
981 (devkey
&& frame_counter
< devkey
->devkey
.frame_counter
)) {
982 spin_unlock_bh(&dev
->lock
);
987 devkey
->devkey
.frame_counter
= frame_counter
+ 1;
989 dev
->dev
.frame_counter
= frame_counter
+ 1;
991 spin_unlock_bh(&dev
->lock
);
996 int mac802154_llsec_decrypt(struct mac802154_llsec
*sec
, struct sk_buff
*skb
)
998 struct ieee802154_hdr hdr
;
999 struct mac802154_llsec_key
*key
;
1000 struct ieee802154_llsec_key_id key_id
;
1001 struct mac802154_llsec_device
*dev
;
1002 struct ieee802154_llsec_seclevel seclevel
;
1007 if (ieee802154_hdr_peek(skb
, &hdr
) < 0)
1009 if (!hdr
.fc
.security_enabled
)
1011 if (hdr
.fc
.version
== 0)
1014 read_lock_bh(&sec
->lock
);
1015 if (!sec
->params
.enabled
) {
1016 read_unlock_bh(&sec
->lock
);
1019 read_unlock_bh(&sec
->lock
);
1023 key
= llsec_lookup_key(sec
, &hdr
, &hdr
.source
, &key_id
);
1029 dev
= llsec_lookup_dev(sec
, &hdr
.source
);
1035 if (llsec_lookup_seclevel(sec
, hdr
.fc
.type
, 0, &seclevel
) < 0) {
1040 if (!(seclevel
.sec_levels
& BIT(hdr
.sec
.level
)) &&
1041 (hdr
.sec
.level
== 0 && seclevel
.device_override
&&
1042 !dev
->dev
.seclevel_exempt
)) {
1047 frame_ctr
= le32_to_cpu(hdr
.sec
.frame_counter
);
1049 if (frame_ctr
== 0xffffffff) {
1054 err
= llsec_update_devkey_info(dev
, &key_id
, frame_ctr
);
1058 dev_addr
= dev
->dev
.hwaddr
;
1062 err
= llsec_do_decrypt(skb
, sec
, &hdr
, key
, dev_addr
);