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