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. 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, ret;
  562. bool pairwise;
  563. pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  564. idx = key->conf.keyidx;
  565. mutex_lock(&sdata->local->key_mtx);
  566. if (sta && pairwise)
  567. old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
  568. else if (sta)
  569. old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
  570. else
  571. old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  572. /*
  573. * Silently accept key re-installation without really installing the
  574. * new version of the key to avoid nonce reuse or replay issues.
  575. */
  576. if (ieee80211_key_identical(sdata, old_key, key)) {
  577. ieee80211_key_free_unused(key);
  578. ret = 0;
  579. goto out;
  580. }
  581. key->local = sdata->local;
  582. key->sdata = sdata;
  583. key->sta = sta;
  584. increment_tailroom_need_count(sdata);
  585. ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  586. ieee80211_key_destroy(old_key, true);
  587. ieee80211_debugfs_key_add(key);
  588. if (!local->wowlan) {
  589. ret = ieee80211_key_enable_hw_accel(key);
  590. if (ret)
  591. ieee80211_key_free(key, true);
  592. } else {
  593. ret = 0;
  594. }
  595. out:
  596. mutex_unlock(&sdata->local->key_mtx);
  597. return ret;
  598. }
  599. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  600. {
  601. if (!key)
  602. return;
  603. /*
  604. * Replace key with nothingness if it was ever used.
  605. */
  606. if (key->sdata)
  607. ieee80211_key_replace(key->sdata, key->sta,
  608. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  609. key, NULL);
  610. ieee80211_key_destroy(key, delay_tailroom);
  611. }
  612. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  613. {
  614. struct ieee80211_key *key;
  615. struct ieee80211_sub_if_data *vlan;
  616. ASSERT_RTNL();
  617. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  618. return;
  619. mutex_lock(&sdata->local->key_mtx);
  620. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  621. sdata->crypto_tx_tailroom_pending_dec);
  622. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  623. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  624. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  625. vlan->crypto_tx_tailroom_pending_dec);
  626. }
  627. list_for_each_entry(key, &sdata->key_list, list) {
  628. increment_tailroom_need_count(sdata);
  629. ieee80211_key_enable_hw_accel(key);
  630. }
  631. mutex_unlock(&sdata->local->key_mtx);
  632. }
  633. void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
  634. {
  635. struct ieee80211_sub_if_data *vlan;
  636. mutex_lock(&sdata->local->key_mtx);
  637. sdata->crypto_tx_tailroom_needed_cnt = 0;
  638. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  639. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  640. vlan->crypto_tx_tailroom_needed_cnt = 0;
  641. }
  642. mutex_unlock(&sdata->local->key_mtx);
  643. }
  644. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  645. struct ieee80211_vif *vif,
  646. void (*iter)(struct ieee80211_hw *hw,
  647. struct ieee80211_vif *vif,
  648. struct ieee80211_sta *sta,
  649. struct ieee80211_key_conf *key,
  650. void *data),
  651. void *iter_data)
  652. {
  653. struct ieee80211_local *local = hw_to_local(hw);
  654. struct ieee80211_key *key, *tmp;
  655. struct ieee80211_sub_if_data *sdata;
  656. ASSERT_RTNL();
  657. mutex_lock(&local->key_mtx);
  658. if (vif) {
  659. sdata = vif_to_sdata(vif);
  660. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  661. iter(hw, &sdata->vif,
  662. key->sta ? &key->sta->sta : NULL,
  663. &key->conf, iter_data);
  664. } else {
  665. list_for_each_entry(sdata, &local->interfaces, list)
  666. list_for_each_entry_safe(key, tmp,
  667. &sdata->key_list, list)
  668. iter(hw, &sdata->vif,
  669. key->sta ? &key->sta->sta : NULL,
  670. &key->conf, iter_data);
  671. }
  672. mutex_unlock(&local->key_mtx);
  673. }
  674. EXPORT_SYMBOL(ieee80211_iter_keys);
  675. static void
  676. _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  677. struct ieee80211_sub_if_data *sdata,
  678. void (*iter)(struct ieee80211_hw *hw,
  679. struct ieee80211_vif *vif,
  680. struct ieee80211_sta *sta,
  681. struct ieee80211_key_conf *key,
  682. void *data),
  683. void *iter_data)
  684. {
  685. struct ieee80211_key *key;
  686. list_for_each_entry_rcu(key, &sdata->key_list, list) {
  687. /* skip keys of station in removal process */
  688. if (key->sta && key->sta->removed)
  689. continue;
  690. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  691. continue;
  692. iter(hw, &sdata->vif,
  693. key->sta ? &key->sta->sta : NULL,
  694. &key->conf, iter_data);
  695. }
  696. }
  697. void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  698. struct ieee80211_vif *vif,
  699. void (*iter)(struct ieee80211_hw *hw,
  700. struct ieee80211_vif *vif,
  701. struct ieee80211_sta *sta,
  702. struct ieee80211_key_conf *key,
  703. void *data),
  704. void *iter_data)
  705. {
  706. struct ieee80211_local *local = hw_to_local(hw);
  707. struct ieee80211_sub_if_data *sdata;
  708. if (vif) {
  709. sdata = vif_to_sdata(vif);
  710. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  711. } else {
  712. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  713. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  714. }
  715. }
  716. EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
  717. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  718. struct list_head *keys)
  719. {
  720. struct ieee80211_key *key, *tmp;
  721. decrease_tailroom_need_count(sdata,
  722. sdata->crypto_tx_tailroom_pending_dec);
  723. sdata->crypto_tx_tailroom_pending_dec = 0;
  724. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  725. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  726. ieee80211_key_replace(key->sdata, key->sta,
  727. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  728. key, NULL);
  729. list_add_tail(&key->list, keys);
  730. }
  731. ieee80211_debugfs_key_update_default(sdata);
  732. }
  733. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  734. bool force_synchronize)
  735. {
  736. struct ieee80211_local *local = sdata->local;
  737. struct ieee80211_sub_if_data *vlan;
  738. struct ieee80211_sub_if_data *master;
  739. struct ieee80211_key *key, *tmp;
  740. LIST_HEAD(keys);
  741. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  742. mutex_lock(&local->key_mtx);
  743. ieee80211_free_keys_iface(sdata, &keys);
  744. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  745. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  746. ieee80211_free_keys_iface(vlan, &keys);
  747. }
  748. if (!list_empty(&keys) || force_synchronize)
  749. synchronize_net();
  750. list_for_each_entry_safe(key, tmp, &keys, list)
  751. __ieee80211_key_destroy(key, false);
  752. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  753. if (sdata->bss) {
  754. master = container_of(sdata->bss,
  755. struct ieee80211_sub_if_data,
  756. u.ap);
  757. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
  758. master->crypto_tx_tailroom_needed_cnt);
  759. }
  760. } else {
  761. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  762. sdata->crypto_tx_tailroom_pending_dec);
  763. }
  764. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  765. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  766. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  767. vlan->crypto_tx_tailroom_pending_dec);
  768. }
  769. mutex_unlock(&local->key_mtx);
  770. }
  771. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  772. struct sta_info *sta)
  773. {
  774. struct ieee80211_key *key;
  775. int i;
  776. mutex_lock(&local->key_mtx);
  777. for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
  778. key = key_mtx_dereference(local, sta->gtk[i]);
  779. if (!key)
  780. continue;
  781. ieee80211_key_replace(key->sdata, key->sta,
  782. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  783. key, NULL);
  784. __ieee80211_key_destroy(key, true);
  785. }
  786. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  787. key = key_mtx_dereference(local, sta->ptk[i]);
  788. if (!key)
  789. continue;
  790. ieee80211_key_replace(key->sdata, key->sta,
  791. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  792. key, NULL);
  793. __ieee80211_key_destroy(key, true);
  794. }
  795. mutex_unlock(&local->key_mtx);
  796. }
  797. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  798. {
  799. struct ieee80211_sub_if_data *sdata;
  800. sdata = container_of(wk, struct ieee80211_sub_if_data,
  801. dec_tailroom_needed_wk.work);
  802. /*
  803. * The reason for the delayed tailroom needed decrementing is to
  804. * make roaming faster: during roaming, all keys are first deleted
  805. * and then new keys are installed. The first new key causes the
  806. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  807. * the cost of synchronize_net() (which can be slow). Avoid this
  808. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  809. * key removal for a while, so if we roam the value is larger than
  810. * zero and no 0->1 transition happens.
  811. *
  812. * The cost is that if the AP switching was from an AP with keys
  813. * to one without, we still allocate tailroom while it would no
  814. * longer be needed. However, in the typical (fast) roaming case
  815. * within an ESS this usually won't happen.
  816. */
  817. mutex_lock(&sdata->local->key_mtx);
  818. decrease_tailroom_need_count(sdata,
  819. sdata->crypto_tx_tailroom_pending_dec);
  820. sdata->crypto_tx_tailroom_pending_dec = 0;
  821. mutex_unlock(&sdata->local->key_mtx);
  822. }
  823. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  824. const u8 *replay_ctr, gfp_t gfp)
  825. {
  826. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  827. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  828. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  829. }
  830. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  831. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  832. int tid, struct ieee80211_key_seq *seq)
  833. {
  834. struct ieee80211_key *key;
  835. const u8 *pn;
  836. key = container_of(keyconf, struct ieee80211_key, conf);
  837. switch (key->conf.cipher) {
  838. case WLAN_CIPHER_SUITE_TKIP:
  839. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  840. return;
  841. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  842. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  843. break;
  844. case WLAN_CIPHER_SUITE_CCMP:
  845. case WLAN_CIPHER_SUITE_CCMP_256:
  846. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  847. return;
  848. if (tid < 0)
  849. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  850. else
  851. pn = key->u.ccmp.rx_pn[tid];
  852. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  853. break;
  854. case WLAN_CIPHER_SUITE_AES_CMAC:
  855. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  856. if (WARN_ON(tid != 0))
  857. return;
  858. pn = key->u.aes_cmac.rx_pn;
  859. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  860. break;
  861. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  862. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  863. if (WARN_ON(tid != 0))
  864. return;
  865. pn = key->u.aes_gmac.rx_pn;
  866. memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
  867. break;
  868. case WLAN_CIPHER_SUITE_GCMP:
  869. case WLAN_CIPHER_SUITE_GCMP_256:
  870. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  871. return;
  872. if (tid < 0)
  873. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  874. else
  875. pn = key->u.gcmp.rx_pn[tid];
  876. memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
  877. break;
  878. }
  879. }
  880. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  881. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  882. int tid, struct ieee80211_key_seq *seq)
  883. {
  884. struct ieee80211_key *key;
  885. u8 *pn;
  886. key = container_of(keyconf, struct ieee80211_key, conf);
  887. switch (key->conf.cipher) {
  888. case WLAN_CIPHER_SUITE_TKIP:
  889. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  890. return;
  891. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  892. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  893. break;
  894. case WLAN_CIPHER_SUITE_CCMP:
  895. case WLAN_CIPHER_SUITE_CCMP_256:
  896. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  897. return;
  898. if (tid < 0)
  899. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  900. else
  901. pn = key->u.ccmp.rx_pn[tid];
  902. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  903. break;
  904. case WLAN_CIPHER_SUITE_AES_CMAC:
  905. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  906. if (WARN_ON(tid != 0))
  907. return;
  908. pn = key->u.aes_cmac.rx_pn;
  909. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  910. break;
  911. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  912. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  913. if (WARN_ON(tid != 0))
  914. return;
  915. pn = key->u.aes_gmac.rx_pn;
  916. memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
  917. break;
  918. case WLAN_CIPHER_SUITE_GCMP:
  919. case WLAN_CIPHER_SUITE_GCMP_256:
  920. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  921. return;
  922. if (tid < 0)
  923. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  924. else
  925. pn = key->u.gcmp.rx_pn[tid];
  926. memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
  927. break;
  928. default:
  929. WARN_ON(1);
  930. break;
  931. }
  932. }
  933. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  934. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  935. {
  936. struct ieee80211_key *key;
  937. key = container_of(keyconf, struct ieee80211_key, conf);
  938. assert_key_lock(key->local);
  939. /*
  940. * if key was uploaded, we assume the driver will/has remove(d)
  941. * it, so adjust bookkeeping accordingly
  942. */
  943. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  944. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  945. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  946. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  947. increment_tailroom_need_count(key->sdata);
  948. }
  949. ieee80211_key_free(key, false);
  950. }
  951. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  952. struct ieee80211_key_conf *
  953. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  954. struct ieee80211_key_conf *keyconf)
  955. {
  956. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  957. struct ieee80211_local *local = sdata->local;
  958. struct ieee80211_key *key;
  959. int err;
  960. if (WARN_ON(!local->wowlan))
  961. return ERR_PTR(-EINVAL);
  962. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  963. return ERR_PTR(-EINVAL);
  964. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  965. keyconf->keylen, keyconf->key,
  966. 0, NULL, NULL);
  967. if (IS_ERR(key))
  968. return ERR_CAST(key);
  969. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  970. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  971. err = ieee80211_key_link(key, sdata, NULL);
  972. if (err)
  973. return ERR_PTR(err);
  974. return &key->conf;
  975. }
  976. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);