llsec.c 25 KB

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