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