1518 lines · 42.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Copyright 2002-2005, Instant802 Networks, Inc. * Copyright 2005-2006, Devicescape Software, Inc. * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz> * Copyright 2007-2008	Johannes Berg <johannes@sipsolutions.net> * Copyright 2013-2014  Intel Mobile Communications GmbH * Copyright 2015-2017	Intel Deutschland GmbH * Copyright 2018-2020, 2022-2026  Intel Corporation */ #include <crypto/utils.h>#include <linux/if_ether.h>#include <linux/etherdevice.h>#include <linux/list.h>#include <linux/rcupdate.h>#include <linux/rtnetlink.h>#include <linux/slab.h>#include <linux/export.h>#include <net/mac80211.h>#include <linux/unaligned.h>#include "ieee80211_i.h"#include "driver-ops.h"#include "debugfs_key.h"#include "aes_ccm.h"#include "aes_cmac.h"#include "aes_gmac.h"#include "aes_gcm.h"  /** * DOC: Key handling basics * * Key handling in mac80211 is done based on per-interface (sub_if_data) * keys and per-station keys. Since each station belongs to an interface, * each station key also belongs to that interface. * * Hardware acceleration is done on a best-effort basis for algorithms * that are implemented in software,  for each key the hardware is asked * to enable that key for offloading but if it cannot do that the key is * simply kept for software encryption (unless it is for an algorithm * that isn't implemented in software). * There is currently no way of knowing whether a key is handled in SW * or HW except by looking into debugfs. * * All key management is internally protected by a mutex. Within all * other parts of mac80211, key references are, just as STA structure * references, protected by RCU. Note, however, that some things are * unprotected, namely the key->sta dereferences within the hardware * acceleration functions. This means that sta_info_destroy() must * remove the key which waits for an RCU grace period. */ static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; static voidupdate_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta){	struct ieee80211_sub_if_data *vlan; 	if (sdata->vif.type != NL80211_IFTYPE_AP)		return; 	/* crypto_tx_tailroom_needed_cnt is protected by this */	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	rcu_read_lock(); 	list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)		vlan->crypto_tx_tailroom_needed_cnt += delta; 	rcu_read_unlock();} static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata){	/*	 * When this count is zero, SKB resizing for allocating tailroom	 * for IV or MMIC is skipped. But, this check has created two race	 * cases in xmit path while transiting from zero count to one:	 *	 * 1. SKB resize was skipped because no key was added but just before	 * the xmit key is added and SW encryption kicks off.	 *	 * 2. SKB resize was skipped because all the keys were hw planted but	 * just before xmit one of the key is deleted and SW encryption kicks	 * off.	 *	 * In both the above case SW encryption will find not enough space for	 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)	 *	 * Solution has been explained at	 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net	 */ 	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	update_vlan_tailroom_need_count(sdata, 1); 	if (!sdata->crypto_tx_tailroom_needed_cnt++) {		/*		 * Flush all XMIT packets currently using HW encryption or no		 * encryption at all if the count transition is from 0 -> 1.		 */		synchronize_net();	}} static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,					 int delta){	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta); 	update_vlan_tailroom_need_count(sdata, -delta);	sdata->crypto_tx_tailroom_needed_cnt -= delta;} static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key){	struct ieee80211_sub_if_data *sdata = key->sdata;	struct sta_info *sta;	int ret = -EOPNOTSUPP; 	might_sleep();	lockdep_assert_wiphy(key->local->hw.wiphy); 	if (key->flags & KEY_FLAG_TAINTED) {		/* If we get here, it's during resume and the key is		 * tainted so shouldn't be used/programmed any more.		 * However, its flags may still indicate that it was		 * programmed into the device (since we're in resume)		 * so clear that flag now to avoid trying to remove		 * it again later.		 */		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&		    !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |					 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |					 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))			increment_tailroom_need_count(sdata); 		key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;		return -EINVAL;	} 	if (!key->local->ops->set_key)		goto out_unsupported; 	sta = key->sta; 	/*	 * Allow installation of a per-STA GTK if per-STA GTK is supported	 * by the driver or the interface is a NAN Data interface (as	 * per-station GTKs are required to be supported if secure NAN is	 * supported).	 */	if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&	    !(ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK) ||	      sdata->vif.type == NL80211_IFTYPE_NAN_DATA))		goto out_unsupported; 	if (sta && !sta->uploaded)		goto out_unsupported; 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {		/*		 * The driver doesn't know anything about VLAN interfaces.		 * Hence, don't send GTKs for VLAN interfaces to the driver.		 */		if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {			ret = 1;			goto out_unsupported;		}	} 	if (key->conf.link_id >= 0 && sdata->vif.active_links &&	    !(sdata->vif.active_links & BIT(key->conf.link_id)))		return 0; 	ret = drv_set_key(key->local, SET_KEY, sdata,			  sta ? &sta->sta : NULL, &key->conf); 	if (!ret) {		key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; 		if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |					 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |					 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))			decrease_tailroom_need_count(sdata, 1); 		WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&			(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)); 		WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&			(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)); 		return 0;	} 	if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)		sdata_err(sdata,			  "failed to set key (%d, %pM) to hardware (%d)\n",			  key->conf.keyidx,			  sta ? sta->sta.addr : bcast_addr, ret);  out_unsupported:	switch (key->conf.cipher) {	case WLAN_CIPHER_SUITE_WEP40:	case WLAN_CIPHER_SUITE_WEP104:	case WLAN_CIPHER_SUITE_TKIP:	case WLAN_CIPHER_SUITE_CCMP:	case WLAN_CIPHER_SUITE_CCMP_256:	case WLAN_CIPHER_SUITE_GCMP:	case WLAN_CIPHER_SUITE_GCMP_256:	case WLAN_CIPHER_SUITE_AES_CMAC:	case WLAN_CIPHER_SUITE_BIP_CMAC_256:	case WLAN_CIPHER_SUITE_BIP_GMAC_128:	case WLAN_CIPHER_SUITE_BIP_GMAC_256:		/* all of these we can do in software - if driver can */		if (ret == 1)			return 0;		if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))			return -EINVAL;		return 0;	default:		return -EINVAL;	}} static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key){	struct ieee80211_sub_if_data *sdata;	struct sta_info *sta;	int ret; 	might_sleep(); 	if (!key || !key->local->ops->set_key)		return; 	if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))		return; 	sta = key->sta;	sdata = key->sdata; 	lockdep_assert_wiphy(key->local->hw.wiphy); 	if (key->conf.link_id >= 0 && sdata->vif.active_links &&	    !(sdata->vif.active_links & BIT(key->conf.link_id)))		return; 	if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |				 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |				 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))		increment_tailroom_need_count(sdata); 	key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;	ret = drv_set_key(key->local, DISABLE_KEY, sdata,			  sta ? &sta->sta : NULL, &key->conf); 	if (ret)		sdata_err(sdata,			  "failed to remove key (%d, %pM) from hardware (%d)\n",			  key->conf.keyidx,			  sta ? sta->sta.addr : bcast_addr, ret);} static int _ieee80211_set_tx_key(struct ieee80211_key *key, bool force){	struct sta_info *sta = key->sta;	struct ieee80211_local *local = key->local; 	lockdep_assert_wiphy(local->hw.wiphy); 	set_sta_flag(sta, WLAN_STA_USES_ENCRYPTION); 	sta->ptk_idx = key->conf.keyidx; 	if (force || !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT))		clear_sta_flag(sta, WLAN_STA_BLOCK_BA);	ieee80211_check_fast_xmit(sta); 	return 0;} int ieee80211_set_tx_key(struct ieee80211_key *key){	return _ieee80211_set_tx_key(key, false);} static void ieee80211_pairwise_rekey(struct ieee80211_key *old,				     struct ieee80211_key *new){	struct ieee80211_local *local = new->local;	struct sta_info *sta = new->sta;	int i; 	lockdep_assert_wiphy(local->hw.wiphy); 	if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) {		/* Extended Key ID key install, initial one or rekey */ 		if (sta->ptk_idx != INVALID_PTK_KEYIDX &&		    !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) {			/* Aggregation Sessions with Extended Key ID must not			 * mix MPDUs with different keyIDs within one A-MPDU.			 * Tear down running Tx aggregation sessions and block			 * new Rx/Tx aggregation requests during rekey to			 * ensure there are no A-MPDUs when the driver is not			 * supporting A-MPDU key borders. (Blocking Tx only			 * would be sufficient but WLAN_STA_BLOCK_BA gets the			 * job done for the few ms we need it.)			 */			set_sta_flag(sta, WLAN_STA_BLOCK_BA);			for (i = 0; i <  IEEE80211_NUM_TIDS; i++)				__ieee80211_stop_tx_ba_session(sta, i,							       AGG_STOP_LOCAL_REQUEST);		}	} else if (old) {		/* Rekey without Extended Key ID.		 * Aggregation sessions are OK when running on SW crypto.		 * A broken remote STA may cause issues not observed with HW		 * crypto, though.		 */		if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))			return; 		/* Stop Tx till we are on the new key */		old->flags |= KEY_FLAG_TAINTED;		ieee80211_clear_fast_xmit(sta);		if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {			set_sta_flag(sta, WLAN_STA_BLOCK_BA);			ieee80211_sta_tear_down_BA_sessions(sta,							    AGG_STOP_LOCAL_REQUEST);		}		if (!wiphy_ext_feature_isset(local->hw.wiphy,					     NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {			pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",					    sta->sta.addr);			/* Flushing the driver queues *may* help prevent			 * the clear text leaks and freezes.			 */			ieee80211_flush_queues(local, old->sdata, false);		}	}} static void __ieee80211_set_default_key(struct ieee80211_link_data *link,					int idx, bool uni, bool multi){	struct ieee80211_sub_if_data *sdata = link->sdata;	struct ieee80211_key *key = NULL; 	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	if (idx >= 0 && idx < NUM_DEFAULT_KEYS) {		key = wiphy_dereference(sdata->local->hw.wiphy,					sdata->keys[idx]);		if (!key)			key = wiphy_dereference(sdata->local->hw.wiphy,						link->gtk[idx]);	} 	if (uni) {		rcu_assign_pointer(sdata->default_unicast_key, key);		ieee80211_check_fast_xmit_iface(sdata);		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)			drv_set_default_unicast_key(sdata->local, sdata, idx);	} 	if (multi)		rcu_assign_pointer(link->default_multicast_key, key); 	ieee80211_debugfs_key_update_default(sdata);} void ieee80211_set_default_key(struct ieee80211_link_data *link, int idx,			       bool uni, bool multi){	lockdep_assert_wiphy(link->sdata->local->hw.wiphy); 	__ieee80211_set_default_key(link, idx, uni, multi);} static void__ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link, int idx){	struct ieee80211_sub_if_data *sdata = link->sdata;	struct ieee80211_key *key = NULL; 	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	if (idx >= NUM_DEFAULT_KEYS &&	    idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)		key = wiphy_dereference(sdata->local->hw.wiphy,					link->gtk[idx]); 	rcu_assign_pointer(link->default_mgmt_key, key); 	ieee80211_debugfs_key_update_default(sdata);} void ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link,				    int idx){	lockdep_assert_wiphy(link->sdata->local->hw.wiphy); 	__ieee80211_set_default_mgmt_key(link, idx);} static void__ieee80211_set_default_beacon_key(struct ieee80211_link_data *link, int idx){	struct ieee80211_sub_if_data *sdata = link->sdata;	struct ieee80211_key *key = NULL; 	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	if (idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS &&	    idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +	    NUM_DEFAULT_BEACON_KEYS)		key = wiphy_dereference(sdata->local->hw.wiphy,					link->gtk[idx]); 	rcu_assign_pointer(link->default_beacon_key, key); 	ieee80211_debugfs_key_update_default(sdata);} void ieee80211_set_default_beacon_key(struct ieee80211_link_data *link,				      int idx){	lockdep_assert_wiphy(link->sdata->local->hw.wiphy); 	__ieee80211_set_default_beacon_key(link, idx);} static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,				 struct ieee80211_link_data *link,				 struct sta_info *sta,				 bool pairwise,				 struct ieee80211_key *old,				 struct ieee80211_key *new){	struct link_sta_info *link_sta = sta ? &sta->deflink : NULL;	int link_id;	int idx;	int ret = 0;	bool defunikey, defmultikey, defmgmtkey, defbeaconkey;	bool is_wep; 	lockdep_assert_wiphy(sdata->local->hw.wiphy); 	/* caller must provide at least one old/new */	if (WARN_ON(!new && !old))		return 0; 	if (new) {		idx = new->conf.keyidx;		is_wep = new->conf.cipher == WLAN_CIPHER_SUITE_WEP40 ||			 new->conf.cipher == WLAN_CIPHER_SUITE_WEP104;		link_id = new->conf.link_id;	} else {		idx = old->conf.keyidx;		is_wep = old->conf.cipher == WLAN_CIPHER_SUITE_WEP40 ||			 old->conf.cipher == WLAN_CIPHER_SUITE_WEP104;		link_id = old->conf.link_id;	} 	if (WARN(old && old->conf.link_id != link_id,		 "old link ID %d doesn't match new link ID %d\n",		 old->conf.link_id, link_id))		return -EINVAL; 	if (link_id >= 0) {		if (!link) {			link = sdata_dereference(sdata->link[link_id], sdata);			if (!link)				return -ENOLINK;		} 		if (sta) {			link_sta = rcu_dereference_protected(sta->link[link_id],							     lockdep_is_held(&sta->local->hw.wiphy->mtx));			if (!link_sta)				return -ENOLINK;		}	} else {		link = &sdata->deflink;	} 	if ((is_wep || pairwise) && idx >= NUM_DEFAULT_KEYS)		return -EINVAL; 	WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); 	if (new && sta && pairwise) {		/* Unicast rekey needs special handling. With Extended Key ID

Lines 1–500 of 1518.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t