key.c 24 KB

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