key.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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-2024 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
  423. new->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  424. }
  425. if (ret)
  426. return ret;
  427. if (new)
  428. list_add_tail_rcu(&new->list, &sdata->key_list);
  429. if (sta) {
  430. if (pairwise) {
  431. rcu_assign_pointer(sta->ptk[idx], new);
  432. if (new &&
  433. !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX))
  434. _ieee80211_set_tx_key(new, true);
  435. } else {
  436. rcu_assign_pointer(link_sta->gtk[idx], new);
  437. }
  438. /* Only needed for transition from no key -> key.
  439. * Still triggers unnecessary when using Extended Key ID
  440. * and installing the second key ID the first time.
  441. */
  442. if (new && !old)
  443. ieee80211_check_fast_rx(sta);
  444. } else {
  445. defunikey = old &&
  446. old == wiphy_dereference(sdata->local->hw.wiphy,
  447. sdata->default_unicast_key);
  448. defmultikey = old &&
  449. old == wiphy_dereference(sdata->local->hw.wiphy,
  450. link->default_multicast_key);
  451. defmgmtkey = old &&
  452. old == wiphy_dereference(sdata->local->hw.wiphy,
  453. link->default_mgmt_key);
  454. defbeaconkey = old &&
  455. old == wiphy_dereference(sdata->local->hw.wiphy,
  456. link->default_beacon_key);
  457. if (defunikey && !new)
  458. __ieee80211_set_default_key(link, -1, true, false);
  459. if (defmultikey && !new)
  460. __ieee80211_set_default_key(link, -1, false, true);
  461. if (defmgmtkey && !new)
  462. __ieee80211_set_default_mgmt_key(link, -1);
  463. if (defbeaconkey && !new)
  464. __ieee80211_set_default_beacon_key(link, -1);
  465. if (is_wep || pairwise)
  466. rcu_assign_pointer(sdata->keys[idx], new);
  467. else
  468. rcu_assign_pointer(link->gtk[idx], new);
  469. if (defunikey && new)
  470. __ieee80211_set_default_key(link, new->conf.keyidx,
  471. true, false);
  472. if (defmultikey && new)
  473. __ieee80211_set_default_key(link, new->conf.keyidx,
  474. false, true);
  475. if (defmgmtkey && new)
  476. __ieee80211_set_default_mgmt_key(link,
  477. new->conf.keyidx);
  478. if (defbeaconkey && new)
  479. __ieee80211_set_default_beacon_key(link,
  480. new->conf.keyidx);
  481. }
  482. if (old)
  483. list_del_rcu(&old->list);
  484. return 0;
  485. }
  486. struct ieee80211_key *
  487. ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  488. const u8 *key_data,
  489. size_t seq_len, const u8 *seq)
  490. {
  491. struct ieee80211_key *key;
  492. int i, j, err;
  493. if (WARN_ON(idx < 0 ||
  494. idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
  495. NUM_DEFAULT_BEACON_KEYS))
  496. return ERR_PTR(-EINVAL);
  497. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  498. if (!key)
  499. return ERR_PTR(-ENOMEM);
  500. /*
  501. * Default to software encryption; we'll later upload the
  502. * key to the hardware if possible.
  503. */
  504. key->conf.flags = 0;
  505. key->flags = 0;
  506. key->conf.link_id = -1;
  507. key->conf.cipher = cipher;
  508. key->conf.keyidx = idx;
  509. key->conf.keylen = key_len;
  510. switch (cipher) {
  511. case WLAN_CIPHER_SUITE_WEP40:
  512. case WLAN_CIPHER_SUITE_WEP104:
  513. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  514. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  515. break;
  516. case WLAN_CIPHER_SUITE_TKIP:
  517. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  518. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  519. if (seq) {
  520. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  521. key->u.tkip.rx[i].iv32 =
  522. get_unaligned_le32(&seq[2]);
  523. key->u.tkip.rx[i].iv16 =
  524. get_unaligned_le16(seq);
  525. }
  526. }
  527. spin_lock_init(&key->u.tkip.txlock);
  528. break;
  529. case WLAN_CIPHER_SUITE_CCMP:
  530. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  531. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  532. if (seq) {
  533. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  534. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  535. key->u.ccmp.rx_pn[i][j] =
  536. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  537. }
  538. /*
  539. * Initialize AES key state here as an optimization so that
  540. * it does not need to be initialized for every packet.
  541. */
  542. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  543. key_data, key_len, IEEE80211_CCMP_MIC_LEN);
  544. if (IS_ERR(key->u.ccmp.tfm)) {
  545. err = PTR_ERR(key->u.ccmp.tfm);
  546. kfree(key);
  547. return ERR_PTR(err);
  548. }
  549. break;
  550. case WLAN_CIPHER_SUITE_CCMP_256:
  551. key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
  552. key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
  553. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  554. for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
  555. key->u.ccmp.rx_pn[i][j] =
  556. seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
  557. /* Initialize AES key state here as an optimization so that
  558. * it does not need to be initialized for every packet.
  559. */
  560. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  561. key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
  562. if (IS_ERR(key->u.ccmp.tfm)) {
  563. err = PTR_ERR(key->u.ccmp.tfm);
  564. kfree(key);
  565. return ERR_PTR(err);
  566. }
  567. break;
  568. case WLAN_CIPHER_SUITE_AES_CMAC:
  569. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  570. key->conf.iv_len = 0;
  571. if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  572. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  573. else
  574. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  575. if (seq)
  576. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  577. key->u.aes_cmac.rx_pn[j] =
  578. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  579. /*
  580. * Initialize AES key state here as an optimization so that
  581. * it does not need to be initialized for every packet.
  582. */
  583. key->u.aes_cmac.tfm =
  584. ieee80211_aes_cmac_key_setup(key_data, key_len);
  585. if (IS_ERR(key->u.aes_cmac.tfm)) {
  586. err = PTR_ERR(key->u.aes_cmac.tfm);
  587. kfree(key);
  588. return ERR_PTR(err);
  589. }
  590. break;
  591. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  592. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  593. key->conf.iv_len = 0;
  594. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  595. if (seq)
  596. for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
  597. key->u.aes_gmac.rx_pn[j] =
  598. seq[IEEE80211_GMAC_PN_LEN - j - 1];
  599. /* Initialize AES key state here as an optimization so that
  600. * it does not need to be initialized for every packet.
  601. */
  602. key->u.aes_gmac.tfm =
  603. ieee80211_aes_gmac_key_setup(key_data, key_len);
  604. if (IS_ERR(key->u.aes_gmac.tfm)) {
  605. err = PTR_ERR(key->u.aes_gmac.tfm);
  606. kfree(key);
  607. return ERR_PTR(err);
  608. }
  609. break;
  610. case WLAN_CIPHER_SUITE_GCMP:
  611. case WLAN_CIPHER_SUITE_GCMP_256:
  612. key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
  613. key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
  614. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  615. for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
  616. key->u.gcmp.rx_pn[i][j] =
  617. seq[IEEE80211_GCMP_PN_LEN - j - 1];
  618. /* Initialize AES key state here as an optimization so that
  619. * it does not need to be initialized for every packet.
  620. */
  621. key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
  622. key_len);
  623. if (IS_ERR(key->u.gcmp.tfm)) {
  624. err = PTR_ERR(key->u.gcmp.tfm);
  625. kfree(key);
  626. return ERR_PTR(err);
  627. }
  628. break;
  629. }
  630. memcpy(key->conf.key, key_data, key_len);
  631. INIT_LIST_HEAD(&key->list);
  632. return key;
  633. }
  634. static void ieee80211_key_free_common(struct ieee80211_key *key)
  635. {
  636. switch (key->conf.cipher) {
  637. case WLAN_CIPHER_SUITE_CCMP:
  638. case WLAN_CIPHER_SUITE_CCMP_256:
  639. ieee80211_aes_key_free(key->u.ccmp.tfm);
  640. break;
  641. case WLAN_CIPHER_SUITE_AES_CMAC:
  642. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  643. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  644. break;
  645. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  646. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  647. ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
  648. break;
  649. case WLAN_CIPHER_SUITE_GCMP:
  650. case WLAN_CIPHER_SUITE_GCMP_256:
  651. ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
  652. break;
  653. }
  654. kfree_sensitive(key);
  655. }
  656. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  657. bool delay_tailroom)
  658. {
  659. if (key->local) {
  660. struct ieee80211_sub_if_data *sdata = key->sdata;
  661. ieee80211_debugfs_key_remove(key);
  662. if (delay_tailroom) {
  663. /* see ieee80211_delayed_tailroom_dec */
  664. sdata->crypto_tx_tailroom_pending_dec++;
  665. wiphy_delayed_work_queue(sdata->local->hw.wiphy,
  666. &sdata->dec_tailroom_needed_wk,
  667. HZ / 2);
  668. } else {
  669. decrease_tailroom_need_count(sdata, 1);
  670. }
  671. }
  672. ieee80211_key_free_common(key);
  673. }
  674. static void ieee80211_key_destroy(struct ieee80211_key *key,
  675. bool delay_tailroom)
  676. {
  677. if (!key)
  678. return;
  679. /*
  680. * Synchronize so the TX path and rcu key iterators
  681. * can no longer be using this key before we free/remove it.
  682. */
  683. synchronize_net();
  684. __ieee80211_key_destroy(key, delay_tailroom);
  685. }
  686. void ieee80211_key_free_unused(struct ieee80211_key *key)
  687. {
  688. if (!key)
  689. return;
  690. WARN_ON(key->sdata || key->local);
  691. ieee80211_key_free_common(key);
  692. }
  693. static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
  694. struct ieee80211_key *old,
  695. struct ieee80211_key *new)
  696. {
  697. u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
  698. u8 *tk_old, *tk_new;
  699. if (!old || new->conf.keylen != old->conf.keylen)
  700. return false;
  701. tk_old = old->conf.key;
  702. tk_new = new->conf.key;
  703. /*
  704. * In station mode, don't compare the TX MIC key, as it's never used
  705. * and offloaded rekeying may not care to send it to the host. This
  706. * is the case in iwlwifi, for example.
  707. */
  708. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  709. new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
  710. new->conf.keylen == WLAN_KEY_LEN_TKIP &&
  711. !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  712. memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
  713. memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
  714. memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
  715. memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
  716. tk_old = tkip_old;
  717. tk_new = tkip_new;
  718. }
  719. return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
  720. }
  721. int ieee80211_key_link(struct ieee80211_key *key,
  722. struct ieee80211_link_data *link,
  723. struct sta_info *sta)
  724. {
  725. struct ieee80211_sub_if_data *sdata = link->sdata;
  726. static atomic_t key_color = ATOMIC_INIT(0);
  727. struct ieee80211_key *old_key = NULL;
  728. int idx = key->conf.keyidx;
  729. bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  730. /*
  731. * We want to delay tailroom updates only for station - in that
  732. * case it helps roaming speed, but in other cases it hurts and
  733. * can cause warnings to appear.
  734. */
  735. bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
  736. int ret;
  737. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  738. if (sta && pairwise) {
  739. struct ieee80211_key *alt_key;
  740. old_key = wiphy_dereference(sdata->local->hw.wiphy,
  741. sta->ptk[idx]);
  742. alt_key = wiphy_dereference(sdata->local->hw.wiphy,
  743. sta->ptk[idx ^ 1]);
  744. /* The rekey code assumes that the old and new key are using
  745. * the same cipher. Enforce the assumption for pairwise keys.
  746. */
  747. if ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
  748. (old_key && old_key->conf.cipher != key->conf.cipher)) {
  749. ret = -EOPNOTSUPP;
  750. goto out;
  751. }
  752. } else if (sta) {
  753. struct link_sta_info *link_sta = &sta->deflink;
  754. int link_id = key->conf.link_id;
  755. if (link_id >= 0) {
  756. link_sta = rcu_dereference_protected(sta->link[link_id],
  757. lockdep_is_held(&sta->local->hw.wiphy->mtx));
  758. if (!link_sta) {
  759. ret = -ENOLINK;
  760. goto out;
  761. }
  762. }
  763. old_key = wiphy_dereference(sdata->local->hw.wiphy,
  764. link_sta->gtk[idx]);
  765. } else {
  766. if (idx < NUM_DEFAULT_KEYS)
  767. old_key = wiphy_dereference(sdata->local->hw.wiphy,
  768. sdata->keys[idx]);
  769. if (!old_key)
  770. old_key = wiphy_dereference(sdata->local->hw.wiphy,
  771. link->gtk[idx]);
  772. }
  773. /* Non-pairwise keys must also not switch the cipher on rekey */
  774. if (!pairwise) {
  775. if (old_key && old_key->conf.cipher != key->conf.cipher) {
  776. ret = -EOPNOTSUPP;
  777. goto out;
  778. }
  779. }
  780. /*
  781. * Silently accept key re-installation without really installing the
  782. * new version of the key to avoid nonce reuse or replay issues.
  783. */
  784. if (ieee80211_key_identical(sdata, old_key, key)) {
  785. ret = -EALREADY;
  786. goto out;
  787. }
  788. key->local = sdata->local;
  789. key->sdata = sdata;
  790. key->sta = sta;
  791. /*
  792. * Assign a unique ID to every key so we can easily prevent mixed
  793. * key and fragment cache attacks.
  794. */
  795. key->color = atomic_inc_return(&key_color);
  796. /* keep this flag for easier access later */
  797. if (sta && sta->sta.spp_amsdu)
  798. key->conf.flags |= IEEE80211_KEY_FLAG_SPP_AMSDU;
  799. increment_tailroom_need_count(sdata);
  800. ret = ieee80211_key_replace(sdata, link, sta, pairwise, old_key, key);
  801. if (!ret) {
  802. ieee80211_debugfs_key_add(key);
  803. ieee80211_key_destroy(old_key, delay_tailroom);
  804. } else {
  805. ieee80211_key_free(key, delay_tailroom);
  806. }
  807. key = NULL;
  808. out:
  809. ieee80211_key_free_unused(key);
  810. return ret;
  811. }
  812. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  813. {
  814. if (!key)
  815. return;
  816. /*
  817. * Replace key with nothingness if it was ever used.
  818. */
  819. if (key->sdata)
  820. ieee80211_key_replace(key->sdata, NULL, key->sta,
  821. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  822. key, NULL);
  823. ieee80211_key_destroy(key, delay_tailroom);
  824. }
  825. void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata)
  826. {
  827. struct ieee80211_key *key;
  828. struct ieee80211_sub_if_data *vlan;
  829. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  830. sdata->crypto_tx_tailroom_needed_cnt = 0;
  831. sdata->crypto_tx_tailroom_pending_dec = 0;
  832. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  833. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
  834. vlan->crypto_tx_tailroom_needed_cnt = 0;
  835. vlan->crypto_tx_tailroom_pending_dec = 0;
  836. }
  837. }
  838. if (ieee80211_sdata_running(sdata)) {
  839. list_for_each_entry(key, &sdata->key_list, list) {
  840. increment_tailroom_need_count(sdata);
  841. ieee80211_key_enable_hw_accel(key);
  842. }
  843. }
  844. }
  845. static void
  846. ieee80211_key_iter(struct ieee80211_hw *hw,
  847. struct ieee80211_vif *vif,
  848. struct ieee80211_key *key,
  849. void (*iter)(struct ieee80211_hw *hw,
  850. struct ieee80211_vif *vif,
  851. struct ieee80211_sta *sta,
  852. struct ieee80211_key_conf *key,
  853. void *data),
  854. void *iter_data)
  855. {
  856. /* skip keys of station in removal process */
  857. if (key->sta && key->sta->removed)
  858. return;
  859. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  860. return;
  861. iter(hw, vif, key->sta ? &key->sta->sta : NULL,
  862. &key->conf, iter_data);
  863. }
  864. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  865. struct ieee80211_vif *vif,
  866. void (*iter)(struct ieee80211_hw *hw,
  867. struct ieee80211_vif *vif,
  868. struct ieee80211_sta *sta,
  869. struct ieee80211_key_conf *key,
  870. void *data),
  871. void *iter_data)
  872. {
  873. struct ieee80211_local *local = hw_to_local(hw);
  874. struct ieee80211_key *key, *tmp;
  875. struct ieee80211_sub_if_data *sdata;
  876. lockdep_assert_wiphy(hw->wiphy);
  877. if (vif) {
  878. sdata = vif_to_sdata(vif);
  879. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  880. ieee80211_key_iter(hw, vif, key, iter, iter_data);
  881. } else {
  882. list_for_each_entry(sdata, &local->interfaces, list)
  883. list_for_each_entry_safe(key, tmp,
  884. &sdata->key_list, list)
  885. ieee80211_key_iter(hw, &sdata->vif, key,
  886. iter, iter_data);
  887. }
  888. }
  889. EXPORT_SYMBOL(ieee80211_iter_keys);
  890. static void
  891. _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  892. struct ieee80211_sub_if_data *sdata,
  893. void (*iter)(struct ieee80211_hw *hw,
  894. struct ieee80211_vif *vif,
  895. struct ieee80211_sta *sta,
  896. struct ieee80211_key_conf *key,
  897. void *data),
  898. void *iter_data)
  899. {
  900. struct ieee80211_key *key;
  901. list_for_each_entry_rcu(key, &sdata->key_list, list)
  902. ieee80211_key_iter(hw, &sdata->vif, key, iter, iter_data);
  903. }
  904. void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  905. struct ieee80211_vif *vif,
  906. void (*iter)(struct ieee80211_hw *hw,
  907. struct ieee80211_vif *vif,
  908. struct ieee80211_sta *sta,
  909. struct ieee80211_key_conf *key,
  910. void *data),
  911. void *iter_data)
  912. {
  913. struct ieee80211_local *local = hw_to_local(hw);
  914. struct ieee80211_sub_if_data *sdata;
  915. if (vif) {
  916. sdata = vif_to_sdata(vif);
  917. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  918. } else {
  919. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  920. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  921. }
  922. }
  923. EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
  924. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  925. struct list_head *keys)
  926. {
  927. struct ieee80211_key *key, *tmp;
  928. decrease_tailroom_need_count(sdata,
  929. sdata->crypto_tx_tailroom_pending_dec);
  930. sdata->crypto_tx_tailroom_pending_dec = 0;
  931. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  932. ieee80211_debugfs_key_remove_beacon_default(sdata);
  933. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  934. ieee80211_key_replace(key->sdata, NULL, key->sta,
  935. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  936. key, NULL);
  937. list_add_tail(&key->list, keys);
  938. }
  939. ieee80211_debugfs_key_update_default(sdata);
  940. }
  941. void ieee80211_remove_link_keys(struct ieee80211_link_data *link,
  942. struct list_head *keys)
  943. {
  944. struct ieee80211_sub_if_data *sdata = link->sdata;
  945. struct ieee80211_local *local = sdata->local;
  946. struct ieee80211_key *key, *tmp;
  947. lockdep_assert_wiphy(local->hw.wiphy);
  948. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  949. if (key->conf.link_id != link->link_id)
  950. continue;
  951. ieee80211_key_replace(key->sdata, link, key->sta,
  952. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  953. key, NULL);
  954. list_add_tail(&key->list, keys);
  955. }
  956. }
  957. void ieee80211_free_key_list(struct ieee80211_local *local,
  958. struct list_head *keys)
  959. {
  960. struct ieee80211_key *key, *tmp;
  961. lockdep_assert_wiphy(local->hw.wiphy);
  962. list_for_each_entry_safe(key, tmp, keys, list)
  963. __ieee80211_key_destroy(key, false);
  964. }
  965. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  966. bool force_synchronize)
  967. {
  968. struct ieee80211_local *local = sdata->local;
  969. struct ieee80211_sub_if_data *vlan;
  970. struct ieee80211_sub_if_data *master;
  971. struct ieee80211_key *key, *tmp;
  972. LIST_HEAD(keys);
  973. wiphy_delayed_work_cancel(local->hw.wiphy,
  974. &sdata->dec_tailroom_needed_wk);
  975. lockdep_assert_wiphy(local->hw.wiphy);
  976. ieee80211_free_keys_iface(sdata, &keys);
  977. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  978. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  979. ieee80211_free_keys_iface(vlan, &keys);
  980. }
  981. if (!list_empty(&keys) || force_synchronize)
  982. synchronize_net();
  983. list_for_each_entry_safe(key, tmp, &keys, list)
  984. __ieee80211_key_destroy(key, false);
  985. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  986. if (sdata->bss) {
  987. master = container_of(sdata->bss,
  988. struct ieee80211_sub_if_data,
  989. u.ap);
  990. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
  991. master->crypto_tx_tailroom_needed_cnt);
  992. }
  993. } else {
  994. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  995. sdata->crypto_tx_tailroom_pending_dec);
  996. }
  997. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  998. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  999. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  1000. vlan->crypto_tx_tailroom_pending_dec);
  1001. }
  1002. }
  1003. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  1004. struct sta_info *sta)
  1005. {
  1006. struct ieee80211_key *key;
  1007. int i;
  1008. lockdep_assert_wiphy(local->hw.wiphy);
  1009. for (i = 0; i < ARRAY_SIZE(sta->deflink.gtk); i++) {
  1010. key = wiphy_dereference(local->hw.wiphy, sta->deflink.gtk[i]);
  1011. if (!key)
  1012. continue;
  1013. ieee80211_key_replace(key->sdata, NULL, key->sta,
  1014. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  1015. key, NULL);
  1016. __ieee80211_key_destroy(key, key->sdata->vif.type ==
  1017. NL80211_IFTYPE_STATION);
  1018. }
  1019. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1020. key = wiphy_dereference(local->hw.wiphy, sta->ptk[i]);
  1021. if (!key)
  1022. continue;
  1023. ieee80211_key_replace(key->sdata, NULL, key->sta,
  1024. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  1025. key, NULL);
  1026. __ieee80211_key_destroy(key, key->sdata->vif.type ==
  1027. NL80211_IFTYPE_STATION);
  1028. }
  1029. }
  1030. void ieee80211_delayed_tailroom_dec(struct wiphy *wiphy,
  1031. struct wiphy_work *wk)
  1032. {
  1033. struct ieee80211_sub_if_data *sdata;
  1034. sdata = container_of(wk, struct ieee80211_sub_if_data,
  1035. dec_tailroom_needed_wk.work);
  1036. /*
  1037. * The reason for the delayed tailroom needed decrementing is to
  1038. * make roaming faster: during roaming, all keys are first deleted
  1039. * and then new keys are installed. The first new key causes the
  1040. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  1041. * the cost of synchronize_net() (which can be slow). Avoid this
  1042. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  1043. * key removal for a while, so if we roam the value is larger than
  1044. * zero and no 0->1 transition happens.
  1045. *
  1046. * The cost is that if the AP switching was from an AP with keys
  1047. * to one without, we still allocate tailroom while it would no
  1048. * longer be needed. However, in the typical (fast) roaming case
  1049. * within an ESS this usually won't happen.
  1050. */
  1051. decrease_tailroom_need_count(sdata,
  1052. sdata->crypto_tx_tailroom_pending_dec);
  1053. sdata->crypto_tx_tailroom_pending_dec = 0;
  1054. }
  1055. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  1056. const u8 *replay_ctr, gfp_t gfp)
  1057. {
  1058. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1059. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  1060. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  1061. }
  1062. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  1063. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  1064. int tid, struct ieee80211_key_seq *seq)
  1065. {
  1066. struct ieee80211_key *key;
  1067. const u8 *pn;
  1068. key = container_of(keyconf, struct ieee80211_key, conf);
  1069. switch (key->conf.cipher) {
  1070. case WLAN_CIPHER_SUITE_TKIP:
  1071. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  1072. return;
  1073. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  1074. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  1075. break;
  1076. case WLAN_CIPHER_SUITE_CCMP:
  1077. case WLAN_CIPHER_SUITE_CCMP_256:
  1078. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1079. return;
  1080. if (tid < 0)
  1081. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  1082. else
  1083. pn = key->u.ccmp.rx_pn[tid];
  1084. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  1085. break;
  1086. case WLAN_CIPHER_SUITE_AES_CMAC:
  1087. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1088. if (WARN_ON(tid != 0))
  1089. return;
  1090. pn = key->u.aes_cmac.rx_pn;
  1091. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  1092. break;
  1093. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1094. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1095. if (WARN_ON(tid != 0))
  1096. return;
  1097. pn = key->u.aes_gmac.rx_pn;
  1098. memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
  1099. break;
  1100. case WLAN_CIPHER_SUITE_GCMP:
  1101. case WLAN_CIPHER_SUITE_GCMP_256:
  1102. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1103. return;
  1104. if (tid < 0)
  1105. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  1106. else
  1107. pn = key->u.gcmp.rx_pn[tid];
  1108. memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
  1109. break;
  1110. }
  1111. }
  1112. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  1113. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  1114. int tid, struct ieee80211_key_seq *seq)
  1115. {
  1116. struct ieee80211_key *key;
  1117. u8 *pn;
  1118. key = container_of(keyconf, struct ieee80211_key, conf);
  1119. switch (key->conf.cipher) {
  1120. case WLAN_CIPHER_SUITE_TKIP:
  1121. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  1122. return;
  1123. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  1124. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  1125. break;
  1126. case WLAN_CIPHER_SUITE_CCMP:
  1127. case WLAN_CIPHER_SUITE_CCMP_256:
  1128. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1129. return;
  1130. if (tid < 0)
  1131. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  1132. else
  1133. pn = key->u.ccmp.rx_pn[tid];
  1134. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  1135. break;
  1136. case WLAN_CIPHER_SUITE_AES_CMAC:
  1137. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1138. if (WARN_ON(tid != 0))
  1139. return;
  1140. pn = key->u.aes_cmac.rx_pn;
  1141. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  1142. break;
  1143. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1144. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1145. if (WARN_ON(tid != 0))
  1146. return;
  1147. pn = key->u.aes_gmac.rx_pn;
  1148. memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
  1149. break;
  1150. case WLAN_CIPHER_SUITE_GCMP:
  1151. case WLAN_CIPHER_SUITE_GCMP_256:
  1152. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  1153. return;
  1154. if (tid < 0)
  1155. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  1156. else
  1157. pn = key->u.gcmp.rx_pn[tid];
  1158. memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
  1159. break;
  1160. default:
  1161. WARN_ON(1);
  1162. break;
  1163. }
  1164. }
  1165. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  1166. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  1167. {
  1168. struct ieee80211_key *key;
  1169. key = container_of(keyconf, struct ieee80211_key, conf);
  1170. lockdep_assert_wiphy(key->local->hw.wiphy);
  1171. /*
  1172. * if key was uploaded, we assume the driver will/has remove(d)
  1173. * it, so adjust bookkeeping accordingly
  1174. */
  1175. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  1176. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  1177. if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
  1178. IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
  1179. IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  1180. increment_tailroom_need_count(key->sdata);
  1181. }
  1182. ieee80211_key_free(key, false);
  1183. }
  1184. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  1185. struct ieee80211_key_conf *
  1186. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  1187. struct ieee80211_key_conf *keyconf,
  1188. int link_id)
  1189. {
  1190. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1191. struct ieee80211_local *local = sdata->local;
  1192. struct ieee80211_key *key;
  1193. int err;
  1194. struct ieee80211_link_data *link_data =
  1195. link_id < 0 ? &sdata->deflink :
  1196. sdata_dereference(sdata->link[link_id], sdata);
  1197. if (WARN_ON(!link_data))
  1198. return ERR_PTR(-EINVAL);
  1199. if (WARN_ON(!local->wowlan))
  1200. return ERR_PTR(-EINVAL);
  1201. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1202. return ERR_PTR(-EINVAL);
  1203. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  1204. keyconf->keylen, keyconf->key,
  1205. 0, NULL);
  1206. if (IS_ERR(key))
  1207. return ERR_CAST(key);
  1208. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  1209. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  1210. key->conf.link_id = link_id;
  1211. err = ieee80211_key_link(key, link_data, NULL);
  1212. if (err)
  1213. return ERR_PTR(err);
  1214. return &key->conf;
  1215. }
  1216. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
  1217. void ieee80211_key_mic_failure(struct ieee80211_key_conf *keyconf)
  1218. {
  1219. struct ieee80211_key *key;
  1220. key = container_of(keyconf, struct ieee80211_key, conf);
  1221. switch (key->conf.cipher) {
  1222. case WLAN_CIPHER_SUITE_AES_CMAC:
  1223. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1224. key->u.aes_cmac.icverrors++;
  1225. break;
  1226. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1227. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1228. key->u.aes_gmac.icverrors++;
  1229. break;
  1230. default:
  1231. /* ignore the others for now, we don't keep counters now */
  1232. break;
  1233. }
  1234. }
  1235. EXPORT_SYMBOL_GPL(ieee80211_key_mic_failure);
  1236. void ieee80211_key_replay(struct ieee80211_key_conf *keyconf)
  1237. {
  1238. struct ieee80211_key *key;
  1239. key = container_of(keyconf, struct ieee80211_key, conf);
  1240. switch (key->conf.cipher) {
  1241. case WLAN_CIPHER_SUITE_CCMP:
  1242. case WLAN_CIPHER_SUITE_CCMP_256:
  1243. key->u.ccmp.replays++;
  1244. break;
  1245. case WLAN_CIPHER_SUITE_AES_CMAC:
  1246. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1247. key->u.aes_cmac.replays++;
  1248. break;
  1249. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1250. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1251. key->u.aes_gmac.replays++;
  1252. break;
  1253. case WLAN_CIPHER_SUITE_GCMP:
  1254. case WLAN_CIPHER_SUITE_GCMP_256:
  1255. key->u.gcmp.replays++;
  1256. break;
  1257. }
  1258. }
  1259. EXPORT_SYMBOL_GPL(ieee80211_key_replay);
  1260. int ieee80211_key_switch_links(struct ieee80211_sub_if_data *sdata,
  1261. unsigned long del_links_mask,
  1262. unsigned long add_links_mask)
  1263. {
  1264. struct ieee80211_key *key;
  1265. int ret;
  1266. list_for_each_entry(key, &sdata->key_list, list) {
  1267. if (key->conf.link_id < 0 ||
  1268. !(del_links_mask & BIT(key->conf.link_id)))
  1269. continue;
  1270. /* shouldn't happen for per-link keys */
  1271. WARN_ON(key->sta);
  1272. ieee80211_key_disable_hw_accel(key);
  1273. }
  1274. list_for_each_entry(key, &sdata->key_list, list) {
  1275. if (key->conf.link_id < 0 ||
  1276. !(add_links_mask & BIT(key->conf.link_id)))
  1277. continue;
  1278. /* shouldn't happen for per-link keys */
  1279. WARN_ON(key->sta);
  1280. ret = ieee80211_key_enable_hw_accel(key);
  1281. if (ret)
  1282. return ret;
  1283. }
  1284. return 0;
  1285. }