key.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 <[email protected]>
  6. * Copyright 2007-2008 Johannes Berg <[email protected]>
  7. * Copyright 2013-2014 Intel Mobile Communications GmbH
  8. * Copyright 2015-2017 Intel Deutschland GmbH
  9. * Copyright 2018-2020, 2022 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/[email protected]
  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. if (key->conf.link_id >= 0 && sdata->vif.active_links &&
  152. !(sdata->vif.active_links & BIT(key->conf.link_id)))
  153. return 0;
  154. ret = drv_set_key(key->local, SET_KEY, sdata,
  155. sta ? &sta->sta : NULL, &key->conf);
  156. if (!ret) {
  157. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  158. if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
  159. IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
  160. IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  161. decrease_tailroom_need_count(sdata, 1);
  162. WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  163. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
  164. WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
  165. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
  166. return 0;
  167. }
  168. if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
  169. sdata_err(sdata,
  170. "failed to set key (%d, %pM) to hardware (%d)\n",
  171. key->conf.keyidx,
  172. sta ? sta->sta.addr : bcast_addr, ret);
  173. out_unsupported:
  174. switch (key->conf.cipher) {
  175. case WLAN_CIPHER_SUITE_WEP40:
  176. case WLAN_CIPHER_SUITE_WEP104:
  177. case WLAN_CIPHER_SUITE_TKIP:
  178. case WLAN_CIPHER_SUITE_CCMP:
  179. case WLAN_CIPHER_SUITE_CCMP_256:
  180. case WLAN_CIPHER_SUITE_GCMP:
  181. case WLAN_CIPHER_SUITE_GCMP_256:
  182. case WLAN_CIPHER_SUITE_AES_CMAC:
  183. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  184. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  185. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  186. /* all of these we can do in software - if driver can */
  187. if (ret == 1)
  188. return 0;
  189. if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
  190. return -EINVAL;
  191. return 0;
  192. default:
  193. return -EINVAL;
  194. }
  195. }
  196. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  197. {
  198. struct ieee80211_sub_if_data *sdata;
  199. struct sta_info *sta;
  200. int ret;
  201. might_sleep();
  202. if (!key || !key->local->ops->set_key)
  203. return;
  204. assert_key_lock(key->local);
  205. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  206. return;
  207. sta = key->sta;
  208. sdata = key->sdata;
  209. if (key->conf.link_id >= 0 && sdata->vif.active_links &&
  210. !(sdata->vif.active_links & BIT(key->conf.link_id)))
  211. return;
  212. if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
  213. IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
  214. IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  215. increment_tailroom_need_count(sdata);
  216. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  217. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  218. sta ? &sta->sta : NULL, &key->conf);
  219. if (ret)
  220. sdata_err(sdata,
  221. "failed to remove key (%d, %pM) from hardware (%d)\n",
  222. key->conf.keyidx,
  223. sta ? sta->sta.addr : bcast_addr, ret);
  224. }
  225. static int _ieee80211_set_tx_key(struct ieee80211_key *key, bool force)
  226. {
  227. struct sta_info *sta = key->sta;
  228. struct ieee80211_local *local = key->local;
  229. assert_key_lock(local);
  230. set_sta_flag(sta, WLAN_STA_USES_ENCRYPTION);
  231. sta->ptk_idx = key->conf.keyidx;
  232. if (force || !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT))
  233. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  234. ieee80211_check_fast_xmit(sta);
  235. return 0;
  236. }
  237. int ieee80211_set_tx_key(struct ieee80211_key *key)
  238. {
  239. return _ieee80211_set_tx_key(key, false);
  240. }
  241. static void ieee80211_pairwise_rekey(struct ieee80211_key *old,
  242. struct ieee80211_key *new)
  243. {
  244. struct ieee80211_local *local = new->local;
  245. struct sta_info *sta = new->sta;
  246. int i;
  247. assert_key_lock(local);
  248. if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) {
  249. /* Extended Key ID key install, initial one or rekey */
  250. if (sta->ptk_idx != INVALID_PTK_KEYIDX &&
  251. !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) {
  252. /* Aggregation Sessions with Extended Key ID must not
  253. * mix MPDUs with different keyIDs within one A-MPDU.
  254. * Tear down running Tx aggregation sessions and block
  255. * new Rx/Tx aggregation requests during rekey to
  256. * ensure there are no A-MPDUs when the driver is not
  257. * supporting A-MPDU key borders. (Blocking Tx only
  258. * would be sufficient but WLAN_STA_BLOCK_BA gets the
  259. * job done for the few ms we need it.)
  260. */
  261. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  262. mutex_lock(&sta->ampdu_mlme.mtx);
  263. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  264. ___ieee80211_stop_tx_ba_session(sta, i,
  265. AGG_STOP_LOCAL_REQUEST);
  266. mutex_unlock(&sta->ampdu_mlme.mtx);
  267. }
  268. } else if (old) {
  269. /* Rekey without Extended Key ID.
  270. * Aggregation sessions are OK when running on SW crypto.
  271. * A broken remote STA may cause issues not observed with HW
  272. * crypto, though.
  273. */
  274. if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  275. return;
  276. /* Stop Tx till we are on the new key */
  277. old->flags |= KEY_FLAG_TAINTED;
  278. ieee80211_clear_fast_xmit(sta);
  279. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
  280. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  281. ieee80211_sta_tear_down_BA_sessions(sta,
  282. AGG_STOP_LOCAL_REQUEST);
  283. }
  284. if (!wiphy_ext_feature_isset(local->hw.wiphy,
  285. NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
  286. pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
  287. sta->sta.addr);
  288. /* Flushing the driver queues *may* help prevent
  289. * the clear text leaks and freezes.
  290. */
  291. ieee80211_flush_queues(local, old->sdata, false);
  292. }
  293. }
  294. }
  295. static void __ieee80211_set_default_key(struct ieee80211_link_data *link,
  296. int idx, bool uni, bool multi)
  297. {
  298. struct ieee80211_sub_if_data *sdata = link->sdata;
  299. struct ieee80211_key *key = NULL;
  300. assert_key_lock(sdata->local);
  301. if (idx >= 0 && idx < NUM_DEFAULT_KEYS) {
  302. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  303. if (!key)
  304. key = key_mtx_dereference(sdata->local, link->gtk[idx]);
  305. }
  306. if (uni) {
  307. rcu_assign_pointer(sdata->default_unicast_key, key);
  308. ieee80211_check_fast_xmit_iface(sdata);
  309. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  310. drv_set_default_unicast_key(sdata->local, sdata, idx);
  311. }
  312. if (multi)
  313. rcu_assign_pointer(link->default_multicast_key, key);
  314. ieee80211_debugfs_key_update_default(sdata);
  315. }
  316. void ieee80211_set_default_key(struct ieee80211_link_data *link, int idx,
  317. bool uni, bool multi)
  318. {
  319. mutex_lock(&link->sdata->local->key_mtx);
  320. __ieee80211_set_default_key(link, idx, uni, multi);
  321. mutex_unlock(&link->sdata->local->key_mtx);
  322. }
  323. static void
  324. __ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link, int idx)
  325. {
  326. struct ieee80211_sub_if_data *sdata = link->sdata;
  327. struct ieee80211_key *key = NULL;
  328. assert_key_lock(sdata->local);
  329. if (idx >= NUM_DEFAULT_KEYS &&
  330. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  331. key = key_mtx_dereference(sdata->local, link->gtk[idx]);
  332. rcu_assign_pointer(link->default_mgmt_key, key);
  333. ieee80211_debugfs_key_update_default(sdata);
  334. }
  335. void ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link,
  336. int idx)
  337. {
  338. mutex_lock(&link->sdata->local->key_mtx);
  339. __ieee80211_set_default_mgmt_key(link, idx);
  340. mutex_unlock(&link->sdata->local->key_mtx);
  341. }
  342. static void
  343. __ieee80211_set_default_beacon_key(struct ieee80211_link_data *link, int idx)
  344. {
  345. struct ieee80211_sub_if_data *sdata = link->sdata;
  346. struct ieee80211_key *key = NULL;
  347. assert_key_lock(sdata->local);
  348. if (idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS &&
  349. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
  350. NUM_DEFAULT_BEACON_KEYS)
  351. key = key_mtx_dereference(sdata->local, link->gtk[idx]);
  352. rcu_assign_pointer(link->default_beacon_key, key);
  353. ieee80211_debugfs_key_update_default(sdata);
  354. }
  355. void ieee80211_set_default_beacon_key(struct ieee80211_link_data *link,
  356. int idx)
  357. {
  358. mutex_lock(&link->sdata->local->key_mtx);
  359. __ieee80211_set_default_beacon_key(link, idx);
  360. mutex_unlock(&link->sdata->local->key_mtx);
  361. }
  362. static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  363. struct ieee80211_link_data *link,
  364. struct sta_info *sta,
  365. bool pairwise,
  366. struct ieee80211_key *old,
  367. struct ieee80211_key *new)
  368. {
  369. struct link_sta_info *link_sta = sta ? &sta->deflink : NULL;
  370. int link_id;
  371. int idx;
  372. int ret = 0;
  373. bool defunikey, defmultikey, defmgmtkey, defbeaconkey;
  374. bool is_wep;
  375. /* caller must provide at least one old/new */
  376. if (WARN_ON(!new && !old))
  377. return 0;
  378. if (new) {
  379. idx = new->conf.keyidx;
  380. is_wep = new->conf.cipher == WLAN_CIPHER_SUITE_WEP40 ||
  381. new->conf.cipher == WLAN_CIPHER_SUITE_WEP104;
  382. link_id = new->conf.link_id;
  383. } else {
  384. idx = old->conf.keyidx;
  385. is_wep = old->conf.cipher == WLAN_CIPHER_SUITE_WEP40 ||
  386. old->conf.cipher == WLAN_CIPHER_SUITE_WEP104;
  387. link_id = old->conf.link_id;
  388. }
  389. if (WARN(old && old->conf.link_id != link_id,
  390. "old link ID %d doesn't match new link ID %d\n",
  391. old->conf.link_id, link_id))
  392. return -EINVAL;
  393. if (link_id >= 0) {
  394. if (!link) {
  395. link = sdata_dereference(sdata->link[link_id], sdata);
  396. if (!link)
  397. return -ENOLINK;
  398. }
  399. if (sta) {
  400. link_sta = rcu_dereference_protected(sta->link[link_id],
  401. lockdep_is_held(&sta->local->sta_mtx));
  402. if (!link_sta)
  403. return -ENOLINK;
  404. }
  405. } else {
  406. link = &sdata->deflink;
  407. }
  408. if ((is_wep || pairwise) && idx >= NUM_DEFAULT_KEYS)
  409. return -EINVAL;
  410. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  411. if (new && sta && pairwise) {
  412. /* Unicast rekey needs special handling. With Extended Key ID
  413. * old is still NULL for the first rekey.
  414. */
  415. ieee80211_pairwise_rekey(old, new);
  416. }
  417. if (old) {
  418. if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  419. ieee80211_key_disable_hw_accel(old);
  420. if (new)
  421. ret = ieee80211_key_enable_hw_accel(new);
  422. }
  423. } else {
  424. if (!new->local->wowlan)
  425. ret = ieee80211_key_enable_hw_accel(new);
  426. }
  427. if (ret)
  428. return ret;
  429. if (new)
  430. list_add_tail_rcu(&new->list, &sdata->key_list);
  431. if (sta) {
  432. if (pairwise) {
  433. rcu_assign_pointer(sta->ptk[idx], new);
  434. if (new &&
  435. !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX))
  436. _ieee80211_set_tx_key(new, true);
  437. } else {
  438. rcu_assign_pointer(link_sta->gtk[idx], new);
  439. }
  440. /* Only needed for transition from no key -> key.
  441. * Still triggers unnecessary when using Extended Key ID
  442. * and installing the second key ID the first time.
  443. */
  444. if (new && !old)
  445. ieee80211_check_fast_rx(sta);
  446. } else {
  447. defunikey = old &&
  448. old == key_mtx_dereference(sdata->local,
  449. sdata->default_unicast_key);
  450. defmultikey = old &&
  451. old == key_mtx_dereference(sdata->local,
  452. link->default_multicast_key);
  453. defmgmtkey = old &&
  454. old == key_mtx_dereference(sdata->local,
  455. link->default_mgmt_key);
  456. defbeaconkey = old &&
  457. old == key_mtx_dereference(sdata->local,
  458. link->default_beacon_key);
  459. if (defunikey && !new)
  460. __ieee80211_set_default_key(link, -1, true, false);
  461. if (defmultikey && !new)
  462. __ieee80211_set_default_key(link, -1, false, true);
  463. if (defmgmtkey && !new)
  464. __ieee80211_set_default_mgmt_key(link, -1);
  465. if (defbeaconkey && !new)
  466. __ieee80211_set_default_beacon_key(link, -1);
  467. if (is_wep || pairwise)
  468. rcu_assign_pointer(sdata->keys[idx], new);
  469. else
  470. rcu_assign_pointer(link->gtk[idx], new);
  471. if (defunikey && new)
  472. __ieee80211_set_default_key(link, new->conf.keyidx,
  473. true, false);
  474. if (defmultikey && new)
  475. __ieee80211_set_default_key(link, new->conf.keyidx,
  476. false, true);
  477. if (defmgmtkey && new)
  478. __ieee80211_set_default_mgmt_key(link,
  479. new->conf.keyidx);
  480. if (defbeaconkey && new)
  481. __ieee80211_set_default_beacon_key(link,
  482. new->conf.keyidx);
  483. }
  484. if (old)
  485. list_del_rcu(&old->list);
  486. return 0;
  487. }
  488. struct ieee80211_key *
  489. ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  490. const u8 *key_data,
  491. size_t seq_len, const u8 *seq)
  492. {
  493. struct ieee80211_key *key;
  494. int i, j, err;
  495. if (WARN_ON(idx < 0 ||
  496. idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
  497. NUM_DEFAULT_BEACON_KEYS))
  498. return ERR_PTR(-EINVAL);
  499. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  500. if (!key)
  501. return ERR_PTR(-ENOMEM);
  502. /*
  503. * Default to software encryption; we'll later upload the
  504. * key to the hardware if possible.
  505. */
  506. key->conf.flags = 0;
  507. key->flags = 0;
  508. key->conf.link_id = -1;
  509. key->conf.cipher = cipher;
  510. key->conf.keyidx = idx;
  511. key->conf.keylen = key_len;
  512. switch (cipher) {
  513. case WLAN_CIPHER_SUITE_WEP40:
  514. case WLAN_CIPHER_SUITE_WEP104:
  515. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  516. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  517. break;
  518. case WLAN_CIPHER_SUITE_TKIP:
  519. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  520. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  521. if (seq) {
  522. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  523. key->u.tkip.rx[i].iv32 =
  524. get_unaligned_le32(&seq[2]);
  525. key->u.tkip.rx[i].iv16 =
  526. get_unaligned_le16(seq);
  527. }
  528. }
  529. spin_lock_init(&key->u.tkip.txlock);
  530. break;
  531. case WLAN_CIPHER_SUITE_CCMP:
  532. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  533. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  534. if (seq) {
  535. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  536. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  537. key->u.ccmp.rx_pn[i][j] =
  538. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  539. }
  540. /*
  541. * Initialize AES key state here as an optimization so that
  542. * it does not need to be initialized for every packet.
  543. */
  544. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  545. key_data, key_len, IEEE80211_CCMP_MIC_LEN);
  546. if (IS_ERR(key->u.ccmp.tfm)) {
  547. err = PTR_ERR(key->u.ccmp.tfm);
  548. kfree(key);
  549. return ERR_PTR(err);
  550. }
  551. break;
  552. case WLAN_CIPHER_SUITE_CCMP_256:
  553. key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
  554. key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
  555. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  556. for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
  557. key->u.ccmp.rx_pn[i][j] =
  558. seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
  559. /* Initialize AES key state here as an optimization so that
  560. * it does not need to be initialized for every packet.
  561. */
  562. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  563. key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
  564. if (IS_ERR(key->u.ccmp.tfm)) {
  565. err = PTR_ERR(key->u.ccmp.tfm);
  566. kfree(key);
  567. return ERR_PTR(err);
  568. }
  569. break;
  570. case WLAN_CIPHER_SUITE_AES_CMAC:
  571. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  572. key->conf.iv_len = 0;
  573. if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  574. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  575. else
  576. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  577. if (seq)
  578. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  579. key->u.aes_cmac.rx_pn[j] =
  580. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  581. /*
  582. * Initialize AES key state here as an optimization so that
  583. * it does not need to be initialized for every packet.
  584. */
  585. key->u.aes_cmac.tfm =
  586. ieee80211_aes_cmac_key_setup(key_data, key_len);
  587. if (IS_ERR(key->u.aes_cmac.tfm)) {
  588. err = PTR_ERR(key->u.aes_cmac.tfm);
  589. kfree(key);
  590. return ERR_PTR(err);
  591. }
  592. break;
  593. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  594. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  595. key->conf.iv_len = 0;
  596. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  597. if (seq)
  598. for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
  599. key->u.aes_gmac.rx_pn[j] =
  600. seq[IEEE80211_GMAC_PN_LEN - j - 1];
  601. /* Initialize AES key state here as an optimization so that
  602. * it does not need to be initialized for every packet.
  603. */
  604. key->u.aes_gmac.tfm =
  605. ieee80211_aes_gmac_key_setup(key_data, key_len);
  606. if (IS_ERR(key->u.aes_gmac.tfm)) {
  607. err = PTR_ERR(key->u.aes_gmac.tfm);
  608. kfree(key);
  609. return ERR_PTR(err);
  610. }
  611. break;
  612. case WLAN_CIPHER_SUITE_GCMP:
  613. case WLAN_CIPHER_SUITE_GCMP_256:
  614. key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
  615. key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
  616. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  617. for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
  618. key->u.gcmp.rx_pn[i][j] =
  619. seq[IEEE80211_GCMP_PN_LEN - j - 1];
  620. /* Initialize AES key state here as an optimization so that
  621. * it does not need to be initialized for every packet.
  622. */
  623. key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
  624. key_len);
  625. if (IS_ERR(key->u.gcmp.tfm)) {
  626. err = PTR_ERR(key->u.gcmp.tfm);
  627. kfree(key);
  628. return ERR_PTR(err);
  629. }
  630. break;
  631. }
  632. memcpy(key->conf.key, key_data, key_len);
  633. INIT_LIST_HEAD(&key->list);
  634. return key;
  635. }
  636. static void ieee80211_key_free_common(struct ieee80211_key *key)
  637. {
  638. switch (key->conf.cipher) {
  639. case WLAN_CIPHER_SUITE_CCMP:
  640. case WLAN_CIPHER_SUITE_CCMP_256:
  641. ieee80211_aes_key_free(key->u.ccmp.tfm);
  642. break;
  643. case WLAN_CIPHER_SUITE_AES_CMAC:
  644. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  645. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  646. break;
  647. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  648. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  649. ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
  650. break;
  651. case WLAN_CIPHER_SUITE_GCMP:
  652. case WLAN_CIPHER_SUITE_GCMP_256:
  653. ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
  654. break;
  655. }
  656. kfree_sensitive(key);
  657. }
  658. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  659. bool delay_tailroom)
  660. {
  661. if (key->local) {
  662. struct ieee80211_sub_if_data *sdata = key->sdata;
  663. ieee80211_debugfs_key_remove(key);
  664. if (delay_tailroom) {
  665. /* see ieee80211_delayed_tailroom_dec */
  666. sdata->crypto_tx_tailroom_pending_dec++;
  667. schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
  668. HZ/2);
  669. } else {
  670. decrease_tailroom_need_count(sdata, 1);
  671. }
  672. }
  673. ieee80211_key_free_common(key);
  674. }
  675. static void ieee80211_key_destroy(struct ieee80211_key *key,
  676. bool delay_tailroom)
  677. {
  678. if (!key)
  679. return;
  680. /*
  681. * Synchronize so the TX path and rcu key iterators
  682. * can no longer be using this key before we free/remove it.
  683. */
  684. synchronize_net();
  685. __ieee80211_key_destroy(key, delay_tailroom);
  686. }
  687. void ieee80211_key_free_unused(struct ieee80211_key *key)
  688. {
  689. WARN_ON(key->sdata || key->local);
  690. ieee80211_key_free_common(key);
  691. }
  692. static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
  693. struct ieee80211_key *old,
  694. struct ieee80211_key *new)
  695. {
  696. u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
  697. u8 *tk_old, *tk_new;
  698. if (!old || new->conf.keylen != old->conf.keylen)
  699. return false;
  700. tk_old = old->conf.key;
  701. tk_new = new->conf.key;
  702. /*
  703. * In station mode, don't compare the TX MIC key, as it's never used
  704. * and offloaded rekeying may not care to send it to the host. This
  705. * is the case in iwlwifi, for example.
  706. */
  707. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  708. new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
  709. new->conf.keylen == WLAN_KEY_LEN_TKIP &&
  710. !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  711. memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
  712. memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
  713. memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
  714. memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
  715. tk_old = tkip_old;
  716. tk_new = tkip_new;
  717. }
  718. return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
  719. }
  720. int ieee80211_key_link(struct ieee80211_key *key,
  721. struct ieee80211_link_data *link,
  722. struct sta_info *sta)
  723. {
  724. struct ieee80211_sub_if_data *sdata = link->sdata;
  725. static atomic_t key_color = ATOMIC_INIT(0);
  726. struct ieee80211_key *old_key = NULL;
  727. int idx = key->conf.keyidx;
  728. bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  729. /*
  730. * We want to delay tailroom updates only for station - in that
  731. * case it helps roaming speed, but in other cases it hurts and
  732. * can cause warnings to appear.
  733. */
  734. bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
  735. int ret = -EOPNOTSUPP;
  736. mutex_lock(&sdata->local->key_mtx);
  737. if (sta && pairwise) {
  738. struct ieee80211_key *alt_key;
  739. old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
  740. alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
  741. /* The rekey code assumes that the old and new key are using
  742. * the same cipher. Enforce the assumption for pairwise keys.
  743. */
  744. if ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
  745. (old_key && old_key->conf.cipher != key->conf.cipher))
  746. goto out;
  747. } else if (sta) {
  748. struct link_sta_info *link_sta = &sta->deflink;
  749. int link_id = key->conf.link_id;
  750. if (link_id >= 0) {
  751. link_sta = rcu_dereference_protected(sta->link[link_id],
  752. lockdep_is_held(&sta->local->sta_mtx));
  753. if (!link_sta) {
  754. ret = -ENOLINK;
  755. goto out;
  756. }
  757. }
  758. old_key = key_mtx_dereference(sdata->local, link_sta->gtk[idx]);
  759. } else {
  760. if (idx < NUM_DEFAULT_KEYS)
  761. old_key = key_mtx_dereference(sdata->local,
  762. sdata->keys[idx]);
  763. if (!old_key)
  764. old_key = key_mtx_dereference(sdata->local,
  765. link->gtk[idx]);
  766. }
  767. /* Non-pairwise keys must also not switch the cipher on rekey */
  768. if (!pairwise) {
  769. if (old_key && old_key->conf.cipher != key->conf.cipher)
  770. goto out;
  771. }
  772. /*
  773. * Silently accept key re-installation without really installing the
  774. * new version of the key to avoid nonce reuse or replay issues.
  775. */
  776. if (ieee80211_key_identical(sdata, old_key, key)) {
  777. ieee80211_key_free_unused(key);
  778. ret = -EALREADY;
  779. goto out;
  780. }
  781. key->local = sdata->local;
  782. key->sdata = sdata;
  783. key->sta = sta;
  784. /*
  785. * Assign a unique ID to every key so we can easily prevent mixed
  786. * key and fragment cache attacks.
  787. */
  788. key->color = atomic_inc_return(&key_color);
  789. increment_tailroom_need_count(sdata);
  790. ret = ieee80211_key_replace(sdata, link, sta, pairwise, old_key, key);
  791. if (!ret) {
  792. ieee80211_debugfs_key_add(key);
  793. ieee80211_key_destroy(old_key, delay_tailroom);
  794. } else {
  795. ieee80211_key_free(key, delay_tailroom);
  796. }
  797. out:
  798. mutex_unlock(&sdata->local->key_mtx);
  799. return ret;
  800. }
  801. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  802. {
  803. if (!key)
  804. return;
  805. /*
  806. * Replace key with nothingness if it was ever used.
  807. */
  808. if (key->sdata)
  809. ieee80211_key_replace(key->sdata, NULL, key->sta,
  810. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  811. key, NULL);
  812. ieee80211_key_destroy(key, delay_tailroom);
  813. }
  814. void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata)
  815. {
  816. struct ieee80211_key *key;
  817. struct ieee80211_sub_if_data *vlan;
  818. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  819. mutex_lock(&sdata->local->key_mtx);
  820. sdata->crypto_tx_tailroom_needed_cnt = 0;
  821. sdata->crypto_tx_tailroom_pending_dec = 0;
  822. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  823. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
  824. vlan->crypto_tx_tailroom_needed_cnt = 0;
  825. vlan->crypto_tx_tailroom_pending_dec = 0;
  826. }
  827. }
  828. if (ieee80211_sdata_running(sdata)) {
  829. list_for_each_entry(key, &sdata->key_list, list) {
  830. increment_tailroom_need_count(sdata);
  831. ieee80211_key_enable_hw_accel(key);
  832. }
  833. }
  834. mutex_unlock(&sdata->local->key_mtx);
  835. }
  836. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  837. struct ieee80211_vif *vif,
  838. void (*iter)(struct ieee80211_hw *hw,
  839. struct ieee80211_vif *vif,
  840. struct ieee80211_sta *sta,
  841. struct ieee80211_key_conf *key,
  842. void *data),
  843. void *iter_data)
  844. {
  845. struct ieee80211_local *local = hw_to_local(hw);
  846. struct ieee80211_key *key, *tmp;
  847. struct ieee80211_sub_if_data *sdata;
  848. lockdep_assert_wiphy(hw->wiphy);
  849. mutex_lock(&local->key_mtx);
  850. if (vif) {
  851. sdata = vif_to_sdata(vif);
  852. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  853. iter(hw, &sdata->vif,
  854. key->sta ? &key->sta->sta : NULL,
  855. &key->conf, iter_data);
  856. } else {
  857. list_for_each_entry(sdata, &local->interfaces, list)
  858. list_for_each_entry_safe(key, tmp,
  859. &sdata->key_list, list)
  860. iter(hw, &sdata->vif,
  861. key->sta ? &key->sta->sta : NULL,
  862. &key->conf, iter_data);
  863. }
  864. mutex_unlock(&local->key_mtx);
  865. }
  866. EXPORT_SYMBOL(ieee80211_iter_keys);
  867. static void
  868. _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  869. struct ieee80211_sub_if_data *sdata,
  870. void (*iter)(struct ieee80211_hw *hw,
  871. struct ieee80211_vif *vif,
  872. struct ieee80211_sta *sta,
  873. struct ieee80211_key_conf *key,
  874. void *data),
  875. void *iter_data)
  876. {
  877. struct ieee80211_key *key;
  878. list_for_each_entry_rcu(key, &sdata->key_list, list) {
  879. /* skip keys of station in removal process */
  880. if (key->sta && key->sta->removed)
  881. continue;
  882. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  883. continue;
  884. iter(hw, &sdata->vif,
  885. key->sta ? &key->sta->sta : NULL,
  886. &key->conf, iter_data);
  887. }
  888. }
  889. void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  890. struct ieee80211_vif *vif,
  891. void (*iter)(struct ieee80211_hw *hw,
  892. struct ieee80211_vif *vif,
  893. struct ieee80211_sta *sta,
  894. struct ieee80211_key_conf *key,
  895. void *data),
  896. void *iter_data)
  897. {
  898. struct ieee80211_local *local = hw_to_local(hw);
  899. struct ieee80211_sub_if_data *sdata;
  900. if (vif) {
  901. sdata = vif_to_sdata(vif);
  902. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  903. } else {
  904. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  905. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  906. }
  907. }
  908. EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
  909. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  910. struct list_head *keys)
  911. {
  912. struct ieee80211_key *key, *tmp;
  913. decrease_tailroom_need_count(sdata,
  914. sdata->crypto_tx_tailroom_pending_dec);
  915. sdata->crypto_tx_tailroom_pending_dec = 0;
  916. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  917. ieee80211_debugfs_key_remove_beacon_default(sdata);
  918. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  919. ieee80211_key_replace(key->sdata, NULL, key->sta,
  920. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  921. key, NULL);
  922. list_add_tail(&key->list, keys);
  923. }
  924. ieee80211_debugfs_key_update_default(sdata);
  925. }
  926. void ieee80211_remove_link_keys(struct ieee80211_link_data *link,
  927. struct list_head *keys)
  928. {
  929. struct ieee80211_sub_if_data *sdata = link->sdata;
  930. struct ieee80211_local *local = sdata->local;
  931. struct ieee80211_key *key, *tmp;
  932. mutex_lock(&local->key_mtx);
  933. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  934. if (key->conf.link_id != link->link_id)
  935. continue;
  936. ieee80211_key_replace(key->sdata, link, key->sta,
  937. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  938. key, NULL);
  939. list_add_tail(&key->list, keys);
  940. }
  941. mutex_unlock(&local->key_mtx);
  942. }
  943. void ieee80211_free_key_list(struct ieee80211_local *local,
  944. struct list_head *keys)
  945. {
  946. struct ieee80211_key *key, *tmp;
  947. mutex_lock(&local->key_mtx);
  948. list_for_each_entry_safe(key, tmp, keys, list)
  949. __ieee80211_key_destroy(key, false);
  950. mutex_unlock(&local->key_mtx);
  951. }
  952. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  953. bool force_synchronize)
  954. {
  955. struct ieee80211_local *local = sdata->local;
  956. struct ieee80211_sub_if_data *vlan;
  957. struct ieee80211_sub_if_data *master;
  958. struct ieee80211_key *key, *tmp;
  959. LIST_HEAD(keys);
  960. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  961. mutex_lock(&local->key_mtx);
  962. ieee80211_free_keys_iface(sdata, &keys);
  963. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  964. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  965. ieee80211_free_keys_iface(vlan, &keys);
  966. }
  967. if (!list_empty(&keys) || force_synchronize)
  968. synchronize_net();
  969. list_for_each_entry_safe(key, tmp, &keys, list)
  970. __ieee80211_key_destroy(key, false);
  971. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  972. if (sdata->bss) {
  973. master = container_of(sdata->bss,
  974. struct ieee80211_sub_if_data,
  975. u.ap);
  976. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
  977. master->crypto_tx_tailroom_needed_cnt);
  978. }
  979. } else {
  980. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  981. sdata->crypto_tx_tailroom_pending_dec);
  982. }
  983. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  984. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  985. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  986. vlan->crypto_tx_tailroom_pending_dec);
  987. }
  988. mutex_unlock(&local->key_mtx);
  989. }
  990. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  991. struct sta_info *sta)
  992. {
  993. struct ieee80211_key *key;
  994. int i;
  995. mutex_lock(&local->key_mtx);
  996. for (i = 0; i < ARRAY_SIZE(sta->deflink.gtk); i++) {
  997. key = key_mtx_dereference(local, sta->deflink.gtk[i]);
  998. if (!key)
  999. continue;
  1000. ieee80211_key_replace(key->sdata, NULL, key->sta,
  1001. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  1002. key, NULL);
  1003. __ieee80211_key_destroy(key, key->sdata->vif.type ==
  1004. NL80211_IFTYPE_STATION);
  1005. }
  1006. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1007. key = key_mtx_dereference(local, sta->ptk[i]);
  1008. if (!key)
  1009. continue;
  1010. ieee80211_key_replace(key->sdata, NULL, key->sta,
  1011. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  1012. key, NULL);
  1013. __ieee80211_key_destroy(key, key->sdata->vif.type ==
  1014. NL80211_IFTYPE_STATION);
  1015. }
  1016. mutex_unlock(&local->key_mtx);
  1017. }
  1018. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  1019. {
  1020. struct ieee80211_sub_if_data *sdata;
  1021. sdata = container_of(wk, struct ieee80211_sub_if_data,
  1022. dec_tailroom_needed_wk.work);
  1023. /*
  1024. * The reason for the delayed tailroom needed decrementing is to
  1025. * make roaming faster: during roaming, all keys are first deleted
  1026. * and then new keys are installed. The first new key causes the
  1027. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  1028. * the cost of synchronize_net() (which can be slow). Avoid this
  1029. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  1030. * key removal for a while, so if we roam the value is larger than
  1031. * zero and no 0->1 transition happens.
  1032. *
  1033. * The cost is that if the AP switching was from an AP with keys
  1034. * to one without, we still allocate tailroom while it would no
  1035. * longer be needed. However, in the typical (fast) roaming case
  1036. * within an ESS this usually won't happen.
  1037. */
  1038. mutex_lock(&sdata->local->key_mtx);
  1039. decrease_tailroom_need_count(sdata,
  1040. sdata->crypto_tx_tailroom_pending_dec);
  1041. sdata->crypto_tx_tailroom_pending_dec = 0;
  1042. mutex_unlock(&sdata->local->key_mtx);
  1043. }
  1044. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  1045. const u8 *replay_ctr, gfp_t gfp)
  1046. {
  1047. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1048. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  1049. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  1050. }
  1051. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  1052. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  1053. int tid, struct ieee80211_key_seq *seq)
  1054. {
  1055. struct ieee80211_key *key;
  1056. const u8 *pn;
  1057. key = container_of(keyconf, struct ieee80211_key, conf);
  1058. switch (key->conf.cipher) {
  1059. case WLAN_CIPHER_SUITE_TKIP:
  1060. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  1061. return;
  1062. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  1063. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  1064. break;
  1065. case WLAN_CIPHER_SUITE_CCMP:
  1066. case WLAN_CIPHER_SUITE_CCMP_256:
  1067. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1068. return;
  1069. if (tid < 0)
  1070. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  1071. else
  1072. pn = key->u.ccmp.rx_pn[tid];
  1073. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  1074. break;
  1075. case WLAN_CIPHER_SUITE_AES_CMAC:
  1076. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1077. if (WARN_ON(tid != 0))
  1078. return;
  1079. pn = key->u.aes_cmac.rx_pn;
  1080. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  1081. break;
  1082. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1083. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1084. if (WARN_ON(tid != 0))
  1085. return;
  1086. pn = key->u.aes_gmac.rx_pn;
  1087. memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
  1088. break;
  1089. case WLAN_CIPHER_SUITE_GCMP:
  1090. case WLAN_CIPHER_SUITE_GCMP_256:
  1091. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1092. return;
  1093. if (tid < 0)
  1094. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  1095. else
  1096. pn = key->u.gcmp.rx_pn[tid];
  1097. memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
  1098. break;
  1099. }
  1100. }
  1101. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  1102. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  1103. int tid, struct ieee80211_key_seq *seq)
  1104. {
  1105. struct ieee80211_key *key;
  1106. u8 *pn;
  1107. key = container_of(keyconf, struct ieee80211_key, conf);
  1108. switch (key->conf.cipher) {
  1109. case WLAN_CIPHER_SUITE_TKIP:
  1110. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  1111. return;
  1112. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  1113. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  1114. break;
  1115. case WLAN_CIPHER_SUITE_CCMP:
  1116. case WLAN_CIPHER_SUITE_CCMP_256:
  1117. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1118. return;
  1119. if (tid < 0)
  1120. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  1121. else
  1122. pn = key->u.ccmp.rx_pn[tid];
  1123. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  1124. break;
  1125. case WLAN_CIPHER_SUITE_AES_CMAC:
  1126. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1127. if (WARN_ON(tid != 0))
  1128. return;
  1129. pn = key->u.aes_cmac.rx_pn;
  1130. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  1131. break;
  1132. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1133. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1134. if (WARN_ON(tid != 0))
  1135. return;
  1136. pn = key->u.aes_gmac.rx_pn;
  1137. memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
  1138. break;
  1139. case WLAN_CIPHER_SUITE_GCMP:
  1140. case WLAN_CIPHER_SUITE_GCMP_256:
  1141. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1142. return;
  1143. if (tid < 0)
  1144. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  1145. else
  1146. pn = key->u.gcmp.rx_pn[tid];
  1147. memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
  1148. break;
  1149. default:
  1150. WARN_ON(1);
  1151. break;
  1152. }
  1153. }
  1154. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  1155. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  1156. {
  1157. struct ieee80211_key *key;
  1158. key = container_of(keyconf, struct ieee80211_key, conf);
  1159. assert_key_lock(key->local);
  1160. /*
  1161. * if key was uploaded, we assume the driver will/has remove(d)
  1162. * it, so adjust bookkeeping accordingly
  1163. */
  1164. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  1165. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  1166. if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
  1167. IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
  1168. IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  1169. increment_tailroom_need_count(key->sdata);
  1170. }
  1171. ieee80211_key_free(key, false);
  1172. }
  1173. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  1174. struct ieee80211_key_conf *
  1175. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  1176. struct ieee80211_key_conf *keyconf)
  1177. {
  1178. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1179. struct ieee80211_local *local = sdata->local;
  1180. struct ieee80211_key *key;
  1181. int err;
  1182. if (WARN_ON(!local->wowlan))
  1183. return ERR_PTR(-EINVAL);
  1184. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1185. return ERR_PTR(-EINVAL);
  1186. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  1187. keyconf->keylen, keyconf->key,
  1188. 0, NULL);
  1189. if (IS_ERR(key))
  1190. return ERR_CAST(key);
  1191. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  1192. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  1193. /* FIXME: this function needs to get a link ID */
  1194. err = ieee80211_key_link(key, &sdata->deflink, NULL);
  1195. if (err)
  1196. return ERR_PTR(err);
  1197. return &key->conf;
  1198. }
  1199. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
  1200. void ieee80211_key_mic_failure(struct ieee80211_key_conf *keyconf)
  1201. {
  1202. struct ieee80211_key *key;
  1203. key = container_of(keyconf, struct ieee80211_key, conf);
  1204. switch (key->conf.cipher) {
  1205. case WLAN_CIPHER_SUITE_AES_CMAC:
  1206. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1207. key->u.aes_cmac.icverrors++;
  1208. break;
  1209. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1210. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1211. key->u.aes_gmac.icverrors++;
  1212. break;
  1213. default:
  1214. /* ignore the others for now, we don't keep counters now */
  1215. break;
  1216. }
  1217. }
  1218. EXPORT_SYMBOL_GPL(ieee80211_key_mic_failure);
  1219. void ieee80211_key_replay(struct ieee80211_key_conf *keyconf)
  1220. {
  1221. struct ieee80211_key *key;
  1222. key = container_of(keyconf, struct ieee80211_key, conf);
  1223. switch (key->conf.cipher) {
  1224. case WLAN_CIPHER_SUITE_CCMP:
  1225. case WLAN_CIPHER_SUITE_CCMP_256:
  1226. key->u.ccmp.replays++;
  1227. break;
  1228. case WLAN_CIPHER_SUITE_AES_CMAC:
  1229. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1230. key->u.aes_cmac.replays++;
  1231. break;
  1232. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1233. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1234. key->u.aes_gmac.replays++;
  1235. break;
  1236. case WLAN_CIPHER_SUITE_GCMP:
  1237. case WLAN_CIPHER_SUITE_GCMP_256:
  1238. key->u.gcmp.replays++;
  1239. break;
  1240. }
  1241. }
  1242. EXPORT_SYMBOL_GPL(ieee80211_key_replay);
  1243. int ieee80211_key_switch_links(struct ieee80211_sub_if_data *sdata,
  1244. unsigned long del_links_mask,
  1245. unsigned long add_links_mask)
  1246. {
  1247. struct ieee80211_key *key;
  1248. int ret;
  1249. list_for_each_entry(key, &sdata->key_list, list) {
  1250. if (key->conf.link_id < 0 ||
  1251. !(del_links_mask & BIT(key->conf.link_id)))
  1252. continue;
  1253. /* shouldn't happen for per-link keys */
  1254. WARN_ON(key->sta);
  1255. ieee80211_key_disable_hw_accel(key);
  1256. }
  1257. list_for_each_entry(key, &sdata->key_list, list) {
  1258. if (key->conf.link_id < 0 ||
  1259. !(add_links_mask & BIT(key->conf.link_id)))
  1260. continue;
  1261. /* shouldn't happen for per-link keys */
  1262. WARN_ON(key->sta);
  1263. ret = ieee80211_key_enable_hw_accel(key);
  1264. if (ret)
  1265. return ret;
  1266. }
  1267. return 0;
  1268. }