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