key.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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->flags & KEY_FLAG_TAINTED)
  87. return -EINVAL;
  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. sdata_err(sdata,
  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. sdata_err(sdata,
  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. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  167. int idx, bool uni, bool multi)
  168. {
  169. struct ieee80211_key *key = NULL;
  170. assert_key_lock(sdata->local);
  171. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  172. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  173. if (uni) {
  174. rcu_assign_pointer(sdata->default_unicast_key, key);
  175. drv_set_default_unicast_key(sdata->local, sdata, idx);
  176. }
  177. if (multi)
  178. rcu_assign_pointer(sdata->default_multicast_key, key);
  179. ieee80211_debugfs_key_update_default(sdata);
  180. }
  181. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  182. bool uni, bool multi)
  183. {
  184. mutex_lock(&sdata->local->key_mtx);
  185. __ieee80211_set_default_key(sdata, idx, uni, multi);
  186. mutex_unlock(&sdata->local->key_mtx);
  187. }
  188. static void
  189. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  190. {
  191. struct ieee80211_key *key = NULL;
  192. assert_key_lock(sdata->local);
  193. if (idx >= NUM_DEFAULT_KEYS &&
  194. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  195. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  196. rcu_assign_pointer(sdata->default_mgmt_key, key);
  197. ieee80211_debugfs_key_update_default(sdata);
  198. }
  199. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  200. int idx)
  201. {
  202. mutex_lock(&sdata->local->key_mtx);
  203. __ieee80211_set_default_mgmt_key(sdata, idx);
  204. mutex_unlock(&sdata->local->key_mtx);
  205. }
  206. static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  207. struct sta_info *sta,
  208. bool pairwise,
  209. struct ieee80211_key *old,
  210. struct ieee80211_key *new)
  211. {
  212. int idx;
  213. bool defunikey, defmultikey, defmgmtkey;
  214. /* caller must provide at least one old/new */
  215. if (WARN_ON(!new && !old))
  216. return;
  217. if (new)
  218. list_add_tail(&new->list, &sdata->key_list);
  219. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  220. if (old)
  221. idx = old->conf.keyidx;
  222. else
  223. idx = new->conf.keyidx;
  224. if (sta) {
  225. if (pairwise) {
  226. rcu_assign_pointer(sta->ptk[idx], new);
  227. sta->ptk_idx = idx;
  228. } else {
  229. rcu_assign_pointer(sta->gtk[idx], new);
  230. sta->gtk_idx = idx;
  231. }
  232. } else {
  233. defunikey = old &&
  234. old == key_mtx_dereference(sdata->local,
  235. sdata->default_unicast_key);
  236. defmultikey = old &&
  237. old == key_mtx_dereference(sdata->local,
  238. sdata->default_multicast_key);
  239. defmgmtkey = old &&
  240. old == key_mtx_dereference(sdata->local,
  241. sdata->default_mgmt_key);
  242. if (defunikey && !new)
  243. __ieee80211_set_default_key(sdata, -1, true, false);
  244. if (defmultikey && !new)
  245. __ieee80211_set_default_key(sdata, -1, false, true);
  246. if (defmgmtkey && !new)
  247. __ieee80211_set_default_mgmt_key(sdata, -1);
  248. rcu_assign_pointer(sdata->keys[idx], new);
  249. if (defunikey && new)
  250. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  251. true, false);
  252. if (defmultikey && new)
  253. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  254. false, true);
  255. if (defmgmtkey && new)
  256. __ieee80211_set_default_mgmt_key(sdata,
  257. new->conf.keyidx);
  258. }
  259. if (old)
  260. list_del(&old->list);
  261. }
  262. struct ieee80211_key *
  263. ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  264. const u8 *key_data,
  265. size_t seq_len, const u8 *seq,
  266. const struct ieee80211_cipher_scheme *cs)
  267. {
  268. struct ieee80211_key *key;
  269. int i, j, err;
  270. if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
  271. return ERR_PTR(-EINVAL);
  272. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  273. if (!key)
  274. return ERR_PTR(-ENOMEM);
  275. /*
  276. * Default to software encryption; we'll later upload the
  277. * key to the hardware if possible.
  278. */
  279. key->conf.flags = 0;
  280. key->flags = 0;
  281. key->conf.cipher = cipher;
  282. key->conf.keyidx = idx;
  283. key->conf.keylen = key_len;
  284. switch (cipher) {
  285. case WLAN_CIPHER_SUITE_WEP40:
  286. case WLAN_CIPHER_SUITE_WEP104:
  287. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  288. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  289. break;
  290. case WLAN_CIPHER_SUITE_TKIP:
  291. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  292. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  293. if (seq) {
  294. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  295. key->u.tkip.rx[i].iv32 =
  296. get_unaligned_le32(&seq[2]);
  297. key->u.tkip.rx[i].iv16 =
  298. get_unaligned_le16(seq);
  299. }
  300. }
  301. spin_lock_init(&key->u.tkip.txlock);
  302. break;
  303. case WLAN_CIPHER_SUITE_CCMP:
  304. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  305. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  306. if (seq) {
  307. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  308. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  309. key->u.ccmp.rx_pn[i][j] =
  310. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  311. }
  312. /*
  313. * Initialize AES key state here as an optimization so that
  314. * it does not need to be initialized for every packet.
  315. */
  316. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  317. if (IS_ERR(key->u.ccmp.tfm)) {
  318. err = PTR_ERR(key->u.ccmp.tfm);
  319. kfree(key);
  320. return ERR_PTR(err);
  321. }
  322. break;
  323. case WLAN_CIPHER_SUITE_AES_CMAC:
  324. key->conf.iv_len = 0;
  325. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  326. if (seq)
  327. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  328. key->u.aes_cmac.rx_pn[j] =
  329. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  330. /*
  331. * Initialize AES key state here as an optimization so that
  332. * it does not need to be initialized for every packet.
  333. */
  334. key->u.aes_cmac.tfm =
  335. ieee80211_aes_cmac_key_setup(key_data);
  336. if (IS_ERR(key->u.aes_cmac.tfm)) {
  337. err = PTR_ERR(key->u.aes_cmac.tfm);
  338. kfree(key);
  339. return ERR_PTR(err);
  340. }
  341. break;
  342. default:
  343. if (cs) {
  344. size_t len = (seq_len > MAX_PN_LEN) ?
  345. MAX_PN_LEN : seq_len;
  346. key->conf.iv_len = cs->hdr_len;
  347. key->conf.icv_len = cs->mic_len;
  348. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  349. for (j = 0; j < len; j++)
  350. key->u.gen.rx_pn[i][j] =
  351. seq[len - j - 1];
  352. }
  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_free_common(struct ieee80211_key *key)
  359. {
  360. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  361. ieee80211_aes_key_free(key->u.ccmp.tfm);
  362. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  363. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  364. kfree(key);
  365. }
  366. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  367. bool delay_tailroom)
  368. {
  369. if (key->local)
  370. ieee80211_key_disable_hw_accel(key);
  371. if (key->local) {
  372. struct ieee80211_sub_if_data *sdata = key->sdata;
  373. ieee80211_debugfs_key_remove(key);
  374. if (delay_tailroom) {
  375. /* see ieee80211_delayed_tailroom_dec */
  376. sdata->crypto_tx_tailroom_pending_dec++;
  377. schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
  378. HZ/2);
  379. } else {
  380. sdata->crypto_tx_tailroom_needed_cnt--;
  381. }
  382. }
  383. ieee80211_key_free_common(key);
  384. }
  385. static void ieee80211_key_destroy(struct ieee80211_key *key,
  386. bool delay_tailroom)
  387. {
  388. if (!key)
  389. return;
  390. /*
  391. * Synchronize so the TX path can no longer be using
  392. * this key before we free/remove it.
  393. */
  394. synchronize_net();
  395. __ieee80211_key_destroy(key, delay_tailroom);
  396. }
  397. void ieee80211_key_free_unused(struct ieee80211_key *key)
  398. {
  399. WARN_ON(key->sdata || key->local);
  400. ieee80211_key_free_common(key);
  401. }
  402. int ieee80211_key_link(struct ieee80211_key *key,
  403. struct ieee80211_sub_if_data *sdata,
  404. struct sta_info *sta)
  405. {
  406. struct ieee80211_local *local = sdata->local;
  407. struct ieee80211_key *old_key;
  408. int idx, ret;
  409. bool pairwise;
  410. if (WARN_ON(!sdata || !key))
  411. return -EINVAL;
  412. pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  413. idx = key->conf.keyidx;
  414. key->local = sdata->local;
  415. key->sdata = sdata;
  416. key->sta = sta;
  417. mutex_lock(&sdata->local->key_mtx);
  418. if (sta && pairwise)
  419. old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
  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. increment_tailroom_need_count(sdata);
  425. ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  426. ieee80211_key_destroy(old_key, true);
  427. ieee80211_debugfs_key_add(key);
  428. if (!local->wowlan) {
  429. ret = ieee80211_key_enable_hw_accel(key);
  430. if (ret)
  431. ieee80211_key_free(key, true);
  432. } else {
  433. ret = 0;
  434. }
  435. mutex_unlock(&sdata->local->key_mtx);
  436. return ret;
  437. }
  438. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  439. {
  440. if (!key)
  441. return;
  442. /*
  443. * Replace key with nothingness if it was ever used.
  444. */
  445. if (key->sdata)
  446. ieee80211_key_replace(key->sdata, key->sta,
  447. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  448. key, NULL);
  449. ieee80211_key_destroy(key, delay_tailroom);
  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, *tmp;
  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_safe(key, tmp, &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_safe(key, tmp,
  488. &sdata->key_list, list)
  489. iter(hw, &sdata->vif,
  490. key->sta ? &key->sta->sta : NULL,
  491. &key->conf, iter_data);
  492. }
  493. mutex_unlock(&local->key_mtx);
  494. }
  495. EXPORT_SYMBOL(ieee80211_iter_keys);
  496. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  497. struct list_head *keys)
  498. {
  499. struct ieee80211_key *key, *tmp;
  500. sdata->crypto_tx_tailroom_needed_cnt -=
  501. sdata->crypto_tx_tailroom_pending_dec;
  502. sdata->crypto_tx_tailroom_pending_dec = 0;
  503. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  504. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  505. ieee80211_key_replace(key->sdata, key->sta,
  506. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  507. key, NULL);
  508. list_add_tail(&key->list, keys);
  509. }
  510. ieee80211_debugfs_key_update_default(sdata);
  511. }
  512. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  513. bool force_synchronize)
  514. {
  515. struct ieee80211_local *local = sdata->local;
  516. struct ieee80211_sub_if_data *vlan;
  517. struct ieee80211_key *key, *tmp;
  518. LIST_HEAD(keys);
  519. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  520. mutex_lock(&local->key_mtx);
  521. ieee80211_free_keys_iface(sdata, &keys);
  522. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  523. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  524. ieee80211_free_keys_iface(vlan, &keys);
  525. }
  526. if (!list_empty(&keys) || force_synchronize)
  527. synchronize_net();
  528. list_for_each_entry_safe(key, tmp, &keys, list)
  529. __ieee80211_key_destroy(key, false);
  530. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  531. sdata->crypto_tx_tailroom_pending_dec);
  532. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  533. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  534. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  535. vlan->crypto_tx_tailroom_pending_dec);
  536. }
  537. mutex_unlock(&local->key_mtx);
  538. }
  539. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  540. struct sta_info *sta)
  541. {
  542. struct ieee80211_key *key;
  543. int i;
  544. mutex_lock(&local->key_mtx);
  545. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  546. key = key_mtx_dereference(local, sta->gtk[i]);
  547. if (!key)
  548. continue;
  549. ieee80211_key_replace(key->sdata, key->sta,
  550. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  551. key, NULL);
  552. __ieee80211_key_destroy(key, true);
  553. }
  554. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  555. key = key_mtx_dereference(local, sta->ptk[i]);
  556. if (!key)
  557. continue;
  558. ieee80211_key_replace(key->sdata, key->sta,
  559. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  560. key, NULL);
  561. __ieee80211_key_destroy(key, true);
  562. }
  563. mutex_unlock(&local->key_mtx);
  564. }
  565. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  566. {
  567. struct ieee80211_sub_if_data *sdata;
  568. sdata = container_of(wk, struct ieee80211_sub_if_data,
  569. dec_tailroom_needed_wk.work);
  570. /*
  571. * The reason for the delayed tailroom needed decrementing is to
  572. * make roaming faster: during roaming, all keys are first deleted
  573. * and then new keys are installed. The first new key causes the
  574. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  575. * the cost of synchronize_net() (which can be slow). Avoid this
  576. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  577. * key removal for a while, so if we roam the value is larger than
  578. * zero and no 0->1 transition happens.
  579. *
  580. * The cost is that if the AP switching was from an AP with keys
  581. * to one without, we still allocate tailroom while it would no
  582. * longer be needed. However, in the typical (fast) roaming case
  583. * within an ESS this usually won't happen.
  584. */
  585. mutex_lock(&sdata->local->key_mtx);
  586. sdata->crypto_tx_tailroom_needed_cnt -=
  587. sdata->crypto_tx_tailroom_pending_dec;
  588. sdata->crypto_tx_tailroom_pending_dec = 0;
  589. mutex_unlock(&sdata->local->key_mtx);
  590. }
  591. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  592. const u8 *replay_ctr, gfp_t gfp)
  593. {
  594. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  595. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  596. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  597. }
  598. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  599. void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
  600. struct ieee80211_key_seq *seq)
  601. {
  602. struct ieee80211_key *key;
  603. u64 pn64;
  604. if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
  605. return;
  606. key = container_of(keyconf, struct ieee80211_key, conf);
  607. switch (key->conf.cipher) {
  608. case WLAN_CIPHER_SUITE_TKIP:
  609. seq->tkip.iv32 = key->u.tkip.tx.iv32;
  610. seq->tkip.iv16 = key->u.tkip.tx.iv16;
  611. break;
  612. case WLAN_CIPHER_SUITE_CCMP:
  613. pn64 = atomic64_read(&key->u.ccmp.tx_pn);
  614. seq->ccmp.pn[5] = pn64;
  615. seq->ccmp.pn[4] = pn64 >> 8;
  616. seq->ccmp.pn[3] = pn64 >> 16;
  617. seq->ccmp.pn[2] = pn64 >> 24;
  618. seq->ccmp.pn[1] = pn64 >> 32;
  619. seq->ccmp.pn[0] = pn64 >> 40;
  620. break;
  621. case WLAN_CIPHER_SUITE_AES_CMAC:
  622. pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
  623. seq->ccmp.pn[5] = pn64;
  624. seq->ccmp.pn[4] = pn64 >> 8;
  625. seq->ccmp.pn[3] = pn64 >> 16;
  626. seq->ccmp.pn[2] = pn64 >> 24;
  627. seq->ccmp.pn[1] = pn64 >> 32;
  628. seq->ccmp.pn[0] = pn64 >> 40;
  629. break;
  630. default:
  631. WARN_ON(1);
  632. }
  633. }
  634. EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
  635. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  636. int tid, struct ieee80211_key_seq *seq)
  637. {
  638. struct ieee80211_key *key;
  639. const u8 *pn;
  640. key = container_of(keyconf, struct ieee80211_key, conf);
  641. switch (key->conf.cipher) {
  642. case WLAN_CIPHER_SUITE_TKIP:
  643. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  644. return;
  645. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  646. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  647. break;
  648. case WLAN_CIPHER_SUITE_CCMP:
  649. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  650. return;
  651. if (tid < 0)
  652. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  653. else
  654. pn = key->u.ccmp.rx_pn[tid];
  655. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  656. break;
  657. case WLAN_CIPHER_SUITE_AES_CMAC:
  658. if (WARN_ON(tid != 0))
  659. return;
  660. pn = key->u.aes_cmac.rx_pn;
  661. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  662. break;
  663. }
  664. }
  665. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  666. void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
  667. struct ieee80211_key_seq *seq)
  668. {
  669. struct ieee80211_key *key;
  670. u64 pn64;
  671. key = container_of(keyconf, struct ieee80211_key, conf);
  672. switch (key->conf.cipher) {
  673. case WLAN_CIPHER_SUITE_TKIP:
  674. key->u.tkip.tx.iv32 = seq->tkip.iv32;
  675. key->u.tkip.tx.iv16 = seq->tkip.iv16;
  676. break;
  677. case WLAN_CIPHER_SUITE_CCMP:
  678. pn64 = (u64)seq->ccmp.pn[5] |
  679. ((u64)seq->ccmp.pn[4] << 8) |
  680. ((u64)seq->ccmp.pn[3] << 16) |
  681. ((u64)seq->ccmp.pn[2] << 24) |
  682. ((u64)seq->ccmp.pn[1] << 32) |
  683. ((u64)seq->ccmp.pn[0] << 40);
  684. atomic64_set(&key->u.ccmp.tx_pn, pn64);
  685. break;
  686. case WLAN_CIPHER_SUITE_AES_CMAC:
  687. pn64 = (u64)seq->aes_cmac.pn[5] |
  688. ((u64)seq->aes_cmac.pn[4] << 8) |
  689. ((u64)seq->aes_cmac.pn[3] << 16) |
  690. ((u64)seq->aes_cmac.pn[2] << 24) |
  691. ((u64)seq->aes_cmac.pn[1] << 32) |
  692. ((u64)seq->aes_cmac.pn[0] << 40);
  693. atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
  694. break;
  695. default:
  696. WARN_ON(1);
  697. break;
  698. }
  699. }
  700. EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
  701. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  702. int tid, struct ieee80211_key_seq *seq)
  703. {
  704. struct ieee80211_key *key;
  705. u8 *pn;
  706. key = container_of(keyconf, struct ieee80211_key, conf);
  707. switch (key->conf.cipher) {
  708. case WLAN_CIPHER_SUITE_TKIP:
  709. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  710. return;
  711. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  712. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  713. break;
  714. case WLAN_CIPHER_SUITE_CCMP:
  715. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  716. return;
  717. if (tid < 0)
  718. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  719. else
  720. pn = key->u.ccmp.rx_pn[tid];
  721. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  722. break;
  723. case WLAN_CIPHER_SUITE_AES_CMAC:
  724. if (WARN_ON(tid != 0))
  725. return;
  726. pn = key->u.aes_cmac.rx_pn;
  727. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  728. break;
  729. default:
  730. WARN_ON(1);
  731. break;
  732. }
  733. }
  734. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  735. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  736. {
  737. struct ieee80211_key *key;
  738. key = container_of(keyconf, struct ieee80211_key, conf);
  739. assert_key_lock(key->local);
  740. /*
  741. * if key was uploaded, we assume the driver will/has remove(d)
  742. * it, so adjust bookkeeping accordingly
  743. */
  744. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  745. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  746. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  747. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
  748. (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
  749. increment_tailroom_need_count(key->sdata);
  750. }
  751. ieee80211_key_free(key, false);
  752. }
  753. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  754. struct ieee80211_key_conf *
  755. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  756. struct ieee80211_key_conf *keyconf)
  757. {
  758. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  759. struct ieee80211_local *local = sdata->local;
  760. struct ieee80211_key *key;
  761. int err;
  762. if (WARN_ON(!local->wowlan))
  763. return ERR_PTR(-EINVAL);
  764. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  765. return ERR_PTR(-EINVAL);
  766. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  767. keyconf->keylen, keyconf->key,
  768. 0, NULL, NULL);
  769. if (IS_ERR(key))
  770. return ERR_CAST(key);
  771. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  772. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  773. err = ieee80211_key_link(key, sdata, NULL);
  774. if (err)
  775. return ERR_PTR(err);
  776. return &key->conf;
  777. }
  778. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);