xref: /linux/net/mac802154/llsec.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright (C) 2014 Fraunhofer ITWM
3  *
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.
7  *
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.
12  *
13  * Written by:
14  * Phoebe Buckheister <phoebe.buckheister@itwm.fraunhofer.de>
15  */
16 
17 #include <linux/err.h>
18 #include <linux/bug.h>
19 #include <linux/completion.h>
20 #include <linux/crypto.h>
21 #include <linux/ieee802154.h>
22 #include <crypto/aead.h>
23 
24 #include "ieee802154_i.h"
25 #include "llsec.h"
26 
27 static void llsec_key_put(struct mac802154_llsec_key *key);
28 static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id *a,
29 			       const struct ieee802154_llsec_key_id *b);
30 
31 static void llsec_dev_free(struct mac802154_llsec_device *dev);
32 
33 void mac802154_llsec_init(struct mac802154_llsec *sec)
34 {
35 	memset(sec, 0, sizeof(*sec));
36 
37 	memset(&sec->params.default_key_source, 0xFF, IEEE802154_ADDR_LEN);
38 
39 	INIT_LIST_HEAD(&sec->table.security_levels);
40 	INIT_LIST_HEAD(&sec->table.devices);
41 	INIT_LIST_HEAD(&sec->table.keys);
42 	hash_init(sec->devices_short);
43 	hash_init(sec->devices_hw);
44 	rwlock_init(&sec->lock);
45 }
46 
47 void mac802154_llsec_destroy(struct mac802154_llsec *sec)
48 {
49 	struct ieee802154_llsec_seclevel *sl, *sn;
50 	struct ieee802154_llsec_device *dev, *dn;
51 	struct ieee802154_llsec_key_entry *key, *kn;
52 
53 	list_for_each_entry_safe(sl, sn, &sec->table.security_levels, list) {
54 		struct mac802154_llsec_seclevel *msl;
55 
56 		msl = container_of(sl, struct mac802154_llsec_seclevel, level);
57 		list_del(&sl->list);
58 		kfree(msl);
59 	}
60 
61 	list_for_each_entry_safe(dev, dn, &sec->table.devices, list) {
62 		struct mac802154_llsec_device *mdev;
63 
64 		mdev = container_of(dev, struct mac802154_llsec_device, dev);
65 		list_del(&dev->list);
66 		llsec_dev_free(mdev);
67 	}
68 
69 	list_for_each_entry_safe(key, kn, &sec->table.keys, list) {
70 		struct mac802154_llsec_key *mkey;
71 
72 		mkey = container_of(key->key, struct mac802154_llsec_key, key);
73 		list_del(&key->list);
74 		llsec_key_put(mkey);
75 		kfree(key);
76 	}
77 }
78 
79 int mac802154_llsec_get_params(struct mac802154_llsec *sec,
80 			       struct ieee802154_llsec_params *params)
81 {
82 	read_lock_bh(&sec->lock);
83 	*params = sec->params;
84 	read_unlock_bh(&sec->lock);
85 
86 	return 0;
87 }
88 
89 int mac802154_llsec_set_params(struct mac802154_llsec *sec,
90 			       const struct ieee802154_llsec_params *params,
91 			       int changed)
92 {
93 	write_lock_bh(&sec->lock);
94 
95 	if (changed & IEEE802154_LLSEC_PARAM_ENABLED)
96 		sec->params.enabled = params->enabled;
97 	if (changed & IEEE802154_LLSEC_PARAM_FRAME_COUNTER)
98 		sec->params.frame_counter = params->frame_counter;
99 	if (changed & IEEE802154_LLSEC_PARAM_OUT_LEVEL)
100 		sec->params.out_level = params->out_level;
101 	if (changed & IEEE802154_LLSEC_PARAM_OUT_KEY)
102 		sec->params.out_key = params->out_key;
103 	if (changed & IEEE802154_LLSEC_PARAM_KEY_SOURCE)
104 		sec->params.default_key_source = params->default_key_source;
105 	if (changed & IEEE802154_LLSEC_PARAM_PAN_ID)
106 		sec->params.pan_id = params->pan_id;
107 	if (changed & IEEE802154_LLSEC_PARAM_HWADDR)
108 		sec->params.hwaddr = params->hwaddr;
109 	if (changed & IEEE802154_LLSEC_PARAM_COORD_HWADDR)
110 		sec->params.coord_hwaddr = params->coord_hwaddr;
111 	if (changed & IEEE802154_LLSEC_PARAM_COORD_SHORTADDR)
112 		sec->params.coord_shortaddr = params->coord_shortaddr;
113 
114 	write_unlock_bh(&sec->lock);
115 
116 	return 0;
117 }
118 
119 static struct mac802154_llsec_key*
120 llsec_key_alloc(const struct ieee802154_llsec_key *template)
121 {
122 	const int authsizes[3] = { 4, 8, 16 };
123 	struct mac802154_llsec_key *key;
124 	int i;
125 
126 	key = kzalloc(sizeof(*key), GFP_KERNEL);
127 	if (!key)
128 		return NULL;
129 
130 	kref_init(&key->ref);
131 	key->key = *template;
132 
133 	BUILD_BUG_ON(ARRAY_SIZE(authsizes) != ARRAY_SIZE(key->tfm));
134 
135 	for (i = 0; i < ARRAY_SIZE(key->tfm); i++) {
136 		key->tfm[i] = crypto_alloc_aead("ccm(aes)", 0,
137 						CRYPTO_ALG_ASYNC);
138 		if (IS_ERR(key->tfm[i]))
139 			goto err_tfm;
140 		if (crypto_aead_setkey(key->tfm[i], template->key,
141 				       IEEE802154_LLSEC_KEY_SIZE))
142 			goto err_tfm;
143 		if (crypto_aead_setauthsize(key->tfm[i], authsizes[i]))
144 			goto err_tfm;
145 	}
146 
147 	key->tfm0 = crypto_alloc_blkcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC);
148 	if (IS_ERR(key->tfm0))
149 		goto err_tfm;
150 
151 	if (crypto_blkcipher_setkey(key->tfm0, template->key,
152 				    IEEE802154_LLSEC_KEY_SIZE))
153 		goto err_tfm0;
154 
155 	return key;
156 
157 err_tfm0:
158 	crypto_free_blkcipher(key->tfm0);
159 err_tfm:
160 	for (i = 0; i < ARRAY_SIZE(key->tfm); i++)
161 		if (key->tfm[i])
162 			crypto_free_aead(key->tfm[i]);
163 
164 	kfree(key);
165 	return NULL;
166 }
167 
168 static void llsec_key_release(struct kref *ref)
169 {
170 	struct mac802154_llsec_key *key;
171 	int i;
172 
173 	key = container_of(ref, struct mac802154_llsec_key, ref);
174 
175 	for (i = 0; i < ARRAY_SIZE(key->tfm); i++)
176 		crypto_free_aead(key->tfm[i]);
177 
178 	crypto_free_blkcipher(key->tfm0);
179 	kfree(key);
180 }
181 
182 static struct mac802154_llsec_key*
183 llsec_key_get(struct mac802154_llsec_key *key)
184 {
185 	kref_get(&key->ref);
186 	return key;
187 }
188 
189 static void llsec_key_put(struct mac802154_llsec_key *key)
190 {
191 	kref_put(&key->ref, llsec_key_release);
192 }
193 
194 static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id *a,
195 			       const struct ieee802154_llsec_key_id *b)
196 {
197 	if (a->mode != b->mode)
198 		return false;
199 
200 	if (a->mode == IEEE802154_SCF_KEY_IMPLICIT)
201 		return ieee802154_addr_equal(&a->device_addr, &b->device_addr);
202 
203 	if (a->id != b->id)
204 		return false;
205 
206 	switch (a->mode) {
207 	case IEEE802154_SCF_KEY_INDEX:
208 		return true;
209 	case IEEE802154_SCF_KEY_SHORT_INDEX:
210 		return a->short_source == b->short_source;
211 	case IEEE802154_SCF_KEY_HW_INDEX:
212 		return a->extended_source == b->extended_source;
213 	}
214 
215 	return false;
216 }
217 
218 int mac802154_llsec_key_add(struct mac802154_llsec *sec,
219 			    const struct ieee802154_llsec_key_id *id,
220 			    const struct ieee802154_llsec_key *key)
221 {
222 	struct mac802154_llsec_key *mkey = NULL;
223 	struct ieee802154_llsec_key_entry *pos, *new;
224 
225 	if (!(key->frame_types & (1 << IEEE802154_FC_TYPE_MAC_CMD)) &&
226 	    key->cmd_frame_ids)
227 		return -EINVAL;
228 
229 	list_for_each_entry(pos, &sec->table.keys, list) {
230 		if (llsec_key_id_equal(&pos->id, id))
231 			return -EEXIST;
232 
233 		if (memcmp(pos->key->key, key->key,
234 			   IEEE802154_LLSEC_KEY_SIZE))
235 			continue;
236 
237 		mkey = container_of(pos->key, struct mac802154_llsec_key, key);
238 
239 		/* Don't allow multiple instances of the same AES key to have
240 		 * different allowed frame types/command frame ids, as this is
241 		 * not possible in the 802.15.4 PIB.
242 		 */
243 		if (pos->key->frame_types != key->frame_types ||
244 		    pos->key->cmd_frame_ids != key->cmd_frame_ids)
245 			return -EEXIST;
246 
247 		break;
248 	}
249 
250 	new = kzalloc(sizeof(*new), GFP_KERNEL);
251 	if (!new)
252 		return -ENOMEM;
253 
254 	if (!mkey)
255 		mkey = llsec_key_alloc(key);
256 	else
257 		mkey = llsec_key_get(mkey);
258 
259 	if (!mkey)
260 		goto fail;
261 
262 	new->id = *id;
263 	new->key = &mkey->key;
264 
265 	list_add_rcu(&new->list, &sec->table.keys);
266 
267 	return 0;
268 
269 fail:
270 	kfree(new);
271 	return -ENOMEM;
272 }
273 
274 int mac802154_llsec_key_del(struct mac802154_llsec *sec,
275 			    const struct ieee802154_llsec_key_id *key)
276 {
277 	struct ieee802154_llsec_key_entry *pos;
278 
279 	list_for_each_entry(pos, &sec->table.keys, list) {
280 		struct mac802154_llsec_key *mkey;
281 
282 		mkey = container_of(pos->key, struct mac802154_llsec_key, key);
283 
284 		if (llsec_key_id_equal(&pos->id, key)) {
285 			list_del_rcu(&pos->list);
286 			llsec_key_put(mkey);
287 			return 0;
288 		}
289 	}
290 
291 	return -ENOENT;
292 }
293 
294 static bool llsec_dev_use_shortaddr(__le16 short_addr)
295 {
296 	return short_addr != cpu_to_le16(IEEE802154_ADDR_UNDEF) &&
297 		short_addr != cpu_to_le16(0xffff);
298 }
299 
300 static u32 llsec_dev_hash_short(__le16 short_addr, __le16 pan_id)
301 {
302 	return ((__force u16)short_addr) << 16 | (__force u16)pan_id;
303 }
304 
305 static u64 llsec_dev_hash_long(__le64 hwaddr)
306 {
307 	return (__force u64)hwaddr;
308 }
309 
310 static struct mac802154_llsec_device*
311 llsec_dev_find_short(struct mac802154_llsec *sec, __le16 short_addr,
312 		     __le16 pan_id)
313 {
314 	struct mac802154_llsec_device *dev;
315 	u32 key = llsec_dev_hash_short(short_addr, pan_id);
316 
317 	hash_for_each_possible_rcu(sec->devices_short, dev, bucket_s, key) {
318 		if (dev->dev.short_addr == short_addr &&
319 		    dev->dev.pan_id == pan_id)
320 			return dev;
321 	}
322 
323 	return NULL;
324 }
325 
326 static struct mac802154_llsec_device*
327 llsec_dev_find_long(struct mac802154_llsec *sec, __le64 hwaddr)
328 {
329 	struct mac802154_llsec_device *dev;
330 	u64 key = llsec_dev_hash_long(hwaddr);
331 
332 	hash_for_each_possible_rcu(sec->devices_hw, dev, bucket_hw, key) {
333 		if (dev->dev.hwaddr == hwaddr)
334 			return dev;
335 	}
336 
337 	return NULL;
338 }
339 
340 static void llsec_dev_free(struct mac802154_llsec_device *dev)
341 {
342 	struct ieee802154_llsec_device_key *pos, *pn;
343 	struct mac802154_llsec_device_key *devkey;
344 
345 	list_for_each_entry_safe(pos, pn, &dev->dev.keys, list) {
346 		devkey = container_of(pos, struct mac802154_llsec_device_key,
347 				      devkey);
348 
349 		list_del(&pos->list);
350 		kfree(devkey);
351 	}
352 
353 	kfree(dev);
354 }
355 
356 int mac802154_llsec_dev_add(struct mac802154_llsec *sec,
357 			    const struct ieee802154_llsec_device *dev)
358 {
359 	struct mac802154_llsec_device *entry;
360 	u32 skey = llsec_dev_hash_short(dev->short_addr, dev->pan_id);
361 	u64 hwkey = llsec_dev_hash_long(dev->hwaddr);
362 
363 	BUILD_BUG_ON(sizeof(hwkey) != IEEE802154_ADDR_LEN);
364 
365 	if ((llsec_dev_use_shortaddr(dev->short_addr) &&
366 	     llsec_dev_find_short(sec, dev->short_addr, dev->pan_id)) ||
367 	     llsec_dev_find_long(sec, dev->hwaddr))
368 		return -EEXIST;
369 
370 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
371 	if (!entry)
372 		return -ENOMEM;
373 
374 	entry->dev = *dev;
375 	spin_lock_init(&entry->lock);
376 	INIT_LIST_HEAD(&entry->dev.keys);
377 
378 	if (llsec_dev_use_shortaddr(dev->short_addr))
379 		hash_add_rcu(sec->devices_short, &entry->bucket_s, skey);
380 	else
381 		INIT_HLIST_NODE(&entry->bucket_s);
382 
383 	hash_add_rcu(sec->devices_hw, &entry->bucket_hw, hwkey);
384 	list_add_tail_rcu(&entry->dev.list, &sec->table.devices);
385 
386 	return 0;
387 }
388 
389 static void llsec_dev_free_rcu(struct rcu_head *rcu)
390 {
391 	llsec_dev_free(container_of(rcu, struct mac802154_llsec_device, rcu));
392 }
393 
394 int mac802154_llsec_dev_del(struct mac802154_llsec *sec, __le64 device_addr)
395 {
396 	struct mac802154_llsec_device *pos;
397 
398 	pos = llsec_dev_find_long(sec, device_addr);
399 	if (!pos)
400 		return -ENOENT;
401 
402 	hash_del_rcu(&pos->bucket_s);
403 	hash_del_rcu(&pos->bucket_hw);
404 	call_rcu(&pos->rcu, llsec_dev_free_rcu);
405 
406 	return 0;
407 }
408 
409 static struct mac802154_llsec_device_key*
410 llsec_devkey_find(struct mac802154_llsec_device *dev,
411 		  const struct ieee802154_llsec_key_id *key)
412 {
413 	struct ieee802154_llsec_device_key *devkey;
414 
415 	list_for_each_entry_rcu(devkey, &dev->dev.keys, list) {
416 		if (!llsec_key_id_equal(key, &devkey->key_id))
417 			continue;
418 
419 		return container_of(devkey, struct mac802154_llsec_device_key,
420 				    devkey);
421 	}
422 
423 	return NULL;
424 }
425 
426 int mac802154_llsec_devkey_add(struct mac802154_llsec *sec,
427 			       __le64 dev_addr,
428 			       const struct ieee802154_llsec_device_key *key)
429 {
430 	struct mac802154_llsec_device *dev;
431 	struct mac802154_llsec_device_key *devkey;
432 
433 	dev = llsec_dev_find_long(sec, dev_addr);
434 
435 	if (!dev)
436 		return -ENOENT;
437 
438 	if (llsec_devkey_find(dev, &key->key_id))
439 		return -EEXIST;
440 
441 	devkey = kmalloc(sizeof(*devkey), GFP_KERNEL);
442 	if (!devkey)
443 		return -ENOMEM;
444 
445 	devkey->devkey = *key;
446 	list_add_tail_rcu(&devkey->devkey.list, &dev->dev.keys);
447 	return 0;
448 }
449 
450 int mac802154_llsec_devkey_del(struct mac802154_llsec *sec,
451 			       __le64 dev_addr,
452 			       const struct ieee802154_llsec_device_key *key)
453 {
454 	struct mac802154_llsec_device *dev;
455 	struct mac802154_llsec_device_key *devkey;
456 
457 	dev = llsec_dev_find_long(sec, dev_addr);
458 
459 	if (!dev)
460 		return -ENOENT;
461 
462 	devkey = llsec_devkey_find(dev, &key->key_id);
463 	if (!devkey)
464 		return -ENOENT;
465 
466 	list_del_rcu(&devkey->devkey.list);
467 	kfree_rcu(devkey, rcu);
468 	return 0;
469 }
470 
471 static struct mac802154_llsec_seclevel*
472 llsec_find_seclevel(const struct mac802154_llsec *sec,
473 		    const struct ieee802154_llsec_seclevel *sl)
474 {
475 	struct ieee802154_llsec_seclevel *pos;
476 
477 	list_for_each_entry(pos, &sec->table.security_levels, list) {
478 		if (pos->frame_type != sl->frame_type ||
479 		    (pos->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
480 		     pos->cmd_frame_id != sl->cmd_frame_id) ||
481 		    pos->device_override != sl->device_override ||
482 		    pos->sec_levels != sl->sec_levels)
483 			continue;
484 
485 		return container_of(pos, struct mac802154_llsec_seclevel,
486 				    level);
487 	}
488 
489 	return NULL;
490 }
491 
492 int mac802154_llsec_seclevel_add(struct mac802154_llsec *sec,
493 				 const struct ieee802154_llsec_seclevel *sl)
494 {
495 	struct mac802154_llsec_seclevel *entry;
496 
497 	if (llsec_find_seclevel(sec, sl))
498 		return -EEXIST;
499 
500 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
501 	if (!entry)
502 		return -ENOMEM;
503 
504 	entry->level = *sl;
505 
506 	list_add_tail_rcu(&entry->level.list, &sec->table.security_levels);
507 
508 	return 0;
509 }
510 
511 int mac802154_llsec_seclevel_del(struct mac802154_llsec *sec,
512 				 const struct ieee802154_llsec_seclevel *sl)
513 {
514 	struct mac802154_llsec_seclevel *pos;
515 
516 	pos = llsec_find_seclevel(sec, sl);
517 	if (!pos)
518 		return -ENOENT;
519 
520 	list_del_rcu(&pos->level.list);
521 	kfree_rcu(pos, rcu);
522 
523 	return 0;
524 }
525 
526 static int llsec_recover_addr(struct mac802154_llsec *sec,
527 			      struct ieee802154_addr *addr)
528 {
529 	__le16 caddr = sec->params.coord_shortaddr;
530 
531 	addr->pan_id = sec->params.pan_id;
532 
533 	if (caddr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
534 		return -EINVAL;
535 	} else if (caddr == cpu_to_le16(IEEE802154_ADDR_UNDEF)) {
536 		addr->extended_addr = sec->params.coord_hwaddr;
537 		addr->mode = IEEE802154_ADDR_LONG;
538 	} else {
539 		addr->short_addr = sec->params.coord_shortaddr;
540 		addr->mode = IEEE802154_ADDR_SHORT;
541 	}
542 
543 	return 0;
544 }
545 
546 static struct mac802154_llsec_key*
547 llsec_lookup_key(struct mac802154_llsec *sec,
548 		 const struct ieee802154_hdr *hdr,
549 		 const struct ieee802154_addr *addr,
550 		 struct ieee802154_llsec_key_id *key_id)
551 {
552 	struct ieee802154_addr devaddr = *addr;
553 	u8 key_id_mode = hdr->sec.key_id_mode;
554 	struct ieee802154_llsec_key_entry *key_entry;
555 	struct mac802154_llsec_key *key;
556 
557 	if (key_id_mode == IEEE802154_SCF_KEY_IMPLICIT &&
558 	    devaddr.mode == IEEE802154_ADDR_NONE) {
559 		if (hdr->fc.type == IEEE802154_FC_TYPE_BEACON) {
560 			devaddr.extended_addr = sec->params.coord_hwaddr;
561 			devaddr.mode = IEEE802154_ADDR_LONG;
562 		} else if (llsec_recover_addr(sec, &devaddr) < 0) {
563 			return NULL;
564 		}
565 	}
566 
567 	list_for_each_entry_rcu(key_entry, &sec->table.keys, list) {
568 		const struct ieee802154_llsec_key_id *id = &key_entry->id;
569 
570 		if (!(key_entry->key->frame_types & BIT(hdr->fc.type)))
571 			continue;
572 
573 		if (id->mode != key_id_mode)
574 			continue;
575 
576 		if (key_id_mode == IEEE802154_SCF_KEY_IMPLICIT) {
577 			if (ieee802154_addr_equal(&devaddr, &id->device_addr))
578 				goto found;
579 		} else {
580 			if (id->id != hdr->sec.key_id)
581 				continue;
582 
583 			if ((key_id_mode == IEEE802154_SCF_KEY_INDEX) ||
584 			    (key_id_mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
585 			     id->short_source == hdr->sec.short_src) ||
586 			    (key_id_mode == IEEE802154_SCF_KEY_HW_INDEX &&
587 			     id->extended_source == hdr->sec.extended_src))
588 				goto found;
589 		}
590 	}
591 
592 	return NULL;
593 
594 found:
595 	key = container_of(key_entry->key, struct mac802154_llsec_key, key);
596 	if (key_id)
597 		*key_id = key_entry->id;
598 	return llsec_key_get(key);
599 }
600 
601 static void llsec_geniv(u8 iv[16], __le64 addr,
602 			const struct ieee802154_sechdr *sec)
603 {
604 	__be64 addr_bytes = (__force __be64) swab64((__force u64) addr);
605 	__be32 frame_counter = (__force __be32) swab32((__force u32) sec->frame_counter);
606 
607 	iv[0] = 1; /* L' = L - 1 = 1 */
608 	memcpy(iv + 1, &addr_bytes, sizeof(addr_bytes));
609 	memcpy(iv + 9, &frame_counter, sizeof(frame_counter));
610 	iv[13] = sec->level;
611 	iv[14] = 0;
612 	iv[15] = 1;
613 }
614 
615 static int
616 llsec_do_encrypt_unauth(struct sk_buff *skb, const struct mac802154_llsec *sec,
617 			const struct ieee802154_hdr *hdr,
618 			struct mac802154_llsec_key *key)
619 {
620 	u8 iv[16];
621 	struct scatterlist src;
622 	struct blkcipher_desc req = {
623 		.tfm = key->tfm0,
624 		.info = iv,
625 		.flags = 0,
626 	};
627 
628 	llsec_geniv(iv, sec->params.hwaddr, &hdr->sec);
629 	sg_init_one(&src, skb->data, skb->len);
630 	return crypto_blkcipher_encrypt_iv(&req, &src, &src, skb->len);
631 }
632 
633 static struct crypto_aead*
634 llsec_tfm_by_len(struct mac802154_llsec_key *key, int authlen)
635 {
636 	int i;
637 
638 	for (i = 0; i < ARRAY_SIZE(key->tfm); i++)
639 		if (crypto_aead_authsize(key->tfm[i]) == authlen)
640 			return key->tfm[i];
641 
642 	BUG();
643 }
644 
645 static int
646 llsec_do_encrypt_auth(struct sk_buff *skb, const struct mac802154_llsec *sec,
647 		      const struct ieee802154_hdr *hdr,
648 		      struct mac802154_llsec_key *key)
649 {
650 	u8 iv[16];
651 	unsigned char *data;
652 	int authlen, assoclen, datalen, rc;
653 	struct scatterlist sg;
654 	struct aead_request *req;
655 
656 	authlen = ieee802154_sechdr_authtag_len(&hdr->sec);
657 	llsec_geniv(iv, sec->params.hwaddr, &hdr->sec);
658 
659 	req = aead_request_alloc(llsec_tfm_by_len(key, authlen), GFP_ATOMIC);
660 	if (!req)
661 		return -ENOMEM;
662 
663 	assoclen = skb->mac_len;
664 
665 	data = skb_mac_header(skb) + skb->mac_len;
666 	datalen = skb_tail_pointer(skb) - data;
667 
668 	skb_put(skb, authlen);
669 
670 	sg_init_one(&sg, skb_mac_header(skb), assoclen + datalen + authlen);
671 
672 	if (!(hdr->sec.level & IEEE802154_SCF_SECLEVEL_ENC)) {
673 		assoclen += datalen;
674 		datalen = 0;
675 	}
676 
677 	aead_request_set_callback(req, 0, NULL, NULL);
678 	aead_request_set_crypt(req, &sg, &sg, datalen, iv);
679 	aead_request_set_ad(req, assoclen);
680 
681 	rc = crypto_aead_encrypt(req);
682 
683 	kfree(req);
684 
685 	return rc;
686 }
687 
688 static int llsec_do_encrypt(struct sk_buff *skb,
689 			    const struct mac802154_llsec *sec,
690 			    const struct ieee802154_hdr *hdr,
691 			    struct mac802154_llsec_key *key)
692 {
693 	if (hdr->sec.level == IEEE802154_SCF_SECLEVEL_ENC)
694 		return llsec_do_encrypt_unauth(skb, sec, hdr, key);
695 	else
696 		return llsec_do_encrypt_auth(skb, sec, hdr, key);
697 }
698 
699 int mac802154_llsec_encrypt(struct mac802154_llsec *sec, struct sk_buff *skb)
700 {
701 	struct ieee802154_hdr hdr;
702 	int rc, authlen, hlen;
703 	struct mac802154_llsec_key *key;
704 	u32 frame_ctr;
705 
706 	hlen = ieee802154_hdr_pull(skb, &hdr);
707 
708 	if (hlen < 0 || hdr.fc.type != IEEE802154_FC_TYPE_DATA)
709 		return -EINVAL;
710 
711 	if (!hdr.fc.security_enabled || hdr.sec.level == 0) {
712 		skb_push(skb, hlen);
713 		return 0;
714 	}
715 
716 	authlen = ieee802154_sechdr_authtag_len(&hdr.sec);
717 
718 	if (skb->len + hlen + authlen + IEEE802154_MFR_SIZE > IEEE802154_MTU)
719 		return -EMSGSIZE;
720 
721 	rcu_read_lock();
722 
723 	read_lock_bh(&sec->lock);
724 
725 	if (!sec->params.enabled) {
726 		rc = -EINVAL;
727 		goto fail_read;
728 	}
729 
730 	key = llsec_lookup_key(sec, &hdr, &hdr.dest, NULL);
731 	if (!key) {
732 		rc = -ENOKEY;
733 		goto fail_read;
734 	}
735 
736 	read_unlock_bh(&sec->lock);
737 
738 	write_lock_bh(&sec->lock);
739 
740 	frame_ctr = be32_to_cpu(sec->params.frame_counter);
741 	hdr.sec.frame_counter = cpu_to_le32(frame_ctr);
742 	if (frame_ctr == 0xFFFFFFFF) {
743 		write_unlock_bh(&sec->lock);
744 		llsec_key_put(key);
745 		rc = -EOVERFLOW;
746 		goto fail;
747 	}
748 
749 	sec->params.frame_counter = cpu_to_be32(frame_ctr + 1);
750 
751 	write_unlock_bh(&sec->lock);
752 
753 	rcu_read_unlock();
754 
755 	skb->mac_len = ieee802154_hdr_push(skb, &hdr);
756 	skb_reset_mac_header(skb);
757 
758 	rc = llsec_do_encrypt(skb, sec, &hdr, key);
759 	llsec_key_put(key);
760 
761 	return rc;
762 
763 fail_read:
764 	read_unlock_bh(&sec->lock);
765 fail:
766 	rcu_read_unlock();
767 	return rc;
768 }
769 
770 static struct mac802154_llsec_device*
771 llsec_lookup_dev(struct mac802154_llsec *sec,
772 		 const struct ieee802154_addr *addr)
773 {
774 	struct ieee802154_addr devaddr = *addr;
775 	struct mac802154_llsec_device *dev = NULL;
776 
777 	if (devaddr.mode == IEEE802154_ADDR_NONE &&
778 	    llsec_recover_addr(sec, &devaddr) < 0)
779 		return NULL;
780 
781 	if (devaddr.mode == IEEE802154_ADDR_SHORT) {
782 		u32 key = llsec_dev_hash_short(devaddr.short_addr,
783 					       devaddr.pan_id);
784 
785 		hash_for_each_possible_rcu(sec->devices_short, dev,
786 					   bucket_s, key) {
787 			if (dev->dev.pan_id == devaddr.pan_id &&
788 			    dev->dev.short_addr == devaddr.short_addr)
789 				return dev;
790 		}
791 	} else {
792 		u64 key = llsec_dev_hash_long(devaddr.extended_addr);
793 
794 		hash_for_each_possible_rcu(sec->devices_hw, dev,
795 					   bucket_hw, key) {
796 			if (dev->dev.hwaddr == devaddr.extended_addr)
797 				return dev;
798 		}
799 	}
800 
801 	return NULL;
802 }
803 
804 static int
805 llsec_lookup_seclevel(const struct mac802154_llsec *sec,
806 		      u8 frame_type, u8 cmd_frame_id,
807 		      struct ieee802154_llsec_seclevel *rlevel)
808 {
809 	struct ieee802154_llsec_seclevel *level;
810 
811 	list_for_each_entry_rcu(level, &sec->table.security_levels, list) {
812 		if (level->frame_type == frame_type &&
813 		    (frame_type != IEEE802154_FC_TYPE_MAC_CMD ||
814 		     level->cmd_frame_id == cmd_frame_id)) {
815 			*rlevel = *level;
816 			return 0;
817 		}
818 	}
819 
820 	return -EINVAL;
821 }
822 
823 static int
824 llsec_do_decrypt_unauth(struct sk_buff *skb, const struct mac802154_llsec *sec,
825 			const struct ieee802154_hdr *hdr,
826 			struct mac802154_llsec_key *key, __le64 dev_addr)
827 {
828 	u8 iv[16];
829 	unsigned char *data;
830 	int datalen;
831 	struct scatterlist src;
832 	struct blkcipher_desc req = {
833 		.tfm = key->tfm0,
834 		.info = iv,
835 		.flags = 0,
836 	};
837 
838 	llsec_geniv(iv, dev_addr, &hdr->sec);
839 	data = skb_mac_header(skb) + skb->mac_len;
840 	datalen = skb_tail_pointer(skb) - data;
841 
842 	sg_init_one(&src, data, datalen);
843 
844 	return crypto_blkcipher_decrypt_iv(&req, &src, &src, datalen);
845 }
846 
847 static int
848 llsec_do_decrypt_auth(struct sk_buff *skb, const struct mac802154_llsec *sec,
849 		      const struct ieee802154_hdr *hdr,
850 		      struct mac802154_llsec_key *key, __le64 dev_addr)
851 {
852 	u8 iv[16];
853 	unsigned char *data;
854 	int authlen, datalen, assoclen, rc;
855 	struct scatterlist sg;
856 	struct aead_request *req;
857 
858 	authlen = ieee802154_sechdr_authtag_len(&hdr->sec);
859 	llsec_geniv(iv, dev_addr, &hdr->sec);
860 
861 	req = aead_request_alloc(llsec_tfm_by_len(key, authlen), GFP_ATOMIC);
862 	if (!req)
863 		return -ENOMEM;
864 
865 	assoclen = skb->mac_len;
866 
867 	data = skb_mac_header(skb) + skb->mac_len;
868 	datalen = skb_tail_pointer(skb) - data;
869 
870 	sg_init_one(&sg, skb_mac_header(skb), assoclen + datalen);
871 
872 	if (!(hdr->sec.level & IEEE802154_SCF_SECLEVEL_ENC)) {
873 		assoclen += datalen - authlen;
874 		datalen = authlen;
875 	}
876 
877 	aead_request_set_callback(req, 0, NULL, NULL);
878 	aead_request_set_crypt(req, &sg, &sg, datalen, iv);
879 	aead_request_set_ad(req, assoclen);
880 
881 	rc = crypto_aead_decrypt(req);
882 
883 	kfree(req);
884 	skb_trim(skb, skb->len - authlen);
885 
886 	return rc;
887 }
888 
889 static int
890 llsec_do_decrypt(struct sk_buff *skb, const struct mac802154_llsec *sec,
891 		 const struct ieee802154_hdr *hdr,
892 		 struct mac802154_llsec_key *key, __le64 dev_addr)
893 {
894 	if (hdr->sec.level == IEEE802154_SCF_SECLEVEL_ENC)
895 		return llsec_do_decrypt_unauth(skb, sec, hdr, key, dev_addr);
896 	else
897 		return llsec_do_decrypt_auth(skb, sec, hdr, key, dev_addr);
898 }
899 
900 static int
901 llsec_update_devkey_record(struct mac802154_llsec_device *dev,
902 			   const struct ieee802154_llsec_key_id *in_key)
903 {
904 	struct mac802154_llsec_device_key *devkey;
905 
906 	devkey = llsec_devkey_find(dev, in_key);
907 
908 	if (!devkey) {
909 		struct mac802154_llsec_device_key *next;
910 
911 		next = kzalloc(sizeof(*devkey), GFP_ATOMIC);
912 		if (!next)
913 			return -ENOMEM;
914 
915 		next->devkey.key_id = *in_key;
916 
917 		spin_lock_bh(&dev->lock);
918 
919 		devkey = llsec_devkey_find(dev, in_key);
920 		if (!devkey)
921 			list_add_rcu(&next->devkey.list, &dev->dev.keys);
922 		else
923 			kfree(next);
924 
925 		spin_unlock_bh(&dev->lock);
926 	}
927 
928 	return 0;
929 }
930 
931 static int
932 llsec_update_devkey_info(struct mac802154_llsec_device *dev,
933 			 const struct ieee802154_llsec_key_id *in_key,
934 			 u32 frame_counter)
935 {
936 	struct mac802154_llsec_device_key *devkey = NULL;
937 
938 	if (dev->dev.key_mode == IEEE802154_LLSEC_DEVKEY_RESTRICT) {
939 		devkey = llsec_devkey_find(dev, in_key);
940 		if (!devkey)
941 			return -ENOENT;
942 	}
943 
944 	if (dev->dev.key_mode == IEEE802154_LLSEC_DEVKEY_RECORD) {
945 		int rc = llsec_update_devkey_record(dev, in_key);
946 
947 		if (rc < 0)
948 			return rc;
949 	}
950 
951 	spin_lock_bh(&dev->lock);
952 
953 	if ((!devkey && frame_counter < dev->dev.frame_counter) ||
954 	    (devkey && frame_counter < devkey->devkey.frame_counter)) {
955 		spin_unlock_bh(&dev->lock);
956 		return -EINVAL;
957 	}
958 
959 	if (devkey)
960 		devkey->devkey.frame_counter = frame_counter + 1;
961 	else
962 		dev->dev.frame_counter = frame_counter + 1;
963 
964 	spin_unlock_bh(&dev->lock);
965 
966 	return 0;
967 }
968 
969 int mac802154_llsec_decrypt(struct mac802154_llsec *sec, struct sk_buff *skb)
970 {
971 	struct ieee802154_hdr hdr;
972 	struct mac802154_llsec_key *key;
973 	struct ieee802154_llsec_key_id key_id;
974 	struct mac802154_llsec_device *dev;
975 	struct ieee802154_llsec_seclevel seclevel;
976 	int err;
977 	__le64 dev_addr;
978 	u32 frame_ctr;
979 
980 	if (ieee802154_hdr_peek(skb, &hdr) < 0)
981 		return -EINVAL;
982 	if (!hdr.fc.security_enabled)
983 		return 0;
984 	if (hdr.fc.version == 0)
985 		return -EINVAL;
986 
987 	read_lock_bh(&sec->lock);
988 	if (!sec->params.enabled) {
989 		read_unlock_bh(&sec->lock);
990 		return -EINVAL;
991 	}
992 	read_unlock_bh(&sec->lock);
993 
994 	rcu_read_lock();
995 
996 	key = llsec_lookup_key(sec, &hdr, &hdr.source, &key_id);
997 	if (!key) {
998 		err = -ENOKEY;
999 		goto fail;
1000 	}
1001 
1002 	dev = llsec_lookup_dev(sec, &hdr.source);
1003 	if (!dev) {
1004 		err = -EINVAL;
1005 		goto fail_dev;
1006 	}
1007 
1008 	if (llsec_lookup_seclevel(sec, hdr.fc.type, 0, &seclevel) < 0) {
1009 		err = -EINVAL;
1010 		goto fail_dev;
1011 	}
1012 
1013 	if (!(seclevel.sec_levels & BIT(hdr.sec.level)) &&
1014 	    (hdr.sec.level == 0 && seclevel.device_override &&
1015 	     !dev->dev.seclevel_exempt)) {
1016 		err = -EINVAL;
1017 		goto fail_dev;
1018 	}
1019 
1020 	frame_ctr = le32_to_cpu(hdr.sec.frame_counter);
1021 
1022 	if (frame_ctr == 0xffffffff) {
1023 		err = -EOVERFLOW;
1024 		goto fail_dev;
1025 	}
1026 
1027 	err = llsec_update_devkey_info(dev, &key_id, frame_ctr);
1028 	if (err)
1029 		goto fail_dev;
1030 
1031 	dev_addr = dev->dev.hwaddr;
1032 
1033 	rcu_read_unlock();
1034 
1035 	err = llsec_do_decrypt(skb, sec, &hdr, key, dev_addr);
1036 	llsec_key_put(key);
1037 	return err;
1038 
1039 fail_dev:
1040 	llsec_key_put(key);
1041 fail:
1042 	rcu_read_unlock();
1043 	return err;
1044 }
1045