key.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  221. drv_set_default_unicast_key(sdata->local, sdata, idx);
  222. }
  223. if (multi)
  224. rcu_assign_pointer(sdata->default_multicast_key, key);
  225. ieee80211_debugfs_key_update_default(sdata);
  226. }
  227. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  228. bool uni, bool multi)
  229. {
  230. mutex_lock(&sdata->local->key_mtx);
  231. __ieee80211_set_default_key(sdata, idx, uni, multi);
  232. mutex_unlock(&sdata->local->key_mtx);
  233. }
  234. static void
  235. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  236. {
  237. struct ieee80211_key *key = NULL;
  238. assert_key_lock(sdata->local);
  239. if (idx >= NUM_DEFAULT_KEYS &&
  240. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  241. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  242. rcu_assign_pointer(sdata->default_mgmt_key, key);
  243. ieee80211_debugfs_key_update_default(sdata);
  244. }
  245. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  246. int idx)
  247. {
  248. mutex_lock(&sdata->local->key_mtx);
  249. __ieee80211_set_default_mgmt_key(sdata, idx);
  250. mutex_unlock(&sdata->local->key_mtx);
  251. }
  252. static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  253. struct sta_info *sta,
  254. bool pairwise,
  255. struct ieee80211_key *old,
  256. struct ieee80211_key *new)
  257. {
  258. int idx;
  259. bool defunikey, defmultikey, defmgmtkey;
  260. /* caller must provide at least one old/new */
  261. if (WARN_ON(!new && !old))
  262. return;
  263. if (new)
  264. list_add_tail_rcu(&new->list, &sdata->key_list);
  265. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  266. if (old)
  267. idx = old->conf.keyidx;
  268. else
  269. idx = new->conf.keyidx;
  270. if (sta) {
  271. if (pairwise) {
  272. rcu_assign_pointer(sta->ptk[idx], new);
  273. sta->ptk_idx = idx;
  274. ieee80211_check_fast_xmit(sta);
  275. } else {
  276. rcu_assign_pointer(sta->gtk[idx], new);
  277. }
  278. ieee80211_check_fast_rx(sta);
  279. } else {
  280. defunikey = old &&
  281. old == key_mtx_dereference(sdata->local,
  282. sdata->default_unicast_key);
  283. defmultikey = old &&
  284. old == key_mtx_dereference(sdata->local,
  285. sdata->default_multicast_key);
  286. defmgmtkey = old &&
  287. old == key_mtx_dereference(sdata->local,
  288. sdata->default_mgmt_key);
  289. if (defunikey && !new)
  290. __ieee80211_set_default_key(sdata, -1, true, false);
  291. if (defmultikey && !new)
  292. __ieee80211_set_default_key(sdata, -1, false, true);
  293. if (defmgmtkey && !new)
  294. __ieee80211_set_default_mgmt_key(sdata, -1);
  295. rcu_assign_pointer(sdata->keys[idx], new);
  296. if (defunikey && new)
  297. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  298. true, false);
  299. if (defmultikey && new)
  300. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  301. false, true);
  302. if (defmgmtkey && new)
  303. __ieee80211_set_default_mgmt_key(sdata,
  304. new->conf.keyidx);
  305. }
  306. if (old)
  307. list_del_rcu(&old->list);
  308. }
  309. struct ieee80211_key *
  310. ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  311. const u8 *key_data,
  312. size_t seq_len, const u8 *seq,
  313. const struct ieee80211_cipher_scheme *cs)
  314. {
  315. struct ieee80211_key *key;
  316. int i, j, err;
  317. if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
  318. return ERR_PTR(-EINVAL);
  319. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  320. if (!key)
  321. return ERR_PTR(-ENOMEM);
  322. /*
  323. * Default to software encryption; we'll later upload the
  324. * key to the hardware if possible.
  325. */
  326. key->conf.flags = 0;
  327. key->flags = 0;
  328. key->conf.cipher = cipher;
  329. key->conf.keyidx = idx;
  330. key->conf.keylen = key_len;
  331. switch (cipher) {
  332. case WLAN_CIPHER_SUITE_WEP40:
  333. case WLAN_CIPHER_SUITE_WEP104:
  334. key->conf.iv_len = IEEE80211_WEP_IV_LEN;
  335. key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
  336. break;
  337. case WLAN_CIPHER_SUITE_TKIP:
  338. key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
  339. key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
  340. if (seq) {
  341. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  342. key->u.tkip.rx[i].iv32 =
  343. get_unaligned_le32(&seq[2]);
  344. key->u.tkip.rx[i].iv16 =
  345. get_unaligned_le16(seq);
  346. }
  347. }
  348. spin_lock_init(&key->u.tkip.txlock);
  349. break;
  350. case WLAN_CIPHER_SUITE_CCMP:
  351. key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
  352. key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
  353. if (seq) {
  354. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  355. for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
  356. key->u.ccmp.rx_pn[i][j] =
  357. seq[IEEE80211_CCMP_PN_LEN - j - 1];
  358. }
  359. /*
  360. * Initialize AES key state here as an optimization so that
  361. * it does not need to be initialized for every packet.
  362. */
  363. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  364. key_data, key_len, IEEE80211_CCMP_MIC_LEN);
  365. if (IS_ERR(key->u.ccmp.tfm)) {
  366. err = PTR_ERR(key->u.ccmp.tfm);
  367. kfree(key);
  368. return ERR_PTR(err);
  369. }
  370. break;
  371. case WLAN_CIPHER_SUITE_CCMP_256:
  372. key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
  373. key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
  374. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  375. for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
  376. key->u.ccmp.rx_pn[i][j] =
  377. seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
  378. /* Initialize AES key state here as an optimization so that
  379. * it does not need to be initialized for every packet.
  380. */
  381. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
  382. key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
  383. if (IS_ERR(key->u.ccmp.tfm)) {
  384. err = PTR_ERR(key->u.ccmp.tfm);
  385. kfree(key);
  386. return ERR_PTR(err);
  387. }
  388. break;
  389. case WLAN_CIPHER_SUITE_AES_CMAC:
  390. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  391. key->conf.iv_len = 0;
  392. if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  393. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  394. else
  395. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  396. if (seq)
  397. for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
  398. key->u.aes_cmac.rx_pn[j] =
  399. seq[IEEE80211_CMAC_PN_LEN - j - 1];
  400. /*
  401. * Initialize AES key state here as an optimization so that
  402. * it does not need to be initialized for every packet.
  403. */
  404. key->u.aes_cmac.tfm =
  405. ieee80211_aes_cmac_key_setup(key_data, key_len);
  406. if (IS_ERR(key->u.aes_cmac.tfm)) {
  407. err = PTR_ERR(key->u.aes_cmac.tfm);
  408. kfree(key);
  409. return ERR_PTR(err);
  410. }
  411. break;
  412. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  413. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  414. key->conf.iv_len = 0;
  415. key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
  416. if (seq)
  417. for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
  418. key->u.aes_gmac.rx_pn[j] =
  419. seq[IEEE80211_GMAC_PN_LEN - j - 1];
  420. /* Initialize AES key state here as an optimization so that
  421. * it does not need to be initialized for every packet.
  422. */
  423. key->u.aes_gmac.tfm =
  424. ieee80211_aes_gmac_key_setup(key_data, key_len);
  425. if (IS_ERR(key->u.aes_gmac.tfm)) {
  426. err = PTR_ERR(key->u.aes_gmac.tfm);
  427. kfree(key);
  428. return ERR_PTR(err);
  429. }
  430. break;
  431. case WLAN_CIPHER_SUITE_GCMP:
  432. case WLAN_CIPHER_SUITE_GCMP_256:
  433. key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
  434. key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
  435. for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
  436. for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
  437. key->u.gcmp.rx_pn[i][j] =
  438. seq[IEEE80211_GCMP_PN_LEN - j - 1];
  439. /* Initialize AES key state here as an optimization so that
  440. * it does not need to be initialized for every packet.
  441. */
  442. key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
  443. key_len);
  444. if (IS_ERR(key->u.gcmp.tfm)) {
  445. err = PTR_ERR(key->u.gcmp.tfm);
  446. kfree(key);
  447. return ERR_PTR(err);
  448. }
  449. break;
  450. default:
  451. if (cs) {
  452. if (seq_len && seq_len != cs->pn_len) {
  453. kfree(key);
  454. return ERR_PTR(-EINVAL);
  455. }
  456. key->conf.iv_len = cs->hdr_len;
  457. key->conf.icv_len = cs->mic_len;
  458. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
  459. for (j = 0; j < seq_len; j++)
  460. key->u.gen.rx_pn[i][j] =
  461. seq[seq_len - j - 1];
  462. key->flags |= KEY_FLAG_CIPHER_SCHEME;
  463. }
  464. }
  465. memcpy(key->conf.key, key_data, key_len);
  466. INIT_LIST_HEAD(&key->list);
  467. return key;
  468. }
  469. static void ieee80211_key_free_common(struct ieee80211_key *key)
  470. {
  471. switch (key->conf.cipher) {
  472. case WLAN_CIPHER_SUITE_CCMP:
  473. case WLAN_CIPHER_SUITE_CCMP_256:
  474. ieee80211_aes_key_free(key->u.ccmp.tfm);
  475. break;
  476. case WLAN_CIPHER_SUITE_AES_CMAC:
  477. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  478. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  479. break;
  480. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  481. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  482. ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
  483. break;
  484. case WLAN_CIPHER_SUITE_GCMP:
  485. case WLAN_CIPHER_SUITE_GCMP_256:
  486. ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
  487. break;
  488. }
  489. kzfree(key);
  490. }
  491. static void __ieee80211_key_destroy(struct ieee80211_key *key,
  492. bool delay_tailroom)
  493. {
  494. if (key->local)
  495. ieee80211_key_disable_hw_accel(key);
  496. if (key->local) {
  497. struct ieee80211_sub_if_data *sdata = key->sdata;
  498. ieee80211_debugfs_key_remove(key);
  499. if (delay_tailroom) {
  500. /* see ieee80211_delayed_tailroom_dec */
  501. sdata->crypto_tx_tailroom_pending_dec++;
  502. schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
  503. HZ/2);
  504. } else {
  505. decrease_tailroom_need_count(sdata, 1);
  506. }
  507. }
  508. ieee80211_key_free_common(key);
  509. }
  510. static void ieee80211_key_destroy(struct ieee80211_key *key,
  511. bool delay_tailroom)
  512. {
  513. if (!key)
  514. return;
  515. /*
  516. * Synchronize so the TX path and rcu key iterators
  517. * can no longer be using this key before we free/remove it.
  518. */
  519. synchronize_net();
  520. __ieee80211_key_destroy(key, delay_tailroom);
  521. }
  522. void ieee80211_key_free_unused(struct ieee80211_key *key)
  523. {
  524. WARN_ON(key->sdata || key->local);
  525. ieee80211_key_free_common(key);
  526. }
  527. int ieee80211_key_link(struct ieee80211_key *key,
  528. struct ieee80211_sub_if_data *sdata,
  529. struct sta_info *sta)
  530. {
  531. struct ieee80211_local *local = sdata->local;
  532. struct ieee80211_key *old_key;
  533. int idx, ret;
  534. bool pairwise;
  535. pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  536. idx = key->conf.keyidx;
  537. key->local = sdata->local;
  538. key->sdata = sdata;
  539. key->sta = sta;
  540. mutex_lock(&sdata->local->key_mtx);
  541. if (sta && pairwise)
  542. old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
  543. else if (sta)
  544. old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
  545. else
  546. old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  547. increment_tailroom_need_count(sdata);
  548. ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  549. ieee80211_key_destroy(old_key, true);
  550. ieee80211_debugfs_key_add(key);
  551. if (!local->wowlan) {
  552. ret = ieee80211_key_enable_hw_accel(key);
  553. if (ret)
  554. ieee80211_key_free(key, true);
  555. } else {
  556. ret = 0;
  557. }
  558. mutex_unlock(&sdata->local->key_mtx);
  559. return ret;
  560. }
  561. void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
  562. {
  563. if (!key)
  564. return;
  565. /*
  566. * Replace key with nothingness if it was ever used.
  567. */
  568. if (key->sdata)
  569. ieee80211_key_replace(key->sdata, key->sta,
  570. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  571. key, NULL);
  572. ieee80211_key_destroy(key, delay_tailroom);
  573. }
  574. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  575. {
  576. struct ieee80211_key *key;
  577. struct ieee80211_sub_if_data *vlan;
  578. ASSERT_RTNL();
  579. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  580. return;
  581. mutex_lock(&sdata->local->key_mtx);
  582. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  583. sdata->crypto_tx_tailroom_pending_dec);
  584. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  585. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  586. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  587. vlan->crypto_tx_tailroom_pending_dec);
  588. }
  589. list_for_each_entry(key, &sdata->key_list, list) {
  590. increment_tailroom_need_count(sdata);
  591. ieee80211_key_enable_hw_accel(key);
  592. }
  593. mutex_unlock(&sdata->local->key_mtx);
  594. }
  595. void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
  596. {
  597. struct ieee80211_sub_if_data *vlan;
  598. mutex_lock(&sdata->local->key_mtx);
  599. sdata->crypto_tx_tailroom_needed_cnt = 0;
  600. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  601. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  602. vlan->crypto_tx_tailroom_needed_cnt = 0;
  603. }
  604. mutex_unlock(&sdata->local->key_mtx);
  605. }
  606. void ieee80211_iter_keys(struct ieee80211_hw *hw,
  607. struct ieee80211_vif *vif,
  608. void (*iter)(struct ieee80211_hw *hw,
  609. struct ieee80211_vif *vif,
  610. struct ieee80211_sta *sta,
  611. struct ieee80211_key_conf *key,
  612. void *data),
  613. void *iter_data)
  614. {
  615. struct ieee80211_local *local = hw_to_local(hw);
  616. struct ieee80211_key *key, *tmp;
  617. struct ieee80211_sub_if_data *sdata;
  618. ASSERT_RTNL();
  619. mutex_lock(&local->key_mtx);
  620. if (vif) {
  621. sdata = vif_to_sdata(vif);
  622. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  623. iter(hw, &sdata->vif,
  624. key->sta ? &key->sta->sta : NULL,
  625. &key->conf, iter_data);
  626. } else {
  627. list_for_each_entry(sdata, &local->interfaces, list)
  628. list_for_each_entry_safe(key, tmp,
  629. &sdata->key_list, list)
  630. iter(hw, &sdata->vif,
  631. key->sta ? &key->sta->sta : NULL,
  632. &key->conf, iter_data);
  633. }
  634. mutex_unlock(&local->key_mtx);
  635. }
  636. EXPORT_SYMBOL(ieee80211_iter_keys);
  637. static void
  638. _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  639. struct ieee80211_sub_if_data *sdata,
  640. void (*iter)(struct ieee80211_hw *hw,
  641. struct ieee80211_vif *vif,
  642. struct ieee80211_sta *sta,
  643. struct ieee80211_key_conf *key,
  644. void *data),
  645. void *iter_data)
  646. {
  647. struct ieee80211_key *key;
  648. list_for_each_entry_rcu(key, &sdata->key_list, list) {
  649. /* skip keys of station in removal process */
  650. if (key->sta && key->sta->removed)
  651. continue;
  652. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  653. continue;
  654. iter(hw, &sdata->vif,
  655. key->sta ? &key->sta->sta : NULL,
  656. &key->conf, iter_data);
  657. }
  658. }
  659. void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
  660. struct ieee80211_vif *vif,
  661. void (*iter)(struct ieee80211_hw *hw,
  662. struct ieee80211_vif *vif,
  663. struct ieee80211_sta *sta,
  664. struct ieee80211_key_conf *key,
  665. void *data),
  666. void *iter_data)
  667. {
  668. struct ieee80211_local *local = hw_to_local(hw);
  669. struct ieee80211_sub_if_data *sdata;
  670. if (vif) {
  671. sdata = vif_to_sdata(vif);
  672. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  673. } else {
  674. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  675. _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
  676. }
  677. }
  678. EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
  679. static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
  680. struct list_head *keys)
  681. {
  682. struct ieee80211_key *key, *tmp;
  683. decrease_tailroom_need_count(sdata,
  684. sdata->crypto_tx_tailroom_pending_dec);
  685. sdata->crypto_tx_tailroom_pending_dec = 0;
  686. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  687. list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
  688. ieee80211_key_replace(key->sdata, key->sta,
  689. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  690. key, NULL);
  691. list_add_tail(&key->list, keys);
  692. }
  693. ieee80211_debugfs_key_update_default(sdata);
  694. }
  695. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
  696. bool force_synchronize)
  697. {
  698. struct ieee80211_local *local = sdata->local;
  699. struct ieee80211_sub_if_data *vlan;
  700. struct ieee80211_sub_if_data *master;
  701. struct ieee80211_key *key, *tmp;
  702. LIST_HEAD(keys);
  703. cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
  704. mutex_lock(&local->key_mtx);
  705. ieee80211_free_keys_iface(sdata, &keys);
  706. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  707. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  708. ieee80211_free_keys_iface(vlan, &keys);
  709. }
  710. if (!list_empty(&keys) || force_synchronize)
  711. synchronize_net();
  712. list_for_each_entry_safe(key, tmp, &keys, list)
  713. __ieee80211_key_destroy(key, false);
  714. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  715. if (sdata->bss) {
  716. master = container_of(sdata->bss,
  717. struct ieee80211_sub_if_data,
  718. u.ap);
  719. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
  720. master->crypto_tx_tailroom_needed_cnt);
  721. }
  722. } else {
  723. WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
  724. sdata->crypto_tx_tailroom_pending_dec);
  725. }
  726. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  727. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  728. WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
  729. vlan->crypto_tx_tailroom_pending_dec);
  730. }
  731. mutex_unlock(&local->key_mtx);
  732. }
  733. void ieee80211_free_sta_keys(struct ieee80211_local *local,
  734. struct sta_info *sta)
  735. {
  736. struct ieee80211_key *key;
  737. int i;
  738. mutex_lock(&local->key_mtx);
  739. for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
  740. key = key_mtx_dereference(local, sta->gtk[i]);
  741. if (!key)
  742. continue;
  743. ieee80211_key_replace(key->sdata, key->sta,
  744. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  745. key, NULL);
  746. __ieee80211_key_destroy(key, true);
  747. }
  748. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  749. key = key_mtx_dereference(local, sta->ptk[i]);
  750. if (!key)
  751. continue;
  752. ieee80211_key_replace(key->sdata, key->sta,
  753. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  754. key, NULL);
  755. __ieee80211_key_destroy(key, true);
  756. }
  757. mutex_unlock(&local->key_mtx);
  758. }
  759. void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
  760. {
  761. struct ieee80211_sub_if_data *sdata;
  762. sdata = container_of(wk, struct ieee80211_sub_if_data,
  763. dec_tailroom_needed_wk.work);
  764. /*
  765. * The reason for the delayed tailroom needed decrementing is to
  766. * make roaming faster: during roaming, all keys are first deleted
  767. * and then new keys are installed. The first new key causes the
  768. * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
  769. * the cost of synchronize_net() (which can be slow). Avoid this
  770. * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
  771. * key removal for a while, so if we roam the value is larger than
  772. * zero and no 0->1 transition happens.
  773. *
  774. * The cost is that if the AP switching was from an AP with keys
  775. * to one without, we still allocate tailroom while it would no
  776. * longer be needed. However, in the typical (fast) roaming case
  777. * within an ESS this usually won't happen.
  778. */
  779. mutex_lock(&sdata->local->key_mtx);
  780. decrease_tailroom_need_count(sdata,
  781. sdata->crypto_tx_tailroom_pending_dec);
  782. sdata->crypto_tx_tailroom_pending_dec = 0;
  783. mutex_unlock(&sdata->local->key_mtx);
  784. }
  785. void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
  786. const u8 *replay_ctr, gfp_t gfp)
  787. {
  788. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  789. trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
  790. cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
  791. }
  792. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
  793. void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
  794. int tid, struct ieee80211_key_seq *seq)
  795. {
  796. struct ieee80211_key *key;
  797. const u8 *pn;
  798. key = container_of(keyconf, struct ieee80211_key, conf);
  799. switch (key->conf.cipher) {
  800. case WLAN_CIPHER_SUITE_TKIP:
  801. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  802. return;
  803. seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
  804. seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
  805. break;
  806. case WLAN_CIPHER_SUITE_CCMP:
  807. case WLAN_CIPHER_SUITE_CCMP_256:
  808. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  809. return;
  810. if (tid < 0)
  811. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  812. else
  813. pn = key->u.ccmp.rx_pn[tid];
  814. memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
  815. break;
  816. case WLAN_CIPHER_SUITE_AES_CMAC:
  817. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  818. if (WARN_ON(tid != 0))
  819. return;
  820. pn = key->u.aes_cmac.rx_pn;
  821. memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
  822. break;
  823. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  824. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  825. if (WARN_ON(tid != 0))
  826. return;
  827. pn = key->u.aes_gmac.rx_pn;
  828. memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
  829. break;
  830. case WLAN_CIPHER_SUITE_GCMP:
  831. case WLAN_CIPHER_SUITE_GCMP_256:
  832. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  833. return;
  834. if (tid < 0)
  835. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  836. else
  837. pn = key->u.gcmp.rx_pn[tid];
  838. memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
  839. break;
  840. }
  841. }
  842. EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
  843. void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
  844. int tid, struct ieee80211_key_seq *seq)
  845. {
  846. struct ieee80211_key *key;
  847. u8 *pn;
  848. key = container_of(keyconf, struct ieee80211_key, conf);
  849. switch (key->conf.cipher) {
  850. case WLAN_CIPHER_SUITE_TKIP:
  851. if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
  852. return;
  853. key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
  854. key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
  855. break;
  856. case WLAN_CIPHER_SUITE_CCMP:
  857. case WLAN_CIPHER_SUITE_CCMP_256:
  858. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  859. return;
  860. if (tid < 0)
  861. pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
  862. else
  863. pn = key->u.ccmp.rx_pn[tid];
  864. memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
  865. break;
  866. case WLAN_CIPHER_SUITE_AES_CMAC:
  867. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  868. if (WARN_ON(tid != 0))
  869. return;
  870. pn = key->u.aes_cmac.rx_pn;
  871. memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
  872. break;
  873. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  874. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  875. if (WARN_ON(tid != 0))
  876. return;
  877. pn = key->u.aes_gmac.rx_pn;
  878. memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
  879. break;
  880. case WLAN_CIPHER_SUITE_GCMP:
  881. case WLAN_CIPHER_SUITE_GCMP_256:
  882. if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
  883. return;
  884. if (tid < 0)
  885. pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
  886. else
  887. pn = key->u.gcmp.rx_pn[tid];
  888. memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
  889. break;
  890. default:
  891. WARN_ON(1);
  892. break;
  893. }
  894. }
  895. EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
  896. void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
  897. {
  898. struct ieee80211_key *key;
  899. key = container_of(keyconf, struct ieee80211_key, conf);
  900. assert_key_lock(key->local);
  901. /*
  902. * if key was uploaded, we assume the driver will/has remove(d)
  903. * it, so adjust bookkeeping accordingly
  904. */
  905. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  906. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  907. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  908. (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
  909. increment_tailroom_need_count(key->sdata);
  910. }
  911. ieee80211_key_free(key, false);
  912. }
  913. EXPORT_SYMBOL_GPL(ieee80211_remove_key);
  914. struct ieee80211_key_conf *
  915. ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
  916. struct ieee80211_key_conf *keyconf)
  917. {
  918. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  919. struct ieee80211_local *local = sdata->local;
  920. struct ieee80211_key *key;
  921. int err;
  922. if (WARN_ON(!local->wowlan))
  923. return ERR_PTR(-EINVAL);
  924. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  925. return ERR_PTR(-EINVAL);
  926. key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
  927. keyconf->keylen, keyconf->key,
  928. 0, NULL, NULL);
  929. if (IS_ERR(key))
  930. return ERR_CAST(key);
  931. if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
  932. key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
  933. err = ieee80211_key_link(key, sdata, NULL);
  934. if (err)
  935. return ERR_PTR(err);
  936. return &key->conf;
  937. }
  938. EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);