key.c 19 KB

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