key.c 32 KB

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