key.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/if_ether.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/list.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/slab.h>
  17. #include <linux/export.h>
  18. #include <net/mac80211.h>
  19. #include <asm/unaligned.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "debugfs_key.h"
  23. #include "aes_ccm.h"
  24. #include "aes_cmac.h"
  25. /**
  26. * DOC: Key handling basics
  27. *
  28. * Key handling in mac80211 is done based on per-interface (sub_if_data)
  29. * keys and per-station keys. Since each station belongs to an interface,
  30. * each station key also belongs to that interface.
  31. *
  32. * Hardware acceleration is done on a best-effort basis for algorithms
  33. * that are implemented in software, for each key the hardware is asked
  34. * to enable that key for offloading but if it cannot do that the key is
  35. * simply kept for software encryption (unless it is for an algorithm
  36. * that isn't implemented in software).
  37. * There is currently no way of knowing whether a key is handled in SW
  38. * or HW except by looking into debugfs.
  39. *
  40. * All key management is internally protected by a mutex. Within all
  41. * other parts of mac80211, key references are, just as STA structure
  42. * references, protected by RCU. Note, however, that some things are
  43. * unprotected, namely the key->sta dereferences within the hardware
  44. * acceleration functions. This means that sta_info_destroy() must
  45. * remove the key which waits for an RCU grace period.
  46. */
  47. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  48. static void assert_key_lock(struct ieee80211_local *local)
  49. {
  50. lockdep_assert_held(&local->key_mtx);
  51. }
  52. static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
  53. {
  54. /*
  55. * When this count is zero, SKB resizing for allocating tailroom
  56. * for IV or MMIC is skipped. But, this check has created two race
  57. * cases in xmit path while transiting from zero count to one:
  58. *
  59. * 1. SKB resize was skipped because no key was added but just before
  60. * the xmit key is added and SW encryption kicks off.
  61. *
  62. * 2. SKB resize was skipped because all the keys were hw planted but
  63. * just before xmit one of the key is deleted and SW encryption kicks
  64. * off.
  65. *
  66. * In both the above case SW encryption will find not enough space for
  67. * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
  68. *
  69. * Solution has been explained at
  70. * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
  71. */
  72. if (!sdata->crypto_tx_tailroom_needed_cnt++) {
  73. /*
  74. * Flush all XMIT packets currently using HW encryption or no
  75. * encryption at all if the count transition is from 0 -> 1.
  76. */
  77. synchronize_net();
  78. }
  79. }
  80. static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  81. {
  82. struct ieee80211_sub_if_data *sdata;
  83. struct sta_info *sta;
  84. int ret;
  85. might_sleep();
  86. if (!key->local->ops->set_key)
  87. goto out_unsupported;
  88. assert_key_lock(key->local);
  89. sta = key->sta;
  90. /*
  91. * If this is a per-STA GTK, check if it
  92. * is supported; if not, return.
  93. */
  94. if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
  95. !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
  96. goto out_unsupported;
  97. if (sta && !sta->uploaded)
  98. goto out_unsupported;
  99. sdata = key->sdata;
  100. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  101. /*
  102. * The driver doesn't know anything about VLAN interfaces.
  103. * Hence, don't send GTKs for VLAN interfaces to the driver.
  104. */
  105. if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
  106. goto out_unsupported;
  107. }
  108. ret = drv_set_key(key->local, SET_KEY, sdata,
  109. sta ? &sta->sta : NULL, &key->conf);
  110. if (!ret) {
  111. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  112. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  113. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
  114. (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
  115. sdata->crypto_tx_tailroom_needed_cnt--;
  116. WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  117. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
  118. return 0;
  119. }
  120. if (ret != -ENOSPC && ret != -EOPNOTSUPP)
  121. sdata_err(sdata,
  122. "failed to set key (%d, %pM) to hardware (%d)\n",
  123. key->conf.keyidx,
  124. sta ? sta->sta.addr : bcast_addr, ret);
  125. out_unsupported:
  126. switch (key->conf.cipher) {
  127. case WLAN_CIPHER_SUITE_WEP40:
  128. case WLAN_CIPHER_SUITE_WEP104:
  129. case WLAN_CIPHER_SUITE_TKIP:
  130. case WLAN_CIPHER_SUITE_CCMP:
  131. case WLAN_CIPHER_SUITE_AES_CMAC:
  132. /* all of these we can do in software */
  133. return 0;
  134. default:
  135. return -EINVAL;
  136. }
  137. }
  138. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  139. {
  140. struct ieee80211_sub_if_data *sdata;
  141. struct sta_info *sta;
  142. int ret;
  143. might_sleep();
  144. if (!key || !key->local->ops->set_key)
  145. return;
  146. assert_key_lock(key->local);
  147. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  148. return;
  149. sta = key->sta;
  150. sdata = key->sdata;
  151. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  152. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
  153. (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
  154. increment_tailroom_need_count(sdata);
  155. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  156. sta ? &sta->sta : NULL, &key->conf);
  157. if (ret)
  158. sdata_err(sdata,
  159. "failed to remove key (%d, %pM) from hardware (%d)\n",
  160. key->conf.keyidx,
  161. sta ? sta->sta.addr : bcast_addr, ret);
  162. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  163. }
  164. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  165. int idx, bool uni, bool multi)
  166. {
  167. struct ieee80211_key *key = NULL;
  168. assert_key_lock(sdata->local);
  169. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  170. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  171. if (uni) {
  172. rcu_assign_pointer(sdata->default_unicast_key, key);
  173. drv_set_default_unicast_key(sdata->local, sdata, idx);
  174. }
  175. if (multi)
  176. rcu_assign_pointer(sdata->default_multicast_key, key);
  177. ieee80211_debugfs_key_update_default(sdata);
  178. }
  179. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  180. bool uni, bool multi)
  181. {
  182. mutex_lock(&sdata->local->key_mtx);
  183. __ieee80211_set_default_key(sdata, idx, uni, multi);
  184. mutex_unlock(&sdata->local->key_mtx);
  185. }
  186. static void
  187. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  188. {
  189. struct ieee80211_key *key = NULL;
  190. assert_key_lock(sdata->local);
  191. if (idx >= NUM_DEFAULT_KEYS &&
  192. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  193. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  194. rcu_assign_pointer(sdata->default_mgmt_key, key);
  195. ieee80211_debugfs_key_update_default(sdata);
  196. }
  197. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  198. int idx)
  199. {
  200. mutex_lock(&sdata->local->key_mtx);
  201. __ieee80211_set_default_mgmt_key(sdata, idx);
  202. mutex_unlock(&sdata->local->key_mtx);
  203. }
  204. static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  205. struct sta_info *sta,
  206. bool pairwise,
  207. struct ieee80211_key *old,
  208. struct ieee80211_key *new)
  209. {
  210. int idx;
  211. bool defunikey, defmultikey, defmgmtkey;
  212. if (new)
  213. list_add_tail(&new->list, &sdata->key_list);
  214. if (sta && pairwise) {
  215. rcu_assign_pointer(sta->ptk, new);
  216. } else if (sta) {
  217. if (old)
  218. idx = old->conf.keyidx;
  219. else
  220. idx = new->conf.keyidx;
  221. rcu_assign_pointer(sta->gtk[idx], new);
  222. } else {
  223. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  224. if (old)
  225. idx = old->conf.keyidx;
  226. else
  227. idx = new->conf.keyidx;
  228. defunikey = old &&
  229. old == key_mtx_dereference(sdata->local,
  230. sdata->default_unicast_key);
  231. defmultikey = old &&
  232. old == key_mtx_dereference(sdata->local,
  233. sdata->default_multicast_key);
  234. defmgmtkey = old &&
  235. old == key_mtx_dereference(sdata->local,
  236. sdata->default_mgmt_key);
  237. if (defunikey && !new)
  238. __ieee80211_set_default_key(sdata, -1, true, false);
  239. if (defmultikey && !new)
  240. __ieee80211_set_default_key(sdata, -1, false, true);
  241. if (defmgmtkey && !new)
  242. __ieee80211_set_default_mgmt_key(sdata, -1);
  243. rcu_assign_pointer(sdata->keys[idx], new);
  244. if (defunikey && new)
  245. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  246. true, false);
  247. if (defmultikey && new)
  248. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  249. false, true);
  250. if (defmgmtkey && new)
  251. __ieee80211_set_default_mgmt_key(sdata,
  252. new->conf.keyidx);
  253. }
  254. if (old)
  255. list_del(&old->list);
  256. }
  257. struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  258. const u8 *key_data,
  259. size_t seq_len, const u8 *seq)
  260. {
  261. struct ieee80211_key *key;
  262. int i, j, err;
  263. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  264. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  265. if (!key)
  266. return ERR_PTR(-ENOMEM);
  267. /*
  268. * Default to software encryption; we'll later upload the
  269. * key to the hardware if possible.
  270. */
  271. key->conf.flags = 0;
  272. key->flags = 0;
  273. key->conf.cipher = cipher;
  274. key->conf.keyidx = idx;
  275. key->conf.keylen = key_len;
  276. switch (cipher) {
  277. case WLAN_CIPHER_SUITE_WEP40:
  278. case WLAN_CIPHER_SUITE_WEP104:
  279. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  280. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  281. break;
  282. case WLAN_CIPHER_SUITE_TKIP:
  283. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  284. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  285. if (seq) {
  286. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  287. key->u.tkip.rx[i].iv32 =
  288. get_unaligned_le32(&seq[2]);
  289. key->u.tkip.rx[i].iv16 =
  290. get_unaligned_le16(seq);
  291. }
  292. }
  293. spin_lock_init(&key->u.tkip.txlock);
  294. break;
  295. case WLAN_CIPHER_SUITE_CCMP:
  296. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  297. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  298. if (seq) {
  299. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  300. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  301. key->u.ccmp.rx_pn[i][j] =
  302. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  303. }
  304. /*
  305. * Initialize AES key state here as an optimization so that
  306. * it does not need to be initialized for every packet.
  307. */
  308. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  309. if (IS_ERR(key->u.ccmp.tfm)) {
  310. err = PTR_ERR(key->u.ccmp.tfm);
  311. kfree(key);
  312. return ERR_PTR(err);
  313. }
  314. break;
  315. case WLAN_CIPHER_SUITE_AES_CMAC:
  316. key->conf.iv_len = 0;
  317. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  318. if (seq)
  319. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  320. key->u.aes_cmac.rx_pn[j] =
  321. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  322. /*
  323. * Initialize AES key state here as an optimization so that
  324. * it does not need to be initialized for every packet.
  325. */
  326. key->u.aes_cmac.tfm =
  327. ieee80211_aes_cmac_key_setup(key_data);
  328. if (IS_ERR(key->u.aes_cmac.tfm)) {
  329. err = PTR_ERR(key->u.aes_cmac.tfm);
  330. kfree(key);
  331. return ERR_PTR(err);
  332. }
  333. break;
  334. }
  335. memcpy(key->conf.key, key_data, key_len);
  336. INIT_LIST_HEAD(&key->list);
  337. return key;
  338. }
  339. static void ieee80211_key_free_common(struct ieee80211_key *key)
  340. {
  341. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  342. ieee80211_aes_key_free(key->u.ccmp.tfm);
  343. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  344. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  345. kfree(key);
  346. }
  347. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  348. bool delay_tailroom)
  349. {
  350. if (key->local)
  351. ieee80211_key_disable_hw_accel(key);
  352. if (key->local) {
  353. struct ieee80211_sub_if_data *sdata = key->sdata;
  354. ieee80211_debugfs_key_remove(key);
  355. if (delay_tailroom) {
  356. /* see ieee80211_delayed_tailroom_dec */
  357. sdata->crypto_tx_tailroom_pending_dec++;
  358. schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
  359. HZ/2);
  360. } else {
  361. sdata->crypto_tx_tailroom_needed_cnt--;
  362. }
  363. }
  364. ieee80211_key_free_common(key);
  365. }
  366. static void ieee80211_key_destroy(struct ieee80211_key *key,
  367. bool delay_tailroom)
  368. {
  369. if (!key)
  370. return;
  371. /*
  372. * Synchronize so the TX path can no longer be using
  373. * this key before we free/remove it.
  374. */
  375. synchronize_net();
  376. __ieee80211_key_destroy(key, delay_tailroom);
  377. }
  378. void ieee80211_key_free_unused(struct ieee80211_key *key)
  379. {
  380. WARN_ON(key->sdata || key->local);
  381. ieee80211_key_free_common(key);
  382. }
  383. int ieee80211_key_link(struct ieee80211_key *key,
  384. struct ieee80211_sub_if_data *sdata,
  385. struct sta_info *sta)
  386. {
  387. struct ieee80211_key *old_key;
  388. int idx, ret;
  389. bool pairwise;
  390. BUG_ON(!sdata);
  391. BUG_ON(!key);
  392. pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  393. idx = key->conf.keyidx;
  394. key->local = sdata->local;
  395. key->sdata = sdata;
  396. key->sta = sta;
  397. mutex_lock(&sdata->local->key_mtx);
  398. if (sta && pairwise)
  399. old_key = key_mtx_dereference(sdata->local, sta->ptk);
  400. else if (sta)
  401. old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
  402. else
  403. old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  404. increment_tailroom_need_count(sdata);
  405. ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  406. ieee80211_key_destroy(old_key, true);
  407. ieee80211_debugfs_key_add(key);
  408. ret = ieee80211_key_enable_hw_accel(key);
  409. if (ret)
  410. ieee80211_key_free(key, true);
  411. mutex_unlock(&sdata->local->key_mtx);
  412. return ret;
  413. }
  414. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  415. {
  416. if (!key)
  417. return;
  418. /*
  419. * Replace key with nothingness if it was ever used.
  420. */
  421. if (key->sdata)
  422. ieee80211_key_replace(key->sdata, key->sta,
  423. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  424. key, NULL);
  425. ieee80211_key_destroy(key, delay_tailroom);
  426. }
  427. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  428. {
  429. struct ieee80211_key *key;
  430. ASSERT_RTNL();
  431. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  432. return;
  433. mutex_lock(&sdata->local->key_mtx);
  434. sdata->crypto_tx_tailroom_needed_cnt = 0;
  435. list_for_each_entry(key, &sdata->key_list, list) {
  436. increment_tailroom_need_count(sdata);
  437. ieee80211_key_enable_hw_accel(key);
  438. }
  439. mutex_unlock(&sdata->local->key_mtx);
  440. }
  441. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  442. struct ieee80211_vif *vif,
  443. void (*iter)(struct ieee80211_hw *hw,
  444. struct ieee80211_vif *vif,
  445. struct ieee80211_sta *sta,
  446. struct ieee80211_key_conf *key,
  447. void *data),
  448. void *iter_data)
  449. {
  450. struct ieee80211_local *local = hw_to_local(hw);
  451. struct ieee80211_key *key;
  452. struct ieee80211_sub_if_data *sdata;
  453. ASSERT_RTNL();
  454. mutex_lock(&local->key_mtx);
  455. if (vif) {
  456. sdata = vif_to_sdata(vif);
  457. list_for_each_entry(key, &sdata->key_list, list)
  458. iter(hw, &sdata->vif,
  459. key->sta ? &key->sta->sta : NULL,
  460. &key->conf, iter_data);
  461. } else {
  462. list_for_each_entry(sdata, &local->interfaces, list)
  463. list_for_each_entry(key, &sdata->key_list, list)
  464. iter(hw, &sdata->vif,
  465. key->sta ? &key->sta->sta : NULL,
  466. &key->conf, iter_data);
  467. }
  468. mutex_unlock(&local->key_mtx);
  469. }
  470. EXPORT_SYMBOL(ieee80211_iter_keys);
  471. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  472. {
  473. struct ieee80211_key *key, *tmp;
  474. LIST_HEAD(keys);
  475. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  476. mutex_lock(&sdata->local->key_mtx);
  477. sdata->crypto_tx_tailroom_needed_cnt -=
  478. sdata->crypto_tx_tailroom_pending_dec;
  479. sdata->crypto_tx_tailroom_pending_dec = 0;
  480. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  481. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  482. ieee80211_key_replace(key->sdata, key->sta,
  483. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  484. key, NULL);
  485. list_add_tail(&key->list, &keys);
  486. }
  487. ieee80211_debugfs_key_update_default(sdata);
  488. if (!list_empty(&keys)) {
  489. synchronize_net();
  490. list_for_each_entry_safe(key, tmp, &keys, list)
  491. __ieee80211_key_destroy(key, false);
  492. }
  493. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  494. sdata->crypto_tx_tailroom_pending_dec);
  495. mutex_unlock(&sdata->local->key_mtx);
  496. }
  497. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  498. struct sta_info *sta)
  499. {
  500. struct ieee80211_key *key, *tmp;
  501. LIST_HEAD(keys);
  502. int i;
  503. mutex_lock(&local->key_mtx);
  504. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  505. key = key_mtx_dereference(local, sta->gtk[i]);
  506. if (!key)
  507. continue;
  508. ieee80211_key_replace(key->sdata, key->sta,
  509. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  510. key, NULL);
  511. list_add(&key->list, &keys);
  512. }
  513. key = key_mtx_dereference(local, sta->ptk);
  514. if (key) {
  515. ieee80211_key_replace(key->sdata, key->sta,
  516. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  517. key, NULL);
  518. list_add(&key->list, &keys);
  519. }
  520. /*
  521. * NB: the station code relies on this being
  522. * done even if there aren't any keys
  523. */
  524. synchronize_net();
  525. list_for_each_entry_safe(key, tmp, &keys, list)
  526. __ieee80211_key_destroy(key, true);
  527. mutex_unlock(&local->key_mtx);
  528. }
  529. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  530. {
  531. struct ieee80211_sub_if_data *sdata;
  532. sdata = container_of(wk, struct ieee80211_sub_if_data,
  533. dec_tailroom_needed_wk.work);
  534. /*
  535. * The reason for the delayed tailroom needed decrementing is to
  536. * make roaming faster: during roaming, all keys are first deleted
  537. * and then new keys are installed. The first new key causes the
  538. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  539. * the cost of synchronize_net() (which can be slow). Avoid this
  540. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  541. * key removal for a while, so if we roam the value is larger than
  542. * zero and no 0->1 transition happens.
  543. *
  544. * The cost is that if the AP switching was from an AP with keys
  545. * to one without, we still allocate tailroom while it would no
  546. * longer be needed. However, in the typical (fast) roaming case
  547. * within an ESS this usually won't happen.
  548. */
  549. mutex_lock(&sdata->local->key_mtx);
  550. sdata->crypto_tx_tailroom_needed_cnt -=
  551. sdata->crypto_tx_tailroom_pending_dec;
  552. sdata->crypto_tx_tailroom_pending_dec = 0;
  553. mutex_unlock(&sdata->local->key_mtx);
  554. }
  555. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  556. const u8 *replay_ctr, gfp_t gfp)
  557. {
  558. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  559. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  560. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  561. }
  562. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  563. void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
  564. struct ieee80211_key_seq *seq)
  565. {
  566. struct ieee80211_key *key;
  567. u64 pn64;
  568. if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
  569. return;
  570. key = container_of(keyconf, struct ieee80211_key, conf);
  571. switch (key->conf.cipher) {
  572. case WLAN_CIPHER_SUITE_TKIP:
  573. seq->tkip.iv32 = key->u.tkip.tx.iv32;
  574. seq->tkip.iv16 = key->u.tkip.tx.iv16;
  575. break;
  576. case WLAN_CIPHER_SUITE_CCMP:
  577. pn64 = atomic64_read(&key->u.ccmp.tx_pn);
  578. seq->ccmp.pn[5] = pn64;
  579. seq->ccmp.pn[4] = pn64 >> 8;
  580. seq->ccmp.pn[3] = pn64 >> 16;
  581. seq->ccmp.pn[2] = pn64 >> 24;
  582. seq->ccmp.pn[1] = pn64 >> 32;
  583. seq->ccmp.pn[0] = pn64 >> 40;
  584. break;
  585. case WLAN_CIPHER_SUITE_AES_CMAC:
  586. pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
  587. seq->ccmp.pn[5] = pn64;
  588. seq->ccmp.pn[4] = pn64 >> 8;
  589. seq->ccmp.pn[3] = pn64 >> 16;
  590. seq->ccmp.pn[2] = pn64 >> 24;
  591. seq->ccmp.pn[1] = pn64 >> 32;
  592. seq->ccmp.pn[0] = pn64 >> 40;
  593. break;
  594. default:
  595. WARN_ON(1);
  596. }
  597. }
  598. EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
  599. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  600. int tid, struct ieee80211_key_seq *seq)
  601. {
  602. struct ieee80211_key *key;
  603. const u8 *pn;
  604. key = container_of(keyconf, struct ieee80211_key, conf);
  605. switch (key->conf.cipher) {
  606. case WLAN_CIPHER_SUITE_TKIP:
  607. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  608. return;
  609. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  610. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  611. break;
  612. case WLAN_CIPHER_SUITE_CCMP:
  613. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  614. return;
  615. if (tid < 0)
  616. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  617. else
  618. pn = key->u.ccmp.rx_pn[tid];
  619. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  620. break;
  621. case WLAN_CIPHER_SUITE_AES_CMAC:
  622. if (WARN_ON(tid != 0))
  623. return;
  624. pn = key->u.aes_cmac.rx_pn;
  625. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  626. break;
  627. }
  628. }
  629. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);