key.c 32 KB

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