key.c 31 KB

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