key.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 2013-2014 Intel Mobile Communications 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 <asm/unaligned.h>
  21. #include "ieee80211_i.h"
  22. #include "driver-ops.h"
  23. #include "debugfs_key.h"
  24. #include "aes_ccm.h"
  25. #include "aes_cmac.h"
  26. /**
  27. * DOC: Key handling basics
  28. *
  29. * Key handling in mac80211 is done based on per-interface (sub_if_data)
  30. * keys and per-station keys. Since each station belongs to an interface,
  31. * each station key also belongs to that interface.
  32. *
  33. * Hardware acceleration is done on a best-effort basis for algorithms
  34. * that are implemented in software, for each key the hardware is asked
  35. * to enable that key for offloading but if it cannot do that the key is
  36. * simply kept for software encryption (unless it is for an algorithm
  37. * that isn't implemented in software).
  38. * There is currently no way of knowing whether a key is handled in SW
  39. * or HW except by looking into debugfs.
  40. *
  41. * All key management is internally protected by a mutex. Within all
  42. * other parts of mac80211, key references are, just as STA structure
  43. * references, protected by RCU. Note, however, that some things are
  44. * unprotected, namely the key->sta dereferences within the hardware
  45. * acceleration functions. This means that sta_info_destroy() must
  46. * remove the key which waits for an RCU grace period.
  47. */
  48. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  49. static void assert_key_lock(struct ieee80211_local *local)
  50. {
  51. lockdep_assert_held(&local->key_mtx);
  52. }
  53. static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
  54. {
  55. /*
  56. * When this count is zero, SKB resizing for allocating tailroom
  57. * for IV or MMIC is skipped. But, this check has created two race
  58. * cases in xmit path while transiting from zero count to one:
  59. *
  60. * 1. SKB resize was skipped because no key was added but just before
  61. * the xmit key is added and SW encryption kicks off.
  62. *
  63. * 2. SKB resize was skipped because all the keys were hw planted but
  64. * just before xmit one of the key is deleted and SW encryption kicks
  65. * off.
  66. *
  67. * In both the above case SW encryption will find not enough space for
  68. * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
  69. *
  70. * Solution has been explained at
  71. * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
  72. */
  73. if (!sdata->crypto_tx_tailroom_needed_cnt++) {
  74. /*
  75. * Flush all XMIT packets currently using HW encryption or no
  76. * encryption at all if the count transition is from 0 -> 1.
  77. */
  78. synchronize_net();
  79. }
  80. }
  81. static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  82. {
  83. struct ieee80211_sub_if_data *sdata;
  84. struct sta_info *sta;
  85. int ret;
  86. might_sleep();
  87. if (key->flags & KEY_FLAG_TAINTED) {
  88. /* If we get here, it's during resume and the key is
  89. * tainted so shouldn't be used/programmed any more.
  90. * However, its flags may still indicate that it was
  91. * programmed into the device (since we're in resume)
  92. * so clear that flag now to avoid trying to remove
  93. * it again later.
  94. */
  95. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  96. return -EINVAL;
  97. }
  98. if (!key->local->ops->set_key)
  99. goto out_unsupported;
  100. assert_key_lock(key->local);
  101. sta = key->sta;
  102. /*
  103. * If this is a per-STA GTK, check if it
  104. * is supported; if not, return.
  105. */
  106. if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
  107. !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
  108. goto out_unsupported;
  109. if (sta && !sta->uploaded)
  110. goto out_unsupported;
  111. sdata = key->sdata;
  112. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  113. /*
  114. * The driver doesn't know anything about VLAN interfaces.
  115. * Hence, don't send GTKs for VLAN interfaces to the driver.
  116. */
  117. if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
  118. goto out_unsupported;
  119. }
  120. ret = drv_set_key(key->local, SET_KEY, sdata,
  121. sta ? &sta->sta : NULL, &key->conf);
  122. if (!ret) {
  123. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  124. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  125. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  126. sdata->crypto_tx_tailroom_needed_cnt--;
  127. WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  128. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
  129. return 0;
  130. }
  131. if (ret != -ENOSPC && ret != -EOPNOTSUPP)
  132. sdata_err(sdata,
  133. "failed to set key (%d, %pM) to hardware (%d)\n",
  134. key->conf.keyidx,
  135. sta ? sta->sta.addr : bcast_addr, ret);
  136. out_unsupported:
  137. switch (key->conf.cipher) {
  138. case WLAN_CIPHER_SUITE_WEP40:
  139. case WLAN_CIPHER_SUITE_WEP104:
  140. case WLAN_CIPHER_SUITE_TKIP:
  141. case WLAN_CIPHER_SUITE_CCMP:
  142. case WLAN_CIPHER_SUITE_AES_CMAC:
  143. /* all of these we can do in software */
  144. return 0;
  145. default:
  146. return -EINVAL;
  147. }
  148. }
  149. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  150. {
  151. struct ieee80211_sub_if_data *sdata;
  152. struct sta_info *sta;
  153. int ret;
  154. might_sleep();
  155. if (!key || !key->local->ops->set_key)
  156. return;
  157. assert_key_lock(key->local);
  158. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  159. return;
  160. sta = key->sta;
  161. sdata = key->sdata;
  162. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  163. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  164. increment_tailroom_need_count(sdata);
  165. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  166. sta ? &sta->sta : NULL, &key->conf);
  167. if (ret)
  168. sdata_err(sdata,
  169. "failed to remove key (%d, %pM) from hardware (%d)\n",
  170. key->conf.keyidx,
  171. sta ? sta->sta.addr : bcast_addr, ret);
  172. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  173. }
  174. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  175. int idx, bool uni, bool multi)
  176. {
  177. struct ieee80211_key *key = NULL;
  178. assert_key_lock(sdata->local);
  179. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  180. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  181. if (uni) {
  182. rcu_assign_pointer(sdata->default_unicast_key, key);
  183. drv_set_default_unicast_key(sdata->local, sdata, idx);
  184. }
  185. if (multi)
  186. rcu_assign_pointer(sdata->default_multicast_key, key);
  187. ieee80211_debugfs_key_update_default(sdata);
  188. }
  189. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  190. bool uni, bool multi)
  191. {
  192. mutex_lock(&sdata->local->key_mtx);
  193. __ieee80211_set_default_key(sdata, idx, uni, multi);
  194. mutex_unlock(&sdata->local->key_mtx);
  195. }
  196. static void
  197. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  198. {
  199. struct ieee80211_key *key = NULL;
  200. assert_key_lock(sdata->local);
  201. if (idx >= NUM_DEFAULT_KEYS &&
  202. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  203. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  204. rcu_assign_pointer(sdata->default_mgmt_key, key);
  205. ieee80211_debugfs_key_update_default(sdata);
  206. }
  207. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  208. int idx)
  209. {
  210. mutex_lock(&sdata->local->key_mtx);
  211. __ieee80211_set_default_mgmt_key(sdata, idx);
  212. mutex_unlock(&sdata->local->key_mtx);
  213. }
  214. static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  215. struct sta_info *sta,
  216. bool pairwise,
  217. struct ieee80211_key *old,
  218. struct ieee80211_key *new)
  219. {
  220. int idx;
  221. bool defunikey, defmultikey, defmgmtkey;
  222. /* caller must provide at least one old/new */
  223. if (WARN_ON(!new && !old))
  224. return;
  225. if (new)
  226. list_add_tail(&new->list, &sdata->key_list);
  227. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  228. if (old)
  229. idx = old->conf.keyidx;
  230. else
  231. idx = new->conf.keyidx;
  232. if (sta) {
  233. if (pairwise) {
  234. rcu_assign_pointer(sta->ptk[idx], new);
  235. sta->ptk_idx = idx;
  236. } else {
  237. rcu_assign_pointer(sta->gtk[idx], new);
  238. sta->gtk_idx = idx;
  239. }
  240. } else {
  241. defunikey = old &&
  242. old == key_mtx_dereference(sdata->local,
  243. sdata->default_unicast_key);
  244. defmultikey = old &&
  245. old == key_mtx_dereference(sdata->local,
  246. sdata->default_multicast_key);
  247. defmgmtkey = old &&
  248. old == key_mtx_dereference(sdata->local,
  249. sdata->default_mgmt_key);
  250. if (defunikey && !new)
  251. __ieee80211_set_default_key(sdata, -1, true, false);
  252. if (defmultikey && !new)
  253. __ieee80211_set_default_key(sdata, -1, false, true);
  254. if (defmgmtkey && !new)
  255. __ieee80211_set_default_mgmt_key(sdata, -1);
  256. rcu_assign_pointer(sdata->keys[idx], new);
  257. if (defunikey && new)
  258. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  259. true, false);
  260. if (defmultikey && new)
  261. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  262. false, true);
  263. if (defmgmtkey && new)
  264. __ieee80211_set_default_mgmt_key(sdata,
  265. new->conf.keyidx);
  266. }
  267. if (old)
  268. list_del(&old->list);
  269. }
  270. struct ieee80211_key *
  271. ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  272. const u8 *key_data,
  273. size_t seq_len, const u8 *seq,
  274. const struct ieee80211_cipher_scheme *cs)
  275. {
  276. struct ieee80211_key *key;
  277. int i, j, err;
  278. if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
  279. return ERR_PTR(-EINVAL);
  280. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  281. if (!key)
  282. return ERR_PTR(-ENOMEM);
  283. /*
  284. * Default to software encryption; we'll later upload the
  285. * key to the hardware if possible.
  286. */
  287. key->conf.flags = 0;
  288. key->flags = 0;
  289. key->conf.cipher = cipher;
  290. key->conf.keyidx = idx;
  291. key->conf.keylen = key_len;
  292. switch (cipher) {
  293. case WLAN_CIPHER_SUITE_WEP40:
  294. case WLAN_CIPHER_SUITE_WEP104:
  295. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  296. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  297. break;
  298. case WLAN_CIPHER_SUITE_TKIP:
  299. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  300. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  301. if (seq) {
  302. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  303. key->u.tkip.rx[i].iv32 =
  304. get_unaligned_le32(&seq[2]);
  305. key->u.tkip.rx[i].iv16 =
  306. get_unaligned_le16(seq);
  307. }
  308. }
  309. spin_lock_init(&key->u.tkip.txlock);
  310. break;
  311. case WLAN_CIPHER_SUITE_CCMP:
  312. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  313. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  314. if (seq) {
  315. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  316. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  317. key->u.ccmp.rx_pn[i][j] =
  318. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  319. }
  320. /*
  321. * Initialize AES key state here as an optimization so that
  322. * it does not need to be initialized for every packet.
  323. */
  324. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  325. if (IS_ERR(key->u.ccmp.tfm)) {
  326. err = PTR_ERR(key->u.ccmp.tfm);
  327. kfree(key);
  328. return ERR_PTR(err);
  329. }
  330. break;
  331. case WLAN_CIPHER_SUITE_AES_CMAC:
  332. key->conf.iv_len = 0;
  333. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  334. if (seq)
  335. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  336. key->u.aes_cmac.rx_pn[j] =
  337. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  338. /*
  339. * Initialize AES key state here as an optimization so that
  340. * it does not need to be initialized for every packet.
  341. */
  342. key->u.aes_cmac.tfm =
  343. ieee80211_aes_cmac_key_setup(key_data);
  344. if (IS_ERR(key->u.aes_cmac.tfm)) {
  345. err = PTR_ERR(key->u.aes_cmac.tfm);
  346. kfree(key);
  347. return ERR_PTR(err);
  348. }
  349. break;
  350. default:
  351. if (cs) {
  352. size_t len = (seq_len > MAX_PN_LEN) ?
  353. MAX_PN_LEN : seq_len;
  354. key->conf.iv_len = cs->hdr_len;
  355. key->conf.icv_len = cs->mic_len;
  356. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  357. for (j = 0; j < len; j++)
  358. key->u.gen.rx_pn[i][j] =
  359. seq[len - j - 1];
  360. }
  361. }
  362. memcpy(key->conf.key, key_data, key_len);
  363. INIT_LIST_HEAD(&key->list);
  364. return key;
  365. }
  366. static void ieee80211_key_free_common(struct ieee80211_key *key)
  367. {
  368. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  369. ieee80211_aes_key_free(key->u.ccmp.tfm);
  370. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  371. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  372. kzfree(key);
  373. }
  374. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  375. bool delay_tailroom)
  376. {
  377. if (key->local)
  378. ieee80211_key_disable_hw_accel(key);
  379. if (key->local) {
  380. struct ieee80211_sub_if_data *sdata = key->sdata;
  381. ieee80211_debugfs_key_remove(key);
  382. if (delay_tailroom) {
  383. /* see ieee80211_delayed_tailroom_dec */
  384. sdata->crypto_tx_tailroom_pending_dec++;
  385. schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
  386. HZ/2);
  387. } else {
  388. sdata->crypto_tx_tailroom_needed_cnt--;
  389. }
  390. }
  391. ieee80211_key_free_common(key);
  392. }
  393. static void ieee80211_key_destroy(struct ieee80211_key *key,
  394. bool delay_tailroom)
  395. {
  396. if (!key)
  397. return;
  398. /*
  399. * Synchronize so the TX path can no longer be using
  400. * this key before we free/remove it.
  401. */
  402. synchronize_net();
  403. __ieee80211_key_destroy(key, delay_tailroom);
  404. }
  405. void ieee80211_key_free_unused(struct ieee80211_key *key)
  406. {
  407. WARN_ON(key->sdata || key->local);
  408. ieee80211_key_free_common(key);
  409. }
  410. int ieee80211_key_link(struct ieee80211_key *key,
  411. struct ieee80211_sub_if_data *sdata,
  412. struct sta_info *sta)
  413. {
  414. struct ieee80211_local *local = sdata->local;
  415. struct ieee80211_key *old_key;
  416. int idx, ret;
  417. bool pairwise;
  418. pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  419. idx = key->conf.keyidx;
  420. key->local = sdata->local;
  421. key->sdata = sdata;
  422. key->sta = sta;
  423. mutex_lock(&sdata->local->key_mtx);
  424. if (sta && pairwise)
  425. old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
  426. else if (sta)
  427. old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
  428. else
  429. old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  430. increment_tailroom_need_count(sdata);
  431. ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  432. ieee80211_key_destroy(old_key, true);
  433. ieee80211_debugfs_key_add(key);
  434. if (!local->wowlan) {
  435. ret = ieee80211_key_enable_hw_accel(key);
  436. if (ret)
  437. ieee80211_key_free(key, true);
  438. } else {
  439. ret = 0;
  440. }
  441. mutex_unlock(&sdata->local->key_mtx);
  442. return ret;
  443. }
  444. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  445. {
  446. if (!key)
  447. return;
  448. /*
  449. * Replace key with nothingness if it was ever used.
  450. */
  451. if (key->sdata)
  452. ieee80211_key_replace(key->sdata, key->sta,
  453. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  454. key, NULL);
  455. ieee80211_key_destroy(key, delay_tailroom);
  456. }
  457. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  458. {
  459. struct ieee80211_key *key;
  460. ASSERT_RTNL();
  461. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  462. return;
  463. mutex_lock(&sdata->local->key_mtx);
  464. sdata->crypto_tx_tailroom_needed_cnt = 0;
  465. list_for_each_entry(key, &sdata->key_list, list) {
  466. increment_tailroom_need_count(sdata);
  467. ieee80211_key_enable_hw_accel(key);
  468. }
  469. mutex_unlock(&sdata->local->key_mtx);
  470. }
  471. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  472. struct ieee80211_vif *vif,
  473. void (*iter)(struct ieee80211_hw *hw,
  474. struct ieee80211_vif *vif,
  475. struct ieee80211_sta *sta,
  476. struct ieee80211_key_conf *key,
  477. void *data),
  478. void *iter_data)
  479. {
  480. struct ieee80211_local *local = hw_to_local(hw);
  481. struct ieee80211_key *key, *tmp;
  482. struct ieee80211_sub_if_data *sdata;
  483. ASSERT_RTNL();
  484. mutex_lock(&local->key_mtx);
  485. if (vif) {
  486. sdata = vif_to_sdata(vif);
  487. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  488. iter(hw, &sdata->vif,
  489. key->sta ? &key->sta->sta : NULL,
  490. &key->conf, iter_data);
  491. } else {
  492. list_for_each_entry(sdata, &local->interfaces, list)
  493. list_for_each_entry_safe(key, tmp,
  494. &sdata->key_list, list)
  495. iter(hw, &sdata->vif,
  496. key->sta ? &key->sta->sta : NULL,
  497. &key->conf, iter_data);
  498. }
  499. mutex_unlock(&local->key_mtx);
  500. }
  501. EXPORT_SYMBOL(ieee80211_iter_keys);
  502. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  503. struct list_head *keys)
  504. {
  505. struct ieee80211_key *key, *tmp;
  506. sdata->crypto_tx_tailroom_needed_cnt -=
  507. sdata->crypto_tx_tailroom_pending_dec;
  508. sdata->crypto_tx_tailroom_pending_dec = 0;
  509. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  510. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  511. ieee80211_key_replace(key->sdata, key->sta,
  512. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  513. key, NULL);
  514. list_add_tail(&key->list, keys);
  515. }
  516. ieee80211_debugfs_key_update_default(sdata);
  517. }
  518. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  519. bool force_synchronize)
  520. {
  521. struct ieee80211_local *local = sdata->local;
  522. struct ieee80211_sub_if_data *vlan;
  523. struct ieee80211_key *key, *tmp;
  524. LIST_HEAD(keys);
  525. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  526. mutex_lock(&local->key_mtx);
  527. ieee80211_free_keys_iface(sdata, &keys);
  528. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  529. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  530. ieee80211_free_keys_iface(vlan, &keys);
  531. }
  532. if (!list_empty(&keys) || force_synchronize)
  533. synchronize_net();
  534. list_for_each_entry_safe(key, tmp, &keys, list)
  535. __ieee80211_key_destroy(key, false);
  536. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  537. sdata->crypto_tx_tailroom_pending_dec);
  538. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  539. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  540. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  541. vlan->crypto_tx_tailroom_pending_dec);
  542. }
  543. mutex_unlock(&local->key_mtx);
  544. }
  545. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  546. struct sta_info *sta)
  547. {
  548. struct ieee80211_key *key;
  549. int i;
  550. mutex_lock(&local->key_mtx);
  551. for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
  552. key = key_mtx_dereference(local, sta->gtk[i]);
  553. if (!key)
  554. continue;
  555. ieee80211_key_replace(key->sdata, key->sta,
  556. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  557. key, NULL);
  558. __ieee80211_key_destroy(key, true);
  559. }
  560. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  561. key = key_mtx_dereference(local, sta->ptk[i]);
  562. if (!key)
  563. continue;
  564. ieee80211_key_replace(key->sdata, key->sta,
  565. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  566. key, NULL);
  567. __ieee80211_key_destroy(key, true);
  568. }
  569. mutex_unlock(&local->key_mtx);
  570. }
  571. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  572. {
  573. struct ieee80211_sub_if_data *sdata;
  574. sdata = container_of(wk, struct ieee80211_sub_if_data,
  575. dec_tailroom_needed_wk.work);
  576. /*
  577. * The reason for the delayed tailroom needed decrementing is to
  578. * make roaming faster: during roaming, all keys are first deleted
  579. * and then new keys are installed. The first new key causes the
  580. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  581. * the cost of synchronize_net() (which can be slow). Avoid this
  582. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  583. * key removal for a while, so if we roam the value is larger than
  584. * zero and no 0->1 transition happens.
  585. *
  586. * The cost is that if the AP switching was from an AP with keys
  587. * to one without, we still allocate tailroom while it would no
  588. * longer be needed. However, in the typical (fast) roaming case
  589. * within an ESS this usually won't happen.
  590. */
  591. mutex_lock(&sdata->local->key_mtx);
  592. sdata->crypto_tx_tailroom_needed_cnt -=
  593. sdata->crypto_tx_tailroom_pending_dec;
  594. sdata->crypto_tx_tailroom_pending_dec = 0;
  595. mutex_unlock(&sdata->local->key_mtx);
  596. }
  597. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  598. const u8 *replay_ctr, gfp_t gfp)
  599. {
  600. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  601. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  602. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  603. }
  604. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  605. void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
  606. struct ieee80211_key_seq *seq)
  607. {
  608. struct ieee80211_key *key;
  609. u64 pn64;
  610. if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
  611. return;
  612. key = container_of(keyconf, struct ieee80211_key, conf);
  613. switch (key->conf.cipher) {
  614. case WLAN_CIPHER_SUITE_TKIP:
  615. seq->tkip.iv32 = key->u.tkip.tx.iv32;
  616. seq->tkip.iv16 = key->u.tkip.tx.iv16;
  617. break;
  618. case WLAN_CIPHER_SUITE_CCMP:
  619. pn64 = atomic64_read(&key->u.ccmp.tx_pn);
  620. seq->ccmp.pn[5] = pn64;
  621. seq->ccmp.pn[4] = pn64 >> 8;
  622. seq->ccmp.pn[3] = pn64 >> 16;
  623. seq->ccmp.pn[2] = pn64 >> 24;
  624. seq->ccmp.pn[1] = pn64 >> 32;
  625. seq->ccmp.pn[0] = pn64 >> 40;
  626. break;
  627. case WLAN_CIPHER_SUITE_AES_CMAC:
  628. pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
  629. seq->ccmp.pn[5] = pn64;
  630. seq->ccmp.pn[4] = pn64 >> 8;
  631. seq->ccmp.pn[3] = pn64 >> 16;
  632. seq->ccmp.pn[2] = pn64 >> 24;
  633. seq->ccmp.pn[1] = pn64 >> 32;
  634. seq->ccmp.pn[0] = pn64 >> 40;
  635. break;
  636. default:
  637. WARN_ON(1);
  638. }
  639. }
  640. EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
  641. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  642. int tid, struct ieee80211_key_seq *seq)
  643. {
  644. struct ieee80211_key *key;
  645. const u8 *pn;
  646. key = container_of(keyconf, struct ieee80211_key, conf);
  647. switch (key->conf.cipher) {
  648. case WLAN_CIPHER_SUITE_TKIP:
  649. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  650. return;
  651. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  652. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  653. break;
  654. case WLAN_CIPHER_SUITE_CCMP:
  655. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  656. return;
  657. if (tid < 0)
  658. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  659. else
  660. pn = key->u.ccmp.rx_pn[tid];
  661. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  662. break;
  663. case WLAN_CIPHER_SUITE_AES_CMAC:
  664. if (WARN_ON(tid != 0))
  665. return;
  666. pn = key->u.aes_cmac.rx_pn;
  667. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  668. break;
  669. }
  670. }
  671. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  672. void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
  673. struct ieee80211_key_seq *seq)
  674. {
  675. struct ieee80211_key *key;
  676. u64 pn64;
  677. key = container_of(keyconf, struct ieee80211_key, conf);
  678. switch (key->conf.cipher) {
  679. case WLAN_CIPHER_SUITE_TKIP:
  680. key->u.tkip.tx.iv32 = seq->tkip.iv32;
  681. key->u.tkip.tx.iv16 = seq->tkip.iv16;
  682. break;
  683. case WLAN_CIPHER_SUITE_CCMP:
  684. pn64 = (u64)seq->ccmp.pn[5] |
  685. ((u64)seq->ccmp.pn[4] << 8) |
  686. ((u64)seq->ccmp.pn[3] << 16) |
  687. ((u64)seq->ccmp.pn[2] << 24) |
  688. ((u64)seq->ccmp.pn[1] << 32) |
  689. ((u64)seq->ccmp.pn[0] << 40);
  690. atomic64_set(&key->u.ccmp.tx_pn, pn64);
  691. break;
  692. case WLAN_CIPHER_SUITE_AES_CMAC:
  693. pn64 = (u64)seq->aes_cmac.pn[5] |
  694. ((u64)seq->aes_cmac.pn[4] << 8) |
  695. ((u64)seq->aes_cmac.pn[3] << 16) |
  696. ((u64)seq->aes_cmac.pn[2] << 24) |
  697. ((u64)seq->aes_cmac.pn[1] << 32) |
  698. ((u64)seq->aes_cmac.pn[0] << 40);
  699. atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
  700. break;
  701. default:
  702. WARN_ON(1);
  703. break;
  704. }
  705. }
  706. EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
  707. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  708. int tid, struct ieee80211_key_seq *seq)
  709. {
  710. struct ieee80211_key *key;
  711. u8 *pn;
  712. key = container_of(keyconf, struct ieee80211_key, conf);
  713. switch (key->conf.cipher) {
  714. case WLAN_CIPHER_SUITE_TKIP:
  715. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  716. return;
  717. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  718. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  719. break;
  720. case WLAN_CIPHER_SUITE_CCMP:
  721. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  722. return;
  723. if (tid < 0)
  724. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  725. else
  726. pn = key->u.ccmp.rx_pn[tid];
  727. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  728. break;
  729. case WLAN_CIPHER_SUITE_AES_CMAC:
  730. if (WARN_ON(tid != 0))
  731. return;
  732. pn = key->u.aes_cmac.rx_pn;
  733. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  734. break;
  735. default:
  736. WARN_ON(1);
  737. break;
  738. }
  739. }
  740. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  741. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  742. {
  743. struct ieee80211_key *key;
  744. key = container_of(keyconf, struct ieee80211_key, conf);
  745. assert_key_lock(key->local);
  746. /*
  747. * if key was uploaded, we assume the driver will/has remove(d)
  748. * it, so adjust bookkeeping accordingly
  749. */
  750. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  751. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  752. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  753. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  754. increment_tailroom_need_count(key->sdata);
  755. }
  756. ieee80211_key_free(key, false);
  757. }
  758. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  759. struct ieee80211_key_conf *
  760. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  761. struct ieee80211_key_conf *keyconf)
  762. {
  763. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  764. struct ieee80211_local *local = sdata->local;
  765. struct ieee80211_key *key;
  766. int err;
  767. if (WARN_ON(!local->wowlan))
  768. return ERR_PTR(-EINVAL);
  769. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  770. return ERR_PTR(-EINVAL);
  771. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  772. keyconf->keylen, keyconf->key,
  773. 0, NULL, NULL);
  774. if (IS_ERR(key))
  775. return ERR_CAST(key);
  776. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  777. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  778. err = ieee80211_key_link(key, sdata, NULL);
  779. if (err)
  780. return ERR_PTR(err);
  781. return &key->conf;
  782. }
  783. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);