key.c 42 KB

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