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