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