key.c 18 KB

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