key.c 32 KB

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