key.c 36 KB

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