key.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright 2017 Intel Deutschland GmbH
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/if_ether.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/list.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/slab.h>
  19. #include <linux/export.h>
  20. #include <net/mac80211.h>
  21. #include <crypto/algapi.h>
  22. #include <asm/unaligned.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "debugfs_key.h"
  26. #include "aes_ccm.h"
  27. #include "aes_cmac.h"
  28. #include "aes_gmac.h"
  29. #include "aes_gcm.h"
  30. /**
  31. * DOC: Key handling basics
  32. *
  33. * Key handling in mac80211 is done based on per-interface (sub_if_data)
  34. * keys and per-station keys. Since each station belongs to an interface,
  35. * each station key also belongs to that interface.
  36. *
  37. * Hardware acceleration is done on a best-effort basis for algorithms
  38. * that are implemented in software, for each key the hardware is asked
  39. * to enable that key for offloading but if it cannot do that the key is
  40. * simply kept for software encryption (unless it is for an algorithm
  41. * that isn't implemented in software).
  42. * There is currently no way of knowing whether a key is handled in SW
  43. * or HW except by looking into debugfs.
  44. *
  45. * All key management is internally protected by a mutex. Within all
  46. * other parts of mac80211, key references are, just as STA structure
  47. * references, protected by RCU. Note, however, that some things are
  48. * unprotected, namely the key->sta dereferences within the hardware
  49. * acceleration functions. This means that sta_info_destroy() must
  50. * remove the key which waits for an RCU grace period.
  51. */
  52. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  53. static void assert_key_lock(struct ieee80211_local *local)
  54. {
  55. lockdep_assert_held(&local->key_mtx);
  56. }
  57. static void
  58. update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
  59. {
  60. struct ieee80211_sub_if_data *vlan;
  61. if (sdata->vif.type != NL80211_IFTYPE_AP)
  62. return;
  63. /* crypto_tx_tailroom_needed_cnt is protected by this */
  64. assert_key_lock(sdata->local);
  65. rcu_read_lock();
  66. list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
  67. vlan->crypto_tx_tailroom_needed_cnt += delta;
  68. rcu_read_unlock();
  69. }
  70. static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
  71. {
  72. /*
  73. * When this count is zero, SKB resizing for allocating tailroom
  74. * for IV or MMIC is skipped. But, this check has created two race
  75. * cases in xmit path while transiting from zero count to one:
  76. *
  77. * 1. SKB resize was skipped because no key was added but just before
  78. * the xmit key is added and SW encryption kicks off.
  79. *
  80. * 2. SKB resize was skipped because all the keys were hw planted but
  81. * just before xmit one of the key is deleted and SW encryption kicks
  82. * off.
  83. *
  84. * In both the above case SW encryption will find not enough space for
  85. * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
  86. *
  87. * Solution has been explained at
  88. * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
  89. */
  90. assert_key_lock(sdata->local);
  91. update_vlan_tailroom_need_count(sdata, 1);
  92. if (!sdata->crypto_tx_tailroom_needed_cnt++) {
  93. /*
  94. * Flush all XMIT packets currently using HW encryption or no
  95. * encryption at all if the count transition is from 0 -> 1.
  96. */
  97. synchronize_net();
  98. }
  99. }
  100. static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
  101. int delta)
  102. {
  103. assert_key_lock(sdata->local);
  104. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
  105. update_vlan_tailroom_need_count(sdata, -delta);
  106. sdata->crypto_tx_tailroom_needed_cnt -= delta;
  107. }
  108. static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  109. {
  110. struct ieee80211_sub_if_data *sdata;
  111. struct sta_info *sta;
  112. int ret = -EOPNOTSUPP;
  113. might_sleep();
  114. if (key->flags & KEY_FLAG_TAINTED) {
  115. /* If we get here, it's during resume and the key is
  116. * tainted so shouldn't be used/programmed any more.
  117. * However, its flags may still indicate that it was
  118. * programmed into the device (since we're in resume)
  119. * so clear that flag now to avoid trying to remove
  120. * it again later.
  121. */
  122. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  123. return -EINVAL;
  124. }
  125. if (!key->local->ops->set_key)
  126. goto out_unsupported;
  127. assert_key_lock(key->local);
  128. sta = key->sta;
  129. /*
  130. * If this is a per-STA GTK, check if it
  131. * is supported; if not, return.
  132. */
  133. if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
  134. !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
  135. goto out_unsupported;
  136. if (sta && !sta->uploaded)
  137. goto out_unsupported;
  138. sdata = key->sdata;
  139. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  140. /*
  141. * The driver doesn't know anything about VLAN interfaces.
  142. * Hence, don't send GTKs for VLAN interfaces to the driver.
  143. */
  144. if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
  145. goto out_unsupported;
  146. }
  147. ret = drv_set_key(key->local, SET_KEY, sdata,
  148. sta ? &sta->sta : NULL, &key->conf);
  149. if (!ret) {
  150. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  151. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  152. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  153. decrease_tailroom_need_count(sdata, 1);
  154. WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  155. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
  156. return 0;
  157. }
  158. if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
  159. sdata_err(sdata,
  160. "failed to set key (%d, %pM) to hardware (%d)\n",
  161. key->conf.keyidx,
  162. sta ? sta->sta.addr : bcast_addr, ret);
  163. out_unsupported:
  164. switch (key->conf.cipher) {
  165. case WLAN_CIPHER_SUITE_WEP40:
  166. case WLAN_CIPHER_SUITE_WEP104:
  167. case WLAN_CIPHER_SUITE_TKIP:
  168. case WLAN_CIPHER_SUITE_CCMP:
  169. case WLAN_CIPHER_SUITE_CCMP_256:
  170. case WLAN_CIPHER_SUITE_AES_CMAC:
  171. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  172. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  173. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  174. case WLAN_CIPHER_SUITE_GCMP:
  175. case WLAN_CIPHER_SUITE_GCMP_256:
  176. /* all of these we can do in software - if driver can */
  177. if (ret == 1)
  178. return 0;
  179. if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
  180. return -EINVAL;
  181. return 0;
  182. default:
  183. return -EINVAL;
  184. }
  185. }
  186. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  187. {
  188. struct ieee80211_sub_if_data *sdata;
  189. struct sta_info *sta;
  190. int ret;
  191. might_sleep();
  192. if (!key || !key->local->ops->set_key)
  193. return;
  194. assert_key_lock(key->local);
  195. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  196. return;
  197. sta = key->sta;
  198. sdata = key->sdata;
  199. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  200. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  201. increment_tailroom_need_count(sdata);
  202. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  203. sta ? &sta->sta : NULL, &key->conf);
  204. if (ret)
  205. sdata_err(sdata,
  206. "failed to remove key (%d, %pM) from hardware (%d)\n",
  207. key->conf.keyidx,
  208. sta ? sta->sta.addr : bcast_addr, ret);
  209. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  210. }
  211. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  212. int idx, bool uni, bool multi)
  213. {
  214. struct ieee80211_key *key = NULL;
  215. assert_key_lock(sdata->local);
  216. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  217. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  218. if (uni) {
  219. rcu_assign_pointer(sdata->default_unicast_key, key);
  220. ieee80211_check_fast_xmit_iface(sdata);
  221. drv_set_default_unicast_key(sdata->local, sdata, idx);
  222. }
  223. if (multi)
  224. rcu_assign_pointer(sdata->default_multicast_key, key);
  225. ieee80211_debugfs_key_update_default(sdata);
  226. }
  227. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  228. bool uni, bool multi)
  229. {
  230. mutex_lock(&sdata->local->key_mtx);
  231. __ieee80211_set_default_key(sdata, idx, uni, multi);
  232. mutex_unlock(&sdata->local->key_mtx);
  233. }
  234. static void
  235. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  236. {
  237. struct ieee80211_key *key = NULL;
  238. assert_key_lock(sdata->local);
  239. if (idx >= NUM_DEFAULT_KEYS &&
  240. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  241. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  242. rcu_assign_pointer(sdata->default_mgmt_key, key);
  243. ieee80211_debugfs_key_update_default(sdata);
  244. }
  245. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  246. int idx)
  247. {
  248. mutex_lock(&sdata->local->key_mtx);
  249. __ieee80211_set_default_mgmt_key(sdata, idx);
  250. mutex_unlock(&sdata->local->key_mtx);
  251. }
  252. static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  253. struct sta_info *sta,
  254. bool pairwise,
  255. struct ieee80211_key *old,
  256. struct ieee80211_key *new)
  257. {
  258. int idx;
  259. bool defunikey, defmultikey, defmgmtkey;
  260. /* caller must provide at least one old/new */
  261. if (WARN_ON(!new && !old))
  262. return;
  263. if (new)
  264. list_add_tail(&new->list, &sdata->key_list);
  265. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  266. if (old)
  267. idx = old->conf.keyidx;
  268. else
  269. idx = new->conf.keyidx;
  270. if (sta) {
  271. if (pairwise) {
  272. rcu_assign_pointer(sta->ptk[idx], new);
  273. sta->ptk_idx = idx;
  274. ieee80211_check_fast_xmit(sta);
  275. } else {
  276. rcu_assign_pointer(sta->gtk[idx], new);
  277. }
  278. } else {
  279. defunikey = old &&
  280. old == key_mtx_dereference(sdata->local,
  281. sdata->default_unicast_key);
  282. defmultikey = old &&
  283. old == key_mtx_dereference(sdata->local,
  284. sdata->default_multicast_key);
  285. defmgmtkey = old &&
  286. old == key_mtx_dereference(sdata->local,
  287. sdata->default_mgmt_key);
  288. if (defunikey && !new)
  289. __ieee80211_set_default_key(sdata, -1, true, false);
  290. if (defmultikey && !new)
  291. __ieee80211_set_default_key(sdata, -1, false, true);
  292. if (defmgmtkey && !new)
  293. __ieee80211_set_default_mgmt_key(sdata, -1);
  294. rcu_assign_pointer(sdata->keys[idx], new);
  295. if (defunikey && new)
  296. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  297. true, false);
  298. if (defmultikey && new)
  299. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  300. false, true);
  301. if (defmgmtkey && new)
  302. __ieee80211_set_default_mgmt_key(sdata,
  303. new->conf.keyidx);
  304. }
  305. if (old)
  306. list_del(&old->list);
  307. }
  308. struct ieee80211_key *
  309. ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  310. const u8 *key_data,
  311. size_t seq_len, const u8 *seq,
  312. const struct ieee80211_cipher_scheme *cs)
  313. {
  314. struct ieee80211_key *key;
  315. int i, j, err;
  316. if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
  317. return ERR_PTR(-EINVAL);
  318. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  319. if (!key)
  320. return ERR_PTR(-ENOMEM);
  321. /*
  322. * Default to software encryption; we'll later upload the
  323. * key to the hardware if possible.
  324. */
  325. key->conf.flags = 0;
  326. key->flags = 0;
  327. key->conf.cipher = cipher;
  328. key->conf.keyidx = idx;
  329. key->conf.keylen = key_len;
  330. switch (cipher) {
  331. case WLAN_CIPHER_SUITE_WEP40:
  332. case WLAN_CIPHER_SUITE_WEP104:
  333. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  334. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  335. break;
  336. case WLAN_CIPHER_SUITE_TKIP:
  337. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  338. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  339. if (seq) {
  340. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  341. key->u.tkip.rx[i].iv32 =
  342. get_unaligned_le32(&seq[2]);
  343. key->u.tkip.rx[i].iv16 =
  344. get_unaligned_le16(seq);
  345. }
  346. }
  347. spin_lock_init(&key->u.tkip.txlock);
  348. break;
  349. case WLAN_CIPHER_SUITE_CCMP:
  350. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  351. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  352. if (seq) {
  353. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  354. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  355. key->u.ccmp.rx_pn[i][j] =
  356. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  357. }
  358. /*
  359. * Initialize AES key state here as an optimization so that
  360. * it does not need to be initialized for every packet.
  361. */
  362. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  363. key_data, key_len, IEEE80211_CCMP_MIC_LEN);
  364. if (IS_ERR(key->u.ccmp.tfm)) {
  365. err = PTR_ERR(key->u.ccmp.tfm);
  366. kfree(key);
  367. return ERR_PTR(err);
  368. }
  369. break;
  370. case WLAN_CIPHER_SUITE_CCMP_256:
  371. key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
  372. key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
  373. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  374. for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
  375. key->u.ccmp.rx_pn[i][j] =
  376. seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
  377. /* Initialize AES key state here as an optimization so that
  378. * it does not need to be initialized for every packet.
  379. */
  380. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  381. key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
  382. if (IS_ERR(key->u.ccmp.tfm)) {
  383. err = PTR_ERR(key->u.ccmp.tfm);
  384. kfree(key);
  385. return ERR_PTR(err);
  386. }
  387. break;
  388. case WLAN_CIPHER_SUITE_AES_CMAC:
  389. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  390. key->conf.iv_len = 0;
  391. if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  392. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  393. else
  394. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  395. if (seq)
  396. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  397. key->u.aes_cmac.rx_pn[j] =
  398. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  399. /*
  400. * Initialize AES key state here as an optimization so that
  401. * it does not need to be initialized for every packet.
  402. */
  403. key->u.aes_cmac.tfm =
  404. ieee80211_aes_cmac_key_setup(key_data, key_len);
  405. if (IS_ERR(key->u.aes_cmac.tfm)) {
  406. err = PTR_ERR(key->u.aes_cmac.tfm);
  407. kfree(key);
  408. return ERR_PTR(err);
  409. }
  410. break;
  411. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  412. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  413. key->conf.iv_len = 0;
  414. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  415. if (seq)
  416. for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
  417. key->u.aes_gmac.rx_pn[j] =
  418. seq[IEEE80211_GMAC_PN_LEN - j - 1];
  419. /* Initialize AES key state here as an optimization so that
  420. * it does not need to be initialized for every packet.
  421. */
  422. key->u.aes_gmac.tfm =
  423. ieee80211_aes_gmac_key_setup(key_data, key_len);
  424. if (IS_ERR(key->u.aes_gmac.tfm)) {
  425. err = PTR_ERR(key->u.aes_gmac.tfm);
  426. kfree(key);
  427. return ERR_PTR(err);
  428. }
  429. break;
  430. case WLAN_CIPHER_SUITE_GCMP:
  431. case WLAN_CIPHER_SUITE_GCMP_256:
  432. key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
  433. key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
  434. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  435. for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
  436. key->u.gcmp.rx_pn[i][j] =
  437. seq[IEEE80211_GCMP_PN_LEN - j - 1];
  438. /* Initialize AES key state here as an optimization so that
  439. * it does not need to be initialized for every packet.
  440. */
  441. key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
  442. key_len);
  443. if (IS_ERR(key->u.gcmp.tfm)) {
  444. err = PTR_ERR(key->u.gcmp.tfm);
  445. kfree(key);
  446. return ERR_PTR(err);
  447. }
  448. break;
  449. default:
  450. if (cs) {
  451. if (seq_len && seq_len != cs->pn_len) {
  452. kfree(key);
  453. return ERR_PTR(-EINVAL);
  454. }
  455. key->conf.iv_len = cs->hdr_len;
  456. key->conf.icv_len = cs->mic_len;
  457. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  458. for (j = 0; j < seq_len; j++)
  459. key->u.gen.rx_pn[i][j] =
  460. seq[seq_len - j - 1];
  461. key->flags |= KEY_FLAG_CIPHER_SCHEME;
  462. }
  463. }
  464. memcpy(key->conf.key, key_data, key_len);
  465. INIT_LIST_HEAD(&key->list);
  466. return key;
  467. }
  468. static void ieee80211_key_free_common(struct ieee80211_key *key)
  469. {
  470. switch (key->conf.cipher) {
  471. case WLAN_CIPHER_SUITE_CCMP:
  472. case WLAN_CIPHER_SUITE_CCMP_256:
  473. ieee80211_aes_key_free(key->u.ccmp.tfm);
  474. break;
  475. case WLAN_CIPHER_SUITE_AES_CMAC:
  476. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  477. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  478. break;
  479. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  480. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  481. ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
  482. break;
  483. case WLAN_CIPHER_SUITE_GCMP:
  484. case WLAN_CIPHER_SUITE_GCMP_256:
  485. ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
  486. break;
  487. }
  488. kzfree(key);
  489. }
  490. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  491. bool delay_tailroom)
  492. {
  493. if (key->local)
  494. ieee80211_key_disable_hw_accel(key);
  495. if (key->local) {
  496. struct ieee80211_sub_if_data *sdata = key->sdata;
  497. ieee80211_debugfs_key_remove(key);
  498. if (delay_tailroom) {
  499. /* see ieee80211_delayed_tailroom_dec */
  500. sdata->crypto_tx_tailroom_pending_dec++;
  501. schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
  502. HZ/2);
  503. } else {
  504. decrease_tailroom_need_count(sdata, 1);
  505. }
  506. }
  507. ieee80211_key_free_common(key);
  508. }
  509. static void ieee80211_key_destroy(struct ieee80211_key *key,
  510. bool delay_tailroom)
  511. {
  512. if (!key)
  513. return;
  514. /*
  515. * Synchronize so the TX path can no longer be using
  516. * this key before we free/remove it.
  517. */
  518. synchronize_net();
  519. __ieee80211_key_destroy(key, delay_tailroom);
  520. }
  521. void ieee80211_key_free_unused(struct ieee80211_key *key)
  522. {
  523. WARN_ON(key->sdata || key->local);
  524. ieee80211_key_free_common(key);
  525. }
  526. static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
  527. struct ieee80211_key *old,
  528. struct ieee80211_key *new)
  529. {
  530. u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
  531. u8 *tk_old, *tk_new;
  532. if (!old || new->conf.keylen != old->conf.keylen)
  533. return false;
  534. tk_old = old->conf.key;
  535. tk_new = new->conf.key;
  536. /*
  537. * In station mode, don't compare the TX MIC key, as it's never used
  538. * and offloaded rekeying may not care to send it to the host. This
  539. * is the case in iwlwifi, for example.
  540. */
  541. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  542. new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
  543. new->conf.keylen == WLAN_KEY_LEN_TKIP &&
  544. !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  545. memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
  546. memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
  547. memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
  548. memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
  549. tk_old = tkip_old;
  550. tk_new = tkip_new;
  551. }
  552. return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
  553. }
  554. int ieee80211_key_link(struct ieee80211_key *key,
  555. struct ieee80211_sub_if_data *sdata,
  556. struct sta_info *sta)
  557. {
  558. struct ieee80211_local *local = sdata->local;
  559. struct ieee80211_key *old_key;
  560. int idx = key->conf.keyidx;
  561. bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  562. /*
  563. * We want to delay tailroom updates only for station - in that
  564. * case it helps roaming speed, but in other cases it hurts and
  565. * can cause warnings to appear.
  566. */
  567. bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
  568. int ret;
  569. mutex_lock(&sdata->local->key_mtx);
  570. if (sta && pairwise)
  571. old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
  572. else if (sta)
  573. old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
  574. else
  575. old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  576. /*
  577. * Silently accept key re-installation without really installing the
  578. * new version of the key to avoid nonce reuse or replay issues.
  579. */
  580. if (ieee80211_key_identical(sdata, old_key, key)) {
  581. ieee80211_key_free_unused(key);
  582. ret = 0;
  583. goto out;
  584. }
  585. key->local = sdata->local;
  586. key->sdata = sdata;
  587. key->sta = sta;
  588. increment_tailroom_need_count(sdata);
  589. ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  590. ieee80211_key_destroy(old_key, delay_tailroom);
  591. ieee80211_debugfs_key_add(key);
  592. if (!local->wowlan) {
  593. ret = ieee80211_key_enable_hw_accel(key);
  594. if (ret)
  595. ieee80211_key_free(key, delay_tailroom);
  596. } else {
  597. ret = 0;
  598. }
  599. out:
  600. mutex_unlock(&sdata->local->key_mtx);
  601. return ret;
  602. }
  603. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  604. {
  605. if (!key)
  606. return;
  607. /*
  608. * Replace key with nothingness if it was ever used.
  609. */
  610. if (key->sdata)
  611. ieee80211_key_replace(key->sdata, key->sta,
  612. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  613. key, NULL);
  614. ieee80211_key_destroy(key, delay_tailroom);
  615. }
  616. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  617. {
  618. struct ieee80211_key *key;
  619. struct ieee80211_sub_if_data *vlan;
  620. ASSERT_RTNL();
  621. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  622. return;
  623. mutex_lock(&sdata->local->key_mtx);
  624. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  625. sdata->crypto_tx_tailroom_pending_dec);
  626. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  627. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  628. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  629. vlan->crypto_tx_tailroom_pending_dec);
  630. }
  631. list_for_each_entry(key, &sdata->key_list, list) {
  632. increment_tailroom_need_count(sdata);
  633. ieee80211_key_enable_hw_accel(key);
  634. }
  635. mutex_unlock(&sdata->local->key_mtx);
  636. }
  637. void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
  638. {
  639. struct ieee80211_sub_if_data *vlan;
  640. mutex_lock(&sdata->local->key_mtx);
  641. sdata->crypto_tx_tailroom_needed_cnt = 0;
  642. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  643. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  644. vlan->crypto_tx_tailroom_needed_cnt = 0;
  645. }
  646. mutex_unlock(&sdata->local->key_mtx);
  647. }
  648. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  649. struct ieee80211_vif *vif,
  650. void (*iter)(struct ieee80211_hw *hw,
  651. struct ieee80211_vif *vif,
  652. struct ieee80211_sta *sta,
  653. struct ieee80211_key_conf *key,
  654. void *data),
  655. void *iter_data)
  656. {
  657. struct ieee80211_local *local = hw_to_local(hw);
  658. struct ieee80211_key *key, *tmp;
  659. struct ieee80211_sub_if_data *sdata;
  660. ASSERT_RTNL();
  661. mutex_lock(&local->key_mtx);
  662. if (vif) {
  663. sdata = vif_to_sdata(vif);
  664. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  665. iter(hw, &sdata->vif,
  666. key->sta ? &key->sta->sta : NULL,
  667. &key->conf, iter_data);
  668. } else {
  669. list_for_each_entry(sdata, &local->interfaces, list)
  670. list_for_each_entry_safe(key, tmp,
  671. &sdata->key_list, list)
  672. iter(hw, &sdata->vif,
  673. key->sta ? &key->sta->sta : NULL,
  674. &key->conf, iter_data);
  675. }
  676. mutex_unlock(&local->key_mtx);
  677. }
  678. EXPORT_SYMBOL(ieee80211_iter_keys);
  679. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  680. struct list_head *keys)
  681. {
  682. struct ieee80211_key *key, *tmp;
  683. decrease_tailroom_need_count(sdata,
  684. sdata->crypto_tx_tailroom_pending_dec);
  685. sdata->crypto_tx_tailroom_pending_dec = 0;
  686. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  687. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  688. ieee80211_key_replace(key->sdata, key->sta,
  689. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  690. key, NULL);
  691. list_add_tail(&key->list, keys);
  692. }
  693. ieee80211_debugfs_key_update_default(sdata);
  694. }
  695. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  696. bool force_synchronize)
  697. {
  698. struct ieee80211_local *local = sdata->local;
  699. struct ieee80211_sub_if_data *vlan;
  700. struct ieee80211_sub_if_data *master;
  701. struct ieee80211_key *key, *tmp;
  702. LIST_HEAD(keys);
  703. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  704. mutex_lock(&local->key_mtx);
  705. ieee80211_free_keys_iface(sdata, &keys);
  706. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  707. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  708. ieee80211_free_keys_iface(vlan, &keys);
  709. }
  710. if (!list_empty(&keys) || force_synchronize)
  711. synchronize_net();
  712. list_for_each_entry_safe(key, tmp, &keys, list)
  713. __ieee80211_key_destroy(key, false);
  714. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  715. if (sdata->bss) {
  716. master = container_of(sdata->bss,
  717. struct ieee80211_sub_if_data,
  718. u.ap);
  719. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
  720. master->crypto_tx_tailroom_needed_cnt);
  721. }
  722. } else {
  723. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  724. sdata->crypto_tx_tailroom_pending_dec);
  725. }
  726. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  727. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  728. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  729. vlan->crypto_tx_tailroom_pending_dec);
  730. }
  731. mutex_unlock(&local->key_mtx);
  732. }
  733. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  734. struct sta_info *sta)
  735. {
  736. struct ieee80211_key *key;
  737. int i;
  738. mutex_lock(&local->key_mtx);
  739. for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
  740. key = key_mtx_dereference(local, sta->gtk[i]);
  741. if (!key)
  742. continue;
  743. ieee80211_key_replace(key->sdata, key->sta,
  744. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  745. key, NULL);
  746. __ieee80211_key_destroy(key, key->sdata->vif.type ==
  747. NL80211_IFTYPE_STATION);
  748. }
  749. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  750. key = key_mtx_dereference(local, sta->ptk[i]);
  751. if (!key)
  752. continue;
  753. ieee80211_key_replace(key->sdata, key->sta,
  754. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  755. key, NULL);
  756. __ieee80211_key_destroy(key, key->sdata->vif.type ==
  757. NL80211_IFTYPE_STATION);
  758. }
  759. mutex_unlock(&local->key_mtx);
  760. }
  761. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  762. {
  763. struct ieee80211_sub_if_data *sdata;
  764. sdata = container_of(wk, struct ieee80211_sub_if_data,
  765. dec_tailroom_needed_wk.work);
  766. /*
  767. * The reason for the delayed tailroom needed decrementing is to
  768. * make roaming faster: during roaming, all keys are first deleted
  769. * and then new keys are installed. The first new key causes the
  770. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  771. * the cost of synchronize_net() (which can be slow). Avoid this
  772. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  773. * key removal for a while, so if we roam the value is larger than
  774. * zero and no 0->1 transition happens.
  775. *
  776. * The cost is that if the AP switching was from an AP with keys
  777. * to one without, we still allocate tailroom while it would no
  778. * longer be needed. However, in the typical (fast) roaming case
  779. * within an ESS this usually won't happen.
  780. */
  781. mutex_lock(&sdata->local->key_mtx);
  782. decrease_tailroom_need_count(sdata,
  783. sdata->crypto_tx_tailroom_pending_dec);
  784. sdata->crypto_tx_tailroom_pending_dec = 0;
  785. mutex_unlock(&sdata->local->key_mtx);
  786. }
  787. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  788. const u8 *replay_ctr, gfp_t gfp)
  789. {
  790. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  791. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  792. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  793. }
  794. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  795. void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
  796. struct ieee80211_key_seq *seq)
  797. {
  798. struct ieee80211_key *key;
  799. u64 pn64;
  800. if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
  801. return;
  802. key = container_of(keyconf, struct ieee80211_key, conf);
  803. switch (key->conf.cipher) {
  804. case WLAN_CIPHER_SUITE_TKIP:
  805. seq->tkip.iv32 = key->u.tkip.tx.iv32;
  806. seq->tkip.iv16 = key->u.tkip.tx.iv16;
  807. break;
  808. case WLAN_CIPHER_SUITE_CCMP:
  809. case WLAN_CIPHER_SUITE_CCMP_256:
  810. case WLAN_CIPHER_SUITE_AES_CMAC:
  811. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  812. BUILD_BUG_ON(offsetof(typeof(*seq), ccmp) !=
  813. offsetof(typeof(*seq), aes_cmac));
  814. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  815. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  816. BUILD_BUG_ON(offsetof(typeof(*seq), ccmp) !=
  817. offsetof(typeof(*seq), aes_gmac));
  818. case WLAN_CIPHER_SUITE_GCMP:
  819. case WLAN_CIPHER_SUITE_GCMP_256:
  820. BUILD_BUG_ON(offsetof(typeof(*seq), ccmp) !=
  821. offsetof(typeof(*seq), gcmp));
  822. pn64 = atomic64_read(&key->conf.tx_pn);
  823. seq->ccmp.pn[5] = pn64;
  824. seq->ccmp.pn[4] = pn64 >> 8;
  825. seq->ccmp.pn[3] = pn64 >> 16;
  826. seq->ccmp.pn[2] = pn64 >> 24;
  827. seq->ccmp.pn[1] = pn64 >> 32;
  828. seq->ccmp.pn[0] = pn64 >> 40;
  829. break;
  830. default:
  831. WARN_ON(1);
  832. }
  833. }
  834. EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
  835. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  836. int tid, struct ieee80211_key_seq *seq)
  837. {
  838. struct ieee80211_key *key;
  839. const u8 *pn;
  840. key = container_of(keyconf, struct ieee80211_key, conf);
  841. switch (key->conf.cipher) {
  842. case WLAN_CIPHER_SUITE_TKIP:
  843. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  844. return;
  845. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  846. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  847. break;
  848. case WLAN_CIPHER_SUITE_CCMP:
  849. case WLAN_CIPHER_SUITE_CCMP_256:
  850. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  851. return;
  852. if (tid < 0)
  853. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  854. else
  855. pn = key->u.ccmp.rx_pn[tid];
  856. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  857. break;
  858. case WLAN_CIPHER_SUITE_AES_CMAC:
  859. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  860. if (WARN_ON(tid != 0))
  861. return;
  862. pn = key->u.aes_cmac.rx_pn;
  863. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  864. break;
  865. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  866. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  867. if (WARN_ON(tid != 0))
  868. return;
  869. pn = key->u.aes_gmac.rx_pn;
  870. memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
  871. break;
  872. case WLAN_CIPHER_SUITE_GCMP:
  873. case WLAN_CIPHER_SUITE_GCMP_256:
  874. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  875. return;
  876. if (tid < 0)
  877. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  878. else
  879. pn = key->u.gcmp.rx_pn[tid];
  880. memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
  881. break;
  882. }
  883. }
  884. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  885. void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
  886. struct ieee80211_key_seq *seq)
  887. {
  888. struct ieee80211_key *key;
  889. u64 pn64;
  890. key = container_of(keyconf, struct ieee80211_key, conf);
  891. switch (key->conf.cipher) {
  892. case WLAN_CIPHER_SUITE_TKIP:
  893. key->u.tkip.tx.iv32 = seq->tkip.iv32;
  894. key->u.tkip.tx.iv16 = seq->tkip.iv16;
  895. break;
  896. case WLAN_CIPHER_SUITE_CCMP:
  897. case WLAN_CIPHER_SUITE_CCMP_256:
  898. case WLAN_CIPHER_SUITE_AES_CMAC:
  899. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  900. BUILD_BUG_ON(offsetof(typeof(*seq), ccmp) !=
  901. offsetof(typeof(*seq), aes_cmac));
  902. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  903. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  904. BUILD_BUG_ON(offsetof(typeof(*seq), ccmp) !=
  905. offsetof(typeof(*seq), aes_gmac));
  906. case WLAN_CIPHER_SUITE_GCMP:
  907. case WLAN_CIPHER_SUITE_GCMP_256:
  908. BUILD_BUG_ON(offsetof(typeof(*seq), ccmp) !=
  909. offsetof(typeof(*seq), gcmp));
  910. pn64 = (u64)seq->ccmp.pn[5] |
  911. ((u64)seq->ccmp.pn[4] << 8) |
  912. ((u64)seq->ccmp.pn[3] << 16) |
  913. ((u64)seq->ccmp.pn[2] << 24) |
  914. ((u64)seq->ccmp.pn[1] << 32) |
  915. ((u64)seq->ccmp.pn[0] << 40);
  916. atomic64_set(&key->conf.tx_pn, pn64);
  917. break;
  918. default:
  919. WARN_ON(1);
  920. break;
  921. }
  922. }
  923. EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
  924. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  925. int tid, struct ieee80211_key_seq *seq)
  926. {
  927. struct ieee80211_key *key;
  928. u8 *pn;
  929. key = container_of(keyconf, struct ieee80211_key, conf);
  930. switch (key->conf.cipher) {
  931. case WLAN_CIPHER_SUITE_TKIP:
  932. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  933. return;
  934. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  935. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  936. break;
  937. case WLAN_CIPHER_SUITE_CCMP:
  938. case WLAN_CIPHER_SUITE_CCMP_256:
  939. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  940. return;
  941. if (tid < 0)
  942. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  943. else
  944. pn = key->u.ccmp.rx_pn[tid];
  945. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  946. break;
  947. case WLAN_CIPHER_SUITE_AES_CMAC:
  948. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  949. if (WARN_ON(tid != 0))
  950. return;
  951. pn = key->u.aes_cmac.rx_pn;
  952. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  953. break;
  954. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  955. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  956. if (WARN_ON(tid != 0))
  957. return;
  958. pn = key->u.aes_gmac.rx_pn;
  959. memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
  960. break;
  961. case WLAN_CIPHER_SUITE_GCMP:
  962. case WLAN_CIPHER_SUITE_GCMP_256:
  963. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  964. return;
  965. if (tid < 0)
  966. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  967. else
  968. pn = key->u.gcmp.rx_pn[tid];
  969. memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
  970. break;
  971. default:
  972. WARN_ON(1);
  973. break;
  974. }
  975. }
  976. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  977. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  978. {
  979. struct ieee80211_key *key;
  980. key = container_of(keyconf, struct ieee80211_key, conf);
  981. assert_key_lock(key->local);
  982. /*
  983. * if key was uploaded, we assume the driver will/has remove(d)
  984. * it, so adjust bookkeeping accordingly
  985. */
  986. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  987. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  988. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  989. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  990. increment_tailroom_need_count(key->sdata);
  991. }
  992. ieee80211_key_free(key, false);
  993. }
  994. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  995. struct ieee80211_key_conf *
  996. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  997. struct ieee80211_key_conf *keyconf)
  998. {
  999. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1000. struct ieee80211_local *local = sdata->local;
  1001. struct ieee80211_key *key;
  1002. int err;
  1003. if (WARN_ON(!local->wowlan))
  1004. return ERR_PTR(-EINVAL);
  1005. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1006. return ERR_PTR(-EINVAL);
  1007. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  1008. keyconf->keylen, keyconf->key,
  1009. 0, NULL, NULL);
  1010. if (IS_ERR(key))
  1011. return ERR_CAST(key);
  1012. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  1013. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  1014. err = ieee80211_key_link(key, sdata, NULL);
  1015. if (err)
  1016. return ERR_PTR(err);
  1017. return &key->conf;
  1018. }
  1019. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);