• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007-2008	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2017	Intel Deutschland GmbH
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/if_ether.h>
15 #include <linux/etherdevice.h>
16 #include <linux/list.h>
17 #include <linux/rcupdate.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/slab.h>
20 #include <linux/export.h>
21 #include <net/mac80211.h>
22 #include <crypto/algapi.h>
23 #include <asm/unaligned.h>
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "debugfs_key.h"
27 #include "aes_ccm.h"
28 #include "aes_cmac.h"
29 
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 
assert_key_lock(struct ieee80211_local * local)56 static void assert_key_lock(struct ieee80211_local *local)
57 {
58 	lockdep_assert_held(&local->key_mtx);
59 }
60 
increment_tailroom_need_count(struct ieee80211_sub_if_data * sdata)61 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
62 {
63 	/*
64 	 * When this count is zero, SKB resizing for allocating tailroom
65 	 * for IV or MMIC is skipped. But, this check has created two race
66 	 * cases in xmit path while transiting from zero count to one:
67 	 *
68 	 * 1. SKB resize was skipped because no key was added but just before
69 	 * the xmit key is added and SW encryption kicks off.
70 	 *
71 	 * 2. SKB resize was skipped because all the keys were hw planted but
72 	 * just before xmit one of the key is deleted and SW encryption kicks
73 	 * off.
74 	 *
75 	 * In both the above case SW encryption will find not enough space for
76 	 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
77 	 *
78 	 * Solution has been explained at
79 	 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
80 	 */
81 
82 	if (!sdata->crypto_tx_tailroom_needed_cnt++) {
83 		/*
84 		 * Flush all XMIT packets currently using HW encryption or no
85 		 * encryption at all if the count transition is from 0 -> 1.
86 		 */
87 		synchronize_net();
88 	}
89 }
90 
ieee80211_key_enable_hw_accel(struct ieee80211_key * key)91 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
92 {
93 	struct ieee80211_sub_if_data *sdata;
94 	struct sta_info *sta;
95 	int ret;
96 
97 	might_sleep();
98 
99 	if (key->flags & KEY_FLAG_TAINTED)
100 		return -EINVAL;
101 
102 	if (!key->local->ops->set_key)
103 		goto out_unsupported;
104 
105 	assert_key_lock(key->local);
106 
107 	sta = key->sta;
108 
109 	/*
110 	 * If this is a per-STA GTK, check if it
111 	 * is supported; if not, return.
112 	 */
113 	if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
114 	    !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
115 		goto out_unsupported;
116 
117 	if (sta && !sta->uploaded)
118 		goto out_unsupported;
119 
120 	sdata = key->sdata;
121 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
122 		/*
123 		 * The driver doesn't know anything about VLAN interfaces.
124 		 * Hence, don't send GTKs for VLAN interfaces to the driver.
125 		 */
126 		if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
127 			goto out_unsupported;
128 	}
129 
130 	ret = drv_set_key(key->local, SET_KEY, sdata,
131 			  sta ? &sta->sta : NULL, &key->conf);
132 
133 	if (!ret) {
134 		key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
135 
136 		if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
137 		      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
138 		      (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
139 			sdata->crypto_tx_tailroom_needed_cnt--;
140 
141 		WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
142 			(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
143 
144 		return 0;
145 	}
146 
147 	if (ret != -ENOSPC && ret != -EOPNOTSUPP)
148 		sdata_err(sdata,
149 			  "failed to set key (%d, %pM) to hardware (%d)\n",
150 			  key->conf.keyidx,
151 			  sta ? sta->sta.addr : bcast_addr, ret);
152 
153  out_unsupported:
154 	switch (key->conf.cipher) {
155 	case WLAN_CIPHER_SUITE_WEP40:
156 	case WLAN_CIPHER_SUITE_WEP104:
157 	case WLAN_CIPHER_SUITE_TKIP:
158 	case WLAN_CIPHER_SUITE_CCMP:
159 	case WLAN_CIPHER_SUITE_AES_CMAC:
160 		/* all of these we can do in software */
161 		return 0;
162 	default:
163 		return -EINVAL;
164 	}
165 }
166 
ieee80211_key_disable_hw_accel(struct ieee80211_key * key)167 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
168 {
169 	struct ieee80211_sub_if_data *sdata;
170 	struct sta_info *sta;
171 	int ret;
172 
173 	might_sleep();
174 
175 	if (!key || !key->local->ops->set_key)
176 		return;
177 
178 	assert_key_lock(key->local);
179 
180 	if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
181 		return;
182 
183 	sta = key->sta;
184 	sdata = key->sdata;
185 
186 	if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
187 	      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
188 	      (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
189 		increment_tailroom_need_count(sdata);
190 
191 	ret = drv_set_key(key->local, DISABLE_KEY, sdata,
192 			  sta ? &sta->sta : NULL, &key->conf);
193 
194 	if (ret)
195 		sdata_err(sdata,
196 			  "failed to remove key (%d, %pM) from hardware (%d)\n",
197 			  key->conf.keyidx,
198 			  sta ? sta->sta.addr : bcast_addr, ret);
199 
200 	key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
201 }
202 
__ieee80211_set_default_key(struct ieee80211_sub_if_data * sdata,int idx,bool uni,bool multi)203 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
204 					int idx, bool uni, bool multi)
205 {
206 	struct ieee80211_key *key = NULL;
207 
208 	assert_key_lock(sdata->local);
209 
210 	if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
211 		key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
212 
213 	if (uni) {
214 		rcu_assign_pointer(sdata->default_unicast_key, key);
215 		drv_set_default_unicast_key(sdata->local, sdata, idx);
216 	}
217 
218 	if (multi)
219 		rcu_assign_pointer(sdata->default_multicast_key, key);
220 
221 	ieee80211_debugfs_key_update_default(sdata);
222 }
223 
ieee80211_set_default_key(struct ieee80211_sub_if_data * sdata,int idx,bool uni,bool multi)224 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
225 			       bool uni, bool multi)
226 {
227 	mutex_lock(&sdata->local->key_mtx);
228 	__ieee80211_set_default_key(sdata, idx, uni, multi);
229 	mutex_unlock(&sdata->local->key_mtx);
230 }
231 
232 static void
__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data * sdata,int idx)233 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
234 {
235 	struct ieee80211_key *key = NULL;
236 
237 	assert_key_lock(sdata->local);
238 
239 	if (idx >= NUM_DEFAULT_KEYS &&
240 	    idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
241 		key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
242 
243 	rcu_assign_pointer(sdata->default_mgmt_key, key);
244 
245 	ieee80211_debugfs_key_update_default(sdata);
246 }
247 
ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data * sdata,int idx)248 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
249 				    int idx)
250 {
251 	mutex_lock(&sdata->local->key_mtx);
252 	__ieee80211_set_default_mgmt_key(sdata, idx);
253 	mutex_unlock(&sdata->local->key_mtx);
254 }
255 
256 
ieee80211_key_replace(struct ieee80211_sub_if_data * sdata,struct sta_info * sta,bool pairwise,struct ieee80211_key * old,struct ieee80211_key * new)257 static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
258 				  struct sta_info *sta,
259 				  bool pairwise,
260 				  struct ieee80211_key *old,
261 				  struct ieee80211_key *new)
262 {
263 	int idx;
264 	bool defunikey, defmultikey, defmgmtkey;
265 
266 	/* caller must provide at least one old/new */
267 	if (WARN_ON(!new && !old))
268 		return;
269 
270 	if (new)
271 		list_add_tail(&new->list, &sdata->key_list);
272 
273 	WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
274 
275 	if (old)
276 		idx = old->conf.keyidx;
277 	else
278 		idx = new->conf.keyidx;
279 
280 	if (sta) {
281 		if (pairwise) {
282 			rcu_assign_pointer(sta->ptk[idx], new);
283 			sta->ptk_idx = idx;
284 		} else {
285 			rcu_assign_pointer(sta->gtk[idx], new);
286 			sta->gtk_idx = idx;
287 		}
288 	} else {
289 		defunikey = old &&
290 			old == key_mtx_dereference(sdata->local,
291 						sdata->default_unicast_key);
292 		defmultikey = old &&
293 			old == key_mtx_dereference(sdata->local,
294 						sdata->default_multicast_key);
295 		defmgmtkey = old &&
296 			old == key_mtx_dereference(sdata->local,
297 						sdata->default_mgmt_key);
298 
299 		if (defunikey && !new)
300 			__ieee80211_set_default_key(sdata, -1, true, false);
301 		if (defmultikey && !new)
302 			__ieee80211_set_default_key(sdata, -1, false, true);
303 		if (defmgmtkey && !new)
304 			__ieee80211_set_default_mgmt_key(sdata, -1);
305 
306 		rcu_assign_pointer(sdata->keys[idx], new);
307 		if (defunikey && new)
308 			__ieee80211_set_default_key(sdata, new->conf.keyidx,
309 						    true, false);
310 		if (defmultikey && new)
311 			__ieee80211_set_default_key(sdata, new->conf.keyidx,
312 						    false, true);
313 		if (defmgmtkey && new)
314 			__ieee80211_set_default_mgmt_key(sdata,
315 							 new->conf.keyidx);
316 	}
317 
318 	if (old)
319 		list_del(&old->list);
320 }
321 
322 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,const struct ieee80211_cipher_scheme * cs)323 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
324 		    const u8 *key_data,
325 		    size_t seq_len, const u8 *seq,
326 		    const struct ieee80211_cipher_scheme *cs)
327 {
328 	struct ieee80211_key *key;
329 	int i, j, err;
330 
331 	if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
332 		return ERR_PTR(-EINVAL);
333 
334 	key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
335 	if (!key)
336 		return ERR_PTR(-ENOMEM);
337 
338 	/*
339 	 * Default to software encryption; we'll later upload the
340 	 * key to the hardware if possible.
341 	 */
342 	key->conf.flags = 0;
343 	key->flags = 0;
344 
345 	key->conf.cipher = cipher;
346 	key->conf.keyidx = idx;
347 	key->conf.keylen = key_len;
348 	switch (cipher) {
349 	case WLAN_CIPHER_SUITE_WEP40:
350 	case WLAN_CIPHER_SUITE_WEP104:
351 		key->conf.iv_len = IEEE80211_WEP_IV_LEN;
352 		key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
353 		break;
354 	case WLAN_CIPHER_SUITE_TKIP:
355 		key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
356 		key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
357 		if (seq) {
358 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
359 				key->u.tkip.rx[i].iv32 =
360 					get_unaligned_le32(&seq[2]);
361 				key->u.tkip.rx[i].iv16 =
362 					get_unaligned_le16(seq);
363 			}
364 		}
365 		spin_lock_init(&key->u.tkip.txlock);
366 		break;
367 	case WLAN_CIPHER_SUITE_CCMP:
368 		key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
369 		key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
370 		if (seq) {
371 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
372 				for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
373 					key->u.ccmp.rx_pn[i][j] =
374 						seq[IEEE80211_CCMP_PN_LEN - j - 1];
375 		}
376 		/*
377 		 * Initialize AES key state here as an optimization so that
378 		 * it does not need to be initialized for every packet.
379 		 */
380 		key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
381 		if (IS_ERR(key->u.ccmp.tfm)) {
382 			err = PTR_ERR(key->u.ccmp.tfm);
383 			kfree(key);
384 			return ERR_PTR(err);
385 		}
386 		break;
387 	case WLAN_CIPHER_SUITE_AES_CMAC:
388 		key->conf.iv_len = 0;
389 		key->conf.icv_len = sizeof(struct ieee80211_mmie);
390 		if (seq)
391 			for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
392 				key->u.aes_cmac.rx_pn[j] =
393 					seq[IEEE80211_CMAC_PN_LEN - j - 1];
394 		/*
395 		 * Initialize AES key state here as an optimization so that
396 		 * it does not need to be initialized for every packet.
397 		 */
398 		key->u.aes_cmac.tfm =
399 			ieee80211_aes_cmac_key_setup(key_data);
400 		if (IS_ERR(key->u.aes_cmac.tfm)) {
401 			err = PTR_ERR(key->u.aes_cmac.tfm);
402 			kfree(key);
403 			return ERR_PTR(err);
404 		}
405 		break;
406 	default:
407 		if (cs) {
408 			size_t len = (seq_len > MAX_PN_LEN) ?
409 						MAX_PN_LEN : seq_len;
410 
411 			key->conf.iv_len = cs->hdr_len;
412 			key->conf.icv_len = cs->mic_len;
413 			for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
414 				for (j = 0; j < len; j++)
415 					key->u.gen.rx_pn[i][j] =
416 							seq[len - j - 1];
417 		}
418 	}
419 	memcpy(key->conf.key, key_data, key_len);
420 	INIT_LIST_HEAD(&key->list);
421 
422 	return key;
423 }
424 
ieee80211_key_free_common(struct ieee80211_key * key)425 static void ieee80211_key_free_common(struct ieee80211_key *key)
426 {
427 	if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
428 		ieee80211_aes_key_free(key->u.ccmp.tfm);
429 	if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
430 		ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
431 	kzfree(key);
432 }
433 
__ieee80211_key_destroy(struct ieee80211_key * key,bool delay_tailroom)434 static void __ieee80211_key_destroy(struct ieee80211_key *key,
435 				    bool delay_tailroom)
436 {
437 	if (key->local)
438 		ieee80211_key_disable_hw_accel(key);
439 
440 	if (key->local) {
441 		struct ieee80211_sub_if_data *sdata = key->sdata;
442 
443 		ieee80211_debugfs_key_remove(key);
444 
445 		if (delay_tailroom) {
446 			/* see ieee80211_delayed_tailroom_dec */
447 			sdata->crypto_tx_tailroom_pending_dec++;
448 			schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
449 					      HZ/2);
450 		} else {
451 			sdata->crypto_tx_tailroom_needed_cnt--;
452 		}
453 	}
454 
455 	ieee80211_key_free_common(key);
456 }
457 
ieee80211_key_destroy(struct ieee80211_key * key,bool delay_tailroom)458 static void ieee80211_key_destroy(struct ieee80211_key *key,
459 				  bool delay_tailroom)
460 {
461 	if (!key)
462 		return;
463 
464 	/*
465 	 * Synchronize so the TX path can no longer be using
466 	 * this key before we free/remove it.
467 	 */
468 	synchronize_net();
469 
470 	__ieee80211_key_destroy(key, delay_tailroom);
471 }
472 
ieee80211_key_free_unused(struct ieee80211_key * key)473 void ieee80211_key_free_unused(struct ieee80211_key *key)
474 {
475 	WARN_ON(key->sdata || key->local);
476 	ieee80211_key_free_common(key);
477 }
478 
ieee80211_key_identical(struct ieee80211_sub_if_data * sdata,struct ieee80211_key * old,struct ieee80211_key * new)479 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
480 				    struct ieee80211_key *old,
481 				    struct ieee80211_key *new)
482 {
483 	u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
484 	u8 *tk_old, *tk_new;
485 
486 	if (!old || new->conf.keylen != old->conf.keylen)
487 		return false;
488 
489 	tk_old = old->conf.key;
490 	tk_new = new->conf.key;
491 
492 	/*
493 	 * In station mode, don't compare the TX MIC key, as it's never used
494 	 * and offloaded rekeying may not care to send it to the host. This
495 	 * is the case in iwlwifi, for example.
496 	 */
497 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
498 	    new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
499 	    new->conf.keylen == WLAN_KEY_LEN_TKIP &&
500 	    !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
501 		memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
502 		memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
503 		memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
504 		memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
505 		tk_old = tkip_old;
506 		tk_new = tkip_new;
507 	}
508 
509 	return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
510 }
511 
ieee80211_key_link(struct ieee80211_key * key,struct ieee80211_sub_if_data * sdata,struct sta_info * sta)512 int ieee80211_key_link(struct ieee80211_key *key,
513 		       struct ieee80211_sub_if_data *sdata,
514 		       struct sta_info *sta)
515 {
516 	struct ieee80211_local *local = sdata->local;
517 	struct ieee80211_key *old_key;
518 	int idx, ret;
519 	bool pairwise;
520 
521 	pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
522 	idx = key->conf.keyidx;
523 
524 	mutex_lock(&sdata->local->key_mtx);
525 
526 	if (sta && pairwise)
527 		old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
528 	else if (sta)
529 		old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
530 	else
531 		old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
532 
533 	/*
534 	 * Silently accept key re-installation without really installing the
535 	 * new version of the key to avoid nonce reuse or replay issues.
536 	 */
537 	if (ieee80211_key_identical(sdata, old_key, key)) {
538 		ieee80211_key_free_unused(key);
539 		ret = 0;
540 		goto out;
541 	}
542 
543 	key->local = sdata->local;
544 	key->sdata = sdata;
545 	key->sta = sta;
546 
547 	increment_tailroom_need_count(sdata);
548 
549 	ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
550 	ieee80211_key_destroy(old_key, true);
551 
552 	ieee80211_debugfs_key_add(key);
553 
554 	if (!local->wowlan) {
555 		ret = ieee80211_key_enable_hw_accel(key);
556 		if (ret)
557 			ieee80211_key_free(key, true);
558 	} else {
559 		ret = 0;
560 	}
561 
562  out:
563 	mutex_unlock(&sdata->local->key_mtx);
564 
565 	return ret;
566 }
567 
ieee80211_key_free(struct ieee80211_key * key,bool delay_tailroom)568 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
569 {
570 	if (!key)
571 		return;
572 
573 	/*
574 	 * Replace key with nothingness if it was ever used.
575 	 */
576 	if (key->sdata)
577 		ieee80211_key_replace(key->sdata, key->sta,
578 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
579 				key, NULL);
580 	ieee80211_key_destroy(key, delay_tailroom);
581 }
582 
ieee80211_enable_keys(struct ieee80211_sub_if_data * sdata)583 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
584 {
585 	struct ieee80211_key *key;
586 
587 	ASSERT_RTNL();
588 
589 	if (WARN_ON(!ieee80211_sdata_running(sdata)))
590 		return;
591 
592 	mutex_lock(&sdata->local->key_mtx);
593 
594 	sdata->crypto_tx_tailroom_needed_cnt = 0;
595 
596 	list_for_each_entry(key, &sdata->key_list, list) {
597 		increment_tailroom_need_count(sdata);
598 		ieee80211_key_enable_hw_accel(key);
599 	}
600 
601 	mutex_unlock(&sdata->local->key_mtx);
602 }
603 
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)604 void ieee80211_iter_keys(struct ieee80211_hw *hw,
605 			 struct ieee80211_vif *vif,
606 			 void (*iter)(struct ieee80211_hw *hw,
607 				      struct ieee80211_vif *vif,
608 				      struct ieee80211_sta *sta,
609 				      struct ieee80211_key_conf *key,
610 				      void *data),
611 			 void *iter_data)
612 {
613 	struct ieee80211_local *local = hw_to_local(hw);
614 	struct ieee80211_key *key, *tmp;
615 	struct ieee80211_sub_if_data *sdata;
616 
617 	ASSERT_RTNL();
618 
619 	mutex_lock(&local->key_mtx);
620 	if (vif) {
621 		sdata = vif_to_sdata(vif);
622 		list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
623 			iter(hw, &sdata->vif,
624 			     key->sta ? &key->sta->sta : NULL,
625 			     &key->conf, iter_data);
626 	} else {
627 		list_for_each_entry(sdata, &local->interfaces, list)
628 			list_for_each_entry_safe(key, tmp,
629 						 &sdata->key_list, list)
630 				iter(hw, &sdata->vif,
631 				     key->sta ? &key->sta->sta : NULL,
632 				     &key->conf, iter_data);
633 	}
634 	mutex_unlock(&local->key_mtx);
635 }
636 EXPORT_SYMBOL(ieee80211_iter_keys);
637 
ieee80211_free_keys_iface(struct ieee80211_sub_if_data * sdata,struct list_head * keys)638 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
639 				      struct list_head *keys)
640 {
641 	struct ieee80211_key *key, *tmp;
642 
643 	sdata->crypto_tx_tailroom_needed_cnt -=
644 		sdata->crypto_tx_tailroom_pending_dec;
645 	sdata->crypto_tx_tailroom_pending_dec = 0;
646 
647 	ieee80211_debugfs_key_remove_mgmt_default(sdata);
648 
649 	list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
650 		ieee80211_key_replace(key->sdata, key->sta,
651 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
652 				key, NULL);
653 		list_add_tail(&key->list, keys);
654 	}
655 
656 	ieee80211_debugfs_key_update_default(sdata);
657 }
658 
ieee80211_free_keys(struct ieee80211_sub_if_data * sdata,bool force_synchronize)659 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
660 			 bool force_synchronize)
661 {
662 	struct ieee80211_local *local = sdata->local;
663 	struct ieee80211_sub_if_data *vlan;
664 	struct ieee80211_key *key, *tmp;
665 	LIST_HEAD(keys);
666 
667 	cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
668 
669 	mutex_lock(&local->key_mtx);
670 
671 	ieee80211_free_keys_iface(sdata, &keys);
672 
673 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
674 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
675 			ieee80211_free_keys_iface(vlan, &keys);
676 	}
677 
678 	if (!list_empty(&keys) || force_synchronize)
679 		synchronize_net();
680 	list_for_each_entry_safe(key, tmp, &keys, list)
681 		__ieee80211_key_destroy(key, false);
682 
683 	WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
684 		     sdata->crypto_tx_tailroom_pending_dec);
685 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
686 		list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
687 			WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
688 				     vlan->crypto_tx_tailroom_pending_dec);
689 	}
690 
691 	mutex_unlock(&local->key_mtx);
692 }
693 
ieee80211_free_sta_keys(struct ieee80211_local * local,struct sta_info * sta)694 void ieee80211_free_sta_keys(struct ieee80211_local *local,
695 			     struct sta_info *sta)
696 {
697 	struct ieee80211_key *key;
698 	int i;
699 
700 	mutex_lock(&local->key_mtx);
701 	for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
702 		key = key_mtx_dereference(local, sta->gtk[i]);
703 		if (!key)
704 			continue;
705 		ieee80211_key_replace(key->sdata, key->sta,
706 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
707 				key, NULL);
708 		__ieee80211_key_destroy(key, true);
709 	}
710 
711 	for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
712 		key = key_mtx_dereference(local, sta->ptk[i]);
713 		if (!key)
714 			continue;
715 		ieee80211_key_replace(key->sdata, key->sta,
716 				key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
717 				key, NULL);
718 		__ieee80211_key_destroy(key, true);
719 	}
720 
721 	mutex_unlock(&local->key_mtx);
722 }
723 
ieee80211_delayed_tailroom_dec(struct work_struct * wk)724 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
725 {
726 	struct ieee80211_sub_if_data *sdata;
727 
728 	sdata = container_of(wk, struct ieee80211_sub_if_data,
729 			     dec_tailroom_needed_wk.work);
730 
731 	/*
732 	 * The reason for the delayed tailroom needed decrementing is to
733 	 * make roaming faster: during roaming, all keys are first deleted
734 	 * and then new keys are installed. The first new key causes the
735 	 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
736 	 * the cost of synchronize_net() (which can be slow). Avoid this
737 	 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
738 	 * key removal for a while, so if we roam the value is larger than
739 	 * zero and no 0->1 transition happens.
740 	 *
741 	 * The cost is that if the AP switching was from an AP with keys
742 	 * to one without, we still allocate tailroom while it would no
743 	 * longer be needed. However, in the typical (fast) roaming case
744 	 * within an ESS this usually won't happen.
745 	 */
746 
747 	mutex_lock(&sdata->local->key_mtx);
748 	sdata->crypto_tx_tailroom_needed_cnt -=
749 		sdata->crypto_tx_tailroom_pending_dec;
750 	sdata->crypto_tx_tailroom_pending_dec = 0;
751 	mutex_unlock(&sdata->local->key_mtx);
752 }
753 
ieee80211_gtk_rekey_notify(struct ieee80211_vif * vif,const u8 * bssid,const u8 * replay_ctr,gfp_t gfp)754 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
755 				const u8 *replay_ctr, gfp_t gfp)
756 {
757 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
758 
759 	trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
760 
761 	cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
762 }
763 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
764 
ieee80211_get_key_tx_seq(struct ieee80211_key_conf * keyconf,struct ieee80211_key_seq * seq)765 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
766 			      struct ieee80211_key_seq *seq)
767 {
768 	struct ieee80211_key *key;
769 	u64 pn64;
770 
771 	if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
772 		return;
773 
774 	key = container_of(keyconf, struct ieee80211_key, conf);
775 
776 	switch (key->conf.cipher) {
777 	case WLAN_CIPHER_SUITE_TKIP:
778 		seq->tkip.iv32 = key->u.tkip.tx.iv32;
779 		seq->tkip.iv16 = key->u.tkip.tx.iv16;
780 		break;
781 	case WLAN_CIPHER_SUITE_CCMP:
782 		pn64 = atomic64_read(&key->u.ccmp.tx_pn);
783 		seq->ccmp.pn[5] = pn64;
784 		seq->ccmp.pn[4] = pn64 >> 8;
785 		seq->ccmp.pn[3] = pn64 >> 16;
786 		seq->ccmp.pn[2] = pn64 >> 24;
787 		seq->ccmp.pn[1] = pn64 >> 32;
788 		seq->ccmp.pn[0] = pn64 >> 40;
789 		break;
790 	case WLAN_CIPHER_SUITE_AES_CMAC:
791 		pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
792 		seq->ccmp.pn[5] = pn64;
793 		seq->ccmp.pn[4] = pn64 >> 8;
794 		seq->ccmp.pn[3] = pn64 >> 16;
795 		seq->ccmp.pn[2] = pn64 >> 24;
796 		seq->ccmp.pn[1] = pn64 >> 32;
797 		seq->ccmp.pn[0] = pn64 >> 40;
798 		break;
799 	default:
800 		WARN_ON(1);
801 	}
802 }
803 EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
804 
ieee80211_get_key_rx_seq(struct ieee80211_key_conf * keyconf,int tid,struct ieee80211_key_seq * seq)805 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
806 			      int tid, struct ieee80211_key_seq *seq)
807 {
808 	struct ieee80211_key *key;
809 	const u8 *pn;
810 
811 	key = container_of(keyconf, struct ieee80211_key, conf);
812 
813 	switch (key->conf.cipher) {
814 	case WLAN_CIPHER_SUITE_TKIP:
815 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
816 			return;
817 		seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
818 		seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
819 		break;
820 	case WLAN_CIPHER_SUITE_CCMP:
821 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
822 			return;
823 		if (tid < 0)
824 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
825 		else
826 			pn = key->u.ccmp.rx_pn[tid];
827 		memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
828 		break;
829 	case WLAN_CIPHER_SUITE_AES_CMAC:
830 		if (WARN_ON(tid != 0))
831 			return;
832 		pn = key->u.aes_cmac.rx_pn;
833 		memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
834 		break;
835 	}
836 }
837 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
838 
ieee80211_set_key_tx_seq(struct ieee80211_key_conf * keyconf,struct ieee80211_key_seq * seq)839 void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
840 			      struct ieee80211_key_seq *seq)
841 {
842 	struct ieee80211_key *key;
843 	u64 pn64;
844 
845 	key = container_of(keyconf, struct ieee80211_key, conf);
846 
847 	switch (key->conf.cipher) {
848 	case WLAN_CIPHER_SUITE_TKIP:
849 		key->u.tkip.tx.iv32 = seq->tkip.iv32;
850 		key->u.tkip.tx.iv16 = seq->tkip.iv16;
851 		break;
852 	case WLAN_CIPHER_SUITE_CCMP:
853 		pn64 = (u64)seq->ccmp.pn[5] |
854 		       ((u64)seq->ccmp.pn[4] << 8) |
855 		       ((u64)seq->ccmp.pn[3] << 16) |
856 		       ((u64)seq->ccmp.pn[2] << 24) |
857 		       ((u64)seq->ccmp.pn[1] << 32) |
858 		       ((u64)seq->ccmp.pn[0] << 40);
859 		atomic64_set(&key->u.ccmp.tx_pn, pn64);
860 		break;
861 	case WLAN_CIPHER_SUITE_AES_CMAC:
862 		pn64 = (u64)seq->aes_cmac.pn[5] |
863 		       ((u64)seq->aes_cmac.pn[4] << 8) |
864 		       ((u64)seq->aes_cmac.pn[3] << 16) |
865 		       ((u64)seq->aes_cmac.pn[2] << 24) |
866 		       ((u64)seq->aes_cmac.pn[1] << 32) |
867 		       ((u64)seq->aes_cmac.pn[0] << 40);
868 		atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
869 		break;
870 	default:
871 		WARN_ON(1);
872 		break;
873 	}
874 }
875 EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
876 
ieee80211_set_key_rx_seq(struct ieee80211_key_conf * keyconf,int tid,struct ieee80211_key_seq * seq)877 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
878 			      int tid, struct ieee80211_key_seq *seq)
879 {
880 	struct ieee80211_key *key;
881 	u8 *pn;
882 
883 	key = container_of(keyconf, struct ieee80211_key, conf);
884 
885 	switch (key->conf.cipher) {
886 	case WLAN_CIPHER_SUITE_TKIP:
887 		if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
888 			return;
889 		key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
890 		key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
891 		break;
892 	case WLAN_CIPHER_SUITE_CCMP:
893 		if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
894 			return;
895 		if (tid < 0)
896 			pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
897 		else
898 			pn = key->u.ccmp.rx_pn[tid];
899 		memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
900 		break;
901 	case WLAN_CIPHER_SUITE_AES_CMAC:
902 		if (WARN_ON(tid != 0))
903 			return;
904 		pn = key->u.aes_cmac.rx_pn;
905 		memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
906 		break;
907 	default:
908 		WARN_ON(1);
909 		break;
910 	}
911 }
912 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
913 
ieee80211_remove_key(struct ieee80211_key_conf * keyconf)914 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
915 {
916 	struct ieee80211_key *key;
917 
918 	key = container_of(keyconf, struct ieee80211_key, conf);
919 
920 	assert_key_lock(key->local);
921 
922 	/*
923 	 * if key was uploaded, we assume the driver will/has remove(d)
924 	 * it, so adjust bookkeeping accordingly
925 	 */
926 	if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
927 		key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
928 
929 		if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
930 		      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
931 		      (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
932 			increment_tailroom_need_count(key->sdata);
933 	}
934 
935 	ieee80211_key_free(key, false);
936 }
937 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
938 
939 struct ieee80211_key_conf *
ieee80211_gtk_rekey_add(struct ieee80211_vif * vif,struct ieee80211_key_conf * keyconf)940 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
941 			struct ieee80211_key_conf *keyconf)
942 {
943 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
944 	struct ieee80211_local *local = sdata->local;
945 	struct ieee80211_key *key;
946 	int err;
947 
948 	if (WARN_ON(!local->wowlan))
949 		return ERR_PTR(-EINVAL);
950 
951 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
952 		return ERR_PTR(-EINVAL);
953 
954 	key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
955 				  keyconf->keylen, keyconf->key,
956 				  0, NULL, NULL);
957 	if (IS_ERR(key))
958 		return ERR_CAST(key);
959 
960 	if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
961 		key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
962 
963 	err = ieee80211_key_link(key, sdata, NULL);
964 	if (err)
965 		return ERR_PTR(err);
966 
967 	return &key->conf;
968 }
969 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
970