File Index
Files and directories
Sign in
FILE

mlme.c

File preview with syntax highlighting.

linux-6.19.14/net/mac80211/mlme.c C
    1// SPDX-License-Identifier: GPL-2.0-only
    2/*
    3 * BSS client mode implementation
    4 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
    5 * Copyright 2004, Instant802 Networks, Inc.
    6 * Copyright 2005, Devicescape Software, Inc.
    7 * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
    8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
    9 * Copyright 2013-2014  Intel Mobile Communications GmbH
   10 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
   11 * Copyright (C) 2018 - 2026 Intel Corporation
   12 */
   13
   14#include <linux/delay.h>
   15#include <linux/fips.h>
   16#include <linux/if_ether.h>
   17#include <linux/skbuff.h>
   18#include <linux/if_arp.h>
   19#include <linux/etherdevice.h>
   20#include <linux/moduleparam.h>
   21#include <linux/rtnetlink.h>
   22#include <linux/crc32.h>
   23#include <linux/slab.h>
   24#include <linux/export.h>
   25#include <net/mac80211.h>
   26#include <linux/unaligned.h>
   27
   28#include "ieee80211_i.h"
   29#include "driver-ops.h"
   30#include "rate.h"
   31#include "led.h"
   32#include "fils_aead.h"
   33
   34#include <kunit/static_stub.h>
   35
   36#define IEEE80211_AUTH_TIMEOUT		(HZ / 5)
   37#define IEEE80211_AUTH_TIMEOUT_LONG	(HZ / 2)
   38#define IEEE80211_AUTH_TIMEOUT_SHORT	(HZ / 10)
   39#define IEEE80211_AUTH_TIMEOUT_SAE	(HZ * 2)
   40#define IEEE80211_AUTH_MAX_TRIES	3
   41#define IEEE80211_AUTH_WAIT_ASSOC	(HZ * 5)
   42#define IEEE80211_AUTH_WAIT_SAE_RETRY	(HZ * 2)
   43#define IEEE80211_ASSOC_TIMEOUT		(HZ / 5)
   44#define IEEE80211_ASSOC_TIMEOUT_LONG	(HZ / 2)
   45#define IEEE80211_ASSOC_TIMEOUT_SHORT	(HZ / 10)
   46#define IEEE80211_ASSOC_MAX_TRIES	3
   47
   48#define IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS (100 * USEC_PER_MSEC)
   49#define IEEE80211_ADV_TTLM_ST_UNDERFLOW 0xff00
   50
   51#define IEEE80211_NEG_TTLM_REQ_TIMEOUT (HZ / 5)
   52
   53static int max_nullfunc_tries = 2;
   54module_param(max_nullfunc_tries, int, 0644);
   55MODULE_PARM_DESC(max_nullfunc_tries,
   56		 "Maximum nullfunc tx tries before disconnecting (reason 4).");
   57
   58static int max_probe_tries = 5;
   59module_param(max_probe_tries, int, 0644);
   60MODULE_PARM_DESC(max_probe_tries,
   61		 "Maximum probe tries before disconnecting (reason 4).");
   62
   63/*
   64 * Beacon loss timeout is calculated as N frames times the
   65 * advertised beacon interval.  This may need to be somewhat
   66 * higher than what hardware might detect to account for
   67 * delays in the host processing frames. But since we also
   68 * probe on beacon miss before declaring the connection lost
   69 * default to what we want.
   70 */
   71static int beacon_loss_count = 7;
   72module_param(beacon_loss_count, int, 0644);
   73MODULE_PARM_DESC(beacon_loss_count,
   74		 "Number of beacon intervals before we decide beacon was lost.");
   75
   76/*
   77 * Time the connection can be idle before we probe
   78 * it to see if we can still talk to the AP.
   79 */
   80#define IEEE80211_CONNECTION_IDLE_TIME	(30 * HZ)
   81/*
   82 * Time we wait for a probe response after sending
   83 * a probe request because of beacon loss or for
   84 * checking the connection still works.
   85 */
   86static int probe_wait_ms = 500;
   87module_param(probe_wait_ms, int, 0644);
   88MODULE_PARM_DESC(probe_wait_ms,
   89		 "Maximum time(ms) to wait for probe response"
   90		 " before disconnecting (reason 4).");
   91
   92/*
   93 * How many Beacon frames need to have been used in average signal strength
   94 * before starting to indicate signal change events.
   95 */
   96#define IEEE80211_SIGNAL_AVE_MIN_COUNT	4
   97
   98/*
   99 * We can have multiple work items (and connection probing)
  100 * scheduling this timer, but we need to take care to only
  101 * reschedule it when it should fire _earlier_ than it was
  102 * asked for before, or if it's not pending right now. This
  103 * function ensures that. Note that it then is required to
  104 * run this function for all timeouts after the first one
  105 * has happened -- the work that runs from this timer will
  106 * do that.
  107 */
  108static void run_again(struct ieee80211_sub_if_data *sdata,
  109		      unsigned long timeout)
  110{
  111	lockdep_assert_wiphy(sdata->local->hw.wiphy);
  112
  113	if (!timer_pending(&sdata->u.mgd.timer) ||
  114	    time_before(timeout, sdata->u.mgd.timer.expires))
  115		mod_timer(&sdata->u.mgd.timer, timeout);
  116}
  117
  118void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  119{
  120	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
  121		return;
  122
  123	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  124		return;
  125
  126	mod_timer(&sdata->u.mgd.bcn_mon_timer,
  127		  round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  128}
  129
  130void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  131{
  132	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  133
  134	if (unlikely(!ifmgd->associated))
  135		return;
  136
  137	if (ifmgd->probe_send_count)
  138		ifmgd->probe_send_count = 0;
  139
  140	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  141		return;
  142
  143	mod_timer(&ifmgd->conn_mon_timer,
  144		  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  145}
  146
  147static int ecw2cw(int ecw)
  148{
  149	return (1 << ecw) - 1;
  150}
  151
  152static enum ieee80211_conn_mode
  153ieee80211_determine_ap_chan(struct ieee80211_sub_if_data *sdata,
  154			    struct ieee80211_channel *channel,
  155			    u32 vht_cap_info,
  156			    const struct ieee802_11_elems *elems,
  157			    bool ignore_ht_channel_mismatch,
  158			    const struct ieee80211_conn_settings *conn,
  159			    struct cfg80211_chan_def *chandef)
  160{
  161	const struct ieee80211_ht_operation *ht_oper = elems->ht_operation;
  162	const struct ieee80211_vht_operation *vht_oper = elems->vht_operation;
  163	const struct ieee80211_he_operation *he_oper = elems->he_operation;
  164	const struct ieee80211_eht_operation *eht_oper = elems->eht_operation;
  165	struct ieee80211_supported_band *sband =
  166		sdata->local->hw.wiphy->bands[channel->band];
  167	struct cfg80211_chan_def vht_chandef;
  168	bool no_vht = false;
  169	u32 ht_cfreq;
  170
  171	if (ieee80211_hw_check(&sdata->local->hw, STRICT))
  172		ignore_ht_channel_mismatch = false;
  173
  174	*chandef = (struct cfg80211_chan_def) {
  175		.chan = channel,
  176		.width = NL80211_CHAN_WIDTH_20_NOHT,
  177		.center_freq1 = channel->center_freq,
  178		.freq1_offset = channel->freq_offset,
  179	};
  180
  181	/* get special S1G case out of the way */
  182	if (sband->band == NL80211_BAND_S1GHZ) {
  183		if (!ieee80211_chandef_s1g_oper(sdata->local, elems->s1g_oper,
  184						chandef)) {
  185			/* Fallback to default 1MHz */
  186			chandef->width = NL80211_CHAN_WIDTH_1;
  187			chandef->s1g_primary_2mhz = false;
  188		}
  189
  190		return IEEE80211_CONN_MODE_S1G;
  191	}
  192
  193	/* get special 6 GHz case out of the way */
  194	if (sband->band == NL80211_BAND_6GHZ) {
  195		enum ieee80211_conn_mode mode = IEEE80211_CONN_MODE_EHT;
  196
  197		/* this is an error */
  198		if (conn->mode < IEEE80211_CONN_MODE_HE)
  199			return IEEE80211_CONN_MODE_LEGACY;
  200
  201		if (!elems->he_6ghz_capa || !elems->he_cap) {
  202			sdata_info(sdata,
  203				   "HE 6 GHz AP is missing HE/HE 6 GHz band capability\n");
  204			return IEEE80211_CONN_MODE_LEGACY;
  205		}
  206
  207		if (!eht_oper || !elems->eht_cap) {
  208			eht_oper = NULL;
  209			mode = IEEE80211_CONN_MODE_HE;
  210		}
  211
  212		if (!ieee80211_chandef_he_6ghz_oper(sdata->local, he_oper,
  213						    eht_oper, chandef)) {
  214			sdata_info(sdata, "bad HE/EHT 6 GHz operation\n");
  215			return IEEE80211_CONN_MODE_LEGACY;
  216		}
  217
  218		return mode;
  219	}
  220
  221	/* now we have the progression HT, VHT, ... */
  222	if (conn->mode < IEEE80211_CONN_MODE_HT)
  223		return IEEE80211_CONN_MODE_LEGACY;
  224
  225	if (!ht_oper || !elems->ht_cap_elem)
  226		return IEEE80211_CONN_MODE_LEGACY;
  227
  228	chandef->width = NL80211_CHAN_WIDTH_20;
  229
  230	ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  231						  channel->band);
  232	/* check that channel matches the right operating channel */
  233	if (!ignore_ht_channel_mismatch && channel->center_freq != ht_cfreq) {
  234		/*
  235		 * It's possible that some APs are confused here;
  236		 * Netgear WNDR3700 sometimes reports 4 higher than
  237		 * the actual channel in association responses, but
  238		 * since we look at probe response/beacon data here
  239		 * it should be OK.
  240		 */
  241		sdata_info(sdata,
  242			   "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  243			   channel->center_freq, ht_cfreq,
  244			   ht_oper->primary_chan, channel->band);
  245		return IEEE80211_CONN_MODE_LEGACY;
  246	}
  247
  248	ieee80211_chandef_ht_oper(ht_oper, chandef);
  249
  250	if (conn->mode < IEEE80211_CONN_MODE_VHT)
  251		return IEEE80211_CONN_MODE_HT;
  252
  253	vht_chandef = *chandef;
  254
  255	/*
  256	 * having he_cap/he_oper parsed out implies we're at
  257	 * least operating as HE STA
  258	 */
  259	if (elems->he_cap && he_oper &&
  260	    he_oper->he_oper_params & cpu_to_le32(IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
  261		struct ieee80211_vht_operation he_oper_vht_cap;
  262
  263		/*
  264		 * Set only first 3 bytes (other 2 aren't used in
  265		 * ieee80211_chandef_vht_oper() anyway)
  266		 */
  267		memcpy(&he_oper_vht_cap, he_oper->optional, 3);
  268		he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
  269
  270		if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
  271						&he_oper_vht_cap, ht_oper,
  272						&vht_chandef)) {
  273			sdata_info(sdata,
  274				   "HE AP VHT information is invalid, disabling HE\n");
  275			/* this will cause us to re-parse as VHT STA */
  276			return IEEE80211_CONN_MODE_VHT;
  277		}
  278	} else if (!vht_oper || !elems->vht_cap_elem) {
  279		if (sband->band == NL80211_BAND_5GHZ)
  280			return IEEE80211_CONN_MODE_HT;
  281		no_vht = true;
  282	} else if (sband->band == NL80211_BAND_2GHZ) {
  283		no_vht = true;
  284	} else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
  285					       vht_cap_info,
  286					       vht_oper, ht_oper,
  287					       &vht_chandef)) {
  288		sdata_info(sdata,
  289			   "AP VHT information is invalid, disabling VHT\n");
  290		return IEEE80211_CONN_MODE_HT;
  291	}
  292
  293	if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
  294		sdata_info(sdata,
  295			   "AP VHT information doesn't match HT, disabling VHT\n");
  296		return IEEE80211_CONN_MODE_HT;
  297	}
  298
  299	*chandef = vht_chandef;
  300
  301	/* stick to current max mode if we or the AP don't have HE */
  302	if (conn->mode < IEEE80211_CONN_MODE_HE ||
  303	    !elems->he_operation || !elems->he_cap) {
  304		if (no_vht)
  305			return IEEE80211_CONN_MODE_HT;
  306		return IEEE80211_CONN_MODE_VHT;
  307	}
  308
  309	/* stick to HE if we or the AP don't have EHT */
  310	if (conn->mode < IEEE80211_CONN_MODE_EHT ||
  311	    !eht_oper || !elems->eht_cap)
  312		return IEEE80211_CONN_MODE_HE;
  313
  314	/*
  315	 * handle the case that the EHT operation indicates that it holds EHT
  316	 * operation information (in case that the channel width differs from
  317	 * the channel width reported in HT/VHT/HE).
  318	 */
  319	if (eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT) {
  320		struct cfg80211_chan_def eht_chandef = *chandef;
  321
  322		ieee80211_chandef_eht_oper((const void *)eht_oper->optional,
  323					   &eht_chandef);
  324
  325		eht_chandef.punctured =
  326			ieee80211_eht_oper_dis_subchan_bitmap(eht_oper);
  327
  328		if (!cfg80211_chandef_valid(&eht_chandef)) {
  329			sdata_info(sdata,
  330				   "AP EHT information is invalid, disabling EHT\n");
  331			return IEEE80211_CONN_MODE_HE;
  332		}
  333
  334		if (!cfg80211_chandef_compatible(chandef, &eht_chandef)) {
  335			sdata_info(sdata,
  336				   "AP EHT information doesn't match HT/VHT/HE, disabling EHT\n");
  337			return IEEE80211_CONN_MODE_HE;
  338		}
  339
  340		*chandef = eht_chandef;
  341	}
  342
  343	return IEEE80211_CONN_MODE_EHT;
  344}
  345
  346static bool
  347ieee80211_verify_sta_ht_mcs_support(struct ieee80211_sub_if_data *sdata,
  348				    struct ieee80211_supported_band *sband,
  349				    const struct ieee80211_ht_operation *ht_op)
  350{
  351	struct ieee80211_sta_ht_cap sta_ht_cap;
  352	int i;
  353
  354	if (sband->band == NL80211_BAND_6GHZ)
  355		return true;
  356
  357	if (!ht_op)
  358		return false;
  359
  360	memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
  361	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
  362
  363	/*
  364	 * P802.11REVme/D7.0 - 6.5.4.2.4
  365	 * ...
  366	 * If the MLME of an HT STA receives an MLME-JOIN.request primitive
  367	 * with the SelectedBSS parameter containing a Basic HT-MCS Set field
  368	 * in the HT Operation parameter that contains any unsupported MCSs,
  369	 * the MLME response in the resulting MLME-JOIN.confirm primitive shall
  370	 * contain a ResultCode parameter that is not set to the value SUCCESS.
  371	 * ...
  372	 */
  373
  374	/* Simply check that all basic rates are in the STA RX mask */
  375	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  376		if ((ht_op->basic_set[i] & sta_ht_cap.mcs.rx_mask[i]) !=
  377		    ht_op->basic_set[i])
  378			return false;
  379	}
  380
  381	return true;
  382}
  383
  384static bool
  385ieee80211_verify_sta_vht_mcs_support(struct ieee80211_sub_if_data *sdata,
  386				     int link_id,
  387				     struct ieee80211_supported_band *sband,
  388				     const struct ieee80211_vht_operation *vht_op)
  389{
  390	struct ieee80211_sta_vht_cap sta_vht_cap;
  391	u16 ap_min_req_set, sta_rx_mcs_map, sta_tx_mcs_map;
  392	int nss;
  393
  394	if (sband->band != NL80211_BAND_5GHZ)
  395		return true;
  396
  397	if (!vht_op)
  398		return false;
  399
  400	memcpy(&sta_vht_cap, &sband->vht_cap, sizeof(sta_vht_cap));
  401	ieee80211_apply_vhtcap_overrides(sdata, &sta_vht_cap);
  402
  403	ap_min_req_set = le16_to_cpu(vht_op->basic_mcs_set);
  404	sta_rx_mcs_map = le16_to_cpu(sta_vht_cap.vht_mcs.rx_mcs_map);
  405	sta_tx_mcs_map = le16_to_cpu(sta_vht_cap.vht_mcs.tx_mcs_map);
  406
  407	/*
  408	 * Many APs are incorrectly advertising an all-zero value here,
  409	 * which really means MCS 0-7 are required for 1-8 streams, but
  410	 * they don't really mean it that way.
  411	 * Some other APs are incorrectly advertising 3 spatial streams
  412	 * with MCS 0-7 are required, but don't really mean it that way
  413	 * and we'll connect only with HT, rather than even HE.
  414	 * As a result, unfortunately the VHT basic MCS/NSS set cannot
  415	 * be used at all, so check it only in strict mode.
  416	 */
  417	if (!ieee80211_hw_check(&sdata->local->hw, STRICT))
  418		return true;
  419
  420	/*
  421	 * P802.11REVme/D7.0 - 6.5.4.2.4
  422	 * ...
  423	 * If the MLME of a VHT STA receives an MLME-JOIN.request primitive
  424	 * with a SelectedBSS parameter containing a Basic VHT-MCS And NSS Set
  425	 * field in the VHT Operation parameter that contains any unsupported
  426	 * <VHT-MCS, NSS> tuple, the MLME response in the resulting
  427	 * MLME-JOIN.confirm primitive shall contain a ResultCode parameter
  428	 * that is not set to the value SUCCESS.
  429	 * ...
  430	 */
  431	for (nss = 8; nss > 0; nss--) {
  432		u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
  433		u8 sta_rx_val;
  434		u8 sta_tx_val;
  435
  436		if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
  437			continue;
  438
  439		sta_rx_val = (sta_rx_mcs_map >> (2 * (nss - 1))) & 3;
  440		sta_tx_val = (sta_tx_mcs_map >> (2 * (nss - 1))) & 3;
  441
  442		if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  443		    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  444		    sta_rx_val < ap_op_val || sta_tx_val < ap_op_val) {
  445			link_id_info(sdata, link_id,
  446				     "Missing mandatory rates for %d Nss, rx %d, tx %d oper %d, disable VHT\n",
  447				     nss, sta_rx_val, sta_tx_val, ap_op_val);
  448			return false;
  449		}
  450	}
  451
  452	return true;
  453}
  454
  455static bool
  456ieee80211_verify_peer_he_mcs_support(struct ieee80211_sub_if_data *sdata,
  457				     int link_id,
  458				     const struct ieee80211_he_cap_elem *he_cap,
  459				     const struct ieee80211_he_operation *he_op)
  460{
  461	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
  462	u16 mcs_80_map_tx, mcs_80_map_rx;
  463	u16 ap_min_req_set;
  464	int nss;
  465
  466	if (!he_cap)
  467		return false;
  468
  469	/* mcs_nss is right after he_cap info */
  470	he_mcs_nss_supp = (void *)(he_cap + 1);
  471
  472	mcs_80_map_tx = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
  473	mcs_80_map_rx = le16_to_cpu(he_mcs_nss_supp->rx_mcs_80);
  474
  475	/* P802.11-REVme/D0.3
  476	 * 27.1.1 Introduction to the HE PHY
  477	 * ...
  478	 * An HE STA shall support the following features:
  479	 * ...
  480	 * Single spatial stream HE-MCSs 0 to 7 (transmit and receive) in all
  481	 * supported channel widths for HE SU PPDUs
  482	 */
  483	if ((mcs_80_map_tx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  484	    (mcs_80_map_rx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED) {
  485		link_id_info(sdata, link_id,
  486			     "Missing mandatory rates for 1 Nss, rx 0x%x, tx 0x%x, disable HE\n",
  487			     mcs_80_map_tx, mcs_80_map_rx);
  488		return false;
  489	}
  490
  491	if (!he_op)
  492		return true;
  493
  494	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
  495
  496	/*
  497	 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all
  498	 * zeroes, which is nonsense, and completely inconsistent with itself
  499	 * (it doesn't have 8 streams). Accept the settings in this case anyway.
  500	 */
  501	if (!ieee80211_hw_check(&sdata->local->hw, STRICT) && !ap_min_req_set)
  502		return true;
  503
  504	/* make sure the AP is consistent with itself
  505	 *
  506	 * P802.11-REVme/D0.3
  507	 * 26.17.1 Basic HE BSS operation
  508	 *
  509	 * A STA that is operating in an HE BSS shall be able to receive and
  510	 * transmit at each of the <HE-MCS, NSS> tuple values indicated by the
  511	 * Basic HE-MCS And NSS Set field of the HE Operation parameter of the
  512	 * MLME-START.request primitive and shall be able to receive at each of
  513	 * the <HE-MCS, NSS> tuple values indicated by the Supported HE-MCS and
  514	 * NSS Set field in the HE Capabilities parameter of the MLMESTART.request
  515	 * primitive
  516	 */
  517	for (nss = 8; nss > 0; nss--) {
  518		u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
  519		u8 ap_rx_val;
  520		u8 ap_tx_val;
  521
  522		if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
  523			continue;
  524
  525		ap_rx_val = (mcs_80_map_rx >> (2 * (nss - 1))) & 3;
  526		ap_tx_val = (mcs_80_map_tx >> (2 * (nss - 1))) & 3;
  527
  528		if (ap_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  529		    ap_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  530		    ap_rx_val < ap_op_val || ap_tx_val < ap_op_val) {
  531			link_id_info(sdata, link_id,
  532				     "Invalid rates for %d Nss, rx %d, tx %d oper %d, disable HE\n",
  533				     nss, ap_rx_val, ap_tx_val, ap_op_val);
  534			return false;
  535		}
  536	}
  537
  538	return true;
  539}
  540
  541static bool
  542ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata,
  543				    struct ieee80211_supported_band *sband,
  544				    const struct ieee80211_he_operation *he_op)
  545{
  546	const struct ieee80211_sta_he_cap *sta_he_cap =
  547		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
  548	u16 ap_min_req_set;
  549	int i;
  550
  551	if (!sta_he_cap || !he_op)
  552		return false;
  553
  554	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
  555
  556	/*
  557	 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all
  558	 * zeroes, which is nonsense, and completely inconsistent with itself
  559	 * (it doesn't have 8 streams). Accept the settings in this case anyway.
  560	 */
  561	if (!ieee80211_hw_check(&sdata->local->hw, STRICT) && !ap_min_req_set)
  562		return true;
  563
  564	/* Need to go over for 80MHz, 160MHz and for 80+80 */
  565	for (i = 0; i < 3; i++) {
  566		const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
  567			&sta_he_cap->he_mcs_nss_supp;
  568		u16 sta_mcs_map_rx =
  569			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
  570		u16 sta_mcs_map_tx =
  571			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
  572		u8 nss;
  573		bool verified = true;
  574
  575		/*
  576		 * For each band there is a maximum of 8 spatial streams
  577		 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
  578		 * of 2 bits per NSS (1-8), with the values defined in enum
  579		 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
  580		 * capabilities aren't less than the AP's minimum requirements
  581		 * for this HE BSS per SS.
  582		 * It is enough to find one such band that meets the reqs.
  583		 */
  584		for (nss = 8; nss > 0; nss--) {
  585			u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
  586			u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
  587			u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
  588
  589			if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
  590				continue;
  591
  592			/*
  593			 * Make sure the HE AP doesn't require MCSs that aren't
  594			 * supported by the client as required by spec
  595			 *
  596			 * P802.11-REVme/D0.3
  597			 * 26.17.1 Basic HE BSS operation
  598			 *
  599			 * An HE STA shall not attempt to join * (MLME-JOIN.request primitive)
  600			 * a BSS, unless it supports (i.e., is able to both transmit and
  601			 * receive using) all of the <HE-MCS, NSS> tuples in the basic
  602			 * HE-MCS and NSS set.
  603			 */
  604			if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  605			    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
  606			    (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
  607				verified = false;
  608				break;
  609			}
  610		}
  611
  612		if (verified)
  613			return true;
  614	}
  615
  616	/* If here, STA doesn't meet AP's HE min requirements */
  617	return false;
  618}
  619
  620static u8
  621ieee80211_get_eht_cap_mcs_nss(const struct ieee80211_sta_he_cap *sta_he_cap,
  622			      const struct ieee80211_sta_eht_cap *sta_eht_cap,
  623			      unsigned int idx, int bw)
  624{
  625	u8 he_phy_cap0 = sta_he_cap->he_cap_elem.phy_cap_info[0];
  626	u8 eht_phy_cap0 = sta_eht_cap->eht_cap_elem.phy_cap_info[0];
  627
  628	/* handle us being a 20 MHz-only EHT STA - with four values
  629	 * for MCS 0-7, 8-9, 10-11, 12-13.
  630	 */
  631	if (!(he_phy_cap0 & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL))
  632		return sta_eht_cap->eht_mcs_nss_supp.only_20mhz.rx_tx_max_nss[idx];
  633
  634	/* the others have MCS 0-9 together, rather than separately from 0-7 */
  635	if (idx > 0)
  636		idx--;
  637
  638	switch (bw) {
  639	case 0:
  640		return sta_eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_max_nss[idx];
  641	case 1:
  642		if (!(he_phy_cap0 &
  643		      (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  644		       IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)))
  645			return 0xff; /* pass check */
  646		return sta_eht_cap->eht_mcs_nss_supp.bw._160.rx_tx_max_nss[idx];
  647	case 2:
  648		if (!(eht_phy_cap0 & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ))
  649			return 0xff; /* pass check */
  650		return sta_eht_cap->eht_mcs_nss_supp.bw._320.rx_tx_max_nss[idx];
  651	}
  652
  653	WARN_ON(1);
  654	return 0;
  655}
  656
  657static bool
  658ieee80211_verify_sta_eht_mcs_support(struct ieee80211_sub_if_data *sdata,
  659				     struct ieee80211_supported_band *sband,
  660				     const struct ieee80211_eht_operation *eht_op)
  661{
  662	const struct ieee80211_sta_he_cap *sta_he_cap =
  663		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
  664	const struct ieee80211_sta_eht_cap *sta_eht_cap =
  665		ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
  666	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *req;
  667	unsigned int i;
  668
  669	if (!sta_he_cap || !sta_eht_cap || !eht_op)
  670		return false;
  671
  672	req = &eht_op->basic_mcs_nss;
  673
  674	for (i = 0; i < ARRAY_SIZE(req->rx_tx_max_nss); i++) {
  675		u8 req_rx_nss, req_tx_nss;
  676		unsigned int bw;
  677
  678		req_rx_nss = u8_get_bits(req->rx_tx_max_nss[i],
  679					 IEEE80211_EHT_MCS_NSS_RX);
  680		req_tx_nss = u8_get_bits(req->rx_tx_max_nss[i],
  681					 IEEE80211_EHT_MCS_NSS_TX);
  682
  683		for (bw = 0; bw < 3; bw++) {
  684			u8 have, have_rx_nss, have_tx_nss;
  685
  686			have = ieee80211_get_eht_cap_mcs_nss(sta_he_cap,
  687							     sta_eht_cap,
  688							     i, bw);
  689			have_rx_nss = u8_get_bits(have,
  690						  IEEE80211_EHT_MCS_NSS_RX);
  691			have_tx_nss = u8_get_bits(have,
  692						  IEEE80211_EHT_MCS_NSS_TX);
  693
  694			if (req_rx_nss > have_rx_nss ||
  695			    req_tx_nss > have_tx_nss)
  696				return false;
  697		}
  698	}
  699
  700	return true;
  701}
  702
  703static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  704				const u8 *supp_rates,
  705				unsigned int supp_rates_len,
  706				const u8 *ext_supp_rates,
  707				unsigned int ext_supp_rates_len,
  708				u32 *rates, u32 *basic_rates,
  709				unsigned long *unknown_rates_selectors,
  710				bool *have_higher_than_11mbit,
  711				int *min_rate, int *min_rate_index)
  712{
  713	int i, j;
  714
  715	for (i = 0; i < supp_rates_len + ext_supp_rates_len; i++) {
  716		u8 supp_rate = i < supp_rates_len ?
  717				supp_rates[i] :
  718				ext_supp_rates[i - supp_rates_len];
  719		int rate = supp_rate & 0x7f;
  720		bool is_basic = !!(supp_rate & 0x80);
  721
  722		if ((rate * 5) > 110 && have_higher_than_11mbit)
  723			*have_higher_than_11mbit = true;
  724
  725		/*
  726		 * Skip membership selectors since they're not rates.
  727		 *
  728		 * Note: Even though the membership selector and the basic
  729		 *	 rate flag share the same bit, they are not exactly
  730		 *	 the same.
  731		 */
  732		if (is_basic && rate >= BSS_MEMBERSHIP_SELECTOR_MIN) {
  733			if (unknown_rates_selectors)
  734				set_bit(rate, unknown_rates_selectors);
  735			continue;
  736		}
  737
  738		for (j = 0; j < sband->n_bitrates; j++) {
  739			struct ieee80211_rate *br;
  740			int brate;
  741
  742			br = &sband->bitrates[j];
  743
  744			brate = DIV_ROUND_UP(br->bitrate, 5);
  745			if (brate == rate) {
  746				if (rates)
  747					*rates |= BIT(j);
  748				if (is_basic && basic_rates)
  749					*basic_rates |= BIT(j);
  750				if (min_rate && (rate * 5) < *min_rate) {
  751					*min_rate = rate * 5;
  752					if (min_rate_index)
  753						*min_rate_index = j;
  754				}
  755				break;
  756			}
  757		}
  758
  759		/* Handle an unknown entry as if it is an unknown selector */
  760		if (is_basic && unknown_rates_selectors && j == sband->n_bitrates)
  761			set_bit(rate, unknown_rates_selectors);
  762	}
  763}
  764
  765static bool ieee80211_chandef_usable(struct ieee80211_sub_if_data *sdata,
  766				     const struct cfg80211_chan_def *chandef,
  767				     u32 prohibited_flags)
  768{
  769	if (!cfg80211_chandef_usable(sdata->local->hw.wiphy,
  770				     chandef, prohibited_flags))
  771		return false;
  772
  773	if (chandef->punctured &&
  774	    ieee80211_hw_check(&sdata->local->hw, DISALLOW_PUNCTURING))
  775		return false;
  776
  777	return true;
  778}
  779
  780static int ieee80211_chandef_num_subchans(const struct cfg80211_chan_def *c)
  781{
  782	if (c->width == NL80211_CHAN_WIDTH_80P80)
  783		return 4 + 4;
  784
  785	return cfg80211_chandef_get_width(c) / 20;
  786}
  787
  788static int ieee80211_chandef_num_widths(const struct cfg80211_chan_def *c)
  789{
  790	switch (c->width) {
  791	case NL80211_CHAN_WIDTH_20:
  792	case NL80211_CHAN_WIDTH_20_NOHT:
  793		return 1;
  794	case NL80211_CHAN_WIDTH_40:
  795		return 2;
  796	case NL80211_CHAN_WIDTH_80P80:
  797	case NL80211_CHAN_WIDTH_80:
  798		return 3;
  799	case NL80211_CHAN_WIDTH_160:
  800		return 4;
  801	case NL80211_CHAN_WIDTH_320:
  802		return 5;
  803	default:
  804		WARN_ON(1);
  805		return 0;
  806	}
  807}
  808
  809VISIBLE_IF_MAC80211_KUNIT int
  810ieee80211_calc_chandef_subchan_offset(const struct cfg80211_chan_def *ap,
  811				      u8 n_partial_subchans)
  812{
  813	int n = ieee80211_chandef_num_subchans(ap);
  814	struct cfg80211_chan_def tmp = *ap;
  815	int offset = 0;
  816
  817	/*
  818	 * Given a chandef (in this context, it's the AP's) and a number
  819	 * of subchannels that we want to look at ('n_partial_subchans'),
  820	 * calculate the offset in number of subchannels between the full
  821	 * and the subset with the desired width.
  822	 */
  823
  824	/* same number of subchannels means no offset, obviously */
  825	if (n == n_partial_subchans)
  826		return 0;
  827
  828	/* don't WARN - misconfigured APs could cause this if their N > width */
  829	if (n < n_partial_subchans)
  830		return 0;
  831
  832	while (ieee80211_chandef_num_subchans(&tmp) > n_partial_subchans) {
  833		u32 prev = tmp.center_freq1;
  834
  835		ieee80211_chandef_downgrade(&tmp, NULL);
  836
  837		/*
  838		 * if center_freq moved up, half the original channels
  839		 * are gone now but were below, so increase offset
  840		 */
  841		if (prev < tmp.center_freq1)
  842			offset += ieee80211_chandef_num_subchans(&tmp);
  843	}
  844
  845	/*
  846	 * 80+80 with secondary 80 below primary - four subchannels for it
  847	 * (we cannot downgrade *to* 80+80, so no need to consider 'tmp')
  848	 */
  849	if (ap->width == NL80211_CHAN_WIDTH_80P80 &&
  850	    ap->center_freq2 < ap->center_freq1)
  851		offset += 4;
  852
  853	return offset;
  854}
  855EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_calc_chandef_subchan_offset);
  856
  857VISIBLE_IF_MAC80211_KUNIT void
  858ieee80211_rearrange_tpe_psd(struct ieee80211_parsed_tpe_psd *psd,
  859			    const struct cfg80211_chan_def *ap,
  860			    const struct cfg80211_chan_def *used)
  861{
  862	u8 needed = ieee80211_chandef_num_subchans(used);
  863	u8 have = ieee80211_chandef_num_subchans(ap);
  864	u8 tmp[IEEE80211_TPE_PSD_ENTRIES_320MHZ];
  865	u8 offset;
  866
  867	if (!psd->valid)
  868		return;
  869
  870	/* if N is zero, all defaults were used, no point in rearranging */
  871	if (!psd->n)
  872		goto out;
  873
  874	BUILD_BUG_ON(sizeof(tmp) != sizeof(psd->power));
  875
  876	/*
  877	 * This assumes that 'N' is consistent with the HE channel, as
  878	 * it should be (otherwise the AP is broken).
  879	 *
  880	 * In psd->power we have values in the order 0..N, 0..K, where
  881	 * N+K should cover the entire channel per 'ap', but even if it
  882	 * doesn't then we've pre-filled 'unlimited' as defaults.
  883	 *
  884	 * But this is all the wrong order, we want to have them in the
  885	 * order of the 'used' channel.
  886	 *
  887	 * So for example, we could have a 320 MHz EHT AP, which has the
  888	 * HE channel as 80 MHz (e.g. due to puncturing, which doesn't
  889	 * seem to be considered for the TPE), as follows:
  890	 *
  891	 * EHT  320:   |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  892	 * HE    80:                           |  |  |  |  |
  893	 * used 160:                           |  |  |  |  |  |  |  |  |
  894	 *
  895	 * N entries:                          |--|--|--|--|
  896	 * K entries:  |--|--|--|--|--|--|--|--|           |--|--|--|--|
  897	 * power idx:   4  5  6  7  8  9  10 11 0  1  2  3  12 13 14 15
  898	 * full chan:   0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
  899	 * used chan:                           0  1  2  3  4  5  6  7
  900	 *
  901	 * The idx in the power array ('power idx') is like this since it
  902	 * comes directly from the element's N and K entries in their
  903	 * element order, and those are this way for HE compatibility.
  904	 *
  905	 * Rearrange them as desired here, first by putting them into the
  906	 * 'full chan' order, and then selecting the necessary subset for
  907	 * the 'used chan'.
  908	 */
  909
  910	/* first reorder according to AP channel */
  911	offset = ieee80211_calc_chandef_subchan_offset(ap, psd->n);
  912	for (int i = 0; i < have; i++) {
  913		if (i < offset)
  914			tmp[i] = psd->power[i + psd->n];
  915		else if (i < offset + psd->n)
  916			tmp[i] = psd->power[i - offset];
  917		else
  918			tmp[i] = psd->power[i];
  919	}
  920
  921	/*
  922	 * and then select the subset for the used channel
  923	 * (set everything to defaults first in case a driver is confused)
  924	 */
  925	memset(psd->power, IEEE80211_TPE_PSD_NO_LIMIT, sizeof(psd->power));
  926	offset = ieee80211_calc_chandef_subchan_offset(ap, needed);
  927	for (int i = 0; i < needed; i++)
  928		psd->power[i] = tmp[offset + i];
  929
  930out:
  931	/* limit, but don't lie if there are defaults in the data */
  932	if (needed < psd->count)
  933		psd->count = needed;
  934}
  935EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_rearrange_tpe_psd);
  936
  937static void ieee80211_rearrange_tpe(struct ieee80211_parsed_tpe *tpe,
  938				    const struct cfg80211_chan_def *ap,
  939				    const struct cfg80211_chan_def *used)
  940{
  941	/* ignore this completely for narrow/invalid channels */
  942	if (!ieee80211_chandef_num_subchans(ap) ||
  943	    !ieee80211_chandef_num_subchans(used)) {
  944		ieee80211_clear_tpe(tpe);
  945		return;
  946	}
  947
  948	for (int i = 0; i < 2; i++) {
  949		int needed_pwr_count;
  950
  951		ieee80211_rearrange_tpe_psd(&tpe->psd_local[i], ap, used);
  952		ieee80211_rearrange_tpe_psd(&tpe->psd_reg_client[i], ap, used);
  953
  954		/* limit this to the widths we actually need */
  955		needed_pwr_count = ieee80211_chandef_num_widths(used);
  956		if (needed_pwr_count < tpe->max_local[i].count)
  957			tpe->max_local[i].count = needed_pwr_count;
  958		if (needed_pwr_count < tpe->max_reg_client[i].count)
  959			tpe->max_reg_client[i].count = needed_pwr_count;
  960	}
  961}
  962
  963/*
  964 * The AP part of the channel request is used to distinguish settings
  965 * to the device used for wider bandwidth OFDMA. This is used in the
  966 * channel context code to assign two channel contexts even if they're
  967 * both for the same channel, if the AP bandwidths are incompatible.
  968 * If not EHT (or driver override) then ap.chan == NULL indicates that
  969 * there's no wider BW OFDMA used.
  970 */
  971static void ieee80211_set_chanreq_ap(struct ieee80211_sub_if_data *sdata,
  972				     struct ieee80211_chan_req *chanreq,
  973				     struct ieee80211_conn_settings *conn,
  974				     struct cfg80211_chan_def *ap_chandef)
  975{
  976	chanreq->ap.chan = NULL;
  977
  978	if (conn->mode < IEEE80211_CONN_MODE_EHT)
  979		return;
  980	if (sdata->vif.driver_flags & IEEE80211_VIF_IGNORE_OFDMA_WIDER_BW)
  981		return;
  982
  983	chanreq->ap = *ap_chandef;
  984}
  985
  986VISIBLE_IF_MAC80211_KUNIT struct ieee802_11_elems *
  987ieee80211_determine_chan_mode(struct ieee80211_sub_if_data *sdata,
  988			      struct ieee80211_conn_settings *conn,
  989			      struct cfg80211_bss *cbss, int link_id,
  990			      struct ieee80211_chan_req *chanreq,
  991			      struct cfg80211_chan_def *ap_chandef,
  992			      unsigned long *userspace_selectors)
  993{
  994	const struct cfg80211_bss_ies *ies = rcu_dereference(cbss->ies);
  995	struct ieee80211_bss *bss = (void *)cbss->priv;
  996	struct ieee80211_channel *channel = cbss->channel;
  997	struct ieee80211_elems_parse_params parse_params = {
  998		.link_id = -1,
  999		.from_ap = true,
 1000		.start = ies->data,
 1001		.len = ies->len,
 1002		.type = ies->from_beacon ?
 1003			IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON :
 1004			IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_RESP,
 1005	};
 1006	struct ieee802_11_elems *elems;
 1007	struct ieee80211_supported_band *sband;
 1008	enum ieee80211_conn_mode ap_mode;
 1009	unsigned long unknown_rates_selectors[BITS_TO_LONGS(128)] = {};
 1010	unsigned long sta_selectors[BITS_TO_LONGS(128)] = {};
 1011	int ret;
 1012
 1013again:
 1014	parse_params.mode = conn->mode;
 1015	elems = ieee802_11_parse_elems_full(&parse_params);
 1016	if (!elems)
 1017		return ERR_PTR(-ENOMEM);
 1018
 1019	ap_mode = ieee80211_determine_ap_chan(sdata, channel, bss->vht_cap_info,
 1020					      elems, false, conn, ap_chandef);
 1021
 1022	/* this should be impossible since parsing depends on our mode */
 1023	if (WARN_ON(ap_mode > conn->mode)) {
 1024		ret = -EINVAL;
 1025		goto free;
 1026	}
 1027
 1028	if (conn->mode != ap_mode) {
 1029		conn->mode = ap_mode;
 1030		kfree(elems);
 1031		goto again;
 1032	}
 1033
 1034	mlme_link_id_dbg(sdata, link_id, "determined AP %pM to be %s\n",
 1035			 cbss->bssid, ieee80211_conn_mode_str(ap_mode));
 1036
 1037	sband = sdata->local->hw.wiphy->bands[channel->band];
 1038
 1039	ieee80211_get_rates(sband, elems->supp_rates, elems->supp_rates_len,
 1040			    elems->ext_supp_rates, elems->ext_supp_rates_len,
 1041			    NULL, NULL, unknown_rates_selectors, NULL, NULL,
 1042			    NULL);
 1043
 1044	switch (channel->band) {
 1045	case NL80211_BAND_S1GHZ:
 1046		if (WARN_ON(ap_mode != IEEE80211_CONN_MODE_S1G)) {
 1047			ret = -EINVAL;
 1048			goto free;
 1049		}
 1050
 1051		chanreq->oper = *ap_chandef;
 1052		if (!cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
 1053					     IEEE80211_CHAN_DISABLED)) {
 1054			ret = -EINVAL;
 1055			goto free;
 1056		}
 1057
 1058		return elems;
 1059	case NL80211_BAND_6GHZ:
 1060		if (ap_mode < IEEE80211_CONN_MODE_HE) {
 1061			link_id_info(sdata, link_id,
 1062				     "Rejecting non-HE 6/7 GHz connection");
 1063			ret = -EINVAL;
 1064			goto free;
 1065		}
 1066		break;
 1067	default:
 1068		if (WARN_ON(ap_mode == IEEE80211_CONN_MODE_S1G)) {
 1069			ret = -EINVAL;
 1070			goto free;
 1071		}
 1072	}
 1073
 1074	switch (ap_mode) {
 1075	case IEEE80211_CONN_MODE_S1G:
 1076		WARN_ON(1);
 1077		ret = -EINVAL;
 1078		goto free;
 1079	case IEEE80211_CONN_MODE_LEGACY:
 1080		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
 1081		break;
 1082	case IEEE80211_CONN_MODE_HT:
 1083		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1084				       conn->bw_limit,
 1085				       IEEE80211_CONN_BW_LIMIT_40);
 1086		break;
 1087	case IEEE80211_CONN_MODE_VHT:
 1088	case IEEE80211_CONN_MODE_HE:
 1089		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1090				       conn->bw_limit,
 1091				       IEEE80211_CONN_BW_LIMIT_160);
 1092		break;
 1093	case IEEE80211_CONN_MODE_EHT:
 1094		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1095				       conn->bw_limit,
 1096				       IEEE80211_CONN_BW_LIMIT_320);
 1097		break;
 1098	}
 1099
 1100	chanreq->oper = *ap_chandef;
 1101
 1102	bitmap_copy(sta_selectors, userspace_selectors, 128);
 1103	if (conn->mode >= IEEE80211_CONN_MODE_HT)
 1104		set_bit(BSS_MEMBERSHIP_SELECTOR_HT_PHY, sta_selectors);
 1105	if (conn->mode >= IEEE80211_CONN_MODE_VHT)
 1106		set_bit(BSS_MEMBERSHIP_SELECTOR_VHT_PHY, sta_selectors);
 1107	if (conn->mode >= IEEE80211_CONN_MODE_HE)
 1108		set_bit(BSS_MEMBERSHIP_SELECTOR_HE_PHY, sta_selectors);
 1109	if (conn->mode >= IEEE80211_CONN_MODE_EHT)
 1110		set_bit(BSS_MEMBERSHIP_SELECTOR_EHT_PHY, sta_selectors);
 1111
 1112	/*
 1113	 * We do not support EPD or GLK so never add them.
 1114	 * SAE_H2E is handled through userspace_selectors.
 1115	 */
 1116
 1117	/* Check if we support all required features */
 1118	if (!bitmap_subset(unknown_rates_selectors, sta_selectors, 128)) {
 1119		link_id_info(sdata, link_id,
 1120			     "required basic rate or BSS membership selectors not supported or disabled, rejecting connection\n");
 1121		ret = -EINVAL;
 1122		goto free;
 1123	}
 1124
 1125	ieee80211_set_chanreq_ap(sdata, chanreq, conn, ap_chandef);
 1126
 1127	while (!ieee80211_chandef_usable(sdata, &chanreq->oper,
 1128					 IEEE80211_CHAN_DISABLED)) {
 1129		if (chanreq->oper.width == NL80211_CHAN_WIDTH_20_NOHT) {
 1130			link_id_info(sdata, link_id,
 1131				     "unusable channel (%d MHz) for connection\n",
 1132				     chanreq->oper.chan->center_freq);
 1133			ret = -EINVAL;
 1134			goto free;
 1135		}
 1136
 1137		ieee80211_chanreq_downgrade(chanreq, conn);
 1138	}
 1139
 1140	if (conn->mode >= IEEE80211_CONN_MODE_HE &&
 1141	    !cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
 1142				     IEEE80211_CHAN_NO_HE)) {
 1143		conn->mode = IEEE80211_CONN_MODE_VHT;
 1144		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1145				       conn->bw_limit,
 1146				       IEEE80211_CONN_BW_LIMIT_160);
 1147	}
 1148
 1149	if (conn->mode >= IEEE80211_CONN_MODE_EHT &&
 1150	    !cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
 1151				     IEEE80211_CHAN_NO_EHT)) {
 1152		conn->mode = IEEE80211_CONN_MODE_HE;
 1153		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1154				       conn->bw_limit,
 1155				       IEEE80211_CONN_BW_LIMIT_160);
 1156	}
 1157
 1158	if (chanreq->oper.width != ap_chandef->width || ap_mode != conn->mode)
 1159		link_id_info(sdata, link_id,
 1160			     "regulatory prevented using AP config, downgraded\n");
 1161
 1162	if (conn->mode >= IEEE80211_CONN_MODE_HT &&
 1163	    !ieee80211_verify_sta_ht_mcs_support(sdata, sband,
 1164						 elems->ht_operation)) {
 1165		conn->mode = IEEE80211_CONN_MODE_LEGACY;
 1166		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
 1167		link_id_info(sdata, link_id,
 1168			     "required MCSes not supported, disabling HT\n");
 1169	}
 1170
 1171	if (conn->mode >= IEEE80211_CONN_MODE_VHT &&
 1172	    !ieee80211_verify_sta_vht_mcs_support(sdata, link_id, sband,
 1173						  elems->vht_operation)) {
 1174		conn->mode = IEEE80211_CONN_MODE_HT;
 1175		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1176				       conn->bw_limit,
 1177				       IEEE80211_CONN_BW_LIMIT_40);
 1178		link_id_info(sdata, link_id,
 1179			     "required MCSes not supported, disabling VHT\n");
 1180	}
 1181
 1182	if (conn->mode >= IEEE80211_CONN_MODE_HE &&
 1183	    (!ieee80211_verify_peer_he_mcs_support(sdata, link_id,
 1184						   (void *)elems->he_cap,
 1185						   elems->he_operation) ||
 1186	     !ieee80211_verify_sta_he_mcs_support(sdata, sband,
 1187						  elems->he_operation))) {
 1188		conn->mode = IEEE80211_CONN_MODE_VHT;
 1189		link_id_info(sdata, link_id,
 1190			     "required MCSes not supported, disabling HE\n");
 1191	}
 1192
 1193	if (conn->mode >= IEEE80211_CONN_MODE_EHT &&
 1194	    !ieee80211_verify_sta_eht_mcs_support(sdata, sband,
 1195						  elems->eht_operation)) {
 1196		conn->mode = IEEE80211_CONN_MODE_HE;
 1197		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 1198				       conn->bw_limit,
 1199				       IEEE80211_CONN_BW_LIMIT_160);
 1200		link_id_info(sdata, link_id,
 1201			     "required MCSes not supported, disabling EHT\n");
 1202	}
 1203
 1204	if (conn->mode >= IEEE80211_CONN_MODE_EHT &&
 1205	    channel->band != NL80211_BAND_2GHZ &&
 1206	    conn->bw_limit == IEEE80211_CONN_BW_LIMIT_40) {
 1207		conn->mode = IEEE80211_CONN_MODE_HE;
 1208		link_id_info(sdata, link_id,
 1209			     "required bandwidth not supported, disabling EHT\n");
 1210	}
 1211
 1212	/* the mode can only decrease, so this must terminate */
 1213	if (ap_mode != conn->mode) {
 1214		kfree(elems);
 1215		goto again;
 1216	}
 1217
 1218	mlme_link_id_dbg(sdata, link_id,
 1219			 "connecting with %s mode, max bandwidth %d MHz\n",
 1220			 ieee80211_conn_mode_str(conn->mode),
 1221			 20 * (1 << conn->bw_limit));
 1222
 1223	if (WARN_ON_ONCE(!cfg80211_chandef_valid(&chanreq->oper))) {
 1224		ret = -EINVAL;
 1225		goto free;
 1226	}
 1227
 1228	return elems;
 1229free:
 1230	kfree(elems);
 1231	return ERR_PTR(ret);
 1232}
 1233EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_determine_chan_mode);
 1234
 1235static int ieee80211_config_bw(struct ieee80211_link_data *link,
 1236			       struct ieee802_11_elems *elems,
 1237			       bool update, u64 *changed, u16 stype)
 1238{
 1239	struct ieee80211_channel *channel = link->conf->chanreq.oper.chan;
 1240	struct ieee80211_sub_if_data *sdata = link->sdata;
 1241	struct ieee80211_chan_req chanreq = {};
 1242	struct cfg80211_chan_def ap_chandef;
 1243	enum ieee80211_conn_mode ap_mode;
 1244	const char *frame;
 1245	u32 vht_cap_info = 0;
 1246	u16 ht_opmode;
 1247	int ret;
 1248
 1249	switch (stype) {
 1250	case IEEE80211_STYPE_BEACON:
 1251		frame = "beacon";
 1252		break;
 1253	case IEEE80211_STYPE_ASSOC_RESP:
 1254		frame = "assoc response";
 1255		break;
 1256	case IEEE80211_STYPE_REASSOC_RESP:
 1257		frame = "reassoc response";
 1258		break;
 1259	case IEEE80211_STYPE_ACTION:
 1260		/* the only action frame that gets here */
 1261		frame = "ML reconf response";
 1262		break;
 1263	default:
 1264		return -EINVAL;
 1265	}
 1266
 1267	/* don't track any bandwidth changes in legacy/S1G modes */
 1268	if (link->u.mgd.conn.mode == IEEE80211_CONN_MODE_LEGACY ||
 1269	    link->u.mgd.conn.mode == IEEE80211_CONN_MODE_S1G)
 1270		return 0;
 1271
 1272	if (elems->vht_cap_elem)
 1273		vht_cap_info = le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
 1274
 1275	ap_mode = ieee80211_determine_ap_chan(sdata, channel, vht_cap_info,
 1276					      elems, true, &link->u.mgd.conn,
 1277					      &ap_chandef);
 1278
 1279	if (ap_mode != link->u.mgd.conn.mode) {
 1280		link_info(link,
 1281			  "AP %pM appears to change mode (expected %s, found %s) in %s, disconnect\n",
 1282			  link->u.mgd.bssid,
 1283			  ieee80211_conn_mode_str(link->u.mgd.conn.mode),
 1284			  ieee80211_conn_mode_str(ap_mode), frame);
 1285		return -EINVAL;
 1286	}
 1287
 1288	chanreq.oper = ap_chandef;
 1289	ieee80211_set_chanreq_ap(sdata, &chanreq, &link->u.mgd.conn,
 1290				 &ap_chandef);
 1291
 1292	/*
 1293	 * if HT operation mode changed store the new one -
 1294	 * this may be applicable even if channel is identical
 1295	 */
 1296	if (elems->ht_operation) {
 1297		ht_opmode = le16_to_cpu(elems->ht_operation->operation_mode);
 1298		if (link->conf->ht_operation_mode != ht_opmode) {
 1299			*changed |= BSS_CHANGED_HT;
 1300			link->conf->ht_operation_mode = ht_opmode;
 1301		}
 1302	}
 1303
 1304	/*
 1305	 * Downgrade the new channel if we associated with restricted
 1306	 * bandwidth capabilities. For example, if we associated as a
 1307	 * 20 MHz STA to a 40 MHz AP (due to regulatory, capabilities
 1308	 * or config reasons) then switching to a 40 MHz channel now
 1309	 * won't do us any good -- we couldn't use it with the AP.
 1310	 */
 1311	while (link->u.mgd.conn.bw_limit <
 1312			ieee80211_min_bw_limit_from_chandef(&chanreq.oper))
 1313		ieee80211_chandef_downgrade(&chanreq.oper, NULL);
 1314
 1315	/* TPE element is not present in (re)assoc/ML reconfig response */
 1316	if (stype == IEEE80211_STYPE_BEACON &&
 1317	    ap_chandef.chan->band == NL80211_BAND_6GHZ &&
 1318	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE) {
 1319		ieee80211_rearrange_tpe(&elems->tpe, &ap_chandef,
 1320					&chanreq.oper);
 1321		if (memcmp(&link->conf->tpe, &elems->tpe, sizeof(elems->tpe))) {
 1322			link->conf->tpe = elems->tpe;
 1323			*changed |= BSS_CHANGED_TPE;
 1324		}
 1325	}
 1326
 1327	if (ieee80211_chanreq_identical(&chanreq, &link->conf->chanreq))
 1328		return 0;
 1329
 1330	link_info(link,
 1331		  "AP %pM changed bandwidth in %s, new used config is %d.%03d MHz, width %d (%d.%03d/%d MHz)\n",
 1332		  link->u.mgd.bssid, frame, chanreq.oper.chan->center_freq,
 1333		  chanreq.oper.chan->freq_offset, chanreq.oper.width,
 1334		  chanreq.oper.center_freq1, chanreq.oper.freq1_offset,
 1335		  chanreq.oper.center_freq2);
 1336
 1337	if (!cfg80211_chandef_valid(&chanreq.oper)) {
 1338		sdata_info(sdata,
 1339			   "AP %pM changed caps/bw in %s in a way we can't support - disconnect\n",
 1340			   link->u.mgd.bssid, frame);
 1341		return -EINVAL;
 1342	}
 1343
 1344	if (!update) {
 1345		link->conf->chanreq = chanreq;
 1346		return 0;
 1347	}
 1348
 1349	/*
 1350	 * We're tracking the current AP here, so don't do any further checks
 1351	 * here. This keeps us from playing ping-pong with regulatory, without
 1352	 * it the following can happen (for example):
 1353	 *  - connect to an AP with 80 MHz, world regdom allows 80 MHz
 1354	 *  - AP advertises regdom US
 1355	 *  - CRDA loads regdom US with 80 MHz prohibited (old database)
 1356	 *  - we detect an unsupported channel and disconnect
 1357	 *  - disconnect causes CRDA to reload world regdomain and the game
 1358	 *    starts anew.
 1359	 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
 1360	 *
 1361	 * It seems possible that there are still scenarios with CSA or real
 1362	 * bandwidth changes where a this could happen, but those cases are
 1363	 * less common and wouldn't completely prevent using the AP.
 1364	 */
 1365
 1366	ret = ieee80211_link_change_chanreq(link, &chanreq, changed);
 1367	if (ret) {
 1368		sdata_info(sdata,
 1369			   "AP %pM changed bandwidth in %s to incompatible one - disconnect\n",
 1370			   link->u.mgd.bssid, frame);
 1371		return ret;
 1372	}
 1373
 1374	cfg80211_schedule_channels_check(&sdata->wdev);
 1375	return 0;
 1376}
 1377
 1378/* frame sending functions */
 1379
 1380static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
 1381				struct sk_buff *skb, u8 ap_ht_param,
 1382				struct ieee80211_supported_band *sband,
 1383				struct ieee80211_channel *channel,
 1384				enum ieee80211_smps_mode smps,
 1385				const struct ieee80211_conn_settings *conn)
 1386{
 1387	u8 *pos;
 1388	u32 flags = channel->flags;
 1389	u16 cap;
 1390	struct ieee80211_sta_ht_cap ht_cap;
 1391
 1392	BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
 1393
 1394	memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
 1395	ieee80211_apply_htcap_overrides(sdata, &ht_cap);
 1396
 1397	/* determine capability flags */
 1398	cap = ht_cap.cap;
 1399
 1400	switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
 1401	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
 1402		if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
 1403			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 1404			cap &= ~IEEE80211_HT_CAP_SGI_40;
 1405		}
 1406		break;
 1407	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
 1408		if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
 1409			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 1410			cap &= ~IEEE80211_HT_CAP_SGI_40;
 1411		}
 1412		break;
 1413	}
 1414
 1415	/*
 1416	 * If 40 MHz was disabled associate as though we weren't
 1417	 * capable of 40 MHz -- some broken APs will never fall
 1418	 * back to trying to transmit in 20 MHz.
 1419	 */
 1420	if (conn->bw_limit <= IEEE80211_CONN_BW_LIMIT_20) {
 1421		cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 1422		cap &= ~IEEE80211_HT_CAP_SGI_40;
 1423	}
 1424
 1425	/* set SM PS mode properly */
 1426	cap &= ~IEEE80211_HT_CAP_SM_PS;
 1427	switch (smps) {
 1428	case IEEE80211_SMPS_AUTOMATIC:
 1429	case IEEE80211_SMPS_NUM_MODES:
 1430		WARN_ON(1);
 1431		fallthrough;
 1432	case IEEE80211_SMPS_OFF:
 1433		cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
 1434			IEEE80211_HT_CAP_SM_PS_SHIFT;
 1435		break;
 1436	case IEEE80211_SMPS_STATIC:
 1437		cap |= WLAN_HT_CAP_SM_PS_STATIC <<
 1438			IEEE80211_HT_CAP_SM_PS_SHIFT;
 1439		break;
 1440	case IEEE80211_SMPS_DYNAMIC:
 1441		cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
 1442			IEEE80211_HT_CAP_SM_PS_SHIFT;
 1443		break;
 1444	}
 1445
 1446	/* reserve and fill IE */
 1447	pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
 1448	ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
 1449}
 1450
 1451/* This function determines vht capability flags for the association
 1452 * and builds the IE.
 1453 * Note - the function returns true to own the MU-MIMO capability
 1454 */
 1455static bool ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
 1456				 struct sk_buff *skb,
 1457				 struct ieee80211_supported_band *sband,
 1458				 struct ieee80211_vht_cap *ap_vht_cap,
 1459				 const struct ieee80211_conn_settings *conn)
 1460{
 1461	struct ieee80211_local *local = sdata->local;
 1462	u8 *pos;
 1463	u32 cap;
 1464	struct ieee80211_sta_vht_cap vht_cap;
 1465	u32 mask, ap_bf_sts, our_bf_sts;
 1466	bool mu_mimo_owner = false;
 1467
 1468	BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
 1469
 1470	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
 1471	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
 1472
 1473	/* determine capability flags */
 1474	cap = vht_cap.cap;
 1475
 1476	if (conn->bw_limit <= IEEE80211_CONN_BW_LIMIT_80) {
 1477		cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
 1478		cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
 1479	}
 1480
 1481	/*
 1482	 * Some APs apparently get confused if our capabilities are better
 1483	 * than theirs, so restrict what we advertise in the assoc request.
 1484	 */
 1485	if (!ieee80211_hw_check(&local->hw, STRICT)) {
 1486		if (!(ap_vht_cap->vht_cap_info &
 1487				cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
 1488			cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
 1489				 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
 1490		else if (!(ap_vht_cap->vht_cap_info &
 1491				cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
 1492			cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
 1493	}
 1494
 1495	/*
 1496	 * If some other vif is using the MU-MIMO capability we cannot associate
 1497	 * using MU-MIMO - this will lead to contradictions in the group-id
 1498	 * mechanism.
 1499	 * Ownership is defined since association request, in order to avoid
 1500	 * simultaneous associations with MU-MIMO.
 1501	 */
 1502	if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
 1503		bool disable_mu_mimo = false;
 1504		struct ieee80211_sub_if_data *other;
 1505
 1506		list_for_each_entry(other, &local->interfaces, list) {
 1507			if (other->vif.bss_conf.mu_mimo_owner) {
 1508				disable_mu_mimo = true;
 1509				break;
 1510			}
 1511		}
 1512		if (disable_mu_mimo)
 1513			cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
 1514		else
 1515			mu_mimo_owner = true;
 1516	}
 1517
 1518	mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
 1519
 1520	ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
 1521	our_bf_sts = cap & mask;
 1522
 1523	if (ap_bf_sts < our_bf_sts) {
 1524		cap &= ~mask;
 1525		cap |= ap_bf_sts;
 1526	}
 1527
 1528	/* reserve and fill IE */
 1529	pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
 1530	ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
 1531
 1532	return mu_mimo_owner;
 1533}
 1534
 1535static void ieee80211_assoc_add_rates(struct ieee80211_local *local,
 1536				      struct sk_buff *skb,
 1537				      enum nl80211_chan_width width,
 1538				      struct ieee80211_supported_band *sband,
 1539				      struct ieee80211_mgd_assoc_data *assoc_data)
 1540{
 1541	u32 rates;
 1542
 1543	if (assoc_data->supp_rates_len &&
 1544	    !ieee80211_hw_check(&local->hw, STRICT)) {
 1545		/*
 1546		 * Get all rates supported by the device and the AP as
 1547		 * some APs don't like getting a superset of their rates
 1548		 * in the association request (e.g. D-Link DAP 1353 in
 1549		 * b-only mode)...
 1550		 */
 1551		ieee80211_parse_bitrates(width, sband,
 1552					 assoc_data->supp_rates,
 1553					 assoc_data->supp_rates_len,
 1554					 &rates);
 1555	} else {
 1556		/*
 1557		 * In case AP not provide any supported rates information
 1558		 * before association, we send information element(s) with
 1559		 * all rates that we support.
 1560		 */
 1561		rates = ~0;
 1562	}
 1563
 1564	ieee80211_put_srates_elem(skb, sband, 0, ~rates,
 1565				  WLAN_EID_SUPP_RATES);
 1566	ieee80211_put_srates_elem(skb, sband, 0, ~rates,
 1567				  WLAN_EID_EXT_SUPP_RATES);
 1568}
 1569
 1570static size_t ieee80211_add_before_ht_elems(struct sk_buff *skb,
 1571					    const u8 *elems,
 1572					    size_t elems_len,
 1573					    size_t offset)
 1574{
 1575	size_t noffset;
 1576
 1577	static const u8 before_ht[] = {
 1578		WLAN_EID_SSID,
 1579		WLAN_EID_SUPP_RATES,
 1580		WLAN_EID_EXT_SUPP_RATES,
 1581		WLAN_EID_PWR_CAPABILITY,
 1582		WLAN_EID_SUPPORTED_CHANNELS,
 1583		WLAN_EID_RSN,
 1584		WLAN_EID_QOS_CAPA,
 1585		WLAN_EID_RRM_ENABLED_CAPABILITIES,
 1586		WLAN_EID_MOBILITY_DOMAIN,
 1587		WLAN_EID_FAST_BSS_TRANSITION,	/* reassoc only */
 1588		WLAN_EID_RIC_DATA,		/* reassoc only */
 1589		WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
 1590	};
 1591	static const u8 after_ric[] = {
 1592		WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
 1593		WLAN_EID_HT_CAPABILITY,
 1594		WLAN_EID_BSS_COEX_2040,
 1595		/* luckily this is almost always there */
 1596		WLAN_EID_EXT_CAPABILITY,
 1597		WLAN_EID_QOS_TRAFFIC_CAPA,
 1598		WLAN_EID_TIM_BCAST_REQ,
 1599		WLAN_EID_INTERWORKING,
 1600		/* 60 GHz (Multi-band, DMG, MMS) can't happen */
 1601		WLAN_EID_VHT_CAPABILITY,
 1602		WLAN_EID_OPMODE_NOTIF,
 1603	};
 1604
 1605	if (!elems_len)
 1606		return offset;
 1607
 1608	noffset = ieee80211_ie_split_ric(elems, elems_len,
 1609					 before_ht,
 1610					 ARRAY_SIZE(before_ht),
 1611					 after_ric,
 1612					 ARRAY_SIZE(after_ric),
 1613					 offset);
 1614	skb_put_data(skb, elems + offset, noffset - offset);
 1615
 1616	return noffset;
 1617}
 1618
 1619static size_t ieee80211_add_before_vht_elems(struct sk_buff *skb,
 1620					     const u8 *elems,
 1621					     size_t elems_len,
 1622					     size_t offset)
 1623{
 1624	static const u8 before_vht[] = {
 1625		/*
 1626		 * no need to list the ones split off before HT
 1627		 * or generated here
 1628		 */
 1629		WLAN_EID_BSS_COEX_2040,
 1630		WLAN_EID_EXT_CAPABILITY,
 1631		WLAN_EID_QOS_TRAFFIC_CAPA,
 1632		WLAN_EID_TIM_BCAST_REQ,
 1633		WLAN_EID_INTERWORKING,
 1634		/* 60 GHz (Multi-band, DMG, MMS) can't happen */
 1635	};
 1636	size_t noffset;
 1637
 1638	if (!elems_len)
 1639		return offset;
 1640
 1641	/* RIC already taken care of in ieee80211_add_before_ht_elems() */
 1642	noffset = ieee80211_ie_split(elems, elems_len,
 1643				     before_vht, ARRAY_SIZE(before_vht),
 1644				     offset);
 1645	skb_put_data(skb, elems + offset, noffset - offset);
 1646
 1647	return noffset;
 1648}
 1649
 1650static size_t ieee80211_add_before_he_elems(struct sk_buff *skb,
 1651					    const u8 *elems,
 1652					    size_t elems_len,
 1653					    size_t offset)
 1654{
 1655	static const u8 before_he[] = {
 1656		/*
 1657		 * no need to list the ones split off before VHT
 1658		 * or generated here
 1659		 */
 1660		WLAN_EID_OPMODE_NOTIF,
 1661		WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
 1662		/* 11ai elements */
 1663		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
 1664		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
 1665		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
 1666		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
 1667		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
 1668		/* TODO: add 11ah/11aj/11ak elements */
 1669	};
 1670	size_t noffset;
 1671
 1672	if (!elems_len)
 1673		return offset;
 1674
 1675	/* RIC already taken care of in ieee80211_add_before_ht_elems() */
 1676	noffset = ieee80211_ie_split(elems, elems_len,
 1677				     before_he, ARRAY_SIZE(before_he),
 1678				     offset);
 1679	skb_put_data(skb, elems + offset, noffset - offset);
 1680
 1681	return noffset;
 1682}
 1683
 1684static size_t ieee80211_add_before_reg_conn(struct sk_buff *skb,
 1685					    const u8 *elems, size_t elems_len,
 1686					    size_t offset)
 1687{
 1688	static const u8 before_reg_conn[] = {
 1689		/*
 1690		 * no need to list the ones split off before HE
 1691		 * or generated here
 1692		 */
 1693		WLAN_EID_EXTENSION, WLAN_EID_EXT_DH_PARAMETER,
 1694		WLAN_EID_EXTENSION, WLAN_EID_EXT_KNOWN_STA_IDENTIFCATION,
 1695	};
 1696	size_t noffset;
 1697
 1698	if (!elems_len)
 1699		return offset;
 1700
 1701	noffset = ieee80211_ie_split(elems, elems_len, before_reg_conn,
 1702				     ARRAY_SIZE(before_reg_conn), offset);
 1703	skb_put_data(skb, elems + offset, noffset - offset);
 1704
 1705	return noffset;
 1706}
 1707
 1708#define PRESENT_ELEMS_MAX	8
 1709#define PRESENT_ELEM_EXT_OFFS	0x100
 1710
 1711static void
 1712ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata,
 1713			    struct sk_buff *skb, u16 capab,
 1714			    const struct element *ext_capa,
 1715			    const u16 *present_elems,
 1716			    struct ieee80211_mgd_assoc_data *assoc_data);
 1717
 1718static size_t
 1719ieee80211_add_link_elems(struct ieee80211_sub_if_data *sdata,
 1720			 struct sk_buff *skb, u16 *capab,
 1721			 const struct element *ext_capa,
 1722			 const u8 *extra_elems,
 1723			 size_t extra_elems_len,
 1724			 unsigned int link_id,
 1725			 struct ieee80211_link_data *link,
 1726			 u16 *present_elems,
 1727			 struct ieee80211_mgd_assoc_data *assoc_data)
 1728{
 1729	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
 1730	struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
 1731	struct ieee80211_channel *chan = cbss->channel;
 1732	const struct ieee80211_sband_iftype_data *iftd;
 1733	struct ieee80211_local *local = sdata->local;
 1734	struct ieee80211_supported_band *sband;
 1735	enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20;
 1736	struct ieee80211_chanctx_conf *chanctx_conf;
 1737	enum ieee80211_smps_mode smps_mode;
 1738	u16 orig_capab = *capab;
 1739	size_t offset = 0;
 1740	int present_elems_len = 0;
 1741	u8 *pos;
 1742	int i;
 1743
 1744#define ADD_PRESENT_ELEM(id) do {					\
 1745	/* need a last for termination - we use 0 == SSID */		\
 1746	if (!WARN_ON(present_elems_len >= PRESENT_ELEMS_MAX - 1))	\
 1747		present_elems[present_elems_len++] = (id);		\
 1748} while (0)
 1749#define ADD_PRESENT_EXT_ELEM(id) ADD_PRESENT_ELEM(PRESENT_ELEM_EXT_OFFS | (id))
 1750
 1751	if (link)
 1752		smps_mode = link->smps_mode;
 1753	else if (sdata->u.mgd.powersave)
 1754		smps_mode = IEEE80211_SMPS_DYNAMIC;
 1755	else
 1756		smps_mode = IEEE80211_SMPS_OFF;
 1757
 1758	if (link) {
 1759		/*
 1760		 * 5/10 MHz scenarios are only viable without MLO, in which
 1761		 * case this pointer should be used ... All of this is a bit
 1762		 * unclear though, not sure this even works at all.
 1763		 */
 1764		rcu_read_lock();
 1765		chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
 1766		if (chanctx_conf)
 1767			width = chanctx_conf->def.width;
 1768		rcu_read_unlock();
 1769	}
 1770
 1771	sband = local->hw.wiphy->bands[chan->band];
 1772	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
 1773
 1774	if (sband->band == NL80211_BAND_2GHZ) {
 1775		*capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
 1776		*capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
 1777	}
 1778
 1779	if ((cbss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
 1780	    ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
 1781		*capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
 1782
 1783	if (sband->band != NL80211_BAND_S1GHZ)
 1784		ieee80211_assoc_add_rates(local, skb, width, sband, assoc_data);
 1785
 1786	if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
 1787	    *capab & WLAN_CAPABILITY_RADIO_MEASURE) {
 1788		struct cfg80211_chan_def chandef = {
 1789			.width = width,
 1790			.chan = chan,
 1791		};
 1792
 1793		pos = skb_put(skb, 4);
 1794		*pos++ = WLAN_EID_PWR_CAPABILITY;
 1795		*pos++ = 2;
 1796		*pos++ = 0; /* min tx power */
 1797		 /* max tx power */
 1798		*pos++ = ieee80211_chandef_max_power(&chandef);
 1799		ADD_PRESENT_ELEM(WLAN_EID_PWR_CAPABILITY);
 1800	}
 1801
 1802	/*
 1803	 * Per spec, we shouldn't include the list of channels if we advertise
 1804	 * support for extended channel switching, but we've always done that;
 1805	 * (for now?) apply this restriction only on the (new) 6 GHz band.
 1806	 */
 1807	if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
 1808	    (sband->band != NL80211_BAND_6GHZ ||
 1809	     !ext_capa || ext_capa->datalen < 1 ||
 1810	     !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
 1811		/* TODO: get this in reg domain format */
 1812		pos = skb_put(skb, 2 * sband->n_channels + 2);
 1813		*pos++ = WLAN_EID_SUPPORTED_CHANNELS;
 1814		*pos++ = 2 * sband->n_channels;
 1815		for (i = 0; i < sband->n_channels; i++) {
 1816			int cf = sband->channels[i].center_freq;
 1817
 1818			*pos++ = ieee80211_frequency_to_channel(cf);
 1819			*pos++ = 1; /* one channel in the subband*/
 1820		}
 1821		ADD_PRESENT_ELEM(WLAN_EID_SUPPORTED_CHANNELS);
 1822	}
 1823
 1824	/* if present, add any custom IEs that go before HT */
 1825	offset = ieee80211_add_before_ht_elems(skb, extra_elems,
 1826					       extra_elems_len,
 1827					       offset);
 1828
 1829	if (sband->band != NL80211_BAND_6GHZ &&
 1830	    assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_HT) {
 1831		ieee80211_add_ht_ie(sdata, skb,
 1832				    assoc_data->link[link_id].ap_ht_param,
 1833				    sband, chan, smps_mode,
 1834				    &assoc_data->link[link_id].conn);
 1835		ADD_PRESENT_ELEM(WLAN_EID_HT_CAPABILITY);
 1836	}
 1837
 1838	/* if present, add any custom IEs that go before VHT */
 1839	offset = ieee80211_add_before_vht_elems(skb, extra_elems,
 1840						extra_elems_len,
 1841						offset);
 1842
 1843	if (sband->band != NL80211_BAND_6GHZ &&
 1844	    assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_VHT &&
 1845	    sband->vht_cap.vht_supported) {
 1846		bool mu_mimo_owner =
 1847			ieee80211_add_vht_ie(sdata, skb, sband,
 1848					     &assoc_data->link[link_id].ap_vht_cap,
 1849					     &assoc_data->link[link_id].conn);
 1850
 1851		if (link)
 1852			link->conf->mu_mimo_owner = mu_mimo_owner;
 1853		ADD_PRESENT_ELEM(WLAN_EID_VHT_CAPABILITY);
 1854	}
 1855
 1856	/* if present, add any custom IEs that go before HE */
 1857	offset = ieee80211_add_before_he_elems(skb, extra_elems,
 1858					       extra_elems_len,
 1859					       offset);
 1860
 1861	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_HE) {
 1862		ieee80211_put_he_cap(skb, sdata, sband,
 1863				     &assoc_data->link[link_id].conn);
 1864		ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_HE_CAPABILITY);
 1865		if (sband->band == NL80211_BAND_6GHZ)
 1866			ieee80211_put_he_6ghz_cap(skb, sdata, smps_mode);
 1867	}
 1868
 1869	/*
 1870	 * if present, add any custom IEs that go before regulatory
 1871	 * connectivity element
 1872	 */
 1873	offset = ieee80211_add_before_reg_conn(skb, extra_elems,
 1874					       extra_elems_len, offset);
 1875
 1876	if (sband->band == NL80211_BAND_6GHZ) {
 1877		/*
 1878		 * as per Section E.2.7 of IEEE 802.11 REVme D7.0, non-AP STA
 1879		 * capable of operating on the 6 GHz band shall transmit
 1880		 * regulatory connectivity element.
 1881		 */
 1882		ieee80211_put_reg_conn(skb, chan->flags);
 1883	}
 1884
 1885	/*
 1886	 * careful - need to know about all the present elems before
 1887	 * calling ieee80211_assoc_add_ml_elem(), so add this one if
 1888	 * we're going to put it after the ML element
 1889	 */
 1890	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_EHT)
 1891		ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_EHT_CAPABILITY);
 1892
 1893	if (link_id == assoc_data->assoc_link_id)
 1894		ieee80211_assoc_add_ml_elem(sdata, skb, orig_capab, ext_capa,
 1895					    present_elems, assoc_data);
 1896
 1897	/* crash if somebody gets it wrong */
 1898	present_elems = NULL;
 1899
 1900	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_EHT)
 1901		ieee80211_put_eht_cap(skb, sdata, sband,
 1902				      &assoc_data->link[link_id].conn);
 1903
 1904	if (sband->band == NL80211_BAND_S1GHZ) {
 1905		ieee80211_add_aid_request_ie(sdata, skb);
 1906		ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
 1907	}
 1908
 1909	if (iftd && iftd->vendor_elems.data && iftd->vendor_elems.len)
 1910		skb_put_data(skb, iftd->vendor_elems.data, iftd->vendor_elems.len);
 1911
 1912	return offset;
 1913}
 1914
 1915static void ieee80211_add_non_inheritance_elem(struct sk_buff *skb,
 1916					       const u16 *outer,
 1917					       const u16 *inner)
 1918{
 1919	unsigned int skb_len = skb->len;
 1920	bool at_extension = false;
 1921	bool added = false;
 1922	int i, j;
 1923	u8 *len, *list_len = NULL;
 1924
 1925	skb_put_u8(skb, WLAN_EID_EXTENSION);
 1926	len = skb_put(skb, 1);
 1927	skb_put_u8(skb, WLAN_EID_EXT_NON_INHERITANCE);
 1928
 1929	for (i = 0; i < PRESENT_ELEMS_MAX && outer[i]; i++) {
 1930		u16 elem = outer[i];
 1931		bool have_inner = false;
 1932
 1933		/* should at least be sorted in the sense of normal -> ext */
 1934		WARN_ON(at_extension && elem < PRESENT_ELEM_EXT_OFFS);
 1935
 1936		/* switch to extension list */
 1937		if (!at_extension && elem >= PRESENT_ELEM_EXT_OFFS) {
 1938			at_extension = true;
 1939			if (!list_len)
 1940				skb_put_u8(skb, 0);
 1941			list_len = NULL;
 1942		}
 1943
 1944		for (j = 0; j < PRESENT_ELEMS_MAX && inner[j]; j++) {
 1945			if (elem == inner[j]) {
 1946				have_inner = true;
 1947				break;
 1948			}
 1949		}
 1950
 1951		if (have_inner)
 1952			continue;
 1953
 1954		if (!list_len) {
 1955			list_len = skb_put(skb, 1);
 1956			*list_len = 0;
 1957		}
 1958		*list_len += 1;
 1959		skb_put_u8(skb, (u8)elem);
 1960		added = true;
 1961	}
 1962
 1963	/* if we added a list but no extension list, make a zero-len one */
 1964	if (added && (!at_extension || !list_len))
 1965		skb_put_u8(skb, 0);
 1966
 1967	/* if nothing added remove extension element completely */
 1968	if (!added)
 1969		skb_trim(skb, skb_len);
 1970	else
 1971		*len = skb->len - skb_len - 2;
 1972}
 1973
 1974static void
 1975ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata,
 1976			    struct sk_buff *skb, u16 capab,
 1977			    const struct element *ext_capa,
 1978			    const u16 *outer_present_elems,
 1979			    struct ieee80211_mgd_assoc_data *assoc_data)
 1980{
 1981	struct ieee80211_local *local = sdata->local;
 1982	struct ieee80211_multi_link_elem *ml_elem;
 1983	struct ieee80211_mle_basic_common_info *common;
 1984	const struct wiphy_iftype_ext_capab *ift_ext_capa;
 1985	__le16 eml_capa = 0, mld_capa_ops = 0;
 1986	unsigned int link_id;
 1987	u8 *ml_elem_len;
 1988	void *capab_pos;
 1989
 1990	if (!ieee80211_vif_is_mld(&sdata->vif))
 1991		return;
 1992
 1993	ift_ext_capa = cfg80211_get_iftype_ext_capa(local->hw.wiphy,
 1994						    ieee80211_vif_type_p2p(&sdata->vif));
 1995	if (ift_ext_capa) {
 1996		eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities);
 1997		mld_capa_ops = cpu_to_le16(ift_ext_capa->mld_capa_and_ops);
 1998	}
 1999
 2000	skb_put_u8(skb, WLAN_EID_EXTENSION);
 2001	ml_elem_len = skb_put(skb, 1);
 2002	skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
 2003	ml_elem = skb_put(skb, sizeof(*ml_elem));
 2004	ml_elem->control =
 2005		cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC |
 2006			    IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP);
 2007	common = skb_put(skb, sizeof(*common));
 2008	common->len = sizeof(*common) +
 2009		      2;  /* MLD capa/ops */
 2010	memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
 2011
 2012	/* add EML_CAPA only if needed, see Draft P802.11be_D2.1, 35.3.17 */
 2013	if (eml_capa &
 2014	    cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
 2015			 IEEE80211_EML_CAP_EMLMR_SUPPORT))) {
 2016		common->len += 2; /* EML capabilities */
 2017		ml_elem->control |=
 2018			cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EML_CAPA);
 2019		skb_put_data(skb, &eml_capa, sizeof(eml_capa));
 2020	}
 2021	skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops));
 2022
 2023	if (assoc_data->ext_mld_capa_ops) {
 2024		ml_elem->control |=
 2025			cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP);
 2026		common->len += 2;
 2027		skb_put_data(skb, &assoc_data->ext_mld_capa_ops,
 2028			     sizeof(assoc_data->ext_mld_capa_ops));
 2029	}
 2030
 2031	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 2032		u16 link_present_elems[PRESENT_ELEMS_MAX] = {};
 2033		const u8 *extra_elems;
 2034		size_t extra_elems_len;
 2035		size_t extra_used;
 2036		u8 *subelem_len = NULL;
 2037		__le16 ctrl;
 2038
 2039		if (!assoc_data->link[link_id].bss ||
 2040		    link_id == assoc_data->assoc_link_id)
 2041			continue;
 2042
 2043		extra_elems = assoc_data->link[link_id].elems;
 2044		extra_elems_len = assoc_data->link[link_id].elems_len;
 2045
 2046		skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
 2047		subelem_len = skb_put(skb, 1);
 2048
 2049		ctrl = cpu_to_le16(link_id |
 2050				   IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE |
 2051				   IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT);
 2052		skb_put_data(skb, &ctrl, sizeof(ctrl));
 2053		skb_put_u8(skb, 1 + ETH_ALEN); /* STA Info Length */
 2054		skb_put_data(skb, assoc_data->link[link_id].addr,
 2055			     ETH_ALEN);
 2056		/*
 2057		 * Now add the contents of the (re)association request,
 2058		 * but the "listen interval" and "current AP address"
 2059		 * (if applicable) are skipped. So we only have
 2060		 * the capability field (remember the position and fill
 2061		 * later), followed by the elements added below by
 2062		 * calling ieee80211_add_link_elems().
 2063		 */
 2064		capab_pos = skb_put(skb, 2);
 2065
 2066		extra_used = ieee80211_add_link_elems(sdata, skb, &capab,
 2067						      ext_capa,
 2068						      extra_elems,
 2069						      extra_elems_len,
 2070						      link_id, NULL,
 2071						      link_present_elems,
 2072						      assoc_data);
 2073		if (extra_elems)
 2074			skb_put_data(skb, extra_elems + extra_used,
 2075				     extra_elems_len - extra_used);
 2076
 2077		put_unaligned_le16(capab, capab_pos);
 2078
 2079		ieee80211_add_non_inheritance_elem(skb, outer_present_elems,
 2080						   link_present_elems);
 2081
 2082		ieee80211_fragment_element(skb, subelem_len,
 2083					   IEEE80211_MLE_SUBELEM_FRAGMENT);
 2084	}
 2085
 2086	ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT);
 2087}
 2088
 2089static int
 2090ieee80211_link_common_elems_size(struct ieee80211_sub_if_data *sdata,
 2091				 enum nl80211_iftype iftype,
 2092				 struct cfg80211_bss *cbss,
 2093				 size_t elems_len)
 2094{
 2095	struct ieee80211_local *local = sdata->local;
 2096	const struct ieee80211_sband_iftype_data *iftd;
 2097	struct ieee80211_supported_band *sband;
 2098	size_t size = 0;
 2099
 2100	if (!cbss)
 2101		return size;
 2102
 2103	sband = local->hw.wiphy->bands[cbss->channel->band];
 2104
 2105	/* add STA profile elements length */
 2106	size += elems_len;
 2107
 2108	/* and supported rates length */
 2109	size += 4 + sband->n_bitrates;
 2110
 2111	/* supported channels */
 2112	size += 2 + 2 * sband->n_channels;
 2113
 2114	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
 2115	if (iftd)
 2116		size += iftd->vendor_elems.len;
 2117
 2118	/* power capability */
 2119	size += 4;
 2120
 2121	/* HT, VHT, HE, EHT */
 2122	size += 2 + sizeof(struct ieee80211_ht_cap);
 2123	size += 2 + sizeof(struct ieee80211_vht_cap);
 2124	size += 2 + 1 + sizeof(struct ieee80211_he_cap_elem) +
 2125		sizeof(struct ieee80211_he_mcs_nss_supp) +
 2126		IEEE80211_HE_PPE_THRES_MAX_LEN;
 2127
 2128	if (sband->band == NL80211_BAND_6GHZ) {
 2129		size += 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa);
 2130		/* reg connection */
 2131		size += 4;
 2132	}
 2133
 2134	size += 2 + 1 + sizeof(struct ieee80211_eht_cap_elem) +
 2135		sizeof(struct ieee80211_eht_mcs_nss_supp) +
 2136		IEEE80211_EHT_PPE_THRES_MAX_LEN;
 2137
 2138	return size;
 2139}
 2140
 2141static int ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
 2142{
 2143	struct ieee80211_local *local = sdata->local;
 2144	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2145	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
 2146	struct ieee80211_link_data *link;
 2147	struct sk_buff *skb;
 2148	struct ieee80211_mgmt *mgmt;
 2149	u8 *pos, qos_info, *ie_start;
 2150	size_t offset, noffset;
 2151	u16 capab = 0, link_capab;
 2152	__le16 listen_int;
 2153	struct element *ext_capa = NULL;
 2154	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
 2155	struct ieee80211_prep_tx_info info = {};
 2156	unsigned int link_id, n_links = 0;
 2157	u16 present_elems[PRESENT_ELEMS_MAX] = {};
 2158	void *capab_pos;
 2159	size_t size;
 2160	int ret;
 2161
 2162	/* we know it's writable, cast away the const */
 2163	if (assoc_data->ie_len)
 2164		ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
 2165						      assoc_data->ie,
 2166						      assoc_data->ie_len);
 2167
 2168	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 2169
 2170	size = local->hw.extra_tx_headroom +
 2171	       sizeof(*mgmt) + /* bit too much but doesn't matter */
 2172	       2 + assoc_data->ssid_len + /* SSID */
 2173	       assoc_data->ie_len + /* extra IEs */
 2174	       (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
 2175	       9; /* WMM */
 2176
 2177	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 2178		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
 2179		size_t elems_len = assoc_data->link[link_id].elems_len;
 2180
 2181		if (!cbss)
 2182			continue;
 2183
 2184		n_links++;
 2185
 2186		size += ieee80211_link_common_elems_size(sdata, iftype, cbss,
 2187							 elems_len);
 2188
 2189		/* non-inheritance element */
 2190		size += 2 + 2 + PRESENT_ELEMS_MAX;
 2191
 2192		/* should be the same across all BSSes */
 2193		if (cbss->capability & WLAN_CAPABILITY_PRIVACY)
 2194			capab |= WLAN_CAPABILITY_PRIVACY;
 2195	}
 2196
 2197	if (ieee80211_vif_is_mld(&sdata->vif)) {
 2198		/* consider the multi-link element with STA profile */
 2199		size += sizeof(struct ieee80211_multi_link_elem);
 2200		/* max common info field in basic multi-link element */
 2201		size += sizeof(struct ieee80211_mle_basic_common_info) +
 2202			2 + /* capa & op */
 2203			2 + /* ext capa & op */
 2204			2; /* EML capa */
 2205
 2206		/* The capability elements were already considered above */
 2207		size += (n_links - 1) *
 2208			(1 + 1 + /* subelement ID/length */
 2209			 2 + /* STA control */
 2210			 1 + ETH_ALEN + 2 /* STA Info field */);
 2211	}
 2212
 2213	link = sdata_dereference(sdata->link[assoc_data->assoc_link_id], sdata);
 2214	if (WARN_ON(!link))
 2215		return -EINVAL;
 2216
 2217	if (WARN_ON(!assoc_data->link[assoc_data->assoc_link_id].bss))
 2218		return -EINVAL;
 2219
 2220	skb = alloc_skb(size, GFP_KERNEL);
 2221	if (!skb)
 2222		return -ENOMEM;
 2223
 2224	skb_reserve(skb, local->hw.extra_tx_headroom);
 2225
 2226	if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
 2227		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
 2228
 2229	/* Set MBSSID support for HE AP if needed */
 2230	if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
 2231	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE &&
 2232	    ext_capa && ext_capa->datalen >= 3)
 2233		ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
 2234
 2235	mgmt = skb_put_zero(skb, 24);
 2236	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
 2237	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
 2238	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
 2239
 2240	listen_int = cpu_to_le16(assoc_data->s1g ?
 2241			ieee80211_encode_usf(local->hw.conf.listen_interval) :
 2242			local->hw.conf.listen_interval);
 2243	if (!is_zero_ether_addr(assoc_data->prev_ap_addr)) {
 2244		skb_put(skb, 10);
 2245		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
 2246						  IEEE80211_STYPE_REASSOC_REQ);
 2247		capab_pos = &mgmt->u.reassoc_req.capab_info;
 2248		mgmt->u.reassoc_req.listen_interval = listen_int;
 2249		memcpy(mgmt->u.reassoc_req.current_ap,
 2250		       assoc_data->prev_ap_addr, ETH_ALEN);
 2251		info.subtype = IEEE80211_STYPE_REASSOC_REQ;
 2252	} else {
 2253		skb_put(skb, 4);
 2254		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
 2255						  IEEE80211_STYPE_ASSOC_REQ);
 2256		capab_pos = &mgmt->u.assoc_req.capab_info;
 2257		mgmt->u.assoc_req.listen_interval = listen_int;
 2258		info.subtype = IEEE80211_STYPE_ASSOC_REQ;
 2259	}
 2260
 2261	/* SSID */
 2262	pos = skb_put(skb, 2 + assoc_data->ssid_len);
 2263	ie_start = pos;
 2264	*pos++ = WLAN_EID_SSID;
 2265	*pos++ = assoc_data->ssid_len;
 2266	memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
 2267
 2268	/*
 2269	 * This bit is technically reserved, so it shouldn't matter for either
 2270	 * the AP or us, but it also means we shouldn't set it. However, we've
 2271	 * always set it in the past, and apparently some EHT APs check that
 2272	 * we don't set it. To avoid interoperability issues with old APs that
 2273	 * for some reason check it and want it to be set, set the bit for all
 2274	 * pre-EHT connections as we used to do.
 2275	 */
 2276	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_EHT &&
 2277	    !ieee80211_hw_check(&local->hw, STRICT))
 2278		capab |= WLAN_CAPABILITY_ESS;
 2279
 2280	/* add the elements for the assoc (main) link */
 2281	link_capab = capab;
 2282	offset = ieee80211_add_link_elems(sdata, skb, &link_capab,
 2283					  ext_capa,
 2284					  assoc_data->ie,
 2285					  assoc_data->ie_len,
 2286					  assoc_data->assoc_link_id, link,
 2287					  present_elems, assoc_data);
 2288	put_unaligned_le16(link_capab, capab_pos);
 2289
 2290	/* if present, add any custom non-vendor IEs */
 2291	if (assoc_data->ie_len) {
 2292		noffset = ieee80211_ie_split_vendor(assoc_data->ie,
 2293						    assoc_data->ie_len,
 2294						    offset);
 2295		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
 2296		offset = noffset;
 2297	}
 2298
 2299	if (assoc_data->wmm) {
 2300		if (assoc_data->uapsd) {
 2301			qos_info = ifmgd->uapsd_queues;
 2302			qos_info |= (ifmgd->uapsd_max_sp_len <<
 2303				     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
 2304		} else {
 2305			qos_info = 0;
 2306		}
 2307
 2308		pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
 2309	}
 2310
 2311	/* add any remaining custom (i.e. vendor specific here) IEs */
 2312	if (assoc_data->ie_len) {
 2313		noffset = assoc_data->ie_len;
 2314		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
 2315	}
 2316
 2317	if (assoc_data->fils_kek_len) {
 2318		ret = fils_encrypt_assoc_req(skb, assoc_data);
 2319		if (ret < 0) {
 2320			dev_kfree_skb(skb);
 2321			return ret;
 2322		}
 2323	}
 2324
 2325	pos = skb_tail_pointer(skb);
 2326	kfree(ifmgd->assoc_req_ies);
 2327	ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
 2328	if (!ifmgd->assoc_req_ies) {
 2329		dev_kfree_skb(skb);
 2330		return -ENOMEM;
 2331	}
 2332
 2333	ifmgd->assoc_req_ies_len = pos - ie_start;
 2334
 2335	info.link_id = assoc_data->assoc_link_id;
 2336	drv_mgd_prepare_tx(local, sdata, &info);
 2337
 2338	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
 2339	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
 2340		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
 2341						IEEE80211_TX_INTFL_MLME_CONN_TX;
 2342	ieee80211_tx_skb(sdata, skb);
 2343
 2344	return 0;
 2345}
 2346
 2347void ieee80211_send_pspoll(struct ieee80211_local *local,
 2348			   struct ieee80211_sub_if_data *sdata)
 2349{
 2350	struct ieee80211_pspoll *pspoll;
 2351	struct sk_buff *skb;
 2352
 2353	skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
 2354	if (!skb)
 2355		return;
 2356
 2357	pspoll = (struct ieee80211_pspoll *) skb->data;
 2358	pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
 2359
 2360	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
 2361	ieee80211_tx_skb(sdata, skb);
 2362}
 2363
 2364void ieee80211_send_nullfunc(struct ieee80211_local *local,
 2365			     struct ieee80211_sub_if_data *sdata,
 2366			     bool powersave)
 2367{
 2368	struct sk_buff *skb;
 2369	struct ieee80211_hdr_3addr *nullfunc;
 2370	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2371
 2372	skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif, -1,
 2373				     !ieee80211_hw_check(&local->hw,
 2374							 DOESNT_SUPPORT_QOS_NDP));
 2375	if (!skb)
 2376		return;
 2377
 2378	nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
 2379	if (powersave)
 2380		nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
 2381
 2382	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
 2383					IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
 2384
 2385	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
 2386		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
 2387
 2388	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
 2389		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
 2390
 2391	ieee80211_tx_skb(sdata, skb);
 2392}
 2393
 2394void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
 2395				   struct ieee80211_sub_if_data *sdata)
 2396{
 2397	struct sk_buff *skb;
 2398	struct ieee80211_hdr *nullfunc;
 2399	__le16 fc;
 2400
 2401	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
 2402		return;
 2403
 2404	skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
 2405	if (!skb)
 2406		return;
 2407
 2408	skb_reserve(skb, local->hw.extra_tx_headroom);
 2409
 2410	nullfunc = skb_put_zero(skb, 30);
 2411	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
 2412			 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
 2413	nullfunc->frame_control = fc;
 2414	memcpy(nullfunc->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
 2415	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
 2416	memcpy(nullfunc->addr3, sdata->deflink.u.mgd.bssid, ETH_ALEN);
 2417	memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
 2418
 2419	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
 2420	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
 2421	ieee80211_tx_skb(sdata, skb);
 2422}
 2423
 2424/* spectrum management related things */
 2425static void ieee80211_csa_switch_work(struct wiphy *wiphy,
 2426				      struct wiphy_work *work)
 2427{
 2428	struct ieee80211_link_data *link =
 2429		container_of(work, struct ieee80211_link_data,
 2430			     u.mgd.csa.switch_work.work);
 2431	struct ieee80211_sub_if_data *sdata = link->sdata;
 2432	struct ieee80211_local *local = sdata->local;
 2433	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2434	int ret;
 2435
 2436	if (!ieee80211_sdata_running(sdata))
 2437		return;
 2438
 2439	lockdep_assert_wiphy(local->hw.wiphy);
 2440
 2441	if (!ifmgd->associated)
 2442		return;
 2443
 2444	if (!link->conf->csa_active)
 2445		return;
 2446
 2447	/*
 2448	 * If the link isn't active (now), we cannot wait for beacons, won't
 2449	 * have a reserved chanctx, etc. Just switch over the chandef and
 2450	 * update cfg80211 directly.
 2451	 */
 2452	if (!ieee80211_vif_link_active(&sdata->vif, link->link_id)) {
 2453		struct link_sta_info *link_sta;
 2454		struct sta_info *ap_sta;
 2455
 2456		link->conf->chanreq = link->csa.chanreq;
 2457		cfg80211_ch_switch_notify(sdata->dev, &link->csa.chanreq.oper,
 2458					  link->link_id);
 2459		link->conf->csa_active = false;
 2460
 2461		ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
 2462		if (WARN_ON(!ap_sta))
 2463			return;
 2464
 2465		link_sta = wiphy_dereference(wiphy,
 2466					     ap_sta->link[link->link_id]);
 2467		if (WARN_ON(!link_sta))
 2468			return;
 2469
 2470		link_sta->pub->bandwidth =
 2471			_ieee80211_sta_cur_vht_bw(link_sta,
 2472						  &link->csa.chanreq.oper);
 2473		return;
 2474	}
 2475
 2476	/*
 2477	 * using reservation isn't immediate as it may be deferred until later
 2478	 * with multi-vif. once reservation is complete it will re-schedule the
 2479	 * work with no reserved_chanctx so verify chandef to check if it
 2480	 * completed successfully
 2481	 */
 2482
 2483	if (link->reserved_chanctx) {
 2484		/*
 2485		 * with multi-vif csa driver may call ieee80211_csa_finish()
 2486		 * many times while waiting for other interfaces to use their
 2487		 * reservations
 2488		 */
 2489		if (link->reserved_ready)
 2490			return;
 2491
 2492		ret = ieee80211_link_use_reserved_context(link);
 2493		if (ret) {
 2494			link_info(link,
 2495				  "failed to use reserved channel context, disconnecting (err=%d)\n",
 2496				  ret);
 2497			wiphy_work_queue(sdata->local->hw.wiphy,
 2498					 &ifmgd->csa_connection_drop_work);
 2499		}
 2500		return;
 2501	}
 2502
 2503	if (!ieee80211_chanreq_identical(&link->conf->chanreq,
 2504					 &link->csa.chanreq)) {
 2505		link_info(link,
 2506			  "failed to finalize channel switch, disconnecting\n");
 2507		wiphy_work_queue(sdata->local->hw.wiphy,
 2508				 &ifmgd->csa_connection_drop_work);
 2509		return;
 2510	}
 2511
 2512	link->u.mgd.csa.waiting_bcn = true;
 2513
 2514	/*
 2515	 * The next beacon really should always be different, so this should
 2516	 * have no effect whatsoever. However, some APs (we observed this in
 2517	 * an Asus AXE11000), the beacon after the CSA might be identical to
 2518	 * the last beacon on the old channel - in this case we'd ignore it.
 2519	 * Resetting the CRC will lead us to handle it better (albeit with a
 2520	 * disconnect, but clearly the AP is broken.)
 2521	 */
 2522	link->u.mgd.beacon_crc_valid = false;
 2523
 2524	/* apply new TPE restrictions immediately on the new channel */
 2525	if (link->u.mgd.csa.ap_chandef.chan->band == NL80211_BAND_6GHZ &&
 2526	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE) {
 2527		ieee80211_rearrange_tpe(&link->u.mgd.csa.tpe,
 2528					&link->u.mgd.csa.ap_chandef,
 2529					&link->conf->chanreq.oper);
 2530		if (memcmp(&link->conf->tpe, &link->u.mgd.csa.tpe,
 2531			   sizeof(link->u.mgd.csa.tpe))) {
 2532			link->conf->tpe = link->u.mgd.csa.tpe;
 2533			ieee80211_link_info_change_notify(sdata, link,
 2534							  BSS_CHANGED_TPE);
 2535		}
 2536	}
 2537
 2538	/*
 2539	 * It is not necessary to reset these timers if any link does not
 2540	 * have an active CSA and that link still receives the beacons
 2541	 * when other links have active CSA.
 2542	 */
 2543	for_each_link_data(sdata, link) {
 2544		if (!link->conf->csa_active)
 2545			return;
 2546	}
 2547
 2548	/*
 2549	 * Reset the beacon monitor and connection monitor timers when CSA
 2550	 * is active for all links in MLO when channel switch occurs in all
 2551	 * the links.
 2552	 */
 2553	ieee80211_sta_reset_beacon_monitor(sdata);
 2554	ieee80211_sta_reset_conn_monitor(sdata);
 2555}
 2556
 2557static void ieee80211_chswitch_post_beacon(struct ieee80211_link_data *link)
 2558{
 2559	struct ieee80211_sub_if_data *sdata = link->sdata;
 2560	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2561	int ret;
 2562
 2563	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 2564
 2565	WARN_ON(!link->conf->csa_active);
 2566
 2567	ieee80211_vif_unblock_queues_csa(sdata);
 2568
 2569	link->conf->csa_active = false;
 2570	link->u.mgd.csa.blocked_tx = false;
 2571	link->u.mgd.csa.waiting_bcn = false;
 2572
 2573	ret = drv_post_channel_switch(link);
 2574	if (ret) {
 2575		link_info(link,
 2576			  "driver post channel switch failed, disconnecting\n");
 2577		wiphy_work_queue(sdata->local->hw.wiphy,
 2578				 &ifmgd->csa_connection_drop_work);
 2579		return;
 2580	}
 2581
 2582	cfg80211_ch_switch_notify(sdata->dev, &link->conf->chanreq.oper,
 2583				  link->link_id);
 2584}
 2585
 2586void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success,
 2587			     unsigned int link_id)
 2588{
 2589	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 2590
 2591	trace_api_chswitch_done(sdata, success, link_id);
 2592
 2593	rcu_read_lock();
 2594
 2595	if (!success) {
 2596		sdata_info(sdata,
 2597			   "driver channel switch failed (link %d), disconnecting\n",
 2598			   link_id);
 2599		wiphy_work_queue(sdata->local->hw.wiphy,
 2600				 &sdata->u.mgd.csa_connection_drop_work);
 2601	} else {
 2602		struct ieee80211_link_data *link =
 2603			rcu_dereference(sdata->link[link_id]);
 2604
 2605		if (WARN_ON(!link)) {
 2606			rcu_read_unlock();
 2607			return;
 2608		}
 2609
 2610		wiphy_hrtimer_work_queue(sdata->local->hw.wiphy,
 2611					 &link->u.mgd.csa.switch_work, 0);
 2612	}
 2613
 2614	rcu_read_unlock();
 2615}
 2616EXPORT_SYMBOL(ieee80211_chswitch_done);
 2617
 2618static void
 2619ieee80211_sta_abort_chanswitch(struct ieee80211_link_data *link)
 2620{
 2621	struct ieee80211_sub_if_data *sdata = link->sdata;
 2622	struct ieee80211_local *local = sdata->local;
 2623
 2624	lockdep_assert_wiphy(local->hw.wiphy);
 2625
 2626	if (!local->ops->abort_channel_switch)
 2627		return;
 2628
 2629	if (rcu_access_pointer(link->conf->chanctx_conf))
 2630		ieee80211_link_unreserve_chanctx(link);
 2631
 2632	ieee80211_vif_unblock_queues_csa(sdata);
 2633
 2634	link->conf->csa_active = false;
 2635	link->u.mgd.csa.blocked_tx = false;
 2636
 2637	drv_abort_channel_switch(link);
 2638}
 2639
 2640struct sta_csa_rnr_iter_data {
 2641	struct ieee80211_link_data *link;
 2642	struct ieee80211_channel *chan;
 2643	u8 mld_id;
 2644};
 2645
 2646static enum cfg80211_rnr_iter_ret
 2647ieee80211_sta_csa_rnr_iter(void *_data, u8 type,
 2648			   const struct ieee80211_neighbor_ap_info *info,
 2649			   const u8 *tbtt_info, u8 tbtt_info_len)
 2650{
 2651	struct sta_csa_rnr_iter_data *data = _data;
 2652	struct ieee80211_link_data *link = data->link;
 2653	struct ieee80211_sub_if_data *sdata = link->sdata;
 2654	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2655	const struct ieee80211_tbtt_info_ge_11 *ti;
 2656	enum nl80211_band band;
 2657	unsigned int center_freq;
 2658	int link_id;
 2659
 2660	if (type != IEEE80211_TBTT_INFO_TYPE_TBTT)
 2661		return RNR_ITER_CONTINUE;
 2662
 2663	if (tbtt_info_len < sizeof(*ti))
 2664		return RNR_ITER_CONTINUE;
 2665
 2666	ti = (const void *)tbtt_info;
 2667
 2668	if (ti->mld_params.mld_id != data->mld_id)
 2669		return RNR_ITER_CONTINUE;
 2670
 2671	link_id = le16_get_bits(ti->mld_params.params,
 2672				IEEE80211_RNR_MLD_PARAMS_LINK_ID);
 2673	if (link_id != data->link->link_id)
 2674		return RNR_ITER_CONTINUE;
 2675
 2676	/* we found the entry for our link! */
 2677
 2678	/* this AP is confused, it had this right before ... just disconnect */
 2679	if (!ieee80211_operating_class_to_band(info->op_class, &band)) {
 2680		link_info(link,
 2681			  "AP now has invalid operating class in RNR, disconnect\n");
 2682		wiphy_work_queue(sdata->local->hw.wiphy,
 2683				 &ifmgd->csa_connection_drop_work);
 2684		return RNR_ITER_BREAK;
 2685	}
 2686
 2687	center_freq = ieee80211_channel_to_frequency(info->channel, band);
 2688	data->chan = ieee80211_get_channel(sdata->local->hw.wiphy, center_freq);
 2689
 2690	return RNR_ITER_BREAK;
 2691}
 2692
 2693static void
 2694ieee80211_sta_other_link_csa_disappeared(struct ieee80211_link_data *link,
 2695					 struct ieee802_11_elems *elems)
 2696{
 2697	struct ieee80211_sub_if_data *sdata = link->sdata;
 2698	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2699	struct sta_csa_rnr_iter_data data = {
 2700		.link = link,
 2701	};
 2702
 2703	/*
 2704	 * If we get here, we see a beacon from another link without
 2705	 * CSA still being reported for it, so now we have to check
 2706	 * if the CSA was aborted or completed. This may not even be
 2707	 * perfectly possible if the CSA was only done for changing
 2708	 * the puncturing, but in that case if the link in inactive
 2709	 * we don't really care, and if it's an active link (or when
 2710	 * it's activated later) we'll get a beacon and adjust.
 2711	 */
 2712
 2713	if (WARN_ON(!elems->ml_basic))
 2714		return;
 2715
 2716	data.mld_id = ieee80211_mle_get_mld_id((const void *)elems->ml_basic);
 2717
 2718	/*
 2719	 * So in order to do this, iterate the RNR element(s) and see
 2720	 * what channel is reported now.
 2721	 */
 2722	cfg80211_iter_rnr(elems->ie_start, elems->total_len,
 2723			  ieee80211_sta_csa_rnr_iter, &data);
 2724
 2725	if (!data.chan) {
 2726		link_info(link,
 2727			  "couldn't find (valid) channel in RNR for CSA, disconnect\n");
 2728		wiphy_work_queue(sdata->local->hw.wiphy,
 2729				 &ifmgd->csa_connection_drop_work);
 2730		return;
 2731	}
 2732
 2733	/*
 2734	 * If it doesn't match the CSA, then assume it aborted. This
 2735	 * may erroneously detect that it was _not_ aborted when it
 2736	 * was in fact aborted, but only changed the bandwidth or the
 2737	 * puncturing configuration, but we don't have enough data to
 2738	 * detect that.
 2739	 */
 2740	if (data.chan != link->csa.chanreq.oper.chan)
 2741		ieee80211_sta_abort_chanswitch(link);
 2742}
 2743
 2744enum ieee80211_csa_source {
 2745	IEEE80211_CSA_SOURCE_BEACON,
 2746	IEEE80211_CSA_SOURCE_OTHER_LINK,
 2747	IEEE80211_CSA_SOURCE_PROT_ACTION,
 2748	IEEE80211_CSA_SOURCE_UNPROT_ACTION,
 2749};
 2750
 2751static void
 2752ieee80211_sta_process_chanswitch(struct ieee80211_link_data *link,
 2753				 u64 timestamp, u32 device_timestamp,
 2754				 struct ieee802_11_elems *full_elems,
 2755				 struct ieee802_11_elems *csa_elems,
 2756				 enum ieee80211_csa_source source)
 2757{
 2758	struct ieee80211_sub_if_data *sdata = link->sdata;
 2759	struct ieee80211_local *local = sdata->local;
 2760	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 2761	struct ieee80211_chanctx *chanctx = NULL;
 2762	struct ieee80211_chanctx_conf *conf;
 2763	struct ieee80211_csa_ie csa_ie = {};
 2764	struct ieee80211_channel_switch ch_switch = {
 2765		.link_id = link->link_id,
 2766		.timestamp = timestamp,
 2767		.device_timestamp = device_timestamp,
 2768	};
 2769	u32 csa_time_tu;
 2770	ktime_t now;
 2771	int res;
 2772
 2773	lockdep_assert_wiphy(local->hw.wiphy);
 2774
 2775	if (csa_elems) {
 2776		struct cfg80211_bss *cbss = link->conf->bss;
 2777		enum nl80211_band current_band;
 2778		struct ieee80211_bss *bss;
 2779
 2780		if (WARN_ON(!cbss))
 2781			return;
 2782
 2783		current_band = cbss->channel->band;
 2784		bss = (void *)cbss->priv;
 2785
 2786		res = ieee80211_parse_ch_switch_ie(sdata, csa_elems,
 2787						   current_band,
 2788						   bss->vht_cap_info,
 2789						   &link->u.mgd.conn,
 2790						   link->u.mgd.bssid,
 2791						   source == IEEE80211_CSA_SOURCE_UNPROT_ACTION,
 2792						   &csa_ie);
 2793		if (res == 0) {
 2794			ch_switch.block_tx = csa_ie.mode;
 2795			ch_switch.chandef = csa_ie.chanreq.oper;
 2796			ch_switch.count = csa_ie.count;
 2797			ch_switch.delay = csa_ie.max_switch_time;
 2798		}
 2799
 2800		link->u.mgd.csa.tpe = csa_elems->csa_tpe;
 2801	} else {
 2802		/*
 2803		 * If there was no per-STA profile for this link, we
 2804		 * get called with csa_elems == NULL. This of course means
 2805		 * there are no CSA elements, so set res=1 indicating
 2806		 * no more CSA.
 2807		 */
 2808		res = 1;
 2809	}
 2810
 2811	if (res < 0) {
 2812		/* ignore this case, not a protected frame */
 2813		if (source == IEEE80211_CSA_SOURCE_UNPROT_ACTION)
 2814			return;
 2815		goto drop_connection;
 2816	}
 2817
 2818	if (link->conf->csa_active) {
 2819		switch (source) {
 2820		case IEEE80211_CSA_SOURCE_PROT_ACTION:
 2821		case IEEE80211_CSA_SOURCE_UNPROT_ACTION:
 2822			/* already processing - disregard action frames */
 2823			return;
 2824		case IEEE80211_CSA_SOURCE_BEACON:
 2825			if (link->u.mgd.csa.waiting_bcn) {
 2826				ieee80211_chswitch_post_beacon(link);
 2827				/*
 2828				 * If the CSA is still present after the switch
 2829				 * we need to consider it as a new CSA (possibly
 2830				 * to self). This happens by not returning here
 2831				 * so we'll get to the check below.
 2832				 */
 2833			} else if (res) {
 2834				ieee80211_sta_abort_chanswitch(link);
 2835				return;
 2836			} else {
 2837				drv_channel_switch_rx_beacon(sdata, &ch_switch);
 2838				return;
 2839			}
 2840			break;
 2841		case IEEE80211_CSA_SOURCE_OTHER_LINK:
 2842			/* active link: we want to see the beacon to continue */
 2843			if (ieee80211_vif_link_active(&sdata->vif,
 2844						      link->link_id))
 2845				return;
 2846
 2847			/* switch work ran, so just complete the process */
 2848			if (link->u.mgd.csa.waiting_bcn) {
 2849				ieee80211_chswitch_post_beacon(link);
 2850				/*
 2851				 * If the CSA is still present after the switch
 2852				 * we need to consider it as a new CSA (possibly
 2853				 * to self). This happens by not returning here
 2854				 * so we'll get to the check below.
 2855				 */
 2856				break;
 2857			}
 2858
 2859			/* link still has CSA but we already know, do nothing */
 2860			if (!res)
 2861				return;
 2862
 2863			/* check in the RNR if the CSA aborted */
 2864			ieee80211_sta_other_link_csa_disappeared(link,
 2865								 full_elems);
 2866			return;
 2867		}
 2868	}
 2869
 2870	/* no active CSA nor a new one */
 2871	if (res) {
 2872		/*
 2873		 * However, we may have stopped queues when receiving a public
 2874		 * action frame that couldn't be protected, if it had the quiet
 2875		 * bit set. This is a trade-off, we want to be quiet as soon as
 2876		 * possible, but also don't trust the public action frame much,
 2877		 * as it can't be protected.
 2878		 */
 2879		if (unlikely(link->u.mgd.csa.blocked_tx)) {
 2880			link->u.mgd.csa.blocked_tx = false;
 2881			ieee80211_vif_unblock_queues_csa(sdata);
 2882		}
 2883		return;
 2884	}
 2885
 2886	/*
 2887	 * We don't really trust public action frames, but block queues (go to
 2888	 * quiet mode) for them anyway, we should get a beacon soon to either
 2889	 * know what the CSA really is, or figure out the public action frame
 2890	 * was actually an attack.
 2891	 */
 2892	if (source == IEEE80211_CSA_SOURCE_UNPROT_ACTION) {
 2893		if (csa_ie.mode) {
 2894			link->u.mgd.csa.blocked_tx = true;
 2895			ieee80211_vif_block_queues_csa(sdata);
 2896		}
 2897		return;
 2898	}
 2899
 2900	if (link->conf->chanreq.oper.chan->band !=
 2901	    csa_ie.chanreq.oper.chan->band) {
 2902		link_info(link,
 2903			  "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
 2904			  link->u.mgd.bssid,
 2905			  csa_ie.chanreq.oper.chan->center_freq,
 2906			  csa_ie.chanreq.oper.width,
 2907			  csa_ie.chanreq.oper.center_freq1,
 2908			  csa_ie.chanreq.oper.center_freq2);
 2909		goto drop_connection;
 2910	}
 2911
 2912	if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chanreq.oper,
 2913				     IEEE80211_CHAN_DISABLED)) {
 2914		link_info(link,
 2915			  "AP %pM switches to unsupported channel (%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), disconnecting\n",
 2916			  link->u.mgd.bssid,
 2917			  csa_ie.chanreq.oper.chan->center_freq,
 2918			  csa_ie.chanreq.oper.chan->freq_offset,
 2919			  csa_ie.chanreq.oper.width,
 2920			  csa_ie.chanreq.oper.center_freq1,
 2921			  csa_ie.chanreq.oper.freq1_offset,
 2922			  csa_ie.chanreq.oper.center_freq2);
 2923		goto drop_connection;
 2924	}
 2925
 2926	if (cfg80211_chandef_identical(&csa_ie.chanreq.oper,
 2927				       &link->conf->chanreq.oper) &&
 2928	    (!csa_ie.mode || source != IEEE80211_CSA_SOURCE_BEACON)) {
 2929		if (link->u.mgd.csa.ignored_same_chan)
 2930			return;
 2931		link_info(link,
 2932			  "AP %pM tries to chanswitch to same channel, ignore\n",
 2933			  link->u.mgd.bssid);
 2934		link->u.mgd.csa.ignored_same_chan = true;
 2935		return;
 2936	}
 2937
 2938	/*
 2939	 * Drop all TDLS peers on the affected link - either we disconnect or
 2940	 * move to a different channel from this point on. There's no telling
 2941	 * what our peer will do.
 2942	 * The TDLS WIDER_BW scenario is also problematic, as peers might now
 2943	 * have an incompatible wider chandef.
 2944	 */
 2945	ieee80211_teardown_tdls_peers(link);
 2946
 2947	conf = rcu_dereference_protected(link->conf->chanctx_conf,
 2948					 lockdep_is_held(&local->hw.wiphy->mtx));
 2949	if (ieee80211_vif_link_active(&sdata->vif, link->link_id) && !conf) {
 2950		link_info(link,
 2951			  "no channel context assigned to vif?, disconnecting\n");
 2952		goto drop_connection;
 2953	}
 2954
 2955	if (conf)
 2956		chanctx = container_of(conf, struct ieee80211_chanctx, conf);
 2957
 2958	if (!ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
 2959		link_info(link,
 2960			  "driver doesn't support chan-switch with channel contexts\n");
 2961		goto drop_connection;
 2962	}
 2963
 2964	if (drv_pre_channel_switch(sdata, &ch_switch)) {
 2965		link_info(link,
 2966			  "preparing for channel switch failed, disconnecting\n");
 2967		goto drop_connection;
 2968	}
 2969
 2970	link->u.mgd.csa.ap_chandef = csa_ie.chanreq.ap;
 2971
 2972	link->csa.chanreq.oper = csa_ie.chanreq.oper;
 2973	ieee80211_set_chanreq_ap(sdata, &link->csa.chanreq, &link->u.mgd.conn,
 2974				 &csa_ie.chanreq.ap);
 2975
 2976	if (chanctx) {
 2977		res = ieee80211_link_reserve_chanctx(link, &link->csa.chanreq,
 2978						     chanctx->mode, false);
 2979		if (res) {
 2980			link_info(link,
 2981				  "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
 2982				  res);
 2983			goto drop_connection;
 2984		}
 2985	}
 2986
 2987	link->conf->csa_active = true;
 2988	link->u.mgd.csa.ignored_same_chan = false;
 2989	link->u.mgd.beacon_crc_valid = false;
 2990	link->u.mgd.csa.blocked_tx = csa_ie.mode;
 2991
 2992	if (csa_ie.mode)
 2993		ieee80211_vif_block_queues_csa(sdata);
 2994
 2995	cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chanreq.oper,
 2996					  link->link_id, csa_ie.count,
 2997					  csa_ie.mode);
 2998
 2999	/* we may have to handle timeout for deactivated link in software */
 3000	now = ktime_get_boottime();
 3001	csa_time_tu = (max_t(int, csa_ie.count, 1) - 1) * link->conf->beacon_int;
 3002	link->u.mgd.csa.time = now + us_to_ktime(ieee80211_tu_to_usec(csa_time_tu));
 3003
 3004	if (ieee80211_vif_link_active(&sdata->vif, link->link_id) &&
 3005	    local->ops->channel_switch) {
 3006		/*
 3007		 * Use driver's channel switch callback, the driver will
 3008		 * later call ieee80211_chswitch_done(). It may deactivate
 3009		 * the link as well, we handle that elsewhere and queue
 3010		 * the csa.switch_work for the calculated time then.
 3011		 */
 3012		drv_channel_switch(local, sdata, &ch_switch);
 3013		return;
 3014	}
 3015
 3016	/* channel switch handled in software */
 3017	wiphy_hrtimer_work_queue(local->hw.wiphy,
 3018				 &link->u.mgd.csa.switch_work,
 3019				 link->u.mgd.csa.time - now);
 3020	return;
 3021 drop_connection:
 3022	/*
 3023	 * This is just so that the disconnect flow will know that
 3024	 * we were trying to switch channel and failed. In case the
 3025	 * mode is 1 (we are not allowed to Tx), we will know not to
 3026	 * send a deauthentication frame. Those two fields will be
 3027	 * reset when the disconnection worker runs.
 3028	 */
 3029	link->conf->csa_active = true;
 3030	link->u.mgd.csa.blocked_tx = csa_ie.mode;
 3031
 3032	wiphy_work_queue(sdata->local->hw.wiphy,
 3033			 &ifmgd->csa_connection_drop_work);
 3034}
 3035
 3036struct sta_bss_param_ch_cnt_data {
 3037	struct ieee80211_sub_if_data *sdata;
 3038	u8 reporting_link_id;
 3039	u8 mld_id;
 3040};
 3041
 3042static enum cfg80211_rnr_iter_ret
 3043ieee80211_sta_bss_param_ch_cnt_iter(void *_data, u8 type,
 3044				    const struct ieee80211_neighbor_ap_info *info,
 3045				    const u8 *tbtt_info, u8 tbtt_info_len)
 3046{
 3047	struct sta_bss_param_ch_cnt_data *data = _data;
 3048	struct ieee80211_sub_if_data *sdata = data->sdata;
 3049	const struct ieee80211_tbtt_info_ge_11 *ti;
 3050	u8 bss_param_ch_cnt;
 3051	int link_id;
 3052
 3053	if (type != IEEE80211_TBTT_INFO_TYPE_TBTT)
 3054		return RNR_ITER_CONTINUE;
 3055
 3056	if (tbtt_info_len < sizeof(*ti))
 3057		return RNR_ITER_CONTINUE;
 3058
 3059	ti = (const void *)tbtt_info;
 3060
 3061	if (ti->mld_params.mld_id != data->mld_id)
 3062		return RNR_ITER_CONTINUE;
 3063
 3064	link_id = le16_get_bits(ti->mld_params.params,
 3065				IEEE80211_RNR_MLD_PARAMS_LINK_ID);
 3066	bss_param_ch_cnt =
 3067		le16_get_bits(ti->mld_params.params,
 3068			      IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT);
 3069
 3070	if (bss_param_ch_cnt != 255 &&
 3071	    link_id < ARRAY_SIZE(sdata->link)) {
 3072		struct ieee80211_link_data *link =
 3073			sdata_dereference(sdata->link[link_id], sdata);
 3074
 3075		if (link && link->conf->bss_param_ch_cnt != bss_param_ch_cnt) {
 3076			link->conf->bss_param_ch_cnt = bss_param_ch_cnt;
 3077			link->conf->bss_param_ch_cnt_link_id =
 3078				data->reporting_link_id;
 3079		}
 3080	}
 3081
 3082	return RNR_ITER_CONTINUE;
 3083}
 3084
 3085static void
 3086ieee80211_mgd_update_bss_param_ch_cnt(struct ieee80211_sub_if_data *sdata,
 3087				      struct ieee80211_bss_conf *bss_conf,
 3088				      struct ieee802_11_elems *elems)
 3089{
 3090	struct sta_bss_param_ch_cnt_data data = {
 3091		.reporting_link_id = bss_conf->link_id,
 3092		.sdata = sdata,
 3093	};
 3094	int bss_param_ch_cnt;
 3095
 3096	if (!elems->ml_basic)
 3097		return;
 3098
 3099	data.mld_id = ieee80211_mle_get_mld_id((const void *)elems->ml_basic);
 3100
 3101	cfg80211_iter_rnr(elems->ie_start, elems->total_len,
 3102			  ieee80211_sta_bss_param_ch_cnt_iter, &data);
 3103
 3104	bss_param_ch_cnt =
 3105		ieee80211_mle_get_bss_param_ch_cnt((const void *)elems->ml_basic);
 3106
 3107	/*
 3108	 * Update bss_param_ch_cnt_link_id even if bss_param_ch_cnt
 3109	 * didn't change to indicate that we got a beacon on our own
 3110	 * link.
 3111	 */
 3112	if (bss_param_ch_cnt >= 0 && bss_param_ch_cnt != 255) {
 3113		bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
 3114		bss_conf->bss_param_ch_cnt_link_id =
 3115			bss_conf->link_id;
 3116	}
 3117}
 3118
 3119static bool
 3120ieee80211_find_80211h_pwr_constr(struct ieee80211_channel *channel,
 3121				 const u8 *country_ie, u8 country_ie_len,
 3122				 const u8 *pwr_constr_elem,
 3123				 int *chan_pwr, int *pwr_reduction)
 3124{
 3125	struct ieee80211_country_ie_triplet *triplet;
 3126	int chan = ieee80211_frequency_to_channel(channel->center_freq);
 3127	int i, chan_increment;
 3128	bool have_chan_pwr = false;
 3129
 3130	/* Invalid IE */
 3131	if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
 3132		return false;
 3133
 3134	triplet = (void *)(country_ie + 3);
 3135	country_ie_len -= 3;
 3136
 3137	switch (channel->band) {
 3138	default:
 3139		WARN_ON_ONCE(1);
 3140		fallthrough;
 3141	case NL80211_BAND_2GHZ:
 3142	case NL80211_BAND_60GHZ:
 3143	case NL80211_BAND_LC:
 3144		chan_increment = 1;
 3145		break;
 3146	case NL80211_BAND_5GHZ:
 3147		chan_increment = 4;
 3148		break;
 3149	case NL80211_BAND_6GHZ:
 3150		/*
 3151		 * In the 6 GHz band, the "maximum transmit power level"
 3152		 * field in the triplets is reserved, and thus will be
 3153		 * zero and we shouldn't use it to control TX power.
 3154		 * The actual TX power will be given in the transmit
 3155		 * power envelope element instead.
 3156		 */
 3157		return false;
 3158	}
 3159
 3160	/* find channel */
 3161	while (country_ie_len >= 3) {
 3162		u8 first_channel = triplet->chans.first_channel;
 3163
 3164		if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
 3165			goto next;
 3166
 3167		for (i = 0; i < triplet->chans.num_channels; i++) {
 3168			if (first_channel + i * chan_increment == chan) {
 3169				have_chan_pwr = true;
 3170				*chan_pwr = triplet->chans.max_power;
 3171				break;
 3172			}
 3173		}
 3174		if (have_chan_pwr)
 3175			break;
 3176
 3177 next:
 3178		triplet++;
 3179		country_ie_len -= 3;
 3180	}
 3181
 3182	if (have_chan_pwr && pwr_constr_elem)
 3183		*pwr_reduction = *pwr_constr_elem;
 3184	else
 3185		*pwr_reduction = 0;
 3186
 3187	return have_chan_pwr;
 3188}
 3189
 3190static void ieee80211_find_cisco_dtpc(struct ieee80211_channel *channel,
 3191				      const u8 *cisco_dtpc_ie,
 3192				      int *pwr_level)
 3193{
 3194	/* From practical testing, the first data byte of the DTPC element
 3195	 * seems to contain the requested dBm level, and the CLI on Cisco
 3196	 * APs clearly state the range is -127 to 127 dBm, which indicates
 3197	 * a signed byte, although it seemingly never actually goes negative.
 3198	 * The other byte seems to always be zero.
 3199	 */
 3200	*pwr_level = (__s8)cisco_dtpc_ie[4];
 3201}
 3202
 3203static u64 ieee80211_handle_pwr_constr(struct ieee80211_link_data *link,
 3204				       struct ieee80211_channel *channel,
 3205				       struct ieee80211_mgmt *mgmt,
 3206				       const u8 *country_ie, u8 country_ie_len,
 3207				       const u8 *pwr_constr_ie,
 3208				       const u8 *cisco_dtpc_ie)
 3209{
 3210	struct ieee80211_sub_if_data *sdata = link->sdata;
 3211	bool has_80211h_pwr = false, has_cisco_pwr = false;
 3212	int chan_pwr = 0, pwr_reduction_80211h = 0;
 3213	int pwr_level_cisco, pwr_level_80211h;
 3214	int new_ap_level;
 3215	__le16 capab = mgmt->u.probe_resp.capab_info;
 3216
 3217	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
 3218		return 0;	/* TODO */
 3219
 3220	if (country_ie &&
 3221	    (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
 3222	     capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
 3223		has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
 3224			channel, country_ie, country_ie_len,
 3225			pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
 3226		pwr_level_80211h =
 3227			max_t(int, 0, chan_pwr - pwr_reduction_80211h);
 3228	}
 3229
 3230	if (cisco_dtpc_ie) {
 3231		ieee80211_find_cisco_dtpc(
 3232			channel, cisco_dtpc_ie, &pwr_level_cisco);
 3233		has_cisco_pwr = true;
 3234	}
 3235
 3236	if (!has_80211h_pwr && !has_cisco_pwr)
 3237		return 0;
 3238
 3239	/* If we have both 802.11h and Cisco DTPC, apply both limits
 3240	 * by picking the smallest of the two power levels advertised.
 3241	 */
 3242	if (has_80211h_pwr &&
 3243	    (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
 3244		new_ap_level = pwr_level_80211h;
 3245
 3246		if (link->ap_power_level == new_ap_level)
 3247			return 0;
 3248
 3249		sdata_dbg(sdata,
 3250			  "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
 3251			  pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
 3252			  link->u.mgd.bssid);
 3253	} else {  /* has_cisco_pwr is always true here. */
 3254		new_ap_level = pwr_level_cisco;
 3255
 3256		if (link->ap_power_level == new_ap_level)
 3257			return 0;
 3258
 3259		sdata_dbg(sdata,
 3260			  "Limiting TX power to %d dBm as advertised by %pM\n",
 3261			  pwr_level_cisco, link->u.mgd.bssid);
 3262	}
 3263
 3264	link->ap_power_level = new_ap_level;
 3265	if (__ieee80211_recalc_txpower(link))
 3266		return BSS_CHANGED_TXPOWER;
 3267	return 0;
 3268}
 3269
 3270/* powersave */
 3271static void ieee80211_enable_ps(struct ieee80211_local *local,
 3272				struct ieee80211_sub_if_data *sdata)
 3273{
 3274	struct ieee80211_conf *conf = &local->hw.conf;
 3275
 3276	/*
 3277	 * If we are scanning right now then the parameters will
 3278	 * take effect when scan finishes.
 3279	 */
 3280	if (local->scanning)
 3281		return;
 3282
 3283	if (conf->dynamic_ps_timeout > 0 &&
 3284	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
 3285		mod_timer(&local->dynamic_ps_timer, jiffies +
 3286			  msecs_to_jiffies(conf->dynamic_ps_timeout));
 3287	} else {
 3288		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
 3289			ieee80211_send_nullfunc(local, sdata, true);
 3290
 3291		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
 3292		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
 3293			return;
 3294
 3295		conf->flags |= IEEE80211_CONF_PS;
 3296		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
 3297	}
 3298}
 3299
 3300static void ieee80211_change_ps(struct ieee80211_local *local)
 3301{
 3302	struct ieee80211_conf *conf = &local->hw.conf;
 3303
 3304	if (local->ps_sdata) {
 3305		ieee80211_enable_ps(local, local->ps_sdata);
 3306	} else if (conf->flags & IEEE80211_CONF_PS) {
 3307		conf->flags &= ~IEEE80211_CONF_PS;
 3308		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
 3309		timer_delete_sync(&local->dynamic_ps_timer);
 3310		wiphy_work_cancel(local->hw.wiphy,
 3311				  &local->dynamic_ps_enable_work);
 3312	}
 3313}
 3314
 3315static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
 3316{
 3317	struct ieee80211_local *local = sdata->local;
 3318	struct ieee80211_if_managed *mgd = &sdata->u.mgd;
 3319	struct sta_info *sta = NULL;
 3320	bool authorized = false;
 3321
 3322	if (!mgd->powersave)
 3323		return false;
 3324
 3325	if (mgd->broken_ap)
 3326		return false;
 3327
 3328	if (!mgd->associated)
 3329		return false;
 3330
 3331	if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
 3332		return false;
 3333
 3334	if (!(local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO) &&
 3335	    !sdata->deflink.u.mgd.have_beacon)
 3336		return false;
 3337
 3338	rcu_read_lock();
 3339	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
 3340	if (sta)
 3341		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
 3342	rcu_read_unlock();
 3343
 3344	return authorized;
 3345}
 3346
 3347/* need to hold RTNL or interface lock */
 3348void ieee80211_recalc_ps(struct ieee80211_local *local)
 3349{
 3350	struct ieee80211_sub_if_data *sdata, *found = NULL;
 3351	int count = 0;
 3352	int timeout;
 3353
 3354	if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS) ||
 3355	    ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
 3356		local->ps_sdata = NULL;
 3357		return;
 3358	}
 3359
 3360	list_for_each_entry(sdata, &local->interfaces, list) {
 3361		if (!ieee80211_sdata_running(sdata))
 3362			continue;
 3363		if (sdata->vif.type == NL80211_IFTYPE_AP) {
 3364			/* If an AP vif is found, then disable PS
 3365			 * by setting the count to zero thereby setting
 3366			 * ps_sdata to NULL.
 3367			 */
 3368			count = 0;
 3369			break;
 3370		}
 3371		if (sdata->vif.type != NL80211_IFTYPE_STATION)
 3372			continue;
 3373		found = sdata;
 3374		count++;
 3375	}
 3376
 3377	if (count == 1 && ieee80211_powersave_allowed(found)) {
 3378		u8 dtimper = found->deflink.u.mgd.dtim_period;
 3379
 3380		timeout = local->dynamic_ps_forced_timeout;
 3381		if (timeout < 0)
 3382			timeout = 100;
 3383		local->hw.conf.dynamic_ps_timeout = timeout;
 3384
 3385		/* If the TIM IE is invalid, pretend the value is 1 */
 3386		if (!dtimper)
 3387			dtimper = 1;
 3388
 3389		local->hw.conf.ps_dtim_period = dtimper;
 3390		local->ps_sdata = found;
 3391	} else {
 3392		local->ps_sdata = NULL;
 3393	}
 3394
 3395	ieee80211_change_ps(local);
 3396}
 3397
 3398void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
 3399{
 3400	bool ps_allowed = ieee80211_powersave_allowed(sdata);
 3401
 3402	if (sdata->vif.cfg.ps != ps_allowed) {
 3403		sdata->vif.cfg.ps = ps_allowed;
 3404		ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_PS);
 3405	}
 3406}
 3407
 3408void ieee80211_dynamic_ps_disable_work(struct wiphy *wiphy,
 3409				       struct wiphy_work *work)
 3410{
 3411	struct ieee80211_local *local =
 3412		container_of(work, struct ieee80211_local,
 3413			     dynamic_ps_disable_work);
 3414
 3415	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
 3416		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
 3417		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
 3418	}
 3419
 3420	ieee80211_wake_queues_by_reason(&local->hw,
 3421					IEEE80211_MAX_QUEUE_MAP,
 3422					IEEE80211_QUEUE_STOP_REASON_PS,
 3423					false);
 3424}
 3425
 3426void ieee80211_dynamic_ps_enable_work(struct wiphy *wiphy,
 3427				      struct wiphy_work *work)
 3428{
 3429	struct ieee80211_local *local =
 3430		container_of(work, struct ieee80211_local,
 3431			     dynamic_ps_enable_work);
 3432	struct ieee80211_sub_if_data *sdata = local->ps_sdata;
 3433	struct ieee80211_if_managed *ifmgd;
 3434	unsigned long flags;
 3435	int q;
 3436
 3437	/* can only happen when PS was just disabled anyway */
 3438	if (!sdata)
 3439		return;
 3440
 3441	ifmgd = &sdata->u.mgd;
 3442
 3443	if (local->hw.conf.flags & IEEE80211_CONF_PS)
 3444		return;
 3445
 3446	if (local->hw.conf.dynamic_ps_timeout > 0) {
 3447		/* don't enter PS if TX frames are pending */
 3448		if (drv_tx_frames_pending(local)) {
 3449			mod_timer(&local->dynamic_ps_timer, jiffies +
 3450				  msecs_to_jiffies(
 3451				  local->hw.conf.dynamic_ps_timeout));
 3452			return;
 3453		}
 3454
 3455		/*
 3456		 * transmission can be stopped by others which leads to
 3457		 * dynamic_ps_timer expiry. Postpone the ps timer if it
 3458		 * is not the actual idle state.
 3459		 */
 3460		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
 3461		for (q = 0; q < local->hw.queues; q++) {
 3462			if (local->queue_stop_reasons[q]) {
 3463				spin_unlock_irqrestore(&local->queue_stop_reason_lock,
 3464						       flags);
 3465				mod_timer(&local->dynamic_ps_timer, jiffies +
 3466					  msecs_to_jiffies(
 3467					  local->hw.conf.dynamic_ps_timeout));
 3468				return;
 3469			}
 3470		}
 3471		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
 3472	}
 3473
 3474	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
 3475	    !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
 3476		if (drv_tx_frames_pending(local)) {
 3477			mod_timer(&local->dynamic_ps_timer, jiffies +
 3478				  msecs_to_jiffies(
 3479				  local->hw.conf.dynamic_ps_timeout));
 3480		} else {
 3481			ieee80211_send_nullfunc(local, sdata, true);
 3482			/* Flush to get the tx status of nullfunc frame */
 3483			ieee80211_flush_queues(local, sdata, false);
 3484		}
 3485	}
 3486
 3487	if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
 3488	      ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
 3489	    (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
 3490		ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
 3491		local->hw.conf.flags |= IEEE80211_CONF_PS;
 3492		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
 3493	}
 3494}
 3495
 3496void ieee80211_dynamic_ps_timer(struct timer_list *t)
 3497{
 3498	struct ieee80211_local *local = timer_container_of(local, t,
 3499							   dynamic_ps_timer);
 3500
 3501	wiphy_work_queue(local->hw.wiphy, &local->dynamic_ps_enable_work);
 3502}
 3503
 3504void ieee80211_dfs_cac_timer_work(struct wiphy *wiphy, struct wiphy_work *work)
 3505{
 3506	struct ieee80211_link_data *link =
 3507		container_of(work, struct ieee80211_link_data,
 3508			     dfs_cac_timer_work.work);
 3509	struct cfg80211_chan_def chandef = link->conf->chanreq.oper;
 3510	struct ieee80211_sub_if_data *sdata = link->sdata;
 3511
 3512	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 3513
 3514	if (sdata->wdev.links[link->link_id].cac_started) {
 3515		ieee80211_link_release_channel(link);
 3516		cfg80211_cac_event(sdata->dev, &chandef,
 3517				   NL80211_RADAR_CAC_FINISHED,
 3518				   GFP_KERNEL, link->link_id);
 3519	}
 3520}
 3521
 3522static bool
 3523__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
 3524{
 3525	struct ieee80211_local *local = sdata->local;
 3526	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 3527	bool ret = false;
 3528	int ac;
 3529
 3530	if (local->hw.queues < IEEE80211_NUM_ACS)
 3531		return false;
 3532
 3533	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 3534		struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
 3535		int non_acm_ac;
 3536		unsigned long now = jiffies;
 3537
 3538		if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
 3539		    tx_tspec->admitted_time &&
 3540		    time_after(now, tx_tspec->time_slice_start + HZ)) {
 3541			tx_tspec->consumed_tx_time = 0;
 3542			tx_tspec->time_slice_start = now;
 3543
 3544			if (tx_tspec->downgraded)
 3545				tx_tspec->action =
 3546					TX_TSPEC_ACTION_STOP_DOWNGRADE;
 3547		}
 3548
 3549		switch (tx_tspec->action) {
 3550		case TX_TSPEC_ACTION_STOP_DOWNGRADE:
 3551			/* take the original parameters */
 3552			if (drv_conf_tx(local, &sdata->deflink, ac,
 3553					&sdata->deflink.tx_conf[ac]))
 3554				link_err(&sdata->deflink,
 3555					 "failed to set TX queue parameters for queue %d\n",
 3556					 ac);
 3557			tx_tspec->action = TX_TSPEC_ACTION_NONE;
 3558			tx_tspec->downgraded = false;
 3559			ret = true;
 3560			break;
 3561		case TX_TSPEC_ACTION_DOWNGRADE:
 3562			if (time_after(now, tx_tspec->time_slice_start + HZ)) {
 3563				tx_tspec->action = TX_TSPEC_ACTION_NONE;
 3564				ret = true;
 3565				break;
 3566			}
 3567			/* downgrade next lower non-ACM AC */
 3568			for (non_acm_ac = ac + 1;
 3569			     non_acm_ac < IEEE80211_NUM_ACS;
 3570			     non_acm_ac++)
 3571				if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
 3572					break;
 3573			/* Usually the loop will result in using BK even if it
 3574			 * requires admission control, but such a configuration
 3575			 * makes no sense and we have to transmit somehow - the
 3576			 * AC selection does the same thing.
 3577			 * If we started out trying to downgrade from BK, then
 3578			 * the extra condition here might be needed.
 3579			 */
 3580			if (non_acm_ac >= IEEE80211_NUM_ACS)
 3581				non_acm_ac = IEEE80211_AC_BK;
 3582			if (drv_conf_tx(local, &sdata->deflink, ac,
 3583					&sdata->deflink.tx_conf[non_acm_ac]))
 3584				link_err(&sdata->deflink,
 3585					 "failed to set TX queue parameters for queue %d\n",
 3586					 ac);
 3587			tx_tspec->action = TX_TSPEC_ACTION_NONE;
 3588			ret = true;
 3589			wiphy_delayed_work_queue(local->hw.wiphy,
 3590						 &ifmgd->tx_tspec_wk,
 3591						 tx_tspec->time_slice_start +
 3592						 HZ - now + 1);
 3593			break;
 3594		case TX_TSPEC_ACTION_NONE:
 3595			/* nothing now */
 3596			break;
 3597		}
 3598	}
 3599
 3600	return ret;
 3601}
 3602
 3603void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
 3604{
 3605	if (__ieee80211_sta_handle_tspec_ac_params(sdata))
 3606		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
 3607						  BSS_CHANGED_QOS);
 3608}
 3609
 3610static void ieee80211_sta_handle_tspec_ac_params_wk(struct wiphy *wiphy,
 3611						    struct wiphy_work *work)
 3612{
 3613	struct ieee80211_sub_if_data *sdata;
 3614
 3615	sdata = container_of(work, struct ieee80211_sub_if_data,
 3616			     u.mgd.tx_tspec_wk.work);
 3617	ieee80211_sta_handle_tspec_ac_params(sdata);
 3618}
 3619
 3620void ieee80211_mgd_set_link_qos_params(struct ieee80211_link_data *link)
 3621{
 3622	struct ieee80211_sub_if_data *sdata = link->sdata;
 3623	struct ieee80211_local *local = sdata->local;
 3624	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 3625	struct ieee80211_tx_queue_params *params = link->tx_conf;
 3626	u8 ac;
 3627
 3628	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 3629		mlme_dbg(sdata,
 3630			 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
 3631			 ac, params[ac].acm,
 3632			 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
 3633			 params[ac].txop, params[ac].uapsd,
 3634			 ifmgd->tx_tspec[ac].downgraded);
 3635		if (!ifmgd->tx_tspec[ac].downgraded &&
 3636		    drv_conf_tx(local, link, ac, &params[ac]))
 3637			link_err(link,
 3638				 "failed to set TX queue parameters for AC %d\n",
 3639				 ac);
 3640	}
 3641}
 3642
 3643/* MLME */
 3644static bool
 3645_ieee80211_sta_wmm_params(struct ieee80211_local *local,
 3646			  struct ieee80211_link_data *link,
 3647			  const u8 *wmm_param, size_t wmm_param_len,
 3648			  const struct ieee80211_mu_edca_param_set *mu_edca)
 3649{
 3650	struct ieee80211_sub_if_data *sdata = link->sdata;
 3651	struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
 3652	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 3653	size_t left;
 3654	int count, mu_edca_count, ac;
 3655	const u8 *pos;
 3656	u8 uapsd_queues = 0;
 3657
 3658	if (!local->ops->conf_tx)
 3659		return false;
 3660
 3661	if (local->hw.queues < IEEE80211_NUM_ACS)
 3662		return false;
 3663
 3664	if (!wmm_param)
 3665		return false;
 3666
 3667	if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
 3668		return false;
 3669
 3670	if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
 3671		uapsd_queues = ifmgd->uapsd_queues;
 3672
 3673	count = wmm_param[6] & 0x0f;
 3674	/* -1 is the initial value of ifmgd->mu_edca_last_param_set.
 3675	 * if mu_edca was preset before and now it disappeared tell
 3676	 * the driver about it.
 3677	 */
 3678	mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
 3679	if (count == link->u.mgd.wmm_last_param_set &&
 3680	    mu_edca_count == link->u.mgd.mu_edca_last_param_set)
 3681		return false;
 3682	link->u.mgd.wmm_last_param_set = count;
 3683	link->u.mgd.mu_edca_last_param_set = mu_edca_count;
 3684
 3685	pos = wmm_param + 8;
 3686	left = wmm_param_len - 8;
 3687
 3688	memset(&params, 0, sizeof(params));
 3689
 3690	sdata->wmm_acm = 0;
 3691	for (; left >= 4; left -= 4, pos += 4) {
 3692		int aci = (pos[0] >> 5) & 0x03;
 3693		int acm = (pos[0] >> 4) & 0x01;
 3694		bool uapsd = false;
 3695
 3696		switch (aci) {
 3697		case 1: /* AC_BK */
 3698			ac = IEEE80211_AC_BK;
 3699			if (acm)
 3700				sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
 3701			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
 3702				uapsd = true;
 3703			params[ac].mu_edca = !!mu_edca;
 3704			if (mu_edca)
 3705				params[ac].mu_edca_param_rec = mu_edca->ac_bk;
 3706			break;
 3707		case 2: /* AC_VI */
 3708			ac = IEEE80211_AC_VI;
 3709			if (acm)
 3710				sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
 3711			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
 3712				uapsd = true;
 3713			params[ac].mu_edca = !!mu_edca;
 3714			if (mu_edca)
 3715				params[ac].mu_edca_param_rec = mu_edca->ac_vi;
 3716			break;
 3717		case 3: /* AC_VO */
 3718			ac = IEEE80211_AC_VO;
 3719			if (acm)
 3720				sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
 3721			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
 3722				uapsd = true;
 3723			params[ac].mu_edca = !!mu_edca;
 3724			if (mu_edca)
 3725				params[ac].mu_edca_param_rec = mu_edca->ac_vo;
 3726			break;
 3727		case 0: /* AC_BE */
 3728		default:
 3729			ac = IEEE80211_AC_BE;
 3730			if (acm)
 3731				sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
 3732			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
 3733				uapsd = true;
 3734			params[ac].mu_edca = !!mu_edca;
 3735			if (mu_edca)
 3736				params[ac].mu_edca_param_rec = mu_edca->ac_be;
 3737			break;
 3738		}
 3739
 3740		params[ac].aifs = pos[0] & 0x0f;
 3741
 3742		if (params[ac].aifs < 2) {
 3743			link_info(link,
 3744				  "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
 3745				  params[ac].aifs, aci);
 3746			params[ac].aifs = 2;
 3747		}
 3748		params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
 3749		params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
 3750		params[ac].txop = get_unaligned_le16(pos + 2);
 3751		params[ac].acm = acm;
 3752		params[ac].uapsd = uapsd;
 3753
 3754		if (params[ac].cw_min == 0 ||
 3755		    params[ac].cw_min > params[ac].cw_max) {
 3756			link_info(link,
 3757				  "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
 3758				  params[ac].cw_min, params[ac].cw_max, aci);
 3759			return false;
 3760		}
 3761		ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
 3762	}
 3763
 3764	/* WMM specification requires all 4 ACIs. */
 3765	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 3766		if (params[ac].cw_min == 0) {
 3767			link_info(link,
 3768				  "AP has invalid WMM params (missing AC %d), using defaults\n",
 3769				  ac);
 3770			return false;
 3771		}
 3772	}
 3773
 3774	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 3775		link->tx_conf[ac] = params[ac];
 3776
 3777	return true;
 3778}
 3779
 3780static bool
 3781ieee80211_sta_wmm_params(struct ieee80211_local *local,
 3782			 struct ieee80211_link_data *link,
 3783			 const u8 *wmm_param, size_t wmm_param_len,
 3784			 const struct ieee80211_mu_edca_param_set *mu_edca)
 3785{
 3786	if (!_ieee80211_sta_wmm_params(local, link, wmm_param, wmm_param_len,
 3787				       mu_edca))
 3788		return false;
 3789
 3790	ieee80211_mgd_set_link_qos_params(link);
 3791
 3792	/* enable WMM or activate new settings */
 3793	link->conf->qos = true;
 3794	return true;
 3795}
 3796
 3797static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
 3798{
 3799	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 3800
 3801	sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
 3802	ieee80211_run_deferred_scan(sdata->local);
 3803}
 3804
 3805static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
 3806{
 3807	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 3808
 3809	__ieee80211_stop_poll(sdata);
 3810}
 3811
 3812static u64 ieee80211_handle_bss_capability(struct ieee80211_link_data *link,
 3813					   u16 capab, bool erp_valid, u8 erp)
 3814{
 3815	struct ieee80211_bss_conf *bss_conf = link->conf;
 3816	struct ieee80211_supported_band *sband;
 3817	u64 changed = 0;
 3818	bool use_protection;
 3819	bool use_short_preamble;
 3820	bool use_short_slot;
 3821
 3822	sband = ieee80211_get_link_sband(link);
 3823	if (!sband)
 3824		return changed;
 3825
 3826	if (erp_valid) {
 3827		use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
 3828		use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
 3829	} else {
 3830		use_protection = false;
 3831		use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
 3832	}
 3833
 3834	use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
 3835	if (sband->band == NL80211_BAND_5GHZ ||
 3836	    sband->band == NL80211_BAND_6GHZ)
 3837		use_short_slot = true;
 3838
 3839	if (use_protection != bss_conf->use_cts_prot) {
 3840		bss_conf->use_cts_prot = use_protection;
 3841		changed |= BSS_CHANGED_ERP_CTS_PROT;
 3842	}
 3843
 3844	if (use_short_preamble != bss_conf->use_short_preamble) {
 3845		bss_conf->use_short_preamble = use_short_preamble;
 3846		changed |= BSS_CHANGED_ERP_PREAMBLE;
 3847	}
 3848
 3849	if (use_short_slot != bss_conf->use_short_slot) {
 3850		bss_conf->use_short_slot = use_short_slot;
 3851		changed |= BSS_CHANGED_ERP_SLOT;
 3852	}
 3853
 3854	return changed;
 3855}
 3856
 3857static u64 ieee80211_link_set_associated(struct ieee80211_link_data *link,
 3858					 struct cfg80211_bss *cbss)
 3859{
 3860	struct ieee80211_sub_if_data *sdata = link->sdata;
 3861	struct ieee80211_bss_conf *bss_conf = link->conf;
 3862	struct ieee80211_bss *bss = (void *)cbss->priv;
 3863	u64 changed = BSS_CHANGED_QOS;
 3864
 3865	/* not really used in MLO */
 3866	sdata->u.mgd.beacon_timeout =
 3867		usecs_to_jiffies(ieee80211_tu_to_usec(beacon_loss_count *
 3868						      bss_conf->beacon_int));
 3869
 3870	changed |= ieee80211_handle_bss_capability(link,
 3871						   bss_conf->assoc_capability,
 3872						   bss->has_erp_value,
 3873						   bss->erp_value);
 3874
 3875	ieee80211_check_rate_mask(link);
 3876
 3877	link->conf->bss = cbss;
 3878	memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
 3879
 3880	if (sdata->vif.p2p ||
 3881	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
 3882		const struct cfg80211_bss_ies *ies;
 3883
 3884		rcu_read_lock();
 3885		ies = rcu_dereference(cbss->ies);
 3886		if (ies) {
 3887			int ret;
 3888
 3889			ret = cfg80211_get_p2p_attr(
 3890					ies->data, ies->len,
 3891					IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
 3892					(u8 *) &bss_conf->p2p_noa_attr,
 3893					sizeof(bss_conf->p2p_noa_attr));
 3894			if (ret >= 2) {
 3895				link->u.mgd.p2p_noa_index =
 3896					bss_conf->p2p_noa_attr.index;
 3897				changed |= BSS_CHANGED_P2P_PS;
 3898			}
 3899		}
 3900		rcu_read_unlock();
 3901	}
 3902
 3903	if (link->u.mgd.have_beacon) {
 3904		bss_conf->beacon_rate = bss->beacon_rate;
 3905		changed |= BSS_CHANGED_BEACON_INFO;
 3906	} else {
 3907		bss_conf->beacon_rate = NULL;
 3908	}
 3909
 3910	/* Tell the driver to monitor connection quality (if supported) */
 3911	if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
 3912	    bss_conf->cqm_rssi_thold)
 3913		changed |= BSS_CHANGED_CQM;
 3914
 3915	return changed;
 3916}
 3917
 3918static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
 3919				     struct ieee80211_mgd_assoc_data *assoc_data,
 3920				     u64 changed[IEEE80211_MLD_MAX_NUM_LINKS])
 3921{
 3922	struct ieee80211_local *local = sdata->local;
 3923	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
 3924	u64 vif_changed = BSS_CHANGED_ASSOC;
 3925	unsigned int link_id;
 3926
 3927	lockdep_assert_wiphy(local->hw.wiphy);
 3928
 3929	sdata->u.mgd.associated = true;
 3930
 3931	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 3932		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
 3933		struct ieee80211_link_data *link;
 3934
 3935		if (!cbss ||
 3936		    assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS)
 3937			continue;
 3938
 3939		if (ieee80211_vif_is_mld(&sdata->vif) &&
 3940		    !(ieee80211_vif_usable_links(&sdata->vif) & BIT(link_id)))
 3941			continue;
 3942
 3943		link = sdata_dereference(sdata->link[link_id], sdata);
 3944		if (WARN_ON(!link))
 3945			return;
 3946
 3947		changed[link_id] |= ieee80211_link_set_associated(link, cbss);
 3948	}
 3949
 3950	/* just to be sure */
 3951	ieee80211_stop_poll(sdata);
 3952
 3953	ieee80211_led_assoc(local, 1);
 3954
 3955	vif_cfg->assoc = 1;
 3956
 3957	/* Enable ARP filtering */
 3958	if (vif_cfg->arp_addr_cnt)
 3959		vif_changed |= BSS_CHANGED_ARP_FILTER;
 3960
 3961	if (ieee80211_vif_is_mld(&sdata->vif)) {
 3962		for (link_id = 0;
 3963		     link_id < IEEE80211_MLD_MAX_NUM_LINKS;
 3964		     link_id++) {
 3965			struct ieee80211_link_data *link;
 3966			struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
 3967
 3968			if (!cbss ||
 3969			    !(BIT(link_id) &
 3970			      ieee80211_vif_usable_links(&sdata->vif)) ||
 3971			    assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS)
 3972				continue;
 3973
 3974			link = sdata_dereference(sdata->link[link_id], sdata);
 3975			if (WARN_ON(!link))
 3976				return;
 3977
 3978			ieee80211_link_info_change_notify(sdata, link,
 3979							  changed[link_id]);
 3980
 3981			ieee80211_recalc_smps(sdata, link);
 3982		}
 3983
 3984		ieee80211_vif_cfg_change_notify(sdata, vif_changed);
 3985	} else {
 3986		ieee80211_bss_info_change_notify(sdata,
 3987						 vif_changed | changed[0]);
 3988	}
 3989
 3990	ieee80211_recalc_ps(local);
 3991
 3992	/* leave this here to not change ordering in non-MLO cases */
 3993	if (!ieee80211_vif_is_mld(&sdata->vif))
 3994		ieee80211_recalc_smps(sdata, &sdata->deflink);
 3995	ieee80211_recalc_ps_vif(sdata);
 3996
 3997	netif_carrier_on(sdata->dev);
 3998}
 3999
 4000static void ieee80211_ml_reconf_reset(struct ieee80211_sub_if_data *sdata)
 4001{
 4002	struct ieee80211_mgd_assoc_data *add_links_data =
 4003		sdata->u.mgd.reconf.add_links_data;
 4004
 4005	if (!ieee80211_vif_is_mld(&sdata->vif) ||
 4006	    !(sdata->u.mgd.reconf.added_links |
 4007	      sdata->u.mgd.reconf.removed_links))
 4008		return;
 4009
 4010	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
 4011				  &sdata->u.mgd.reconf.wk);
 4012	sdata->u.mgd.reconf.added_links = 0;
 4013	sdata->u.mgd.reconf.removed_links = 0;
 4014	sdata->u.mgd.reconf.dialog_token = 0;
 4015
 4016	if (add_links_data) {
 4017		struct cfg80211_mlo_reconf_done_data done_data = {};
 4018		u8 link_id;
 4019
 4020		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
 4021		     link_id++)
 4022			done_data.links[link_id].bss =
 4023				add_links_data->link[link_id].bss;
 4024
 4025		cfg80211_mlo_reconf_add_done(sdata->dev, &done_data);
 4026
 4027		kfree(sdata->u.mgd.reconf.add_links_data);
 4028		sdata->u.mgd.reconf.add_links_data = NULL;
 4029	}
 4030}
 4031
 4032static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
 4033				   u16 stype, u16 reason, bool tx,
 4034				   u8 *frame_buf)
 4035{
 4036	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4037	struct ieee80211_local *local = sdata->local;
 4038	struct sta_info *ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
 4039	unsigned int link_id;
 4040	u64 changed = 0;
 4041	struct ieee80211_prep_tx_info info = {
 4042		.subtype = stype,
 4043		.was_assoc = true,
 4044		.link_id = ffs(sdata->vif.active_links) - 1,
 4045	};
 4046
 4047	lockdep_assert_wiphy(local->hw.wiphy);
 4048
 4049	if (frame_buf)
 4050		memset(frame_buf, 0, IEEE80211_DEAUTH_FRAME_LEN);
 4051
 4052	if (WARN_ON(!ap_sta))
 4053		return;
 4054
 4055	if (WARN_ON_ONCE(tx && !frame_buf))
 4056		return;
 4057
 4058	if (WARN_ON(!ifmgd->associated))
 4059		return;
 4060
 4061	ieee80211_stop_poll(sdata);
 4062
 4063	ifmgd->associated = false;
 4064
 4065	if (tx) {
 4066		bool tx_link_found = false;
 4067
 4068		for (link_id = 0;
 4069		     link_id < ARRAY_SIZE(sdata->link);
 4070		     link_id++) {
 4071			struct ieee80211_link_data *link;
 4072
 4073			if (!ieee80211_vif_link_active(&sdata->vif, link_id))
 4074				continue;
 4075
 4076			link = sdata_dereference(sdata->link[link_id], sdata);
 4077			if (WARN_ON_ONCE(!link))
 4078				continue;
 4079
 4080			if (link->u.mgd.csa.blocked_tx)
 4081				continue;
 4082
 4083			tx_link_found = true;
 4084			break;
 4085		}
 4086
 4087		tx = tx_link_found;
 4088	}
 4089
 4090	/* other links will be destroyed */
 4091	sdata->deflink.conf->bss = NULL;
 4092	sdata->deflink.conf->epcs_support = false;
 4093	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
 4094
 4095	netif_carrier_off(sdata->dev);
 4096
 4097	/*
 4098	 * if we want to get out of ps before disassoc (why?) we have
 4099	 * to do it before sending disassoc, as otherwise the null-packet
 4100	 * won't be valid.
 4101	 */
 4102	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
 4103		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
 4104		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
 4105	}
 4106	local->ps_sdata = NULL;
 4107
 4108	/* disable per-vif ps */
 4109	ieee80211_recalc_ps_vif(sdata);
 4110
 4111	/* make sure ongoing transmission finishes */
 4112	synchronize_net();
 4113
 4114	/*
 4115	 * drop any frame before deauth/disassoc, this can be data or
 4116	 * management frame. Since we are disconnecting, we should not
 4117	 * insist sending these frames which can take time and delay
 4118	 * the disconnection and possible the roaming.
 4119	 */
 4120	ieee80211_flush_queues(local, sdata, true);
 4121
 4122	if (tx) {
 4123		drv_mgd_prepare_tx(sdata->local, sdata, &info);
 4124
 4125		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
 4126					       sdata->vif.cfg.ap_addr, stype,
 4127					       reason, true, frame_buf);
 4128
 4129		/* flush out frame - make sure the deauth was actually sent */
 4130		ieee80211_flush_queues(local, sdata, false);
 4131
 4132		drv_mgd_complete_tx(sdata->local, sdata, &info);
 4133	} else if (frame_buf) {
 4134		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
 4135					       sdata->vif.cfg.ap_addr, stype,
 4136					       reason, false, frame_buf);
 4137	}
 4138
 4139	/* clear AP addr only after building the needed mgmt frames */
 4140	eth_zero_addr(sdata->deflink.u.mgd.bssid);
 4141	eth_zero_addr(sdata->vif.cfg.ap_addr);
 4142
 4143	sdata->vif.cfg.ssid_len = 0;
 4144
 4145	/* Remove TDLS peers */
 4146	__sta_info_flush(sdata, false, -1, ap_sta);
 4147
 4148	if (sdata->vif.driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC) {
 4149		/* Only move the AP state */
 4150		sta_info_move_state(ap_sta, IEEE80211_STA_NONE);
 4151	} else {
 4152		/* Remove AP peer */
 4153		sta_info_flush(sdata, -1);
 4154	}
 4155
 4156	/* finally reset all BSS / config parameters */
 4157	if (!ieee80211_vif_is_mld(&sdata->vif))
 4158		changed |= ieee80211_reset_erp_info(sdata);
 4159
 4160	ieee80211_led_assoc(local, 0);
 4161	changed |= BSS_CHANGED_ASSOC;
 4162	sdata->vif.cfg.assoc = false;
 4163
 4164	sdata->deflink.u.mgd.p2p_noa_index = -1;
 4165	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
 4166	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
 4167
 4168	/* on the next assoc, re-program HT/VHT parameters */
 4169	memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
 4170	memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
 4171	memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
 4172	memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
 4173
 4174	/*
 4175	 * reset MU-MIMO ownership and group data in default link,
 4176	 * if used, other links are destroyed
 4177	 */
 4178	memset(sdata->vif.bss_conf.mu_group.membership, 0,
 4179	       sizeof(sdata->vif.bss_conf.mu_group.membership));
 4180	memset(sdata->vif.bss_conf.mu_group.position, 0,
 4181	       sizeof(sdata->vif.bss_conf.mu_group.position));
 4182	if (!ieee80211_vif_is_mld(&sdata->vif))
 4183		changed |= BSS_CHANGED_MU_GROUPS;
 4184	sdata->vif.bss_conf.mu_mimo_owner = false;
 4185
 4186	sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
 4187
 4188	timer_delete_sync(&local->dynamic_ps_timer);
 4189	wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
 4190
 4191	/* Disable ARP filtering */
 4192	if (sdata->vif.cfg.arp_addr_cnt)
 4193		changed |= BSS_CHANGED_ARP_FILTER;
 4194
 4195	sdata->vif.bss_conf.qos = false;
 4196	if (!ieee80211_vif_is_mld(&sdata->vif)) {
 4197		changed |= BSS_CHANGED_QOS;
 4198		/* The BSSID (not really interesting) and HT changed */
 4199		changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
 4200		ieee80211_bss_info_change_notify(sdata, changed);
 4201	} else {
 4202		ieee80211_vif_cfg_change_notify(sdata, changed);
 4203	}
 4204
 4205	if (sdata->vif.driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC) {
 4206		/*
 4207		 * After notifying the driver about the disassoc,
 4208		 * remove the ap sta.
 4209		 */
 4210		sta_info_flush(sdata, -1);
 4211	}
 4212
 4213	/* disassociated - set to defaults now */
 4214	ieee80211_set_wmm_default(&sdata->deflink, false, false);
 4215
 4216	timer_delete_sync(&sdata->u.mgd.conn_mon_timer);
 4217	timer_delete_sync(&sdata->u.mgd.bcn_mon_timer);
 4218	timer_delete_sync(&sdata->u.mgd.timer);
 4219
 4220	sdata->vif.bss_conf.dtim_period = 0;
 4221	sdata->vif.bss_conf.beacon_rate = NULL;
 4222
 4223	sdata->deflink.u.mgd.have_beacon = false;
 4224	sdata->deflink.u.mgd.tracking_signal_avg = false;
 4225	sdata->deflink.u.mgd.disable_wmm_tracking = false;
 4226
 4227	ifmgd->flags = 0;
 4228
 4229	for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
 4230		struct ieee80211_link_data *link;
 4231
 4232		link = sdata_dereference(sdata->link[link_id], sdata);
 4233		if (!link)
 4234			continue;
 4235		ieee80211_link_release_channel(link);
 4236	}
 4237
 4238	sdata->vif.bss_conf.csa_active = false;
 4239	sdata->deflink.u.mgd.csa.blocked_tx = false;
 4240	sdata->deflink.u.mgd.csa.waiting_bcn = false;
 4241	sdata->deflink.u.mgd.csa.ignored_same_chan = false;
 4242	ieee80211_vif_unblock_queues_csa(sdata);
 4243
 4244	/* existing TX TSPEC sessions no longer exist */
 4245	memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
 4246	wiphy_delayed_work_cancel(local->hw.wiphy, &ifmgd->tx_tspec_wk);
 4247
 4248	sdata->vif.bss_conf.power_type = IEEE80211_REG_UNSET_AP;
 4249	sdata->vif.bss_conf.pwr_reduction = 0;
 4250	ieee80211_clear_tpe(&sdata->vif.bss_conf.tpe);
 4251
 4252	sdata->vif.cfg.eml_cap = 0;
 4253	sdata->vif.cfg.eml_med_sync_delay = 0;
 4254	sdata->vif.cfg.mld_capa_op = 0;
 4255
 4256	memset(&sdata->u.mgd.ttlm_info, 0,
 4257	       sizeof(sdata->u.mgd.ttlm_info));
 4258	wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy, &ifmgd->ttlm_work);
 4259
 4260	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
 4261	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
 4262				  &ifmgd->neg_ttlm_timeout_work);
 4263
 4264	sdata->u.mgd.removed_links = 0;
 4265	wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
 4266				  &sdata->u.mgd.ml_reconf_work);
 4267
 4268	wiphy_work_cancel(sdata->local->hw.wiphy,
 4269			  &ifmgd->teardown_ttlm_work);
 4270
 4271	/* if disconnection happens in the middle of the ML reconfiguration
 4272	 * flow, cfg80211 must called to release the BSS references obtained
 4273	 * when the flow started.
 4274	 */
 4275	ieee80211_ml_reconf_reset(sdata);
 4276
 4277	ieee80211_vif_set_links(sdata, 0, 0);
 4278
 4279	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
 4280
 4281	ifmgd->epcs.enabled = false;
 4282	ifmgd->epcs.dialog_token = 0;
 4283
 4284	memset(ifmgd->userspace_selectors, 0,
 4285	       sizeof(ifmgd->userspace_selectors));
 4286}
 4287
 4288static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
 4289{
 4290	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4291	struct ieee80211_local *local = sdata->local;
 4292
 4293	lockdep_assert_wiphy(local->hw.wiphy);
 4294
 4295	if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
 4296		return;
 4297
 4298	__ieee80211_stop_poll(sdata);
 4299
 4300	ieee80211_recalc_ps(local);
 4301
 4302	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
 4303		return;
 4304
 4305	/*
 4306	 * We've received a probe response, but are not sure whether
 4307	 * we have or will be receiving any beacons or data, so let's
 4308	 * schedule the timers again, just in case.
 4309	 */
 4310	ieee80211_sta_reset_beacon_monitor(sdata);
 4311
 4312	mod_timer(&ifmgd->conn_mon_timer,
 4313		  round_jiffies_up(jiffies +
 4314				   IEEE80211_CONNECTION_IDLE_TIME));
 4315}
 4316
 4317static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
 4318					   struct ieee80211_hdr *hdr,
 4319					   u16 tx_time)
 4320{
 4321	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4322	u16 tid;
 4323	int ac;
 4324	struct ieee80211_sta_tx_tspec *tx_tspec;
 4325	unsigned long now = jiffies;
 4326
 4327	if (!ieee80211_is_data_qos(hdr->frame_control))
 4328		return;
 4329
 4330	tid = ieee80211_get_tid(hdr);
 4331	ac = ieee80211_ac_from_tid(tid);
 4332	tx_tspec = &ifmgd->tx_tspec[ac];
 4333
 4334	if (likely(!tx_tspec->admitted_time))
 4335		return;
 4336
 4337	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
 4338		tx_tspec->consumed_tx_time = 0;
 4339		tx_tspec->time_slice_start = now;
 4340
 4341		if (tx_tspec->downgraded) {
 4342			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
 4343			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
 4344						 &ifmgd->tx_tspec_wk, 0);
 4345		}
 4346	}
 4347
 4348	if (tx_tspec->downgraded)
 4349		return;
 4350
 4351	tx_tspec->consumed_tx_time += tx_time;
 4352
 4353	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
 4354		tx_tspec->downgraded = true;
 4355		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
 4356		wiphy_delayed_work_queue(sdata->local->hw.wiphy,
 4357					 &ifmgd->tx_tspec_wk, 0);
 4358	}
 4359}
 4360
 4361void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
 4362			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
 4363{
 4364	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
 4365
 4366	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
 4367	    !sdata->u.mgd.probe_send_count)
 4368		return;
 4369
 4370	if (ack)
 4371		sdata->u.mgd.probe_send_count = 0;
 4372	else
 4373		sdata->u.mgd.nullfunc_failed = true;
 4374	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
 4375}
 4376
 4377static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
 4378					  const u8 *src, const u8 *dst,
 4379					  const u8 *ssid, size_t ssid_len,
 4380					  struct ieee80211_channel *channel)
 4381{
 4382	struct sk_buff *skb;
 4383
 4384	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
 4385					ssid, ssid_len, NULL, 0,
 4386					IEEE80211_PROBE_FLAG_DIRECTED);
 4387	if (skb)
 4388		ieee80211_tx_skb(sdata, skb);
 4389}
 4390
 4391static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
 4392{
 4393	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4394	u8 *dst = sdata->vif.cfg.ap_addr;
 4395	u8 unicast_limit = max(1, max_probe_tries - 3);
 4396	struct sta_info *sta;
 4397
 4398	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4399
 4400	/*
 4401	 * Try sending broadcast probe requests for the last three
 4402	 * probe requests after the first ones failed since some
 4403	 * buggy APs only support broadcast probe requests.
 4404	 */
 4405	if (ifmgd->probe_send_count >= unicast_limit)
 4406		dst = NULL;
 4407
 4408	/*
 4409	 * When the hardware reports an accurate Tx ACK status, it's
 4410	 * better to send a nullfunc frame instead of a probe request,
 4411	 * as it will kick us off the AP quickly if we aren't associated
 4412	 * anymore. The timeout will be reset if the frame is ACKed by
 4413	 * the AP.
 4414	 */
 4415	ifmgd->probe_send_count++;
 4416
 4417	if (dst) {
 4418		sta = sta_info_get(sdata, dst);
 4419		if (!WARN_ON(!sta))
 4420			ieee80211_check_fast_rx(sta);
 4421	}
 4422
 4423	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
 4424		ifmgd->nullfunc_failed = false;
 4425		ieee80211_send_nullfunc(sdata->local, sdata, false);
 4426	} else {
 4427		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
 4428					      sdata->vif.cfg.ssid,
 4429					      sdata->vif.cfg.ssid_len,
 4430					      sdata->deflink.conf->bss->channel);
 4431	}
 4432
 4433	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
 4434	run_again(sdata, ifmgd->probe_timeout);
 4435}
 4436
 4437static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
 4438				   bool beacon)
 4439{
 4440	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4441	bool already = false;
 4442
 4443	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4444
 4445	if (!ieee80211_sdata_running(sdata))
 4446		return;
 4447
 4448	if (!ifmgd->associated)
 4449		return;
 4450
 4451	if (sdata->local->tmp_channel || sdata->local->scanning)
 4452		return;
 4453
 4454	if (sdata->local->suspending) {
 4455		/* reschedule after resume */
 4456		ieee80211_reset_ap_probe(sdata);
 4457		return;
 4458	}
 4459
 4460	if (beacon) {
 4461		mlme_dbg_ratelimited(sdata,
 4462				     "detected beacon loss from AP (missed %d beacons) - probing\n",
 4463				     beacon_loss_count);
 4464
 4465		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
 4466	}
 4467
 4468	/*
 4469	 * The driver/our work has already reported this event or the
 4470	 * connection monitoring has kicked in and we have already sent
 4471	 * a probe request. Or maybe the AP died and the driver keeps
 4472	 * reporting until we disassociate...
 4473	 *
 4474	 * In either case we have to ignore the current call to this
 4475	 * function (except for setting the correct probe reason bit)
 4476	 * because otherwise we would reset the timer every time and
 4477	 * never check whether we received a probe response!
 4478	 */
 4479	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
 4480		already = true;
 4481
 4482	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
 4483
 4484	if (already)
 4485		return;
 4486
 4487	ieee80211_recalc_ps(sdata->local);
 4488
 4489	ifmgd->probe_send_count = 0;
 4490	ieee80211_mgd_probe_ap_send(sdata);
 4491}
 4492
 4493struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
 4494					  struct ieee80211_vif *vif)
 4495{
 4496	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 4497	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4498	struct cfg80211_bss *cbss;
 4499	struct sk_buff *skb;
 4500	const struct element *ssid;
 4501	int ssid_len;
 4502
 4503	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4504
 4505	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
 4506		    ieee80211_vif_is_mld(&sdata->vif)))
 4507		return NULL;
 4508
 4509	if (ifmgd->associated)
 4510		cbss = sdata->deflink.conf->bss;
 4511	else if (ifmgd->auth_data)
 4512		cbss = ifmgd->auth_data->bss;
 4513	else if (ifmgd->assoc_data && ifmgd->assoc_data->link[0].bss)
 4514		cbss = ifmgd->assoc_data->link[0].bss;
 4515	else
 4516		return NULL;
 4517
 4518	rcu_read_lock();
 4519	ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
 4520	if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
 4521		      "invalid SSID element (len=%d)",
 4522		      ssid ? ssid->datalen : -1))
 4523		ssid_len = 0;
 4524	else
 4525		ssid_len = ssid->datalen;
 4526
 4527	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
 4528					(u32) -1, cbss->channel,
 4529					ssid->data, ssid_len,
 4530					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
 4531	rcu_read_unlock();
 4532
 4533	return skb;
 4534}
 4535EXPORT_SYMBOL(ieee80211_ap_probereq_get);
 4536
 4537static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
 4538					const u8 *buf, size_t len, bool tx,
 4539					u16 reason, bool reconnect)
 4540{
 4541	struct ieee80211_event event = {
 4542		.type = MLME_EVENT,
 4543		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
 4544		.u.mlme.reason = reason,
 4545	};
 4546
 4547	if (tx)
 4548		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
 4549	else
 4550		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
 4551
 4552	drv_event_callback(sdata->local, sdata, &event);
 4553}
 4554
 4555static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
 4556{
 4557	struct ieee80211_local *local = sdata->local;
 4558	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4559	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
 4560
 4561	lockdep_assert_wiphy(local->hw.wiphy);
 4562
 4563	if (!ifmgd->associated)
 4564		return;
 4565
 4566	if (!ifmgd->driver_disconnect) {
 4567		unsigned int link_id;
 4568
 4569		/*
 4570		 * AP is probably out of range (or not reachable for another
 4571		 * reason) so remove the bss structs for that AP. In the case
 4572		 * of multi-link, it's not clear that all of them really are
 4573		 * out of range, but if they weren't the driver likely would
 4574		 * have switched to just have a single link active?
 4575		 */
 4576		for (link_id = 0;
 4577		     link_id < ARRAY_SIZE(sdata->link);
 4578		     link_id++) {
 4579			struct ieee80211_link_data *link;
 4580
 4581			link = sdata_dereference(sdata->link[link_id], sdata);
 4582			if (!link || !link->conf->bss)
 4583				continue;
 4584			cfg80211_unlink_bss(local->hw.wiphy, link->conf->bss);
 4585			link->conf->bss = NULL;
 4586		}
 4587	}
 4588
 4589	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
 4590			       ifmgd->driver_disconnect ?
 4591					WLAN_REASON_DEAUTH_LEAVING :
 4592					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
 4593			       true, frame_buf);
 4594	/* the other links will be destroyed */
 4595	sdata->vif.bss_conf.csa_active = false;
 4596	sdata->deflink.u.mgd.csa.waiting_bcn = false;
 4597	sdata->deflink.u.mgd.csa.blocked_tx = false;
 4598	ieee80211_vif_unblock_queues_csa(sdata);
 4599
 4600	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
 4601				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
 4602				    ifmgd->reconnect);
 4603	ifmgd->reconnect = false;
 4604}
 4605
 4606static void ieee80211_beacon_connection_loss_work(struct wiphy *wiphy,
 4607						  struct wiphy_work *work)
 4608{
 4609	struct ieee80211_sub_if_data *sdata =
 4610		container_of(work, struct ieee80211_sub_if_data,
 4611			     u.mgd.beacon_connection_loss_work);
 4612	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4613
 4614	if (ifmgd->connection_loss) {
 4615		sdata_info(sdata, "Connection to AP %pM lost\n",
 4616			   sdata->vif.cfg.ap_addr);
 4617		__ieee80211_disconnect(sdata);
 4618		ifmgd->connection_loss = false;
 4619	} else if (ifmgd->driver_disconnect) {
 4620		sdata_info(sdata,
 4621			   "Driver requested disconnection from AP %pM\n",
 4622			   sdata->vif.cfg.ap_addr);
 4623		__ieee80211_disconnect(sdata);
 4624		ifmgd->driver_disconnect = false;
 4625	} else {
 4626		if (ifmgd->associated)
 4627			sdata->deflink.u.mgd.beacon_loss_count++;
 4628		ieee80211_mgd_probe_ap(sdata, true);
 4629	}
 4630}
 4631
 4632static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
 4633					       struct wiphy_work *work)
 4634{
 4635	struct ieee80211_sub_if_data *sdata =
 4636		container_of(work, struct ieee80211_sub_if_data,
 4637			     u.mgd.csa_connection_drop_work);
 4638
 4639	__ieee80211_disconnect(sdata);
 4640}
 4641
 4642void ieee80211_beacon_loss(struct ieee80211_vif *vif)
 4643{
 4644	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 4645	struct ieee80211_hw *hw = &sdata->local->hw;
 4646
 4647	trace_api_beacon_loss(sdata);
 4648
 4649	sdata->u.mgd.connection_loss = false;
 4650	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
 4651}
 4652EXPORT_SYMBOL(ieee80211_beacon_loss);
 4653
 4654void ieee80211_connection_loss(struct ieee80211_vif *vif)
 4655{
 4656	struct ieee80211_sub_if_data *sdata;
 4657	struct ieee80211_hw *hw;
 4658
 4659	KUNIT_STATIC_STUB_REDIRECT(ieee80211_connection_loss, vif);
 4660
 4661	sdata = vif_to_sdata(vif);
 4662	hw = &sdata->local->hw;
 4663
 4664	trace_api_connection_loss(sdata);
 4665
 4666	sdata->u.mgd.connection_loss = true;
 4667	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
 4668}
 4669EXPORT_SYMBOL(ieee80211_connection_loss);
 4670
 4671void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
 4672{
 4673	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 4674	struct ieee80211_hw *hw = &sdata->local->hw;
 4675
 4676	trace_api_disconnect(sdata, reconnect);
 4677
 4678	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
 4679		return;
 4680
 4681	sdata->u.mgd.driver_disconnect = true;
 4682	sdata->u.mgd.reconnect = reconnect;
 4683	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
 4684}
 4685EXPORT_SYMBOL(ieee80211_disconnect);
 4686
 4687static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
 4688					bool assoc)
 4689{
 4690	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
 4691
 4692	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4693
 4694	sdata->u.mgd.auth_data = NULL;
 4695
 4696	if (!assoc) {
 4697		/*
 4698		 * we are not authenticated yet, the only timer that could be
 4699		 * running is the timeout for the authentication response which
 4700		 * which is not relevant anymore.
 4701		 */
 4702		timer_delete_sync(&sdata->u.mgd.timer);
 4703		sta_info_destroy_addr(sdata, auth_data->ap_addr);
 4704
 4705		/* other links are destroyed */
 4706		eth_zero_addr(sdata->deflink.u.mgd.bssid);
 4707		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
 4708						  BSS_CHANGED_BSSID);
 4709		sdata->u.mgd.flags = 0;
 4710
 4711		ieee80211_link_release_channel(&sdata->deflink);
 4712		ieee80211_vif_set_links(sdata, 0, 0);
 4713	}
 4714
 4715	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
 4716	kfree(auth_data);
 4717}
 4718
 4719enum assoc_status {
 4720	ASSOC_SUCCESS,
 4721	ASSOC_REJECTED,
 4722	ASSOC_TIMEOUT,
 4723	ASSOC_ABANDON,
 4724};
 4725
 4726static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
 4727					 enum assoc_status status)
 4728{
 4729	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
 4730
 4731	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4732
 4733	sdata->u.mgd.assoc_data = NULL;
 4734
 4735	if (status != ASSOC_SUCCESS) {
 4736		/*
 4737		 * we are not associated yet, the only timer that could be
 4738		 * running is the timeout for the association response which
 4739		 * which is not relevant anymore.
 4740		 */
 4741		timer_delete_sync(&sdata->u.mgd.timer);
 4742		sta_info_destroy_addr(sdata, assoc_data->ap_addr);
 4743
 4744		eth_zero_addr(sdata->deflink.u.mgd.bssid);
 4745		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
 4746						  BSS_CHANGED_BSSID);
 4747		sdata->u.mgd.flags = 0;
 4748		sdata->vif.bss_conf.mu_mimo_owner = false;
 4749
 4750		if (status != ASSOC_REJECTED) {
 4751			struct cfg80211_assoc_failure data = {
 4752				.timeout = status == ASSOC_TIMEOUT,
 4753			};
 4754			int i;
 4755
 4756			BUILD_BUG_ON(ARRAY_SIZE(data.bss) !=
 4757				     ARRAY_SIZE(assoc_data->link));
 4758
 4759			for (i = 0; i < ARRAY_SIZE(data.bss); i++)
 4760				data.bss[i] = assoc_data->link[i].bss;
 4761
 4762			if (ieee80211_vif_is_mld(&sdata->vif))
 4763				data.ap_mld_addr = assoc_data->ap_addr;
 4764
 4765			cfg80211_assoc_failure(sdata->dev, &data);
 4766		}
 4767
 4768		ieee80211_link_release_channel(&sdata->deflink);
 4769		ieee80211_vif_set_links(sdata, 0, 0);
 4770	}
 4771
 4772	kfree(assoc_data);
 4773}
 4774
 4775static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
 4776				     struct ieee80211_mgmt *mgmt, size_t len)
 4777{
 4778	struct ieee80211_local *local = sdata->local;
 4779	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
 4780	const struct element *challenge;
 4781	u8 *pos;
 4782	u32 tx_flags = 0;
 4783	struct ieee80211_prep_tx_info info = {
 4784		.subtype = IEEE80211_STYPE_AUTH,
 4785		.link_id = auth_data->link_id,
 4786	};
 4787
 4788	pos = mgmt->u.auth.variable;
 4789	challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
 4790				       len - (pos - (u8 *)mgmt));
 4791	if (!challenge)
 4792		return;
 4793	auth_data->expected_transaction = 4;
 4794	drv_mgd_prepare_tx(sdata->local, sdata, &info);
 4795	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
 4796		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
 4797			   IEEE80211_TX_INTFL_MLME_CONN_TX;
 4798	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
 4799			    (void *)challenge,
 4800			    challenge->datalen + sizeof(*challenge),
 4801			    auth_data->ap_addr, auth_data->ap_addr,
 4802			    auth_data->key, auth_data->key_len,
 4803			    auth_data->key_idx, tx_flags);
 4804}
 4805
 4806static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata)
 4807{
 4808	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4809	const u8 *ap_addr = ifmgd->auth_data->ap_addr;
 4810	struct sta_info *sta;
 4811
 4812	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4813
 4814	sdata_info(sdata, "authenticated\n");
 4815	ifmgd->auth_data->done = true;
 4816	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
 4817	ifmgd->auth_data->timeout_started = true;
 4818	run_again(sdata, ifmgd->auth_data->timeout);
 4819
 4820	/* move station state to auth */
 4821	sta = sta_info_get(sdata, ap_addr);
 4822	if (!sta) {
 4823		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, ap_addr);
 4824		return false;
 4825	}
 4826	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
 4827		sdata_info(sdata, "failed moving %pM to auth\n", ap_addr);
 4828		return false;
 4829	}
 4830
 4831	return true;
 4832}
 4833
 4834static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
 4835				   struct ieee80211_mgmt *mgmt, size_t len)
 4836{
 4837	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 4838	u16 auth_alg, auth_transaction, status_code;
 4839	struct ieee80211_event event = {
 4840		.type = MLME_EVENT,
 4841		.u.mlme.data = AUTH_EVENT,
 4842	};
 4843	struct ieee80211_prep_tx_info info = {
 4844		.subtype = IEEE80211_STYPE_AUTH,
 4845	};
 4846	bool sae_need_confirm = false;
 4847
 4848	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 4849
 4850	if (len < 24 + 6)
 4851		return;
 4852
 4853	if (!ifmgd->auth_data || ifmgd->auth_data->done)
 4854		return;
 4855
 4856	if (!ether_addr_equal(ifmgd->auth_data->ap_addr, mgmt->bssid))
 4857		return;
 4858
 4859	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
 4860	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
 4861	status_code = le16_to_cpu(mgmt->u.auth.status_code);
 4862
 4863	info.link_id = ifmgd->auth_data->link_id;
 4864
 4865	if (auth_alg != ifmgd->auth_data->algorithm ||
 4866	    (auth_alg != WLAN_AUTH_SAE &&
 4867	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
 4868	    (auth_alg == WLAN_AUTH_SAE &&
 4869	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
 4870	      auth_transaction > 2))) {
 4871		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
 4872			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
 4873			   auth_transaction,
 4874			   ifmgd->auth_data->expected_transaction);
 4875		goto notify_driver;
 4876	}
 4877
 4878	if (status_code != WLAN_STATUS_SUCCESS) {
 4879		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
 4880
 4881		if (auth_alg == WLAN_AUTH_SAE &&
 4882		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
 4883		     (auth_transaction == 1 &&
 4884		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
 4885		       status_code == WLAN_STATUS_SAE_PK)))) {
 4886			/* waiting for userspace now */
 4887			ifmgd->auth_data->waiting = true;
 4888			ifmgd->auth_data->timeout =
 4889				jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
 4890			ifmgd->auth_data->timeout_started = true;
 4891			run_again(sdata, ifmgd->auth_data->timeout);
 4892			if (auth_transaction == 1)
 4893				sae_need_confirm = true;
 4894			goto notify_driver;
 4895		}
 4896
 4897		sdata_info(sdata, "%pM denied authentication (status %d)\n",
 4898			   mgmt->sa, status_code);
 4899		ieee80211_destroy_auth_data(sdata, false);
 4900		event.u.mlme.status = MLME_DENIED;
 4901		event.u.mlme.reason = status_code;
 4902		drv_event_callback(sdata->local, sdata, &event);
 4903		goto notify_driver;
 4904	}
 4905
 4906	switch (ifmgd->auth_data->algorithm) {
 4907	case WLAN_AUTH_OPEN:
 4908	case WLAN_AUTH_LEAP:
 4909	case WLAN_AUTH_FT:
 4910	case WLAN_AUTH_SAE:
 4911	case WLAN_AUTH_FILS_SK:
 4912	case WLAN_AUTH_FILS_SK_PFS:
 4913	case WLAN_AUTH_FILS_PK:
 4914		break;
 4915	case WLAN_AUTH_SHARED_KEY:
 4916		if (ifmgd->auth_data->expected_transaction != 4) {
 4917			ieee80211_auth_challenge(sdata, mgmt, len);
 4918			/* need another frame */
 4919			return;
 4920		}
 4921		break;
 4922	default:
 4923		WARN_ONCE(1, "invalid auth alg %d",
 4924			  ifmgd->auth_data->algorithm);
 4925		goto notify_driver;
 4926	}
 4927
 4928	event.u.mlme.status = MLME_SUCCESS;
 4929	info.success = 1;
 4930	drv_event_callback(sdata->local, sdata, &event);
 4931	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
 4932	    (auth_transaction == 2 &&
 4933	     ifmgd->auth_data->expected_transaction == 2)) {
 4934		if (!ieee80211_mark_sta_auth(sdata))
 4935			return; /* ignore frame -- wait for timeout */
 4936	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
 4937		   auth_transaction == 1) {
 4938		sae_need_confirm = true;
 4939	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
 4940		   auth_transaction == 2) {
 4941		sdata_info(sdata, "SAE peer confirmed\n");
 4942		ifmgd->auth_data->peer_confirmed = true;
 4943	}
 4944
 4945	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
 4946notify_driver:
 4947	if (!sae_need_confirm)
 4948		drv_mgd_complete_tx(sdata->local, sdata, &info);
 4949}
 4950
 4951#define case_WLAN(type) \
 4952	case WLAN_REASON_##type: return #type
 4953
 4954const char *ieee80211_get_reason_code_string(u16 reason_code)
 4955{
 4956	switch (reason_code) {
 4957	case_WLAN(UNSPECIFIED);
 4958	case_WLAN(PREV_AUTH_NOT_VALID);
 4959	case_WLAN(DEAUTH_LEAVING);
 4960	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
 4961	case_WLAN(DISASSOC_AP_BUSY);
 4962	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
 4963	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
 4964	case_WLAN(DISASSOC_STA_HAS_LEFT);
 4965	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
 4966	case_WLAN(DISASSOC_BAD_POWER);
 4967	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
 4968	case_WLAN(INVALID_IE);
 4969	case_WLAN(MIC_FAILURE);
 4970	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
 4971	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
 4972	case_WLAN(IE_DIFFERENT);
 4973	case_WLAN(INVALID_GROUP_CIPHER);
 4974	case_WLAN(INVALID_PAIRWISE_CIPHER);
 4975	case_WLAN(INVALID_AKMP);
 4976	case_WLAN(UNSUPP_RSN_VERSION);
 4977	case_WLAN(INVALID_RSN_IE_CAP);
 4978	case_WLAN(IEEE8021X_FAILED);
 4979	case_WLAN(CIPHER_SUITE_REJECTED);
 4980	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
 4981	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
 4982	case_WLAN(DISASSOC_LOW_ACK);
 4983	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
 4984	case_WLAN(QSTA_LEAVE_QBSS);
 4985	case_WLAN(QSTA_NOT_USE);
 4986	case_WLAN(QSTA_REQUIRE_SETUP);
 4987	case_WLAN(QSTA_TIMEOUT);
 4988	case_WLAN(QSTA_CIPHER_NOT_SUPP);
 4989	case_WLAN(MESH_PEER_CANCELED);
 4990	case_WLAN(MESH_MAX_PEERS);
 4991	case_WLAN(MESH_CONFIG);
 4992	case_WLAN(MESH_CLOSE);
 4993	case_WLAN(MESH_MAX_RETRIES);
 4994	case_WLAN(MESH_CONFIRM_TIMEOUT);
 4995	case_WLAN(MESH_INVALID_GTK);
 4996	case_WLAN(MESH_INCONSISTENT_PARAM);
 4997	case_WLAN(MESH_INVALID_SECURITY);
 4998	case_WLAN(MESH_PATH_ERROR);
 4999	case_WLAN(MESH_PATH_NOFORWARD);
 5000	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
 5001	case_WLAN(MAC_EXISTS_IN_MBSS);
 5002	case_WLAN(MESH_CHAN_REGULATORY);
 5003	case_WLAN(MESH_CHAN);
 5004	default: return "<unknown>";
 5005	}
 5006}
 5007
 5008static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
 5009				     struct ieee80211_mgmt *mgmt, size_t len)
 5010{
 5011	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 5012	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
 5013
 5014	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 5015
 5016	if (len < 24 + 2)
 5017		return;
 5018
 5019	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
 5020		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
 5021		return;
 5022	}
 5023
 5024	if (ifmgd->associated &&
 5025	    ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
 5026		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
 5027			   sdata->vif.cfg.ap_addr, reason_code,
 5028			   ieee80211_get_reason_code_string(reason_code));
 5029
 5030		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
 5031
 5032		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
 5033					    reason_code, false);
 5034		return;
 5035	}
 5036
 5037	if (ifmgd->assoc_data &&
 5038	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->ap_addr)) {
 5039		sdata_info(sdata,
 5040			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
 5041			   ifmgd->assoc_data->ap_addr, reason_code,
 5042			   ieee80211_get_reason_code_string(reason_code));
 5043
 5044		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
 5045
 5046		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
 5047		return;
 5048	}
 5049}
 5050
 5051
 5052static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
 5053				       struct ieee80211_mgmt *mgmt, size_t len)
 5054{
 5055	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 5056	u16 reason_code;
 5057
 5058	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 5059
 5060	if (len < 24 + 2)
 5061		return;
 5062
 5063	if (!ifmgd->associated ||
 5064	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
 5065		return;
 5066
 5067	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
 5068
 5069	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
 5070		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
 5071		return;
 5072	}
 5073
 5074	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
 5075		   sdata->vif.cfg.ap_addr, reason_code,
 5076		   ieee80211_get_reason_code_string(reason_code));
 5077
 5078	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
 5079
 5080	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
 5081				    false);
 5082}
 5083
 5084static bool ieee80211_twt_req_supported(struct ieee80211_sub_if_data *sdata,
 5085					struct ieee80211_supported_band *sband,
 5086					const struct link_sta_info *link_sta,
 5087					const struct ieee802_11_elems *elems)
 5088{
 5089	const struct ieee80211_sta_he_cap *own_he_cap =
 5090		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
 5091
 5092	if (elems->ext_capab_len < 10)
 5093		return false;
 5094
 5095	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
 5096		return false;
 5097
 5098	return link_sta->pub->he_cap.he_cap_elem.mac_cap_info[0] &
 5099		IEEE80211_HE_MAC_CAP0_TWT_RES &&
 5100		own_he_cap &&
 5101		(own_he_cap->he_cap_elem.mac_cap_info[0] &
 5102			IEEE80211_HE_MAC_CAP0_TWT_REQ);
 5103}
 5104
 5105static u64 ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
 5106				    struct ieee80211_supported_band *sband,
 5107				    struct ieee80211_link_data *link,
 5108				    struct link_sta_info *link_sta,
 5109				    struct ieee802_11_elems *elems)
 5110{
 5111	bool twt = ieee80211_twt_req_supported(sdata, sband, link_sta, elems);
 5112
 5113	if (link->conf->twt_requester != twt) {
 5114		link->conf->twt_requester = twt;
 5115		return BSS_CHANGED_TWT;
 5116	}
 5117	return 0;
 5118}
 5119
 5120static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
 5121					struct ieee80211_bss_conf *bss_conf,
 5122					struct ieee80211_supported_band *sband,
 5123					struct link_sta_info *link_sta)
 5124{
 5125	const struct ieee80211_sta_he_cap *own_he_cap =
 5126		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
 5127
 5128	return bss_conf->he_support &&
 5129		(link_sta->pub->he_cap.he_cap_elem.mac_cap_info[2] &
 5130			IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
 5131		own_he_cap &&
 5132		(own_he_cap->he_cap_elem.mac_cap_info[2] &
 5133			IEEE80211_HE_MAC_CAP2_BCAST_TWT);
 5134}
 5135
 5136static void ieee80211_epcs_changed(struct ieee80211_sub_if_data *sdata,
 5137				   bool enabled)
 5138{
 5139	/* in any case this is called, dialog token should be reset */
 5140	sdata->u.mgd.epcs.dialog_token = 0;
 5141
 5142	if (sdata->u.mgd.epcs.enabled == enabled)
 5143		return;
 5144
 5145	sdata->u.mgd.epcs.enabled = enabled;
 5146	cfg80211_epcs_changed(sdata->dev, enabled);
 5147}
 5148
 5149static void ieee80211_epcs_teardown(struct ieee80211_sub_if_data *sdata)
 5150{
 5151	struct ieee80211_local *local = sdata->local;
 5152	u8 link_id;
 5153
 5154	if (!sdata->u.mgd.epcs.enabled)
 5155		return;
 5156
 5157	lockdep_assert_wiphy(local->hw.wiphy);
 5158
 5159	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 5160		struct ieee802_11_elems *elems;
 5161		struct ieee80211_link_data *link;
 5162		const struct cfg80211_bss_ies *ies;
 5163		bool ret;
 5164
 5165		rcu_read_lock();
 5166
 5167		link = sdata_dereference(sdata->link[link_id], sdata);
 5168		if (!link || !link->conf || !link->conf->bss) {
 5169			rcu_read_unlock();
 5170			continue;
 5171		}
 5172
 5173		if (link->u.mgd.disable_wmm_tracking) {
 5174			rcu_read_unlock();
 5175			ieee80211_set_wmm_default(link, false, false);
 5176			continue;
 5177		}
 5178
 5179		ies = rcu_dereference(link->conf->bss->beacon_ies);
 5180		if (!ies) {
 5181			rcu_read_unlock();
 5182			ieee80211_set_wmm_default(link, false, false);
 5183			continue;
 5184		}
 5185
 5186		elems = ieee802_11_parse_elems(ies->data, ies->len,
 5187					       IEEE80211_FTYPE_MGMT |
 5188					       IEEE80211_STYPE_BEACON,
 5189					       NULL);
 5190		if (!elems) {
 5191			rcu_read_unlock();
 5192			ieee80211_set_wmm_default(link, false, false);
 5193			continue;
 5194		}
 5195
 5196		ret = _ieee80211_sta_wmm_params(local, link,
 5197						elems->wmm_param,
 5198						elems->wmm_param_len,
 5199						elems->mu_edca_param_set);
 5200
 5201		kfree(elems);
 5202		rcu_read_unlock();
 5203
 5204		if (!ret) {
 5205			ieee80211_set_wmm_default(link, false, false);
 5206			continue;
 5207		}
 5208
 5209		ieee80211_mgd_set_link_qos_params(link);
 5210		ieee80211_link_info_change_notify(sdata, link, BSS_CHANGED_QOS);
 5211	}
 5212}
 5213
 5214static bool ieee80211_assoc_config_link(struct ieee80211_link_data *link,
 5215					struct link_sta_info *link_sta,
 5216					struct cfg80211_bss *cbss,
 5217					struct ieee80211_mgmt *mgmt,
 5218					const u8 *elem_start,
 5219					unsigned int elem_len,
 5220					u64 *changed)
 5221{
 5222	struct ieee80211_sub_if_data *sdata = link->sdata;
 5223	struct ieee80211_mgd_assoc_data *assoc_data =
 5224		sdata->u.mgd.assoc_data ?: sdata->u.mgd.reconf.add_links_data;
 5225	struct ieee80211_bss_conf *bss_conf = link->conf;
 5226	struct ieee80211_local *local = sdata->local;
 5227	unsigned int link_id = link->link_id;
 5228	struct ieee80211_elems_parse_params parse_params = {
 5229		.mode = link->u.mgd.conn.mode,
 5230		.start = elem_start,
 5231		.len = elem_len,
 5232		.link_id = link_id == assoc_data->assoc_link_id ? -1 : link_id,
 5233		.from_ap = true,
 5234		.type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE,
 5235	};
 5236	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
 5237	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
 5238	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
 5239	const struct cfg80211_bss_ies *bss_ies = NULL;
 5240	struct ieee80211_supported_band *sband;
 5241	struct ieee802_11_elems *elems;
 5242	const __le16 prof_bss_param_ch_present =
 5243		cpu_to_le16(IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT);
 5244	u16 capab_info;
 5245	bool ret;
 5246
 5247	elems = ieee802_11_parse_elems_full(&parse_params);
 5248	if (!elems)
 5249		return false;
 5250
 5251	if (link_id == assoc_data->assoc_link_id) {
 5252		capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
 5253
 5254		/*
 5255		 * we should not get to this flow unless the association was
 5256		 * successful, so set the status directly to success
 5257		 */
 5258		assoc_data->link[link_id].status = WLAN_STATUS_SUCCESS;
 5259		if (elems->ml_basic) {
 5260			int bss_param_ch_cnt =
 5261				ieee80211_mle_get_bss_param_ch_cnt((const void *)elems->ml_basic);
 5262
 5263			if (bss_param_ch_cnt < 0) {
 5264				ret = false;
 5265				goto out;
 5266			}
 5267			bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
 5268			bss_conf->bss_param_ch_cnt_link_id = link_id;
 5269		}
 5270	} else if (elems->parse_error & IEEE80211_PARSE_ERR_DUP_NEST_ML_BASIC ||
 5271		   !elems->prof ||
 5272		   !(elems->prof->control & prof_bss_param_ch_present)) {
 5273		ret = false;
 5274		goto out;
 5275	} else {
 5276		const u8 *ptr = elems->prof->variable +
 5277				elems->prof->sta_info_len - 1;
 5278		int bss_param_ch_cnt;
 5279
 5280		/*
 5281		 * During parsing, we validated that these fields exist,
 5282		 * otherwise elems->prof would have been set to NULL.
 5283		 */
 5284		capab_info = get_unaligned_le16(ptr);
 5285		assoc_data->link[link_id].status = get_unaligned_le16(ptr + 2);
 5286		bss_param_ch_cnt =
 5287			ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(elems->prof);
 5288		bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
 5289		bss_conf->bss_param_ch_cnt_link_id = link_id;
 5290
 5291		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
 5292			link_info(link, "association response status code=%u\n",
 5293				  assoc_data->link[link_id].status);
 5294			ret = true;
 5295			goto out;
 5296		}
 5297	}
 5298
 5299	if (!is_s1g && !elems->supp_rates) {
 5300		sdata_info(sdata, "no SuppRates element in AssocResp\n");
 5301		ret = false;
 5302		goto out;
 5303	}
 5304
 5305	link->u.mgd.tdls_chan_switch_prohibited =
 5306		elems->ext_capab && elems->ext_capab_len >= 5 &&
 5307		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
 5308
 5309	/*
 5310	 * Some APs are erroneously not including some information in their
 5311	 * (re)association response frames. Try to recover by using the data
 5312	 * from the beacon or probe response. This seems to afflict mobile
 5313	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
 5314	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
 5315	 */
 5316	if (!ieee80211_hw_check(&local->hw, STRICT) && !is_6ghz &&
 5317	    ((assoc_data->wmm && !elems->wmm_param) ||
 5318	     (link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
 5319	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
 5320	     (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
 5321	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
 5322		const struct cfg80211_bss_ies *ies;
 5323		struct ieee802_11_elems *bss_elems;
 5324
 5325		rcu_read_lock();
 5326		ies = rcu_dereference(cbss->ies);
 5327		if (ies)
 5328			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
 5329					  GFP_ATOMIC);
 5330		rcu_read_unlock();
 5331		if (!bss_ies) {
 5332			ret = false;
 5333			goto out;
 5334		}
 5335
 5336		parse_params.start = bss_ies->data;
 5337		parse_params.len = bss_ies->len;
 5338		parse_params.bss = cbss;
 5339		parse_params.link_id = -1;
 5340		bss_elems = ieee802_11_parse_elems_full(&parse_params);
 5341		if (!bss_elems) {
 5342			ret = false;
 5343			goto out;
 5344		}
 5345
 5346		if (assoc_data->wmm &&
 5347		    !elems->wmm_param && bss_elems->wmm_param) {
 5348			elems->wmm_param = bss_elems->wmm_param;
 5349			sdata_info(sdata,
 5350				   "AP bug: WMM param missing from AssocResp\n");
 5351		}
 5352
 5353		/*
 5354		 * Also check if we requested HT/VHT, otherwise the AP doesn't
 5355		 * have to include the IEs in the (re)association response.
 5356		 */
 5357		if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
 5358		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
 5359			elems->ht_cap_elem = bss_elems->ht_cap_elem;
 5360			sdata_info(sdata,
 5361				   "AP bug: HT capability missing from AssocResp\n");
 5362		}
 5363		if (!elems->ht_operation && bss_elems->ht_operation &&
 5364		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
 5365			elems->ht_operation = bss_elems->ht_operation;
 5366			sdata_info(sdata,
 5367				   "AP bug: HT operation missing from AssocResp\n");
 5368		}
 5369
 5370		if (is_5ghz) {
 5371			if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
 5372			    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
 5373				elems->vht_cap_elem = bss_elems->vht_cap_elem;
 5374				sdata_info(sdata,
 5375					   "AP bug: VHT capa missing from AssocResp\n");
 5376			}
 5377
 5378			if (!elems->vht_operation && bss_elems->vht_operation &&
 5379			    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
 5380				elems->vht_operation = bss_elems->vht_operation;
 5381				sdata_info(sdata,
 5382					   "AP bug: VHT operation missing from AssocResp\n");
 5383			}
 5384		}
 5385		kfree(bss_elems);
 5386	}
 5387
 5388	/*
 5389	 * We previously checked these in the beacon/probe response, so
 5390	 * they should be present here. This is just a safety net.
 5391	 * Note that the ieee80211_config_bw() below would also check
 5392	 * for this (and more), but this has better error reporting.
 5393	 */
 5394	if (!is_6ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
 5395	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
 5396		sdata_info(sdata,
 5397			   "HT AP is missing WMM params or HT capability/operation\n");
 5398		ret = false;
 5399		goto out;
 5400	}
 5401
 5402	if (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
 5403	    (!elems->vht_cap_elem || !elems->vht_operation)) {
 5404		sdata_info(sdata,
 5405			   "VHT AP is missing VHT capability/operation\n");
 5406		ret = false;
 5407		goto out;
 5408	}
 5409
 5410	/* check/update if AP changed anything in assoc response vs. scan */
 5411	if (ieee80211_config_bw(link, elems,
 5412				link_id == assoc_data->assoc_link_id,
 5413				changed,
 5414				le16_to_cpu(mgmt->frame_control) &
 5415					IEEE80211_FCTL_STYPE)) {
 5416		ret = false;
 5417		goto out;
 5418	}
 5419
 5420	if (WARN_ON(!link->conf->chanreq.oper.chan)) {
 5421		ret = false;
 5422		goto out;
 5423	}
 5424	sband = local->hw.wiphy->bands[link->conf->chanreq.oper.chan->band];
 5425
 5426	/* Set up internal HT/VHT capabilities */
 5427	if (elems->ht_cap_elem && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT)
 5428		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
 5429						  elems->ht_cap_elem,
 5430						  link_sta);
 5431
 5432	if (elems->vht_cap_elem &&
 5433	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
 5434		const struct ieee80211_vht_cap *bss_vht_cap = NULL;
 5435		const struct cfg80211_bss_ies *ies;
 5436
 5437		/*
 5438		 * Cisco AP module 9115 with FW 17.3 has a bug and sends a
 5439		 * too large maximum MPDU length in the association response
 5440		 * (indicating 12k) that it cannot actually process ...
 5441		 * Work around that.
 5442		 */
 5443		rcu_read_lock();
 5444		ies = rcu_dereference(cbss->ies);
 5445		if (ies) {
 5446			const struct element *elem;
 5447
 5448			elem = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY,
 5449						  ies->data, ies->len);
 5450			if (elem && elem->datalen >= sizeof(*bss_vht_cap))
 5451				bss_vht_cap = (const void *)elem->data;
 5452		}
 5453
 5454		if (ieee80211_hw_check(&local->hw, STRICT) &&
 5455		    (!bss_vht_cap || memcmp(bss_vht_cap, elems->vht_cap_elem,
 5456					    sizeof(*bss_vht_cap)))) {
 5457			rcu_read_unlock();
 5458			ret = false;
 5459			link_info(link, "VHT capabilities mismatch\n");
 5460			goto out;
 5461		}
 5462
 5463		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
 5464						    elems->vht_cap_elem,
 5465						    bss_vht_cap, link_sta);
 5466		rcu_read_unlock();
 5467	}
 5468
 5469	if (elems->he_operation &&
 5470	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE &&
 5471	    elems->he_cap) {
 5472		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
 5473						  elems->he_cap,
 5474						  elems->he_cap_len,
 5475						  elems->he_6ghz_capa,
 5476						  link_sta);
 5477
 5478		bss_conf->he_support = link_sta->pub->he_cap.has_he;
 5479		if (elems->rsnx && elems->rsnx_len &&
 5480		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
 5481		    wiphy_ext_feature_isset(local->hw.wiphy,
 5482					    NL80211_EXT_FEATURE_PROTECTED_TWT))
 5483			bss_conf->twt_protected = true;
 5484		else
 5485			bss_conf->twt_protected = false;
 5486
 5487		*changed |= ieee80211_recalc_twt_req(sdata, sband, link,
 5488						     link_sta, elems);
 5489
 5490		if (elems->eht_operation && elems->eht_cap &&
 5491		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_EHT) {
 5492			ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband,
 5493							    elems->he_cap,
 5494							    elems->he_cap_len,
 5495							    elems->eht_cap,
 5496							    elems->eht_cap_len,
 5497							    link_sta);
 5498
 5499			bss_conf->eht_support = link_sta->pub->eht_cap.has_eht;
 5500			bss_conf->epcs_support = bss_conf->eht_support &&
 5501				!!(elems->eht_cap->fixed.mac_cap_info[0] &
 5502				   IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS);
 5503
 5504			/* EPCS might be already enabled but a new added link
 5505			 * does not support EPCS. This should not really happen
 5506			 * in practice.
 5507			 */
 5508			if (sdata->u.mgd.epcs.enabled &&
 5509			    !bss_conf->epcs_support)
 5510				ieee80211_epcs_teardown(sdata);
 5511		} else {
 5512			bss_conf->eht_support = false;
 5513			bss_conf->epcs_support = false;
 5514		}
 5515	} else {
 5516		bss_conf->he_support = false;
 5517		bss_conf->twt_requester = false;
 5518		bss_conf->twt_protected = false;
 5519		bss_conf->eht_support = false;
 5520		bss_conf->epcs_support = false;
 5521	}
 5522
 5523	if (elems->s1g_oper &&
 5524	    link->u.mgd.conn.mode == IEEE80211_CONN_MODE_S1G &&
 5525	    elems->s1g_capab)
 5526		ieee80211_s1g_cap_to_sta_s1g_cap(sdata, elems->s1g_capab,
 5527						 link_sta);
 5528
 5529	bss_conf->twt_broadcast =
 5530		ieee80211_twt_bcast_support(sdata, bss_conf, sband, link_sta);
 5531
 5532	if (bss_conf->he_support) {
 5533		bss_conf->he_bss_color.color =
 5534			le32_get_bits(elems->he_operation->he_oper_params,
 5535				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
 5536		bss_conf->he_bss_color.partial =
 5537			le32_get_bits(elems->he_operation->he_oper_params,
 5538				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
 5539		bss_conf->he_bss_color.enabled =
 5540			!le32_get_bits(elems->he_operation->he_oper_params,
 5541				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
 5542
 5543		if (bss_conf->he_bss_color.enabled)
 5544			*changed |= BSS_CHANGED_HE_BSS_COLOR;
 5545
 5546		bss_conf->htc_trig_based_pkt_ext =
 5547			le32_get_bits(elems->he_operation->he_oper_params,
 5548				      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
 5549		bss_conf->frame_time_rts_th =
 5550			le32_get_bits(elems->he_operation->he_oper_params,
 5551				      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
 5552
 5553		bss_conf->uora_exists = !!elems->uora_element;
 5554		if (elems->uora_element)
 5555			bss_conf->uora_ocw_range = elems->uora_element[0];
 5556
 5557		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
 5558		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
 5559		/* TODO: OPEN: what happens if BSS color disable is set? */
 5560	}
 5561
 5562	if (cbss->transmitted_bss) {
 5563		bss_conf->nontransmitted = true;
 5564		ether_addr_copy(bss_conf->transmitter_bssid,
 5565				cbss->transmitted_bss->bssid);
 5566		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
 5567		bss_conf->bssid_index = cbss->bssid_index;
 5568	}
 5569
 5570	/*
 5571	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
 5572	 * in their association response, so ignore that data for our own
 5573	 * configuration. If it changed since the last beacon, we'll get the
 5574	 * next beacon and update then.
 5575	 */
 5576
 5577	/*
 5578	 * If an operating mode notification IE is present, override the
 5579	 * NSS calculation (that would be done in rate_control_rate_init())
 5580	 * and use the # of streams from that element.
 5581	 */
 5582	if (elems->opmode_notif &&
 5583	    !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
 5584		u8 nss;
 5585
 5586		nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
 5587		nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
 5588		nss += 1;
 5589		link_sta->pub->rx_nss = nss;
 5590	}
 5591
 5592	/*
 5593	 * Always handle WMM once after association regardless
 5594	 * of the first value the AP uses. Setting -1 here has
 5595	 * that effect because the AP values is an unsigned
 5596	 * 4-bit value.
 5597	 */
 5598	link->u.mgd.wmm_last_param_set = -1;
 5599	link->u.mgd.mu_edca_last_param_set = -1;
 5600
 5601	if (link->u.mgd.disable_wmm_tracking) {
 5602		ieee80211_set_wmm_default(link, false, false);
 5603	} else if (!ieee80211_sta_wmm_params(local, link, elems->wmm_param,
 5604					     elems->wmm_param_len,
 5605					     elems->mu_edca_param_set)) {
 5606		/* still enable QoS since we might have HT/VHT */
 5607		ieee80211_set_wmm_default(link, false, true);
 5608		/* disable WMM tracking in this case to disable
 5609		 * tracking WMM parameter changes in the beacon if
 5610		 * the parameters weren't actually valid. Doing so
 5611		 * avoids changing parameters very strangely when
 5612		 * the AP is going back and forth between valid and
 5613		 * invalid parameters.
 5614		 */
 5615		link->u.mgd.disable_wmm_tracking = true;
 5616	}
 5617
 5618	if (elems->max_idle_period_ie) {
 5619		bss_conf->max_idle_period =
 5620			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
 5621		bss_conf->protected_keep_alive =
 5622			!!(elems->max_idle_period_ie->idle_options &
 5623			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
 5624		*changed |= BSS_CHANGED_KEEP_ALIVE;
 5625	} else {
 5626		bss_conf->max_idle_period = 0;
 5627		bss_conf->protected_keep_alive = false;
 5628	}
 5629
 5630	/* set assoc capability (AID was already set earlier),
 5631	 * ieee80211_set_associated() will tell the driver */
 5632	bss_conf->assoc_capability = capab_info;
 5633
 5634	ret = true;
 5635out:
 5636	kfree(elems);
 5637	kfree(bss_ies);
 5638	return ret;
 5639}
 5640
 5641static int ieee80211_mgd_setup_link_sta(struct ieee80211_link_data *link,
 5642					struct sta_info *sta,
 5643					struct link_sta_info *link_sta,
 5644					struct cfg80211_bss *cbss)
 5645{
 5646	struct ieee80211_sub_if_data *sdata = link->sdata;
 5647	struct ieee80211_local *local = sdata->local;
 5648	struct ieee80211_bss *bss = (void *)cbss->priv;
 5649	u32 rates = 0, basic_rates = 0;
 5650	bool have_higher_than_11mbit = false;
 5651	int min_rate = INT_MAX, min_rate_index = -1;
 5652	struct ieee80211_supported_band *sband;
 5653
 5654	memcpy(link_sta->addr, cbss->bssid, ETH_ALEN);
 5655	memcpy(link_sta->pub->addr, cbss->bssid, ETH_ALEN);
 5656
 5657	/* TODO: S1G Basic Rate Set is expressed elsewhere */
 5658	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
 5659		ieee80211_s1g_sta_rate_init(sta);
 5660		return 0;
 5661	}
 5662
 5663	sband = local->hw.wiphy->bands[cbss->channel->band];
 5664
 5665	ieee80211_get_rates(sband, bss->supp_rates, bss->supp_rates_len,
 5666			    NULL, 0,
 5667			    &rates, &basic_rates, NULL,
 5668			    &have_higher_than_11mbit,
 5669			    &min_rate, &min_rate_index);
 5670
 5671	/*
 5672	 * This used to be a workaround for basic rates missing
 5673	 * in the association response frame. Now that we no
 5674	 * longer use the basic rates from there, it probably
 5675	 * doesn't happen any more, but keep the workaround so
 5676	 * in case some *other* APs are buggy in different ways
 5677	 * we can connect -- with a warning.
 5678	 * Allow this workaround only in case the AP provided at least
 5679	 * one rate.
 5680	 */
 5681	if (min_rate_index < 0) {
 5682		link_info(link, "No legacy rates in association response\n");
 5683		return -EINVAL;
 5684	} else if (!basic_rates) {
 5685		link_info(link, "No basic rates, using min rate instead\n");
 5686		basic_rates = BIT(min_rate_index);
 5687	}
 5688
 5689	if (rates)
 5690		link_sta->pub->supp_rates[cbss->channel->band] = rates;
 5691	else
 5692		link_info(link, "No rates found, keeping mandatory only\n");
 5693
 5694	link->conf->basic_rates = basic_rates;
 5695
 5696	/* cf. IEEE 802.11 9.2.12 */
 5697	link->operating_11g_mode = sband->band == NL80211_BAND_2GHZ &&
 5698				   have_higher_than_11mbit;
 5699
 5700	return 0;
 5701}
 5702
 5703static u8 ieee80211_max_rx_chains(struct ieee80211_link_data *link,
 5704				  struct cfg80211_bss *cbss)
 5705{
 5706	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
 5707	const struct element *ht_cap_elem, *vht_cap_elem;
 5708	const struct cfg80211_bss_ies *ies;
 5709	const struct ieee80211_ht_cap *ht_cap;
 5710	const struct ieee80211_vht_cap *vht_cap;
 5711	const struct ieee80211_he_cap_elem *he_cap;
 5712	const struct element *he_cap_elem;
 5713	u16 mcs_80_map, mcs_160_map;
 5714	int i, mcs_nss_size;
 5715	bool support_160;
 5716	u8 chains = 1;
 5717
 5718	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HT)
 5719		return chains;
 5720
 5721	ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
 5722	if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
 5723		ht_cap = (void *)ht_cap_elem->data;
 5724		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
 5725		/*
 5726		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
 5727		 *	 "Tx Unequal Modulation Supported" fields.
 5728		 */
 5729	}
 5730
 5731	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_VHT)
 5732		return chains;
 5733
 5734	vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
 5735	if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
 5736		u8 nss;
 5737		u16 tx_mcs_map;
 5738
 5739		vht_cap = (void *)vht_cap_elem->data;
 5740		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
 5741		for (nss = 8; nss > 0; nss--) {
 5742			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
 5743					IEEE80211_VHT_MCS_NOT_SUPPORTED)
 5744				break;
 5745		}
 5746		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
 5747		chains = max(chains, nss);
 5748	}
 5749
 5750	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HE)
 5751		return chains;
 5752
 5753	ies = rcu_dereference(cbss->ies);
 5754	he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
 5755					     ies->data, ies->len);
 5756
 5757	if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap) + 1)
 5758		return chains;
 5759
 5760	/* skip one byte ext_tag_id */
 5761	he_cap = (void *)(he_cap_elem->data + 1);
 5762	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
 5763
 5764	/* invalid HE IE */
 5765	if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap))
 5766		return chains;
 5767
 5768	/* mcs_nss is right after he_cap info */
 5769	he_mcs_nss_supp = (void *)(he_cap + 1);
 5770
 5771	mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
 5772
 5773	for (i = 7; i >= 0; i--) {
 5774		u8 mcs_80 = mcs_80_map >> (2 * i) & 3;
 5775
 5776		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
 5777			chains = max_t(u8, chains, i + 1);
 5778			break;
 5779		}
 5780	}
 5781
 5782	support_160 = he_cap->phy_cap_info[0] &
 5783		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
 5784
 5785	if (!support_160)
 5786		return chains;
 5787
 5788	mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160);
 5789	for (i = 7; i >= 0; i--) {
 5790		u8 mcs_160 = mcs_160_map >> (2 * i) & 3;
 5791
 5792		if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
 5793			chains = max_t(u8, chains, i + 1);
 5794			break;
 5795		}
 5796	}
 5797
 5798	return chains;
 5799}
 5800
 5801static void
 5802ieee80211_determine_our_sta_mode(struct ieee80211_sub_if_data *sdata,
 5803				 struct ieee80211_supported_band *sband,
 5804				 struct cfg80211_assoc_request *req,
 5805				 bool wmm_used, int link_id,
 5806				 struct ieee80211_conn_settings *conn)
 5807{
 5808	struct ieee80211_sta_ht_cap sta_ht_cap = sband->ht_cap;
 5809	bool is_5ghz = sband->band == NL80211_BAND_5GHZ;
 5810	bool is_6ghz = sband->band == NL80211_BAND_6GHZ;
 5811	const struct ieee80211_sta_he_cap *he_cap;
 5812	const struct ieee80211_sta_eht_cap *eht_cap;
 5813	struct ieee80211_sta_vht_cap vht_cap;
 5814
 5815	if (sband->band == NL80211_BAND_S1GHZ) {
 5816		conn->mode = IEEE80211_CONN_MODE_S1G;
 5817		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
 5818		mlme_dbg(sdata, "operating as S1G STA\n");
 5819		return;
 5820	}
 5821
 5822	conn->mode = IEEE80211_CONN_MODE_LEGACY;
 5823	conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
 5824
 5825	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
 5826
 5827	if (req && req->flags & ASSOC_REQ_DISABLE_HT) {
 5828		mlme_link_id_dbg(sdata, link_id,
 5829				 "HT disabled by flag, limiting to legacy\n");
 5830		goto out;
 5831	}
 5832
 5833	if (!wmm_used) {
 5834		mlme_link_id_dbg(sdata, link_id,
 5835				 "WMM/QoS not supported, limiting to legacy\n");
 5836		goto out;
 5837	}
 5838
 5839	if (req) {
 5840		unsigned int i;
 5841
 5842		for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
 5843			if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
 5844			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
 5845			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
 5846				netdev_info(sdata->dev,
 5847					    "WEP/TKIP use, limiting to legacy\n");
 5848				goto out;
 5849			}
 5850		}
 5851	}
 5852
 5853	if (!sta_ht_cap.ht_supported && !is_6ghz) {
 5854		mlme_link_id_dbg(sdata, link_id,
 5855				 "HT not supported (and not on 6 GHz), limiting to legacy\n");
 5856		goto out;
 5857	}
 5858
 5859	/* HT is fine */
 5860	conn->mode = IEEE80211_CONN_MODE_HT;
 5861	conn->bw_limit = sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
 5862		IEEE80211_CONN_BW_LIMIT_40 :
 5863		IEEE80211_CONN_BW_LIMIT_20;
 5864
 5865	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
 5866	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
 5867
 5868	if (req && req->flags & ASSOC_REQ_DISABLE_VHT) {
 5869		mlme_link_id_dbg(sdata, link_id,
 5870				 "VHT disabled by flag, limiting to HT\n");
 5871		goto out;
 5872	}
 5873
 5874	if (vht_cap.vht_supported && is_5ghz) {
 5875		bool have_80mhz = false;
 5876		unsigned int i;
 5877
 5878		if (conn->bw_limit == IEEE80211_CONN_BW_LIMIT_20) {
 5879			mlme_link_id_dbg(sdata, link_id,
 5880					 "no 40 MHz support on 5 GHz, limiting to HT\n");
 5881			goto out;
 5882		}
 5883
 5884		/* Allow VHT if at least one channel on the sband supports 80 MHz */
 5885		for (i = 0; i < sband->n_channels; i++) {
 5886			if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
 5887							IEEE80211_CHAN_NO_80MHZ))
 5888				continue;
 5889
 5890			have_80mhz = true;
 5891			break;
 5892		}
 5893
 5894		if (!have_80mhz) {
 5895			mlme_link_id_dbg(sdata, link_id,
 5896					 "no 80 MHz channel support on 5 GHz, limiting to HT\n");
 5897			goto out;
 5898		}
 5899	} else if (is_5ghz) { /* !vht_supported but on 5 GHz */
 5900		mlme_link_id_dbg(sdata, link_id,
 5901				 "no VHT support on 5 GHz, limiting to HT\n");
 5902		goto out;
 5903	}
 5904
 5905	/* VHT - if we have - is fine, including 80 MHz, check 160 below again */
 5906	if (sband->band != NL80211_BAND_2GHZ) {
 5907		conn->mode = IEEE80211_CONN_MODE_VHT;
 5908		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_160;
 5909	}
 5910
 5911	if (is_5ghz &&
 5912	    !(vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
 5913			     IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
 5914		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_80;
 5915		mlme_link_id_dbg(sdata, link_id,
 5916				 "no VHT 160 MHz capability on 5 GHz, limiting to 80 MHz");
 5917	}
 5918
 5919	if (req && req->flags & ASSOC_REQ_DISABLE_HE) {
 5920		mlme_link_id_dbg(sdata, link_id,
 5921				 "HE disabled by flag, limiting to HT/VHT\n");
 5922		goto out;
 5923	}
 5924
 5925	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
 5926	if (!he_cap) {
 5927		WARN_ON(is_6ghz);
 5928		mlme_link_id_dbg(sdata, link_id,
 5929				 "no HE support, limiting to HT/VHT\n");
 5930		goto out;
 5931	}
 5932
 5933	/* so we have HE */
 5934	conn->mode = IEEE80211_CONN_MODE_HE;
 5935
 5936	/* check bandwidth */
 5937	switch (sband->band) {
 5938	default:
 5939	case NL80211_BAND_2GHZ:
 5940		if (he_cap->he_cap_elem.phy_cap_info[0] &
 5941		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
 5942			break;
 5943		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
 5944		mlme_link_id_dbg(sdata, link_id,
 5945				 "no 40 MHz HE cap in 2.4 GHz, limiting to 20 MHz\n");
 5946		break;
 5947	case NL80211_BAND_5GHZ:
 5948		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
 5949		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
 5950			conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
 5951			mlme_link_id_dbg(sdata, link_id,
 5952					 "no 40/80 MHz HE cap in 5 GHz, limiting to 20 MHz\n");
 5953			break;
 5954		}
 5955		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
 5956		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
 5957			conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 5958					       conn->bw_limit,
 5959					       IEEE80211_CONN_BW_LIMIT_80);
 5960			mlme_link_id_dbg(sdata, link_id,
 5961					 "no 160 MHz HE cap in 5 GHz, limiting to 80 MHz\n");
 5962		}
 5963		break;
 5964	case NL80211_BAND_6GHZ:
 5965		if (he_cap->he_cap_elem.phy_cap_info[0] &
 5966		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
 5967			break;
 5968		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 5969				       conn->bw_limit,
 5970				       IEEE80211_CONN_BW_LIMIT_80);
 5971		mlme_link_id_dbg(sdata, link_id,
 5972				 "no 160 MHz HE cap in 6 GHz, limiting to 80 MHz\n");
 5973		break;
 5974	}
 5975
 5976	if (req && req->flags & ASSOC_REQ_DISABLE_EHT) {
 5977		mlme_link_id_dbg(sdata, link_id,
 5978				 "EHT disabled by flag, limiting to HE\n");
 5979		goto out;
 5980	}
 5981
 5982	eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
 5983	if (!eht_cap) {
 5984		mlme_link_id_dbg(sdata, link_id,
 5985				 "no EHT support, limiting to HE\n");
 5986		goto out;
 5987	}
 5988
 5989	/* we have EHT */
 5990
 5991	conn->mode = IEEE80211_CONN_MODE_EHT;
 5992
 5993	/* check bandwidth */
 5994	if (is_6ghz &&
 5995	    eht_cap->eht_cap_elem.phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
 5996		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_320;
 5997	else if (is_6ghz)
 5998		mlme_link_id_dbg(sdata, link_id,
 5999				 "no EHT 320 MHz cap in 6 GHz, limiting to 160 MHz\n");
 6000
 6001out:
 6002	mlme_link_id_dbg(sdata, link_id,
 6003			 "determined local STA to be %s, BW limited to %d MHz\n",
 6004			 ieee80211_conn_mode_str(conn->mode),
 6005			 20 * (1 << conn->bw_limit));
 6006}
 6007
 6008static void
 6009ieee80211_determine_our_sta_mode_auth(struct ieee80211_sub_if_data *sdata,
 6010				      struct ieee80211_supported_band *sband,
 6011				      struct cfg80211_auth_request *req,
 6012				      bool wmm_used,
 6013				      struct ieee80211_conn_settings *conn)
 6014{
 6015	ieee80211_determine_our_sta_mode(sdata, sband, NULL, wmm_used,
 6016					 req->link_id > 0 ? req->link_id : 0,
 6017					 conn);
 6018}
 6019
 6020static void
 6021ieee80211_determine_our_sta_mode_assoc(struct ieee80211_sub_if_data *sdata,
 6022				       struct ieee80211_supported_band *sband,
 6023				       struct cfg80211_assoc_request *req,
 6024				       bool wmm_used, int link_id,
 6025				       struct ieee80211_conn_settings *conn)
 6026{
 6027	struct ieee80211_conn_settings tmp;
 6028
 6029	WARN_ON(!req);
 6030
 6031	ieee80211_determine_our_sta_mode(sdata, sband, req, wmm_used, link_id,
 6032					 &tmp);
 6033
 6034	conn->mode = min_t(enum ieee80211_conn_mode,
 6035			   conn->mode, tmp.mode);
 6036	conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
 6037			       conn->bw_limit, tmp.bw_limit);
 6038}
 6039
 6040static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
 6041				  struct ieee80211_link_data *link,
 6042				  int link_id,
 6043				  struct cfg80211_bss *cbss, bool mlo,
 6044				  struct ieee80211_conn_settings *conn,
 6045				  unsigned long *userspace_selectors)
 6046{
 6047	struct ieee80211_local *local = sdata->local;
 6048	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
 6049	struct ieee80211_chan_req chanreq = {};
 6050	struct cfg80211_chan_def ap_chandef;
 6051	struct ieee802_11_elems *elems;
 6052	int ret;
 6053
 6054	lockdep_assert_wiphy(local->hw.wiphy);
 6055
 6056	rcu_read_lock();
 6057	elems = ieee80211_determine_chan_mode(sdata, conn, cbss, link_id,
 6058					      &chanreq, &ap_chandef,
 6059					      userspace_selectors);
 6060
 6061	if (IS_ERR(elems)) {
 6062		rcu_read_unlock();
 6063		return PTR_ERR(elems);
 6064	}
 6065
 6066	if (mlo && !elems->ml_basic) {
 6067		sdata_info(sdata, "Rejecting MLO as it is not supported by AP\n");
 6068		rcu_read_unlock();
 6069		kfree(elems);
 6070		return -EINVAL;
 6071	}
 6072
 6073	if (link && is_6ghz && conn->mode >= IEEE80211_CONN_MODE_HE) {
 6074		const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
 6075
 6076		if (elems->pwr_constr_elem)
 6077			link->conf->pwr_reduction = *elems->pwr_constr_elem;
 6078
 6079		he_6ghz_oper = ieee80211_he_6ghz_oper(elems->he_operation);
 6080		if (he_6ghz_oper)
 6081			link->conf->power_type =
 6082				cfg80211_6ghz_power_type(he_6ghz_oper->control,
 6083							 cbss->channel->flags);
 6084		else
 6085			link_info(link,
 6086				  "HE 6 GHz operation missing (on %d MHz), expect issues\n",
 6087				  cbss->channel->center_freq);
 6088
 6089		link->conf->tpe = elems->tpe;
 6090		ieee80211_rearrange_tpe(&link->conf->tpe, &ap_chandef,
 6091					&chanreq.oper);
 6092	}
 6093	rcu_read_unlock();
 6094	/* the element data was RCU protected so no longer valid anyway */
 6095	kfree(elems);
 6096	elems = NULL;
 6097
 6098	if (!link)
 6099		return 0;
 6100
 6101	rcu_read_lock();
 6102	link->needed_rx_chains = min(ieee80211_max_rx_chains(link, cbss),
 6103				     local->rx_chains);
 6104	rcu_read_unlock();
 6105
 6106	/*
 6107	 * If this fails (possibly due to channel context sharing
 6108	 * on incompatible channels, e.g. 80+80 and 160 sharing the
 6109	 * same control channel) try to use a smaller bandwidth.
 6110	 */
 6111	ret = ieee80211_link_use_channel(link, &chanreq,
 6112					 IEEE80211_CHANCTX_SHARED);
 6113
 6114	/* don't downgrade for 5/10/S1G MHz channels, though. */
 6115	if (chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
 6116	    chanreq.oper.width == NL80211_CHAN_WIDTH_10 ||
 6117	    cfg80211_chandef_is_s1g(&chanreq.oper))
 6118		return ret;
 6119
 6120	while (ret && chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT) {
 6121		ieee80211_chanreq_downgrade(&chanreq, conn);
 6122
 6123		ret = ieee80211_link_use_channel(link, &chanreq,
 6124						 IEEE80211_CHANCTX_SHARED);
 6125	}
 6126
 6127	return ret;
 6128}
 6129
 6130static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
 6131			       u8 *dtim_count, u8 *dtim_period)
 6132{
 6133	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
 6134	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
 6135					 ies->len);
 6136	const struct ieee80211_tim_ie *tim = NULL;
 6137	const struct ieee80211_bssid_index *idx;
 6138	bool valid = tim_ie && tim_ie[1] >= 2;
 6139
 6140	if (valid)
 6141		tim = (void *)(tim_ie + 2);
 6142
 6143	if (dtim_count)
 6144		*dtim_count = valid ? tim->dtim_count : 0;
 6145
 6146	if (dtim_period)
 6147		*dtim_period = valid ? tim->dtim_period : 0;
 6148
 6149	/* Check if value is overridden by non-transmitted profile */
 6150	if (!idx_ie || idx_ie[1] < 3)
 6151		return valid;
 6152
 6153	idx = (void *)(idx_ie + 2);
 6154
 6155	if (dtim_count)
 6156		*dtim_count = idx->dtim_count;
 6157
 6158	if (dtim_period)
 6159		*dtim_period = idx->dtim_period;
 6160
 6161	return true;
 6162}
 6163
 6164static u16 ieee80211_get_ttlm(u8 bm_size, u8 *data)
 6165{
 6166	if (bm_size == 1)
 6167		return *data;
 6168
 6169	return get_unaligned_le16(data);
 6170}
 6171
 6172static int
 6173ieee80211_parse_adv_t2l(struct ieee80211_sub_if_data *sdata,
 6174			const struct ieee80211_ttlm_elem *ttlm,
 6175			struct ieee80211_adv_ttlm_info *ttlm_info)
 6176{
 6177	/* The element size was already validated in
 6178	 * ieee80211_tid_to_link_map_size_ok()
 6179	 */
 6180	u8 control, link_map_presence, map_size, tid;
 6181	u8 *pos;
 6182
 6183	memset(ttlm_info, 0, sizeof(*ttlm_info));
 6184	pos = (void *)ttlm->optional;
 6185	control	= ttlm->control;
 6186
 6187	if ((control & IEEE80211_TTLM_CONTROL_DIRECTION) !=
 6188	    IEEE80211_TTLM_DIRECTION_BOTH) {
 6189		sdata_info(sdata, "Invalid advertised T2L map direction\n");
 6190		return -EINVAL;
 6191	}
 6192
 6193	if (!(control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP)) {
 6194		link_map_presence = *pos;
 6195		pos++;
 6196	}
 6197
 6198	if (control & IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT) {
 6199		ttlm_info->switch_time = get_unaligned_le16(pos);
 6200
 6201		/* Since ttlm_info->switch_time == 0 means no switch time, bump
 6202		 * it by 1.
 6203		 */
 6204		if (!ttlm_info->switch_time)
 6205			ttlm_info->switch_time = 1;
 6206
 6207		pos += 2;
 6208	}
 6209
 6210	if (control & IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT) {
 6211		ttlm_info->duration = pos[0] | pos[1] << 8 | pos[2] << 16;
 6212		pos += 3;
 6213	}
 6214
 6215	if (control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) {
 6216		ttlm_info->map = 0xffff;
 6217		return 0;
 6218	}
 6219
 6220	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
 6221		map_size = 1;
 6222	else
 6223		map_size = 2;
 6224
 6225	/* According to Draft P802.11be_D3.0 clause 35.3.7.1.7, an AP MLD shall
 6226	 * not advertise a TID-to-link mapping that does not map all TIDs to the
 6227	 * same link set, reject frame if not all links have mapping
 6228	 */
 6229	if (link_map_presence != 0xff) {
 6230		sdata_info(sdata,
 6231			   "Invalid advertised T2L mapping presence indicator\n");
 6232		return -EINVAL;
 6233	}
 6234
 6235	ttlm_info->map = ieee80211_get_ttlm(map_size, pos);
 6236	if (!ttlm_info->map) {
 6237		sdata_info(sdata,
 6238			   "Invalid advertised T2L map for TID 0\n");
 6239		return -EINVAL;
 6240	}
 6241
 6242	pos += map_size;
 6243
 6244	for (tid = 1; tid < 8; tid++) {
 6245		u16 map = ieee80211_get_ttlm(map_size, pos);
 6246
 6247		if (map != ttlm_info->map) {
 6248			sdata_info(sdata, "Invalid advertised T2L map for tid %d\n",
 6249				   tid);
 6250			return -EINVAL;
 6251		}
 6252
 6253		pos += map_size;
 6254	}
 6255	return 0;
 6256}
 6257
 6258static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
 6259				    struct ieee80211_mgmt *mgmt,
 6260				    struct ieee802_11_elems *elems,
 6261				    const u8 *elem_start, unsigned int elem_len)
 6262{
 6263	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 6264	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
 6265	struct ieee80211_local *local = sdata->local;
 6266	unsigned int link_id;
 6267	struct sta_info *sta;
 6268	u64 changed[IEEE80211_MLD_MAX_NUM_LINKS] = {};
 6269	u16 valid_links = 0, dormant_links = 0;
 6270	int err;
 6271
 6272	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 6273	/*
 6274	 * station info was already allocated and inserted before
 6275	 * the association and should be available to us
 6276	 */
 6277	sta = sta_info_get(sdata, assoc_data->ap_addr);
 6278	if (WARN_ON(!sta))
 6279		goto out_err;
 6280
 6281	sta->sta.spp_amsdu = assoc_data->spp_amsdu;
 6282
 6283	if (ieee80211_vif_is_mld(&sdata->vif)) {
 6284		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 6285			if (!assoc_data->link[link_id].bss)
 6286				continue;
 6287
 6288			valid_links |= BIT(link_id);
 6289
 6290			if (link_id != assoc_data->assoc_link_id) {
 6291				err = ieee80211_sta_allocate_link(sta, link_id);
 6292				if (err)
 6293					goto out_err;
 6294			}
 6295		}
 6296
 6297		/*
 6298		 * We do not support setting a negotiated TTLM during
 6299		 * association. As such, we can assume that if there is a TTLM,
 6300		 * then it is the currently active advertised TTLM.
 6301		 * In that case, there must be exactly one TTLM that does not
 6302		 * have a switch time set. This mapping should also leave us
 6303		 * with at least one usable link.
 6304		 */
 6305		if (elems->ttlm_num > 1) {
 6306			sdata_info(sdata,
 6307				   "More than one advertised TTLM in association response\n");
 6308			goto out_err;
 6309		} else if (elems->ttlm_num == 1) {
 6310			if (ieee80211_parse_adv_t2l(sdata, elems->ttlm[0],
 6311						    &sdata->u.mgd.ttlm_info) ||
 6312			    sdata->u.mgd.ttlm_info.switch_time != 0 ||
 6313			    !(valid_links & sdata->u.mgd.ttlm_info.map)) {
 6314				sdata_info(sdata,
 6315					   "Invalid advertised TTLM in association response\n");
 6316				goto out_err;
 6317			}
 6318
 6319			sdata->u.mgd.ttlm_info.active = true;
 6320			dormant_links =
 6321				valid_links & ~sdata->u.mgd.ttlm_info.map;
 6322		}
 6323
 6324		ieee80211_vif_set_links(sdata, valid_links, dormant_links);
 6325	}
 6326
 6327	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 6328		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
 6329		struct ieee80211_link_data *link;
 6330		struct link_sta_info *link_sta;
 6331
 6332		if (!cbss)
 6333			continue;
 6334
 6335		link = sdata_dereference(sdata->link[link_id], sdata);
 6336		if (WARN_ON(!link))
 6337			goto out_err;
 6338
 6339		if (ieee80211_vif_is_mld(&sdata->vif))
 6340			link_info(link,
 6341				  "local address %pM, AP link address %pM%s\n",
 6342				  link->conf->addr,
 6343				  assoc_data->link[link_id].bss->bssid,
 6344				  link_id == assoc_data->assoc_link_id ?
 6345					" (assoc)" : "");
 6346
 6347		link_sta = rcu_dereference_protected(sta->link[link_id],
 6348						     lockdep_is_held(&local->hw.wiphy->mtx));
 6349		if (WARN_ON(!link_sta))
 6350			goto out_err;
 6351
 6352		if (!link->u.mgd.have_beacon) {
 6353			const struct cfg80211_bss_ies *ies;
 6354
 6355			rcu_read_lock();
 6356			ies = rcu_dereference(cbss->beacon_ies);
 6357			if (ies)
 6358				link->u.mgd.have_beacon = true;
 6359			else
 6360				ies = rcu_dereference(cbss->ies);
 6361			ieee80211_get_dtim(ies,
 6362					   &link->conf->sync_dtim_count,
 6363					   &link->u.mgd.dtim_period);
 6364			link->conf->beacon_int = cbss->beacon_interval;
 6365			rcu_read_unlock();
 6366		}
 6367
 6368		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
 6369
 6370		if (link_id != assoc_data->assoc_link_id) {
 6371			link->u.mgd.conn = assoc_data->link[link_id].conn;
 6372
 6373			err = ieee80211_prep_channel(sdata, link, link_id, cbss,
 6374						     true, &link->u.mgd.conn,
 6375						     sdata->u.mgd.userspace_selectors);
 6376			if (err) {
 6377				link_info(link, "prep_channel failed\n");
 6378				goto out_err;
 6379			}
 6380		}
 6381
 6382		err = ieee80211_mgd_setup_link_sta(link, sta, link_sta,
 6383						   assoc_data->link[link_id].bss);
 6384		if (err)
 6385			goto out_err;
 6386
 6387		if (!ieee80211_assoc_config_link(link, link_sta,
 6388						 assoc_data->link[link_id].bss,
 6389						 mgmt, elem_start, elem_len,
 6390						 &changed[link_id]))
 6391			goto out_err;
 6392
 6393		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
 6394			valid_links &= ~BIT(link_id);
 6395			ieee80211_sta_remove_link(sta, link_id);
 6396			continue;
 6397		}
 6398
 6399		if (link_id != assoc_data->assoc_link_id) {
 6400			err = ieee80211_sta_activate_link(sta, link_id);
 6401			if (err)
 6402				goto out_err;
 6403		}
 6404	}
 6405
 6406	/* links might have changed due to rejected ones, set them again */
 6407	ieee80211_vif_set_links(sdata, valid_links, dormant_links);
 6408
 6409	rate_control_rate_init_all_links(sta);
 6410
 6411	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
 6412		set_sta_flag(sta, WLAN_STA_MFP);
 6413		sta->sta.mfp = true;
 6414	} else {
 6415		sta->sta.mfp = false;
 6416	}
 6417
 6418	ieee80211_sta_set_max_amsdu_subframes(sta, elems->ext_capab,
 6419					      elems->ext_capab_len);
 6420
 6421	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
 6422		       local->hw.queues >= IEEE80211_NUM_ACS;
 6423
 6424	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
 6425	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
 6426		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
 6427	if (err) {
 6428		sdata_info(sdata,
 6429			   "failed to move station %pM to desired state\n",
 6430			   sta->sta.addr);
 6431		WARN_ON(__sta_info_destroy(sta));
 6432		goto out_err;
 6433	}
 6434
 6435	if (sdata->wdev.use_4addr)
 6436		drv_sta_set_4addr(local, sdata, &sta->sta, true);
 6437
 6438	ieee80211_set_associated(sdata, assoc_data, changed);
 6439
 6440	/*
 6441	 * If we're using 4-addr mode, let the AP know that we're
 6442	 * doing so, so that it can create the STA VLAN on its side
 6443	 */
 6444	if (ifmgd->use_4addr)
 6445		ieee80211_send_4addr_nullfunc(local, sdata);
 6446
 6447	/*
 6448	 * Start timer to probe the connection to the AP now.
 6449	 * Also start the timer that will detect beacon loss.
 6450	 */
 6451	ieee80211_sta_reset_beacon_monitor(sdata);
 6452	ieee80211_sta_reset_conn_monitor(sdata);
 6453
 6454	return true;
 6455out_err:
 6456	eth_zero_addr(sdata->vif.cfg.ap_addr);
 6457	return false;
 6458}
 6459
 6460static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
 6461					 struct ieee80211_mgmt *mgmt,
 6462					 size_t len)
 6463{
 6464	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 6465	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
 6466	u16 capab_info, status_code, aid;
 6467	struct ieee80211_elems_parse_params parse_params = {
 6468		.bss = NULL,
 6469		.link_id = -1,
 6470		.from_ap = true,
 6471		.type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE,
 6472	};
 6473	struct ieee802_11_elems *elems;
 6474	int ac;
 6475	const u8 *elem_start;
 6476	unsigned int elem_len;
 6477	bool reassoc;
 6478	struct ieee80211_event event = {
 6479		.type = MLME_EVENT,
 6480		.u.mlme.data = ASSOC_EVENT,
 6481	};
 6482	struct ieee80211_prep_tx_info info = {};
 6483	struct cfg80211_rx_assoc_resp_data resp = {
 6484		.uapsd_queues = -1,
 6485	};
 6486	u8 ap_mld_addr[ETH_ALEN] __aligned(2);
 6487	unsigned int link_id;
 6488	u16 max_aid = IEEE80211_MAX_AID;
 6489
 6490	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 6491
 6492	if (!assoc_data)
 6493		return;
 6494
 6495	info.link_id = assoc_data->assoc_link_id;
 6496
 6497	parse_params.mode =
 6498		assoc_data->link[assoc_data->assoc_link_id].conn.mode;
 6499
 6500	if (!ether_addr_equal(assoc_data->ap_addr, mgmt->bssid) ||
 6501	    !ether_addr_equal(assoc_data->ap_addr, mgmt->sa))
 6502		return;
 6503
 6504	/*
 6505	 * AssocResp and ReassocResp have identical structure, so process both
 6506	 * of them in this function.
 6507	 */
 6508
 6509	if (len < 24 + 6)
 6510		return;
 6511
 6512	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
 6513	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
 6514	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
 6515	if (assoc_data->s1g) {
 6516		elem_start = mgmt->u.s1g_assoc_resp.variable;
 6517		max_aid = IEEE80211_MAX_SUPPORTED_S1G_AID;
 6518	} else {
 6519		elem_start = mgmt->u.assoc_resp.variable;
 6520	}
 6521
 6522	/*
 6523	 * Note: this may not be perfect, AP might misbehave - if
 6524	 * anyone needs to rely on perfect complete notification
 6525	 * with the exact right subtype, then we need to track what
 6526	 * we actually transmitted.
 6527	 */
 6528	info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
 6529				 IEEE80211_STYPE_ASSOC_REQ;
 6530
 6531	if (assoc_data->fils_kek_len &&
 6532	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
 6533		return;
 6534
 6535	elem_len = len - (elem_start - (u8 *)mgmt);
 6536	parse_params.start = elem_start;
 6537	parse_params.len = elem_len;
 6538	elems = ieee802_11_parse_elems_full(&parse_params);
 6539	if (!elems)
 6540		goto notify_driver;
 6541
 6542	if (elems->aid_resp)
 6543		aid = le16_to_cpu(elems->aid_resp->aid);
 6544	else
 6545		aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
 6546
 6547	/*
 6548	 * The 5 MSB of the AID field are reserved for a non-S1G STA. For
 6549	 * an S1G STA the 3 MSBs are reserved.
 6550	 * (802.11-2016 9.4.1.8 AID field).
 6551	 */
 6552	aid &= assoc_data->s1g ? 0x1fff : 0x7ff;
 6553
 6554	sdata_info(sdata,
 6555		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
 6556		   reassoc ? "Rea" : "A", assoc_data->ap_addr,
 6557		   capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
 6558
 6559	ifmgd->broken_ap = false;
 6560
 6561	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
 6562	    elems->timeout_int &&
 6563	    elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
 6564		u32 tu, ms;
 6565
 6566		cfg80211_assoc_comeback(sdata->dev, assoc_data->ap_addr,
 6567					le32_to_cpu(elems->timeout_int->value));
 6568
 6569		tu = le32_to_cpu(elems->timeout_int->value);
 6570		ms = tu * 1024 / 1000;
 6571		sdata_info(sdata,
 6572			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
 6573			   assoc_data->ap_addr, tu, ms);
 6574		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
 6575		assoc_data->timeout_started = true;
 6576		assoc_data->comeback = true;
 6577		if (ms > IEEE80211_ASSOC_TIMEOUT)
 6578			run_again(sdata, assoc_data->timeout);
 6579		goto notify_driver;
 6580	}
 6581
 6582	if (status_code != WLAN_STATUS_SUCCESS) {
 6583		sdata_info(sdata, "%pM denied association (code=%d)\n",
 6584			   assoc_data->ap_addr, status_code);
 6585		event.u.mlme.status = MLME_DENIED;
 6586		event.u.mlme.reason = status_code;
 6587		drv_event_callback(sdata->local, sdata, &event);
 6588	} else {
 6589		if (aid == 0 || aid > max_aid) {
 6590			sdata_info(sdata,
 6591				   "invalid AID value %d (out of range), turn off PS\n",
 6592				   aid);
 6593			aid = 0;
 6594			ifmgd->broken_ap = true;
 6595		}
 6596
 6597		if (ieee80211_vif_is_mld(&sdata->vif)) {
 6598			struct ieee80211_mle_basic_common_info *common;
 6599
 6600			if (!elems->ml_basic) {
 6601				sdata_info(sdata,
 6602					   "MLO association with %pM but no (basic) multi-link element in response!\n",
 6603					   assoc_data->ap_addr);
 6604				goto abandon_assoc;
 6605			}
 6606
 6607			common = (void *)elems->ml_basic->variable;
 6608
 6609			if (memcmp(assoc_data->ap_addr,
 6610				   common->mld_mac_addr, ETH_ALEN)) {
 6611				sdata_info(sdata,
 6612					   "AP MLD MAC address mismatch: got %pM expected %pM\n",
 6613					   common->mld_mac_addr,
 6614					   assoc_data->ap_addr);
 6615				goto abandon_assoc;
 6616			}
 6617
 6618			sdata->vif.cfg.eml_cap =
 6619				ieee80211_mle_get_eml_cap((const void *)elems->ml_basic);
 6620			sdata->vif.cfg.eml_med_sync_delay =
 6621				ieee80211_mle_get_eml_med_sync_delay((const void *)elems->ml_basic);
 6622			sdata->vif.cfg.mld_capa_op =
 6623				ieee80211_mle_get_mld_capa_op((const void *)elems->ml_basic);
 6624		}
 6625
 6626		sdata->vif.cfg.aid = aid;
 6627		sdata->vif.cfg.s1g = assoc_data->s1g;
 6628
 6629		if (!ieee80211_assoc_success(sdata, mgmt, elems,
 6630					     elem_start, elem_len)) {
 6631			/* oops -- internal error -- send timeout for now */
 6632			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
 6633			goto notify_driver;
 6634		}
 6635		event.u.mlme.status = MLME_SUCCESS;
 6636		drv_event_callback(sdata->local, sdata, &event);
 6637		sdata_info(sdata, "associated\n");
 6638
 6639		info.success = 1;
 6640	}
 6641
 6642	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 6643		struct ieee80211_link_data *link;
 6644
 6645		if (!assoc_data->link[link_id].bss)
 6646			continue;
 6647
 6648		resp.links[link_id].bss = assoc_data->link[link_id].bss;
 6649		ether_addr_copy(resp.links[link_id].addr,
 6650				assoc_data->link[link_id].addr);
 6651		resp.links[link_id].status = assoc_data->link[link_id].status;
 6652
 6653		link = sdata_dereference(sdata->link[link_id], sdata);
 6654		if (!link)
 6655			continue;
 6656
 6657		/* get uapsd queues configuration - same for all links */
 6658		resp.uapsd_queues = 0;
 6659		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 6660			if (link->tx_conf[ac].uapsd)
 6661				resp.uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
 6662	}
 6663
 6664	if (ieee80211_vif_is_mld(&sdata->vif)) {
 6665		ether_addr_copy(ap_mld_addr, sdata->vif.cfg.ap_addr);
 6666		resp.ap_mld_addr = ap_mld_addr;
 6667	}
 6668
 6669	ieee80211_destroy_assoc_data(sdata,
 6670				     status_code == WLAN_STATUS_SUCCESS ?
 6671					ASSOC_SUCCESS :
 6672					ASSOC_REJECTED);
 6673
 6674	resp.buf = (u8 *)mgmt;
 6675	resp.len = len;
 6676	resp.req_ies = ifmgd->assoc_req_ies;
 6677	resp.req_ies_len = ifmgd->assoc_req_ies_len;
 6678	cfg80211_rx_assoc_resp(sdata->dev, &resp);
 6679notify_driver:
 6680	drv_mgd_complete_tx(sdata->local, sdata, &info);
 6681	kfree(elems);
 6682	return;
 6683abandon_assoc:
 6684	ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
 6685	goto notify_driver;
 6686}
 6687
 6688static void ieee80211_rx_bss_info(struct ieee80211_link_data *link,
 6689				  struct ieee80211_mgmt *mgmt, size_t len,
 6690				  struct ieee80211_rx_status *rx_status)
 6691{
 6692	struct ieee80211_sub_if_data *sdata = link->sdata;
 6693	struct ieee80211_local *local = sdata->local;
 6694	struct ieee80211_bss *bss;
 6695	struct ieee80211_channel *channel;
 6696
 6697	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 6698
 6699	channel = ieee80211_get_channel_khz(local->hw.wiphy,
 6700					ieee80211_rx_status_to_khz(rx_status));
 6701	if (!channel)
 6702		return;
 6703
 6704	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
 6705	if (bss) {
 6706		link->conf->beacon_rate = bss->beacon_rate;
 6707		ieee80211_rx_bss_put(local, bss);
 6708	}
 6709}
 6710
 6711
 6712static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_link_data *link,
 6713					 struct sk_buff *skb)
 6714{
 6715	struct ieee80211_sub_if_data *sdata = link->sdata;
 6716	struct ieee80211_mgmt *mgmt = (void *)skb->data;
 6717	struct ieee80211_if_managed *ifmgd;
 6718	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
 6719	struct ieee80211_channel *channel;
 6720	size_t baselen, len = skb->len;
 6721
 6722	ifmgd = &sdata->u.mgd;
 6723
 6724	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 6725
 6726	/*
 6727	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
 6728	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
 6729	 * a Probe Response frame [..], the Address 1 field of the Probe
 6730	 * Response frame shall be set to the broadcast address [..]"
 6731	 * So, on 6GHz band we should also accept broadcast responses.
 6732	 */
 6733	channel = ieee80211_get_channel_khz(sdata->local->hw.wiphy,
 6734					    ieee80211_rx_status_to_khz(rx_status));
 6735	if (!channel)
 6736		return;
 6737
 6738	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
 6739	    (channel->band != NL80211_BAND_6GHZ ||
 6740	     !is_broadcast_ether_addr(mgmt->da)))
 6741		return; /* ignore ProbeResp to foreign address */
 6742
 6743	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
 6744	if (baselen > len)
 6745		return;
 6746
 6747	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
 6748
 6749	if (ifmgd->associated &&
 6750	    ether_addr_equal(mgmt->bssid, link->u.mgd.bssid))
 6751		ieee80211_reset_ap_probe(sdata);
 6752}
 6753
 6754/*
 6755 * This is the canonical list of information elements we care about,
 6756 * the filter code also gives us all changes to the Microsoft OUI
 6757 * (00:50:F2) vendor IE which is used for WMM which we need to track,
 6758 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
 6759 * changes to requested client power.
 6760 *
 6761 * We implement beacon filtering in software since that means we can
 6762 * avoid processing the frame here and in cfg80211, and userspace
 6763 * will not be able to tell whether the hardware supports it or not.
 6764 *
 6765 * XXX: This list needs to be dynamic -- userspace needs to be able to
 6766 *	add items it requires. It also needs to be able to tell us to
 6767 *	look out for other vendor IEs.
 6768 */
 6769static const u64 care_about_ies =
 6770	(1ULL << WLAN_EID_COUNTRY) |
 6771	(1ULL << WLAN_EID_ERP_INFO) |
 6772	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
 6773	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
 6774	(1ULL << WLAN_EID_HT_CAPABILITY) |
 6775	(1ULL << WLAN_EID_HT_OPERATION) |
 6776	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
 6777
 6778static void ieee80211_handle_beacon_sig(struct ieee80211_link_data *link,
 6779					struct ieee80211_if_managed *ifmgd,
 6780					struct ieee80211_bss_conf *bss_conf,
 6781					struct ieee80211_local *local,
 6782					struct ieee80211_rx_status *rx_status)
 6783{
 6784	struct ieee80211_sub_if_data *sdata = link->sdata;
 6785
 6786	/* Track average RSSI from the Beacon frames of the current AP */
 6787
 6788	if (!link->u.mgd.tracking_signal_avg) {
 6789		link->u.mgd.tracking_signal_avg = true;
 6790		ewma_beacon_signal_init(&link->u.mgd.ave_beacon_signal);
 6791		link->u.mgd.last_cqm_event_signal = 0;
 6792		link->u.mgd.count_beacon_signal = 1;
 6793		link->u.mgd.last_ave_beacon_signal = 0;
 6794	} else {
 6795		link->u.mgd.count_beacon_signal++;
 6796	}
 6797
 6798	ewma_beacon_signal_add(&link->u.mgd.ave_beacon_signal,
 6799			       -rx_status->signal);
 6800
 6801	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
 6802	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
 6803		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
 6804		int last_sig = link->u.mgd.last_ave_beacon_signal;
 6805		struct ieee80211_event event = {
 6806			.type = RSSI_EVENT,
 6807		};
 6808
 6809		/*
 6810		 * if signal crosses either of the boundaries, invoke callback
 6811		 * with appropriate parameters
 6812		 */
 6813		if (sig > ifmgd->rssi_max_thold &&
 6814		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
 6815			link->u.mgd.last_ave_beacon_signal = sig;
 6816			event.u.rssi.data = RSSI_EVENT_HIGH;
 6817			drv_event_callback(local, sdata, &event);
 6818		} else if (sig < ifmgd->rssi_min_thold &&
 6819			   (last_sig >= ifmgd->rssi_max_thold ||
 6820			   last_sig == 0)) {
 6821			link->u.mgd.last_ave_beacon_signal = sig;
 6822			event.u.rssi.data = RSSI_EVENT_LOW;
 6823			drv_event_callback(local, sdata, &event);
 6824		}
 6825	}
 6826
 6827	if (bss_conf->cqm_rssi_thold &&
 6828	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
 6829	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
 6830		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
 6831		int last_event = link->u.mgd.last_cqm_event_signal;
 6832		int thold = bss_conf->cqm_rssi_thold;
 6833		int hyst = bss_conf->cqm_rssi_hyst;
 6834
 6835		if (sig < thold &&
 6836		    (last_event == 0 || sig < last_event - hyst)) {
 6837			link->u.mgd.last_cqm_event_signal = sig;
 6838			ieee80211_cqm_rssi_notify(
 6839				&sdata->vif,
 6840				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
 6841				sig, GFP_KERNEL);
 6842		} else if (sig > thold &&
 6843			   (last_event == 0 || sig > last_event + hyst)) {
 6844			link->u.mgd.last_cqm_event_signal = sig;
 6845			ieee80211_cqm_rssi_notify(
 6846				&sdata->vif,
 6847				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
 6848				sig, GFP_KERNEL);
 6849		}
 6850	}
 6851
 6852	if (bss_conf->cqm_rssi_low &&
 6853	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
 6854		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
 6855		int last_event = link->u.mgd.last_cqm_event_signal;
 6856		int low = bss_conf->cqm_rssi_low;
 6857		int high = bss_conf->cqm_rssi_high;
 6858
 6859		if (sig < low &&
 6860		    (last_event == 0 || last_event >= low)) {
 6861			link->u.mgd.last_cqm_event_signal = sig;
 6862			ieee80211_cqm_rssi_notify(
 6863				&sdata->vif,
 6864				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
 6865				sig, GFP_KERNEL);
 6866		} else if (sig > high &&
 6867			   (last_event == 0 || last_event <= high)) {
 6868			link->u.mgd.last_cqm_event_signal = sig;
 6869			ieee80211_cqm_rssi_notify(
 6870				&sdata->vif,
 6871				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
 6872				sig, GFP_KERNEL);
 6873		}
 6874	}
 6875}
 6876
 6877static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
 6878				    struct cfg80211_bss *bss)
 6879{
 6880	if (ether_addr_equal(tx_bssid, bss->bssid))
 6881		return true;
 6882	if (!bss->transmitted_bss)
 6883		return false;
 6884	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
 6885}
 6886
 6887static void ieee80211_ml_reconf_work(struct wiphy *wiphy,
 6888				     struct wiphy_work *work)
 6889{
 6890	struct ieee80211_sub_if_data *sdata =
 6891		container_of(work, struct ieee80211_sub_if_data,
 6892			     u.mgd.ml_reconf_work.work);
 6893	u16 new_valid_links, new_active_links, new_dormant_links;
 6894	int ret;
 6895
 6896	if (!sdata->u.mgd.removed_links)
 6897		return;
 6898
 6899	sdata_info(sdata,
 6900		   "MLO Reconfiguration: work: valid=0x%x, removed=0x%x\n",
 6901		   sdata->vif.valid_links, sdata->u.mgd.removed_links);
 6902
 6903	new_valid_links = sdata->vif.valid_links & ~sdata->u.mgd.removed_links;
 6904	if (new_valid_links == sdata->vif.valid_links)
 6905		return;
 6906
 6907	if (!new_valid_links ||
 6908	    !(new_valid_links & ~sdata->vif.dormant_links)) {
 6909		sdata_info(sdata, "No valid links after reconfiguration\n");
 6910		ret = -EINVAL;
 6911		goto out;
 6912	}
 6913
 6914	new_active_links = sdata->vif.active_links & ~sdata->u.mgd.removed_links;
 6915	if (new_active_links != sdata->vif.active_links) {
 6916		if (!new_active_links)
 6917			new_active_links =
 6918				BIT(ffs(new_valid_links &
 6919					~sdata->vif.dormant_links) - 1);
 6920
 6921		ret = ieee80211_set_active_links(&sdata->vif, new_active_links);
 6922		if (ret) {
 6923			sdata_info(sdata,
 6924				   "Failed setting active links\n");
 6925			goto out;
 6926		}
 6927	}
 6928
 6929	new_dormant_links = sdata->vif.dormant_links & ~sdata->u.mgd.removed_links;
 6930
 6931	ret = ieee80211_vif_set_links(sdata, new_valid_links,
 6932				      new_dormant_links);
 6933	if (ret)
 6934		sdata_info(sdata, "Failed setting valid links\n");
 6935
 6936	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
 6937
 6938out:
 6939	if (!ret)
 6940		cfg80211_links_removed(sdata->dev, sdata->u.mgd.removed_links);
 6941	else
 6942		__ieee80211_disconnect(sdata);
 6943
 6944	sdata->u.mgd.removed_links = 0;
 6945}
 6946
 6947static void ieee80211_ml_reconfiguration(struct ieee80211_sub_if_data *sdata,
 6948					 struct ieee802_11_elems *elems)
 6949{
 6950	const struct element *sub;
 6951	unsigned long removed_links = 0;
 6952	u16 link_removal_timeout[IEEE80211_MLD_MAX_NUM_LINKS] = {};
 6953	u8 link_id;
 6954	u32 delay;
 6955
 6956	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_reconf)
 6957		return;
 6958
 6959	/* Directly parse the sub elements as the common information doesn't
 6960	 * hold any useful information.
 6961	 */
 6962	for_each_mle_subelement(sub, (const u8 *)elems->ml_reconf,
 6963				elems->ml_reconf_len) {
 6964		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
 6965		u8 *pos = prof->variable;
 6966		u16 control;
 6967
 6968		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
 6969			continue;
 6970
 6971		if (!ieee80211_mle_reconf_sta_prof_size_ok(sub->data,
 6972							   sub->datalen))
 6973			return;
 6974
 6975		control = le16_to_cpu(prof->control);
 6976		link_id = control & IEEE80211_MLE_STA_RECONF_CONTROL_LINK_ID;
 6977
 6978		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
 6979			continue;
 6980
 6981		removed_links |= BIT(link_id);
 6982
 6983		/* the MAC address should not be included, but handle it */
 6984		if (control &
 6985		    IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT)
 6986			pos += 6;
 6987
 6988		/* According to Draft P802.11be_D3.0, the control should
 6989		 * include the AP Removal Timer present. If the AP Removal Timer
 6990		 * is not present assume immediate removal.
 6991		 */
 6992		if (control &
 6993		    IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT)
 6994			link_removal_timeout[link_id] = get_unaligned_le16(pos);
 6995	}
 6996
 6997	removed_links &= sdata->vif.valid_links;
 6998	if (!removed_links) {
 6999		/* In case the removal was cancelled, abort it */
 7000		if (sdata->u.mgd.removed_links) {
 7001			sdata->u.mgd.removed_links = 0;
 7002			wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
 7003						  &sdata->u.mgd.ml_reconf_work);
 7004		}
 7005		return;
 7006	}
 7007
 7008	delay = 0;
 7009	for_each_set_bit(link_id, &removed_links, IEEE80211_MLD_MAX_NUM_LINKS) {
 7010		struct ieee80211_bss_conf *link_conf =
 7011			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
 7012		u32 link_delay;
 7013
 7014		if (!link_conf) {
 7015			removed_links &= ~BIT(link_id);
 7016			continue;
 7017		}
 7018
 7019		if (link_removal_timeout[link_id] < 1)
 7020			link_delay = 0;
 7021		else
 7022			link_delay = link_conf->beacon_int *
 7023				(link_removal_timeout[link_id] - 1);
 7024
 7025		if (!delay)
 7026			delay = link_delay;
 7027		else
 7028			delay = min(delay, link_delay);
 7029	}
 7030
 7031	sdata->u.mgd.removed_links = removed_links;
 7032	wiphy_hrtimer_work_queue(sdata->local->hw.wiphy,
 7033				 &sdata->u.mgd.ml_reconf_work,
 7034				 us_to_ktime(ieee80211_tu_to_usec(delay)));
 7035}
 7036
 7037static int ieee80211_ttlm_set_links(struct ieee80211_sub_if_data *sdata,
 7038				    u16 active_links, u16 dormant_links,
 7039				    u16 suspended_links)
 7040{
 7041	u64 changed = 0;
 7042	int ret;
 7043
 7044	if (!active_links) {
 7045		ret = -EINVAL;
 7046		goto out;
 7047	}
 7048
 7049	/* If there is an active negotiated TTLM, it should be discarded by
 7050	 * the new negotiated/advertised TTLM.
 7051	 */
 7052	if (sdata->vif.neg_ttlm.valid) {
 7053		memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
 7054		sdata->vif.suspended_links = 0;
 7055		changed = BSS_CHANGED_MLD_TTLM;
 7056	}
 7057
 7058	if (sdata->vif.active_links != active_links) {
 7059		/* usable links are affected when active_links are changed,
 7060		 * so notify the driver about the status change
 7061		 */
 7062		changed |= BSS_CHANGED_MLD_VALID_LINKS;
 7063		active_links &= sdata->vif.active_links;
 7064		if (!active_links)
 7065			active_links =
 7066				BIT(__ffs(sdata->vif.valid_links &
 7067				    ~dormant_links));
 7068		ret = ieee80211_set_active_links(&sdata->vif, active_links);
 7069		if (ret) {
 7070			sdata_info(sdata, "Failed to set TTLM active links\n");
 7071			goto out;
 7072		}
 7073	}
 7074
 7075	ret = ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
 7076				      dormant_links);
 7077	if (ret) {
 7078		sdata_info(sdata, "Failed to set TTLM dormant links\n");
 7079		goto out;
 7080	}
 7081
 7082	sdata->vif.suspended_links = suspended_links;
 7083	if (sdata->vif.suspended_links)
 7084		changed |= BSS_CHANGED_MLD_TTLM;
 7085
 7086	ieee80211_vif_cfg_change_notify(sdata, changed);
 7087
 7088out:
 7089	if (ret)
 7090		ieee80211_disconnect(&sdata->vif, false);
 7091
 7092	return ret;
 7093}
 7094
 7095static void ieee80211_tid_to_link_map_work(struct wiphy *wiphy,
 7096					   struct wiphy_work *work)
 7097{
 7098	u16 new_active_links, new_dormant_links;
 7099	struct ieee80211_sub_if_data *sdata =
 7100		container_of(work, struct ieee80211_sub_if_data,
 7101			     u.mgd.ttlm_work.work);
 7102
 7103	new_active_links = sdata->u.mgd.ttlm_info.map &
 7104			   sdata->vif.valid_links;
 7105	new_dormant_links = ~sdata->u.mgd.ttlm_info.map &
 7106			    sdata->vif.valid_links;
 7107
 7108	ieee80211_vif_set_links(sdata, sdata->vif.valid_links, 0);
 7109	if (ieee80211_ttlm_set_links(sdata, new_active_links, new_dormant_links,
 7110				     0))
 7111		return;
 7112
 7113	sdata->u.mgd.ttlm_info.active = true;
 7114	sdata->u.mgd.ttlm_info.switch_time = 0;
 7115}
 7116
 7117static void ieee80211_process_adv_ttlm(struct ieee80211_sub_if_data *sdata,
 7118					  struct ieee802_11_elems *elems,
 7119					  u64 beacon_ts)
 7120{
 7121	u8 i;
 7122	int ret;
 7123
 7124	if (!ieee80211_vif_is_mld(&sdata->vif))
 7125		return;
 7126
 7127	if (!elems->ttlm_num) {
 7128		if (sdata->u.mgd.ttlm_info.switch_time) {
 7129			/* if a planned TID-to-link mapping was cancelled -
 7130			 * abort it
 7131			 */
 7132			wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
 7133						  &sdata->u.mgd.ttlm_work);
 7134		} else if (sdata->u.mgd.ttlm_info.active) {
 7135			/* if no TID-to-link element, set to default mapping in
 7136			 * which all TIDs are mapped to all setup links
 7137			 */
 7138			ret = ieee80211_vif_set_links(sdata,
 7139						      sdata->vif.valid_links,
 7140						      0);
 7141			if (ret) {
 7142				sdata_info(sdata, "Failed setting valid/dormant links\n");
 7143				return;
 7144			}
 7145			ieee80211_vif_cfg_change_notify(sdata,
 7146							BSS_CHANGED_MLD_VALID_LINKS);
 7147		}
 7148		memset(&sdata->u.mgd.ttlm_info, 0,
 7149		       sizeof(sdata->u.mgd.ttlm_info));
 7150		return;
 7151	}
 7152
 7153	for (i = 0; i < elems->ttlm_num; i++) {
 7154		struct ieee80211_adv_ttlm_info ttlm_info;
 7155		u32 res;
 7156
 7157		res = ieee80211_parse_adv_t2l(sdata, elems->ttlm[i],
 7158					      &ttlm_info);
 7159
 7160		if (res) {
 7161			__ieee80211_disconnect(sdata);
 7162			return;
 7163		}
 7164
 7165		if (ttlm_info.switch_time) {
 7166			u16 beacon_ts_tu, st_tu, delay;
 7167			u64 delay_usec;
 7168			u64 mask;
 7169
 7170			/* The t2l map switch time is indicated with a partial
 7171			 * TSF value (bits 10 to 25), get the partial beacon TS
 7172			 * as well, and calc the delay to the start time.
 7173			 */
 7174			mask = GENMASK_ULL(25, 10);
 7175			beacon_ts_tu = (beacon_ts & mask) >> 10;
 7176			st_tu = ttlm_info.switch_time;
 7177			delay = st_tu - beacon_ts_tu;
 7178
 7179			/*
 7180			 * If the switch time is far in the future, then it
 7181			 * could also be the previous switch still being
 7182			 * announced.
 7183			 * We can simply ignore it for now, if it is a future
 7184			 * switch the AP will continue to announce it anyway.
 7185			 */
 7186			if (delay > IEEE80211_ADV_TTLM_ST_UNDERFLOW)
 7187				return;
 7188
 7189			delay_usec = ieee80211_tu_to_usec(delay);
 7190
 7191			/* Link switching can take time, so schedule it
 7192			 * 100ms before to be ready on time
 7193			 */
 7194			if (delay_usec > IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS)
 7195				delay_usec -=
 7196					IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS;
 7197			else
 7198				delay_usec = 0;
 7199
 7200			sdata->u.mgd.ttlm_info = ttlm_info;
 7201			wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
 7202						  &sdata->u.mgd.ttlm_work);
 7203			wiphy_hrtimer_work_queue(sdata->local->hw.wiphy,
 7204						 &sdata->u.mgd.ttlm_work,
 7205						 us_to_ktime(delay_usec));
 7206			return;
 7207		}
 7208	}
 7209}
 7210
 7211static void
 7212ieee80211_mgd_check_cross_link_csa(struct ieee80211_sub_if_data *sdata,
 7213				   int reporting_link_id,
 7214				   struct ieee802_11_elems *elems)
 7215{
 7216	const struct element *sta_profiles[IEEE80211_MLD_MAX_NUM_LINKS] = {};
 7217	ssize_t sta_profiles_len[IEEE80211_MLD_MAX_NUM_LINKS] = {};
 7218	const struct element *sub;
 7219	const u8 *subelems;
 7220	size_t subelems_len;
 7221	u8 common_size;
 7222	int link_id;
 7223
 7224	if (!ieee80211_mle_size_ok((u8 *)elems->ml_basic, elems->ml_basic_len))
 7225		return;
 7226
 7227	common_size = ieee80211_mle_common_size((u8 *)elems->ml_basic);
 7228	subelems = (u8 *)elems->ml_basic + common_size;
 7229	subelems_len = elems->ml_basic_len - common_size;
 7230
 7231	for_each_element_id(sub, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
 7232			    subelems, subelems_len) {
 7233		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
 7234		struct ieee80211_link_data *link;
 7235		ssize_t len;
 7236
 7237		if (!ieee80211_mle_basic_sta_prof_size_ok(sub->data,
 7238							  sub->datalen))
 7239			continue;
 7240
 7241		link_id = le16_get_bits(prof->control,
 7242					IEEE80211_MLE_STA_CONTROL_LINK_ID);
 7243		/* need a valid link ID, but also not our own, both AP bugs */
 7244		if (link_id == reporting_link_id ||
 7245		    link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
 7246			continue;
 7247
 7248		link = sdata_dereference(sdata->link[link_id], sdata);
 7249		if (!link)
 7250			continue;
 7251
 7252		len = cfg80211_defragment_element(sub, subelems, subelems_len,
 7253						  NULL, 0,
 7254						  IEEE80211_MLE_SUBELEM_FRAGMENT);
 7255		if (WARN_ON(len < 0))
 7256			continue;
 7257
 7258		sta_profiles[link_id] = sub;
 7259		sta_profiles_len[link_id] = len;
 7260	}
 7261
 7262	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
 7263		struct ieee80211_mle_per_sta_profile *prof;
 7264		struct ieee802_11_elems *prof_elems;
 7265		struct ieee80211_link_data *link;
 7266		ssize_t len;
 7267
 7268		if (link_id == reporting_link_id)
 7269			continue;
 7270
 7271		link = sdata_dereference(sdata->link[link_id], sdata);
 7272		if (!link)
 7273			continue;
 7274
 7275		if (!sta_profiles[link_id]) {
 7276			prof_elems = NULL;
 7277			goto handle;
 7278		}
 7279
 7280		/* we can defragment in-place, won't use the buffer again */
 7281		len = cfg80211_defragment_element(sta_profiles[link_id],
 7282						  subelems, subelems_len,
 7283						  (void *)sta_profiles[link_id],
 7284						  sta_profiles_len[link_id],
 7285						  IEEE80211_MLE_SUBELEM_FRAGMENT);
 7286		if (WARN_ON(len != sta_profiles_len[link_id]))
 7287			continue;
 7288
 7289		prof = (void *)sta_profiles[link_id];
 7290		prof_elems = ieee802_11_parse_elems(prof->variable +
 7291						    (prof->sta_info_len - 1),
 7292						    len -
 7293						    (prof->sta_info_len - 1),
 7294						    IEEE80211_FTYPE_MGMT |
 7295						    IEEE80211_STYPE_BEACON,
 7296						    NULL);
 7297
 7298		/* memory allocation failed - let's hope that's transient */
 7299		if (!prof_elems)
 7300			continue;
 7301
 7302handle:
 7303		/*
 7304		 * FIXME: the timings here are obviously incorrect,
 7305		 * but only older Intel drivers seem to care, and
 7306		 * those don't have MLO. If you really need this,
 7307		 * the problem is having to calculate it with the
 7308		 * TSF offset etc. The device_timestamp is still
 7309		 * correct, of course.
 7310		 */
 7311		ieee80211_sta_process_chanswitch(link, 0, 0, elems, prof_elems,
 7312						 IEEE80211_CSA_SOURCE_OTHER_LINK);
 7313		kfree(prof_elems);
 7314	}
 7315}
 7316
 7317static bool ieee80211_mgd_ssid_mismatch(struct ieee80211_sub_if_data *sdata,
 7318					const struct ieee802_11_elems *elems)
 7319{
 7320	struct ieee80211_vif_cfg *cfg = &sdata->vif.cfg;
 7321	static u8 zero_ssid[IEEE80211_MAX_SSID_LEN];
 7322
 7323	if (!elems->ssid)
 7324		return false;
 7325
 7326	/* hidden SSID: zero length */
 7327	if (elems->ssid_len == 0)
 7328		return false;
 7329
 7330	if (elems->ssid_len != cfg->ssid_len)
 7331		return true;
 7332
 7333	/* hidden SSID: zeroed out */
 7334	if (!memcmp(elems->ssid, zero_ssid, elems->ssid_len))
 7335		return false;
 7336
 7337	return memcmp(elems->ssid, cfg->ssid, cfg->ssid_len);
 7338}
 7339
 7340static bool
 7341ieee80211_rx_beacon_freq_valid(struct ieee80211_local *local,
 7342			       struct ieee80211_mgmt *mgmt,
 7343			       struct ieee80211_rx_status *rx_status,
 7344			       struct ieee80211_chanctx_conf *chanctx)
 7345{
 7346	u32 pri_2mhz_khz;
 7347	struct ieee80211_channel *s1g_sibling_1mhz;
 7348	u32 pri_khz = ieee80211_channel_to_khz(chanctx->def.chan);
 7349	u32 rx_khz = ieee80211_rx_status_to_khz(rx_status);
 7350
 7351	if (rx_khz == pri_khz)
 7352		return true;
 7353
 7354	if (!chanctx->def.s1g_primary_2mhz)
 7355		return false;
 7356
 7357	/*
 7358	 * If we have an S1G interface with a 2MHz primary, beacons are
 7359	 * sent on the center frequency of the 2MHz primary. Find the sibling
 7360	 * 1MHz channel and calculate the 2MHz primary center frequency.
 7361	 */
 7362	s1g_sibling_1mhz = cfg80211_s1g_get_primary_sibling(local->hw.wiphy,
 7363							    &chanctx->def);
 7364	if (!s1g_sibling_1mhz)
 7365		return false;
 7366
 7367	pri_2mhz_khz =
 7368		(pri_khz + ieee80211_channel_to_khz(s1g_sibling_1mhz)) / 2;
 7369	return rx_khz == pri_2mhz_khz;
 7370}
 7371
 7372static void ieee80211_rx_mgmt_beacon(struct ieee80211_link_data *link,
 7373				     struct ieee80211_hdr *hdr, size_t len,
 7374				     struct ieee80211_rx_status *rx_status)
 7375{
 7376	struct ieee80211_sub_if_data *sdata = link->sdata;
 7377	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 7378	struct ieee80211_bss_conf *bss_conf = link->conf;
 7379	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
 7380	struct ieee80211_mgmt *mgmt = (void *) hdr;
 7381	struct ieee80211_ext *ext = NULL;
 7382	size_t baselen;
 7383	struct ieee802_11_elems *elems;
 7384	struct ieee80211_local *local = sdata->local;
 7385	struct ieee80211_chanctx_conf *chanctx_conf;
 7386	struct ieee80211_supported_band *sband;
 7387	struct ieee80211_channel *chan;
 7388	struct link_sta_info *link_sta;
 7389	struct sta_info *sta;
 7390	u64 changed = 0;
 7391	bool erp_valid;
 7392	u8 erp_value = 0;
 7393	u32 ncrc = 0;
 7394	u8 *bssid, *variable = mgmt->u.beacon.variable;
 7395	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
 7396	struct ieee80211_elems_parse_params parse_params = {
 7397		.mode = link->u.mgd.conn.mode,
 7398		.link_id = -1,
 7399		.from_ap = true,
 7400		.type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE,
 7401	};
 7402
 7403	lockdep_assert_wiphy(local->hw.wiphy);
 7404
 7405	/* Process beacon from the current BSS */
 7406	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
 7407	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
 7408		ext = (void *)mgmt;
 7409		variable = ext->u.s1g_beacon.variable +
 7410			   ieee80211_s1g_optional_len(ext->frame_control);
 7411	}
 7412
 7413	baselen = (u8 *) variable - (u8 *) mgmt;
 7414	if (baselen > len)
 7415		return;
 7416
 7417	parse_params.start = variable;
 7418	parse_params.len = len - baselen;
 7419
 7420	rcu_read_lock();
 7421	chanctx_conf = rcu_dereference(bss_conf->chanctx_conf);
 7422	if (!chanctx_conf) {
 7423		rcu_read_unlock();
 7424		return;
 7425	}
 7426
 7427	if (!ieee80211_rx_beacon_freq_valid(local, mgmt, rx_status,
 7428					    chanctx_conf)) {
 7429		rcu_read_unlock();
 7430		return;
 7431	}
 7432	chan = chanctx_conf->def.chan;
 7433	rcu_read_unlock();
 7434
 7435	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
 7436	    !WARN_ON(ieee80211_vif_is_mld(&sdata->vif)) &&
 7437	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->link[0].bss)) {
 7438		parse_params.bss = ifmgd->assoc_data->link[0].bss;
 7439		elems = ieee802_11_parse_elems_full(&parse_params);
 7440		if (!elems)
 7441			return;
 7442
 7443		ieee80211_rx_bss_info(link, mgmt, len, rx_status);
 7444
 7445		if (elems->dtim_period)
 7446			link->u.mgd.dtim_period = elems->dtim_period;
 7447		link->u.mgd.have_beacon = true;
 7448		ifmgd->assoc_data->need_beacon = false;
 7449		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
 7450		    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
 7451			bss_conf->sync_tsf =
 7452				le64_to_cpu(mgmt->u.beacon.timestamp);
 7453			bss_conf->sync_device_ts =
 7454				rx_status->device_timestamp;
 7455			bss_conf->sync_dtim_count = elems->dtim_count;
 7456		}
 7457
 7458		if (elems->mbssid_config_ie)
 7459			bss_conf->profile_periodicity =
 7460				elems->mbssid_config_ie->profile_periodicity;
 7461		else
 7462			bss_conf->profile_periodicity = 0;
 7463
 7464		if (elems->ext_capab_len >= 11 &&
 7465		    (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
 7466			bss_conf->ema_ap = true;
 7467		else
 7468			bss_conf->ema_ap = false;
 7469
 7470		/* continue assoc process */
 7471		ifmgd->assoc_data->timeout = jiffies;
 7472		ifmgd->assoc_data->timeout_started = true;
 7473		run_again(sdata, ifmgd->assoc_data->timeout);
 7474		kfree(elems);
 7475		return;
 7476	}
 7477
 7478	if (!ifmgd->associated ||
 7479	    !ieee80211_rx_our_beacon(bssid, bss_conf->bss))
 7480		return;
 7481	bssid = link->u.mgd.bssid;
 7482
 7483	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
 7484		ieee80211_handle_beacon_sig(link, ifmgd, bss_conf,
 7485					    local, rx_status);
 7486
 7487	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
 7488		mlme_dbg_ratelimited(sdata,
 7489				     "cancelling AP probe due to a received beacon\n");
 7490		ieee80211_reset_ap_probe(sdata);
 7491	}
 7492
 7493	/*
 7494	 * Push the beacon loss detection into the future since
 7495	 * we are processing a beacon from the AP just now.
 7496	 */
 7497	ieee80211_sta_reset_beacon_monitor(sdata);
 7498
 7499	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
 7500	 * element (which carries the beacon interval). Don't forget to add a
 7501	 * bit to care_about_ies[] above if mac80211 is interested in a
 7502	 * changing S1G element.
 7503	 */
 7504	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
 7505		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
 7506	parse_params.bss = bss_conf->bss;
 7507	parse_params.filter = care_about_ies;
 7508	parse_params.crc = ncrc;
 7509	elems = ieee802_11_parse_elems_full(&parse_params);
 7510	if (!elems)
 7511		return;
 7512
 7513	if (rx_status->flag & RX_FLAG_DECRYPTED &&
 7514	    ieee80211_mgd_ssid_mismatch(sdata, elems)) {
 7515		sdata_info(sdata, "SSID mismatch for AP %pM, disconnect\n",
 7516			   sdata->vif.cfg.ap_addr);
 7517		__ieee80211_disconnect(sdata);
 7518		return;
 7519	}
 7520
 7521	ncrc = elems->crc;
 7522
 7523	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
 7524	    ieee80211_check_tim(elems->tim, elems->tim_len, vif_cfg->aid,
 7525				vif_cfg->s1g)) {
 7526		if (local->hw.conf.dynamic_ps_timeout > 0) {
 7527			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
 7528				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
 7529				ieee80211_hw_config(local, -1,
 7530						    IEEE80211_CONF_CHANGE_PS);
 7531			}
 7532			ieee80211_send_nullfunc(local, sdata, false);
 7533		} else if (!local->pspolling && sdata->u.mgd.powersave) {
 7534			local->pspolling = true;
 7535
 7536			/*
 7537			 * Here is assumed that the driver will be
 7538			 * able to send ps-poll frame and receive a
 7539			 * response even though power save mode is
 7540			 * enabled, but some drivers might require
 7541			 * to disable power save here. This needs
 7542			 * to be investigated.
 7543			 */
 7544			ieee80211_send_pspoll(local, sdata);
 7545		}
 7546	}
 7547
 7548	if (sdata->vif.p2p ||
 7549	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
 7550		struct ieee80211_p2p_noa_attr noa = {};
 7551		int ret;
 7552
 7553		ret = cfg80211_get_p2p_attr(variable,
 7554					    len - baselen,
 7555					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
 7556					    (u8 *) &noa, sizeof(noa));
 7557		if (ret >= 2) {
 7558			if (link->u.mgd.p2p_noa_index != noa.index) {
 7559				/* valid noa_attr and index changed */
 7560				link->u.mgd.p2p_noa_index = noa.index;
 7561				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
 7562				changed |= BSS_CHANGED_P2P_PS;
 7563				/*
 7564				 * make sure we update all information, the CRC
 7565				 * mechanism doesn't look at P2P attributes.
 7566				 */
 7567				link->u.mgd.beacon_crc_valid = false;
 7568			}
 7569		} else if (link->u.mgd.p2p_noa_index != -1) {
 7570			/* noa_attr not found and we had valid noa_attr before */
 7571			link->u.mgd.p2p_noa_index = -1;
 7572			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
 7573			changed |= BSS_CHANGED_P2P_PS;
 7574			link->u.mgd.beacon_crc_valid = false;
 7575		}
 7576	}
 7577
 7578	/*
 7579	 * Update beacon timing and dtim count on every beacon appearance. This
 7580	 * will allow the driver to use the most updated values. Do it before
 7581	 * comparing this one with last received beacon.
 7582	 * IMPORTANT: These parameters would possibly be out of sync by the time
 7583	 * the driver will use them. The synchronized view is currently
 7584	 * guaranteed only in certain callbacks.
 7585	 */
 7586	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
 7587	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
 7588		bss_conf->sync_tsf =
 7589			le64_to_cpu(mgmt->u.beacon.timestamp);
 7590		bss_conf->sync_device_ts =
 7591			rx_status->device_timestamp;
 7592		bss_conf->sync_dtim_count = elems->dtim_count;
 7593	}
 7594
 7595	if ((ncrc == link->u.mgd.beacon_crc && link->u.mgd.beacon_crc_valid) ||
 7596	    (ext && ieee80211_is_s1g_short_beacon(ext->frame_control,
 7597						  parse_params.start,
 7598						  parse_params.len)))
 7599		goto free;
 7600	link->u.mgd.beacon_crc = ncrc;
 7601	link->u.mgd.beacon_crc_valid = true;
 7602
 7603	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
 7604
 7605	ieee80211_sta_process_chanswitch(link, rx_status->mactime,
 7606					 rx_status->device_timestamp,
 7607					 elems, elems,
 7608					 IEEE80211_CSA_SOURCE_BEACON);
 7609
 7610	/* note that after this elems->ml_basic can no longer be used fully */
 7611	ieee80211_mgd_check_cross_link_csa(sdata, rx_status->link_id, elems);
 7612
 7613	ieee80211_mgd_update_bss_param_ch_cnt(sdata, bss_conf, elems);
 7614
 7615	if (!sdata->u.mgd.epcs.enabled &&
 7616	    !link->u.mgd.disable_wmm_tracking &&
 7617	    ieee80211_sta_wmm_params(local, link, elems->wmm_param,
 7618				     elems->wmm_param_len,
 7619				     elems->mu_edca_param_set))
 7620		changed |= BSS_CHANGED_QOS;
 7621
 7622	/*
 7623	 * If we haven't had a beacon before, tell the driver about the
 7624	 * DTIM period (and beacon timing if desired) now.
 7625	 */
 7626	if (!link->u.mgd.have_beacon) {
 7627		/* a few bogus AP send dtim_period = 0 or no TIM IE */
 7628		bss_conf->dtim_period = elems->dtim_period ?: 1;
 7629
 7630		changed |= BSS_CHANGED_BEACON_INFO;
 7631		link->u.mgd.have_beacon = true;
 7632
 7633		ieee80211_recalc_ps(local);
 7634
 7635		ieee80211_recalc_ps_vif(sdata);
 7636	}
 7637
 7638	if (elems->erp_info) {
 7639		erp_valid = true;
 7640		erp_value = elems->erp_info[0];
 7641	} else {
 7642		erp_valid = false;
 7643	}
 7644
 7645	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
 7646		changed |= ieee80211_handle_bss_capability(link,
 7647				le16_to_cpu(mgmt->u.beacon.capab_info),
 7648				erp_valid, erp_value);
 7649
 7650	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
 7651	if (WARN_ON(!sta)) {
 7652		goto free;
 7653	}
 7654	link_sta = rcu_dereference_protected(sta->link[link->link_id],
 7655					     lockdep_is_held(&local->hw.wiphy->mtx));
 7656	if (WARN_ON(!link_sta)) {
 7657		goto free;
 7658	}
 7659
 7660	if (WARN_ON(!bss_conf->chanreq.oper.chan))
 7661		goto free;
 7662
 7663	sband = local->hw.wiphy->bands[bss_conf->chanreq.oper.chan->band];
 7664
 7665	changed |= ieee80211_recalc_twt_req(sdata, sband, link, link_sta, elems);
 7666
 7667	if (ieee80211_config_bw(link, elems, true, &changed,
 7668				IEEE80211_STYPE_BEACON)) {
 7669		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
 7670				       WLAN_REASON_DEAUTH_LEAVING,
 7671				       true, deauth_buf);
 7672		ieee80211_report_disconnect(sdata, deauth_buf,
 7673					    sizeof(deauth_buf), true,
 7674					    WLAN_REASON_DEAUTH_LEAVING,
 7675					    false);
 7676		goto free;
 7677	}
 7678
 7679	if (elems->opmode_notif)
 7680		ieee80211_vht_handle_opmode(sdata, link_sta,
 7681					    *elems->opmode_notif,
 7682					    rx_status->band);
 7683
 7684	changed |= ieee80211_handle_pwr_constr(link, chan, mgmt,
 7685					       elems->country_elem,
 7686					       elems->country_elem_len,
 7687					       elems->pwr_constr_elem,
 7688					       elems->cisco_dtpc_elem);
 7689
 7690	ieee80211_ml_reconfiguration(sdata, elems);
 7691	ieee80211_process_adv_ttlm(sdata, elems,
 7692				      le64_to_cpu(mgmt->u.beacon.timestamp));
 7693
 7694	ieee80211_link_info_change_notify(sdata, link, changed);
 7695free:
 7696	kfree(elems);
 7697}
 7698
 7699static void ieee80211_apply_neg_ttlm(struct ieee80211_sub_if_data *sdata,
 7700				     struct ieee80211_neg_ttlm neg_ttlm)
 7701{
 7702	u16 new_active_links, new_dormant_links, new_suspended_links, map = 0;
 7703	u8 i;
 7704
 7705	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++)
 7706		map |= neg_ttlm.downlink[i] | neg_ttlm.uplink[i];
 7707
 7708	/* If there is an active TTLM, unset previously suspended links */
 7709	if (sdata->vif.neg_ttlm.valid)
 7710		sdata->vif.dormant_links &= ~sdata->vif.suspended_links;
 7711
 7712	/* exclude links that are already disabled by advertised TTLM */
 7713	new_active_links =
 7714		map & sdata->vif.valid_links & ~sdata->vif.dormant_links;
 7715	new_suspended_links =
 7716		(~map & sdata->vif.valid_links) & ~sdata->vif.dormant_links;
 7717	new_dormant_links = sdata->vif.dormant_links | new_suspended_links;
 7718	if (ieee80211_ttlm_set_links(sdata, new_active_links,
 7719				     new_dormant_links, new_suspended_links))
 7720		return;
 7721
 7722	sdata->vif.neg_ttlm = neg_ttlm;
 7723	sdata->vif.neg_ttlm.valid = true;
 7724}
 7725
 7726static void ieee80211_neg_ttlm_timeout_work(struct wiphy *wiphy,
 7727					    struct wiphy_work *work)
 7728{
 7729	struct ieee80211_sub_if_data *sdata =
 7730		container_of(work, struct ieee80211_sub_if_data,
 7731			     u.mgd.neg_ttlm_timeout_work.work);
 7732
 7733	sdata_info(sdata,
 7734		   "No negotiated TTLM response from AP, disconnecting.\n");
 7735
 7736	__ieee80211_disconnect(sdata);
 7737}
 7738
 7739static void
 7740ieee80211_neg_ttlm_add_suggested_map(struct sk_buff *skb,
 7741				     struct ieee80211_neg_ttlm *neg_ttlm)
 7742{
 7743	u8 i, direction[IEEE80211_TTLM_MAX_CNT];
 7744
 7745	if (memcmp(neg_ttlm->downlink, neg_ttlm->uplink,
 7746		   sizeof(neg_ttlm->downlink))) {
 7747		direction[0] = IEEE80211_TTLM_DIRECTION_DOWN;
 7748		direction[1] = IEEE80211_TTLM_DIRECTION_UP;
 7749	} else {
 7750		direction[0] = IEEE80211_TTLM_DIRECTION_BOTH;
 7751	}
 7752
 7753	for (i = 0; i < ARRAY_SIZE(direction); i++) {
 7754		u8 tid, len, map_ind = 0, *len_pos, *map_ind_pos, *pos;
 7755		__le16 map;
 7756
 7757		len = sizeof(struct ieee80211_ttlm_elem) + 1 + 1;
 7758
 7759		pos = skb_put(skb, len + 2);
 7760		*pos++ = WLAN_EID_EXTENSION;
 7761		len_pos = pos++;
 7762		*pos++ = WLAN_EID_EXT_TID_TO_LINK_MAPPING;
 7763		*pos++ = direction[i];
 7764		map_ind_pos = pos++;
 7765		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
 7766			map = direction[i] == IEEE80211_TTLM_DIRECTION_UP ?
 7767				cpu_to_le16(neg_ttlm->uplink[tid]) :
 7768				cpu_to_le16(neg_ttlm->downlink[tid]);
 7769			if (!map)
 7770				continue;
 7771
 7772			len += 2;
 7773			map_ind |= BIT(tid);
 7774			skb_put_data(skb, &map, sizeof(map));
 7775		}
 7776
 7777		*map_ind_pos = map_ind;
 7778		*len_pos = len;
 7779
 7780		if (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH)
 7781			break;
 7782	}
 7783}
 7784
 7785static void
 7786ieee80211_send_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
 7787			    struct ieee80211_neg_ttlm *neg_ttlm,
 7788			    u8 dialog_token)
 7789{
 7790	struct ieee80211_local *local = sdata->local;
 7791	struct ieee80211_mgmt *mgmt;
 7792	struct sk_buff *skb;
 7793	int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.ttlm_req);
 7794	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
 7795		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
 7796
 7797	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
 7798	if (!skb)
 7799		return;
 7800
 7801	skb_reserve(skb, local->tx_headroom);
 7802	mgmt = skb_put_zero(skb, hdr_len);
 7803	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
 7804					  IEEE80211_STYPE_ACTION);
 7805	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
 7806	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
 7807	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
 7808
 7809	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
 7810	mgmt->u.action.u.ttlm_req.action_code =
 7811		WLAN_PROTECTED_EHT_ACTION_TTLM_REQ;
 7812	mgmt->u.action.u.ttlm_req.dialog_token = dialog_token;
 7813	ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
 7814	ieee80211_tx_skb(sdata, skb);
 7815}
 7816
 7817int ieee80211_req_neg_ttlm(struct ieee80211_sub_if_data *sdata,
 7818			   struct cfg80211_ttlm_params *params)
 7819{
 7820	struct ieee80211_neg_ttlm neg_ttlm = {};
 7821	u8 i;
 7822
 7823	if (!ieee80211_vif_is_mld(&sdata->vif) ||
 7824	    !(sdata->vif.cfg.mld_capa_op &
 7825	      IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP))
 7826		return -EINVAL;
 7827
 7828	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
 7829		if ((params->dlink[i] & ~sdata->vif.valid_links) ||
 7830		    (params->ulink[i] & ~sdata->vif.valid_links))
 7831			return -EINVAL;
 7832
 7833		neg_ttlm.downlink[i] = params->dlink[i];
 7834		neg_ttlm.uplink[i] = params->ulink[i];
 7835	}
 7836
 7837	if (drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm) !=
 7838	    NEG_TTLM_RES_ACCEPT)
 7839		return -EINVAL;
 7840
 7841	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
 7842	sdata->u.mgd.dialog_token_alloc++;
 7843	ieee80211_send_neg_ttlm_req(sdata, &sdata->vif.neg_ttlm,
 7844				    sdata->u.mgd.dialog_token_alloc);
 7845	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
 7846				  &sdata->u.mgd.neg_ttlm_timeout_work);
 7847	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
 7848				 &sdata->u.mgd.neg_ttlm_timeout_work,
 7849				 IEEE80211_NEG_TTLM_REQ_TIMEOUT);
 7850	return 0;
 7851}
 7852
 7853static void
 7854ieee80211_send_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
 7855			    enum ieee80211_neg_ttlm_res ttlm_res,
 7856			    u8 dialog_token,
 7857			    struct ieee80211_neg_ttlm *neg_ttlm)
 7858{
 7859	struct ieee80211_local *local = sdata->local;
 7860	struct ieee80211_mgmt *mgmt;
 7861	struct sk_buff *skb;
 7862	int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.ttlm_res);
 7863	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
 7864		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
 7865	u16 status_code;
 7866
 7867	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
 7868	if (!skb)
 7869		return;
 7870
 7871	skb_reserve(skb, local->tx_headroom);
 7872	mgmt = skb_put_zero(skb, hdr_len);
 7873	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
 7874					  IEEE80211_STYPE_ACTION);
 7875	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
 7876	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
 7877	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
 7878
 7879	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
 7880	mgmt->u.action.u.ttlm_res.action_code =
 7881		WLAN_PROTECTED_EHT_ACTION_TTLM_RES;
 7882	mgmt->u.action.u.ttlm_res.dialog_token = dialog_token;
 7883	switch (ttlm_res) {
 7884	default:
 7885		WARN_ON(1);
 7886		fallthrough;
 7887	case NEG_TTLM_RES_REJECT:
 7888		status_code = WLAN_STATUS_DENIED_TID_TO_LINK_MAPPING;
 7889		break;
 7890	case NEG_TTLM_RES_ACCEPT:
 7891		status_code = WLAN_STATUS_SUCCESS;
 7892		break;
 7893	case NEG_TTLM_RES_SUGGEST_PREFERRED:
 7894		status_code = WLAN_STATUS_PREF_TID_TO_LINK_MAPPING_SUGGESTED;
 7895		ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
 7896		break;
 7897	}
 7898
 7899	mgmt->u.action.u.ttlm_res.status_code = cpu_to_le16(status_code);
 7900	ieee80211_tx_skb(sdata, skb);
 7901}
 7902
 7903static int
 7904ieee80211_parse_neg_ttlm(struct ieee80211_sub_if_data *sdata,
 7905			 const struct ieee80211_ttlm_elem *ttlm,
 7906			 struct ieee80211_neg_ttlm *neg_ttlm,
 7907			 u8 *direction)
 7908{
 7909	u8 control, link_map_presence, map_size, tid;
 7910	u8 *pos;
 7911
 7912	/* The element size was already validated in
 7913	 * ieee80211_tid_to_link_map_size_ok()
 7914	 */
 7915	pos = (void *)ttlm->optional;
 7916
 7917	control = ttlm->control;
 7918
 7919	/* mapping switch time and expected duration fields are not expected
 7920	 * in case of negotiated TTLM
 7921	 */
 7922	if (control & (IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT |
 7923		       IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT)) {
 7924		mlme_dbg(sdata,
 7925			 "Invalid TTLM element in negotiated TTLM request\n");
 7926		return -EINVAL;
 7927	}
 7928
 7929	if (control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) {
 7930		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
 7931			neg_ttlm->downlink[tid] = sdata->vif.valid_links;
 7932			neg_ttlm->uplink[tid] = sdata->vif.valid_links;
 7933		}
 7934		*direction = IEEE80211_TTLM_DIRECTION_BOTH;
 7935		return 0;
 7936	}
 7937
 7938	*direction = u8_get_bits(control, IEEE80211_TTLM_CONTROL_DIRECTION);
 7939	if (*direction != IEEE80211_TTLM_DIRECTION_DOWN &&
 7940	    *direction != IEEE80211_TTLM_DIRECTION_UP &&
 7941	    *direction != IEEE80211_TTLM_DIRECTION_BOTH)
 7942		return -EINVAL;
 7943
 7944	link_map_presence = *pos;
 7945	pos++;
 7946
 7947	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
 7948		map_size = 1;
 7949	else
 7950		map_size = 2;
 7951
 7952	for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
 7953		u16 map;
 7954
 7955		if (link_map_presence & BIT(tid)) {
 7956			map = ieee80211_get_ttlm(map_size, pos);
 7957			if (!map) {
 7958				mlme_dbg(sdata,
 7959					 "No active links for TID %d", tid);
 7960				return -EINVAL;
 7961			}
 7962		} else {
 7963			map = 0;
 7964		}
 7965
 7966		switch (*direction) {
 7967		case IEEE80211_TTLM_DIRECTION_BOTH:
 7968			neg_ttlm->downlink[tid] = map;
 7969			neg_ttlm->uplink[tid] = map;
 7970			break;
 7971		case IEEE80211_TTLM_DIRECTION_DOWN:
 7972			neg_ttlm->downlink[tid] = map;
 7973			break;
 7974		case IEEE80211_TTLM_DIRECTION_UP:
 7975			neg_ttlm->uplink[tid] = map;
 7976			break;
 7977		default:
 7978			return -EINVAL;
 7979		}
 7980		pos += map_size;
 7981	}
 7982	return 0;
 7983}
 7984
 7985void ieee80211_process_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
 7986				    struct ieee80211_mgmt *mgmt, size_t len)
 7987{
 7988	u8 dialog_token, direction[IEEE80211_TTLM_MAX_CNT] = {}, i;
 7989	size_t ies_len;
 7990	enum ieee80211_neg_ttlm_res ttlm_res = NEG_TTLM_RES_ACCEPT;
 7991	struct ieee802_11_elems *elems = NULL;
 7992	struct ieee80211_neg_ttlm neg_ttlm = {};
 7993
 7994	BUILD_BUG_ON(ARRAY_SIZE(direction) != ARRAY_SIZE(elems->ttlm));
 7995
 7996	if (!ieee80211_vif_is_mld(&sdata->vif))
 7997		return;
 7998
 7999	dialog_token = mgmt->u.action.u.ttlm_req.dialog_token;
 8000	ies_len  = len - offsetof(struct ieee80211_mgmt,
 8001				  u.action.u.ttlm_req.variable);
 8002	elems = ieee802_11_parse_elems(mgmt->u.action.u.ttlm_req.variable,
 8003				       ies_len,
 8004				       IEEE80211_FTYPE_MGMT |
 8005				       IEEE80211_STYPE_ACTION,
 8006				       NULL);
 8007	if (!elems) {
 8008		ttlm_res = NEG_TTLM_RES_REJECT;
 8009		goto out;
 8010	}
 8011
 8012	for (i = 0; i < elems->ttlm_num; i++) {
 8013		if (ieee80211_parse_neg_ttlm(sdata, elems->ttlm[i],
 8014					     &neg_ttlm, &direction[i]) ||
 8015		    (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH &&
 8016		     elems->ttlm_num != 1)) {
 8017			ttlm_res = NEG_TTLM_RES_REJECT;
 8018			goto out;
 8019		}
 8020	}
 8021
 8022	if (!elems->ttlm_num ||
 8023	    (elems->ttlm_num == 2 && direction[0] == direction[1])) {
 8024		ttlm_res = NEG_TTLM_RES_REJECT;
 8025		goto out;
 8026	}
 8027
 8028	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
 8029		if ((neg_ttlm.downlink[i] &&
 8030		     (neg_ttlm.downlink[i] & ~sdata->vif.valid_links)) ||
 8031		    (neg_ttlm.uplink[i] &&
 8032		     (neg_ttlm.uplink[i] & ~sdata->vif.valid_links))) {
 8033			ttlm_res = NEG_TTLM_RES_REJECT;
 8034			goto out;
 8035		}
 8036	}
 8037
 8038	ttlm_res = drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm);
 8039
 8040	if (ttlm_res != NEG_TTLM_RES_ACCEPT)
 8041		goto out;
 8042
 8043	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
 8044out:
 8045	kfree(elems);
 8046	ieee80211_send_neg_ttlm_res(sdata, ttlm_res, dialog_token, &neg_ttlm);
 8047}
 8048
 8049void ieee80211_process_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
 8050				    struct ieee80211_mgmt *mgmt, size_t len)
 8051{
 8052	if (!ieee80211_vif_is_mld(&sdata->vif) ||
 8053	    mgmt->u.action.u.ttlm_req.dialog_token !=
 8054	    sdata->u.mgd.dialog_token_alloc)
 8055		return;
 8056
 8057	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
 8058				  &sdata->u.mgd.neg_ttlm_timeout_work);
 8059
 8060	/* MLD station sends a TID to link mapping request, mainly to handle
 8061	 * BTM (BSS transition management) request, in which case it needs to
 8062	 * restrict the active links set.
 8063	 * In this case it's not expected that the MLD AP will reject the
 8064	 * negotiated TTLM request.
 8065	 * This can be better implemented in the future, to handle request
 8066	 * rejections.
 8067	 */
 8068	if (le16_to_cpu(mgmt->u.action.u.ttlm_res.status_code) != WLAN_STATUS_SUCCESS)
 8069		__ieee80211_disconnect(sdata);
 8070}
 8071
 8072void ieee80211_process_ttlm_teardown(struct ieee80211_sub_if_data *sdata)
 8073{
 8074	u16 new_dormant_links;
 8075
 8076	if (!sdata->vif.neg_ttlm.valid)
 8077		return;
 8078
 8079	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
 8080	new_dormant_links =
 8081		sdata->vif.dormant_links & ~sdata->vif.suspended_links;
 8082	sdata->vif.suspended_links = 0;
 8083	ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
 8084				new_dormant_links);
 8085	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_TTLM |
 8086					       BSS_CHANGED_MLD_VALID_LINKS);
 8087}
 8088
 8089static void ieee80211_teardown_ttlm_work(struct wiphy *wiphy,
 8090					 struct wiphy_work *work)
 8091{
 8092	struct ieee80211_sub_if_data *sdata =
 8093		container_of(work, struct ieee80211_sub_if_data,
 8094			     u.mgd.teardown_ttlm_work);
 8095
 8096	ieee80211_process_ttlm_teardown(sdata);
 8097}
 8098
 8099void ieee80211_send_teardown_neg_ttlm(struct ieee80211_vif *vif)
 8100{
 8101	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 8102	struct ieee80211_local *local = sdata->local;
 8103	struct ieee80211_mgmt *mgmt;
 8104	struct sk_buff *skb;
 8105	int frame_len = offsetofend(struct ieee80211_mgmt,
 8106				  u.action.u.ttlm_tear_down);
 8107	struct ieee80211_tx_info *info;
 8108
 8109	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
 8110	if (!skb)
 8111		return;
 8112
 8113	skb_reserve(skb, local->hw.extra_tx_headroom);
 8114	mgmt = skb_put_zero(skb, frame_len);
 8115	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
 8116					  IEEE80211_STYPE_ACTION);
 8117	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
 8118	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
 8119	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
 8120
 8121	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
 8122	mgmt->u.action.u.ttlm_tear_down.action_code =
 8123		WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN;
 8124
 8125	info = IEEE80211_SKB_CB(skb);
 8126	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
 8127	info->status_data = IEEE80211_STATUS_TYPE_NEG_TTLM;
 8128	ieee80211_tx_skb(sdata, skb);
 8129}
 8130EXPORT_SYMBOL(ieee80211_send_teardown_neg_ttlm);
 8131
 8132void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
 8133				 struct sk_buff *skb)
 8134{
 8135	struct ieee80211_link_data *link = &sdata->deflink;
 8136	struct ieee80211_rx_status *rx_status;
 8137	struct ieee80211_hdr *hdr;
 8138	u16 fc;
 8139
 8140	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8141
 8142	rx_status = (struct ieee80211_rx_status *) skb->cb;
 8143	hdr = (struct ieee80211_hdr *) skb->data;
 8144	fc = le16_to_cpu(hdr->frame_control);
 8145
 8146	switch (fc & IEEE80211_FCTL_STYPE) {
 8147	case IEEE80211_STYPE_S1G_BEACON:
 8148		ieee80211_rx_mgmt_beacon(link, hdr, skb->len, rx_status);
 8149		break;
 8150	}
 8151}
 8152
 8153void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
 8154				  struct sk_buff *skb)
 8155{
 8156	struct ieee80211_link_data *link = &sdata->deflink;
 8157	struct ieee80211_rx_status *rx_status;
 8158	struct ieee802_11_elems *elems;
 8159	struct ieee80211_mgmt *mgmt;
 8160	u16 fc;
 8161	int ies_len;
 8162
 8163	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8164
 8165	rx_status = (struct ieee80211_rx_status *) skb->cb;
 8166	mgmt = (struct ieee80211_mgmt *) skb->data;
 8167	fc = le16_to_cpu(mgmt->frame_control);
 8168
 8169	if (rx_status->link_valid) {
 8170		link = sdata_dereference(sdata->link[rx_status->link_id],
 8171					 sdata);
 8172		if (!link)
 8173			return;
 8174	}
 8175
 8176	switch (fc & IEEE80211_FCTL_STYPE) {
 8177	case IEEE80211_STYPE_BEACON:
 8178		ieee80211_rx_mgmt_beacon(link, (void *)mgmt,
 8179					 skb->len, rx_status);
 8180		break;
 8181	case IEEE80211_STYPE_PROBE_RESP:
 8182		ieee80211_rx_mgmt_probe_resp(link, skb);
 8183		break;
 8184	case IEEE80211_STYPE_AUTH:
 8185		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
 8186		break;
 8187	case IEEE80211_STYPE_DEAUTH:
 8188		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
 8189		break;
 8190	case IEEE80211_STYPE_DISASSOC:
 8191		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
 8192		break;
 8193	case IEEE80211_STYPE_ASSOC_RESP:
 8194	case IEEE80211_STYPE_REASSOC_RESP:
 8195		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
 8196		break;
 8197	case IEEE80211_STYPE_ACTION:
 8198		if (!sdata->u.mgd.associated ||
 8199		    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
 8200			break;
 8201
 8202		switch (mgmt->u.action.category) {
 8203		case WLAN_CATEGORY_SPECTRUM_MGMT:
 8204			ies_len = skb->len -
 8205				  offsetof(struct ieee80211_mgmt,
 8206					   u.action.u.chan_switch.variable);
 8207
 8208			if (ies_len < 0)
 8209				break;
 8210
 8211			/* CSA IE cannot be overridden, no need for BSSID */
 8212			elems = ieee802_11_parse_elems(mgmt->u.action.u.chan_switch.variable,
 8213						       ies_len,
 8214						       IEEE80211_FTYPE_MGMT |
 8215						       IEEE80211_STYPE_ACTION,
 8216						       NULL);
 8217
 8218			if (elems && !elems->parse_error) {
 8219				enum ieee80211_csa_source src =
 8220					IEEE80211_CSA_SOURCE_PROT_ACTION;
 8221
 8222				ieee80211_sta_process_chanswitch(link,
 8223								 rx_status->mactime,
 8224								 rx_status->device_timestamp,
 8225								 elems, elems,
 8226								 src);
 8227			}
 8228			kfree(elems);
 8229			break;
 8230		case WLAN_CATEGORY_PUBLIC:
 8231		case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
 8232			ies_len = skb->len -
 8233				  offsetof(struct ieee80211_mgmt,
 8234					   u.action.u.ext_chan_switch.variable);
 8235
 8236			if (ies_len < 0)
 8237				break;
 8238
 8239			/*
 8240			 * extended CSA IE can't be overridden, no need for
 8241			 * BSSID
 8242			 */
 8243			elems = ieee802_11_parse_elems(mgmt->u.action.u.ext_chan_switch.variable,
 8244						       ies_len,
 8245						       IEEE80211_FTYPE_MGMT |
 8246						       IEEE80211_STYPE_ACTION,
 8247						       NULL);
 8248
 8249			if (elems && !elems->parse_error) {
 8250				enum ieee80211_csa_source src;
 8251
 8252				if (mgmt->u.action.category ==
 8253						WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
 8254					src = IEEE80211_CSA_SOURCE_PROT_ACTION;
 8255				else
 8256					src = IEEE80211_CSA_SOURCE_UNPROT_ACTION;
 8257
 8258				/* for the handling code pretend it was an IE */
 8259				elems->ext_chansw_ie =
 8260					&mgmt->u.action.u.ext_chan_switch.data;
 8261
 8262				ieee80211_sta_process_chanswitch(link,
 8263								 rx_status->mactime,
 8264								 rx_status->device_timestamp,
 8265								 elems, elems,
 8266								 src);
 8267			}
 8268
 8269			kfree(elems);
 8270			break;
 8271		}
 8272		break;
 8273	}
 8274}
 8275
 8276static void ieee80211_sta_timer(struct timer_list *t)
 8277{
 8278	struct ieee80211_sub_if_data *sdata =
 8279		timer_container_of(sdata, t, u.mgd.timer);
 8280
 8281	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
 8282}
 8283
 8284void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
 8285				   u8 reason, bool tx)
 8286{
 8287	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
 8288
 8289	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
 8290			       tx, frame_buf);
 8291
 8292	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
 8293				    reason, false);
 8294}
 8295
 8296static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
 8297{
 8298	struct ieee80211_local *local = sdata->local;
 8299	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8300	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
 8301	u32 tx_flags = 0;
 8302	u16 trans = 1;
 8303	u16 status = 0;
 8304	struct ieee80211_prep_tx_info info = {
 8305		.subtype = IEEE80211_STYPE_AUTH,
 8306	};
 8307
 8308	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8309
 8310	if (WARN_ON_ONCE(!auth_data))
 8311		return -EINVAL;
 8312
 8313	auth_data->tries++;
 8314
 8315	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
 8316		sdata_info(sdata, "authentication with %pM timed out\n",
 8317			   auth_data->ap_addr);
 8318
 8319		/*
 8320		 * Most likely AP is not in the range so remove the
 8321		 * bss struct for that AP.
 8322		 */
 8323		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
 8324
 8325		return -ETIMEDOUT;
 8326	}
 8327
 8328	if (auth_data->algorithm == WLAN_AUTH_SAE)
 8329		info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
 8330
 8331	info.link_id = auth_data->link_id;
 8332	drv_mgd_prepare_tx(local, sdata, &info);
 8333
 8334	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
 8335		   auth_data->ap_addr, auth_data->tries,
 8336		   IEEE80211_AUTH_MAX_TRIES);
 8337
 8338	auth_data->expected_transaction = 2;
 8339
 8340	if (auth_data->algorithm == WLAN_AUTH_SAE) {
 8341		trans = auth_data->sae_trans;
 8342		status = auth_data->sae_status;
 8343		auth_data->expected_transaction = trans;
 8344	}
 8345
 8346	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
 8347		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
 8348			   IEEE80211_TX_INTFL_MLME_CONN_TX;
 8349
 8350	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
 8351			    auth_data->data, auth_data->data_len,
 8352			    auth_data->ap_addr, auth_data->ap_addr,
 8353			    NULL, 0, 0, tx_flags);
 8354
 8355	if (tx_flags == 0) {
 8356		if (auth_data->algorithm == WLAN_AUTH_SAE)
 8357			auth_data->timeout = jiffies +
 8358				IEEE80211_AUTH_TIMEOUT_SAE;
 8359		else
 8360			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
 8361	} else {
 8362		auth_data->timeout =
 8363			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
 8364	}
 8365
 8366	auth_data->timeout_started = true;
 8367	run_again(sdata, auth_data->timeout);
 8368
 8369	return 0;
 8370}
 8371
 8372static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
 8373{
 8374	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
 8375	struct ieee80211_local *local = sdata->local;
 8376	int ret;
 8377
 8378	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8379
 8380	assoc_data->tries++;
 8381	assoc_data->comeback = false;
 8382	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
 8383		sdata_info(sdata, "association with %pM timed out\n",
 8384			   assoc_data->ap_addr);
 8385
 8386		/*
 8387		 * Most likely AP is not in the range so remove the
 8388		 * bss struct for that AP.
 8389		 */
 8390		cfg80211_unlink_bss(local->hw.wiphy,
 8391				    assoc_data->link[assoc_data->assoc_link_id].bss);
 8392
 8393		return -ETIMEDOUT;
 8394	}
 8395
 8396	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
 8397		   assoc_data->ap_addr, assoc_data->tries,
 8398		   IEEE80211_ASSOC_MAX_TRIES);
 8399	ret = ieee80211_send_assoc(sdata);
 8400	if (ret)
 8401		return ret;
 8402
 8403	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
 8404		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
 8405		assoc_data->timeout_started = true;
 8406		run_again(sdata, assoc_data->timeout);
 8407	} else {
 8408		assoc_data->timeout =
 8409			round_jiffies_up(jiffies +
 8410					 IEEE80211_ASSOC_TIMEOUT_LONG);
 8411		assoc_data->timeout_started = true;
 8412		run_again(sdata, assoc_data->timeout);
 8413	}
 8414
 8415	return 0;
 8416}
 8417
 8418void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
 8419				  __le16 fc, bool acked)
 8420{
 8421	struct ieee80211_local *local = sdata->local;
 8422
 8423	sdata->u.mgd.status_fc = fc;
 8424	sdata->u.mgd.status_acked = acked;
 8425	sdata->u.mgd.status_received = true;
 8426
 8427	wiphy_work_queue(local->hw.wiphy, &sdata->work);
 8428}
 8429
 8430void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
 8431{
 8432	struct ieee80211_local *local = sdata->local;
 8433	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8434
 8435	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8436
 8437	if (ifmgd->status_received) {
 8438		__le16 fc = ifmgd->status_fc;
 8439		bool status_acked = ifmgd->status_acked;
 8440
 8441		ifmgd->status_received = false;
 8442		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
 8443			if (status_acked) {
 8444				if (ifmgd->auth_data->algorithm ==
 8445				    WLAN_AUTH_SAE)
 8446					ifmgd->auth_data->timeout =
 8447						jiffies +
 8448						IEEE80211_AUTH_TIMEOUT_SAE;
 8449				else
 8450					ifmgd->auth_data->timeout =
 8451						jiffies +
 8452						IEEE80211_AUTH_TIMEOUT_SHORT;
 8453				run_again(sdata, ifmgd->auth_data->timeout);
 8454			} else {
 8455				ifmgd->auth_data->timeout = jiffies - 1;
 8456			}
 8457			ifmgd->auth_data->timeout_started = true;
 8458		} else if (ifmgd->assoc_data &&
 8459			   !ifmgd->assoc_data->comeback &&
 8460			   (ieee80211_is_assoc_req(fc) ||
 8461			    ieee80211_is_reassoc_req(fc))) {
 8462			/*
 8463			 * Update association timeout based on the TX status
 8464			 * for the (Re)Association Request frame. Skip this if
 8465			 * we have already processed a (Re)Association Response
 8466			 * frame that indicated need for association comeback
 8467			 * at a specific time in the future. This could happen
 8468			 * if the TX status information is delayed enough for
 8469			 * the response to be received and processed first.
 8470			 */
 8471			if (status_acked) {
 8472				ifmgd->assoc_data->timeout =
 8473					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
 8474				run_again(sdata, ifmgd->assoc_data->timeout);
 8475			} else {
 8476				ifmgd->assoc_data->timeout = jiffies - 1;
 8477			}
 8478			ifmgd->assoc_data->timeout_started = true;
 8479		}
 8480	}
 8481
 8482	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
 8483	    time_after(jiffies, ifmgd->auth_data->timeout)) {
 8484		if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
 8485			/*
 8486			 * ok ... we waited for assoc or continuation but
 8487			 * userspace didn't do it, so kill the auth data
 8488			 */
 8489			ieee80211_destroy_auth_data(sdata, false);
 8490		} else if (ieee80211_auth(sdata)) {
 8491			u8 ap_addr[ETH_ALEN];
 8492			struct ieee80211_event event = {
 8493				.type = MLME_EVENT,
 8494				.u.mlme.data = AUTH_EVENT,
 8495				.u.mlme.status = MLME_TIMEOUT,
 8496			};
 8497
 8498			memcpy(ap_addr, ifmgd->auth_data->ap_addr, ETH_ALEN);
 8499
 8500			ieee80211_destroy_auth_data(sdata, false);
 8501
 8502			cfg80211_auth_timeout(sdata->dev, ap_addr);
 8503			drv_event_callback(sdata->local, sdata, &event);
 8504		}
 8505	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
 8506		run_again(sdata, ifmgd->auth_data->timeout);
 8507
 8508	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
 8509	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
 8510		if ((ifmgd->assoc_data->need_beacon &&
 8511		     !sdata->deflink.u.mgd.have_beacon) ||
 8512		    ieee80211_do_assoc(sdata)) {
 8513			struct ieee80211_event event = {
 8514				.type = MLME_EVENT,
 8515				.u.mlme.data = ASSOC_EVENT,
 8516				.u.mlme.status = MLME_TIMEOUT,
 8517			};
 8518
 8519			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
 8520			drv_event_callback(sdata->local, sdata, &event);
 8521		}
 8522	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
 8523		run_again(sdata, ifmgd->assoc_data->timeout);
 8524
 8525	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
 8526	    ifmgd->associated) {
 8527		u8 *bssid = sdata->deflink.u.mgd.bssid;
 8528		int max_tries;
 8529
 8530		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
 8531			max_tries = max_nullfunc_tries;
 8532		else
 8533			max_tries = max_probe_tries;
 8534
 8535		/* ACK received for nullfunc probing frame */
 8536		if (!ifmgd->probe_send_count)
 8537			ieee80211_reset_ap_probe(sdata);
 8538		else if (ifmgd->nullfunc_failed) {
 8539			if (ifmgd->probe_send_count < max_tries) {
 8540				mlme_dbg(sdata,
 8541					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
 8542					 bssid, ifmgd->probe_send_count,
 8543					 max_tries);
 8544				ieee80211_mgd_probe_ap_send(sdata);
 8545			} else {
 8546				mlme_dbg(sdata,
 8547					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
 8548					 bssid);
 8549				ieee80211_sta_connection_lost(sdata,
 8550					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
 8551					false);
 8552			}
 8553		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
 8554			run_again(sdata, ifmgd->probe_timeout);
 8555		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
 8556			mlme_dbg(sdata,
 8557				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
 8558				 bssid, probe_wait_ms);
 8559			ieee80211_sta_connection_lost(sdata,
 8560				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
 8561		} else if (ifmgd->probe_send_count < max_tries) {
 8562			mlme_dbg(sdata,
 8563				 "No probe response from AP %pM after %dms, try %d/%i\n",
 8564				 bssid, probe_wait_ms,
 8565				 ifmgd->probe_send_count, max_tries);
 8566			ieee80211_mgd_probe_ap_send(sdata);
 8567		} else {
 8568			/*
 8569			 * We actually lost the connection ... or did we?
 8570			 * Let's make sure!
 8571			 */
 8572			mlme_dbg(sdata,
 8573				 "No probe response from AP %pM after %dms, disconnecting.\n",
 8574				 bssid, probe_wait_ms);
 8575
 8576			ieee80211_sta_connection_lost(sdata,
 8577				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
 8578		}
 8579	}
 8580}
 8581
 8582static bool
 8583ieee80211_is_csa_in_progress(struct ieee80211_sub_if_data *sdata)
 8584{
 8585	/*
 8586	 * In MLO, check the CSA flags 'active' and 'waiting_bcn' for all
 8587	 * the links.
 8588	 */
 8589	struct ieee80211_link_data *link;
 8590
 8591	guard(rcu)();
 8592
 8593	for_each_link_data_rcu(sdata, link) {
 8594		if (!(link->conf->csa_active &&
 8595		      !link->u.mgd.csa.waiting_bcn))
 8596			return false;
 8597	}
 8598
 8599	return true;
 8600}
 8601
 8602static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
 8603{
 8604	struct ieee80211_sub_if_data *sdata =
 8605		timer_container_of(sdata, t, u.mgd.bcn_mon_timer);
 8606
 8607	if (ieee80211_is_csa_in_progress(sdata))
 8608		return;
 8609
 8610	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
 8611		return;
 8612
 8613	sdata->u.mgd.connection_loss = false;
 8614	wiphy_work_queue(sdata->local->hw.wiphy,
 8615			 &sdata->u.mgd.beacon_connection_loss_work);
 8616}
 8617
 8618static unsigned long
 8619ieee80211_latest_active_link_conn_timeout(struct ieee80211_sub_if_data *sdata)
 8620{
 8621	unsigned long latest_timeout = jiffies;
 8622	unsigned int link_id;
 8623	struct sta_info *sta;
 8624
 8625	guard(rcu)();
 8626
 8627	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
 8628	if (!sta)
 8629		return 0;
 8630
 8631	for (link_id = 0; link_id < ARRAY_SIZE(sta->link);
 8632	     link_id++) {
 8633		struct link_sta_info *link_sta;
 8634		unsigned long timeout;
 8635
 8636		link_sta = rcu_dereference(sta->link[link_id]);
 8637		if (!link_sta)
 8638			continue;
 8639
 8640		timeout = link_sta->status_stats.last_ack;
 8641		if (time_before(timeout, link_sta->rx_stats.last_rx))
 8642			timeout = link_sta->rx_stats.last_rx;
 8643
 8644		timeout += IEEE80211_CONNECTION_IDLE_TIME;
 8645
 8646		/*
 8647		 * latest_timeout holds the timeout of the link
 8648		 * that will expire last among all links in an
 8649		 * non-AP MLD STA. This ensures that the connection
 8650		 * monitor timer is only reset if at least one link
 8651		 * is still active, and it is scheduled to fire at
 8652		 * the latest possible timeout.
 8653		 */
 8654		if (time_after(timeout, latest_timeout))
 8655			latest_timeout = timeout;
 8656	}
 8657
 8658	return latest_timeout;
 8659}
 8660
 8661static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
 8662{
 8663	struct ieee80211_sub_if_data *sdata =
 8664		timer_container_of(sdata, t, u.mgd.conn_mon_timer);
 8665	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8666	struct ieee80211_local *local = sdata->local;
 8667	unsigned long latest_timeout;
 8668
 8669	if (ieee80211_is_csa_in_progress(sdata))
 8670		return;
 8671
 8672	latest_timeout = ieee80211_latest_active_link_conn_timeout(sdata);
 8673
 8674	/*
 8675	 * If latest timeout is after now, then update timer to fire at
 8676	 * the later date, but do not actually probe at this time.
 8677	 */
 8678	if (time_is_after_jiffies(latest_timeout)) {
 8679		mod_timer(&ifmgd->conn_mon_timer,
 8680			  round_jiffies_up(latest_timeout));
 8681		return;
 8682	}
 8683
 8684	wiphy_work_queue(local->hw.wiphy, &sdata->u.mgd.monitor_work);
 8685}
 8686
 8687static void ieee80211_sta_monitor_work(struct wiphy *wiphy,
 8688				       struct wiphy_work *work)
 8689{
 8690	struct ieee80211_sub_if_data *sdata =
 8691		container_of(work, struct ieee80211_sub_if_data,
 8692			     u.mgd.monitor_work);
 8693
 8694	ieee80211_mgd_probe_ap(sdata, false);
 8695}
 8696
 8697static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
 8698{
 8699	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
 8700		__ieee80211_stop_poll(sdata);
 8701
 8702		/* let's probe the connection once */
 8703		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
 8704			wiphy_work_queue(sdata->local->hw.wiphy,
 8705					 &sdata->u.mgd.monitor_work);
 8706	}
 8707}
 8708
 8709#ifdef CONFIG_PM
 8710void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
 8711{
 8712	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8713	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
 8714
 8715	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8716
 8717	if (ifmgd->auth_data || ifmgd->assoc_data) {
 8718		const u8 *ap_addr = ifmgd->auth_data ?
 8719				ifmgd->auth_data->ap_addr :
 8720				ifmgd->assoc_data->ap_addr;
 8721
 8722		/*
 8723		 * If we are trying to authenticate / associate while suspending,
 8724		 * cfg80211 won't know and won't actually abort those attempts,
 8725		 * thus we need to do that ourselves.
 8726		 */
 8727		ieee80211_send_deauth_disassoc(sdata, ap_addr, ap_addr,
 8728					       IEEE80211_STYPE_DEAUTH,
 8729					       WLAN_REASON_DEAUTH_LEAVING,
 8730					       false, frame_buf);
 8731		if (ifmgd->assoc_data)
 8732			ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
 8733		if (ifmgd->auth_data)
 8734			ieee80211_destroy_auth_data(sdata, false);
 8735		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
 8736				      IEEE80211_DEAUTH_FRAME_LEN,
 8737				      false);
 8738	}
 8739
 8740	/* This is a bit of a hack - we should find a better and more generic
 8741	 * solution to this. Normally when suspending, cfg80211 will in fact
 8742	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
 8743	 * auth (not so important) or assoc (this is the problem) process.
 8744	 *
 8745	 * As a consequence, it can happen that we are in the process of both
 8746	 * associating and suspending, and receive an association response
 8747	 * after cfg80211 has checked if it needs to disconnect, but before
 8748	 * we actually set the flag to drop incoming frames. This will then
 8749	 * cause the workqueue flush to process the association response in
 8750	 * the suspend, resulting in a successful association just before it
 8751	 * tries to remove the interface from the driver, which now though
 8752	 * has a channel context assigned ... this results in issues.
 8753	 *
 8754	 * To work around this (for now) simply deauth here again if we're
 8755	 * now connected.
 8756	 */
 8757	if (ifmgd->associated && !sdata->local->wowlan) {
 8758		u8 bssid[ETH_ALEN];
 8759		struct cfg80211_deauth_request req = {
 8760			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
 8761			.bssid = bssid,
 8762		};
 8763
 8764		memcpy(bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
 8765		ieee80211_mgd_deauth(sdata, &req);
 8766	}
 8767}
 8768#endif
 8769
 8770void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
 8771{
 8772	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8773
 8774	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 8775
 8776	if (!ifmgd->associated)
 8777		return;
 8778
 8779	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
 8780		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
 8781		mlme_dbg(sdata, "driver requested disconnect after resume\n");
 8782		ieee80211_sta_connection_lost(sdata,
 8783					      WLAN_REASON_UNSPECIFIED,
 8784					      true);
 8785		return;
 8786	}
 8787
 8788	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_HW_RESTART) {
 8789		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_HW_RESTART;
 8790		mlme_dbg(sdata, "driver requested disconnect after hardware restart\n");
 8791		ieee80211_sta_connection_lost(sdata,
 8792					      WLAN_REASON_UNSPECIFIED,
 8793					      true);
 8794		return;
 8795	}
 8796}
 8797
 8798static void ieee80211_request_smps_mgd_work(struct wiphy *wiphy,
 8799					    struct wiphy_work *work)
 8800{
 8801	struct ieee80211_link_data *link =
 8802		container_of(work, struct ieee80211_link_data,
 8803			     u.mgd.request_smps_work);
 8804
 8805	__ieee80211_request_smps_mgd(link->sdata, link,
 8806				     link->u.mgd.driver_smps_mode);
 8807}
 8808
 8809static void ieee80211_ml_sta_reconf_timeout(struct wiphy *wiphy,
 8810					    struct wiphy_work *work)
 8811{
 8812	struct ieee80211_sub_if_data *sdata =
 8813		container_of(work, struct ieee80211_sub_if_data,
 8814			     u.mgd.reconf.wk.work);
 8815
 8816	if (!sdata->u.mgd.reconf.added_links &&
 8817	    !sdata->u.mgd.reconf.removed_links)
 8818		return;
 8819
 8820	sdata_info(sdata,
 8821		   "mlo: reconf: timeout: added=0x%x, removed=0x%x\n",
 8822		   sdata->u.mgd.reconf.added_links,
 8823		   sdata->u.mgd.reconf.removed_links);
 8824
 8825	__ieee80211_disconnect(sdata);
 8826}
 8827
 8828/* interface setup */
 8829void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
 8830{
 8831	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8832
 8833	wiphy_work_init(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
 8834	wiphy_work_init(&ifmgd->beacon_connection_loss_work,
 8835			ieee80211_beacon_connection_loss_work);
 8836	wiphy_work_init(&ifmgd->csa_connection_drop_work,
 8837			ieee80211_csa_connection_drop_work);
 8838	wiphy_delayed_work_init(&ifmgd->tdls_peer_del_work,
 8839				ieee80211_tdls_peer_del_work);
 8840	wiphy_hrtimer_work_init(&ifmgd->ml_reconf_work,
 8841				ieee80211_ml_reconf_work);
 8842	wiphy_delayed_work_init(&ifmgd->reconf.wk,
 8843				ieee80211_ml_sta_reconf_timeout);
 8844	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
 8845	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
 8846	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
 8847	wiphy_delayed_work_init(&ifmgd->tx_tspec_wk,
 8848				ieee80211_sta_handle_tspec_ac_params_wk);
 8849	wiphy_hrtimer_work_init(&ifmgd->ttlm_work,
 8850				ieee80211_tid_to_link_map_work);
 8851	wiphy_delayed_work_init(&ifmgd->neg_ttlm_timeout_work,
 8852				ieee80211_neg_ttlm_timeout_work);
 8853	wiphy_work_init(&ifmgd->teardown_ttlm_work,
 8854			ieee80211_teardown_ttlm_work);
 8855
 8856	ifmgd->flags = 0;
 8857	ifmgd->powersave = sdata->wdev.ps;
 8858	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
 8859	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
 8860	/* Setup TDLS data */
 8861	spin_lock_init(&ifmgd->teardown_lock);
 8862	ifmgd->teardown_skb = NULL;
 8863	ifmgd->orig_teardown_skb = NULL;
 8864	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
 8865}
 8866
 8867static void ieee80211_recalc_smps_work(struct wiphy *wiphy,
 8868				       struct wiphy_work *work)
 8869{
 8870	struct ieee80211_link_data *link =
 8871		container_of(work, struct ieee80211_link_data,
 8872			     u.mgd.recalc_smps);
 8873
 8874	ieee80211_recalc_smps(link->sdata, link);
 8875}
 8876
 8877void ieee80211_mgd_setup_link(struct ieee80211_link_data *link)
 8878{
 8879	struct ieee80211_sub_if_data *sdata = link->sdata;
 8880	struct ieee80211_local *local = sdata->local;
 8881	unsigned int link_id = link->link_id;
 8882
 8883	link->u.mgd.p2p_noa_index = -1;
 8884	link->conf->bssid = link->u.mgd.bssid;
 8885	link->smps_mode = IEEE80211_SMPS_OFF;
 8886
 8887	wiphy_work_init(&link->u.mgd.request_smps_work,
 8888			ieee80211_request_smps_mgd_work);
 8889	wiphy_work_init(&link->u.mgd.recalc_smps,
 8890			ieee80211_recalc_smps_work);
 8891	if (local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
 8892		link->u.mgd.req_smps = IEEE80211_SMPS_AUTOMATIC;
 8893	else
 8894		link->u.mgd.req_smps = IEEE80211_SMPS_OFF;
 8895
 8896	wiphy_hrtimer_work_init(&link->u.mgd.csa.switch_work,
 8897				ieee80211_csa_switch_work);
 8898
 8899	ieee80211_clear_tpe(&link->conf->tpe);
 8900
 8901	if (sdata->u.mgd.assoc_data)
 8902		ether_addr_copy(link->conf->addr,
 8903				sdata->u.mgd.assoc_data->link[link_id].addr);
 8904	else if (sdata->u.mgd.reconf.add_links_data)
 8905		ether_addr_copy(link->conf->addr,
 8906				sdata->u.mgd.reconf.add_links_data->link[link_id].addr);
 8907	else if (!is_valid_ether_addr(link->conf->addr))
 8908		eth_random_addr(link->conf->addr);
 8909}
 8910
 8911/* scan finished notification */
 8912void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
 8913{
 8914	struct ieee80211_sub_if_data *sdata;
 8915
 8916	/* Restart STA timers */
 8917	rcu_read_lock();
 8918	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
 8919		if (ieee80211_sdata_running(sdata))
 8920			ieee80211_restart_sta_timer(sdata);
 8921	}
 8922	rcu_read_unlock();
 8923}
 8924
 8925static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
 8926				     struct cfg80211_bss *cbss, s8 link_id,
 8927				     const u8 *ap_mld_addr, bool assoc,
 8928				     struct ieee80211_conn_settings *conn,
 8929				     bool override,
 8930				     unsigned long *userspace_selectors)
 8931{
 8932	struct ieee80211_local *local = sdata->local;
 8933	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 8934	struct ieee80211_bss *bss = (void *)cbss->priv;
 8935	struct sta_info *new_sta = NULL;
 8936	struct ieee80211_link_data *link;
 8937	bool have_sta = false;
 8938	bool mlo;
 8939	int err;
 8940	u16 new_links;
 8941
 8942	if (link_id >= 0) {
 8943		mlo = true;
 8944		if (WARN_ON(!ap_mld_addr))
 8945			return -EINVAL;
 8946		new_links = BIT(link_id);
 8947	} else {
 8948		if (WARN_ON(ap_mld_addr))
 8949			return -EINVAL;
 8950		ap_mld_addr = cbss->bssid;
 8951		new_links = 0;
 8952		link_id = 0;
 8953		mlo = false;
 8954	}
 8955
 8956	if (assoc) {
 8957		rcu_read_lock();
 8958		have_sta = sta_info_get(sdata, ap_mld_addr);
 8959		rcu_read_unlock();
 8960	}
 8961
 8962	if (mlo && !have_sta &&
 8963	    WARN_ON(sdata->vif.valid_links || sdata->vif.active_links))
 8964		return -EINVAL;
 8965
 8966	err = ieee80211_vif_set_links(sdata, new_links, 0);
 8967	if (err)
 8968		return err;
 8969
 8970	link = sdata_dereference(sdata->link[link_id], sdata);
 8971	if (WARN_ON(!link)) {
 8972		err = -ENOLINK;
 8973		goto out_err;
 8974	}
 8975
 8976	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data)) {
 8977		err = -EINVAL;
 8978		goto out_err;
 8979	}
 8980
 8981	/* If a reconfig is happening, bail out */
 8982	if (local->in_reconfig) {
 8983		err = -EBUSY;
 8984		goto out_err;
 8985	}
 8986
 8987	if (!have_sta) {
 8988		if (mlo)
 8989			new_sta = sta_info_alloc_with_link(sdata, ap_mld_addr,
 8990							   link_id, cbss->bssid,
 8991							   GFP_KERNEL);
 8992		else
 8993			new_sta = sta_info_alloc(sdata, ap_mld_addr, GFP_KERNEL);
 8994
 8995		if (!new_sta) {
 8996			err = -ENOMEM;
 8997			goto out_err;
 8998		}
 8999
 9000		new_sta->sta.mlo = mlo;
 9001	}
 9002
 9003	/*
 9004	 * Set up the information for the new channel before setting the
 9005	 * new channel. We can't - completely race-free - change the basic
 9006	 * rates bitmap and the channel (sband) that it refers to, but if
 9007	 * we set it up before we at least avoid calling into the driver's
 9008	 * bss_info_changed() method with invalid information (since we do
 9009	 * call that from changing the channel - only for IDLE and perhaps
 9010	 * some others, but ...).
 9011	 *
 9012	 * So to avoid that, just set up all the new information before the
 9013	 * channel, but tell the driver to apply it only afterwards, since
 9014	 * it might need the new channel for that.
 9015	 */
 9016	if (new_sta) {
 9017		const struct cfg80211_bss_ies *ies;
 9018		struct link_sta_info *link_sta;
 9019
 9020		rcu_read_lock();
 9021		link_sta = rcu_dereference(new_sta->link[link_id]);
 9022		if (WARN_ON(!link_sta)) {
 9023			rcu_read_unlock();
 9024			sta_info_free(local, new_sta);
 9025			err = -EINVAL;
 9026			goto out_err;
 9027		}
 9028
 9029		err = ieee80211_mgd_setup_link_sta(link, new_sta,
 9030						   link_sta, cbss);
 9031		if (err) {
 9032			rcu_read_unlock();
 9033			sta_info_free(local, new_sta);
 9034			goto out_err;
 9035		}
 9036
 9037		memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
 9038
 9039		/* set timing information */
 9040		link->conf->beacon_int = cbss->beacon_interval;
 9041		ies = rcu_dereference(cbss->beacon_ies);
 9042		if (ies) {
 9043			link->conf->sync_tsf = ies->tsf;
 9044			link->conf->sync_device_ts =
 9045				bss->device_ts_beacon;
 9046
 9047			ieee80211_get_dtim(ies,
 9048					   &link->conf->sync_dtim_count,
 9049					   NULL);
 9050		} else if (!ieee80211_hw_check(&sdata->local->hw,
 9051					       TIMING_BEACON_ONLY)) {
 9052			ies = rcu_dereference(cbss->proberesp_ies);
 9053			/* must be non-NULL since beacon IEs were NULL */
 9054			link->conf->sync_tsf = ies->tsf;
 9055			link->conf->sync_device_ts =
 9056				bss->device_ts_presp;
 9057			link->conf->sync_dtim_count = 0;
 9058		} else {
 9059			link->conf->sync_tsf = 0;
 9060			link->conf->sync_device_ts = 0;
 9061			link->conf->sync_dtim_count = 0;
 9062		}
 9063		rcu_read_unlock();
 9064	}
 9065
 9066	if (new_sta || override) {
 9067		/*
 9068		 * Only set this if we're also going to calculate the AP
 9069		 * settings etc., otherwise this was set before in a
 9070		 * previous call. Note override is set to %true in assoc
 9071		 * if the settings were changed.
 9072		 */
 9073		link->u.mgd.conn = *conn;
 9074		err = ieee80211_prep_channel(sdata, link, link->link_id, cbss,
 9075					     mlo, &link->u.mgd.conn,
 9076					     userspace_selectors);
 9077		if (err) {
 9078			if (new_sta)
 9079				sta_info_free(local, new_sta);
 9080			goto out_err;
 9081		}
 9082		/* pass out for use in assoc */
 9083		*conn = link->u.mgd.conn;
 9084	}
 9085
 9086	if (new_sta) {
 9087		/*
 9088		 * tell driver about BSSID, basic rates and timing
 9089		 * this was set up above, before setting the channel
 9090		 */
 9091		ieee80211_link_info_change_notify(sdata, link,
 9092						  BSS_CHANGED_BSSID |
 9093						  BSS_CHANGED_BASIC_RATES |
 9094						  BSS_CHANGED_BEACON_INT);
 9095
 9096		if (assoc)
 9097			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
 9098
 9099		err = sta_info_insert(new_sta);
 9100		new_sta = NULL;
 9101		if (err) {
 9102			sdata_info(sdata,
 9103				   "failed to insert STA entry for the AP (error %d)\n",
 9104				   err);
 9105			goto out_release_chan;
 9106		}
 9107	} else
 9108		WARN_ON_ONCE(!ether_addr_equal(link->u.mgd.bssid, cbss->bssid));
 9109
 9110	/* Cancel scan to ensure that nothing interferes with connection */
 9111	if (local->scanning)
 9112		ieee80211_scan_cancel(local);
 9113
 9114	return 0;
 9115
 9116out_release_chan:
 9117	ieee80211_link_release_channel(link);
 9118out_err:
 9119	ieee80211_vif_set_links(sdata, 0, 0);
 9120	return err;
 9121}
 9122
 9123static bool ieee80211_mgd_csa_present(struct ieee80211_sub_if_data *sdata,
 9124				      const struct cfg80211_bss_ies *ies,
 9125				      u8 cur_channel, bool ignore_ecsa)
 9126{
 9127	const struct element *csa_elem, *ecsa_elem;
 9128	struct ieee80211_channel_sw_ie *csa = NULL;
 9129	struct ieee80211_ext_chansw_ie *ecsa = NULL;
 9130
 9131	if (!ies)
 9132		return false;
 9133
 9134	csa_elem = cfg80211_find_elem(WLAN_EID_CHANNEL_SWITCH,
 9135				      ies->data, ies->len);
 9136	if (csa_elem && csa_elem->datalen == sizeof(*csa))
 9137		csa = (void *)csa_elem->data;
 9138
 9139	ecsa_elem = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
 9140				       ies->data, ies->len);
 9141	if (ecsa_elem && ecsa_elem->datalen == sizeof(*ecsa))
 9142		ecsa = (void *)ecsa_elem->data;
 9143
 9144	if (csa && csa->count == 0)
 9145		csa = NULL;
 9146	if (csa && !csa->mode && csa->new_ch_num == cur_channel)
 9147		csa = NULL;
 9148
 9149	if (ecsa && ecsa->count == 0)
 9150		ecsa = NULL;
 9151	if (ecsa && !ecsa->mode && ecsa->new_ch_num == cur_channel)
 9152		ecsa = NULL;
 9153
 9154	if (ignore_ecsa && ecsa) {
 9155		sdata_info(sdata,
 9156			   "Ignoring ECSA in probe response - was considered stuck!\n");
 9157		return csa;
 9158	}
 9159
 9160	return csa || ecsa;
 9161}
 9162
 9163static bool ieee80211_mgd_csa_in_process(struct ieee80211_sub_if_data *sdata,
 9164					 struct cfg80211_bss *bss)
 9165{
 9166	u8 cur_channel;
 9167	bool ret;
 9168
 9169	cur_channel = ieee80211_frequency_to_channel(bss->channel->center_freq);
 9170
 9171	rcu_read_lock();
 9172	if (ieee80211_mgd_csa_present(sdata,
 9173				      rcu_dereference(bss->beacon_ies),
 9174				      cur_channel, false)) {
 9175		ret = true;
 9176		goto out;
 9177	}
 9178
 9179	if (ieee80211_mgd_csa_present(sdata,
 9180				      rcu_dereference(bss->proberesp_ies),
 9181				      cur_channel, bss->proberesp_ecsa_stuck)) {
 9182		ret = true;
 9183		goto out;
 9184	}
 9185
 9186	ret = false;
 9187out:
 9188	rcu_read_unlock();
 9189	return ret;
 9190}
 9191
 9192static void ieee80211_parse_cfg_selectors(unsigned long *userspace_selectors,
 9193					  const u8 *supported_selectors,
 9194					  u8 supported_selectors_len)
 9195{
 9196	if (supported_selectors) {
 9197		for (int i = 0; i < supported_selectors_len; i++) {
 9198			set_bit(supported_selectors[i],
 9199				userspace_selectors);
 9200		}
 9201	} else {
 9202		/* Assume SAE_H2E support for backward compatibility. */
 9203		set_bit(BSS_MEMBERSHIP_SELECTOR_SAE_H2E,
 9204			userspace_selectors);
 9205	}
 9206}
 9207
 9208/* config hooks */
 9209int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
 9210		       struct cfg80211_auth_request *req)
 9211{
 9212	struct ieee80211_local *local = sdata->local;
 9213	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 9214	struct ieee80211_mgd_auth_data *auth_data;
 9215	struct ieee80211_conn_settings conn;
 9216	struct ieee80211_link_data *link;
 9217	struct ieee80211_supported_band *sband;
 9218	struct ieee80211_bss *bss;
 9219	u16 auth_alg;
 9220	int err;
 9221	bool cont_auth, wmm_used;
 9222
 9223	lockdep_assert_wiphy(sdata->local->hw.wiphy);
 9224
 9225	/* prepare auth data structure */
 9226
 9227	switch (req->auth_type) {
 9228	case NL80211_AUTHTYPE_OPEN_SYSTEM:
 9229		auth_alg = WLAN_AUTH_OPEN;
 9230		break;
 9231	case NL80211_AUTHTYPE_SHARED_KEY:
 9232		if (fips_enabled)
 9233			return -EOPNOTSUPP;
 9234		auth_alg = WLAN_AUTH_SHARED_KEY;
 9235		break;
 9236	case NL80211_AUTHTYPE_FT:
 9237		auth_alg = WLAN_AUTH_FT;
 9238		break;
 9239	case NL80211_AUTHTYPE_NETWORK_EAP:
 9240		auth_alg = WLAN_AUTH_LEAP;
 9241		break;
 9242	case NL80211_AUTHTYPE_SAE:
 9243		auth_alg = WLAN_AUTH_SAE;
 9244		break;
 9245	case NL80211_AUTHTYPE_FILS_SK:
 9246		auth_alg = WLAN_AUTH_FILS_SK;
 9247		break;
 9248	case NL80211_AUTHTYPE_FILS_SK_PFS:
 9249		auth_alg = WLAN_AUTH_FILS_SK_PFS;
 9250		break;
 9251	case NL80211_AUTHTYPE_FILS_PK:
 9252		auth_alg = WLAN_AUTH_FILS_PK;
 9253		break;
 9254	default:
 9255		return -EOPNOTSUPP;
 9256	}
 9257
 9258	if (ifmgd->assoc_data)
 9259		return -EBUSY;
 9260
 9261	if (ieee80211_mgd_csa_in_process(sdata, req->bss)) {
 9262		sdata_info(sdata, "AP is in CSA process, reject auth\n");
 9263		return -EINVAL;
 9264	}
 9265
 9266	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
 9267			    req->ie_len, GFP_KERNEL);
 9268	if (!auth_data)
 9269		return -ENOMEM;
 9270
 9271	memcpy(auth_data->ap_addr,
 9272	       req->ap_mld_addr ?: req->bss->bssid,
 9273	       ETH_ALEN);
 9274	auth_data->bss = req->bss;
 9275	auth_data->link_id = req->link_id;
 9276
 9277	if (req->auth_data_len >= 4) {
 9278		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
 9279			__le16 *pos = (__le16 *) req->auth_data;
 9280
 9281			auth_data->sae_trans = le16_to_cpu(pos[0]);
 9282			auth_data->sae_status = le16_to_cpu(pos[1]);
 9283		}
 9284		memcpy(auth_data->data, req->auth_data + 4,
 9285		       req->auth_data_len - 4);
 9286		auth_data->data_len += req->auth_data_len - 4;
 9287	}
 9288
 9289	/* Check if continuing authentication or trying to authenticate with the
 9290	 * same BSS that we were in the process of authenticating with and avoid
 9291	 * removal and re-addition of the STA entry in
 9292	 * ieee80211_prep_connection().
 9293	 */
 9294	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss &&
 9295		    ifmgd->auth_data->link_id == req->link_id;
 9296
 9297	if (req->ie && req->ie_len) {
 9298		memcpy(&auth_data->data[auth_data->data_len],
 9299		       req->ie, req->ie_len);
 9300		auth_data->data_len += req->ie_len;
 9301	}
 9302
 9303	if (req->key && req->key_len) {
 9304		auth_data->key_len = req->key_len;
 9305		auth_data->key_idx = req->key_idx;
 9306		memcpy(auth_data->key, req->key, req->key_len);
 9307	}
 9308
 9309	ieee80211_parse_cfg_selectors(auth_data->userspace_selectors,
 9310				      req->supported_selectors,
 9311				      req->supported_selectors_len);
 9312
 9313	auth_data->algorithm = auth_alg;
 9314
 9315	/* try to authenticate/probe */
 9316
 9317	if (ifmgd->auth_data) {
 9318		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
 9319			auth_data->peer_confirmed =
 9320				ifmgd->auth_data->peer_confirmed;
 9321		}
 9322		ieee80211_destroy_auth_data(sdata, cont_auth);
 9323	}
 9324
 9325	/* prep auth_data so we don't go into idle on disassoc */
 9326	ifmgd->auth_data = auth_data;
 9327
 9328	/* If this is continuation of an ongoing SAE authentication exchange
 9329	 * (i.e., request to send SAE Confirm) and the peer has already
 9330	 * confirmed, mark authentication completed since we are about to send
 9331	 * out SAE Confirm.
 9332	 */
 9333	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
 9334	    auth_data->peer_confirmed && auth_data->sae_trans == 2)
 9335		ieee80211_mark_sta_auth(sdata);
 9336
 9337	if (ifmgd->associated) {
 9338		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
 9339
 9340		sdata_info(sdata,
 9341			   "disconnect from AP %pM for new auth to %pM\n",
 9342			   sdata->vif.cfg.ap_addr, auth_data->ap_addr);
 9343		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
 9344				       WLAN_REASON_UNSPECIFIED,
 9345				       false, frame_buf);
 9346
 9347		ieee80211_report_disconnect(sdata, frame_buf,
 9348					    sizeof(frame_buf), true,
 9349					    WLAN_REASON_UNSPECIFIED,
 9350					    false);
 9351	}
 9352
 9353	/* needed for transmitting the auth frame(s) properly */
 9354	memcpy(sdata->vif.cfg.ap_addr, auth_data->ap_addr, ETH_ALEN);
 9355
 9356	bss = (void *)req->bss->priv;
 9357	wmm_used = bss->wmm_used && (local->hw.queues >= IEEE80211_NUM_ACS);
 9358
 9359	sband = local->hw.wiphy->bands[req->bss->channel->band];
 9360
 9361	ieee80211_determine_our_sta_mode_auth(sdata, sband, req, wmm_used,
 9362					      &conn);
 9363
 9364	err = ieee80211_prep_connection(sdata, req->bss, req->link_id,
 9365					req->ap_mld_addr, cont_auth,
 9366					&conn, false,
 9367					auth_data->userspace_selectors);
 9368	if (err)
 9369		goto err_clear;
 9370
 9371	if (req->link_id >= 0)
 9372		link = sdata_dereference(sdata->link[req->link_id], sdata);
 9373	else
 9374		link = &sdata->deflink;
 9375
 9376	if (WARN_ON(!link)) {
 9377		err = -ENOLINK;
 9378		goto err_clear;
 9379	}
 9380
 9381	sdata_info(sdata, "authenticate with %pM (local address=%pM)\n",
 9382		   auth_data->ap_addr, link->conf->addr);
 9383
 9384	err = ieee80211_auth(sdata);
 9385	if (err) {
 9386		sta_info_destroy_addr(sdata, auth_data->ap_addr);
 9387		goto err_clear;
 9388	}
 9389
 9390	/* hold our own reference */
 9391	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
 9392	return 0;
 9393
 9394 err_clear:
 9395	if (!ieee80211_vif_is_mld(&sdata->vif)) {
 9396		eth_zero_addr(sdata->deflink.u.mgd.bssid);
 9397		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
 9398						  BSS_CHANGED_BSSID);
 9399		ieee80211_link_release_channel(&sdata->deflink);
 9400	}
 9401	ifmgd->auth_data = NULL;
 9402	kfree(auth_data);
 9403	return err;
 9404}
 9405
 9406static void
 9407ieee80211_setup_assoc_link(struct ieee80211_sub_if_data *sdata,
 9408			   struct ieee80211_mgd_assoc_data *assoc_data,
 9409			   struct cfg80211_assoc_request *req,
 9410			   struct ieee80211_conn_settings *conn,
 9411			   unsigned int link_id)
 9412{
 9413	struct ieee80211_local *local = sdata->local;
 9414	const struct cfg80211_bss_ies *bss_ies;
 9415	struct ieee80211_supported_band *sband;
 9416	struct ieee80211_link_data *link;
 9417	struct cfg80211_bss *cbss;
 9418	struct ieee80211_bss *bss;
 9419
 9420	cbss = assoc_data->link[link_id].bss;
 9421	if (WARN_ON(!cbss))
 9422		return;
 9423
 9424	bss = (void *)cbss->priv;
 9425
 9426	sband = local->hw.wiphy->bands[cbss->channel->band];
 9427	if (WARN_ON(!sband))
 9428		return;
 9429
 9430	link = sdata_dereference(sdata->link[link_id], sdata);
 9431	if (WARN_ON(!link))
 9432		return;
 9433
 9434	/* for MLO connections assume advertising all rates is OK */
 9435	if (!req->ap_mld_addr) {
 9436		assoc_data->supp_rates = bss->supp_rates;
 9437		assoc_data->supp_rates_len = bss->supp_rates_len;
 9438	}
 9439
 9440	/* copy and link elems for the STA profile */
 9441	if (req->links[link_id].elems_len) {
 9442		memcpy(assoc_data->ie_pos, req->links[link_id].elems,
 9443		       req->links[link_id].elems_len);
 9444		assoc_data->link[link_id].elems = assoc_data->ie_pos;
 9445		assoc_data->link[link_id].elems_len = req->links[link_id].elems_len;
 9446		assoc_data->ie_pos += req->links[link_id].elems_len;
 9447	}
 9448
 9449	link->u.mgd.beacon_crc_valid = false;
 9450	link->u.mgd.dtim_period = 0;
 9451	link->u.mgd.have_beacon = false;
 9452
 9453	/* override HT configuration only if the AP and we support it */
 9454	if (conn->mode >= IEEE80211_CONN_MODE_HT) {
 9455		struct ieee80211_sta_ht_cap sta_ht_cap;
 9456
 9457		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
 9458		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
 9459	}
 9460
 9461	rcu_read_lock();
 9462	bss_ies = rcu_dereference(cbss->beacon_ies);
 9463	if (bss_ies) {
 9464		u8 dtim_count = 0;
 9465
 9466		ieee80211_get_dtim(bss_ies, &dtim_count,
 9467				   &link->u.mgd.dtim_period);
 9468
 9469		sdata->deflink.u.mgd.have_beacon = true;
 9470
 9471		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
 9472			link->conf->sync_tsf = bss_ies->tsf;
 9473			link->conf->sync_device_ts = bss->device_ts_beacon;
 9474			link->conf->sync_dtim_count = dtim_count;
 9475		}
 9476	} else {
 9477		bss_ies = rcu_dereference(cbss->ies);
 9478	}
 9479
 9480	if (bss_ies) {
 9481		const struct element *elem;
 9482
 9483		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
 9484					      bss_ies->data, bss_ies->len);
 9485		if (elem && elem->datalen >= 3)
 9486			link->conf->profile_periodicity = elem->data[2];
 9487		else
 9488			link->conf->profile_periodicity = 0;
 9489
 9490		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
 9491					  bss_ies->data, bss_ies->len);
 9492		if (elem && elem->datalen >= 11 &&
 9493		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
 9494			link->conf->ema_ap = true;
 9495		else
 9496			link->conf->ema_ap = false;
 9497	}
 9498	rcu_read_unlock();
 9499
 9500	if (bss->corrupt_data) {
 9501		char *corrupt_type = "data";
 9502
 9503		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
 9504			if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
 9505				corrupt_type = "beacon and probe response";
 9506			else
 9507				corrupt_type = "beacon";
 9508		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) {
 9509			corrupt_type = "probe response";
 9510		}
 9511		sdata_info(sdata, "associating to AP %pM with corrupt %s\n",
 9512			   cbss->bssid, corrupt_type);
 9513	}
 9514
 9515	if (link->u.mgd.req_smps == IEEE80211_SMPS_AUTOMATIC) {
 9516		if (sdata->u.mgd.powersave)
 9517			link->smps_mode = IEEE80211_SMPS_DYNAMIC;
 9518		else
 9519			link->smps_mode = IEEE80211_SMPS_OFF;
 9520	} else {
 9521		link->smps_mode = link->u.mgd.req_smps;
 9522	}
 9523}
 9524
 9525static int
 9526ieee80211_mgd_get_ap_ht_vht_capa(struct ieee80211_sub_if_data *sdata,
 9527				 struct ieee80211_mgd_assoc_data *assoc_data,
 9528				 int link_id)
 9529{
 9530	struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
 9531	enum nl80211_band band = cbss->channel->band;
 9532	struct ieee80211_supported_band *sband;
 9533	const struct element *elem;
 9534	int err;
 9535
 9536	/* neither HT nor VHT elements used on 6 GHz */
 9537	if (band == NL80211_BAND_6GHZ)
 9538		return 0;
 9539
 9540	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_HT)
 9541		return 0;
 9542
 9543	rcu_read_lock();
 9544	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_OPERATION);
 9545	if (!elem || elem->datalen < sizeof(struct ieee80211_ht_operation)) {
 9546		mlme_link_id_dbg(sdata, link_id, "no HT operation on BSS %pM\n",
 9547				 cbss->bssid);
 9548		err = -EINVAL;
 9549		goto out_rcu;
 9550	}
 9551	assoc_data->link[link_id].ap_ht_param =
 9552		((struct ieee80211_ht_operation *)(elem->data))->ht_param;
 9553	rcu_read_unlock();
 9554
 9555	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_VHT)
 9556		return 0;
 9557
 9558	/* some drivers want to support VHT on 2.4 GHz even */
 9559	sband = sdata->local->hw.wiphy->bands[band];
 9560	if (!sband->vht_cap.vht_supported)
 9561		return 0;
 9562
 9563	rcu_read_lock();
 9564	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
 9565	/* but even then accept it not being present on the AP */
 9566	if (!elem && band == NL80211_BAND_2GHZ) {
 9567		err = 0;
 9568		goto out_rcu;
 9569	}
 9570	if (!elem || elem->datalen < sizeof(struct ieee80211_vht_cap)) {
 9571		mlme_link_id_dbg(sdata, link_id, "no VHT capa on BSS %pM\n",
 9572				 cbss->bssid);
 9573		err = -EINVAL;
 9574		goto out_rcu;
 9575	}
 9576	memcpy(&assoc_data->link[link_id].ap_vht_cap, elem->data,
 9577	       sizeof(struct ieee80211_vht_cap));
 9578	rcu_read_unlock();
 9579
 9580	return 0;
 9581out_rcu:
 9582	rcu_read_unlock();
 9583	return err;
 9584}
 9585
 9586static bool
 9587ieee80211_mgd_assoc_bss_has_mld_ext_capa_ops(struct cfg80211_assoc_request *req)
 9588{
 9589	const struct cfg80211_bss_ies *ies;
 9590	struct cfg80211_bss *bss;
 9591	const struct element *ml;
 9592
 9593	/* not an MLO connection if link_id < 0, so irrelevant */
 9594	if (req->link_id < 0)
 9595		return false;
 9596
 9597	bss = req->links[req->link_id].bss;
 9598
 9599	guard(rcu)();
 9600	ies = rcu_dereference(bss->ies);
 9601	for_each_element_extid(ml, WLAN_EID_EXT_EHT_MULTI_LINK,
 9602			       ies->data, ies->len) {
 9603		const struct ieee80211_multi_link_elem *mle;
 9604
 9605		if (!ieee80211_mle_type_ok(ml->data + 1,
 9606					   IEEE80211_ML_CONTROL_TYPE_BASIC,
 9607					   ml->datalen - 1))
 9608			continue;
 9609
 9610		mle = (void *)(ml->data + 1);
 9611		if (mle->control & cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP))
 9612			return true;
 9613	}
 9614
 9615	return false;
 9616
 9617}
 9618
 9619int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 9620			struct cfg80211_assoc_request *req)
 9621{
 9622	unsigned int assoc_link_id = req->link_id < 0 ? 0 : req->link_id;
 9623	struct ieee80211_local *local = sdata->local;
 9624	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 9625	struct ieee80211_mgd_assoc_data *assoc_data;
 9626	const struct element *ssid_elem;
 9627	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
 9628	struct ieee80211_link_data *link;
 9629	struct cfg80211_bss *cbss;
 9630	bool override, uapsd_supported;
 9631	bool match_auth;
 9632	int i, err;
 9633	size_t size = sizeof(*assoc_data) + req->ie_len;
 9634
 9635	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
 9636		size += req->links[i].elems_len;
 9637
 9638	/* FIXME: no support for 4-addr MLO yet */
 9639	if (sdata->u.mgd.use_4addr && req->link_id >= 0)
 9640		return -EOPNOTSUPP;
 9641
 9642	assoc_data = kzalloc(size, GFP_KERNEL);
 9643	if (!assoc_data)
 9644		return -ENOMEM;
 9645
 9646	cbss = req->link_id < 0 ? req->bss : req->links[req->link_id].bss;
 9647
 9648	if (ieee80211_mgd_csa_in_process(sdata, cbss)) {
 9649		sdata_info(sdata, "AP is in CSA process, reject assoc\n");
 9650		err = -EINVAL;
 9651		goto err_free;
 9652	}
 9653
 9654	rcu_read_lock();
 9655	ssid_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
 9656	if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
 9657		rcu_read_unlock();
 9658		err = -EINVAL;
 9659		goto err_free;
 9660	}
 9661
 9662	memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
 9663	assoc_data->ssid_len = ssid_elem->datalen;
 9664	rcu_read_unlock();
 9665
 9666	if (req->ap_mld_addr)
 9667		memcpy(assoc_data->ap_addr, req->ap_mld_addr, ETH_ALEN);
 9668	else
 9669		memcpy(assoc_data->ap_addr, cbss->bssid, ETH_ALEN);
 9670
 9671	/*
 9672	 * Many APs have broken parsing of the extended MLD capa/ops field,
 9673	 * dropping (re-)association request frames or replying with association
 9674	 * response with a failure status if it's present.
 9675	 * Set our value from the userspace request only in strict mode or if
 9676	 * the AP also had that field present.
 9677	 */
 9678	if (ieee80211_hw_check(&local->hw, STRICT) ||
 9679	    ieee80211_mgd_assoc_bss_has_mld_ext_capa_ops(req))
 9680		assoc_data->ext_mld_capa_ops =
 9681			cpu_to_le16(req->ext_mld_capa_ops);
 9682
 9683	if (ifmgd->associated) {
 9684		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
 9685
 9686		sdata_info(sdata,
 9687			   "disconnect from AP %pM for new assoc to %pM\n",
 9688			   sdata->vif.cfg.ap_addr, assoc_data->ap_addr);
 9689		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
 9690				       WLAN_REASON_UNSPECIFIED,
 9691				       false, frame_buf);
 9692
 9693		ieee80211_report_disconnect(sdata, frame_buf,
 9694					    sizeof(frame_buf), true,
 9695					    WLAN_REASON_UNSPECIFIED,
 9696					    false);
 9697	}
 9698
 9699	memset(sdata->u.mgd.userspace_selectors, 0,
 9700	       sizeof(sdata->u.mgd.userspace_selectors));
 9701	ieee80211_parse_cfg_selectors(sdata->u.mgd.userspace_selectors,
 9702				      req->supported_selectors,
 9703				      req->supported_selectors_len);
 9704
 9705	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
 9706	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
 9707	       sizeof(ifmgd->ht_capa_mask));
 9708
 9709	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
 9710	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
 9711	       sizeof(ifmgd->vht_capa_mask));
 9712
 9713	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
 9714	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
 9715	       sizeof(ifmgd->s1g_capa_mask));
 9716
 9717	/* keep some setup (AP STA, channel, ...) if matching */
 9718	match_auth = ifmgd->auth_data &&
 9719		     ether_addr_equal(ifmgd->auth_data->ap_addr,
 9720				      assoc_data->ap_addr) &&
 9721		     ifmgd->auth_data->link_id == req->link_id;
 9722
 9723	if (req->ap_mld_addr) {
 9724		uapsd_supported = true;
 9725
 9726		if (req->flags & (ASSOC_REQ_DISABLE_HT |
 9727				  ASSOC_REQ_DISABLE_VHT |
 9728				  ASSOC_REQ_DISABLE_HE |
 9729				  ASSOC_REQ_DISABLE_EHT)) {
 9730			err = -EINVAL;
 9731			goto err_free;
 9732		}
 9733
 9734		for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
 9735			struct ieee80211_supported_band *sband;
 9736			struct cfg80211_bss *link_cbss = req->links[i].bss;
 9737			struct ieee80211_bss *bss;
 9738
 9739			if (!link_cbss)
 9740				continue;
 9741
 9742			bss = (void *)link_cbss->priv;
 9743
 9744			if (!bss->wmm_used) {
 9745				err = -EINVAL;
 9746				req->links[i].error = err;
 9747				goto err_free;
 9748			}
 9749
 9750			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
 9751				err = -EINVAL;
 9752				req->links[i].error = err;
 9753				goto err_free;
 9754			}
 9755
 9756			link = sdata_dereference(sdata->link[i], sdata);
 9757			if (link)
 9758				ether_addr_copy(assoc_data->link[i].addr,
 9759						link->conf->addr);
 9760			else
 9761				eth_random_addr(assoc_data->link[i].addr);
 9762			sband = local->hw.wiphy->bands[link_cbss->channel->band];
 9763
 9764			if (match_auth && i == assoc_link_id && link)
 9765				assoc_data->link[i].conn = link->u.mgd.conn;
 9766			else
 9767				assoc_data->link[i].conn =
 9768					ieee80211_conn_settings_unlimited;
 9769			ieee80211_determine_our_sta_mode_assoc(sdata, sband,
 9770							       req, true, i,
 9771							       &assoc_data->link[i].conn);
 9772			assoc_data->link[i].bss = link_cbss;
 9773
 9774			if (!bss->uapsd_supported)
 9775				uapsd_supported = false;
 9776
 9777			if (assoc_data->link[i].conn.mode < IEEE80211_CONN_MODE_EHT) {
 9778				err = -EINVAL;
 9779				req->links[i].error = err;
 9780				goto err_free;
 9781			}
 9782
 9783			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata,
 9784							       assoc_data, i);
 9785			if (err) {
 9786				err = -EINVAL;
 9787				req->links[i].error = err;
 9788				goto err_free;
 9789			}
 9790		}
 9791
 9792		assoc_data->wmm = true;
 9793	} else {
 9794		struct ieee80211_supported_band *sband;
 9795		struct ieee80211_bss *bss = (void *)cbss->priv;
 9796
 9797		memcpy(assoc_data->link[0].addr, sdata->vif.addr, ETH_ALEN);
 9798		assoc_data->s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
 9799
 9800		assoc_data->wmm = bss->wmm_used &&
 9801				  (local->hw.queues >= IEEE80211_NUM_ACS);
 9802
 9803		if (cbss->channel->band == NL80211_BAND_6GHZ &&
 9804		    req->flags & (ASSOC_REQ_DISABLE_HT |
 9805				  ASSOC_REQ_DISABLE_VHT |
 9806				  ASSOC_REQ_DISABLE_HE)) {
 9807			err = -EINVAL;
 9808			goto err_free;
 9809		}
 9810
 9811		sband = local->hw.wiphy->bands[cbss->channel->band];
 9812
 9813		assoc_data->link[0].bss = cbss;
 9814
 9815		if (match_auth)
 9816			assoc_data->link[0].conn = sdata->deflink.u.mgd.conn;
 9817		else
 9818			assoc_data->link[0].conn =
 9819				ieee80211_conn_settings_unlimited;
 9820		ieee80211_determine_our_sta_mode_assoc(sdata, sband, req,
 9821						       assoc_data->wmm, 0,
 9822						       &assoc_data->link[0].conn);
 9823
 9824		uapsd_supported = bss->uapsd_supported;
 9825
 9826		err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, assoc_data, 0);
 9827		if (err)
 9828			goto err_free;
 9829	}
 9830
 9831	assoc_data->spp_amsdu = req->flags & ASSOC_REQ_SPP_AMSDU;
 9832
 9833	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
 9834		err = -EBUSY;
 9835		goto err_free;
 9836	}
 9837
 9838	if (ifmgd->assoc_data) {
 9839		err = -EBUSY;
 9840		goto err_free;
 9841	}
 9842
 9843	/* Cleanup is delayed if auth_data matches */
 9844	if (ifmgd->auth_data && !match_auth)
 9845		ieee80211_destroy_auth_data(sdata, false);
 9846
 9847	if (req->ie && req->ie_len) {
 9848		memcpy(assoc_data->ie, req->ie, req->ie_len);
 9849		assoc_data->ie_len = req->ie_len;
 9850		assoc_data->ie_pos = assoc_data->ie + assoc_data->ie_len;
 9851	} else {
 9852		assoc_data->ie_pos = assoc_data->ie;
 9853	}
 9854
 9855	if (req->fils_kek) {
 9856		/* should already be checked in cfg80211 - so warn */
 9857		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
 9858			err = -EINVAL;
 9859			goto err_free;
 9860		}
 9861		memcpy(assoc_data->fils_kek, req->fils_kek,
 9862		       req->fils_kek_len);
 9863		assoc_data->fils_kek_len = req->fils_kek_len;
 9864	}
 9865
 9866	if (req->fils_nonces)
 9867		memcpy(assoc_data->fils_nonces, req->fils_nonces,
 9868		       2 * FILS_NONCE_LEN);
 9869
 9870	/* default timeout */
 9871	assoc_data->timeout = jiffies;
 9872	assoc_data->timeout_started = true;
 9873
 9874	assoc_data->assoc_link_id = assoc_link_id;
 9875
 9876	if (req->ap_mld_addr) {
 9877		/* if there was no authentication, set up the link */
 9878		err = ieee80211_vif_set_links(sdata, BIT(assoc_link_id), 0);
 9879		if (err)
 9880			goto err_clear;
 9881	}
 9882
 9883	link = sdata_dereference(sdata->link[assoc_link_id], sdata);
 9884	if (WARN_ON(!link)) {
 9885		err = -EINVAL;
 9886		goto err_clear;
 9887	}
 9888
 9889	override = link->u.mgd.conn.mode !=
 9890			assoc_data->link[assoc_link_id].conn.mode ||
 9891		   link->u.mgd.conn.bw_limit !=
 9892			assoc_data->link[assoc_link_id].conn.bw_limit;
 9893	link->u.mgd.conn = assoc_data->link[assoc_link_id].conn;
 9894
 9895	ieee80211_setup_assoc_link(sdata, assoc_data, req, &link->u.mgd.conn,
 9896				   assoc_link_id);
 9897
 9898	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
 9899		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
 9900	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
 9901		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
 9902
 9903	if (assoc_data->wmm && uapsd_supported &&
 9904	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
 9905		assoc_data->uapsd = true;
 9906		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
 9907	} else {
 9908		assoc_data->uapsd = false;
 9909		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
 9910	}
 9911
 9912	if (req->prev_bssid)
 9913		memcpy(assoc_data->prev_ap_addr, req->prev_bssid, ETH_ALEN);
 9914
 9915	if (req->use_mfp) {
 9916		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
 9917		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
 9918	} else {
 9919		ifmgd->mfp = IEEE80211_MFP_DISABLED;
 9920		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
 9921	}
 9922
 9923	if (req->flags & ASSOC_REQ_USE_RRM)
 9924		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
 9925	else
 9926		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
 9927
 9928	if (req->crypto.control_port)
 9929		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
 9930	else
 9931		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
 9932
 9933	sdata->control_port_protocol = req->crypto.control_port_ethertype;
 9934	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
 9935	sdata->control_port_over_nl80211 =
 9936					req->crypto.control_port_over_nl80211;
 9937	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
 9938
 9939	/* kick off associate process */
 9940	ifmgd->assoc_data = assoc_data;
 9941
 9942	for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) {
 9943		if (!assoc_data->link[i].bss)
 9944			continue;
 9945		if (i == assoc_data->assoc_link_id)
 9946			continue;
 9947		/* only calculate the mode, hence link == NULL */
 9948		err = ieee80211_prep_channel(sdata, NULL, i,
 9949					     assoc_data->link[i].bss, true,
 9950					     &assoc_data->link[i].conn,
 9951					     sdata->u.mgd.userspace_selectors);
 9952		if (err) {
 9953			req->links[i].error = err;
 9954			goto err_clear;
 9955		}
 9956	}
 9957
 9958	memcpy(vif_cfg->ssid, assoc_data->ssid, assoc_data->ssid_len);
 9959	vif_cfg->ssid_len = assoc_data->ssid_len;
 9960
 9961	/* needed for transmitting the assoc frames properly */
 9962	memcpy(sdata->vif.cfg.ap_addr, assoc_data->ap_addr, ETH_ALEN);
 9963
 9964	err = ieee80211_prep_connection(sdata, cbss, req->link_id,
 9965					req->ap_mld_addr, true,
 9966					&assoc_data->link[assoc_link_id].conn,
 9967					override,
 9968					sdata->u.mgd.userspace_selectors);
 9969	if (err)
 9970		goto err_clear;
 9971
 9972	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC)) {
 9973		const struct cfg80211_bss_ies *beacon_ies;
 9974
 9975		rcu_read_lock();
 9976		beacon_ies = rcu_dereference(req->bss->beacon_ies);
 9977		if (!beacon_ies) {
 9978			/*
 9979			 * Wait up to one beacon interval ...
 9980			 * should this be more if we miss one?
 9981			 */
 9982			sdata_info(sdata, "waiting for beacon from %pM\n",
 9983				   link->u.mgd.bssid);
 9984			assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
 9985			assoc_data->timeout_started = true;
 9986			assoc_data->need_beacon = true;
 9987		}
 9988		rcu_read_unlock();
 9989	}
 9990
 9991	run_again(sdata, assoc_data->timeout);
 9992
 9993	/* We are associating, clean up auth_data */
 9994	if (ifmgd->auth_data)
 9995		ieee80211_destroy_auth_data(sdata, true);
 9996
 9997	return 0;
 9998 err_clear:
 9999	if (!ifmgd->auth_data) {
10000		eth_zero_addr(sdata->deflink.u.mgd.bssid);
10001		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
10002						  BSS_CHANGED_BSSID);
10003	}
10004	ifmgd->assoc_data = NULL;
10005 err_free:
10006	kfree(assoc_data);
10007	return err;
10008}
10009
10010int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
10011			 struct cfg80211_deauth_request *req)
10012{
10013	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10014	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
10015	bool tx = !req->local_state_change;
10016	struct ieee80211_prep_tx_info info = {
10017		.subtype = IEEE80211_STYPE_DEAUTH,
10018	};
10019
10020	if (ifmgd->auth_data &&
10021	    ether_addr_equal(ifmgd->auth_data->ap_addr, req->bssid)) {
10022		sdata_info(sdata,
10023			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
10024			   req->bssid, req->reason_code,
10025			   ieee80211_get_reason_code_string(req->reason_code));
10026
10027		info.link_id = ifmgd->auth_data->link_id;
10028		drv_mgd_prepare_tx(sdata->local, sdata, &info);
10029		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
10030					       IEEE80211_STYPE_DEAUTH,
10031					       req->reason_code, tx,
10032					       frame_buf);
10033		ieee80211_destroy_auth_data(sdata, false);
10034		ieee80211_report_disconnect(sdata, frame_buf,
10035					    sizeof(frame_buf), true,
10036					    req->reason_code, false);
10037		drv_mgd_complete_tx(sdata->local, sdata, &info);
10038		return 0;
10039	}
10040
10041	if (ifmgd->assoc_data &&
10042	    ether_addr_equal(ifmgd->assoc_data->ap_addr, req->bssid)) {
10043		sdata_info(sdata,
10044			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
10045			   req->bssid, req->reason_code,
10046			   ieee80211_get_reason_code_string(req->reason_code));
10047
10048		info.link_id = ifmgd->assoc_data->assoc_link_id;
10049		drv_mgd_prepare_tx(sdata->local, sdata, &info);
10050		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
10051					       IEEE80211_STYPE_DEAUTH,
10052					       req->reason_code, tx,
10053					       frame_buf);
10054		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
10055		ieee80211_report_disconnect(sdata, frame_buf,
10056					    sizeof(frame_buf), true,
10057					    req->reason_code, false);
10058		drv_mgd_complete_tx(sdata->local, sdata, &info);
10059		return 0;
10060	}
10061
10062	if (ifmgd->associated &&
10063	    ether_addr_equal(sdata->vif.cfg.ap_addr, req->bssid)) {
10064		sdata_info(sdata,
10065			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
10066			   req->bssid, req->reason_code,
10067			   ieee80211_get_reason_code_string(req->reason_code));
10068
10069		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
10070				       req->reason_code, tx, frame_buf);
10071		ieee80211_report_disconnect(sdata, frame_buf,
10072					    sizeof(frame_buf), true,
10073					    req->reason_code, false);
10074		return 0;
10075	}
10076
10077	return -ENOTCONN;
10078}
10079
10080int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
10081			   struct cfg80211_disassoc_request *req)
10082{
10083	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
10084
10085	if (!sdata->u.mgd.associated ||
10086	    memcmp(sdata->vif.cfg.ap_addr, req->ap_addr, ETH_ALEN))
10087		return -ENOTCONN;
10088
10089	sdata_info(sdata,
10090		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
10091		   req->ap_addr, req->reason_code,
10092		   ieee80211_get_reason_code_string(req->reason_code));
10093
10094	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
10095			       req->reason_code, !req->local_state_change,
10096			       frame_buf);
10097
10098	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
10099				    req->reason_code, false);
10100
10101	return 0;
10102}
10103
10104void ieee80211_mgd_stop_link(struct ieee80211_link_data *link)
10105{
10106	wiphy_work_cancel(link->sdata->local->hw.wiphy,
10107			  &link->u.mgd.request_smps_work);
10108	wiphy_work_cancel(link->sdata->local->hw.wiphy,
10109			  &link->u.mgd.recalc_smps);
10110	wiphy_hrtimer_work_cancel(link->sdata->local->hw.wiphy,
10111				  &link->u.mgd.csa.switch_work);
10112}
10113
10114void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
10115{
10116	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10117
10118	/*
10119	 * Make sure some work items will not run after this,
10120	 * they will not do anything but might not have been
10121	 * cancelled when disconnecting.
10122	 */
10123	wiphy_work_cancel(sdata->local->hw.wiphy,
10124			  &ifmgd->monitor_work);
10125	wiphy_work_cancel(sdata->local->hw.wiphy,
10126			  &ifmgd->beacon_connection_loss_work);
10127	wiphy_work_cancel(sdata->local->hw.wiphy,
10128			  &ifmgd->csa_connection_drop_work);
10129	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
10130				  &ifmgd->tdls_peer_del_work);
10131
10132	if (ifmgd->assoc_data)
10133		ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
10134	if (ifmgd->auth_data)
10135		ieee80211_destroy_auth_data(sdata, false);
10136	spin_lock_bh(&ifmgd->teardown_lock);
10137	if (ifmgd->teardown_skb) {
10138		kfree_skb(ifmgd->teardown_skb);
10139		ifmgd->teardown_skb = NULL;
10140		ifmgd->orig_teardown_skb = NULL;
10141	}
10142	kfree(ifmgd->assoc_req_ies);
10143	ifmgd->assoc_req_ies = NULL;
10144	ifmgd->assoc_req_ies_len = 0;
10145	spin_unlock_bh(&ifmgd->teardown_lock);
10146	timer_delete_sync(&ifmgd->timer);
10147}
10148
10149void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
10150			       enum nl80211_cqm_rssi_threshold_event rssi_event,
10151			       s32 rssi_level,
10152			       gfp_t gfp)
10153{
10154	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10155
10156	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
10157
10158	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
10159}
10160EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
10161
10162void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
10163{
10164	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10165
10166	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
10167
10168	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
10169}
10170EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
10171
10172static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
10173					    int rssi_min_thold,
10174					    int rssi_max_thold)
10175{
10176	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
10177
10178	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
10179		return;
10180
10181	/*
10182	 * Scale up threshold values before storing it, as the RSSI averaging
10183	 * algorithm uses a scaled up value as well. Change this scaling
10184	 * factor if the RSSI averaging algorithm changes.
10185	 */
10186	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
10187	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
10188}
10189
10190void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
10191				    int rssi_min_thold,
10192				    int rssi_max_thold)
10193{
10194	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10195
10196	WARN_ON(rssi_min_thold == rssi_max_thold ||
10197		rssi_min_thold > rssi_max_thold);
10198
10199	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
10200				       rssi_max_thold);
10201}
10202EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
10203
10204void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
10205{
10206	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10207
10208	_ieee80211_enable_rssi_reports(sdata, 0, 0);
10209}
10210EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
10211
10212void ieee80211_process_ml_reconf_resp(struct ieee80211_sub_if_data *sdata,
10213				      struct ieee80211_mgmt *mgmt, size_t len)
10214{
10215	struct ieee80211_local *local = sdata->local;
10216	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10217	struct ieee80211_mgd_assoc_data *add_links_data =
10218		ifmgd->reconf.add_links_data;
10219	struct sta_info *sta;
10220	struct cfg80211_mlo_reconf_done_data done_data = {};
10221	u16 sta_changed_links = sdata->u.mgd.reconf.added_links |
10222		                sdata->u.mgd.reconf.removed_links;
10223	u16 link_mask, valid_links;
10224	unsigned int link_id;
10225	size_t orig_len = len;
10226	u8 i, group_key_data_len;
10227	u8 *pos;
10228
10229	if (!ieee80211_vif_is_mld(&sdata->vif) ||
10230	    len < offsetofend(typeof(*mgmt), u.action.u.ml_reconf_resp) ||
10231	    mgmt->u.action.u.ml_reconf_resp.dialog_token !=
10232	    sdata->u.mgd.reconf.dialog_token ||
10233	    !sta_changed_links)
10234		return;
10235
10236	pos = mgmt->u.action.u.ml_reconf_resp.variable;
10237	len -= offsetofend(typeof(*mgmt), u.action.u.ml_reconf_resp);
10238
10239	/* each status duple is 3 octets */
10240	if (len < mgmt->u.action.u.ml_reconf_resp.count * 3) {
10241		sdata_info(sdata,
10242			   "mlo: reconf: unexpected len=%zu, count=%u\n",
10243			   len, mgmt->u.action.u.ml_reconf_resp.count);
10244		goto disconnect;
10245	}
10246
10247	link_mask = sta_changed_links;
10248	for (i = 0; i < mgmt->u.action.u.ml_reconf_resp.count; i++) {
10249		u16 status = get_unaligned_le16(pos + 1);
10250
10251		link_id = *pos;
10252
10253		if (!(link_mask & BIT(link_id))) {
10254			sdata_info(sdata,
10255				   "mlo: reconf: unexpected link: %u, changed=0x%x\n",
10256				   link_id, sta_changed_links);
10257			goto disconnect;
10258		}
10259
10260		/* clear the corresponding link, to detect the case that
10261		 * the same link was included more than one time
10262		 */
10263		link_mask &= ~BIT(link_id);
10264
10265		/* Handle failure to remove links here. Failure to remove added
10266		 * links will be done later in the flow.
10267		 */
10268		if (status != WLAN_STATUS_SUCCESS) {
10269			sdata_info(sdata,
10270				   "mlo: reconf: failed on link=%u, status=%u\n",
10271				   link_id, status);
10272
10273			/* The AP MLD failed to remove a link that was already
10274			 * removed locally. As this is not expected behavior,
10275			 * disconnect
10276			 */
10277			if (sdata->u.mgd.reconf.removed_links & BIT(link_id))
10278				goto disconnect;
10279
10280			/* The AP MLD failed to add a link. Remove it from the
10281			 * added links.
10282			 */
10283			sdata->u.mgd.reconf.added_links &= ~BIT(link_id);
10284		}
10285
10286		pos += 3;
10287		len -= 3;
10288	}
10289
10290	if (link_mask) {
10291		sdata_info(sdata,
10292			   "mlo: reconf: no response for links=0x%x\n",
10293			   link_mask);
10294		goto disconnect;
10295	}
10296
10297	if (!sdata->u.mgd.reconf.added_links)
10298		goto out;
10299
10300	if (len < 1 || len < 1 + *pos) {
10301		sdata_info(sdata,
10302			   "mlo: reconf: invalid group key data length");
10303		goto disconnect;
10304	}
10305
10306	/* The Group Key Data field must be present when links are added. This
10307	 * field should be processed by userland.
10308	 */
10309	group_key_data_len = *pos++;
10310
10311	pos += group_key_data_len;
10312	len -= group_key_data_len + 1;
10313
10314	/* Process the information for the added links */
10315	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
10316	if (WARN_ON(!sta))
10317		goto disconnect;
10318
10319	valid_links = sdata->vif.valid_links;
10320	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10321		if (!add_links_data->link[link_id].bss ||
10322		    !(sdata->u.mgd.reconf.added_links & BIT(link_id)))
10323			continue;
10324
10325		valid_links |= BIT(link_id);
10326		if (ieee80211_sta_allocate_link(sta, link_id))
10327			goto disconnect;
10328	}
10329
10330	ieee80211_vif_set_links(sdata, valid_links, sdata->vif.dormant_links);
10331	link_mask = 0;
10332	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10333		struct cfg80211_bss *cbss = add_links_data->link[link_id].bss;
10334		struct ieee80211_link_data *link;
10335		struct link_sta_info *link_sta;
10336		u64 changed = 0;
10337
10338		if (!cbss)
10339			continue;
10340
10341		link = sdata_dereference(sdata->link[link_id], sdata);
10342		if (WARN_ON(!link))
10343			goto disconnect;
10344
10345		link_info(link,
10346			  "mlo: reconf: local address %pM, AP link address %pM\n",
10347			  add_links_data->link[link_id].addr,
10348			  add_links_data->link[link_id].bss->bssid);
10349
10350		link_sta = rcu_dereference_protected(sta->link[link_id],
10351						     lockdep_is_held(&local->hw.wiphy->mtx));
10352		if (WARN_ON(!link_sta))
10353			goto disconnect;
10354
10355		if (!link->u.mgd.have_beacon) {
10356			const struct cfg80211_bss_ies *ies;
10357
10358			rcu_read_lock();
10359			ies = rcu_dereference(cbss->beacon_ies);
10360			if (ies)
10361				link->u.mgd.have_beacon = true;
10362			else
10363				ies = rcu_dereference(cbss->ies);
10364			ieee80211_get_dtim(ies,
10365					   &link->conf->sync_dtim_count,
10366					   &link->u.mgd.dtim_period);
10367			link->conf->beacon_int = cbss->beacon_interval;
10368			rcu_read_unlock();
10369		}
10370
10371		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
10372
10373		link->u.mgd.conn = add_links_data->link[link_id].conn;
10374		if (ieee80211_prep_channel(sdata, link, link_id, cbss,
10375					   true, &link->u.mgd.conn,
10376					   sdata->u.mgd.userspace_selectors)) {
10377			link_info(link, "mlo: reconf: prep_channel failed\n");
10378			goto disconnect;
10379		}
10380
10381		if (ieee80211_mgd_setup_link_sta(link, sta, link_sta,
10382						 add_links_data->link[link_id].bss))
10383			goto disconnect;
10384
10385		if (!ieee80211_assoc_config_link(link, link_sta,
10386						 add_links_data->link[link_id].bss,
10387						 mgmt, pos, len,
10388						 &changed))
10389			goto disconnect;
10390
10391		/* The AP MLD indicated success for this link, but the station
10392		 * profile status indicated otherwise. Since there is an
10393		 * inconsistency in the ML reconfiguration response, disconnect
10394		 */
10395		if (add_links_data->link[link_id].status != WLAN_STATUS_SUCCESS)
10396			goto disconnect;
10397
10398		ieee80211_sta_init_nss(link_sta);
10399		if (ieee80211_sta_activate_link(sta, link_id))
10400			goto disconnect;
10401
10402		changed |= ieee80211_link_set_associated(link, cbss);
10403		ieee80211_link_info_change_notify(sdata, link, changed);
10404
10405		ieee80211_recalc_smps(sdata, link);
10406		link_mask |= BIT(link_id);
10407	}
10408
10409	sdata_info(sdata,
10410		   "mlo: reconf: current valid_links=0x%x, added=0x%x\n",
10411		   valid_links, link_mask);
10412
10413	/* links might have changed due to rejected ones, set them again */
10414	ieee80211_vif_set_links(sdata, valid_links, sdata->vif.dormant_links);
10415	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
10416
10417	ieee80211_recalc_ps(local);
10418	ieee80211_recalc_ps_vif(sdata);
10419
10420	done_data.buf = (const u8 *)mgmt;
10421	done_data.len = orig_len;
10422	done_data.added_links = link_mask;
10423
10424	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10425		done_data.links[link_id].bss = add_links_data->link[link_id].bss;
10426		done_data.links[link_id].addr =
10427			add_links_data->link[link_id].addr;
10428	}
10429
10430	cfg80211_mlo_reconf_add_done(sdata->dev, &done_data);
10431	kfree(sdata->u.mgd.reconf.add_links_data);
10432	sdata->u.mgd.reconf.add_links_data = NULL;
10433out:
10434	ieee80211_ml_reconf_reset(sdata);
10435	return;
10436
10437disconnect:
10438	__ieee80211_disconnect(sdata);
10439}
10440
10441static struct sk_buff *
10442ieee80211_build_ml_reconf_req(struct ieee80211_sub_if_data *sdata,
10443			      struct ieee80211_mgd_assoc_data *add_links_data,
10444			      u16 removed_links, __le16 ext_mld_capa_ops)
10445{
10446	struct ieee80211_local *local = sdata->local;
10447	struct ieee80211_mgmt *mgmt;
10448	struct ieee80211_multi_link_elem *ml_elem;
10449	struct ieee80211_mle_basic_common_info *common;
10450	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
10451	struct sk_buff *skb;
10452	size_t size;
10453	unsigned int link_id;
10454	__le16 eml_capa = 0, mld_capa_ops = 0;
10455	struct ieee80211_tx_info *info;
10456	u8 common_size, var_common_size;
10457	u8 *ml_elem_len;
10458	u16 capab = 0;
10459
10460	size = local->hw.extra_tx_headroom + sizeof(*mgmt);
10461
10462	/* Consider the maximal length of the reconfiguration ML element */
10463	size += sizeof(struct ieee80211_multi_link_elem);
10464
10465	/* The Basic ML element and the Reconfiguration ML element have the same
10466	 * fixed common information fields in the context of ML reconfiguration
10467	 * action frame. The AP MLD MAC address must always be present
10468	 */
10469	common_size = sizeof(*common);
10470
10471	/* when adding links, the MLD capabilities must be present */
10472	var_common_size = 0;
10473	if (add_links_data) {
10474		const struct wiphy_iftype_ext_capab *ift_ext_capa =
10475			cfg80211_get_iftype_ext_capa(local->hw.wiphy,
10476						     ieee80211_vif_type_p2p(&sdata->vif));
10477
10478		if (ift_ext_capa) {
10479			eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities);
10480			mld_capa_ops =
10481				cpu_to_le16(ift_ext_capa->mld_capa_and_ops);
10482		}
10483
10484		/* MLD capabilities and operation */
10485		var_common_size += 2;
10486
10487		/* EML capabilities */
10488		if (eml_capa & cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
10489					    IEEE80211_EML_CAP_EMLMR_SUPPORT)))
10490			var_common_size += 2;
10491	}
10492
10493	if (ext_mld_capa_ops)
10494		var_common_size += 2;
10495
10496	/* Add the common information length */
10497	size += common_size + var_common_size;
10498
10499	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10500		struct cfg80211_bss *cbss;
10501		size_t elems_len;
10502
10503		if (removed_links & BIT(link_id)) {
10504			size += sizeof(struct ieee80211_mle_per_sta_profile) +
10505				ETH_ALEN;
10506			continue;
10507		}
10508
10509		if (!add_links_data || !add_links_data->link[link_id].bss)
10510			continue;
10511
10512		elems_len = add_links_data->link[link_id].elems_len;
10513		cbss = add_links_data->link[link_id].bss;
10514
10515		/* should be the same across all BSSes */
10516		if (cbss->capability & WLAN_CAPABILITY_PRIVACY)
10517			capab |= WLAN_CAPABILITY_PRIVACY;
10518
10519		size += 2 + sizeof(struct ieee80211_mle_per_sta_profile) +
10520			ETH_ALEN;
10521
10522		/* WMM */
10523		size += 9;
10524		size += ieee80211_link_common_elems_size(sdata, iftype, cbss,
10525							 elems_len);
10526	}
10527
10528	skb = alloc_skb(size, GFP_KERNEL);
10529	if (!skb)
10530		return NULL;
10531
10532	skb_reserve(skb, local->hw.extra_tx_headroom);
10533	mgmt = skb_put_zero(skb, offsetofend(struct ieee80211_mgmt,
10534					     u.action.u.ml_reconf_req));
10535
10536	/* Add the MAC header */
10537	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
10538					  IEEE80211_STYPE_ACTION);
10539	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
10540	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
10541	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
10542
10543	/* Add the action frame fixed fields */
10544	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
10545	mgmt->u.action.u.ml_reconf_req.action_code =
10546		WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_REQ;
10547
10548	/* allocate a dialog token and store it */
10549	sdata->u.mgd.reconf.dialog_token = ++sdata->u.mgd.dialog_token_alloc;
10550	mgmt->u.action.u.ml_reconf_req.dialog_token =
10551		sdata->u.mgd.reconf.dialog_token;
10552
10553	/* Add the ML reconfiguration element and the common information  */
10554	skb_put_u8(skb, WLAN_EID_EXTENSION);
10555	ml_elem_len = skb_put(skb, 1);
10556	skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
10557	ml_elem = skb_put(skb, sizeof(*ml_elem));
10558	ml_elem->control =
10559		cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_RECONF |
10560			    IEEE80211_MLC_RECONF_PRES_MLD_MAC_ADDR);
10561	common = skb_put(skb, common_size);
10562	common->len = common_size + var_common_size;
10563	memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
10564
10565	if (add_links_data) {
10566		if (eml_capa &
10567		    cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
10568				 IEEE80211_EML_CAP_EMLMR_SUPPORT))) {
10569			ml_elem->control |=
10570				cpu_to_le16(IEEE80211_MLC_RECONF_PRES_EML_CAPA);
10571			skb_put_data(skb, &eml_capa, sizeof(eml_capa));
10572		}
10573
10574		ml_elem->control |=
10575			cpu_to_le16(IEEE80211_MLC_RECONF_PRES_MLD_CAPA_OP);
10576
10577		skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops));
10578	}
10579
10580	if (ext_mld_capa_ops) {
10581		ml_elem->control |=
10582			cpu_to_le16(IEEE80211_MLC_RECONF_PRES_EXT_MLD_CAPA_OP);
10583		skb_put_data(skb, &ext_mld_capa_ops, sizeof(ext_mld_capa_ops));
10584	}
10585
10586	if (sdata->u.mgd.flags & IEEE80211_STA_ENABLE_RRM)
10587		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
10588
10589	/* Add the per station profile */
10590	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10591		u8 *subelem_len = NULL;
10592		u16 ctrl;
10593		const u8 *addr;
10594
10595		/* Skip links that are not changing */
10596		if (!(removed_links & BIT(link_id)) &&
10597		    (!add_links_data || !add_links_data->link[link_id].bss))
10598			continue;
10599
10600		ctrl = link_id |
10601		       IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT;
10602
10603		if (removed_links & BIT(link_id)) {
10604			struct ieee80211_bss_conf *conf =
10605				sdata_dereference(sdata->vif.link_conf[link_id],
10606						  sdata);
10607			if (!conf)
10608				continue;
10609
10610			addr = conf->addr;
10611			ctrl |= u16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_DEL_LINK,
10612						IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
10613		} else {
10614			addr = add_links_data->link[link_id].addr;
10615			ctrl |= IEEE80211_MLE_STA_RECONF_CONTROL_COMPLETE_PROFILE |
10616				u16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_ADD_LINK,
10617						IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
10618		}
10619
10620		skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
10621		subelem_len = skb_put(skb, 1);
10622
10623		put_unaligned_le16(ctrl, skb_put(skb, sizeof(ctrl)));
10624		skb_put_u8(skb, 1 + ETH_ALEN);
10625		skb_put_data(skb, addr, ETH_ALEN);
10626
10627		if (!(removed_links & BIT(link_id))) {
10628			u16 link_present_elems[PRESENT_ELEMS_MAX] = {};
10629			size_t extra_used;
10630			void *capab_pos;
10631			u8 qos_info;
10632
10633			capab_pos = skb_put(skb, 2);
10634
10635			extra_used =
10636				ieee80211_add_link_elems(sdata, skb, &capab, NULL,
10637							 add_links_data->link[link_id].elems,
10638							 add_links_data->link[link_id].elems_len,
10639							 link_id, NULL,
10640							 link_present_elems,
10641							 add_links_data);
10642
10643			if (add_links_data->link[link_id].elems)
10644				skb_put_data(skb,
10645					     add_links_data->link[link_id].elems +
10646					     extra_used,
10647					     add_links_data->link[link_id].elems_len -
10648					     extra_used);
10649			if (sdata->u.mgd.flags & IEEE80211_STA_UAPSD_ENABLED) {
10650				qos_info = sdata->u.mgd.uapsd_queues;
10651				qos_info |= (sdata->u.mgd.uapsd_max_sp_len <<
10652					     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
10653			} else {
10654				qos_info = 0;
10655			}
10656
10657			ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
10658			put_unaligned_le16(capab, capab_pos);
10659		}
10660
10661		ieee80211_fragment_element(skb, subelem_len,
10662					   IEEE80211_MLE_SUBELEM_FRAGMENT);
10663	}
10664
10665	ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT);
10666
10667	info = IEEE80211_SKB_CB(skb);
10668	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
10669
10670	return skb;
10671}
10672
10673int ieee80211_mgd_assoc_ml_reconf(struct ieee80211_sub_if_data *sdata,
10674				  struct cfg80211_ml_reconf_req *req)
10675{
10676	struct ieee80211_local *local = sdata->local;
10677	struct ieee80211_mgd_assoc_data *data = NULL;
10678	struct sta_info *sta;
10679	struct sk_buff *skb;
10680	u16 added_links, new_valid_links;
10681	int link_id, err;
10682
10683	if (!ieee80211_vif_is_mld(&sdata->vif) ||
10684	    !(sdata->vif.cfg.mld_capa_op &
10685	      IEEE80211_MLD_CAP_OP_LINK_RECONF_SUPPORT))
10686		return -EINVAL;
10687
10688	/* No support for concurrent ML reconfiguration operation */
10689	if (sdata->u.mgd.reconf.added_links ||
10690	    sdata->u.mgd.reconf.removed_links)
10691		return -EBUSY;
10692
10693	added_links = 0;
10694	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10695		if (!req->add_links[link_id].bss)
10696			continue;
10697
10698		added_links |= BIT(link_id);
10699	}
10700
10701	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
10702	if (WARN_ON(!sta))
10703		return -ENOLINK;
10704
10705	/* Adding links to the set of valid link is done only after a successful
10706	 * ML reconfiguration frame exchange. Here prepare the data for the ML
10707	 * reconfiguration frame construction and allocate the required
10708	 * resources
10709	 */
10710	if (added_links) {
10711		bool uapsd_supported;
10712
10713		data = kzalloc(sizeof(*data), GFP_KERNEL);
10714		if (!data)
10715			return -ENOMEM;
10716
10717		data->assoc_link_id = -1;
10718		data->wmm = true;
10719
10720		uapsd_supported = true;
10721		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
10722		     link_id++) {
10723			struct ieee80211_supported_band *sband;
10724			struct cfg80211_bss *link_cbss =
10725				req->add_links[link_id].bss;
10726			struct ieee80211_bss *bss;
10727
10728			if (!link_cbss)
10729				continue;
10730
10731			bss = (void *)link_cbss->priv;
10732
10733			if (!bss->wmm_used) {
10734				err = -EINVAL;
10735				goto err_free;
10736			}
10737
10738			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
10739				err = -EINVAL;
10740				goto err_free;
10741			}
10742
10743			eth_random_addr(data->link[link_id].addr);
10744			data->link[link_id].conn =
10745				ieee80211_conn_settings_unlimited;
10746			sband =
10747				local->hw.wiphy->bands[link_cbss->channel->band];
10748
10749			ieee80211_determine_our_sta_mode(sdata, sband,
10750							 NULL, true, link_id,
10751							 &data->link[link_id].conn);
10752
10753			data->link[link_id].bss = link_cbss;
10754			data->link[link_id].elems =
10755				(u8 *)req->add_links[link_id].elems;
10756			data->link[link_id].elems_len =
10757				req->add_links[link_id].elems_len;
10758
10759			if (!bss->uapsd_supported)
10760				uapsd_supported = false;
10761
10762			if (data->link[link_id].conn.mode <
10763			    IEEE80211_CONN_MODE_EHT) {
10764				err = -EINVAL;
10765				goto err_free;
10766			}
10767
10768			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, data,
10769							       link_id);
10770			if (err) {
10771				err = -EINVAL;
10772				goto err_free;
10773			}
10774		}
10775
10776		/* Require U-APSD support if we enabled it */
10777		if (sdata->u.mgd.flags & IEEE80211_STA_UAPSD_ENABLED &&
10778		    !uapsd_supported) {
10779			err = -EINVAL;
10780			sdata_info(sdata, "U-APSD on but not available on (all) new links\n");
10781			goto err_free;
10782		}
10783
10784		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
10785		     link_id++) {
10786			if (!data->link[link_id].bss)
10787				continue;
10788
10789			/* only used to verify the mode, nothing is allocated */
10790			err = ieee80211_prep_channel(sdata, NULL, link_id,
10791						     data->link[link_id].bss,
10792						     true,
10793						     &data->link[link_id].conn,
10794						     sdata->u.mgd.userspace_selectors);
10795			if (err)
10796				goto err_free;
10797		}
10798	}
10799
10800	/* link removal is done before the ML reconfiguration frame exchange so
10801	 * that these links will not be used between their removal by the AP MLD
10802	 * and before the station got the ML reconfiguration response. Based on
10803	 * Section 35.3.6.4 in Draft P802.11be_D7.0 the AP MLD should accept the
10804	 * link removal request.
10805	 */
10806	if (req->rem_links) {
10807		u16 new_active_links =
10808			sdata->vif.active_links & ~req->rem_links;
10809
10810		new_valid_links = sdata->vif.valid_links & ~req->rem_links;
10811
10812		/* Should not be left with no valid links to perform the
10813		 * ML reconfiguration
10814		 */
10815		if (!new_valid_links ||
10816		    !(new_valid_links & ~sdata->vif.dormant_links)) {
10817			sdata_info(sdata, "mlo: reconf: no valid links\n");
10818			err = -EINVAL;
10819			goto err_free;
10820		}
10821
10822		if (new_active_links != sdata->vif.active_links) {
10823			if (!new_active_links)
10824				new_active_links =
10825					BIT(__ffs(new_valid_links &
10826						  ~sdata->vif.dormant_links));
10827
10828			err = ieee80211_set_active_links(&sdata->vif,
10829							 new_active_links);
10830			if (err) {
10831				sdata_info(sdata,
10832					   "mlo: reconf: failed set active links\n");
10833				goto err_free;
10834			}
10835		}
10836	}
10837
10838	/* Build the SKB before the link removal as the construction of the
10839	 * station info for removed links requires the local address.
10840	 * Invalidate the removed links, so that the transmission of the ML
10841	 * reconfiguration request frame would not be done using them, as the AP
10842	 * is expected to send the ML reconfiguration response frame on the link
10843	 * on which the request was received.
10844	 */
10845	skb = ieee80211_build_ml_reconf_req(sdata, data, req->rem_links,
10846					    cpu_to_le16(req->ext_mld_capa_ops));
10847	if (!skb) {
10848		err = -ENOMEM;
10849		goto err_free;
10850	}
10851
10852	if (req->rem_links) {
10853		u16 new_dormant_links =
10854			sdata->vif.dormant_links & ~req->rem_links;
10855
10856		err = ieee80211_vif_set_links(sdata, new_valid_links,
10857					      new_dormant_links);
10858		if (err) {
10859			sdata_info(sdata,
10860				   "mlo: reconf: failed set valid links\n");
10861			kfree_skb(skb);
10862			goto err_free;
10863		}
10864
10865		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
10866		     link_id++) {
10867			if (!(req->rem_links & BIT(link_id)))
10868				continue;
10869
10870			ieee80211_sta_remove_link(sta, link_id);
10871		}
10872
10873		/* notify the driver and upper layers */
10874		ieee80211_vif_cfg_change_notify(sdata,
10875						BSS_CHANGED_MLD_VALID_LINKS);
10876		cfg80211_links_removed(sdata->dev, req->rem_links);
10877	}
10878
10879	sdata_info(sdata, "mlo: reconf: adding=0x%x, removed=0x%x\n",
10880		   added_links, req->rem_links);
10881
10882	ieee80211_tx_skb(sdata, skb);
10883
10884	sdata->u.mgd.reconf.added_links = added_links;
10885	sdata->u.mgd.reconf.add_links_data = data;
10886	sdata->u.mgd.reconf.removed_links = req->rem_links;
10887	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
10888				 &sdata->u.mgd.reconf.wk,
10889				 IEEE80211_ASSOC_TIMEOUT_SHORT);
10890	return 0;
10891
10892 err_free:
10893	kfree(data);
10894	return err;
10895}
10896
10897static bool ieee80211_mgd_epcs_supp(struct ieee80211_sub_if_data *sdata)
10898{
10899	unsigned long valid_links = sdata->vif.valid_links;
10900	u8 link_id;
10901
10902	lockdep_assert_wiphy(sdata->local->hw.wiphy);
10903
10904	if (!ieee80211_vif_is_mld(&sdata->vif))
10905		return false;
10906
10907	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
10908		struct ieee80211_bss_conf *bss_conf =
10909			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
10910
10911		if (WARN_ON(!bss_conf) || !bss_conf->epcs_support)
10912			return false;
10913	}
10914
10915	return true;
10916}
10917
10918int ieee80211_mgd_set_epcs(struct ieee80211_sub_if_data *sdata, bool enable)
10919{
10920	struct ieee80211_local *local = sdata->local;
10921	struct ieee80211_mgmt *mgmt;
10922	struct sk_buff *skb;
10923	int frame_len = offsetofend(struct ieee80211_mgmt,
10924				    u.action.u.epcs) + (enable ? 1 : 0);
10925
10926	if (!ieee80211_mgd_epcs_supp(sdata))
10927		return -EINVAL;
10928
10929	if (sdata->u.mgd.epcs.enabled == enable &&
10930	    !sdata->u.mgd.epcs.dialog_token)
10931		return 0;
10932
10933	/* Do not allow enabling EPCS if the AP didn't respond yet.
10934	 * However, allow disabling EPCS in such a case.
10935	 */
10936	if (sdata->u.mgd.epcs.dialog_token && enable)
10937		return -EALREADY;
10938
10939	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
10940	if (!skb)
10941		return -ENOBUFS;
10942
10943	skb_reserve(skb, local->hw.extra_tx_headroom);
10944	mgmt = skb_put_zero(skb, frame_len);
10945	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
10946					  IEEE80211_STYPE_ACTION);
10947	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
10948	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
10949	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
10950
10951	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
10952	if (enable) {
10953		u8 *pos = mgmt->u.action.u.epcs.variable;
10954
10955		mgmt->u.action.u.epcs.action_code =
10956			WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_REQ;
10957
10958		*pos = ++sdata->u.mgd.dialog_token_alloc;
10959		sdata->u.mgd.epcs.dialog_token = *pos;
10960	} else {
10961		mgmt->u.action.u.epcs.action_code =
10962			WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN;
10963
10964		ieee80211_epcs_teardown(sdata);
10965		ieee80211_epcs_changed(sdata, false);
10966	}
10967
10968	ieee80211_tx_skb(sdata, skb);
10969	return 0;
10970}
10971
10972static void ieee80211_ml_epcs(struct ieee80211_sub_if_data *sdata,
10973			      struct ieee802_11_elems *elems)
10974{
10975	const struct element *sub;
10976	size_t scratch_len = elems->ml_epcs_len;
10977	u8 *scratch __free(kfree) = kzalloc(scratch_len, GFP_KERNEL);
10978
10979	lockdep_assert_wiphy(sdata->local->hw.wiphy);
10980
10981	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_epcs)
10982		return;
10983
10984	if (WARN_ON(!scratch))
10985		return;
10986
10987	/* Directly parse the sub elements as the common information doesn't
10988	 * hold any useful information.
10989	 */
10990	for_each_mle_subelement(sub, (const u8 *)elems->ml_epcs,
10991				elems->ml_epcs_len) {
10992		struct ieee802_11_elems *link_elems __free(kfree) = NULL;
10993		struct ieee80211_link_data *link;
10994		u8 *pos = (void *)sub->data;
10995		u16 control;
10996		ssize_t len;
10997		u8 link_id;
10998
10999		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
11000			continue;
11001
11002		if (sub->datalen < sizeof(control))
11003			break;
11004
11005		control = get_unaligned_le16(pos);
11006		link_id = control & IEEE80211_MLE_STA_EPCS_CONTROL_LINK_ID;
11007
11008		link = sdata_dereference(sdata->link[link_id], sdata);
11009		if (!link)
11010			continue;
11011
11012		len = cfg80211_defragment_element(sub, (u8 *)elems->ml_epcs,
11013						  elems->ml_epcs_len,
11014						  scratch, scratch_len,
11015						  IEEE80211_MLE_SUBELEM_FRAGMENT);
11016		if (len < (ssize_t)sizeof(control))
11017			continue;
11018
11019		pos = scratch + sizeof(control);
11020		len -= sizeof(control);
11021
11022		link_elems = ieee802_11_parse_elems(pos, len,
11023						    IEEE80211_FTYPE_MGMT |
11024						    IEEE80211_STYPE_ACTION,
11025						    NULL);
11026		if (!link_elems)
11027			continue;
11028
11029		if (ieee80211_sta_wmm_params(sdata->local, link,
11030					     link_elems->wmm_param,
11031					     link_elems->wmm_param_len,
11032					     link_elems->mu_edca_param_set))
11033			ieee80211_link_info_change_notify(sdata, link,
11034							  BSS_CHANGED_QOS);
11035	}
11036}
11037
11038void ieee80211_process_epcs_ena_resp(struct ieee80211_sub_if_data *sdata,
11039				     struct ieee80211_mgmt *mgmt, size_t len)
11040{
11041	struct ieee802_11_elems *elems __free(kfree) = NULL;
11042	size_t ies_len;
11043	u16 status_code;
11044	u8 *pos, dialog_token;
11045
11046	if (!ieee80211_mgd_epcs_supp(sdata))
11047		return;
11048
11049	/* Handle dialog token and status code */
11050	pos = mgmt->u.action.u.epcs.variable;
11051	dialog_token = *pos;
11052	status_code = get_unaligned_le16(pos + 1);
11053
11054	/* An EPCS enable response with dialog token == 0 is an unsolicited
11055	 * notification from the AP MLD. In such a case, EPCS should already be
11056	 * enabled and status must be success
11057	 */
11058	if (!dialog_token &&
11059	    (!sdata->u.mgd.epcs.enabled ||
11060	     status_code != WLAN_STATUS_SUCCESS))
11061		return;
11062
11063	if (sdata->u.mgd.epcs.dialog_token != dialog_token)
11064		return;
11065
11066	sdata->u.mgd.epcs.dialog_token = 0;
11067
11068	if (status_code != WLAN_STATUS_SUCCESS)
11069		return;
11070
11071	pos += IEEE80211_EPCS_ENA_RESP_BODY_LEN;
11072	ies_len = len - offsetof(struct ieee80211_mgmt,
11073				 u.action.u.epcs.variable) -
11074		IEEE80211_EPCS_ENA_RESP_BODY_LEN;
11075
11076	elems = ieee802_11_parse_elems(pos, ies_len,
11077				       IEEE80211_FTYPE_MGMT |
11078				       IEEE80211_STYPE_ACTION,
11079				       NULL);
11080	if (!elems)
11081		return;
11082
11083	ieee80211_ml_epcs(sdata, elems);
11084	ieee80211_epcs_changed(sdata, true);
11085}
11086
11087void ieee80211_process_epcs_teardown(struct ieee80211_sub_if_data *sdata,
11088				     struct ieee80211_mgmt *mgmt, size_t len)
11089{
11090	if (!ieee80211_vif_is_mld(&sdata->vif) ||
11091	    !sdata->u.mgd.epcs.enabled)
11092		return;
11093
11094	ieee80211_epcs_teardown(sdata);
11095	ieee80211_epcs_changed(sdata, false);
11096}
~/ Read-only file preview.