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