key.c 32 KB

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