File Index
Files and directories
Sign in
FILE

key.c

File preview with syntax highlighting.

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