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