key.c 31 KB

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