key.c 30 KB

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