key.c 18 KB

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