key.c 34 KB

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