llsec.c 25 KB

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