xref: /linux/net/mac80211/key.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
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
update_vlan_tailroom_need_count(struct ieee80211_sub_if_data * sdata,int delta)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 
increment_tailroom_need_count(struct ieee80211_sub_if_data * sdata)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 
decrease_tailroom_need_count(struct ieee80211_sub_if_data * sdata,int delta)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 
ieee80211_key_enable_hw_accel(struct ieee80211_key * key)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 
ieee80211_key_disable_hw_accel(struct ieee80211_key * key)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 
_ieee80211_set_tx_key(struct ieee80211_key * key,bool force)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 
ieee80211_set_tx_key(struct ieee80211_key * key)285 int ieee80211_set_tx_key(struct ieee80211_key *key)
286 {
287 	return _ieee80211_set_tx_key(key, false);
288 }
289 
ieee80211_pairwise_rekey(struct ieee80211_key * old,struct ieee80211_key * new)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 
__ieee80211_set_default_key(struct ieee80211_link_data * link,int idx,bool uni,bool multi)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 
ieee80211_set_default_key(struct ieee80211_link_data * link,int idx,bool uni,bool multi)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
__ieee80211_set_default_mgmt_key(struct ieee80211_link_data * link,int idx)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 
ieee80211_set_default_mgmt_key(struct ieee80211_link_data * link,int idx)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
__ieee80211_set_default_beacon_key(struct ieee80211_link_data * link,int idx)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 
ieee80211_set_default_beacon_key(struct ieee80211_link_data * link,int idx)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 
ieee80211_key_replace(struct ieee80211_sub_if_data * sdata,struct ieee80211_link_data * link,struct sta_info * sta,bool pairwise,struct ieee80211_key * old,struct ieee80211_key * new)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 	}
517 
518 	if (ret)
519 		return ret;
520 
521 	if (new)
522 		list_add_tail_rcu(&new->list, &sdata->key_list);
523 
524 	if (sta) {
525 		if (pairwise) {
526 			rcu_assign_pointer(sta->ptk[idx], new);
527 			if (new &&
528 			    !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX))
529 				_ieee80211_set_tx_key(new, true);
530 		} else {
531 			rcu_assign_pointer(link_sta->gtk[idx], new);
532 		}
533 		/* Only needed for transition from no key -> key.
534 		 * Still triggers unnecessary when using Extended Key ID
535 		 * and installing the second key ID the first time.
536 		 */
537 		if (new && !old)
538 			ieee80211_check_fast_rx(sta);
539 	} else {
540 		defunikey = old &&
541 			old == wiphy_dereference(sdata->local->hw.wiphy,
542 						 sdata->default_unicast_key);
543 		defmultikey = old &&
544 			old == wiphy_dereference(sdata->local->hw.wiphy,
545 						 link->default_multicast_key);
546 		defmgmtkey = old &&
547 			old == wiphy_dereference(sdata->local->hw.wiphy,
548 						 link->default_mgmt_key);
549 		defbeaconkey = old &&
550 			old == wiphy_dereference(sdata->local->hw.wiphy,
551 						 link->default_beacon_key);
552 
553 		if (defunikey && !new)
554 			__ieee80211_set_default_key(link, -1, true, false);
555 		if (defmultikey && !new)
556 			__ieee80211_set_default_key(link, -1, false, true);
557 		if (defmgmtkey && !new)
558 			__ieee80211_set_default_mgmt_key(link, -1);
559 		if (defbeaconkey && !new)
560 			__ieee80211_set_default_beacon_key(link, -1);
561 
562 		if (is_wep || pairwise)
563 			rcu_assign_pointer(sdata->keys[idx], new);
564 		else
565 			rcu_assign_pointer(link->gtk[idx], new);
566 
567 		if (defunikey && new)
568 			__ieee80211_set_default_key(link, new->conf.keyidx,
569 						    true, false);
570 		if (defmultikey && new)
571 			__ieee80211_set_default_key(link, new->conf.keyidx,
572 						    false, true);
573 		if (defmgmtkey && new)
574 			__ieee80211_set_default_mgmt_key(link,
575 							 new->conf.keyidx);
576 		if (defbeaconkey && new)
577 			__ieee80211_set_default_beacon_key(link,
578 							   new->conf.keyidx);
579 	}
580 
581 	if (old)
582 		list_del_rcu(&old->list);
583 
584 	return 0;
585 }
586 
587 struct ieee80211_key *
ieee80211_key_alloc(u32 cipher,int idx,size_t key_len,const u8 * key_data,size_t seq_len,const u8 * seq)588 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
589 		    const u8 *key_data,
590 		    size_t seq_len, const u8 *seq)
591 {
592 	struct ieee80211_key *key;
593 	int i, j, err;
594 
595 	if (WARN_ON(idx < 0 ||
596 		    idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
597 		    NUM_DEFAULT_BEACON_KEYS))
598 		return ERR_PTR(-EINVAL);
599 
600 	key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
601 	if (!key)
602 		return ERR_PTR(-ENOMEM);
603 
604 	/*
605 	 * Default to software encryption; we'll later upload the
606 	 * key to the hardware if possible.
607 	 */
608 	key->conf.flags = 0;
609 	key->flags = 0;
610 
611 	key->conf.link_id = -1;
612 	key->conf.cipher = cipher;
613 	key->conf.keyidx = idx;
614 	key->conf.keylen = key_len;
615 	switch (cipher) {
616 	case WLAN_CIPHER_SUITE_WEP40:
617 	case WLAN_CIPHER_SUITE_WEP104:
618 		key->conf.iv_len = IEEE80211_WEP_IV_LEN;
619 		key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
620 		break;
621 	case WLAN_CIPHER_SUITE_TKIP:
622 		key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
623 		key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
624 		if (seq) {
625 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
626 				key->u.tkip.rx[i].iv32 =
627 					get_unaligned_le32(&seq[2]);
628 				key->u.tkip.rx[i].iv16 =
629 					get_unaligned_le16(seq);
630 			}
631 		}
632 		spin_lock_init(&key->u.tkip.txlock);
633 		break;
634 	case WLAN_CIPHER_SUITE_CCMP:
635 		key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
636 		key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
637 		if (seq) {
638 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
639 				for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
640 					key->u.ccmp.rx_pn[i][j] =
641 						seq[IEEE80211_CCMP_PN_LEN - j - 1];
642 		}
643 		/*
644 		 * Initialize AES key state here as an optimization so that
645 		 * it does not need to be initialized for every packet.
646 		 */
647 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
648 			key_data, key_len, IEEE80211_CCMP_MIC_LEN);
649 		if (IS_ERR(key->u.ccmp.tfm)) {
650 			err = PTR_ERR(key->u.ccmp.tfm);
651 			kfree(key);
652 			return ERR_PTR(err);
653 		}
654 		break;
655 	case WLAN_CIPHER_SUITE_CCMP_256:
656 		key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
657 		key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
658 		for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
659 			for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
660 				key->u.ccmp.rx_pn[i][j] =
661 					seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
662 		/* Initialize AES key state here as an optimization so that
663 		 * it does not need to be initialized for every packet.
664 		 */
665 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
666 			key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
667 		if (IS_ERR(key->u.ccmp.tfm)) {
668 			err = PTR_ERR(key->u.ccmp.tfm);
669 			kfree(key);
670 			return ERR_PTR(err);
671 		}
672 		break;
673 	case WLAN_CIPHER_SUITE_AES_CMAC:
674 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
675 		key->conf.iv_len = 0;
676 		if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
677 			key->conf.icv_len = sizeof(struct ieee80211_mmie);
678 		else
679 			key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
680 		if (seq)
681 			for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
682 				key->u.aes_cmac.rx_pn[j] =
683 					seq[IEEE80211_CMAC_PN_LEN - j - 1];
684 		/*
685 		 * Initialize AES key state here as an optimization so that
686 		 * it does not need to be initialized for every packet.
687 		 */
688 		key->u.aes_cmac.tfm =
689 			ieee80211_aes_cmac_key_setup(key_data, key_len);
690 		if (IS_ERR(key->u.aes_cmac.tfm)) {
691 			err = PTR_ERR(key->u.aes_cmac.tfm);
692 			kfree(key);
693 			return ERR_PTR(err);
694 		}
695 		break;
696 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
697 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
698 		key->conf.iv_len = 0;
699 		key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
700 		if (seq)
701 			for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
702 				key->u.aes_gmac.rx_pn[j] =
703 					seq[IEEE80211_GMAC_PN_LEN - j - 1];
704 		/* Initialize AES key state here as an optimization so that
705 		 * it does not need to be initialized for every packet.
706 		 */
707 		key->u.aes_gmac.tfm =
708 			ieee80211_aes_gmac_key_setup(key_data, key_len);
709 		if (IS_ERR(key->u.aes_gmac.tfm)) {
710 			err = PTR_ERR(key->u.aes_gmac.tfm);
711 			kfree(key);
712 			return ERR_PTR(err);
713 		}
714 		break;
715 	case WLAN_CIPHER_SUITE_GCMP:
716 	case WLAN_CIPHER_SUITE_GCMP_256:
717 		key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
718 		key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
719 		for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
720 			for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
721 				key->u.gcmp.rx_pn[i][j] =
722 					seq[IEEE80211_GCMP_PN_LEN - j - 1];
723 		/* Initialize AES key state here as an optimization so that
724 		 * it does not need to be initialized for every packet.
725 		 */
726 		key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
727 								      key_len);
728 		if (IS_ERR(key->u.gcmp.tfm)) {
729 			err = PTR_ERR(key->u.gcmp.tfm);
730 			kfree(key);
731 			return ERR_PTR(err);
732 		}
733 		break;
734 	}
735 	memcpy(key->conf.key, key_data, key_len);
736 	INIT_LIST_HEAD(&key->list);
737 
738 	return key;
739 }
740 
ieee80211_key_free_common(struct ieee80211_key * key)741 static void ieee80211_key_free_common(struct ieee80211_key *key)
742 {
743 	switch (key->conf.cipher) {
744 	case WLAN_CIPHER_SUITE_CCMP:
745 	case WLAN_CIPHER_SUITE_CCMP_256:
746 		ieee80211_aes_key_free(key->u.ccmp.tfm);
747 		break;
748 	case WLAN_CIPHER_SUITE_AES_CMAC:
749 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
750 		ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
751 		break;
752 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
753 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
754 		ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
755 		break;
756 	case WLAN_CIPHER_SUITE_GCMP:
757 	case WLAN_CIPHER_SUITE_GCMP_256:
758 		ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
759 		break;
760 	}
761 	kfree_sensitive(key);
762 }
763 
__ieee80211_key_destroy(struct ieee80211_key * key,bool delay_tailroom)764 static void __ieee80211_key_destroy(struct ieee80211_key *key,
765 				    bool delay_tailroom)
766 {
767 	if (key->local) {
768 		struct ieee80211_sub_if_data *sdata = key->sdata;
769 
770 		ieee80211_debugfs_key_remove(key);
771 
772 		if (delay_tailroom) {
773 			/* see ieee80211_delayed_tailroom_dec */
774 			sdata->crypto_tx_tailroom_pending_dec++;
775 			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
776 						 &sdata->dec_tailroom_needed_wk,
777 						 HZ / 2);
778 		} else {
779 			decrease_tailroom_need_count(sdata, 1);
780 		}
781 	}
782 
783 	ieee80211_key_free_common(key);
784 }
785 
ieee80211_key_destroy(struct ieee80211_key * key,bool delay_tailroom)786 static void ieee80211_key_destroy(struct ieee80211_key *key,
787 				  bool delay_tailroom)
788 {
789 	if (!key)
790 		return;
791 
792 	/*
793 	 * Synchronize so the TX path and rcu key iterators
794 	 * can no longer be using this key before we free/remove it.
795 	 */
796 	synchronize_net();
797 
798 	__ieee80211_key_destroy(key, delay_tailroom);
799 }
800 
ieee80211_key_free_unused(struct ieee80211_key * key)801 void ieee80211_key_free_unused(struct ieee80211_key *key)
802 {
803 	if (!key)
804 		return;
805 
806 	WARN_ON(key->sdata || key->local);
807 	ieee80211_key_free_common(key);
808 }
809 
ieee80211_key_identical(struct ieee80211_sub_if_data * sdata,struct ieee80211_key * old,struct ieee80211_key * new)810 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
811 				    struct ieee80211_key *old,
812 				    struct ieee80211_key *new)
813 {
814 	u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
815 	u8 *tk_old, *tk_new;
816 
817 	if (!old || new->conf.keylen != old->conf.keylen)
818 		return false;
819 
820 	tk_old = old->conf.key;
821 	tk_new = new->conf.key;
822 
823 	/*
824 	 * In station mode, don't compare the TX MIC key, as it's never used
825 	 * and offloaded rekeying may not care to send it to the host. This
826 	 * is the case in iwlwifi, for example.
827 	 */
828 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
829 	    new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
830 	    new->conf.keylen == WLAN_KEY_LEN_TKIP &&
831 	    !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
832 		memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
833 		memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
834 		memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
835 		memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
836 		tk_old = tkip_old;
837 		tk_new = tkip_new;
838 	}
839 
840 	return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
841 }
842 
ieee80211_key_link(struct ieee80211_key * key,struct ieee80211_link_data * link,struct sta_info * sta)843 int ieee80211_key_link(struct ieee80211_key *key,
844 		       struct ieee80211_link_data *link,
845 		       struct sta_info *sta)
846 {
847 	struct ieee80211_sub_if_data *sdata = link->sdata;
848 	static atomic_t key_color = ATOMIC_INIT(0);
849 	struct ieee80211_key *old_key = NULL;
850 	int idx = key->conf.keyidx;
851 	bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
852 	/*
853 	 * We want to delay tailroom updates only for station - in that
854 	 * case it helps roaming speed, but in other cases it hurts and
855 	 * can cause warnings to appear.
856 	 */
857 	bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
858 	int ret;
859 
860 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
861 
862 	if (sta && pairwise) {
863 		struct ieee80211_key *alt_key;
864 
865 		old_key = wiphy_dereference(sdata->local->hw.wiphy,
866 					    sta->ptk[idx]);
867 		alt_key = wiphy_dereference(sdata->local->hw.wiphy,
868 					    sta->ptk[idx ^ 1]);
869 
870 		/* The rekey code assumes that the old and new key are using
871 		 * the same cipher. Enforce the assumption for pairwise keys.
872 		 */
873 		if ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
874 		    (old_key && old_key->conf.cipher != key->conf.cipher)) {
875 			ret = -EOPNOTSUPP;
876 			goto out;
877 		}
878 	} else if (sta) {
879 		struct link_sta_info *link_sta = &sta->deflink;
880 		int link_id = key->conf.link_id;
881 
882 		if (link_id >= 0) {
883 			link_sta = rcu_dereference_protected(sta->link[link_id],
884 							     lockdep_is_held(&sta->local->hw.wiphy->mtx));
885 			if (!link_sta) {
886 				ret = -ENOLINK;
887 				goto out;
888 			}
889 		}
890 
891 		old_key = wiphy_dereference(sdata->local->hw.wiphy,
892 					    link_sta->gtk[idx]);
893 	} else {
894 		if (idx < NUM_DEFAULT_KEYS)
895 			old_key = wiphy_dereference(sdata->local->hw.wiphy,
896 						    sdata->keys[idx]);
897 		if (!old_key)
898 			old_key = wiphy_dereference(sdata->local->hw.wiphy,
899 						    link->gtk[idx]);
900 	}
901 
902 	/* Non-pairwise keys must also not switch the cipher on rekey */
903 	if (!pairwise) {
904 		if (old_key && old_key->conf.cipher != key->conf.cipher) {
905 			ret = -EOPNOTSUPP;
906 			goto out;
907 		}
908 	}
909 
910 	/*
911 	 * Silently accept key re-installation without really installing the
912 	 * new version of the key to avoid nonce reuse or replay issues.
913 	 */
914 	if (ieee80211_key_identical(sdata, old_key, key)) {
915 		ret = -EALREADY;
916 		goto out;
917 	}
918 
919 	key->local = sdata->local;
920 	key->sdata = sdata;
921 	key->sta = sta;
922 
923 	/*
924 	 * Assign a unique ID to every key so we can easily prevent mixed
925 	 * key and fragment cache attacks.
926 	 */
927 	key->color = atomic_inc_return(&key_color);
928 
929 	/* keep this flag for easier access later */
930 	if (sta && sta->sta.spp_amsdu)
931 		key->conf.flags |= IEEE80211_KEY_FLAG_SPP_AMSDU;
932 
933 	increment_tailroom_need_count(sdata);
934 
935 	ret = ieee80211_key_replace(sdata, link, sta, pairwise, old_key, key);
936 
937 	if (!ret) {
938 		ieee80211_debugfs_key_add(key);
939 		ieee80211_key_destroy(old_key, delay_tailroom);
940 	} else {
941 		ieee80211_key_free(key, delay_tailroom);
942 	}
943 
944 	key = NULL;
945 
946  out:
947 	ieee80211_key_free_unused(key);
948 	return ret;
949 }
950 
ieee80211_key_free(struct ieee80211_key * key,bool delay_tailroom)951 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
952 {
953 	if (!key)
954 		return;
955 
956 	/*
957 	 * Replace key with nothingness if it was ever used.
958 	 */
959 	if (key->sdata)
960 		ieee80211_key_replace(key->sdata, NULL, key->sta,
961 				      key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
962 				      key, NULL);
963 	ieee80211_key_destroy(key, delay_tailroom);
964 }
965 
ieee80211_reenable_keys(struct ieee80211_sub_if_data * sdata)966 void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata)
967 {
968 	struct ieee80211_key *key;
969 	struct ieee80211_sub_if_data *vlan;
970 
971 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
972 
973 	sdata->crypto_tx_tailroom_needed_cnt = 0;
974 	sdata->crypto_tx_tailroom_pending_dec = 0;
975 
976 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
977 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
978 			vlan->crypto_tx_tailroom_needed_cnt = 0;
979 			vlan->crypto_tx_tailroom_pending_dec = 0;
980 		}
981 	}
982 
983 	if (ieee80211_sdata_running(sdata)) {
984 		list_for_each_entry(key, &sdata->key_list, list) {
985 			increment_tailroom_need_count(sdata);
986 			ieee80211_key_enable_hw_accel(key);
987 		}
988 	}
989 }
990 
991 static void
ieee80211_key_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_key * key,void (* iter)(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data),void * iter_data)992 ieee80211_key_iter(struct ieee80211_hw *hw,
993 		   struct ieee80211_vif *vif,
994 		   struct ieee80211_key *key,
995 		   void (*iter)(struct ieee80211_hw *hw,
996 				struct ieee80211_vif *vif,
997 				struct ieee80211_sta *sta,
998 				struct ieee80211_key_conf *key,
999 				void *data),
1000 		   void *iter_data)
1001 {
1002 	/* skip keys of station in removal process */
1003 	if (key->sta && key->sta->removed)
1004 		return;
1005 	if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
1006 		return;
1007 	iter(hw, vif, key->sta ? &key->sta->sta : NULL,
1008 	     &key->conf, iter_data);
1009 }
1010 
ieee80211_iter_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,void (* iter)(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data),void * iter_data)1011 void ieee80211_iter_keys(struct ieee80211_hw *hw,
1012 			 struct ieee80211_vif *vif,
1013 			 void (*iter)(struct ieee80211_hw *hw,
1014 				      struct ieee80211_vif *vif,
1015 				      struct ieee80211_sta *sta,
1016 				      struct ieee80211_key_conf *key,
1017 				      void *data),
1018 			 void *iter_data)
1019 {
1020 	struct ieee80211_local *local = hw_to_local(hw);
1021 	struct ieee80211_key *key, *tmp;
1022 	struct ieee80211_sub_if_data *sdata;
1023 
1024 	lockdep_assert_wiphy(hw->wiphy);
1025 
1026 	if (vif) {
1027 		sdata = vif_to_sdata(vif);
1028 		list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
1029 			ieee80211_key_iter(hw, vif, key, iter, iter_data);
1030 	} else {
1031 		list_for_each_entry(sdata, &local->interfaces, list)
1032 			list_for_each_entry_safe(key, tmp,
1033 						 &sdata->key_list, list)
1034 				ieee80211_key_iter(hw, &sdata->vif, key,
1035 						   iter, iter_data);
1036 	}
1037 }
1038 EXPORT_SYMBOL(ieee80211_iter_keys);
1039 
1040 static void
_ieee80211_iter_keys_rcu(struct ieee80211_hw * hw,struct ieee80211_sub_if_data * sdata,void (* iter)(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data),void * iter_data)1041 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
1042 			 struct ieee80211_sub_if_data *sdata,
1043 			 void (*iter)(struct ieee80211_hw *hw,
1044 				      struct ieee80211_vif *vif,
1045 				      struct ieee80211_sta *sta,
1046 				      struct ieee80211_key_conf *key,
1047 				      void *data),
1048 			 void *iter_data)
1049 {
1050 	struct ieee80211_key *key;
1051 
1052 	list_for_each_entry_rcu(key, &sdata->key_list, list)
1053 		ieee80211_key_iter(hw, &sdata->vif, key, iter, iter_data);
1054 }
1055 
ieee80211_iter_keys_rcu(struct ieee80211_hw * hw,struct ieee80211_vif * vif,void (* iter)(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data),void * iter_data)1056 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
1057 			     struct ieee80211_vif *vif,
1058 			     void (*iter)(struct ieee80211_hw *hw,
1059 					  struct ieee80211_vif *vif,
1060 					  struct ieee80211_sta *sta,
1061 					  struct ieee80211_key_conf *key,
1062 					  void *data),
1063 			     void *iter_data)
1064 {
1065 	struct ieee80211_local *local = hw_to_local(hw);
1066 	struct ieee80211_sub_if_data *sdata;
1067 
1068 	if (vif) {
1069 		sdata = vif_to_sdata(vif);
1070 		_ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
1071 	} else {
1072 		list_for_each_entry_rcu(sdata, &local->interfaces, list)
1073 			_ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
1074 	}
1075 }
1076 EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
1077 
ieee80211_free_keys_iface(struct ieee80211_sub_if_data * sdata,struct list_head * keys)1078 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
1079 				      struct list_head *keys)
1080 {
1081 	struct ieee80211_key *key, *tmp;
1082 
1083 	decrease_tailroom_need_count(sdata,
1084 				     sdata->crypto_tx_tailroom_pending_dec);
1085 	sdata->crypto_tx_tailroom_pending_dec = 0;
1086 
1087 	ieee80211_debugfs_key_remove_mgmt_default(sdata);
1088 	ieee80211_debugfs_key_remove_beacon_default(sdata);
1089 
1090 	list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
1091 		ieee80211_key_replace(key->sdata, NULL, key->sta,
1092 				      key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1093 				      key, NULL);
1094 		list_add_tail(&key->list, keys);
1095 	}
1096 
1097 	ieee80211_debugfs_key_update_default(sdata);
1098 }
1099 
ieee80211_remove_link_keys(struct ieee80211_link_data * link,struct list_head * keys)1100 void ieee80211_remove_link_keys(struct ieee80211_link_data *link,
1101 				struct list_head *keys)
1102 {
1103 	struct ieee80211_sub_if_data *sdata = link->sdata;
1104 	struct ieee80211_local *local = sdata->local;
1105 	struct ieee80211_key *key, *tmp;
1106 
1107 	lockdep_assert_wiphy(local->hw.wiphy);
1108 
1109 	list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
1110 		if (key->conf.link_id != link->link_id)
1111 			continue;
1112 		ieee80211_key_replace(key->sdata, link, key->sta,
1113 				      key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1114 				      key, NULL);
1115 		list_add_tail(&key->list, keys);
1116 	}
1117 }
1118 
ieee80211_free_key_list(struct ieee80211_local * local,struct list_head * keys)1119 void ieee80211_free_key_list(struct ieee80211_local *local,
1120 			     struct list_head *keys)
1121 {
1122 	struct ieee80211_key *key, *tmp;
1123 
1124 	lockdep_assert_wiphy(local->hw.wiphy);
1125 
1126 	list_for_each_entry_safe(key, tmp, keys, list)
1127 		__ieee80211_key_destroy(key, false);
1128 }
1129 
ieee80211_free_keys(struct ieee80211_sub_if_data * sdata,bool force_synchronize)1130 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
1131 			 bool force_synchronize)
1132 {
1133 	struct ieee80211_local *local = sdata->local;
1134 	struct ieee80211_sub_if_data *vlan;
1135 	struct ieee80211_sub_if_data *master;
1136 	struct ieee80211_key *key, *tmp;
1137 	LIST_HEAD(keys);
1138 
1139 	wiphy_delayed_work_cancel(local->hw.wiphy,
1140 				  &sdata->dec_tailroom_needed_wk);
1141 
1142 	lockdep_assert_wiphy(local->hw.wiphy);
1143 
1144 	ieee80211_free_keys_iface(sdata, &keys);
1145 
1146 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
1147 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1148 			ieee80211_free_keys_iface(vlan, &keys);
1149 	}
1150 
1151 	if (!list_empty(&keys) || force_synchronize)
1152 		synchronize_net();
1153 	list_for_each_entry_safe(key, tmp, &keys, list)
1154 		__ieee80211_key_destroy(key, false);
1155 
1156 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1157 		if (sdata->bss) {
1158 			master = container_of(sdata->bss,
1159 					      struct ieee80211_sub_if_data,
1160 					      u.ap);
1161 
1162 			WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1163 				     master->crypto_tx_tailroom_needed_cnt);
1164 		}
1165 	} else {
1166 		WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1167 			     sdata->crypto_tx_tailroom_pending_dec);
1168 	}
1169 
1170 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
1171 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1172 			WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1173 				     vlan->crypto_tx_tailroom_pending_dec);
1174 	}
1175 }
1176 
ieee80211_free_sta_keys(struct ieee80211_local * local,struct sta_info * sta)1177 void ieee80211_free_sta_keys(struct ieee80211_local *local,
1178 			     struct sta_info *sta)
1179 {
1180 	struct ieee80211_key *key;
1181 	int i;
1182 
1183 	lockdep_assert_wiphy(local->hw.wiphy);
1184 
1185 	for (i = 0; i < ARRAY_SIZE(sta->deflink.gtk); i++) {
1186 		key = wiphy_dereference(local->hw.wiphy, sta->deflink.gtk[i]);
1187 		if (!key)
1188 			continue;
1189 		ieee80211_key_replace(key->sdata, NULL, key->sta,
1190 				      key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1191 				      key, NULL);
1192 		__ieee80211_key_destroy(key, key->sdata->vif.type ==
1193 					NL80211_IFTYPE_STATION);
1194 	}
1195 
1196 	for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1197 		key = wiphy_dereference(local->hw.wiphy, sta->ptk[i]);
1198 		if (!key)
1199 			continue;
1200 		ieee80211_key_replace(key->sdata, NULL, key->sta,
1201 				      key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1202 				      key, NULL);
1203 		__ieee80211_key_destroy(key, key->sdata->vif.type ==
1204 					NL80211_IFTYPE_STATION);
1205 	}
1206 }
1207 
ieee80211_delayed_tailroom_dec(struct wiphy * wiphy,struct wiphy_work * wk)1208 void ieee80211_delayed_tailroom_dec(struct wiphy *wiphy,
1209 				    struct wiphy_work *wk)
1210 {
1211 	struct ieee80211_sub_if_data *sdata;
1212 
1213 	sdata = container_of(wk, struct ieee80211_sub_if_data,
1214 			     dec_tailroom_needed_wk.work);
1215 
1216 	/*
1217 	 * The reason for the delayed tailroom needed decrementing is to
1218 	 * make roaming faster: during roaming, all keys are first deleted
1219 	 * and then new keys are installed. The first new key causes the
1220 	 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1221 	 * the cost of synchronize_net() (which can be slow). Avoid this
1222 	 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1223 	 * key removal for a while, so if we roam the value is larger than
1224 	 * zero and no 0->1 transition happens.
1225 	 *
1226 	 * The cost is that if the AP switching was from an AP with keys
1227 	 * to one without, we still allocate tailroom while it would no
1228 	 * longer be needed. However, in the typical (fast) roaming case
1229 	 * within an ESS this usually won't happen.
1230 	 */
1231 
1232 	decrease_tailroom_need_count(sdata,
1233 				     sdata->crypto_tx_tailroom_pending_dec);
1234 	sdata->crypto_tx_tailroom_pending_dec = 0;
1235 }
1236 
ieee80211_gtk_rekey_notify(struct ieee80211_vif * vif,const u8 * bssid,const u8 * replay_ctr,gfp_t gfp)1237 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1238 				const u8 *replay_ctr, gfp_t gfp)
1239 {
1240 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1241 
1242 	trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1243 
1244 	cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1245 }
1246 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
1247 
ieee80211_get_key_rx_seq(struct ieee80211_key_conf * keyconf,int tid,struct ieee80211_key_seq * seq)1248 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1249 			      int tid, struct ieee80211_key_seq *seq)
1250 {
1251 	struct ieee80211_key *key;
1252 	const u8 *pn;
1253 
1254 	key = container_of(keyconf, struct ieee80211_key, conf);
1255 
1256 	switch (key->conf.cipher) {
1257 	case WLAN_CIPHER_SUITE_TKIP:
1258 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1259 			return;
1260 		seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1261 		seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1262 		break;
1263 	case WLAN_CIPHER_SUITE_CCMP:
1264 	case WLAN_CIPHER_SUITE_CCMP_256:
1265 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1266 			return;
1267 		if (tid < 0)
1268 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1269 		else
1270 			pn = key->u.ccmp.rx_pn[tid];
1271 		memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
1272 		break;
1273 	case WLAN_CIPHER_SUITE_AES_CMAC:
1274 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1275 		if (WARN_ON(tid != 0))
1276 			return;
1277 		pn = key->u.aes_cmac.rx_pn;
1278 		memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
1279 		break;
1280 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1281 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1282 		if (WARN_ON(tid != 0))
1283 			return;
1284 		pn = key->u.aes_gmac.rx_pn;
1285 		memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1286 		break;
1287 	case WLAN_CIPHER_SUITE_GCMP:
1288 	case WLAN_CIPHER_SUITE_GCMP_256:
1289 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1290 			return;
1291 		if (tid < 0)
1292 			pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1293 		else
1294 			pn = key->u.gcmp.rx_pn[tid];
1295 		memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1296 		break;
1297 	}
1298 }
1299 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
1300 
ieee80211_set_key_rx_seq(struct ieee80211_key_conf * keyconf,int tid,struct ieee80211_key_seq * seq)1301 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1302 			      int tid, struct ieee80211_key_seq *seq)
1303 {
1304 	struct ieee80211_key *key;
1305 	u8 *pn;
1306 
1307 	key = container_of(keyconf, struct ieee80211_key, conf);
1308 
1309 	switch (key->conf.cipher) {
1310 	case WLAN_CIPHER_SUITE_TKIP:
1311 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1312 			return;
1313 		key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1314 		key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1315 		break;
1316 	case WLAN_CIPHER_SUITE_CCMP:
1317 	case WLAN_CIPHER_SUITE_CCMP_256:
1318 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1319 			return;
1320 		if (tid < 0)
1321 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1322 		else
1323 			pn = key->u.ccmp.rx_pn[tid];
1324 		memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1325 		break;
1326 	case WLAN_CIPHER_SUITE_AES_CMAC:
1327 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1328 		if (WARN_ON(tid != 0))
1329 			return;
1330 		pn = key->u.aes_cmac.rx_pn;
1331 		memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1332 		break;
1333 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1334 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1335 		if (WARN_ON(tid != 0))
1336 			return;
1337 		pn = key->u.aes_gmac.rx_pn;
1338 		memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1339 		break;
1340 	case WLAN_CIPHER_SUITE_GCMP:
1341 	case WLAN_CIPHER_SUITE_GCMP_256:
1342 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1343 			return;
1344 		if (tid < 0)
1345 			pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1346 		else
1347 			pn = key->u.gcmp.rx_pn[tid];
1348 		memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1349 		break;
1350 	default:
1351 		WARN_ON(1);
1352 		break;
1353 	}
1354 }
1355 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1356 
1357 struct ieee80211_key_conf *
ieee80211_gtk_rekey_add(struct ieee80211_vif * vif,u8 idx,u8 * key_data,u8 key_len,int link_id)1358 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1359 			u8 idx, u8 *key_data, u8 key_len,
1360 			int link_id)
1361 {
1362 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1363 	struct ieee80211_local *local = sdata->local;
1364 	struct ieee80211_key *prev_key;
1365 	struct ieee80211_key *key;
1366 	int err;
1367 	struct ieee80211_link_data *link_data =
1368 		link_id < 0 ? &sdata->deflink :
1369 		sdata_dereference(sdata->link[link_id], sdata);
1370 
1371 	if (WARN_ON(!link_data))
1372 		return ERR_PTR(-EINVAL);
1373 
1374 	if (WARN_ON(!local->wowlan))
1375 		return ERR_PTR(-EINVAL);
1376 
1377 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1378 		return ERR_PTR(-EINVAL);
1379 
1380 	if (WARN_ON(idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
1381 		    NUM_DEFAULT_BEACON_KEYS))
1382 		return ERR_PTR(-EINVAL);
1383 
1384 	prev_key = wiphy_dereference(local->hw.wiphy,
1385 				     link_data->gtk[idx]);
1386 	if (!prev_key) {
1387 		if (idx < NUM_DEFAULT_KEYS) {
1388 			for (int i = 0; i < NUM_DEFAULT_KEYS; i++) {
1389 				if (i == idx)
1390 					continue;
1391 				prev_key = wiphy_dereference(local->hw.wiphy,
1392 							     link_data->gtk[i]);
1393 				if (prev_key)
1394 					break;
1395 			}
1396 		} else {
1397 			/* For IGTK we have 4 and 5 and for BIGTK - 6 and 7 */
1398 			prev_key = wiphy_dereference(local->hw.wiphy,
1399 						     link_data->gtk[idx ^ 1]);
1400 		}
1401 	}
1402 
1403 	if (WARN_ON(!prev_key))
1404 		return ERR_PTR(-EINVAL);
1405 
1406 	if (WARN_ON(key_len < prev_key->conf.keylen))
1407 		return ERR_PTR(-EINVAL);
1408 
1409 	key = ieee80211_key_alloc(prev_key->conf.cipher, idx,
1410 				  prev_key->conf.keylen, key_data,
1411 				  0, NULL);
1412 	if (IS_ERR(key))
1413 		return ERR_CAST(key);
1414 
1415 	if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1416 		key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1417 
1418 	key->conf.link_id = link_data->link_id;
1419 
1420 	err = ieee80211_key_link(key, link_data, NULL);
1421 	if (err)
1422 		return ERR_PTR(err);
1423 
1424 	return &key->conf;
1425 }
1426 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
1427 
ieee80211_key_mic_failure(struct ieee80211_key_conf * keyconf)1428 void ieee80211_key_mic_failure(struct ieee80211_key_conf *keyconf)
1429 {
1430 	struct ieee80211_key *key;
1431 
1432 	key = container_of(keyconf, struct ieee80211_key, conf);
1433 
1434 	switch (key->conf.cipher) {
1435 	case WLAN_CIPHER_SUITE_AES_CMAC:
1436 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1437 		key->u.aes_cmac.icverrors++;
1438 		break;
1439 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1440 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1441 		key->u.aes_gmac.icverrors++;
1442 		break;
1443 	default:
1444 		/* ignore the others for now, we don't keep counters now */
1445 		break;
1446 	}
1447 }
1448 EXPORT_SYMBOL_GPL(ieee80211_key_mic_failure);
1449 
ieee80211_key_replay(struct ieee80211_key_conf * keyconf)1450 void ieee80211_key_replay(struct ieee80211_key_conf *keyconf)
1451 {
1452 	struct ieee80211_key *key;
1453 
1454 	key = container_of(keyconf, struct ieee80211_key, conf);
1455 
1456 	switch (key->conf.cipher) {
1457 	case WLAN_CIPHER_SUITE_CCMP:
1458 	case WLAN_CIPHER_SUITE_CCMP_256:
1459 		key->u.ccmp.replays++;
1460 		break;
1461 	case WLAN_CIPHER_SUITE_AES_CMAC:
1462 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1463 		key->u.aes_cmac.replays++;
1464 		break;
1465 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1466 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1467 		key->u.aes_gmac.replays++;
1468 		break;
1469 	case WLAN_CIPHER_SUITE_GCMP:
1470 	case WLAN_CIPHER_SUITE_GCMP_256:
1471 		key->u.gcmp.replays++;
1472 		break;
1473 	}
1474 }
1475 EXPORT_SYMBOL_GPL(ieee80211_key_replay);
1476 
ieee80211_key_switch_links(struct ieee80211_sub_if_data * sdata,unsigned long del_links_mask,unsigned long add_links_mask)1477 int ieee80211_key_switch_links(struct ieee80211_sub_if_data *sdata,
1478 			       unsigned long del_links_mask,
1479 			       unsigned long add_links_mask)
1480 {
1481 	struct ieee80211_key *key;
1482 	int ret;
1483 
1484 	list_for_each_entry(key, &sdata->key_list, list) {
1485 		if (key->conf.link_id < 0 ||
1486 		    !(del_links_mask & BIT(key->conf.link_id)))
1487 			continue;
1488 
1489 		/* shouldn't happen for per-link keys */
1490 		WARN_ON(key->sta);
1491 
1492 		ieee80211_key_disable_hw_accel(key);
1493 	}
1494 
1495 	list_for_each_entry(key, &sdata->key_list, list) {
1496 		if (key->conf.link_id < 0 ||
1497 		    !(add_links_mask & BIT(key->conf.link_id)))
1498 			continue;
1499 
1500 		/* shouldn't happen for per-link keys */
1501 		WARN_ON(key->sta);
1502 
1503 		ret = ieee80211_key_enable_hw_accel(key);
1504 		if (ret)
1505 			return ret;
1506 	}
1507 
1508 	return 0;
1509 }
1510