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