xref: /openbmc/linux/net/mac80211/key.c (revision d4fd6347)
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 	struct ieee80211_key *old;
272 
273 	assert_key_lock(local);
274 
275 	old = key_mtx_dereference(local, sta->ptk[sta->ptk_idx]);
276 	sta->ptk_idx = key->conf.keyidx;
277 	ieee80211_check_fast_xmit(sta);
278 
279 	return 0;
280 }
281 
282 static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
283 				    struct ieee80211_key *new_key,
284 				    bool pairwise)
285 {
286 	struct ieee80211_sub_if_data *sdata;
287 	struct ieee80211_local *local;
288 	struct sta_info *sta;
289 	int ret;
290 
291 	/* Aggregation sessions are OK when running on SW crypto.
292 	 * A broken remote STA may cause issues not observed with HW
293 	 * crypto, though.
294 	 */
295 	if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
296 		return 0;
297 
298 	assert_key_lock(old_key->local);
299 	sta = old_key->sta;
300 
301 	/* Unicast rekey without Extended Key ID needs special handling */
302 	if (new_key && sta && pairwise &&
303 	    rcu_access_pointer(sta->ptk[sta->ptk_idx]) == old_key) {
304 		local = old_key->local;
305 		sdata = old_key->sdata;
306 
307 		/* Stop TX till we are on the new key */
308 		old_key->flags |= KEY_FLAG_TAINTED;
309 		ieee80211_clear_fast_xmit(sta);
310 
311 		/* Aggregation sessions during rekey are complicated due to the
312 		 * reorder buffer and retransmits. Side step that by blocking
313 		 * aggregation during rekey and tear down running sessions.
314 		 */
315 		if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
316 			set_sta_flag(sta, WLAN_STA_BLOCK_BA);
317 			ieee80211_sta_tear_down_BA_sessions(sta,
318 							    AGG_STOP_LOCAL_REQUEST);
319 		}
320 
321 		if (!wiphy_ext_feature_isset(local->hw.wiphy,
322 					     NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
323 			pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
324 					    sta->sta.addr);
325 			/* Flushing the driver queues *may* help prevent
326 			 * the clear text leaks and freezes.
327 			 */
328 			ieee80211_flush_queues(local, sdata, false);
329 		}
330 	}
331 
332 	ieee80211_key_disable_hw_accel(old_key);
333 
334 	if (new_key)
335 		ret = ieee80211_key_enable_hw_accel(new_key);
336 	else
337 		ret = 0;
338 
339 	return ret;
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 
398 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
399 				  struct sta_info *sta,
400 				  bool pairwise,
401 				  struct ieee80211_key *old,
402 				  struct ieee80211_key *new)
403 {
404 	int idx;
405 	int ret;
406 	bool defunikey, defmultikey, defmgmtkey;
407 
408 	/* caller must provide at least one old/new */
409 	if (WARN_ON(!new && !old))
410 		return 0;
411 
412 	if (new)
413 		list_add_tail_rcu(&new->list, &sdata->key_list);
414 
415 	WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
416 
417 	if (old) {
418 		idx = old->conf.keyidx;
419 		ret = ieee80211_hw_key_replace(old, new, pairwise);
420 	} else {
421 		/* new must be provided in case old is not */
422 		idx = new->conf.keyidx;
423 		if (!new->local->wowlan)
424 			ret = ieee80211_key_enable_hw_accel(new);
425 		else
426 			ret = 0;
427 	}
428 
429 	if (ret)
430 		return ret;
431 
432 	if (sta) {
433 		if (pairwise) {
434 			rcu_assign_pointer(sta->ptk[idx], new);
435 			if (new &&
436 			    !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
437 				sta->ptk_idx = idx;
438 				clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
439 				ieee80211_check_fast_xmit(sta);
440 			}
441 		} else {
442 			rcu_assign_pointer(sta->gtk[idx], new);
443 		}
444 		/* Only needed for transition from no key -> key.
445 		 * Still triggers unnecessary when using Extended Key ID
446 		 * and installing the second key ID the first time.
447 		 */
448 		if (new && !old)
449 			ieee80211_check_fast_rx(sta);
450 	} else {
451 		defunikey = old &&
452 			old == key_mtx_dereference(sdata->local,
453 						sdata->default_unicast_key);
454 		defmultikey = old &&
455 			old == key_mtx_dereference(sdata->local,
456 						sdata->default_multicast_key);
457 		defmgmtkey = old &&
458 			old == key_mtx_dereference(sdata->local,
459 						sdata->default_mgmt_key);
460 
461 		if (defunikey && !new)
462 			__ieee80211_set_default_key(sdata, -1, true, false);
463 		if (defmultikey && !new)
464 			__ieee80211_set_default_key(sdata, -1, false, true);
465 		if (defmgmtkey && !new)
466 			__ieee80211_set_default_mgmt_key(sdata, -1);
467 
468 		rcu_assign_pointer(sdata->keys[idx], new);
469 		if (defunikey && new)
470 			__ieee80211_set_default_key(sdata, new->conf.keyidx,
471 						    true, false);
472 		if (defmultikey && new)
473 			__ieee80211_set_default_key(sdata, new->conf.keyidx,
474 						    false, true);
475 		if (defmgmtkey && new)
476 			__ieee80211_set_default_mgmt_key(sdata,
477 							 new->conf.keyidx);
478 	}
479 
480 	if (old)
481 		list_del_rcu(&old->list);
482 
483 	return 0;
484 }
485 
486 struct ieee80211_key *
487 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
488 		    const u8 *key_data,
489 		    size_t seq_len, const u8 *seq,
490 		    const struct ieee80211_cipher_scheme *cs)
491 {
492 	struct ieee80211_key *key;
493 	int i, j, err;
494 
495 	if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
496 		return ERR_PTR(-EINVAL);
497 
498 	key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
499 	if (!key)
500 		return ERR_PTR(-ENOMEM);
501 
502 	/*
503 	 * Default to software encryption; we'll later upload the
504 	 * key to the hardware if possible.
505 	 */
506 	key->conf.flags = 0;
507 	key->flags = 0;
508 
509 	key->conf.cipher = cipher;
510 	key->conf.keyidx = idx;
511 	key->conf.keylen = key_len;
512 	switch (cipher) {
513 	case WLAN_CIPHER_SUITE_WEP40:
514 	case WLAN_CIPHER_SUITE_WEP104:
515 		key->conf.iv_len = IEEE80211_WEP_IV_LEN;
516 		key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
517 		break;
518 	case WLAN_CIPHER_SUITE_TKIP:
519 		key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
520 		key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
521 		if (seq) {
522 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
523 				key->u.tkip.rx[i].iv32 =
524 					get_unaligned_le32(&seq[2]);
525 				key->u.tkip.rx[i].iv16 =
526 					get_unaligned_le16(seq);
527 			}
528 		}
529 		spin_lock_init(&key->u.tkip.txlock);
530 		break;
531 	case WLAN_CIPHER_SUITE_CCMP:
532 		key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
533 		key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
534 		if (seq) {
535 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
536 				for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
537 					key->u.ccmp.rx_pn[i][j] =
538 						seq[IEEE80211_CCMP_PN_LEN - j - 1];
539 		}
540 		/*
541 		 * Initialize AES key state here as an optimization so that
542 		 * it does not need to be initialized for every packet.
543 		 */
544 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
545 			key_data, key_len, IEEE80211_CCMP_MIC_LEN);
546 		if (IS_ERR(key->u.ccmp.tfm)) {
547 			err = PTR_ERR(key->u.ccmp.tfm);
548 			kfree(key);
549 			return ERR_PTR(err);
550 		}
551 		break;
552 	case WLAN_CIPHER_SUITE_CCMP_256:
553 		key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
554 		key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
555 		for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
556 			for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
557 				key->u.ccmp.rx_pn[i][j] =
558 					seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
559 		/* Initialize AES key state here as an optimization so that
560 		 * it does not need to be initialized for every packet.
561 		 */
562 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
563 			key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
564 		if (IS_ERR(key->u.ccmp.tfm)) {
565 			err = PTR_ERR(key->u.ccmp.tfm);
566 			kfree(key);
567 			return ERR_PTR(err);
568 		}
569 		break;
570 	case WLAN_CIPHER_SUITE_AES_CMAC:
571 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
572 		key->conf.iv_len = 0;
573 		if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
574 			key->conf.icv_len = sizeof(struct ieee80211_mmie);
575 		else
576 			key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
577 		if (seq)
578 			for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
579 				key->u.aes_cmac.rx_pn[j] =
580 					seq[IEEE80211_CMAC_PN_LEN - j - 1];
581 		/*
582 		 * Initialize AES key state here as an optimization so that
583 		 * it does not need to be initialized for every packet.
584 		 */
585 		key->u.aes_cmac.tfm =
586 			ieee80211_aes_cmac_key_setup(key_data, key_len);
587 		if (IS_ERR(key->u.aes_cmac.tfm)) {
588 			err = PTR_ERR(key->u.aes_cmac.tfm);
589 			kfree(key);
590 			return ERR_PTR(err);
591 		}
592 		break;
593 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
594 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
595 		key->conf.iv_len = 0;
596 		key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
597 		if (seq)
598 			for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
599 				key->u.aes_gmac.rx_pn[j] =
600 					seq[IEEE80211_GMAC_PN_LEN - j - 1];
601 		/* Initialize AES key state here as an optimization so that
602 		 * it does not need to be initialized for every packet.
603 		 */
604 		key->u.aes_gmac.tfm =
605 			ieee80211_aes_gmac_key_setup(key_data, key_len);
606 		if (IS_ERR(key->u.aes_gmac.tfm)) {
607 			err = PTR_ERR(key->u.aes_gmac.tfm);
608 			kfree(key);
609 			return ERR_PTR(err);
610 		}
611 		break;
612 	case WLAN_CIPHER_SUITE_GCMP:
613 	case WLAN_CIPHER_SUITE_GCMP_256:
614 		key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
615 		key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
616 		for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
617 			for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
618 				key->u.gcmp.rx_pn[i][j] =
619 					seq[IEEE80211_GCMP_PN_LEN - j - 1];
620 		/* Initialize AES key state here as an optimization so that
621 		 * it does not need to be initialized for every packet.
622 		 */
623 		key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
624 								      key_len);
625 		if (IS_ERR(key->u.gcmp.tfm)) {
626 			err = PTR_ERR(key->u.gcmp.tfm);
627 			kfree(key);
628 			return ERR_PTR(err);
629 		}
630 		break;
631 	default:
632 		if (cs) {
633 			if (seq_len && seq_len != cs->pn_len) {
634 				kfree(key);
635 				return ERR_PTR(-EINVAL);
636 			}
637 
638 			key->conf.iv_len = cs->hdr_len;
639 			key->conf.icv_len = cs->mic_len;
640 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
641 				for (j = 0; j < seq_len; j++)
642 					key->u.gen.rx_pn[i][j] =
643 							seq[seq_len - j - 1];
644 			key->flags |= KEY_FLAG_CIPHER_SCHEME;
645 		}
646 	}
647 	memcpy(key->conf.key, key_data, key_len);
648 	INIT_LIST_HEAD(&key->list);
649 
650 	return key;
651 }
652 
653 static void ieee80211_key_free_common(struct ieee80211_key *key)
654 {
655 	switch (key->conf.cipher) {
656 	case WLAN_CIPHER_SUITE_CCMP:
657 	case WLAN_CIPHER_SUITE_CCMP_256:
658 		ieee80211_aes_key_free(key->u.ccmp.tfm);
659 		break;
660 	case WLAN_CIPHER_SUITE_AES_CMAC:
661 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
662 		ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
663 		break;
664 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
665 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
666 		ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
667 		break;
668 	case WLAN_CIPHER_SUITE_GCMP:
669 	case WLAN_CIPHER_SUITE_GCMP_256:
670 		ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
671 		break;
672 	}
673 	kzfree(key);
674 }
675 
676 static void __ieee80211_key_destroy(struct ieee80211_key *key,
677 				    bool delay_tailroom)
678 {
679 	if (key->local) {
680 		struct ieee80211_sub_if_data *sdata = key->sdata;
681 
682 		ieee80211_debugfs_key_remove(key);
683 
684 		if (delay_tailroom) {
685 			/* see ieee80211_delayed_tailroom_dec */
686 			sdata->crypto_tx_tailroom_pending_dec++;
687 			schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
688 					      HZ/2);
689 		} else {
690 			decrease_tailroom_need_count(sdata, 1);
691 		}
692 	}
693 
694 	ieee80211_key_free_common(key);
695 }
696 
697 static void ieee80211_key_destroy(struct ieee80211_key *key,
698 				  bool delay_tailroom)
699 {
700 	if (!key)
701 		return;
702 
703 	/*
704 	 * Synchronize so the TX path and rcu key iterators
705 	 * can no longer be using this key before we free/remove it.
706 	 */
707 	synchronize_net();
708 
709 	__ieee80211_key_destroy(key, delay_tailroom);
710 }
711 
712 void ieee80211_key_free_unused(struct ieee80211_key *key)
713 {
714 	WARN_ON(key->sdata || key->local);
715 	ieee80211_key_free_common(key);
716 }
717 
718 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
719 				    struct ieee80211_key *old,
720 				    struct ieee80211_key *new)
721 {
722 	u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
723 	u8 *tk_old, *tk_new;
724 
725 	if (!old || new->conf.keylen != old->conf.keylen)
726 		return false;
727 
728 	tk_old = old->conf.key;
729 	tk_new = new->conf.key;
730 
731 	/*
732 	 * In station mode, don't compare the TX MIC key, as it's never used
733 	 * and offloaded rekeying may not care to send it to the host. This
734 	 * is the case in iwlwifi, for example.
735 	 */
736 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
737 	    new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
738 	    new->conf.keylen == WLAN_KEY_LEN_TKIP &&
739 	    !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
740 		memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
741 		memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
742 		memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
743 		memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
744 		tk_old = tkip_old;
745 		tk_new = tkip_new;
746 	}
747 
748 	return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
749 }
750 
751 int ieee80211_key_link(struct ieee80211_key *key,
752 		       struct ieee80211_sub_if_data *sdata,
753 		       struct sta_info *sta)
754 {
755 	struct ieee80211_key *old_key;
756 	int idx = key->conf.keyidx;
757 	bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
758 	/*
759 	 * We want to delay tailroom updates only for station - in that
760 	 * case it helps roaming speed, but in other cases it hurts and
761 	 * can cause warnings to appear.
762 	 */
763 	bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
764 	int ret = -EOPNOTSUPP;
765 
766 	mutex_lock(&sdata->local->key_mtx);
767 
768 	if (sta && pairwise) {
769 		struct ieee80211_key *alt_key;
770 
771 		old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
772 		alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
773 
774 		/* The rekey code assumes that the old and new key are using
775 		 * the same cipher. Enforce the assumption for pairwise keys.
776 		 */
777 		if (key &&
778 		    ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
779 		     (old_key && old_key->conf.cipher != key->conf.cipher)))
780 			goto out;
781 	} else if (sta) {
782 		old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
783 	} else {
784 		old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
785 	}
786 
787 	/* Non-pairwise keys must also not switch the cipher on rekey */
788 	if (!pairwise) {
789 		if (key && old_key && old_key->conf.cipher != key->conf.cipher)
790 			goto out;
791 	}
792 
793 	/*
794 	 * Silently accept key re-installation without really installing the
795 	 * new version of the key to avoid nonce reuse or replay issues.
796 	 */
797 	if (ieee80211_key_identical(sdata, old_key, key)) {
798 		ieee80211_key_free_unused(key);
799 		ret = 0;
800 		goto out;
801 	}
802 
803 	key->local = sdata->local;
804 	key->sdata = sdata;
805 	key->sta = sta;
806 
807 	increment_tailroom_need_count(sdata);
808 
809 	ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
810 
811 	if (!ret) {
812 		ieee80211_debugfs_key_add(key);
813 		ieee80211_key_destroy(old_key, delay_tailroom);
814 	} else {
815 		ieee80211_key_free(key, delay_tailroom);
816 	}
817 
818  out:
819 	mutex_unlock(&sdata->local->key_mtx);
820 
821 	return ret;
822 }
823 
824 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
825 {
826 	if (!key)
827 		return;
828 
829 	/*
830 	 * Replace key with nothingness if it was ever used.
831 	 */
832 	if (key->sdata)
833 		ieee80211_key_replace(key->sdata, key->sta,
834 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
835 				key, NULL);
836 	ieee80211_key_destroy(key, delay_tailroom);
837 }
838 
839 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
840 {
841 	struct ieee80211_key *key;
842 	struct ieee80211_sub_if_data *vlan;
843 
844 	ASSERT_RTNL();
845 
846 	if (WARN_ON(!ieee80211_sdata_running(sdata)))
847 		return;
848 
849 	mutex_lock(&sdata->local->key_mtx);
850 
851 	WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
852 		     sdata->crypto_tx_tailroom_pending_dec);
853 
854 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
855 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
856 			WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
857 				     vlan->crypto_tx_tailroom_pending_dec);
858 	}
859 
860 	list_for_each_entry(key, &sdata->key_list, list) {
861 		increment_tailroom_need_count(sdata);
862 		ieee80211_key_enable_hw_accel(key);
863 	}
864 
865 	mutex_unlock(&sdata->local->key_mtx);
866 }
867 
868 void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
869 {
870 	struct ieee80211_sub_if_data *vlan;
871 
872 	mutex_lock(&sdata->local->key_mtx);
873 
874 	sdata->crypto_tx_tailroom_needed_cnt = 0;
875 
876 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
877 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
878 			vlan->crypto_tx_tailroom_needed_cnt = 0;
879 	}
880 
881 	mutex_unlock(&sdata->local->key_mtx);
882 }
883 
884 void ieee80211_iter_keys(struct ieee80211_hw *hw,
885 			 struct ieee80211_vif *vif,
886 			 void (*iter)(struct ieee80211_hw *hw,
887 				      struct ieee80211_vif *vif,
888 				      struct ieee80211_sta *sta,
889 				      struct ieee80211_key_conf *key,
890 				      void *data),
891 			 void *iter_data)
892 {
893 	struct ieee80211_local *local = hw_to_local(hw);
894 	struct ieee80211_key *key, *tmp;
895 	struct ieee80211_sub_if_data *sdata;
896 
897 	ASSERT_RTNL();
898 
899 	mutex_lock(&local->key_mtx);
900 	if (vif) {
901 		sdata = vif_to_sdata(vif);
902 		list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
903 			iter(hw, &sdata->vif,
904 			     key->sta ? &key->sta->sta : NULL,
905 			     &key->conf, iter_data);
906 	} else {
907 		list_for_each_entry(sdata, &local->interfaces, list)
908 			list_for_each_entry_safe(key, tmp,
909 						 &sdata->key_list, list)
910 				iter(hw, &sdata->vif,
911 				     key->sta ? &key->sta->sta : NULL,
912 				     &key->conf, iter_data);
913 	}
914 	mutex_unlock(&local->key_mtx);
915 }
916 EXPORT_SYMBOL(ieee80211_iter_keys);
917 
918 static void
919 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
920 			 struct ieee80211_sub_if_data *sdata,
921 			 void (*iter)(struct ieee80211_hw *hw,
922 				      struct ieee80211_vif *vif,
923 				      struct ieee80211_sta *sta,
924 				      struct ieee80211_key_conf *key,
925 				      void *data),
926 			 void *iter_data)
927 {
928 	struct ieee80211_key *key;
929 
930 	list_for_each_entry_rcu(key, &sdata->key_list, list) {
931 		/* skip keys of station in removal process */
932 		if (key->sta && key->sta->removed)
933 			continue;
934 		if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
935 			continue;
936 
937 		iter(hw, &sdata->vif,
938 		     key->sta ? &key->sta->sta : NULL,
939 		     &key->conf, iter_data);
940 	}
941 }
942 
943 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
944 			     struct ieee80211_vif *vif,
945 			     void (*iter)(struct ieee80211_hw *hw,
946 					  struct ieee80211_vif *vif,
947 					  struct ieee80211_sta *sta,
948 					  struct ieee80211_key_conf *key,
949 					  void *data),
950 			     void *iter_data)
951 {
952 	struct ieee80211_local *local = hw_to_local(hw);
953 	struct ieee80211_sub_if_data *sdata;
954 
955 	if (vif) {
956 		sdata = vif_to_sdata(vif);
957 		_ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
958 	} else {
959 		list_for_each_entry_rcu(sdata, &local->interfaces, list)
960 			_ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
961 	}
962 }
963 EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
964 
965 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
966 				      struct list_head *keys)
967 {
968 	struct ieee80211_key *key, *tmp;
969 
970 	decrease_tailroom_need_count(sdata,
971 				     sdata->crypto_tx_tailroom_pending_dec);
972 	sdata->crypto_tx_tailroom_pending_dec = 0;
973 
974 	ieee80211_debugfs_key_remove_mgmt_default(sdata);
975 
976 	list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
977 		ieee80211_key_replace(key->sdata, key->sta,
978 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
979 				key, NULL);
980 		list_add_tail(&key->list, keys);
981 	}
982 
983 	ieee80211_debugfs_key_update_default(sdata);
984 }
985 
986 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
987 			 bool force_synchronize)
988 {
989 	struct ieee80211_local *local = sdata->local;
990 	struct ieee80211_sub_if_data *vlan;
991 	struct ieee80211_sub_if_data *master;
992 	struct ieee80211_key *key, *tmp;
993 	LIST_HEAD(keys);
994 
995 	cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
996 
997 	mutex_lock(&local->key_mtx);
998 
999 	ieee80211_free_keys_iface(sdata, &keys);
1000 
1001 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
1002 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1003 			ieee80211_free_keys_iface(vlan, &keys);
1004 	}
1005 
1006 	if (!list_empty(&keys) || force_synchronize)
1007 		synchronize_net();
1008 	list_for_each_entry_safe(key, tmp, &keys, list)
1009 		__ieee80211_key_destroy(key, false);
1010 
1011 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1012 		if (sdata->bss) {
1013 			master = container_of(sdata->bss,
1014 					      struct ieee80211_sub_if_data,
1015 					      u.ap);
1016 
1017 			WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1018 				     master->crypto_tx_tailroom_needed_cnt);
1019 		}
1020 	} else {
1021 		WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1022 			     sdata->crypto_tx_tailroom_pending_dec);
1023 	}
1024 
1025 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
1026 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1027 			WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1028 				     vlan->crypto_tx_tailroom_pending_dec);
1029 	}
1030 
1031 	mutex_unlock(&local->key_mtx);
1032 }
1033 
1034 void ieee80211_free_sta_keys(struct ieee80211_local *local,
1035 			     struct sta_info *sta)
1036 {
1037 	struct ieee80211_key *key;
1038 	int i;
1039 
1040 	mutex_lock(&local->key_mtx);
1041 	for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
1042 		key = key_mtx_dereference(local, sta->gtk[i]);
1043 		if (!key)
1044 			continue;
1045 		ieee80211_key_replace(key->sdata, key->sta,
1046 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1047 				key, NULL);
1048 		__ieee80211_key_destroy(key, key->sdata->vif.type ==
1049 					NL80211_IFTYPE_STATION);
1050 	}
1051 
1052 	for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1053 		key = key_mtx_dereference(local, sta->ptk[i]);
1054 		if (!key)
1055 			continue;
1056 		ieee80211_key_replace(key->sdata, key->sta,
1057 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1058 				key, NULL);
1059 		__ieee80211_key_destroy(key, key->sdata->vif.type ==
1060 					NL80211_IFTYPE_STATION);
1061 	}
1062 
1063 	mutex_unlock(&local->key_mtx);
1064 }
1065 
1066 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1067 {
1068 	struct ieee80211_sub_if_data *sdata;
1069 
1070 	sdata = container_of(wk, struct ieee80211_sub_if_data,
1071 			     dec_tailroom_needed_wk.work);
1072 
1073 	/*
1074 	 * The reason for the delayed tailroom needed decrementing is to
1075 	 * make roaming faster: during roaming, all keys are first deleted
1076 	 * and then new keys are installed. The first new key causes the
1077 	 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1078 	 * the cost of synchronize_net() (which can be slow). Avoid this
1079 	 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1080 	 * key removal for a while, so if we roam the value is larger than
1081 	 * zero and no 0->1 transition happens.
1082 	 *
1083 	 * The cost is that if the AP switching was from an AP with keys
1084 	 * to one without, we still allocate tailroom while it would no
1085 	 * longer be needed. However, in the typical (fast) roaming case
1086 	 * within an ESS this usually won't happen.
1087 	 */
1088 
1089 	mutex_lock(&sdata->local->key_mtx);
1090 	decrease_tailroom_need_count(sdata,
1091 				     sdata->crypto_tx_tailroom_pending_dec);
1092 	sdata->crypto_tx_tailroom_pending_dec = 0;
1093 	mutex_unlock(&sdata->local->key_mtx);
1094 }
1095 
1096 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1097 				const u8 *replay_ctr, gfp_t gfp)
1098 {
1099 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1100 
1101 	trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1102 
1103 	cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1104 }
1105 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
1106 
1107 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1108 			      int tid, struct ieee80211_key_seq *seq)
1109 {
1110 	struct ieee80211_key *key;
1111 	const u8 *pn;
1112 
1113 	key = container_of(keyconf, struct ieee80211_key, conf);
1114 
1115 	switch (key->conf.cipher) {
1116 	case WLAN_CIPHER_SUITE_TKIP:
1117 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1118 			return;
1119 		seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1120 		seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1121 		break;
1122 	case WLAN_CIPHER_SUITE_CCMP:
1123 	case WLAN_CIPHER_SUITE_CCMP_256:
1124 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1125 			return;
1126 		if (tid < 0)
1127 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1128 		else
1129 			pn = key->u.ccmp.rx_pn[tid];
1130 		memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
1131 		break;
1132 	case WLAN_CIPHER_SUITE_AES_CMAC:
1133 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1134 		if (WARN_ON(tid != 0))
1135 			return;
1136 		pn = key->u.aes_cmac.rx_pn;
1137 		memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
1138 		break;
1139 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1140 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1141 		if (WARN_ON(tid != 0))
1142 			return;
1143 		pn = key->u.aes_gmac.rx_pn;
1144 		memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1145 		break;
1146 	case WLAN_CIPHER_SUITE_GCMP:
1147 	case WLAN_CIPHER_SUITE_GCMP_256:
1148 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1149 			return;
1150 		if (tid < 0)
1151 			pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1152 		else
1153 			pn = key->u.gcmp.rx_pn[tid];
1154 		memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1155 		break;
1156 	}
1157 }
1158 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
1159 
1160 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1161 			      int tid, struct ieee80211_key_seq *seq)
1162 {
1163 	struct ieee80211_key *key;
1164 	u8 *pn;
1165 
1166 	key = container_of(keyconf, struct ieee80211_key, conf);
1167 
1168 	switch (key->conf.cipher) {
1169 	case WLAN_CIPHER_SUITE_TKIP:
1170 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1171 			return;
1172 		key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1173 		key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1174 		break;
1175 	case WLAN_CIPHER_SUITE_CCMP:
1176 	case WLAN_CIPHER_SUITE_CCMP_256:
1177 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1178 			return;
1179 		if (tid < 0)
1180 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1181 		else
1182 			pn = key->u.ccmp.rx_pn[tid];
1183 		memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1184 		break;
1185 	case WLAN_CIPHER_SUITE_AES_CMAC:
1186 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1187 		if (WARN_ON(tid != 0))
1188 			return;
1189 		pn = key->u.aes_cmac.rx_pn;
1190 		memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1191 		break;
1192 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1193 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1194 		if (WARN_ON(tid != 0))
1195 			return;
1196 		pn = key->u.aes_gmac.rx_pn;
1197 		memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1198 		break;
1199 	case WLAN_CIPHER_SUITE_GCMP:
1200 	case WLAN_CIPHER_SUITE_GCMP_256:
1201 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1202 			return;
1203 		if (tid < 0)
1204 			pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1205 		else
1206 			pn = key->u.gcmp.rx_pn[tid];
1207 		memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1208 		break;
1209 	default:
1210 		WARN_ON(1);
1211 		break;
1212 	}
1213 }
1214 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1215 
1216 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1217 {
1218 	struct ieee80211_key *key;
1219 
1220 	key = container_of(keyconf, struct ieee80211_key, conf);
1221 
1222 	assert_key_lock(key->local);
1223 
1224 	/*
1225 	 * if key was uploaded, we assume the driver will/has remove(d)
1226 	 * it, so adjust bookkeeping accordingly
1227 	 */
1228 	if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1229 		key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1230 
1231 		if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1232 					 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1233 					 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
1234 			increment_tailroom_need_count(key->sdata);
1235 	}
1236 
1237 	ieee80211_key_free(key, false);
1238 }
1239 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1240 
1241 struct ieee80211_key_conf *
1242 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1243 			struct ieee80211_key_conf *keyconf)
1244 {
1245 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1246 	struct ieee80211_local *local = sdata->local;
1247 	struct ieee80211_key *key;
1248 	int err;
1249 
1250 	if (WARN_ON(!local->wowlan))
1251 		return ERR_PTR(-EINVAL);
1252 
1253 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1254 		return ERR_PTR(-EINVAL);
1255 
1256 	key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1257 				  keyconf->keylen, keyconf->key,
1258 				  0, NULL, NULL);
1259 	if (IS_ERR(key))
1260 		return ERR_CAST(key);
1261 
1262 	if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1263 		key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1264 
1265 	err = ieee80211_key_link(key, sdata, NULL);
1266 	if (err)
1267 		return ERR_PTR(err);
1268 
1269 	return &key->conf;
1270 }
1271 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
1272