xref: /linux/net/mac80211/key.c (revision 44f57d78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2007-2008	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  * Copyright 2015-2017	Intel Deutschland GmbH
9  */
10 
11 #include <linux/if_ether.h>
12 #include <linux/etherdevice.h>
13 #include <linux/list.h>
14 #include <linux/rcupdate.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/slab.h>
17 #include <linux/export.h>
18 #include <net/mac80211.h>
19 #include <crypto/algapi.h>
20 #include <asm/unaligned.h>
21 #include "ieee80211_i.h"
22 #include "driver-ops.h"
23 #include "debugfs_key.h"
24 #include "aes_ccm.h"
25 #include "aes_cmac.h"
26 #include "aes_gmac.h"
27 #include "aes_gcm.h"
28 
29 
30 /**
31  * DOC: Key handling basics
32  *
33  * Key handling in mac80211 is done based on per-interface (sub_if_data)
34  * keys and per-station keys. Since each station belongs to an interface,
35  * each station key also belongs to that interface.
36  *
37  * Hardware acceleration is done on a best-effort basis for algorithms
38  * that are implemented in software,  for each key the hardware is asked
39  * to enable that key for offloading but if it cannot do that the key is
40  * simply kept for software encryption (unless it is for an algorithm
41  * that isn't implemented in software).
42  * There is currently no way of knowing whether a key is handled in SW
43  * or HW except by looking into debugfs.
44  *
45  * All key management is internally protected by a mutex. Within all
46  * other parts of mac80211, key references are, just as STA structure
47  * references, protected by RCU. Note, however, that some things are
48  * unprotected, namely the key->sta dereferences within the hardware
49  * acceleration functions. This means that sta_info_destroy() must
50  * remove the key which waits for an RCU grace period.
51  */
52 
53 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
54 
55 static void assert_key_lock(struct ieee80211_local *local)
56 {
57 	lockdep_assert_held(&local->key_mtx);
58 }
59 
60 static void
61 update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
62 {
63 	struct ieee80211_sub_if_data *vlan;
64 
65 	if (sdata->vif.type != NL80211_IFTYPE_AP)
66 		return;
67 
68 	/* crypto_tx_tailroom_needed_cnt is protected by this */
69 	assert_key_lock(sdata->local);
70 
71 	rcu_read_lock();
72 
73 	list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
74 		vlan->crypto_tx_tailroom_needed_cnt += delta;
75 
76 	rcu_read_unlock();
77 }
78 
79 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
80 {
81 	/*
82 	 * When this count is zero, SKB resizing for allocating tailroom
83 	 * for IV or MMIC is skipped. But, this check has created two race
84 	 * cases in xmit path while transiting from zero count to one:
85 	 *
86 	 * 1. SKB resize was skipped because no key was added but just before
87 	 * the xmit key is added and SW encryption kicks off.
88 	 *
89 	 * 2. SKB resize was skipped because all the keys were hw planted but
90 	 * just before xmit one of the key is deleted and SW encryption kicks
91 	 * off.
92 	 *
93 	 * In both the above case SW encryption will find not enough space for
94 	 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
95 	 *
96 	 * Solution has been explained at
97 	 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
98 	 */
99 
100 	assert_key_lock(sdata->local);
101 
102 	update_vlan_tailroom_need_count(sdata, 1);
103 
104 	if (!sdata->crypto_tx_tailroom_needed_cnt++) {
105 		/*
106 		 * Flush all XMIT packets currently using HW encryption or no
107 		 * encryption at all if the count transition is from 0 -> 1.
108 		 */
109 		synchronize_net();
110 	}
111 }
112 
113 static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
114 					 int delta)
115 {
116 	assert_key_lock(sdata->local);
117 
118 	WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
119 
120 	update_vlan_tailroom_need_count(sdata, -delta);
121 	sdata->crypto_tx_tailroom_needed_cnt -= delta;
122 }
123 
124 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
125 {
126 	struct ieee80211_sub_if_data *sdata = key->sdata;
127 	struct sta_info *sta;
128 	int ret = -EOPNOTSUPP;
129 
130 	might_sleep();
131 
132 	if (key->flags & KEY_FLAG_TAINTED) {
133 		/* If we get here, it's during resume and the key is
134 		 * tainted so shouldn't be used/programmed any more.
135 		 * However, its flags may still indicate that it was
136 		 * programmed into the device (since we're in resume)
137 		 * so clear that flag now to avoid trying to remove
138 		 * it again later.
139 		 */
140 		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
141 		    !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
142 					 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
143 					 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
144 			increment_tailroom_need_count(sdata);
145 
146 		key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
147 		return -EINVAL;
148 	}
149 
150 	if (!key->local->ops->set_key)
151 		goto out_unsupported;
152 
153 	assert_key_lock(key->local);
154 
155 	sta = key->sta;
156 
157 	/*
158 	 * If this is a per-STA GTK, check if it
159 	 * is supported; if not, return.
160 	 */
161 	if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
162 	    !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
163 		goto out_unsupported;
164 
165 	if (sta && !sta->uploaded)
166 		goto out_unsupported;
167 
168 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
169 		/*
170 		 * The driver doesn't know anything about VLAN interfaces.
171 		 * Hence, don't send GTKs for VLAN interfaces to the driver.
172 		 */
173 		if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
174 			ret = 1;
175 			goto out_unsupported;
176 		}
177 	}
178 
179 	ret = drv_set_key(key->local, SET_KEY, sdata,
180 			  sta ? &sta->sta : NULL, &key->conf);
181 
182 	if (!ret) {
183 		key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
184 
185 		if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
186 					 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
187 					 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
188 			decrease_tailroom_need_count(sdata, 1);
189 
190 		WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
191 			(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
192 
193 		WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
194 			(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
195 
196 		return 0;
197 	}
198 
199 	if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
200 		sdata_err(sdata,
201 			  "failed to set key (%d, %pM) to hardware (%d)\n",
202 			  key->conf.keyidx,
203 			  sta ? sta->sta.addr : bcast_addr, ret);
204 
205  out_unsupported:
206 	switch (key->conf.cipher) {
207 	case WLAN_CIPHER_SUITE_WEP40:
208 	case WLAN_CIPHER_SUITE_WEP104:
209 	case WLAN_CIPHER_SUITE_TKIP:
210 	case WLAN_CIPHER_SUITE_CCMP:
211 	case WLAN_CIPHER_SUITE_CCMP_256:
212 	case WLAN_CIPHER_SUITE_AES_CMAC:
213 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
214 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
215 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
216 	case WLAN_CIPHER_SUITE_GCMP:
217 	case WLAN_CIPHER_SUITE_GCMP_256:
218 		/* all of these we can do in software - if driver can */
219 		if (ret == 1)
220 			return 0;
221 		if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
222 			return -EINVAL;
223 		return 0;
224 	default:
225 		return -EINVAL;
226 	}
227 }
228 
229 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
230 {
231 	struct ieee80211_sub_if_data *sdata;
232 	struct sta_info *sta;
233 	int ret;
234 
235 	might_sleep();
236 
237 	if (!key || !key->local->ops->set_key)
238 		return;
239 
240 	assert_key_lock(key->local);
241 
242 	if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
243 		return;
244 
245 	sta = key->sta;
246 	sdata = key->sdata;
247 
248 	if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
249 				 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
250 				 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
251 		increment_tailroom_need_count(sdata);
252 
253 	key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
254 	ret = drv_set_key(key->local, DISABLE_KEY, sdata,
255 			  sta ? &sta->sta : NULL, &key->conf);
256 
257 	if (ret)
258 		sdata_err(sdata,
259 			  "failed to remove key (%d, %pM) from hardware (%d)\n",
260 			  key->conf.keyidx,
261 			  sta ? sta->sta.addr : bcast_addr, ret);
262 }
263 
264 int ieee80211_set_tx_key(struct ieee80211_key *key)
265 {
266 	struct sta_info *sta = key->sta;
267 	struct ieee80211_local *local = key->local;
268 
269 	assert_key_lock(local);
270 
271 	sta->ptk_idx = key->conf.keyidx;
272 	ieee80211_check_fast_xmit(sta);
273 
274 	return 0;
275 }
276 
277 static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
278 				    struct ieee80211_key *new_key,
279 				    bool pairwise)
280 {
281 	struct ieee80211_sub_if_data *sdata;
282 	struct ieee80211_local *local;
283 	struct sta_info *sta;
284 	int ret;
285 
286 	/* Aggregation sessions are OK when running on SW crypto.
287 	 * A broken remote STA may cause issues not observed with HW
288 	 * crypto, though.
289 	 */
290 	if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
291 		return 0;
292 
293 	assert_key_lock(old_key->local);
294 	sta = old_key->sta;
295 
296 	/* Unicast rekey without Extended Key ID needs special handling */
297 	if (new_key && sta && pairwise &&
298 	    rcu_access_pointer(sta->ptk[sta->ptk_idx]) == old_key) {
299 		local = old_key->local;
300 		sdata = old_key->sdata;
301 
302 		/* Stop TX till we are on the new key */
303 		old_key->flags |= KEY_FLAG_TAINTED;
304 		ieee80211_clear_fast_xmit(sta);
305 
306 		/* Aggregation sessions during rekey are complicated due to the
307 		 * reorder buffer and retransmits. Side step that by blocking
308 		 * aggregation during rekey and tear down running sessions.
309 		 */
310 		if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
311 			set_sta_flag(sta, WLAN_STA_BLOCK_BA);
312 			ieee80211_sta_tear_down_BA_sessions(sta,
313 							    AGG_STOP_LOCAL_REQUEST);
314 		}
315 
316 		if (!wiphy_ext_feature_isset(local->hw.wiphy,
317 					     NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
318 			pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
319 					    sta->sta.addr);
320 			/* Flushing the driver queues *may* help prevent
321 			 * the clear text leaks and freezes.
322 			 */
323 			ieee80211_flush_queues(local, sdata, false);
324 		}
325 	}
326 
327 	ieee80211_key_disable_hw_accel(old_key);
328 
329 	if (new_key)
330 		ret = ieee80211_key_enable_hw_accel(new_key);
331 	else
332 		ret = 0;
333 
334 	return ret;
335 }
336 
337 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
338 					int idx, bool uni, bool multi)
339 {
340 	struct ieee80211_key *key = NULL;
341 
342 	assert_key_lock(sdata->local);
343 
344 	if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
345 		key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
346 
347 	if (uni) {
348 		rcu_assign_pointer(sdata->default_unicast_key, key);
349 		ieee80211_check_fast_xmit_iface(sdata);
350 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
351 			drv_set_default_unicast_key(sdata->local, sdata, idx);
352 	}
353 
354 	if (multi)
355 		rcu_assign_pointer(sdata->default_multicast_key, key);
356 
357 	ieee80211_debugfs_key_update_default(sdata);
358 }
359 
360 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
361 			       bool uni, bool multi)
362 {
363 	mutex_lock(&sdata->local->key_mtx);
364 	__ieee80211_set_default_key(sdata, idx, uni, multi);
365 	mutex_unlock(&sdata->local->key_mtx);
366 }
367 
368 static void
369 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
370 {
371 	struct ieee80211_key *key = NULL;
372 
373 	assert_key_lock(sdata->local);
374 
375 	if (idx >= NUM_DEFAULT_KEYS &&
376 	    idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
377 		key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
378 
379 	rcu_assign_pointer(sdata->default_mgmt_key, key);
380 
381 	ieee80211_debugfs_key_update_default(sdata);
382 }
383 
384 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
385 				    int idx)
386 {
387 	mutex_lock(&sdata->local->key_mtx);
388 	__ieee80211_set_default_mgmt_key(sdata, idx);
389 	mutex_unlock(&sdata->local->key_mtx);
390 }
391 
392 
393 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
394 				  struct sta_info *sta,
395 				  bool pairwise,
396 				  struct ieee80211_key *old,
397 				  struct ieee80211_key *new)
398 {
399 	int idx;
400 	int ret;
401 	bool defunikey, defmultikey, defmgmtkey;
402 
403 	/* caller must provide at least one old/new */
404 	if (WARN_ON(!new && !old))
405 		return 0;
406 
407 	if (new)
408 		list_add_tail_rcu(&new->list, &sdata->key_list);
409 
410 	WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
411 
412 	if (old) {
413 		idx = old->conf.keyidx;
414 		ret = ieee80211_hw_key_replace(old, new, pairwise);
415 	} else {
416 		/* new must be provided in case old is not */
417 		idx = new->conf.keyidx;
418 		if (!new->local->wowlan)
419 			ret = ieee80211_key_enable_hw_accel(new);
420 		else
421 			ret = 0;
422 	}
423 
424 	if (ret)
425 		return ret;
426 
427 	if (sta) {
428 		if (pairwise) {
429 			rcu_assign_pointer(sta->ptk[idx], new);
430 			if (new &&
431 			    !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
432 				sta->ptk_idx = idx;
433 				clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
434 				ieee80211_check_fast_xmit(sta);
435 			}
436 		} else {
437 			rcu_assign_pointer(sta->gtk[idx], new);
438 		}
439 		/* Only needed for transition from no key -> key.
440 		 * Still triggers unnecessary when using Extended Key ID
441 		 * and installing the second key ID the first time.
442 		 */
443 		if (new && !old)
444 			ieee80211_check_fast_rx(sta);
445 	} else {
446 		defunikey = old &&
447 			old == key_mtx_dereference(sdata->local,
448 						sdata->default_unicast_key);
449 		defmultikey = old &&
450 			old == key_mtx_dereference(sdata->local,
451 						sdata->default_multicast_key);
452 		defmgmtkey = old &&
453 			old == key_mtx_dereference(sdata->local,
454 						sdata->default_mgmt_key);
455 
456 		if (defunikey && !new)
457 			__ieee80211_set_default_key(sdata, -1, true, false);
458 		if (defmultikey && !new)
459 			__ieee80211_set_default_key(sdata, -1, false, true);
460 		if (defmgmtkey && !new)
461 			__ieee80211_set_default_mgmt_key(sdata, -1);
462 
463 		rcu_assign_pointer(sdata->keys[idx], new);
464 		if (defunikey && new)
465 			__ieee80211_set_default_key(sdata, new->conf.keyidx,
466 						    true, false);
467 		if (defmultikey && new)
468 			__ieee80211_set_default_key(sdata, new->conf.keyidx,
469 						    false, true);
470 		if (defmgmtkey && new)
471 			__ieee80211_set_default_mgmt_key(sdata,
472 							 new->conf.keyidx);
473 	}
474 
475 	if (old)
476 		list_del_rcu(&old->list);
477 
478 	return 0;
479 }
480 
481 struct ieee80211_key *
482 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
483 		    const u8 *key_data,
484 		    size_t seq_len, const u8 *seq,
485 		    const struct ieee80211_cipher_scheme *cs)
486 {
487 	struct ieee80211_key *key;
488 	int i, j, err;
489 
490 	if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
491 		return ERR_PTR(-EINVAL);
492 
493 	key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
494 	if (!key)
495 		return ERR_PTR(-ENOMEM);
496 
497 	/*
498 	 * Default to software encryption; we'll later upload the
499 	 * key to the hardware if possible.
500 	 */
501 	key->conf.flags = 0;
502 	key->flags = 0;
503 
504 	key->conf.cipher = cipher;
505 	key->conf.keyidx = idx;
506 	key->conf.keylen = key_len;
507 	switch (cipher) {
508 	case WLAN_CIPHER_SUITE_WEP40:
509 	case WLAN_CIPHER_SUITE_WEP104:
510 		key->conf.iv_len = IEEE80211_WEP_IV_LEN;
511 		key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
512 		break;
513 	case WLAN_CIPHER_SUITE_TKIP:
514 		key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
515 		key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
516 		if (seq) {
517 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
518 				key->u.tkip.rx[i].iv32 =
519 					get_unaligned_le32(&seq[2]);
520 				key->u.tkip.rx[i].iv16 =
521 					get_unaligned_le16(seq);
522 			}
523 		}
524 		spin_lock_init(&key->u.tkip.txlock);
525 		break;
526 	case WLAN_CIPHER_SUITE_CCMP:
527 		key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
528 		key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
529 		if (seq) {
530 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
531 				for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
532 					key->u.ccmp.rx_pn[i][j] =
533 						seq[IEEE80211_CCMP_PN_LEN - j - 1];
534 		}
535 		/*
536 		 * Initialize AES key state here as an optimization so that
537 		 * it does not need to be initialized for every packet.
538 		 */
539 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
540 			key_data, key_len, IEEE80211_CCMP_MIC_LEN);
541 		if (IS_ERR(key->u.ccmp.tfm)) {
542 			err = PTR_ERR(key->u.ccmp.tfm);
543 			kfree(key);
544 			return ERR_PTR(err);
545 		}
546 		break;
547 	case WLAN_CIPHER_SUITE_CCMP_256:
548 		key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
549 		key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
550 		for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
551 			for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
552 				key->u.ccmp.rx_pn[i][j] =
553 					seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
554 		/* Initialize AES key state here as an optimization so that
555 		 * it does not need to be initialized for every packet.
556 		 */
557 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
558 			key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
559 		if (IS_ERR(key->u.ccmp.tfm)) {
560 			err = PTR_ERR(key->u.ccmp.tfm);
561 			kfree(key);
562 			return ERR_PTR(err);
563 		}
564 		break;
565 	case WLAN_CIPHER_SUITE_AES_CMAC:
566 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
567 		key->conf.iv_len = 0;
568 		if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
569 			key->conf.icv_len = sizeof(struct ieee80211_mmie);
570 		else
571 			key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
572 		if (seq)
573 			for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
574 				key->u.aes_cmac.rx_pn[j] =
575 					seq[IEEE80211_CMAC_PN_LEN - j - 1];
576 		/*
577 		 * Initialize AES key state here as an optimization so that
578 		 * it does not need to be initialized for every packet.
579 		 */
580 		key->u.aes_cmac.tfm =
581 			ieee80211_aes_cmac_key_setup(key_data, key_len);
582 		if (IS_ERR(key->u.aes_cmac.tfm)) {
583 			err = PTR_ERR(key->u.aes_cmac.tfm);
584 			kfree(key);
585 			return ERR_PTR(err);
586 		}
587 		break;
588 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
589 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
590 		key->conf.iv_len = 0;
591 		key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
592 		if (seq)
593 			for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
594 				key->u.aes_gmac.rx_pn[j] =
595 					seq[IEEE80211_GMAC_PN_LEN - j - 1];
596 		/* Initialize AES key state here as an optimization so that
597 		 * it does not need to be initialized for every packet.
598 		 */
599 		key->u.aes_gmac.tfm =
600 			ieee80211_aes_gmac_key_setup(key_data, key_len);
601 		if (IS_ERR(key->u.aes_gmac.tfm)) {
602 			err = PTR_ERR(key->u.aes_gmac.tfm);
603 			kfree(key);
604 			return ERR_PTR(err);
605 		}
606 		break;
607 	case WLAN_CIPHER_SUITE_GCMP:
608 	case WLAN_CIPHER_SUITE_GCMP_256:
609 		key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
610 		key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
611 		for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
612 			for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
613 				key->u.gcmp.rx_pn[i][j] =
614 					seq[IEEE80211_GCMP_PN_LEN - j - 1];
615 		/* Initialize AES key state here as an optimization so that
616 		 * it does not need to be initialized for every packet.
617 		 */
618 		key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
619 								      key_len);
620 		if (IS_ERR(key->u.gcmp.tfm)) {
621 			err = PTR_ERR(key->u.gcmp.tfm);
622 			kfree(key);
623 			return ERR_PTR(err);
624 		}
625 		break;
626 	default:
627 		if (cs) {
628 			if (seq_len && seq_len != cs->pn_len) {
629 				kfree(key);
630 				return ERR_PTR(-EINVAL);
631 			}
632 
633 			key->conf.iv_len = cs->hdr_len;
634 			key->conf.icv_len = cs->mic_len;
635 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
636 				for (j = 0; j < seq_len; j++)
637 					key->u.gen.rx_pn[i][j] =
638 							seq[seq_len - j - 1];
639 			key->flags |= KEY_FLAG_CIPHER_SCHEME;
640 		}
641 	}
642 	memcpy(key->conf.key, key_data, key_len);
643 	INIT_LIST_HEAD(&key->list);
644 
645 	return key;
646 }
647 
648 static void ieee80211_key_free_common(struct ieee80211_key *key)
649 {
650 	switch (key->conf.cipher) {
651 	case WLAN_CIPHER_SUITE_CCMP:
652 	case WLAN_CIPHER_SUITE_CCMP_256:
653 		ieee80211_aes_key_free(key->u.ccmp.tfm);
654 		break;
655 	case WLAN_CIPHER_SUITE_AES_CMAC:
656 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
657 		ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
658 		break;
659 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
660 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
661 		ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
662 		break;
663 	case WLAN_CIPHER_SUITE_GCMP:
664 	case WLAN_CIPHER_SUITE_GCMP_256:
665 		ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
666 		break;
667 	}
668 	kzfree(key);
669 }
670 
671 static void __ieee80211_key_destroy(struct ieee80211_key *key,
672 				    bool delay_tailroom)
673 {
674 	if (key->local) {
675 		struct ieee80211_sub_if_data *sdata = key->sdata;
676 
677 		ieee80211_debugfs_key_remove(key);
678 
679 		if (delay_tailroom) {
680 			/* see ieee80211_delayed_tailroom_dec */
681 			sdata->crypto_tx_tailroom_pending_dec++;
682 			schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
683 					      HZ/2);
684 		} else {
685 			decrease_tailroom_need_count(sdata, 1);
686 		}
687 	}
688 
689 	ieee80211_key_free_common(key);
690 }
691 
692 static void ieee80211_key_destroy(struct ieee80211_key *key,
693 				  bool delay_tailroom)
694 {
695 	if (!key)
696 		return;
697 
698 	/*
699 	 * Synchronize so the TX path and rcu key iterators
700 	 * can no longer be using this key before we free/remove it.
701 	 */
702 	synchronize_net();
703 
704 	__ieee80211_key_destroy(key, delay_tailroom);
705 }
706 
707 void ieee80211_key_free_unused(struct ieee80211_key *key)
708 {
709 	WARN_ON(key->sdata || key->local);
710 	ieee80211_key_free_common(key);
711 }
712 
713 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
714 				    struct ieee80211_key *old,
715 				    struct ieee80211_key *new)
716 {
717 	u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
718 	u8 *tk_old, *tk_new;
719 
720 	if (!old || new->conf.keylen != old->conf.keylen)
721 		return false;
722 
723 	tk_old = old->conf.key;
724 	tk_new = new->conf.key;
725 
726 	/*
727 	 * In station mode, don't compare the TX MIC key, as it's never used
728 	 * and offloaded rekeying may not care to send it to the host. This
729 	 * is the case in iwlwifi, for example.
730 	 */
731 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
732 	    new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
733 	    new->conf.keylen == WLAN_KEY_LEN_TKIP &&
734 	    !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
735 		memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
736 		memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
737 		memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
738 		memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
739 		tk_old = tkip_old;
740 		tk_new = tkip_new;
741 	}
742 
743 	return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
744 }
745 
746 int ieee80211_key_link(struct ieee80211_key *key,
747 		       struct ieee80211_sub_if_data *sdata,
748 		       struct sta_info *sta)
749 {
750 	struct ieee80211_key *old_key;
751 	int idx = key->conf.keyidx;
752 	bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
753 	/*
754 	 * We want to delay tailroom updates only for station - in that
755 	 * case it helps roaming speed, but in other cases it hurts and
756 	 * can cause warnings to appear.
757 	 */
758 	bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
759 	int ret = -EOPNOTSUPP;
760 
761 	mutex_lock(&sdata->local->key_mtx);
762 
763 	if (sta && pairwise) {
764 		struct ieee80211_key *alt_key;
765 
766 		old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
767 		alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
768 
769 		/* The rekey code assumes that the old and new key are using
770 		 * the same cipher. Enforce the assumption for pairwise keys.
771 		 */
772 		if (key &&
773 		    ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
774 		     (old_key && old_key->conf.cipher != key->conf.cipher)))
775 			goto out;
776 	} else if (sta) {
777 		old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
778 	} else {
779 		old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
780 	}
781 
782 	/* Non-pairwise keys must also not switch the cipher on rekey */
783 	if (!pairwise) {
784 		if (key && old_key && old_key->conf.cipher != key->conf.cipher)
785 			goto out;
786 	}
787 
788 	/*
789 	 * Silently accept key re-installation without really installing the
790 	 * new version of the key to avoid nonce reuse or replay issues.
791 	 */
792 	if (ieee80211_key_identical(sdata, old_key, key)) {
793 		ieee80211_key_free_unused(key);
794 		ret = 0;
795 		goto out;
796 	}
797 
798 	key->local = sdata->local;
799 	key->sdata = sdata;
800 	key->sta = sta;
801 
802 	increment_tailroom_need_count(sdata);
803 
804 	ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
805 
806 	if (!ret) {
807 		ieee80211_debugfs_key_add(key);
808 		ieee80211_key_destroy(old_key, delay_tailroom);
809 	} else {
810 		ieee80211_key_free(key, delay_tailroom);
811 	}
812 
813  out:
814 	mutex_unlock(&sdata->local->key_mtx);
815 
816 	return ret;
817 }
818 
819 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
820 {
821 	if (!key)
822 		return;
823 
824 	/*
825 	 * Replace key with nothingness if it was ever used.
826 	 */
827 	if (key->sdata)
828 		ieee80211_key_replace(key->sdata, key->sta,
829 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
830 				key, NULL);
831 	ieee80211_key_destroy(key, delay_tailroom);
832 }
833 
834 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
835 {
836 	struct ieee80211_key *key;
837 	struct ieee80211_sub_if_data *vlan;
838 
839 	ASSERT_RTNL();
840 
841 	if (WARN_ON(!ieee80211_sdata_running(sdata)))
842 		return;
843 
844 	mutex_lock(&sdata->local->key_mtx);
845 
846 	WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
847 		     sdata->crypto_tx_tailroom_pending_dec);
848 
849 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
850 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
851 			WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
852 				     vlan->crypto_tx_tailroom_pending_dec);
853 	}
854 
855 	list_for_each_entry(key, &sdata->key_list, list) {
856 		increment_tailroom_need_count(sdata);
857 		ieee80211_key_enable_hw_accel(key);
858 	}
859 
860 	mutex_unlock(&sdata->local->key_mtx);
861 }
862 
863 void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
864 {
865 	struct ieee80211_sub_if_data *vlan;
866 
867 	mutex_lock(&sdata->local->key_mtx);
868 
869 	sdata->crypto_tx_tailroom_needed_cnt = 0;
870 
871 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
872 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
873 			vlan->crypto_tx_tailroom_needed_cnt = 0;
874 	}
875 
876 	mutex_unlock(&sdata->local->key_mtx);
877 }
878 
879 void ieee80211_iter_keys(struct ieee80211_hw *hw,
880 			 struct ieee80211_vif *vif,
881 			 void (*iter)(struct ieee80211_hw *hw,
882 				      struct ieee80211_vif *vif,
883 				      struct ieee80211_sta *sta,
884 				      struct ieee80211_key_conf *key,
885 				      void *data),
886 			 void *iter_data)
887 {
888 	struct ieee80211_local *local = hw_to_local(hw);
889 	struct ieee80211_key *key, *tmp;
890 	struct ieee80211_sub_if_data *sdata;
891 
892 	ASSERT_RTNL();
893 
894 	mutex_lock(&local->key_mtx);
895 	if (vif) {
896 		sdata = vif_to_sdata(vif);
897 		list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
898 			iter(hw, &sdata->vif,
899 			     key->sta ? &key->sta->sta : NULL,
900 			     &key->conf, iter_data);
901 	} else {
902 		list_for_each_entry(sdata, &local->interfaces, list)
903 			list_for_each_entry_safe(key, tmp,
904 						 &sdata->key_list, list)
905 				iter(hw, &sdata->vif,
906 				     key->sta ? &key->sta->sta : NULL,
907 				     &key->conf, iter_data);
908 	}
909 	mutex_unlock(&local->key_mtx);
910 }
911 EXPORT_SYMBOL(ieee80211_iter_keys);
912 
913 static void
914 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
915 			 struct ieee80211_sub_if_data *sdata,
916 			 void (*iter)(struct ieee80211_hw *hw,
917 				      struct ieee80211_vif *vif,
918 				      struct ieee80211_sta *sta,
919 				      struct ieee80211_key_conf *key,
920 				      void *data),
921 			 void *iter_data)
922 {
923 	struct ieee80211_key *key;
924 
925 	list_for_each_entry_rcu(key, &sdata->key_list, list) {
926 		/* skip keys of station in removal process */
927 		if (key->sta && key->sta->removed)
928 			continue;
929 		if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
930 			continue;
931 
932 		iter(hw, &sdata->vif,
933 		     key->sta ? &key->sta->sta : NULL,
934 		     &key->conf, iter_data);
935 	}
936 }
937 
938 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
939 			     struct ieee80211_vif *vif,
940 			     void (*iter)(struct ieee80211_hw *hw,
941 					  struct ieee80211_vif *vif,
942 					  struct ieee80211_sta *sta,
943 					  struct ieee80211_key_conf *key,
944 					  void *data),
945 			     void *iter_data)
946 {
947 	struct ieee80211_local *local = hw_to_local(hw);
948 	struct ieee80211_sub_if_data *sdata;
949 
950 	if (vif) {
951 		sdata = vif_to_sdata(vif);
952 		_ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
953 	} else {
954 		list_for_each_entry_rcu(sdata, &local->interfaces, list)
955 			_ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
956 	}
957 }
958 EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
959 
960 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
961 				      struct list_head *keys)
962 {
963 	struct ieee80211_key *key, *tmp;
964 
965 	decrease_tailroom_need_count(sdata,
966 				     sdata->crypto_tx_tailroom_pending_dec);
967 	sdata->crypto_tx_tailroom_pending_dec = 0;
968 
969 	ieee80211_debugfs_key_remove_mgmt_default(sdata);
970 
971 	list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
972 		ieee80211_key_replace(key->sdata, key->sta,
973 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
974 				key, NULL);
975 		list_add_tail(&key->list, keys);
976 	}
977 
978 	ieee80211_debugfs_key_update_default(sdata);
979 }
980 
981 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
982 			 bool force_synchronize)
983 {
984 	struct ieee80211_local *local = sdata->local;
985 	struct ieee80211_sub_if_data *vlan;
986 	struct ieee80211_sub_if_data *master;
987 	struct ieee80211_key *key, *tmp;
988 	LIST_HEAD(keys);
989 
990 	cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
991 
992 	mutex_lock(&local->key_mtx);
993 
994 	ieee80211_free_keys_iface(sdata, &keys);
995 
996 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
997 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
998 			ieee80211_free_keys_iface(vlan, &keys);
999 	}
1000 
1001 	if (!list_empty(&keys) || force_synchronize)
1002 		synchronize_net();
1003 	list_for_each_entry_safe(key, tmp, &keys, list)
1004 		__ieee80211_key_destroy(key, false);
1005 
1006 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1007 		if (sdata->bss) {
1008 			master = container_of(sdata->bss,
1009 					      struct ieee80211_sub_if_data,
1010 					      u.ap);
1011 
1012 			WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1013 				     master->crypto_tx_tailroom_needed_cnt);
1014 		}
1015 	} else {
1016 		WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1017 			     sdata->crypto_tx_tailroom_pending_dec);
1018 	}
1019 
1020 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
1021 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1022 			WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1023 				     vlan->crypto_tx_tailroom_pending_dec);
1024 	}
1025 
1026 	mutex_unlock(&local->key_mtx);
1027 }
1028 
1029 void ieee80211_free_sta_keys(struct ieee80211_local *local,
1030 			     struct sta_info *sta)
1031 {
1032 	struct ieee80211_key *key;
1033 	int i;
1034 
1035 	mutex_lock(&local->key_mtx);
1036 	for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
1037 		key = key_mtx_dereference(local, sta->gtk[i]);
1038 		if (!key)
1039 			continue;
1040 		ieee80211_key_replace(key->sdata, key->sta,
1041 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1042 				key, NULL);
1043 		__ieee80211_key_destroy(key, key->sdata->vif.type ==
1044 					NL80211_IFTYPE_STATION);
1045 	}
1046 
1047 	for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1048 		key = key_mtx_dereference(local, sta->ptk[i]);
1049 		if (!key)
1050 			continue;
1051 		ieee80211_key_replace(key->sdata, key->sta,
1052 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1053 				key, NULL);
1054 		__ieee80211_key_destroy(key, key->sdata->vif.type ==
1055 					NL80211_IFTYPE_STATION);
1056 	}
1057 
1058 	mutex_unlock(&local->key_mtx);
1059 }
1060 
1061 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1062 {
1063 	struct ieee80211_sub_if_data *sdata;
1064 
1065 	sdata = container_of(wk, struct ieee80211_sub_if_data,
1066 			     dec_tailroom_needed_wk.work);
1067 
1068 	/*
1069 	 * The reason for the delayed tailroom needed decrementing is to
1070 	 * make roaming faster: during roaming, all keys are first deleted
1071 	 * and then new keys are installed. The first new key causes the
1072 	 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1073 	 * the cost of synchronize_net() (which can be slow). Avoid this
1074 	 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1075 	 * key removal for a while, so if we roam the value is larger than
1076 	 * zero and no 0->1 transition happens.
1077 	 *
1078 	 * The cost is that if the AP switching was from an AP with keys
1079 	 * to one without, we still allocate tailroom while it would no
1080 	 * longer be needed. However, in the typical (fast) roaming case
1081 	 * within an ESS this usually won't happen.
1082 	 */
1083 
1084 	mutex_lock(&sdata->local->key_mtx);
1085 	decrease_tailroom_need_count(sdata,
1086 				     sdata->crypto_tx_tailroom_pending_dec);
1087 	sdata->crypto_tx_tailroom_pending_dec = 0;
1088 	mutex_unlock(&sdata->local->key_mtx);
1089 }
1090 
1091 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1092 				const u8 *replay_ctr, gfp_t gfp)
1093 {
1094 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1095 
1096 	trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1097 
1098 	cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1099 }
1100 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
1101 
1102 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1103 			      int tid, struct ieee80211_key_seq *seq)
1104 {
1105 	struct ieee80211_key *key;
1106 	const u8 *pn;
1107 
1108 	key = container_of(keyconf, struct ieee80211_key, conf);
1109 
1110 	switch (key->conf.cipher) {
1111 	case WLAN_CIPHER_SUITE_TKIP:
1112 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1113 			return;
1114 		seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1115 		seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1116 		break;
1117 	case WLAN_CIPHER_SUITE_CCMP:
1118 	case WLAN_CIPHER_SUITE_CCMP_256:
1119 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1120 			return;
1121 		if (tid < 0)
1122 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1123 		else
1124 			pn = key->u.ccmp.rx_pn[tid];
1125 		memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
1126 		break;
1127 	case WLAN_CIPHER_SUITE_AES_CMAC:
1128 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1129 		if (WARN_ON(tid != 0))
1130 			return;
1131 		pn = key->u.aes_cmac.rx_pn;
1132 		memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
1133 		break;
1134 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1135 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1136 		if (WARN_ON(tid != 0))
1137 			return;
1138 		pn = key->u.aes_gmac.rx_pn;
1139 		memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1140 		break;
1141 	case WLAN_CIPHER_SUITE_GCMP:
1142 	case WLAN_CIPHER_SUITE_GCMP_256:
1143 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1144 			return;
1145 		if (tid < 0)
1146 			pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1147 		else
1148 			pn = key->u.gcmp.rx_pn[tid];
1149 		memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1150 		break;
1151 	}
1152 }
1153 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
1154 
1155 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1156 			      int tid, struct ieee80211_key_seq *seq)
1157 {
1158 	struct ieee80211_key *key;
1159 	u8 *pn;
1160 
1161 	key = container_of(keyconf, struct ieee80211_key, conf);
1162 
1163 	switch (key->conf.cipher) {
1164 	case WLAN_CIPHER_SUITE_TKIP:
1165 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1166 			return;
1167 		key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1168 		key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1169 		break;
1170 	case WLAN_CIPHER_SUITE_CCMP:
1171 	case WLAN_CIPHER_SUITE_CCMP_256:
1172 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1173 			return;
1174 		if (tid < 0)
1175 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1176 		else
1177 			pn = key->u.ccmp.rx_pn[tid];
1178 		memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1179 		break;
1180 	case WLAN_CIPHER_SUITE_AES_CMAC:
1181 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1182 		if (WARN_ON(tid != 0))
1183 			return;
1184 		pn = key->u.aes_cmac.rx_pn;
1185 		memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1186 		break;
1187 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1188 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1189 		if (WARN_ON(tid != 0))
1190 			return;
1191 		pn = key->u.aes_gmac.rx_pn;
1192 		memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1193 		break;
1194 	case WLAN_CIPHER_SUITE_GCMP:
1195 	case WLAN_CIPHER_SUITE_GCMP_256:
1196 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1197 			return;
1198 		if (tid < 0)
1199 			pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1200 		else
1201 			pn = key->u.gcmp.rx_pn[tid];
1202 		memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1203 		break;
1204 	default:
1205 		WARN_ON(1);
1206 		break;
1207 	}
1208 }
1209 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1210 
1211 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1212 {
1213 	struct ieee80211_key *key;
1214 
1215 	key = container_of(keyconf, struct ieee80211_key, conf);
1216 
1217 	assert_key_lock(key->local);
1218 
1219 	/*
1220 	 * if key was uploaded, we assume the driver will/has remove(d)
1221 	 * it, so adjust bookkeeping accordingly
1222 	 */
1223 	if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1224 		key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1225 
1226 		if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1227 					 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1228 					 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
1229 			increment_tailroom_need_count(key->sdata);
1230 	}
1231 
1232 	ieee80211_key_free(key, false);
1233 }
1234 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1235 
1236 struct ieee80211_key_conf *
1237 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1238 			struct ieee80211_key_conf *keyconf)
1239 {
1240 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1241 	struct ieee80211_local *local = sdata->local;
1242 	struct ieee80211_key *key;
1243 	int err;
1244 
1245 	if (WARN_ON(!local->wowlan))
1246 		return ERR_PTR(-EINVAL);
1247 
1248 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1249 		return ERR_PTR(-EINVAL);
1250 
1251 	key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1252 				  keyconf->keylen, keyconf->key,
1253 				  0, NULL, NULL);
1254 	if (IS_ERR(key))
1255 		return ERR_CAST(key);
1256 
1257 	if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1258 		key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1259 
1260 	err = ieee80211_key_link(key, sdata, NULL);
1261 	if (err)
1262 		return ERR_PTR(err);
1263 
1264 	return &key->conf;
1265 }
1266 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
1267