key.c 31 KB

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