xref: /linux/net/mac80211/mlme.c (revision f7c595c9d9f4cce9ec335f0d3c5d875bb547f9d5)
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 (WARN_ON(!ap_sta))
3984 		return;
3985 
3986 	if (WARN_ON_ONCE(tx && !frame_buf))
3987 		return;
3988 
3989 	if (WARN_ON(!ifmgd->associated))
3990 		return;
3991 
3992 	ieee80211_stop_poll(sdata);
3993 
3994 	ifmgd->associated = false;
3995 
3996 	if (tx) {
3997 		bool tx_link_found = false;
3998 
3999 		for (link_id = 0;
4000 		     link_id < ARRAY_SIZE(sdata->link);
4001 		     link_id++) {
4002 			struct ieee80211_link_data *link;
4003 
4004 			if (!ieee80211_vif_link_active(&sdata->vif, link_id))
4005 				continue;
4006 
4007 			link = sdata_dereference(sdata->link[link_id], sdata);
4008 			if (WARN_ON_ONCE(!link))
4009 				continue;
4010 
4011 			if (link->u.mgd.csa.blocked_tx)
4012 				continue;
4013 
4014 			tx_link_found = true;
4015 			break;
4016 		}
4017 
4018 		tx = tx_link_found;
4019 	}
4020 
4021 	/* other links will be destroyed */
4022 	sdata->deflink.conf->bss = NULL;
4023 	sdata->deflink.conf->epcs_support = false;
4024 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
4025 
4026 	netif_carrier_off(sdata->dev);
4027 
4028 	/*
4029 	 * if we want to get out of ps before disassoc (why?) we have
4030 	 * to do it before sending disassoc, as otherwise the null-packet
4031 	 * won't be valid.
4032 	 */
4033 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4034 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4035 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
4036 	}
4037 	local->ps_sdata = NULL;
4038 
4039 	/* disable per-vif ps */
4040 	ieee80211_recalc_ps_vif(sdata);
4041 
4042 	/* make sure ongoing transmission finishes */
4043 	synchronize_net();
4044 
4045 	/*
4046 	 * drop any frame before deauth/disassoc, this can be data or
4047 	 * management frame. Since we are disconnecting, we should not
4048 	 * insist sending these frames which can take time and delay
4049 	 * the disconnection and possible the roaming.
4050 	 */
4051 	ieee80211_flush_queues(local, sdata, true);
4052 
4053 	if (tx) {
4054 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
4055 
4056 		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
4057 					       sdata->vif.cfg.ap_addr, stype,
4058 					       reason, true, frame_buf);
4059 
4060 		/* flush out frame - make sure the deauth was actually sent */
4061 		ieee80211_flush_queues(local, sdata, false);
4062 
4063 		drv_mgd_complete_tx(sdata->local, sdata, &info);
4064 	} else if (frame_buf) {
4065 		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
4066 					       sdata->vif.cfg.ap_addr, stype,
4067 					       reason, false, frame_buf);
4068 	}
4069 
4070 	/* clear AP addr only after building the needed mgmt frames */
4071 	eth_zero_addr(sdata->deflink.u.mgd.bssid);
4072 	eth_zero_addr(sdata->vif.cfg.ap_addr);
4073 
4074 	sdata->vif.cfg.ssid_len = 0;
4075 
4076 	/* Remove TDLS peers */
4077 	__sta_info_flush(sdata, false, -1, ap_sta);
4078 
4079 	if (sdata->vif.driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC) {
4080 		/* Only move the AP state */
4081 		sta_info_move_state(ap_sta, IEEE80211_STA_NONE);
4082 	} else {
4083 		/* Remove AP peer */
4084 		sta_info_flush(sdata, -1);
4085 	}
4086 
4087 	/* finally reset all BSS / config parameters */
4088 	if (!ieee80211_vif_is_mld(&sdata->vif))
4089 		changed |= ieee80211_reset_erp_info(sdata);
4090 
4091 	ieee80211_led_assoc(local, 0);
4092 	changed |= BSS_CHANGED_ASSOC;
4093 	sdata->vif.cfg.assoc = false;
4094 
4095 	sdata->deflink.u.mgd.p2p_noa_index = -1;
4096 	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
4097 	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
4098 
4099 	/* on the next assoc, re-program HT/VHT parameters */
4100 	memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
4101 	memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
4102 	memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
4103 	memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
4104 
4105 	/*
4106 	 * reset MU-MIMO ownership and group data in default link,
4107 	 * if used, other links are destroyed
4108 	 */
4109 	memset(sdata->vif.bss_conf.mu_group.membership, 0,
4110 	       sizeof(sdata->vif.bss_conf.mu_group.membership));
4111 	memset(sdata->vif.bss_conf.mu_group.position, 0,
4112 	       sizeof(sdata->vif.bss_conf.mu_group.position));
4113 	if (!ieee80211_vif_is_mld(&sdata->vif))
4114 		changed |= BSS_CHANGED_MU_GROUPS;
4115 	sdata->vif.bss_conf.mu_mimo_owner = false;
4116 
4117 	sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
4118 
4119 	timer_delete_sync(&local->dynamic_ps_timer);
4120 	wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
4121 
4122 	/* Disable ARP filtering */
4123 	if (sdata->vif.cfg.arp_addr_cnt)
4124 		changed |= BSS_CHANGED_ARP_FILTER;
4125 
4126 	sdata->vif.bss_conf.qos = false;
4127 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
4128 		changed |= BSS_CHANGED_QOS;
4129 		/* The BSSID (not really interesting) and HT changed */
4130 		changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
4131 		ieee80211_bss_info_change_notify(sdata, changed);
4132 	} else {
4133 		ieee80211_vif_cfg_change_notify(sdata, changed);
4134 	}
4135 
4136 	if (sdata->vif.driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC) {
4137 		/*
4138 		 * After notifying the driver about the disassoc,
4139 		 * remove the ap sta.
4140 		 */
4141 		sta_info_flush(sdata, -1);
4142 	}
4143 
4144 	/* disassociated - set to defaults now */
4145 	ieee80211_set_wmm_default(&sdata->deflink, false, false);
4146 
4147 	timer_delete_sync(&sdata->u.mgd.conn_mon_timer);
4148 	timer_delete_sync(&sdata->u.mgd.bcn_mon_timer);
4149 	timer_delete_sync(&sdata->u.mgd.timer);
4150 
4151 	sdata->vif.bss_conf.dtim_period = 0;
4152 	sdata->vif.bss_conf.beacon_rate = NULL;
4153 
4154 	sdata->deflink.u.mgd.have_beacon = false;
4155 	sdata->deflink.u.mgd.tracking_signal_avg = false;
4156 	sdata->deflink.u.mgd.disable_wmm_tracking = false;
4157 
4158 	ifmgd->flags = 0;
4159 
4160 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
4161 		struct ieee80211_link_data *link;
4162 
4163 		link = sdata_dereference(sdata->link[link_id], sdata);
4164 		if (!link)
4165 			continue;
4166 		ieee80211_link_release_channel(link);
4167 	}
4168 
4169 	sdata->vif.bss_conf.csa_active = false;
4170 	sdata->deflink.u.mgd.csa.blocked_tx = false;
4171 	sdata->deflink.u.mgd.csa.waiting_bcn = false;
4172 	sdata->deflink.u.mgd.csa.ignored_same_chan = false;
4173 	ieee80211_vif_unblock_queues_csa(sdata);
4174 
4175 	/* existing TX TSPEC sessions no longer exist */
4176 	memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
4177 	wiphy_delayed_work_cancel(local->hw.wiphy, &ifmgd->tx_tspec_wk);
4178 
4179 	sdata->vif.bss_conf.power_type = IEEE80211_REG_UNSET_AP;
4180 	sdata->vif.bss_conf.pwr_reduction = 0;
4181 	ieee80211_clear_tpe(&sdata->vif.bss_conf.tpe);
4182 
4183 	sdata->vif.cfg.eml_cap = 0;
4184 	sdata->vif.cfg.eml_med_sync_delay = 0;
4185 	sdata->vif.cfg.mld_capa_op = 0;
4186 
4187 	memset(&sdata->u.mgd.ttlm_info, 0,
4188 	       sizeof(sdata->u.mgd.ttlm_info));
4189 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy, &ifmgd->ttlm_work);
4190 
4191 	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
4192 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
4193 				  &ifmgd->neg_ttlm_timeout_work);
4194 
4195 	sdata->u.mgd.removed_links = 0;
4196 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
4197 				  &sdata->u.mgd.ml_reconf_work);
4198 
4199 	wiphy_work_cancel(sdata->local->hw.wiphy,
4200 			  &ifmgd->teardown_ttlm_work);
4201 
4202 	/* if disconnection happens in the middle of the ML reconfiguration
4203 	 * flow, cfg80211 must called to release the BSS references obtained
4204 	 * when the flow started.
4205 	 */
4206 	ieee80211_ml_reconf_reset(sdata);
4207 
4208 	ieee80211_vif_set_links(sdata, 0, 0);
4209 
4210 	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
4211 
4212 	ifmgd->epcs.enabled = false;
4213 	ifmgd->epcs.dialog_token = 0;
4214 
4215 	memset(ifmgd->userspace_selectors, 0,
4216 	       sizeof(ifmgd->userspace_selectors));
4217 }
4218 
4219 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
4220 {
4221 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4222 	struct ieee80211_local *local = sdata->local;
4223 
4224 	lockdep_assert_wiphy(local->hw.wiphy);
4225 
4226 	if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
4227 		return;
4228 
4229 	__ieee80211_stop_poll(sdata);
4230 
4231 	ieee80211_recalc_ps(local);
4232 
4233 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4234 		return;
4235 
4236 	/*
4237 	 * We've received a probe response, but are not sure whether
4238 	 * we have or will be receiving any beacons or data, so let's
4239 	 * schedule the timers again, just in case.
4240 	 */
4241 	ieee80211_sta_reset_beacon_monitor(sdata);
4242 
4243 	mod_timer(&ifmgd->conn_mon_timer,
4244 		  round_jiffies_up(jiffies +
4245 				   IEEE80211_CONNECTION_IDLE_TIME));
4246 }
4247 
4248 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
4249 					   struct ieee80211_hdr *hdr,
4250 					   u16 tx_time)
4251 {
4252 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4253 	u16 tid;
4254 	int ac;
4255 	struct ieee80211_sta_tx_tspec *tx_tspec;
4256 	unsigned long now = jiffies;
4257 
4258 	if (!ieee80211_is_data_qos(hdr->frame_control))
4259 		return;
4260 
4261 	tid = ieee80211_get_tid(hdr);
4262 	ac = ieee80211_ac_from_tid(tid);
4263 	tx_tspec = &ifmgd->tx_tspec[ac];
4264 
4265 	if (likely(!tx_tspec->admitted_time))
4266 		return;
4267 
4268 	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
4269 		tx_tspec->consumed_tx_time = 0;
4270 		tx_tspec->time_slice_start = now;
4271 
4272 		if (tx_tspec->downgraded) {
4273 			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
4274 			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
4275 						 &ifmgd->tx_tspec_wk, 0);
4276 		}
4277 	}
4278 
4279 	if (tx_tspec->downgraded)
4280 		return;
4281 
4282 	tx_tspec->consumed_tx_time += tx_time;
4283 
4284 	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
4285 		tx_tspec->downgraded = true;
4286 		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
4287 		wiphy_delayed_work_queue(sdata->local->hw.wiphy,
4288 					 &ifmgd->tx_tspec_wk, 0);
4289 	}
4290 }
4291 
4292 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
4293 			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
4294 {
4295 	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
4296 
4297 	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
4298 	    !sdata->u.mgd.probe_send_count)
4299 		return;
4300 
4301 	if (ack)
4302 		sdata->u.mgd.probe_send_count = 0;
4303 	else
4304 		sdata->u.mgd.nullfunc_failed = true;
4305 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
4306 }
4307 
4308 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
4309 					  const u8 *src, const u8 *dst,
4310 					  const u8 *ssid, size_t ssid_len,
4311 					  struct ieee80211_channel *channel)
4312 {
4313 	struct sk_buff *skb;
4314 
4315 	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
4316 					ssid, ssid_len, NULL, 0,
4317 					IEEE80211_PROBE_FLAG_DIRECTED);
4318 	if (skb)
4319 		ieee80211_tx_skb(sdata, skb);
4320 }
4321 
4322 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
4323 {
4324 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4325 	u8 *dst = sdata->vif.cfg.ap_addr;
4326 	u8 unicast_limit = max(1, max_probe_tries - 3);
4327 	struct sta_info *sta;
4328 
4329 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4330 
4331 	if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif)))
4332 		return;
4333 
4334 	/*
4335 	 * Try sending broadcast probe requests for the last three
4336 	 * probe requests after the first ones failed since some
4337 	 * buggy APs only support broadcast probe requests.
4338 	 */
4339 	if (ifmgd->probe_send_count >= unicast_limit)
4340 		dst = NULL;
4341 
4342 	/*
4343 	 * When the hardware reports an accurate Tx ACK status, it's
4344 	 * better to send a nullfunc frame instead of a probe request,
4345 	 * as it will kick us off the AP quickly if we aren't associated
4346 	 * anymore. The timeout will be reset if the frame is ACKed by
4347 	 * the AP.
4348 	 */
4349 	ifmgd->probe_send_count++;
4350 
4351 	if (dst) {
4352 		sta = sta_info_get(sdata, dst);
4353 		if (!WARN_ON(!sta))
4354 			ieee80211_check_fast_rx(sta);
4355 	}
4356 
4357 	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
4358 		ifmgd->nullfunc_failed = false;
4359 		ieee80211_send_nullfunc(sdata->local, sdata, false);
4360 	} else {
4361 		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
4362 					      sdata->vif.cfg.ssid,
4363 					      sdata->vif.cfg.ssid_len,
4364 					      sdata->deflink.conf->bss->channel);
4365 	}
4366 
4367 	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
4368 	run_again(sdata, ifmgd->probe_timeout);
4369 }
4370 
4371 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
4372 				   bool beacon)
4373 {
4374 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4375 	bool already = false;
4376 
4377 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4378 
4379 	if (WARN_ON_ONCE(ieee80211_vif_is_mld(&sdata->vif)))
4380 		return;
4381 
4382 	if (!ieee80211_sdata_running(sdata))
4383 		return;
4384 
4385 	if (!ifmgd->associated)
4386 		return;
4387 
4388 	if (sdata->local->tmp_channel || sdata->local->scanning)
4389 		return;
4390 
4391 	if (sdata->local->suspending) {
4392 		/* reschedule after resume */
4393 		ieee80211_reset_ap_probe(sdata);
4394 		return;
4395 	}
4396 
4397 	if (beacon) {
4398 		mlme_dbg_ratelimited(sdata,
4399 				     "detected beacon loss from AP (missed %d beacons) - probing\n",
4400 				     beacon_loss_count);
4401 
4402 		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
4403 	}
4404 
4405 	/*
4406 	 * The driver/our work has already reported this event or the
4407 	 * connection monitoring has kicked in and we have already sent
4408 	 * a probe request. Or maybe the AP died and the driver keeps
4409 	 * reporting until we disassociate...
4410 	 *
4411 	 * In either case we have to ignore the current call to this
4412 	 * function (except for setting the correct probe reason bit)
4413 	 * because otherwise we would reset the timer every time and
4414 	 * never check whether we received a probe response!
4415 	 */
4416 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
4417 		already = true;
4418 
4419 	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
4420 
4421 	if (already)
4422 		return;
4423 
4424 	ieee80211_recalc_ps(sdata->local);
4425 
4426 	ifmgd->probe_send_count = 0;
4427 	ieee80211_mgd_probe_ap_send(sdata);
4428 }
4429 
4430 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
4431 					  struct ieee80211_vif *vif)
4432 {
4433 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4434 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4435 	struct cfg80211_bss *cbss;
4436 	struct sk_buff *skb;
4437 	const struct element *ssid;
4438 	int ssid_len;
4439 
4440 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4441 
4442 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
4443 		    ieee80211_vif_is_mld(&sdata->vif)))
4444 		return NULL;
4445 
4446 	if (ifmgd->associated)
4447 		cbss = sdata->deflink.conf->bss;
4448 	else if (ifmgd->auth_data)
4449 		cbss = ifmgd->auth_data->bss;
4450 	else if (ifmgd->assoc_data && ifmgd->assoc_data->link[0].bss)
4451 		cbss = ifmgd->assoc_data->link[0].bss;
4452 	else
4453 		return NULL;
4454 
4455 	rcu_read_lock();
4456 	ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
4457 	if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
4458 		      "invalid SSID element (len=%d)",
4459 		      ssid ? ssid->datalen : -1))
4460 		ssid_len = 0;
4461 	else
4462 		ssid_len = ssid->datalen;
4463 
4464 	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
4465 					(u32) -1, cbss->channel,
4466 					ssid->data, ssid_len,
4467 					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
4468 	rcu_read_unlock();
4469 
4470 	return skb;
4471 }
4472 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
4473 
4474 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
4475 					const u8 *buf, size_t len, bool tx,
4476 					u16 reason, bool reconnect)
4477 {
4478 	struct ieee80211_event event = {
4479 		.type = MLME_EVENT,
4480 		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
4481 		.u.mlme.reason = reason,
4482 	};
4483 
4484 	if (tx)
4485 		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
4486 	else
4487 		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
4488 
4489 	drv_event_callback(sdata->local, sdata, &event);
4490 }
4491 
4492 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
4493 {
4494 	struct ieee80211_local *local = sdata->local;
4495 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4496 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4497 
4498 	lockdep_assert_wiphy(local->hw.wiphy);
4499 
4500 	if (!ifmgd->associated)
4501 		return;
4502 
4503 	if (!ifmgd->driver_disconnect) {
4504 		unsigned int link_id;
4505 
4506 		/*
4507 		 * AP is probably out of range (or not reachable for another
4508 		 * reason) so remove the bss structs for that AP. In the case
4509 		 * of multi-link, it's not clear that all of them really are
4510 		 * out of range, but if they weren't the driver likely would
4511 		 * have switched to just have a single link active?
4512 		 */
4513 		for (link_id = 0;
4514 		     link_id < ARRAY_SIZE(sdata->link);
4515 		     link_id++) {
4516 			struct ieee80211_link_data *link;
4517 
4518 			link = sdata_dereference(sdata->link[link_id], sdata);
4519 			if (!link || !link->conf->bss)
4520 				continue;
4521 			cfg80211_unlink_bss(local->hw.wiphy, link->conf->bss);
4522 			link->conf->bss = NULL;
4523 		}
4524 	}
4525 
4526 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4527 			       ifmgd->driver_disconnect ?
4528 					WLAN_REASON_DEAUTH_LEAVING :
4529 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4530 			       true, frame_buf);
4531 	/* the other links will be destroyed */
4532 	sdata->vif.bss_conf.csa_active = false;
4533 	sdata->deflink.u.mgd.csa.waiting_bcn = false;
4534 	sdata->deflink.u.mgd.csa.blocked_tx = false;
4535 	ieee80211_vif_unblock_queues_csa(sdata);
4536 
4537 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4538 				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4539 				    ifmgd->reconnect);
4540 	ifmgd->reconnect = false;
4541 }
4542 
4543 static void ieee80211_beacon_connection_loss_work(struct wiphy *wiphy,
4544 						  struct wiphy_work *work)
4545 {
4546 	struct ieee80211_sub_if_data *sdata =
4547 		container_of(work, struct ieee80211_sub_if_data,
4548 			     u.mgd.beacon_connection_loss_work);
4549 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4550 
4551 	if (ifmgd->connection_loss) {
4552 		sdata_info(sdata, "Connection to AP %pM lost\n",
4553 			   sdata->vif.cfg.ap_addr);
4554 		__ieee80211_disconnect(sdata);
4555 		ifmgd->connection_loss = false;
4556 	} else if (ifmgd->driver_disconnect) {
4557 		sdata_info(sdata,
4558 			   "Driver requested disconnection from AP %pM\n",
4559 			   sdata->vif.cfg.ap_addr);
4560 		__ieee80211_disconnect(sdata);
4561 		ifmgd->driver_disconnect = false;
4562 	} else {
4563 		if (ifmgd->associated)
4564 			sdata->deflink.u.mgd.beacon_loss_count++;
4565 		ieee80211_mgd_probe_ap(sdata, true);
4566 	}
4567 }
4568 
4569 static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
4570 					       struct wiphy_work *work)
4571 {
4572 	struct ieee80211_sub_if_data *sdata =
4573 		container_of(work, struct ieee80211_sub_if_data,
4574 			     u.mgd.csa_connection_drop_work);
4575 
4576 	__ieee80211_disconnect(sdata);
4577 }
4578 
4579 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
4580 {
4581 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4582 	struct ieee80211_hw *hw = &sdata->local->hw;
4583 
4584 	trace_api_beacon_loss(sdata);
4585 
4586 	sdata->u.mgd.connection_loss = false;
4587 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
4588 }
4589 EXPORT_SYMBOL(ieee80211_beacon_loss);
4590 
4591 void ieee80211_connection_loss(struct ieee80211_vif *vif)
4592 {
4593 	struct ieee80211_sub_if_data *sdata;
4594 	struct ieee80211_hw *hw;
4595 
4596 	KUNIT_STATIC_STUB_REDIRECT(ieee80211_connection_loss, vif);
4597 
4598 	sdata = vif_to_sdata(vif);
4599 	hw = &sdata->local->hw;
4600 
4601 	trace_api_connection_loss(sdata);
4602 
4603 	sdata->u.mgd.connection_loss = true;
4604 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
4605 }
4606 EXPORT_SYMBOL(ieee80211_connection_loss);
4607 
4608 void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
4609 {
4610 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4611 	struct ieee80211_hw *hw = &sdata->local->hw;
4612 
4613 	trace_api_disconnect(sdata, reconnect);
4614 
4615 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4616 		return;
4617 
4618 	sdata->u.mgd.driver_disconnect = true;
4619 	sdata->u.mgd.reconnect = reconnect;
4620 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
4621 }
4622 EXPORT_SYMBOL(ieee80211_disconnect);
4623 
4624 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
4625 					bool assoc)
4626 {
4627 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
4628 
4629 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4630 
4631 	sdata->u.mgd.auth_data = NULL;
4632 
4633 	if (!assoc) {
4634 		/*
4635 		 * we are not authenticated yet, the only timer that could be
4636 		 * running is the timeout for the authentication response which
4637 		 * which is not relevant anymore.
4638 		 */
4639 		timer_delete_sync(&sdata->u.mgd.timer);
4640 		sta_info_destroy_addr(sdata, auth_data->ap_addr);
4641 
4642 		/* other links are destroyed */
4643 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
4644 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
4645 						  BSS_CHANGED_BSSID);
4646 		sdata->u.mgd.flags = 0;
4647 
4648 		ieee80211_link_release_channel(&sdata->deflink);
4649 		ieee80211_vif_set_links(sdata, 0, 0);
4650 	}
4651 
4652 	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
4653 	kfree(auth_data);
4654 }
4655 
4656 enum assoc_status {
4657 	ASSOC_SUCCESS,
4658 	ASSOC_REJECTED,
4659 	ASSOC_TIMEOUT,
4660 	ASSOC_ABANDON,
4661 };
4662 
4663 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
4664 					 enum assoc_status status)
4665 {
4666 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4667 
4668 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4669 
4670 	sdata->u.mgd.assoc_data = NULL;
4671 
4672 	if (status != ASSOC_SUCCESS) {
4673 		/*
4674 		 * we are not associated yet, the only timer that could be
4675 		 * running is the timeout for the association response which
4676 		 * which is not relevant anymore.
4677 		 */
4678 		timer_delete_sync(&sdata->u.mgd.timer);
4679 		sta_info_destroy_addr(sdata, assoc_data->ap_addr);
4680 
4681 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
4682 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
4683 						  BSS_CHANGED_BSSID);
4684 		sdata->u.mgd.flags = 0;
4685 		sdata->vif.bss_conf.mu_mimo_owner = false;
4686 
4687 		if (status != ASSOC_REJECTED) {
4688 			struct cfg80211_assoc_failure data = {
4689 				.timeout = status == ASSOC_TIMEOUT,
4690 			};
4691 			int i;
4692 
4693 			BUILD_BUG_ON(ARRAY_SIZE(data.bss) !=
4694 				     ARRAY_SIZE(assoc_data->link));
4695 
4696 			for (i = 0; i < ARRAY_SIZE(data.bss); i++)
4697 				data.bss[i] = assoc_data->link[i].bss;
4698 
4699 			if (ieee80211_vif_is_mld(&sdata->vif))
4700 				data.ap_mld_addr = assoc_data->ap_addr;
4701 
4702 			cfg80211_assoc_failure(sdata->dev, &data);
4703 		}
4704 
4705 		ieee80211_link_release_channel(&sdata->deflink);
4706 		ieee80211_vif_set_links(sdata, 0, 0);
4707 	}
4708 
4709 	kfree(assoc_data);
4710 }
4711 
4712 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
4713 				     struct ieee80211_mgmt *mgmt, size_t len)
4714 {
4715 	struct ieee80211_local *local = sdata->local;
4716 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
4717 	const struct element *challenge;
4718 	u8 *pos;
4719 	u32 tx_flags = 0;
4720 	struct ieee80211_prep_tx_info info = {
4721 		.subtype = IEEE80211_STYPE_AUTH,
4722 		.link_id = auth_data->link_id,
4723 	};
4724 
4725 	pos = mgmt->u.auth.variable;
4726 	challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
4727 				       len - (pos - (u8 *)mgmt));
4728 	if (!challenge)
4729 		return;
4730 	auth_data->expected_transaction = 4;
4731 	drv_mgd_prepare_tx(sdata->local, sdata, &info);
4732 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4733 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4734 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
4735 	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
4736 			    (void *)challenge,
4737 			    challenge->datalen + sizeof(*challenge),
4738 			    auth_data->ap_addr, auth_data->ap_addr,
4739 			    auth_data->key, auth_data->key_len,
4740 			    auth_data->key_idx, tx_flags);
4741 }
4742 
4743 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata)
4744 {
4745 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4746 	const u8 *ap_addr = ifmgd->auth_data->ap_addr;
4747 	struct sta_info *sta;
4748 
4749 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4750 
4751 	sdata_info(sdata, "authenticated\n");
4752 	ifmgd->auth_data->done = true;
4753 	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
4754 	ifmgd->auth_data->timeout_started = true;
4755 	run_again(sdata, ifmgd->auth_data->timeout);
4756 
4757 	/* move station state to auth */
4758 	sta = sta_info_get(sdata, ap_addr);
4759 	if (!sta) {
4760 		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, ap_addr);
4761 		return false;
4762 	}
4763 	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
4764 		sdata_info(sdata, "failed moving %pM to auth\n", ap_addr);
4765 		return false;
4766 	}
4767 
4768 	return true;
4769 }
4770 
4771 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
4772 				   struct ieee80211_mgmt *mgmt, size_t len)
4773 {
4774 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4775 	u16 auth_alg, auth_transaction, status_code;
4776 	struct ieee80211_event event = {
4777 		.type = MLME_EVENT,
4778 		.u.mlme.data = AUTH_EVENT,
4779 	};
4780 	struct ieee80211_prep_tx_info info = {
4781 		.subtype = IEEE80211_STYPE_AUTH,
4782 	};
4783 	bool sae_need_confirm = false;
4784 
4785 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4786 
4787 	if (len < 24 + 6)
4788 		return;
4789 
4790 	if (!ifmgd->auth_data || ifmgd->auth_data->done)
4791 		return;
4792 
4793 	if (!ether_addr_equal(ifmgd->auth_data->ap_addr, mgmt->bssid))
4794 		return;
4795 
4796 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
4797 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
4798 	status_code = le16_to_cpu(mgmt->u.auth.status_code);
4799 
4800 	info.link_id = ifmgd->auth_data->link_id;
4801 
4802 	if (auth_alg != ifmgd->auth_data->algorithm ||
4803 	    (auth_alg != WLAN_AUTH_SAE &&
4804 	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
4805 	    (auth_alg == WLAN_AUTH_SAE &&
4806 	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
4807 	      auth_transaction > 2))) {
4808 		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
4809 			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
4810 			   auth_transaction,
4811 			   ifmgd->auth_data->expected_transaction);
4812 		goto notify_driver;
4813 	}
4814 
4815 	if (status_code != WLAN_STATUS_SUCCESS) {
4816 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
4817 
4818 		if (auth_alg == WLAN_AUTH_SAE &&
4819 		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
4820 		     (auth_transaction == 1 &&
4821 		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
4822 		       status_code == WLAN_STATUS_SAE_PK)))) {
4823 			/* waiting for userspace now */
4824 			ifmgd->auth_data->waiting = true;
4825 			ifmgd->auth_data->timeout =
4826 				jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
4827 			ifmgd->auth_data->timeout_started = true;
4828 			run_again(sdata, ifmgd->auth_data->timeout);
4829 			if (auth_transaction == 1)
4830 				sae_need_confirm = true;
4831 			goto notify_driver;
4832 		}
4833 
4834 		sdata_info(sdata, "%pM denied authentication (status %d)\n",
4835 			   mgmt->sa, status_code);
4836 		ieee80211_destroy_auth_data(sdata, false);
4837 		event.u.mlme.status = MLME_DENIED;
4838 		event.u.mlme.reason = status_code;
4839 		drv_event_callback(sdata->local, sdata, &event);
4840 		goto notify_driver;
4841 	}
4842 
4843 	switch (ifmgd->auth_data->algorithm) {
4844 	case WLAN_AUTH_OPEN:
4845 	case WLAN_AUTH_LEAP:
4846 	case WLAN_AUTH_FT:
4847 	case WLAN_AUTH_SAE:
4848 	case WLAN_AUTH_FILS_SK:
4849 	case WLAN_AUTH_FILS_SK_PFS:
4850 	case WLAN_AUTH_FILS_PK:
4851 		break;
4852 	case WLAN_AUTH_SHARED_KEY:
4853 		if (ifmgd->auth_data->expected_transaction != 4) {
4854 			ieee80211_auth_challenge(sdata, mgmt, len);
4855 			/* need another frame */
4856 			return;
4857 		}
4858 		break;
4859 	default:
4860 		WARN_ONCE(1, "invalid auth alg %d",
4861 			  ifmgd->auth_data->algorithm);
4862 		goto notify_driver;
4863 	}
4864 
4865 	event.u.mlme.status = MLME_SUCCESS;
4866 	info.success = 1;
4867 	drv_event_callback(sdata->local, sdata, &event);
4868 	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
4869 	    (auth_transaction == 2 &&
4870 	     ifmgd->auth_data->expected_transaction == 2)) {
4871 		if (!ieee80211_mark_sta_auth(sdata))
4872 			return; /* ignore frame -- wait for timeout */
4873 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
4874 		   auth_transaction == 1) {
4875 		sae_need_confirm = true;
4876 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
4877 		   auth_transaction == 2) {
4878 		sdata_info(sdata, "SAE peer confirmed\n");
4879 		ifmgd->auth_data->peer_confirmed = true;
4880 	}
4881 
4882 	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
4883 notify_driver:
4884 	if (!sae_need_confirm)
4885 		drv_mgd_complete_tx(sdata->local, sdata, &info);
4886 }
4887 
4888 #define case_WLAN(type) \
4889 	case WLAN_REASON_##type: return #type
4890 
4891 const char *ieee80211_get_reason_code_string(u16 reason_code)
4892 {
4893 	switch (reason_code) {
4894 	case_WLAN(UNSPECIFIED);
4895 	case_WLAN(PREV_AUTH_NOT_VALID);
4896 	case_WLAN(DEAUTH_LEAVING);
4897 	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
4898 	case_WLAN(DISASSOC_AP_BUSY);
4899 	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
4900 	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
4901 	case_WLAN(DISASSOC_STA_HAS_LEFT);
4902 	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
4903 	case_WLAN(DISASSOC_BAD_POWER);
4904 	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
4905 	case_WLAN(INVALID_IE);
4906 	case_WLAN(MIC_FAILURE);
4907 	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
4908 	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
4909 	case_WLAN(IE_DIFFERENT);
4910 	case_WLAN(INVALID_GROUP_CIPHER);
4911 	case_WLAN(INVALID_PAIRWISE_CIPHER);
4912 	case_WLAN(INVALID_AKMP);
4913 	case_WLAN(UNSUPP_RSN_VERSION);
4914 	case_WLAN(INVALID_RSN_IE_CAP);
4915 	case_WLAN(IEEE8021X_FAILED);
4916 	case_WLAN(CIPHER_SUITE_REJECTED);
4917 	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
4918 	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
4919 	case_WLAN(DISASSOC_LOW_ACK);
4920 	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
4921 	case_WLAN(QSTA_LEAVE_QBSS);
4922 	case_WLAN(QSTA_NOT_USE);
4923 	case_WLAN(QSTA_REQUIRE_SETUP);
4924 	case_WLAN(QSTA_TIMEOUT);
4925 	case_WLAN(QSTA_CIPHER_NOT_SUPP);
4926 	case_WLAN(MESH_PEER_CANCELED);
4927 	case_WLAN(MESH_MAX_PEERS);
4928 	case_WLAN(MESH_CONFIG);
4929 	case_WLAN(MESH_CLOSE);
4930 	case_WLAN(MESH_MAX_RETRIES);
4931 	case_WLAN(MESH_CONFIRM_TIMEOUT);
4932 	case_WLAN(MESH_INVALID_GTK);
4933 	case_WLAN(MESH_INCONSISTENT_PARAM);
4934 	case_WLAN(MESH_INVALID_SECURITY);
4935 	case_WLAN(MESH_PATH_ERROR);
4936 	case_WLAN(MESH_PATH_NOFORWARD);
4937 	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
4938 	case_WLAN(MAC_EXISTS_IN_MBSS);
4939 	case_WLAN(MESH_CHAN_REGULATORY);
4940 	case_WLAN(MESH_CHAN);
4941 	default: return "<unknown>";
4942 	}
4943 }
4944 
4945 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
4946 				     struct ieee80211_mgmt *mgmt, size_t len)
4947 {
4948 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4949 	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
4950 
4951 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4952 
4953 	if (len < 24 + 2)
4954 		return;
4955 
4956 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
4957 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
4958 		return;
4959 	}
4960 
4961 	if (ifmgd->associated &&
4962 	    ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
4963 		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
4964 			   sdata->vif.cfg.ap_addr, reason_code,
4965 			   ieee80211_get_reason_code_string(reason_code));
4966 
4967 		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
4968 
4969 		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
4970 					    reason_code, false);
4971 		return;
4972 	}
4973 
4974 	if (ifmgd->assoc_data &&
4975 	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->ap_addr)) {
4976 		sdata_info(sdata,
4977 			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
4978 			   ifmgd->assoc_data->ap_addr, reason_code,
4979 			   ieee80211_get_reason_code_string(reason_code));
4980 
4981 		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
4982 
4983 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
4984 		return;
4985 	}
4986 }
4987 
4988 
4989 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
4990 				       struct ieee80211_mgmt *mgmt, size_t len)
4991 {
4992 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4993 	u16 reason_code;
4994 
4995 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4996 
4997 	if (len < 24 + 2)
4998 		return;
4999 
5000 	if (!ifmgd->associated ||
5001 	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
5002 		return;
5003 
5004 	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
5005 
5006 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
5007 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
5008 		return;
5009 	}
5010 
5011 	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
5012 		   sdata->vif.cfg.ap_addr, reason_code,
5013 		   ieee80211_get_reason_code_string(reason_code));
5014 
5015 	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
5016 
5017 	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
5018 				    false);
5019 }
5020 
5021 static bool ieee80211_twt_req_supported(struct ieee80211_sub_if_data *sdata,
5022 					struct ieee80211_supported_band *sband,
5023 					const struct link_sta_info *link_sta,
5024 					const struct ieee802_11_elems *elems)
5025 {
5026 	const struct ieee80211_sta_he_cap *own_he_cap =
5027 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
5028 
5029 	if (elems->ext_capab_len < 10)
5030 		return false;
5031 
5032 	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
5033 		return false;
5034 
5035 	return link_sta->pub->he_cap.he_cap_elem.mac_cap_info[0] &
5036 		IEEE80211_HE_MAC_CAP0_TWT_RES &&
5037 		own_he_cap &&
5038 		(own_he_cap->he_cap_elem.mac_cap_info[0] &
5039 			IEEE80211_HE_MAC_CAP0_TWT_REQ);
5040 }
5041 
5042 static u64 ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
5043 				    struct ieee80211_supported_band *sband,
5044 				    struct ieee80211_link_data *link,
5045 				    struct link_sta_info *link_sta,
5046 				    struct ieee802_11_elems *elems)
5047 {
5048 	bool twt = ieee80211_twt_req_supported(sdata, sband, link_sta, elems);
5049 
5050 	if (link->conf->twt_requester != twt) {
5051 		link->conf->twt_requester = twt;
5052 		return BSS_CHANGED_TWT;
5053 	}
5054 	return 0;
5055 }
5056 
5057 static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
5058 					struct ieee80211_bss_conf *bss_conf,
5059 					struct ieee80211_supported_band *sband,
5060 					struct link_sta_info *link_sta)
5061 {
5062 	const struct ieee80211_sta_he_cap *own_he_cap =
5063 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
5064 
5065 	return bss_conf->he_support &&
5066 		(link_sta->pub->he_cap.he_cap_elem.mac_cap_info[2] &
5067 			IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
5068 		own_he_cap &&
5069 		(own_he_cap->he_cap_elem.mac_cap_info[2] &
5070 			IEEE80211_HE_MAC_CAP2_BCAST_TWT);
5071 }
5072 
5073 static void ieee80211_epcs_changed(struct ieee80211_sub_if_data *sdata,
5074 				   bool enabled)
5075 {
5076 	/* in any case this is called, dialog token should be reset */
5077 	sdata->u.mgd.epcs.dialog_token = 0;
5078 
5079 	if (sdata->u.mgd.epcs.enabled == enabled)
5080 		return;
5081 
5082 	sdata->u.mgd.epcs.enabled = enabled;
5083 	cfg80211_epcs_changed(sdata->dev, enabled);
5084 }
5085 
5086 static void ieee80211_epcs_teardown(struct ieee80211_sub_if_data *sdata)
5087 {
5088 	struct ieee80211_local *local = sdata->local;
5089 	u8 link_id;
5090 
5091 	if (!sdata->u.mgd.epcs.enabled)
5092 		return;
5093 
5094 	lockdep_assert_wiphy(local->hw.wiphy);
5095 
5096 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
5097 		struct ieee802_11_elems *elems;
5098 		struct ieee80211_link_data *link;
5099 		const struct cfg80211_bss_ies *ies;
5100 		bool ret;
5101 
5102 		rcu_read_lock();
5103 
5104 		link = sdata_dereference(sdata->link[link_id], sdata);
5105 		if (!link || !link->conf || !link->conf->bss) {
5106 			rcu_read_unlock();
5107 			continue;
5108 		}
5109 
5110 		if (link->u.mgd.disable_wmm_tracking) {
5111 			rcu_read_unlock();
5112 			ieee80211_set_wmm_default(link, false, false);
5113 			continue;
5114 		}
5115 
5116 		ies = rcu_dereference(link->conf->bss->beacon_ies);
5117 		if (!ies) {
5118 			rcu_read_unlock();
5119 			ieee80211_set_wmm_default(link, false, false);
5120 			continue;
5121 		}
5122 
5123 		elems = ieee802_11_parse_elems(ies->data, ies->len, false,
5124 					       NULL);
5125 		if (!elems) {
5126 			rcu_read_unlock();
5127 			ieee80211_set_wmm_default(link, false, false);
5128 			continue;
5129 		}
5130 
5131 		ret = _ieee80211_sta_wmm_params(local, link,
5132 						elems->wmm_param,
5133 						elems->wmm_param_len,
5134 						elems->mu_edca_param_set);
5135 
5136 		kfree(elems);
5137 		rcu_read_unlock();
5138 
5139 		if (!ret) {
5140 			ieee80211_set_wmm_default(link, false, false);
5141 			continue;
5142 		}
5143 
5144 		ieee80211_mgd_set_link_qos_params(link);
5145 		ieee80211_link_info_change_notify(sdata, link, BSS_CHANGED_QOS);
5146 	}
5147 }
5148 
5149 static bool ieee80211_assoc_config_link(struct ieee80211_link_data *link,
5150 					struct link_sta_info *link_sta,
5151 					struct cfg80211_bss *cbss,
5152 					struct ieee80211_mgmt *mgmt,
5153 					const u8 *elem_start,
5154 					unsigned int elem_len,
5155 					u64 *changed)
5156 {
5157 	struct ieee80211_sub_if_data *sdata = link->sdata;
5158 	struct ieee80211_mgd_assoc_data *assoc_data =
5159 		sdata->u.mgd.assoc_data ?: sdata->u.mgd.reconf.add_links_data;
5160 	struct ieee80211_bss_conf *bss_conf = link->conf;
5161 	struct ieee80211_local *local = sdata->local;
5162 	unsigned int link_id = link->link_id;
5163 	struct ieee80211_elems_parse_params parse_params = {
5164 		.mode = link->u.mgd.conn.mode,
5165 		.start = elem_start,
5166 		.len = elem_len,
5167 		.link_id = link_id == assoc_data->assoc_link_id ? -1 : link_id,
5168 		.from_ap = true,
5169 	};
5170 	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
5171 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
5172 	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
5173 	const struct cfg80211_bss_ies *bss_ies = NULL;
5174 	struct ieee80211_supported_band *sband;
5175 	struct ieee802_11_elems *elems;
5176 	const __le16 prof_bss_param_ch_present =
5177 		cpu_to_le16(IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT);
5178 	u16 capab_info;
5179 	bool ret;
5180 
5181 	elems = ieee802_11_parse_elems_full(&parse_params);
5182 	if (!elems)
5183 		return false;
5184 
5185 	if (link_id == assoc_data->assoc_link_id) {
5186 		capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
5187 
5188 		/*
5189 		 * we should not get to this flow unless the association was
5190 		 * successful, so set the status directly to success
5191 		 */
5192 		assoc_data->link[link_id].status = WLAN_STATUS_SUCCESS;
5193 		if (elems->ml_basic) {
5194 			int bss_param_ch_cnt =
5195 				ieee80211_mle_get_bss_param_ch_cnt((const void *)elems->ml_basic);
5196 
5197 			if (bss_param_ch_cnt < 0) {
5198 				ret = false;
5199 				goto out;
5200 			}
5201 			bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
5202 			bss_conf->bss_param_ch_cnt_link_id = link_id;
5203 		}
5204 	} else if (elems->parse_error & IEEE80211_PARSE_ERR_DUP_NEST_ML_BASIC ||
5205 		   !elems->prof ||
5206 		   !(elems->prof->control & prof_bss_param_ch_present)) {
5207 		ret = false;
5208 		goto out;
5209 	} else {
5210 		const u8 *ptr = elems->prof->variable +
5211 				elems->prof->sta_info_len - 1;
5212 		int bss_param_ch_cnt;
5213 
5214 		/*
5215 		 * During parsing, we validated that these fields exist,
5216 		 * otherwise elems->prof would have been set to NULL.
5217 		 */
5218 		capab_info = get_unaligned_le16(ptr);
5219 		assoc_data->link[link_id].status = get_unaligned_le16(ptr + 2);
5220 		bss_param_ch_cnt =
5221 			ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(elems->prof);
5222 		bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
5223 		bss_conf->bss_param_ch_cnt_link_id = link_id;
5224 
5225 		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
5226 			link_info(link, "association response status code=%u\n",
5227 				  assoc_data->link[link_id].status);
5228 			ret = true;
5229 			goto out;
5230 		}
5231 	}
5232 
5233 	if (!is_s1g && !elems->supp_rates) {
5234 		sdata_info(sdata, "no SuppRates element in AssocResp\n");
5235 		ret = false;
5236 		goto out;
5237 	}
5238 
5239 	link->u.mgd.tdls_chan_switch_prohibited =
5240 		elems->ext_capab && elems->ext_capab_len >= 5 &&
5241 		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
5242 
5243 	/*
5244 	 * Some APs are erroneously not including some information in their
5245 	 * (re)association response frames. Try to recover by using the data
5246 	 * from the beacon or probe response. This seems to afflict mobile
5247 	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
5248 	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
5249 	 */
5250 	if (!ieee80211_hw_check(&local->hw, STRICT) && !is_6ghz &&
5251 	    ((assoc_data->wmm && !elems->wmm_param) ||
5252 	     (link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
5253 	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
5254 	     (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
5255 	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
5256 		const struct cfg80211_bss_ies *ies;
5257 		struct ieee802_11_elems *bss_elems;
5258 
5259 		rcu_read_lock();
5260 		ies = rcu_dereference(cbss->ies);
5261 		if (ies)
5262 			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
5263 					  GFP_ATOMIC);
5264 		rcu_read_unlock();
5265 		if (!bss_ies) {
5266 			ret = false;
5267 			goto out;
5268 		}
5269 
5270 		parse_params.start = bss_ies->data;
5271 		parse_params.len = bss_ies->len;
5272 		parse_params.bss = cbss;
5273 		parse_params.link_id = -1;
5274 		bss_elems = ieee802_11_parse_elems_full(&parse_params);
5275 		if (!bss_elems) {
5276 			ret = false;
5277 			goto out;
5278 		}
5279 
5280 		if (assoc_data->wmm &&
5281 		    !elems->wmm_param && bss_elems->wmm_param) {
5282 			elems->wmm_param = bss_elems->wmm_param;
5283 			sdata_info(sdata,
5284 				   "AP bug: WMM param missing from AssocResp\n");
5285 		}
5286 
5287 		/*
5288 		 * Also check if we requested HT/VHT, otherwise the AP doesn't
5289 		 * have to include the IEs in the (re)association response.
5290 		 */
5291 		if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
5292 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
5293 			elems->ht_cap_elem = bss_elems->ht_cap_elem;
5294 			sdata_info(sdata,
5295 				   "AP bug: HT capability missing from AssocResp\n");
5296 		}
5297 		if (!elems->ht_operation && bss_elems->ht_operation &&
5298 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
5299 			elems->ht_operation = bss_elems->ht_operation;
5300 			sdata_info(sdata,
5301 				   "AP bug: HT operation missing from AssocResp\n");
5302 		}
5303 
5304 		if (is_5ghz) {
5305 			if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
5306 			    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
5307 				elems->vht_cap_elem = bss_elems->vht_cap_elem;
5308 				sdata_info(sdata,
5309 					   "AP bug: VHT capa missing from AssocResp\n");
5310 			}
5311 
5312 			if (!elems->vht_operation && bss_elems->vht_operation &&
5313 			    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
5314 				elems->vht_operation = bss_elems->vht_operation;
5315 				sdata_info(sdata,
5316 					   "AP bug: VHT operation missing from AssocResp\n");
5317 			}
5318 		}
5319 		kfree(bss_elems);
5320 	}
5321 
5322 	/*
5323 	 * We previously checked these in the beacon/probe response, so
5324 	 * they should be present here. This is just a safety net.
5325 	 * Note that the ieee80211_config_bw() below would also check
5326 	 * for this (and more), but this has better error reporting.
5327 	 */
5328 	if (!is_6ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
5329 	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
5330 		sdata_info(sdata,
5331 			   "HT AP is missing WMM params or HT capability/operation\n");
5332 		ret = false;
5333 		goto out;
5334 	}
5335 
5336 	if (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
5337 	    (!elems->vht_cap_elem || !elems->vht_operation)) {
5338 		sdata_info(sdata,
5339 			   "VHT AP is missing VHT capability/operation\n");
5340 		ret = false;
5341 		goto out;
5342 	}
5343 
5344 	/* check/update if AP changed anything in assoc response vs. scan */
5345 	if (ieee80211_config_bw(link, elems,
5346 				link_id == assoc_data->assoc_link_id,
5347 				changed, "assoc response")) {
5348 		ret = false;
5349 		goto out;
5350 	}
5351 
5352 	if (WARN_ON(!link->conf->chanreq.oper.chan)) {
5353 		ret = false;
5354 		goto out;
5355 	}
5356 	sband = local->hw.wiphy->bands[link->conf->chanreq.oper.chan->band];
5357 
5358 	/* Set up internal HT/VHT capabilities */
5359 	if (elems->ht_cap_elem && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT)
5360 		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
5361 						  elems->ht_cap_elem,
5362 						  link_sta);
5363 
5364 	if (elems->vht_cap_elem &&
5365 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
5366 		const struct ieee80211_vht_cap *bss_vht_cap = NULL;
5367 		const struct cfg80211_bss_ies *ies;
5368 
5369 		/*
5370 		 * Cisco AP module 9115 with FW 17.3 has a bug and sends a
5371 		 * too large maximum MPDU length in the association response
5372 		 * (indicating 12k) that it cannot actually process ...
5373 		 * Work around that.
5374 		 */
5375 		rcu_read_lock();
5376 		ies = rcu_dereference(cbss->ies);
5377 		if (ies) {
5378 			const struct element *elem;
5379 
5380 			elem = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY,
5381 						  ies->data, ies->len);
5382 			if (elem && elem->datalen >= sizeof(*bss_vht_cap))
5383 				bss_vht_cap = (const void *)elem->data;
5384 		}
5385 
5386 		if (ieee80211_hw_check(&local->hw, STRICT) &&
5387 		    (!bss_vht_cap || memcmp(bss_vht_cap, elems->vht_cap_elem,
5388 					    sizeof(*bss_vht_cap)))) {
5389 			rcu_read_unlock();
5390 			ret = false;
5391 			link_info(link, "VHT capabilities mismatch\n");
5392 			goto out;
5393 		}
5394 
5395 		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
5396 						    elems->vht_cap_elem,
5397 						    bss_vht_cap, link_sta);
5398 		rcu_read_unlock();
5399 	}
5400 
5401 	if (elems->he_operation &&
5402 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE &&
5403 	    elems->he_cap) {
5404 		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
5405 						  elems->he_cap,
5406 						  elems->he_cap_len,
5407 						  elems->he_6ghz_capa,
5408 						  link_sta);
5409 
5410 		bss_conf->he_support = link_sta->pub->he_cap.has_he;
5411 		if (elems->rsnx && elems->rsnx_len &&
5412 		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
5413 		    wiphy_ext_feature_isset(local->hw.wiphy,
5414 					    NL80211_EXT_FEATURE_PROTECTED_TWT))
5415 			bss_conf->twt_protected = true;
5416 		else
5417 			bss_conf->twt_protected = false;
5418 
5419 		*changed |= ieee80211_recalc_twt_req(sdata, sband, link,
5420 						     link_sta, elems);
5421 
5422 		if (elems->eht_operation && elems->eht_cap &&
5423 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_EHT) {
5424 			ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband,
5425 							    elems->he_cap,
5426 							    elems->he_cap_len,
5427 							    elems->eht_cap,
5428 							    elems->eht_cap_len,
5429 							    link_sta);
5430 
5431 			bss_conf->eht_support = link_sta->pub->eht_cap.has_eht;
5432 			bss_conf->epcs_support = bss_conf->eht_support &&
5433 				!!(elems->eht_cap->fixed.mac_cap_info[0] &
5434 				   IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS);
5435 
5436 			/* EPCS might be already enabled but a new added link
5437 			 * does not support EPCS. This should not really happen
5438 			 * in practice.
5439 			 */
5440 			if (sdata->u.mgd.epcs.enabled &&
5441 			    !bss_conf->epcs_support)
5442 				ieee80211_epcs_teardown(sdata);
5443 		} else {
5444 			bss_conf->eht_support = false;
5445 			bss_conf->epcs_support = false;
5446 		}
5447 	} else {
5448 		bss_conf->he_support = false;
5449 		bss_conf->twt_requester = false;
5450 		bss_conf->twt_protected = false;
5451 		bss_conf->eht_support = false;
5452 		bss_conf->epcs_support = false;
5453 	}
5454 
5455 	if (elems->s1g_oper &&
5456 	    link->u.mgd.conn.mode == IEEE80211_CONN_MODE_S1G &&
5457 	    elems->s1g_capab)
5458 		ieee80211_s1g_cap_to_sta_s1g_cap(sdata, elems->s1g_capab,
5459 						 link_sta);
5460 
5461 	bss_conf->twt_broadcast =
5462 		ieee80211_twt_bcast_support(sdata, bss_conf, sband, link_sta);
5463 
5464 	if (bss_conf->he_support) {
5465 		bss_conf->he_bss_color.color =
5466 			le32_get_bits(elems->he_operation->he_oper_params,
5467 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
5468 		bss_conf->he_bss_color.partial =
5469 			le32_get_bits(elems->he_operation->he_oper_params,
5470 				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
5471 		bss_conf->he_bss_color.enabled =
5472 			!le32_get_bits(elems->he_operation->he_oper_params,
5473 				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
5474 
5475 		if (bss_conf->he_bss_color.enabled)
5476 			*changed |= BSS_CHANGED_HE_BSS_COLOR;
5477 
5478 		bss_conf->htc_trig_based_pkt_ext =
5479 			le32_get_bits(elems->he_operation->he_oper_params,
5480 				      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
5481 		bss_conf->frame_time_rts_th =
5482 			le32_get_bits(elems->he_operation->he_oper_params,
5483 				      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
5484 
5485 		bss_conf->uora_exists = !!elems->uora_element;
5486 		if (elems->uora_element)
5487 			bss_conf->uora_ocw_range = elems->uora_element[0];
5488 
5489 		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
5490 		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
5491 		/* TODO: OPEN: what happens if BSS color disable is set? */
5492 	}
5493 
5494 	if (cbss->transmitted_bss) {
5495 		bss_conf->nontransmitted = true;
5496 		ether_addr_copy(bss_conf->transmitter_bssid,
5497 				cbss->transmitted_bss->bssid);
5498 		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
5499 		bss_conf->bssid_index = cbss->bssid_index;
5500 	}
5501 
5502 	/*
5503 	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
5504 	 * in their association response, so ignore that data for our own
5505 	 * configuration. If it changed since the last beacon, we'll get the
5506 	 * next beacon and update then.
5507 	 */
5508 
5509 	/*
5510 	 * If an operating mode notification IE is present, override the
5511 	 * NSS calculation (that would be done in rate_control_rate_init())
5512 	 * and use the # of streams from that element.
5513 	 */
5514 	if (elems->opmode_notif &&
5515 	    !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
5516 		u8 nss;
5517 
5518 		nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
5519 		nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
5520 		nss += 1;
5521 		link_sta->pub->rx_nss = nss;
5522 	}
5523 
5524 	/*
5525 	 * Always handle WMM once after association regardless
5526 	 * of the first value the AP uses. Setting -1 here has
5527 	 * that effect because the AP values is an unsigned
5528 	 * 4-bit value.
5529 	 */
5530 	link->u.mgd.wmm_last_param_set = -1;
5531 	link->u.mgd.mu_edca_last_param_set = -1;
5532 
5533 	if (link->u.mgd.disable_wmm_tracking) {
5534 		ieee80211_set_wmm_default(link, false, false);
5535 	} else if (!ieee80211_sta_wmm_params(local, link, elems->wmm_param,
5536 					     elems->wmm_param_len,
5537 					     elems->mu_edca_param_set)) {
5538 		/* still enable QoS since we might have HT/VHT */
5539 		ieee80211_set_wmm_default(link, false, true);
5540 		/* disable WMM tracking in this case to disable
5541 		 * tracking WMM parameter changes in the beacon if
5542 		 * the parameters weren't actually valid. Doing so
5543 		 * avoids changing parameters very strangely when
5544 		 * the AP is going back and forth between valid and
5545 		 * invalid parameters.
5546 		 */
5547 		link->u.mgd.disable_wmm_tracking = true;
5548 	}
5549 
5550 	if (elems->max_idle_period_ie) {
5551 		bss_conf->max_idle_period =
5552 			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
5553 		bss_conf->protected_keep_alive =
5554 			!!(elems->max_idle_period_ie->idle_options &
5555 			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
5556 		*changed |= BSS_CHANGED_KEEP_ALIVE;
5557 	} else {
5558 		bss_conf->max_idle_period = 0;
5559 		bss_conf->protected_keep_alive = false;
5560 	}
5561 
5562 	/* set assoc capability (AID was already set earlier),
5563 	 * ieee80211_set_associated() will tell the driver */
5564 	bss_conf->assoc_capability = capab_info;
5565 
5566 	ret = true;
5567 out:
5568 	kfree(elems);
5569 	kfree(bss_ies);
5570 	return ret;
5571 }
5572 
5573 static int ieee80211_mgd_setup_link_sta(struct ieee80211_link_data *link,
5574 					struct sta_info *sta,
5575 					struct link_sta_info *link_sta,
5576 					struct cfg80211_bss *cbss)
5577 {
5578 	struct ieee80211_sub_if_data *sdata = link->sdata;
5579 	struct ieee80211_local *local = sdata->local;
5580 	struct ieee80211_bss *bss = (void *)cbss->priv;
5581 	u32 rates = 0, basic_rates = 0;
5582 	bool have_higher_than_11mbit = false;
5583 	int min_rate = INT_MAX, min_rate_index = -1;
5584 	struct ieee80211_supported_band *sband;
5585 
5586 	memcpy(link_sta->addr, cbss->bssid, ETH_ALEN);
5587 	memcpy(link_sta->pub->addr, cbss->bssid, ETH_ALEN);
5588 
5589 	/* TODO: S1G Basic Rate Set is expressed elsewhere */
5590 	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5591 		ieee80211_s1g_sta_rate_init(sta);
5592 		return 0;
5593 	}
5594 
5595 	sband = local->hw.wiphy->bands[cbss->channel->band];
5596 
5597 	ieee80211_get_rates(sband, bss->supp_rates, bss->supp_rates_len,
5598 			    NULL, 0,
5599 			    &rates, &basic_rates, NULL,
5600 			    &have_higher_than_11mbit,
5601 			    &min_rate, &min_rate_index);
5602 
5603 	/*
5604 	 * This used to be a workaround for basic rates missing
5605 	 * in the association response frame. Now that we no
5606 	 * longer use the basic rates from there, it probably
5607 	 * doesn't happen any more, but keep the workaround so
5608 	 * in case some *other* APs are buggy in different ways
5609 	 * we can connect -- with a warning.
5610 	 * Allow this workaround only in case the AP provided at least
5611 	 * one rate.
5612 	 */
5613 	if (min_rate_index < 0) {
5614 		link_info(link, "No legacy rates in association response\n");
5615 		return -EINVAL;
5616 	} else if (!basic_rates) {
5617 		link_info(link, "No basic rates, using min rate instead\n");
5618 		basic_rates = BIT(min_rate_index);
5619 	}
5620 
5621 	if (rates)
5622 		link_sta->pub->supp_rates[cbss->channel->band] = rates;
5623 	else
5624 		link_info(link, "No rates found, keeping mandatory only\n");
5625 
5626 	link->conf->basic_rates = basic_rates;
5627 
5628 	/* cf. IEEE 802.11 9.2.12 */
5629 	link->operating_11g_mode = sband->band == NL80211_BAND_2GHZ &&
5630 				   have_higher_than_11mbit;
5631 
5632 	return 0;
5633 }
5634 
5635 static u8 ieee80211_max_rx_chains(struct ieee80211_link_data *link,
5636 				  struct cfg80211_bss *cbss)
5637 {
5638 	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
5639 	const struct element *ht_cap_elem, *vht_cap_elem;
5640 	const struct cfg80211_bss_ies *ies;
5641 	const struct ieee80211_ht_cap *ht_cap;
5642 	const struct ieee80211_vht_cap *vht_cap;
5643 	const struct ieee80211_he_cap_elem *he_cap;
5644 	const struct element *he_cap_elem;
5645 	u16 mcs_80_map, mcs_160_map;
5646 	int i, mcs_nss_size;
5647 	bool support_160;
5648 	u8 chains = 1;
5649 
5650 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HT)
5651 		return chains;
5652 
5653 	ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
5654 	if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
5655 		ht_cap = (void *)ht_cap_elem->data;
5656 		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
5657 		/*
5658 		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
5659 		 *	 "Tx Unequal Modulation Supported" fields.
5660 		 */
5661 	}
5662 
5663 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_VHT)
5664 		return chains;
5665 
5666 	vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
5667 	if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
5668 		u8 nss;
5669 		u16 tx_mcs_map;
5670 
5671 		vht_cap = (void *)vht_cap_elem->data;
5672 		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
5673 		for (nss = 8; nss > 0; nss--) {
5674 			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
5675 					IEEE80211_VHT_MCS_NOT_SUPPORTED)
5676 				break;
5677 		}
5678 		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
5679 		chains = max(chains, nss);
5680 	}
5681 
5682 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HE)
5683 		return chains;
5684 
5685 	ies = rcu_dereference(cbss->ies);
5686 	he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
5687 					     ies->data, ies->len);
5688 
5689 	if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap))
5690 		return chains;
5691 
5692 	/* skip one byte ext_tag_id */
5693 	he_cap = (void *)(he_cap_elem->data + 1);
5694 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
5695 
5696 	/* invalid HE IE */
5697 	if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap))
5698 		return chains;
5699 
5700 	/* mcs_nss is right after he_cap info */
5701 	he_mcs_nss_supp = (void *)(he_cap + 1);
5702 
5703 	mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
5704 
5705 	for (i = 7; i >= 0; i--) {
5706 		u8 mcs_80 = mcs_80_map >> (2 * i) & 3;
5707 
5708 		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
5709 			chains = max_t(u8, chains, i + 1);
5710 			break;
5711 		}
5712 	}
5713 
5714 	support_160 = he_cap->phy_cap_info[0] &
5715 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
5716 
5717 	if (!support_160)
5718 		return chains;
5719 
5720 	mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160);
5721 	for (i = 7; i >= 0; i--) {
5722 		u8 mcs_160 = mcs_160_map >> (2 * i) & 3;
5723 
5724 		if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
5725 			chains = max_t(u8, chains, i + 1);
5726 			break;
5727 		}
5728 	}
5729 
5730 	return chains;
5731 }
5732 
5733 static void
5734 ieee80211_determine_our_sta_mode(struct ieee80211_sub_if_data *sdata,
5735 				 struct ieee80211_supported_band *sband,
5736 				 struct cfg80211_assoc_request *req,
5737 				 bool wmm_used, int link_id,
5738 				 struct ieee80211_conn_settings *conn)
5739 {
5740 	struct ieee80211_sta_ht_cap sta_ht_cap = sband->ht_cap;
5741 	bool is_5ghz = sband->band == NL80211_BAND_5GHZ;
5742 	bool is_6ghz = sband->band == NL80211_BAND_6GHZ;
5743 	const struct ieee80211_sta_he_cap *he_cap;
5744 	const struct ieee80211_sta_eht_cap *eht_cap;
5745 	struct ieee80211_sta_vht_cap vht_cap;
5746 
5747 	if (sband->band == NL80211_BAND_S1GHZ) {
5748 		conn->mode = IEEE80211_CONN_MODE_S1G;
5749 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5750 		mlme_dbg(sdata, "operating as S1G STA\n");
5751 		return;
5752 	}
5753 
5754 	conn->mode = IEEE80211_CONN_MODE_LEGACY;
5755 	conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5756 
5757 	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5758 
5759 	if (req && req->flags & ASSOC_REQ_DISABLE_HT) {
5760 		mlme_link_id_dbg(sdata, link_id,
5761 				 "HT disabled by flag, limiting to legacy\n");
5762 		goto out;
5763 	}
5764 
5765 	if (!wmm_used) {
5766 		mlme_link_id_dbg(sdata, link_id,
5767 				 "WMM/QoS not supported, limiting to legacy\n");
5768 		goto out;
5769 	}
5770 
5771 	if (req) {
5772 		unsigned int i;
5773 
5774 		for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5775 			if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5776 			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5777 			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5778 				netdev_info(sdata->dev,
5779 					    "WEP/TKIP use, limiting to legacy\n");
5780 				goto out;
5781 			}
5782 		}
5783 	}
5784 
5785 	if (!sta_ht_cap.ht_supported && !is_6ghz) {
5786 		mlme_link_id_dbg(sdata, link_id,
5787 				 "HT not supported (and not on 6 GHz), limiting to legacy\n");
5788 		goto out;
5789 	}
5790 
5791 	/* HT is fine */
5792 	conn->mode = IEEE80211_CONN_MODE_HT;
5793 	conn->bw_limit = sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
5794 		IEEE80211_CONN_BW_LIMIT_40 :
5795 		IEEE80211_CONN_BW_LIMIT_20;
5796 
5797 	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
5798 	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
5799 
5800 	if (req && req->flags & ASSOC_REQ_DISABLE_VHT) {
5801 		mlme_link_id_dbg(sdata, link_id,
5802 				 "VHT disabled by flag, limiting to HT\n");
5803 		goto out;
5804 	}
5805 
5806 	if (vht_cap.vht_supported && is_5ghz) {
5807 		bool have_80mhz = false;
5808 		unsigned int i;
5809 
5810 		if (conn->bw_limit == IEEE80211_CONN_BW_LIMIT_20) {
5811 			mlme_link_id_dbg(sdata, link_id,
5812 					 "no 40 MHz support on 5 GHz, limiting to HT\n");
5813 			goto out;
5814 		}
5815 
5816 		/* Allow VHT if at least one channel on the sband supports 80 MHz */
5817 		for (i = 0; i < sband->n_channels; i++) {
5818 			if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5819 							IEEE80211_CHAN_NO_80MHZ))
5820 				continue;
5821 
5822 			have_80mhz = true;
5823 			break;
5824 		}
5825 
5826 		if (!have_80mhz) {
5827 			mlme_link_id_dbg(sdata, link_id,
5828 					 "no 80 MHz channel support on 5 GHz, limiting to HT\n");
5829 			goto out;
5830 		}
5831 	} else if (is_5ghz) { /* !vht_supported but on 5 GHz */
5832 		mlme_link_id_dbg(sdata, link_id,
5833 				 "no VHT support on 5 GHz, limiting to HT\n");
5834 		goto out;
5835 	}
5836 
5837 	/* VHT - if we have - is fine, including 80 MHz, check 160 below again */
5838 	if (sband->band != NL80211_BAND_2GHZ) {
5839 		conn->mode = IEEE80211_CONN_MODE_VHT;
5840 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_160;
5841 	}
5842 
5843 	if (is_5ghz &&
5844 	    !(vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
5845 			     IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
5846 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_80;
5847 		mlme_link_id_dbg(sdata, link_id,
5848 				 "no VHT 160 MHz capability on 5 GHz, limiting to 80 MHz");
5849 	}
5850 
5851 	if (req && req->flags & ASSOC_REQ_DISABLE_HE) {
5852 		mlme_link_id_dbg(sdata, link_id,
5853 				 "HE disabled by flag, limiting to HT/VHT\n");
5854 		goto out;
5855 	}
5856 
5857 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
5858 	if (!he_cap) {
5859 		WARN_ON(is_6ghz);
5860 		mlme_link_id_dbg(sdata, link_id,
5861 				 "no HE support, limiting to HT/VHT\n");
5862 		goto out;
5863 	}
5864 
5865 	/* so we have HE */
5866 	conn->mode = IEEE80211_CONN_MODE_HE;
5867 
5868 	/* check bandwidth */
5869 	switch (sband->band) {
5870 	default:
5871 	case NL80211_BAND_2GHZ:
5872 		if (he_cap->he_cap_elem.phy_cap_info[0] &
5873 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
5874 			break;
5875 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5876 		mlme_link_id_dbg(sdata, link_id,
5877 				 "no 40 MHz HE cap in 2.4 GHz, limiting to 20 MHz\n");
5878 		break;
5879 	case NL80211_BAND_5GHZ:
5880 		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
5881 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
5882 			conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5883 			mlme_link_id_dbg(sdata, link_id,
5884 					 "no 40/80 MHz HE cap in 5 GHz, limiting to 20 MHz\n");
5885 			break;
5886 		}
5887 		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
5888 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
5889 			conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
5890 					       conn->bw_limit,
5891 					       IEEE80211_CONN_BW_LIMIT_80);
5892 			mlme_link_id_dbg(sdata, link_id,
5893 					 "no 160 MHz HE cap in 5 GHz, limiting to 80 MHz\n");
5894 		}
5895 		break;
5896 	case NL80211_BAND_6GHZ:
5897 		if (he_cap->he_cap_elem.phy_cap_info[0] &
5898 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
5899 			break;
5900 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
5901 				       conn->bw_limit,
5902 				       IEEE80211_CONN_BW_LIMIT_80);
5903 		mlme_link_id_dbg(sdata, link_id,
5904 				 "no 160 MHz HE cap in 6 GHz, limiting to 80 MHz\n");
5905 		break;
5906 	}
5907 
5908 	if (req && req->flags & ASSOC_REQ_DISABLE_EHT) {
5909 		mlme_link_id_dbg(sdata, link_id,
5910 				 "EHT disabled by flag, limiting to HE\n");
5911 		goto out;
5912 	}
5913 
5914 	eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
5915 	if (!eht_cap) {
5916 		mlme_link_id_dbg(sdata, link_id,
5917 				 "no EHT support, limiting to HE\n");
5918 		goto out;
5919 	}
5920 
5921 	/* we have EHT */
5922 
5923 	conn->mode = IEEE80211_CONN_MODE_EHT;
5924 
5925 	/* check bandwidth */
5926 	if (is_6ghz &&
5927 	    eht_cap->eht_cap_elem.phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
5928 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_320;
5929 	else if (is_6ghz)
5930 		mlme_link_id_dbg(sdata, link_id,
5931 				 "no EHT 320 MHz cap in 6 GHz, limiting to 160 MHz\n");
5932 
5933 out:
5934 	mlme_link_id_dbg(sdata, link_id,
5935 			 "determined local STA to be %s, BW limited to %d MHz\n",
5936 			 ieee80211_conn_mode_str(conn->mode),
5937 			 20 * (1 << conn->bw_limit));
5938 }
5939 
5940 static void
5941 ieee80211_determine_our_sta_mode_auth(struct ieee80211_sub_if_data *sdata,
5942 				      struct ieee80211_supported_band *sband,
5943 				      struct cfg80211_auth_request *req,
5944 				      bool wmm_used,
5945 				      struct ieee80211_conn_settings *conn)
5946 {
5947 	ieee80211_determine_our_sta_mode(sdata, sband, NULL, wmm_used,
5948 					 req->link_id > 0 ? req->link_id : 0,
5949 					 conn);
5950 }
5951 
5952 static void
5953 ieee80211_determine_our_sta_mode_assoc(struct ieee80211_sub_if_data *sdata,
5954 				       struct ieee80211_supported_band *sband,
5955 				       struct cfg80211_assoc_request *req,
5956 				       bool wmm_used, int link_id,
5957 				       struct ieee80211_conn_settings *conn)
5958 {
5959 	struct ieee80211_conn_settings tmp;
5960 
5961 	WARN_ON(!req);
5962 
5963 	ieee80211_determine_our_sta_mode(sdata, sband, req, wmm_used, link_id,
5964 					 &tmp);
5965 
5966 	conn->mode = min_t(enum ieee80211_conn_mode,
5967 			   conn->mode, tmp.mode);
5968 	conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
5969 			       conn->bw_limit, tmp.bw_limit);
5970 }
5971 
5972 static enum ieee80211_ap_reg_power
5973 ieee80211_ap_power_type(u8 control)
5974 {
5975 	switch (u8_get_bits(control, IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
5976 	case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
5977 	case IEEE80211_6GHZ_CTRL_REG_INDOOR_LPI_AP:
5978 		return IEEE80211_REG_LPI_AP;
5979 	case IEEE80211_6GHZ_CTRL_REG_SP_AP:
5980 	case IEEE80211_6GHZ_CTRL_REG_INDOOR_SP_AP:
5981 	case IEEE80211_6GHZ_CTRL_REG_INDOOR_SP_AP_OLD:
5982 		return IEEE80211_REG_SP_AP;
5983 	case IEEE80211_6GHZ_CTRL_REG_VLP_AP:
5984 		return IEEE80211_REG_VLP_AP;
5985 	default:
5986 		return IEEE80211_REG_UNSET_AP;
5987 	}
5988 }
5989 
5990 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
5991 				  struct ieee80211_link_data *link,
5992 				  int link_id,
5993 				  struct cfg80211_bss *cbss, bool mlo,
5994 				  struct ieee80211_conn_settings *conn,
5995 				  unsigned long *userspace_selectors)
5996 {
5997 	struct ieee80211_local *local = sdata->local;
5998 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
5999 	struct ieee80211_chan_req chanreq = {};
6000 	struct cfg80211_chan_def ap_chandef;
6001 	struct ieee802_11_elems *elems;
6002 	int ret;
6003 
6004 	lockdep_assert_wiphy(local->hw.wiphy);
6005 
6006 	rcu_read_lock();
6007 	elems = ieee80211_determine_chan_mode(sdata, conn, cbss, link_id,
6008 					      &chanreq, &ap_chandef,
6009 					      userspace_selectors);
6010 
6011 	if (IS_ERR(elems)) {
6012 		rcu_read_unlock();
6013 		return PTR_ERR(elems);
6014 	}
6015 
6016 	if (mlo && !elems->ml_basic) {
6017 		sdata_info(sdata, "Rejecting MLO as it is not supported by AP\n");
6018 		rcu_read_unlock();
6019 		kfree(elems);
6020 		return -EINVAL;
6021 	}
6022 
6023 	if (link && is_6ghz && conn->mode >= IEEE80211_CONN_MODE_HE) {
6024 		const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
6025 
6026 		if (elems->pwr_constr_elem)
6027 			link->conf->pwr_reduction = *elems->pwr_constr_elem;
6028 
6029 		he_6ghz_oper = ieee80211_he_6ghz_oper(elems->he_operation);
6030 		if (he_6ghz_oper)
6031 			link->conf->power_type =
6032 				ieee80211_ap_power_type(he_6ghz_oper->control);
6033 		else
6034 			link_info(link,
6035 				  "HE 6 GHz operation missing (on %d MHz), expect issues\n",
6036 				  cbss->channel->center_freq);
6037 
6038 		link->conf->tpe = elems->tpe;
6039 		ieee80211_rearrange_tpe(&link->conf->tpe, &ap_chandef,
6040 					&chanreq.oper);
6041 	}
6042 	rcu_read_unlock();
6043 	/* the element data was RCU protected so no longer valid anyway */
6044 	kfree(elems);
6045 	elems = NULL;
6046 
6047 	if (!link)
6048 		return 0;
6049 
6050 	rcu_read_lock();
6051 	link->needed_rx_chains = min(ieee80211_max_rx_chains(link, cbss),
6052 				     local->rx_chains);
6053 	rcu_read_unlock();
6054 
6055 	/*
6056 	 * If this fails (possibly due to channel context sharing
6057 	 * on incompatible channels, e.g. 80+80 and 160 sharing the
6058 	 * same control channel) try to use a smaller bandwidth.
6059 	 */
6060 	ret = ieee80211_link_use_channel(link, &chanreq,
6061 					 IEEE80211_CHANCTX_SHARED);
6062 
6063 	/* don't downgrade for 5 and 10 MHz channels, though. */
6064 	if (chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
6065 	    chanreq.oper.width == NL80211_CHAN_WIDTH_10)
6066 		return ret;
6067 
6068 	while (ret && chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT) {
6069 		ieee80211_chanreq_downgrade(&chanreq, conn);
6070 
6071 		ret = ieee80211_link_use_channel(link, &chanreq,
6072 						 IEEE80211_CHANCTX_SHARED);
6073 	}
6074 
6075 	return ret;
6076 }
6077 
6078 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
6079 			       u8 *dtim_count, u8 *dtim_period)
6080 {
6081 	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
6082 	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
6083 					 ies->len);
6084 	const struct ieee80211_tim_ie *tim = NULL;
6085 	const struct ieee80211_bssid_index *idx;
6086 	bool valid = tim_ie && tim_ie[1] >= 2;
6087 
6088 	if (valid)
6089 		tim = (void *)(tim_ie + 2);
6090 
6091 	if (dtim_count)
6092 		*dtim_count = valid ? tim->dtim_count : 0;
6093 
6094 	if (dtim_period)
6095 		*dtim_period = valid ? tim->dtim_period : 0;
6096 
6097 	/* Check if value is overridden by non-transmitted profile */
6098 	if (!idx_ie || idx_ie[1] < 3)
6099 		return valid;
6100 
6101 	idx = (void *)(idx_ie + 2);
6102 
6103 	if (dtim_count)
6104 		*dtim_count = idx->dtim_count;
6105 
6106 	if (dtim_period)
6107 		*dtim_period = idx->dtim_period;
6108 
6109 	return true;
6110 }
6111 
6112 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
6113 				    struct ieee80211_mgmt *mgmt,
6114 				    struct ieee802_11_elems *elems,
6115 				    const u8 *elem_start, unsigned int elem_len)
6116 {
6117 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6118 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
6119 	struct ieee80211_local *local = sdata->local;
6120 	unsigned int link_id;
6121 	struct sta_info *sta;
6122 	u64 changed[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6123 	u16 valid_links = 0, dormant_links = 0;
6124 	int err;
6125 
6126 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6127 	/*
6128 	 * station info was already allocated and inserted before
6129 	 * the association and should be available to us
6130 	 */
6131 	sta = sta_info_get(sdata, assoc_data->ap_addr);
6132 	if (WARN_ON(!sta))
6133 		goto out_err;
6134 
6135 	sta->sta.spp_amsdu = assoc_data->spp_amsdu;
6136 
6137 	if (ieee80211_vif_is_mld(&sdata->vif)) {
6138 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
6139 			if (!assoc_data->link[link_id].bss)
6140 				continue;
6141 
6142 			valid_links |= BIT(link_id);
6143 			if (assoc_data->link[link_id].disabled)
6144 				dormant_links |= BIT(link_id);
6145 
6146 			if (link_id != assoc_data->assoc_link_id) {
6147 				err = ieee80211_sta_allocate_link(sta, link_id);
6148 				if (err)
6149 					goto out_err;
6150 			}
6151 		}
6152 
6153 		ieee80211_vif_set_links(sdata, valid_links, dormant_links);
6154 	}
6155 
6156 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
6157 		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
6158 		struct ieee80211_link_data *link;
6159 		struct link_sta_info *link_sta;
6160 
6161 		if (!cbss)
6162 			continue;
6163 
6164 		link = sdata_dereference(sdata->link[link_id], sdata);
6165 		if (WARN_ON(!link))
6166 			goto out_err;
6167 
6168 		if (ieee80211_vif_is_mld(&sdata->vif))
6169 			link_info(link,
6170 				  "local address %pM, AP link address %pM%s\n",
6171 				  link->conf->addr,
6172 				  assoc_data->link[link_id].bss->bssid,
6173 				  link_id == assoc_data->assoc_link_id ?
6174 					" (assoc)" : "");
6175 
6176 		link_sta = rcu_dereference_protected(sta->link[link_id],
6177 						     lockdep_is_held(&local->hw.wiphy->mtx));
6178 		if (WARN_ON(!link_sta))
6179 			goto out_err;
6180 
6181 		if (!link->u.mgd.have_beacon) {
6182 			const struct cfg80211_bss_ies *ies;
6183 
6184 			rcu_read_lock();
6185 			ies = rcu_dereference(cbss->beacon_ies);
6186 			if (ies)
6187 				link->u.mgd.have_beacon = true;
6188 			else
6189 				ies = rcu_dereference(cbss->ies);
6190 			ieee80211_get_dtim(ies,
6191 					   &link->conf->sync_dtim_count,
6192 					   &link->u.mgd.dtim_period);
6193 			link->conf->beacon_int = cbss->beacon_interval;
6194 			rcu_read_unlock();
6195 		}
6196 
6197 		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
6198 
6199 		if (link_id != assoc_data->assoc_link_id) {
6200 			link->u.mgd.conn = assoc_data->link[link_id].conn;
6201 
6202 			err = ieee80211_prep_channel(sdata, link, link_id, cbss,
6203 						     true, &link->u.mgd.conn,
6204 						     sdata->u.mgd.userspace_selectors);
6205 			if (err) {
6206 				link_info(link, "prep_channel failed\n");
6207 				goto out_err;
6208 			}
6209 		}
6210 
6211 		err = ieee80211_mgd_setup_link_sta(link, sta, link_sta,
6212 						   assoc_data->link[link_id].bss);
6213 		if (err)
6214 			goto out_err;
6215 
6216 		if (!ieee80211_assoc_config_link(link, link_sta,
6217 						 assoc_data->link[link_id].bss,
6218 						 mgmt, elem_start, elem_len,
6219 						 &changed[link_id]))
6220 			goto out_err;
6221 
6222 		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
6223 			valid_links &= ~BIT(link_id);
6224 			ieee80211_sta_remove_link(sta, link_id);
6225 			continue;
6226 		}
6227 
6228 		if (link_id != assoc_data->assoc_link_id) {
6229 			err = ieee80211_sta_activate_link(sta, link_id);
6230 			if (err)
6231 				goto out_err;
6232 		}
6233 	}
6234 
6235 	/* links might have changed due to rejected ones, set them again */
6236 	ieee80211_vif_set_links(sdata, valid_links, dormant_links);
6237 
6238 	rate_control_rate_init_all_links(sta);
6239 
6240 	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
6241 		set_sta_flag(sta, WLAN_STA_MFP);
6242 		sta->sta.mfp = true;
6243 	} else {
6244 		sta->sta.mfp = false;
6245 	}
6246 
6247 	ieee80211_sta_set_max_amsdu_subframes(sta, elems->ext_capab,
6248 					      elems->ext_capab_len);
6249 
6250 	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
6251 		       local->hw.queues >= IEEE80211_NUM_ACS;
6252 
6253 	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
6254 	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
6255 		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
6256 	if (err) {
6257 		sdata_info(sdata,
6258 			   "failed to move station %pM to desired state\n",
6259 			   sta->sta.addr);
6260 		WARN_ON(__sta_info_destroy(sta));
6261 		goto out_err;
6262 	}
6263 
6264 	if (sdata->wdev.use_4addr)
6265 		drv_sta_set_4addr(local, sdata, &sta->sta, true);
6266 
6267 	ieee80211_set_associated(sdata, assoc_data, changed);
6268 
6269 	/*
6270 	 * If we're using 4-addr mode, let the AP know that we're
6271 	 * doing so, so that it can create the STA VLAN on its side
6272 	 */
6273 	if (ifmgd->use_4addr)
6274 		ieee80211_send_4addr_nullfunc(local, sdata);
6275 
6276 	/*
6277 	 * Start timer to probe the connection to the AP now.
6278 	 * Also start the timer that will detect beacon loss.
6279 	 */
6280 	ieee80211_sta_reset_beacon_monitor(sdata);
6281 	ieee80211_sta_reset_conn_monitor(sdata);
6282 
6283 	return true;
6284 out_err:
6285 	eth_zero_addr(sdata->vif.cfg.ap_addr);
6286 	return false;
6287 }
6288 
6289 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
6290 					 struct ieee80211_mgmt *mgmt,
6291 					 size_t len)
6292 {
6293 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6294 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
6295 	u16 capab_info, status_code, aid;
6296 	struct ieee80211_elems_parse_params parse_params = {
6297 		.bss = NULL,
6298 		.link_id = -1,
6299 		.from_ap = true,
6300 	};
6301 	struct ieee802_11_elems *elems;
6302 	int ac;
6303 	const u8 *elem_start;
6304 	unsigned int elem_len;
6305 	bool reassoc;
6306 	struct ieee80211_event event = {
6307 		.type = MLME_EVENT,
6308 		.u.mlme.data = ASSOC_EVENT,
6309 	};
6310 	struct ieee80211_prep_tx_info info = {};
6311 	struct cfg80211_rx_assoc_resp_data resp = {
6312 		.uapsd_queues = -1,
6313 	};
6314 	u8 ap_mld_addr[ETH_ALEN] __aligned(2);
6315 	unsigned int link_id;
6316 
6317 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6318 
6319 	if (!assoc_data)
6320 		return;
6321 
6322 	info.link_id = assoc_data->assoc_link_id;
6323 
6324 	parse_params.mode =
6325 		assoc_data->link[assoc_data->assoc_link_id].conn.mode;
6326 
6327 	if (!ether_addr_equal(assoc_data->ap_addr, mgmt->bssid) ||
6328 	    !ether_addr_equal(assoc_data->ap_addr, mgmt->sa))
6329 		return;
6330 
6331 	/*
6332 	 * AssocResp and ReassocResp have identical structure, so process both
6333 	 * of them in this function.
6334 	 */
6335 
6336 	if (len < 24 + 6)
6337 		return;
6338 
6339 	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
6340 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
6341 	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
6342 	if (assoc_data->s1g)
6343 		elem_start = mgmt->u.s1g_assoc_resp.variable;
6344 	else
6345 		elem_start = mgmt->u.assoc_resp.variable;
6346 
6347 	/*
6348 	 * Note: this may not be perfect, AP might misbehave - if
6349 	 * anyone needs to rely on perfect complete notification
6350 	 * with the exact right subtype, then we need to track what
6351 	 * we actually transmitted.
6352 	 */
6353 	info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
6354 				 IEEE80211_STYPE_ASSOC_REQ;
6355 
6356 	if (assoc_data->fils_kek_len &&
6357 	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
6358 		return;
6359 
6360 	elem_len = len - (elem_start - (u8 *)mgmt);
6361 	parse_params.start = elem_start;
6362 	parse_params.len = elem_len;
6363 	elems = ieee802_11_parse_elems_full(&parse_params);
6364 	if (!elems)
6365 		goto notify_driver;
6366 
6367 	if (elems->aid_resp)
6368 		aid = le16_to_cpu(elems->aid_resp->aid);
6369 	else if (assoc_data->s1g)
6370 		aid = 0; /* TODO */
6371 	else
6372 		aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
6373 
6374 	/*
6375 	 * The 5 MSB of the AID field are reserved
6376 	 * (802.11-2016 9.4.1.8 AID field)
6377 	 */
6378 	aid &= 0x7ff;
6379 
6380 	sdata_info(sdata,
6381 		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
6382 		   reassoc ? "Rea" : "A", assoc_data->ap_addr,
6383 		   capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
6384 
6385 	ifmgd->broken_ap = false;
6386 
6387 	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
6388 	    elems->timeout_int &&
6389 	    elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
6390 		u32 tu, ms;
6391 
6392 		cfg80211_assoc_comeback(sdata->dev, assoc_data->ap_addr,
6393 					le32_to_cpu(elems->timeout_int->value));
6394 
6395 		tu = le32_to_cpu(elems->timeout_int->value);
6396 		ms = tu * 1024 / 1000;
6397 		sdata_info(sdata,
6398 			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
6399 			   assoc_data->ap_addr, tu, ms);
6400 		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
6401 		assoc_data->timeout_started = true;
6402 		assoc_data->comeback = true;
6403 		if (ms > IEEE80211_ASSOC_TIMEOUT)
6404 			run_again(sdata, assoc_data->timeout);
6405 		goto notify_driver;
6406 	}
6407 
6408 	if (status_code != WLAN_STATUS_SUCCESS) {
6409 		sdata_info(sdata, "%pM denied association (code=%d)\n",
6410 			   assoc_data->ap_addr, status_code);
6411 		event.u.mlme.status = MLME_DENIED;
6412 		event.u.mlme.reason = status_code;
6413 		drv_event_callback(sdata->local, sdata, &event);
6414 	} else {
6415 		if (aid == 0 || aid > IEEE80211_MAX_AID) {
6416 			sdata_info(sdata,
6417 				   "invalid AID value %d (out of range), turn off PS\n",
6418 				   aid);
6419 			aid = 0;
6420 			ifmgd->broken_ap = true;
6421 		}
6422 
6423 		if (ieee80211_vif_is_mld(&sdata->vif)) {
6424 			struct ieee80211_mle_basic_common_info *common;
6425 
6426 			if (!elems->ml_basic) {
6427 				sdata_info(sdata,
6428 					   "MLO association with %pM but no (basic) multi-link element in response!\n",
6429 					   assoc_data->ap_addr);
6430 				goto abandon_assoc;
6431 			}
6432 
6433 			common = (void *)elems->ml_basic->variable;
6434 
6435 			if (memcmp(assoc_data->ap_addr,
6436 				   common->mld_mac_addr, ETH_ALEN)) {
6437 				sdata_info(sdata,
6438 					   "AP MLD MAC address mismatch: got %pM expected %pM\n",
6439 					   common->mld_mac_addr,
6440 					   assoc_data->ap_addr);
6441 				goto abandon_assoc;
6442 			}
6443 
6444 			sdata->vif.cfg.eml_cap =
6445 				ieee80211_mle_get_eml_cap((const void *)elems->ml_basic);
6446 			sdata->vif.cfg.eml_med_sync_delay =
6447 				ieee80211_mle_get_eml_med_sync_delay((const void *)elems->ml_basic);
6448 			sdata->vif.cfg.mld_capa_op =
6449 				ieee80211_mle_get_mld_capa_op((const void *)elems->ml_basic);
6450 		}
6451 
6452 		sdata->vif.cfg.aid = aid;
6453 
6454 		if (!ieee80211_assoc_success(sdata, mgmt, elems,
6455 					     elem_start, elem_len)) {
6456 			/* oops -- internal error -- send timeout for now */
6457 			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
6458 			goto notify_driver;
6459 		}
6460 		event.u.mlme.status = MLME_SUCCESS;
6461 		drv_event_callback(sdata->local, sdata, &event);
6462 		sdata_info(sdata, "associated\n");
6463 
6464 		info.success = 1;
6465 	}
6466 
6467 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
6468 		struct ieee80211_link_data *link;
6469 
6470 		if (!assoc_data->link[link_id].bss)
6471 			continue;
6472 
6473 		resp.links[link_id].bss = assoc_data->link[link_id].bss;
6474 		ether_addr_copy(resp.links[link_id].addr,
6475 				assoc_data->link[link_id].addr);
6476 		resp.links[link_id].status = assoc_data->link[link_id].status;
6477 
6478 		link = sdata_dereference(sdata->link[link_id], sdata);
6479 		if (!link)
6480 			continue;
6481 
6482 		/* get uapsd queues configuration - same for all links */
6483 		resp.uapsd_queues = 0;
6484 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
6485 			if (link->tx_conf[ac].uapsd)
6486 				resp.uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
6487 	}
6488 
6489 	if (ieee80211_vif_is_mld(&sdata->vif)) {
6490 		ether_addr_copy(ap_mld_addr, sdata->vif.cfg.ap_addr);
6491 		resp.ap_mld_addr = ap_mld_addr;
6492 	}
6493 
6494 	ieee80211_destroy_assoc_data(sdata,
6495 				     status_code == WLAN_STATUS_SUCCESS ?
6496 					ASSOC_SUCCESS :
6497 					ASSOC_REJECTED);
6498 
6499 	resp.buf = (u8 *)mgmt;
6500 	resp.len = len;
6501 	resp.req_ies = ifmgd->assoc_req_ies;
6502 	resp.req_ies_len = ifmgd->assoc_req_ies_len;
6503 	cfg80211_rx_assoc_resp(sdata->dev, &resp);
6504 notify_driver:
6505 	drv_mgd_complete_tx(sdata->local, sdata, &info);
6506 	kfree(elems);
6507 	return;
6508 abandon_assoc:
6509 	ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
6510 	goto notify_driver;
6511 }
6512 
6513 static void ieee80211_rx_bss_info(struct ieee80211_link_data *link,
6514 				  struct ieee80211_mgmt *mgmt, size_t len,
6515 				  struct ieee80211_rx_status *rx_status)
6516 {
6517 	struct ieee80211_sub_if_data *sdata = link->sdata;
6518 	struct ieee80211_local *local = sdata->local;
6519 	struct ieee80211_bss *bss;
6520 	struct ieee80211_channel *channel;
6521 
6522 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6523 
6524 	channel = ieee80211_get_channel_khz(local->hw.wiphy,
6525 					ieee80211_rx_status_to_khz(rx_status));
6526 	if (!channel)
6527 		return;
6528 
6529 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
6530 	if (bss) {
6531 		link->conf->beacon_rate = bss->beacon_rate;
6532 		ieee80211_rx_bss_put(local, bss);
6533 	}
6534 }
6535 
6536 
6537 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_link_data *link,
6538 					 struct sk_buff *skb)
6539 {
6540 	struct ieee80211_sub_if_data *sdata = link->sdata;
6541 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
6542 	struct ieee80211_if_managed *ifmgd;
6543 	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
6544 	struct ieee80211_channel *channel;
6545 	size_t baselen, len = skb->len;
6546 
6547 	ifmgd = &sdata->u.mgd;
6548 
6549 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6550 
6551 	/*
6552 	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
6553 	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
6554 	 * a Probe Response frame [..], the Address 1 field of the Probe
6555 	 * Response frame shall be set to the broadcast address [..]"
6556 	 * So, on 6GHz band we should also accept broadcast responses.
6557 	 */
6558 	channel = ieee80211_get_channel(sdata->local->hw.wiphy,
6559 					rx_status->freq);
6560 	if (!channel)
6561 		return;
6562 
6563 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
6564 	    (channel->band != NL80211_BAND_6GHZ ||
6565 	     !is_broadcast_ether_addr(mgmt->da)))
6566 		return; /* ignore ProbeResp to foreign address */
6567 
6568 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
6569 	if (baselen > len)
6570 		return;
6571 
6572 	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
6573 
6574 	if (ifmgd->associated &&
6575 	    ether_addr_equal(mgmt->bssid, link->u.mgd.bssid))
6576 		ieee80211_reset_ap_probe(sdata);
6577 }
6578 
6579 /*
6580  * This is the canonical list of information elements we care about,
6581  * the filter code also gives us all changes to the Microsoft OUI
6582  * (00:50:F2) vendor IE which is used for WMM which we need to track,
6583  * as well as the DTPC IE (part of the Cisco OUI) used for signaling
6584  * changes to requested client power.
6585  *
6586  * We implement beacon filtering in software since that means we can
6587  * avoid processing the frame here and in cfg80211, and userspace
6588  * will not be able to tell whether the hardware supports it or not.
6589  *
6590  * XXX: This list needs to be dynamic -- userspace needs to be able to
6591  *	add items it requires. It also needs to be able to tell us to
6592  *	look out for other vendor IEs.
6593  */
6594 static const u64 care_about_ies =
6595 	(1ULL << WLAN_EID_COUNTRY) |
6596 	(1ULL << WLAN_EID_ERP_INFO) |
6597 	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
6598 	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
6599 	(1ULL << WLAN_EID_HT_CAPABILITY) |
6600 	(1ULL << WLAN_EID_HT_OPERATION) |
6601 	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
6602 
6603 static void ieee80211_handle_beacon_sig(struct ieee80211_link_data *link,
6604 					struct ieee80211_if_managed *ifmgd,
6605 					struct ieee80211_bss_conf *bss_conf,
6606 					struct ieee80211_local *local,
6607 					struct ieee80211_rx_status *rx_status)
6608 {
6609 	struct ieee80211_sub_if_data *sdata = link->sdata;
6610 
6611 	/* Track average RSSI from the Beacon frames of the current AP */
6612 
6613 	if (!link->u.mgd.tracking_signal_avg) {
6614 		link->u.mgd.tracking_signal_avg = true;
6615 		ewma_beacon_signal_init(&link->u.mgd.ave_beacon_signal);
6616 		link->u.mgd.last_cqm_event_signal = 0;
6617 		link->u.mgd.count_beacon_signal = 1;
6618 		link->u.mgd.last_ave_beacon_signal = 0;
6619 	} else {
6620 		link->u.mgd.count_beacon_signal++;
6621 	}
6622 
6623 	ewma_beacon_signal_add(&link->u.mgd.ave_beacon_signal,
6624 			       -rx_status->signal);
6625 
6626 	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
6627 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
6628 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
6629 		int last_sig = link->u.mgd.last_ave_beacon_signal;
6630 		struct ieee80211_event event = {
6631 			.type = RSSI_EVENT,
6632 		};
6633 
6634 		/*
6635 		 * if signal crosses either of the boundaries, invoke callback
6636 		 * with appropriate parameters
6637 		 */
6638 		if (sig > ifmgd->rssi_max_thold &&
6639 		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
6640 			link->u.mgd.last_ave_beacon_signal = sig;
6641 			event.u.rssi.data = RSSI_EVENT_HIGH;
6642 			drv_event_callback(local, sdata, &event);
6643 		} else if (sig < ifmgd->rssi_min_thold &&
6644 			   (last_sig >= ifmgd->rssi_max_thold ||
6645 			   last_sig == 0)) {
6646 			link->u.mgd.last_ave_beacon_signal = sig;
6647 			event.u.rssi.data = RSSI_EVENT_LOW;
6648 			drv_event_callback(local, sdata, &event);
6649 		}
6650 	}
6651 
6652 	if (bss_conf->cqm_rssi_thold &&
6653 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
6654 	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
6655 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
6656 		int last_event = link->u.mgd.last_cqm_event_signal;
6657 		int thold = bss_conf->cqm_rssi_thold;
6658 		int hyst = bss_conf->cqm_rssi_hyst;
6659 
6660 		if (sig < thold &&
6661 		    (last_event == 0 || sig < last_event - hyst)) {
6662 			link->u.mgd.last_cqm_event_signal = sig;
6663 			ieee80211_cqm_rssi_notify(
6664 				&sdata->vif,
6665 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
6666 				sig, GFP_KERNEL);
6667 		} else if (sig > thold &&
6668 			   (last_event == 0 || sig > last_event + hyst)) {
6669 			link->u.mgd.last_cqm_event_signal = sig;
6670 			ieee80211_cqm_rssi_notify(
6671 				&sdata->vif,
6672 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
6673 				sig, GFP_KERNEL);
6674 		}
6675 	}
6676 
6677 	if (bss_conf->cqm_rssi_low &&
6678 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
6679 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
6680 		int last_event = link->u.mgd.last_cqm_event_signal;
6681 		int low = bss_conf->cqm_rssi_low;
6682 		int high = bss_conf->cqm_rssi_high;
6683 
6684 		if (sig < low &&
6685 		    (last_event == 0 || last_event >= low)) {
6686 			link->u.mgd.last_cqm_event_signal = sig;
6687 			ieee80211_cqm_rssi_notify(
6688 				&sdata->vif,
6689 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
6690 				sig, GFP_KERNEL);
6691 		} else if (sig > high &&
6692 			   (last_event == 0 || last_event <= high)) {
6693 			link->u.mgd.last_cqm_event_signal = sig;
6694 			ieee80211_cqm_rssi_notify(
6695 				&sdata->vif,
6696 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
6697 				sig, GFP_KERNEL);
6698 		}
6699 	}
6700 }
6701 
6702 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
6703 				    struct cfg80211_bss *bss)
6704 {
6705 	if (ether_addr_equal(tx_bssid, bss->bssid))
6706 		return true;
6707 	if (!bss->transmitted_bss)
6708 		return false;
6709 	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
6710 }
6711 
6712 static void ieee80211_ml_reconf_work(struct wiphy *wiphy,
6713 				     struct wiphy_work *work)
6714 {
6715 	struct ieee80211_sub_if_data *sdata =
6716 		container_of(work, struct ieee80211_sub_if_data,
6717 			     u.mgd.ml_reconf_work.work);
6718 	u16 new_valid_links, new_active_links, new_dormant_links;
6719 	int ret;
6720 
6721 	if (!sdata->u.mgd.removed_links)
6722 		return;
6723 
6724 	sdata_info(sdata,
6725 		   "MLO Reconfiguration: work: valid=0x%x, removed=0x%x\n",
6726 		   sdata->vif.valid_links, sdata->u.mgd.removed_links);
6727 
6728 	new_valid_links = sdata->vif.valid_links & ~sdata->u.mgd.removed_links;
6729 	if (new_valid_links == sdata->vif.valid_links)
6730 		return;
6731 
6732 	if (!new_valid_links ||
6733 	    !(new_valid_links & ~sdata->vif.dormant_links)) {
6734 		sdata_info(sdata, "No valid links after reconfiguration\n");
6735 		ret = -EINVAL;
6736 		goto out;
6737 	}
6738 
6739 	new_active_links = sdata->vif.active_links & ~sdata->u.mgd.removed_links;
6740 	if (new_active_links != sdata->vif.active_links) {
6741 		if (!new_active_links)
6742 			new_active_links =
6743 				BIT(ffs(new_valid_links &
6744 					~sdata->vif.dormant_links) - 1);
6745 
6746 		ret = ieee80211_set_active_links(&sdata->vif, new_active_links);
6747 		if (ret) {
6748 			sdata_info(sdata,
6749 				   "Failed setting active links\n");
6750 			goto out;
6751 		}
6752 	}
6753 
6754 	new_dormant_links = sdata->vif.dormant_links & ~sdata->u.mgd.removed_links;
6755 
6756 	ret = ieee80211_vif_set_links(sdata, new_valid_links,
6757 				      new_dormant_links);
6758 	if (ret)
6759 		sdata_info(sdata, "Failed setting valid links\n");
6760 
6761 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
6762 
6763 out:
6764 	if (!ret)
6765 		cfg80211_links_removed(sdata->dev, sdata->u.mgd.removed_links);
6766 	else
6767 		__ieee80211_disconnect(sdata);
6768 
6769 	sdata->u.mgd.removed_links = 0;
6770 }
6771 
6772 static void ieee80211_ml_reconfiguration(struct ieee80211_sub_if_data *sdata,
6773 					 struct ieee802_11_elems *elems)
6774 {
6775 	const struct element *sub;
6776 	unsigned long removed_links = 0;
6777 	u16 link_removal_timeout[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6778 	u8 link_id;
6779 	u32 delay;
6780 
6781 	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_reconf)
6782 		return;
6783 
6784 	/* Directly parse the sub elements as the common information doesn't
6785 	 * hold any useful information.
6786 	 */
6787 	for_each_mle_subelement(sub, (const u8 *)elems->ml_reconf,
6788 				elems->ml_reconf_len) {
6789 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
6790 		u8 *pos = prof->variable;
6791 		u16 control;
6792 
6793 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
6794 			continue;
6795 
6796 		if (!ieee80211_mle_reconf_sta_prof_size_ok(sub->data,
6797 							   sub->datalen))
6798 			return;
6799 
6800 		control = le16_to_cpu(prof->control);
6801 		link_id = control & IEEE80211_MLE_STA_RECONF_CONTROL_LINK_ID;
6802 
6803 		removed_links |= BIT(link_id);
6804 
6805 		/* the MAC address should not be included, but handle it */
6806 		if (control &
6807 		    IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT)
6808 			pos += 6;
6809 
6810 		/* According to Draft P802.11be_D3.0, the control should
6811 		 * include the AP Removal Timer present. If the AP Removal Timer
6812 		 * is not present assume immediate removal.
6813 		 */
6814 		if (control &
6815 		    IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT)
6816 			link_removal_timeout[link_id] = get_unaligned_le16(pos);
6817 	}
6818 
6819 	removed_links &= sdata->vif.valid_links;
6820 	if (!removed_links) {
6821 		/* In case the removal was cancelled, abort it */
6822 		if (sdata->u.mgd.removed_links) {
6823 			sdata->u.mgd.removed_links = 0;
6824 			wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
6825 						  &sdata->u.mgd.ml_reconf_work);
6826 		}
6827 		return;
6828 	}
6829 
6830 	delay = 0;
6831 	for_each_set_bit(link_id, &removed_links, IEEE80211_MLD_MAX_NUM_LINKS) {
6832 		struct ieee80211_bss_conf *link_conf =
6833 			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
6834 		u32 link_delay;
6835 
6836 		if (!link_conf) {
6837 			removed_links &= ~BIT(link_id);
6838 			continue;
6839 		}
6840 
6841 		if (link_removal_timeout[link_id] < 1)
6842 			link_delay = 0;
6843 		else
6844 			link_delay = link_conf->beacon_int *
6845 				(link_removal_timeout[link_id] - 1);
6846 
6847 		if (!delay)
6848 			delay = link_delay;
6849 		else
6850 			delay = min(delay, link_delay);
6851 	}
6852 
6853 	sdata->u.mgd.removed_links = removed_links;
6854 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
6855 				 &sdata->u.mgd.ml_reconf_work,
6856 				 TU_TO_JIFFIES(delay));
6857 }
6858 
6859 static int ieee80211_ttlm_set_links(struct ieee80211_sub_if_data *sdata,
6860 				    u16 active_links, u16 dormant_links,
6861 				    u16 suspended_links)
6862 {
6863 	u64 changed = 0;
6864 	int ret;
6865 
6866 	if (!active_links) {
6867 		ret = -EINVAL;
6868 		goto out;
6869 	}
6870 
6871 	/* If there is an active negotiated TTLM, it should be discarded by
6872 	 * the new negotiated/advertised TTLM.
6873 	 */
6874 	if (sdata->vif.neg_ttlm.valid) {
6875 		memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
6876 		sdata->vif.suspended_links = 0;
6877 		changed = BSS_CHANGED_MLD_TTLM;
6878 	}
6879 
6880 	if (sdata->vif.active_links != active_links) {
6881 		/* usable links are affected when active_links are changed,
6882 		 * so notify the driver about the status change
6883 		 */
6884 		changed |= BSS_CHANGED_MLD_VALID_LINKS;
6885 		active_links &= sdata->vif.active_links;
6886 		if (!active_links)
6887 			active_links =
6888 				BIT(__ffs(sdata->vif.valid_links &
6889 				    ~dormant_links));
6890 		ret = ieee80211_set_active_links(&sdata->vif, active_links);
6891 		if (ret) {
6892 			sdata_info(sdata, "Failed to set TTLM active links\n");
6893 			goto out;
6894 		}
6895 	}
6896 
6897 	ret = ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
6898 				      dormant_links);
6899 	if (ret) {
6900 		sdata_info(sdata, "Failed to set TTLM dormant links\n");
6901 		goto out;
6902 	}
6903 
6904 	sdata->vif.suspended_links = suspended_links;
6905 	if (sdata->vif.suspended_links)
6906 		changed |= BSS_CHANGED_MLD_TTLM;
6907 
6908 	ieee80211_vif_cfg_change_notify(sdata, changed);
6909 
6910 out:
6911 	if (ret)
6912 		ieee80211_disconnect(&sdata->vif, false);
6913 
6914 	return ret;
6915 }
6916 
6917 static void ieee80211_tid_to_link_map_work(struct wiphy *wiphy,
6918 					   struct wiphy_work *work)
6919 {
6920 	u16 new_active_links, new_dormant_links;
6921 	struct ieee80211_sub_if_data *sdata =
6922 		container_of(work, struct ieee80211_sub_if_data,
6923 			     u.mgd.ttlm_work.work);
6924 
6925 	new_active_links = sdata->u.mgd.ttlm_info.map &
6926 			   sdata->vif.valid_links;
6927 	new_dormant_links = ~sdata->u.mgd.ttlm_info.map &
6928 			    sdata->vif.valid_links;
6929 
6930 	ieee80211_vif_set_links(sdata, sdata->vif.valid_links, 0);
6931 	if (ieee80211_ttlm_set_links(sdata, new_active_links, new_dormant_links,
6932 				     0))
6933 		return;
6934 
6935 	sdata->u.mgd.ttlm_info.active = true;
6936 	sdata->u.mgd.ttlm_info.switch_time = 0;
6937 }
6938 
6939 static u16 ieee80211_get_ttlm(u8 bm_size, u8 *data)
6940 {
6941 	if (bm_size == 1)
6942 		return *data;
6943 	else
6944 		return get_unaligned_le16(data);
6945 }
6946 
6947 static int
6948 ieee80211_parse_adv_t2l(struct ieee80211_sub_if_data *sdata,
6949 			const struct ieee80211_ttlm_elem *ttlm,
6950 			struct ieee80211_adv_ttlm_info *ttlm_info)
6951 {
6952 	/* The element size was already validated in
6953 	 * ieee80211_tid_to_link_map_size_ok()
6954 	 */
6955 	u8 control, link_map_presence, map_size, tid;
6956 	u8 *pos;
6957 
6958 	memset(ttlm_info, 0, sizeof(*ttlm_info));
6959 	pos = (void *)ttlm->optional;
6960 	control	= ttlm->control;
6961 
6962 	if ((control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) ||
6963 	    !(control & IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT))
6964 		return 0;
6965 
6966 	if ((control & IEEE80211_TTLM_CONTROL_DIRECTION) !=
6967 	    IEEE80211_TTLM_DIRECTION_BOTH) {
6968 		sdata_info(sdata, "Invalid advertised T2L map direction\n");
6969 		return -EINVAL;
6970 	}
6971 
6972 	link_map_presence = *pos;
6973 	pos++;
6974 
6975 	ttlm_info->switch_time = get_unaligned_le16(pos);
6976 
6977 	/* Since ttlm_info->switch_time == 0 means no switch time, bump it
6978 	 * by 1.
6979 	 */
6980 	if (!ttlm_info->switch_time)
6981 		ttlm_info->switch_time = 1;
6982 
6983 	pos += 2;
6984 
6985 	if (control & IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT) {
6986 		ttlm_info->duration = pos[0] | pos[1] << 8 | pos[2] << 16;
6987 		pos += 3;
6988 	}
6989 
6990 	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
6991 		map_size = 1;
6992 	else
6993 		map_size = 2;
6994 
6995 	/* According to Draft P802.11be_D3.0 clause 35.3.7.1.7, an AP MLD shall
6996 	 * not advertise a TID-to-link mapping that does not map all TIDs to the
6997 	 * same link set, reject frame if not all links have mapping
6998 	 */
6999 	if (link_map_presence != 0xff) {
7000 		sdata_info(sdata,
7001 			   "Invalid advertised T2L mapping presence indicator\n");
7002 		return -EINVAL;
7003 	}
7004 
7005 	ttlm_info->map = ieee80211_get_ttlm(map_size, pos);
7006 	if (!ttlm_info->map) {
7007 		sdata_info(sdata,
7008 			   "Invalid advertised T2L map for TID 0\n");
7009 		return -EINVAL;
7010 	}
7011 
7012 	pos += map_size;
7013 
7014 	for (tid = 1; tid < 8; tid++) {
7015 		u16 map = ieee80211_get_ttlm(map_size, pos);
7016 
7017 		if (map != ttlm_info->map) {
7018 			sdata_info(sdata, "Invalid advertised T2L map for tid %d\n",
7019 				   tid);
7020 			return -EINVAL;
7021 		}
7022 
7023 		pos += map_size;
7024 	}
7025 	return 0;
7026 }
7027 
7028 static void ieee80211_process_adv_ttlm(struct ieee80211_sub_if_data *sdata,
7029 					  struct ieee802_11_elems *elems,
7030 					  u64 beacon_ts)
7031 {
7032 	u8 i;
7033 	int ret;
7034 
7035 	if (!ieee80211_vif_is_mld(&sdata->vif))
7036 		return;
7037 
7038 	if (!elems->ttlm_num) {
7039 		if (sdata->u.mgd.ttlm_info.switch_time) {
7040 			/* if a planned TID-to-link mapping was cancelled -
7041 			 * abort it
7042 			 */
7043 			wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
7044 						  &sdata->u.mgd.ttlm_work);
7045 		} else if (sdata->u.mgd.ttlm_info.active) {
7046 			/* if no TID-to-link element, set to default mapping in
7047 			 * which all TIDs are mapped to all setup links
7048 			 */
7049 			ret = ieee80211_vif_set_links(sdata,
7050 						      sdata->vif.valid_links,
7051 						      0);
7052 			if (ret) {
7053 				sdata_info(sdata, "Failed setting valid/dormant links\n");
7054 				return;
7055 			}
7056 			ieee80211_vif_cfg_change_notify(sdata,
7057 							BSS_CHANGED_MLD_VALID_LINKS);
7058 		}
7059 		memset(&sdata->u.mgd.ttlm_info, 0,
7060 		       sizeof(sdata->u.mgd.ttlm_info));
7061 		return;
7062 	}
7063 
7064 	for (i = 0; i < elems->ttlm_num; i++) {
7065 		struct ieee80211_adv_ttlm_info ttlm_info;
7066 		u32 res;
7067 
7068 		res = ieee80211_parse_adv_t2l(sdata, elems->ttlm[i],
7069 					      &ttlm_info);
7070 
7071 		if (res) {
7072 			__ieee80211_disconnect(sdata);
7073 			return;
7074 		}
7075 
7076 		if (ttlm_info.switch_time) {
7077 			u16 beacon_ts_tu, st_tu, delay;
7078 			u32 delay_jiffies;
7079 			u64 mask;
7080 
7081 			/* The t2l map switch time is indicated with a partial
7082 			 * TSF value (bits 10 to 25), get the partial beacon TS
7083 			 * as well, and calc the delay to the start time.
7084 			 */
7085 			mask = GENMASK_ULL(25, 10);
7086 			beacon_ts_tu = (beacon_ts & mask) >> 10;
7087 			st_tu = ttlm_info.switch_time;
7088 			delay = st_tu - beacon_ts_tu;
7089 
7090 			/*
7091 			 * If the switch time is far in the future, then it
7092 			 * could also be the previous switch still being
7093 			 * announced.
7094 			 * We can simply ignore it for now, if it is a future
7095 			 * switch the AP will continue to announce it anyway.
7096 			 */
7097 			if (delay > IEEE80211_ADV_TTLM_ST_UNDERFLOW)
7098 				return;
7099 
7100 			delay_jiffies = TU_TO_JIFFIES(delay);
7101 
7102 			/* Link switching can take time, so schedule it
7103 			 * 100ms before to be ready on time
7104 			 */
7105 			if (delay_jiffies > IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS)
7106 				delay_jiffies -=
7107 					IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS;
7108 			else
7109 				delay_jiffies = 0;
7110 
7111 			sdata->u.mgd.ttlm_info = ttlm_info;
7112 			wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
7113 						  &sdata->u.mgd.ttlm_work);
7114 			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
7115 						 &sdata->u.mgd.ttlm_work,
7116 						 delay_jiffies);
7117 			return;
7118 		}
7119 	}
7120 }
7121 
7122 static void
7123 ieee80211_mgd_check_cross_link_csa(struct ieee80211_sub_if_data *sdata,
7124 				   int reporting_link_id,
7125 				   struct ieee802_11_elems *elems)
7126 {
7127 	const struct element *sta_profiles[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7128 	ssize_t sta_profiles_len[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7129 	const struct element *sub;
7130 	const u8 *subelems;
7131 	size_t subelems_len;
7132 	u8 common_size;
7133 	int link_id;
7134 
7135 	if (!ieee80211_mle_size_ok((u8 *)elems->ml_basic, elems->ml_basic_len))
7136 		return;
7137 
7138 	common_size = ieee80211_mle_common_size((u8 *)elems->ml_basic);
7139 	subelems = (u8 *)elems->ml_basic + common_size;
7140 	subelems_len = elems->ml_basic_len - common_size;
7141 
7142 	for_each_element_id(sub, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
7143 			    subelems, subelems_len) {
7144 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
7145 		struct ieee80211_link_data *link;
7146 		ssize_t len;
7147 
7148 		if (!ieee80211_mle_basic_sta_prof_size_ok(sub->data,
7149 							  sub->datalen))
7150 			continue;
7151 
7152 		link_id = le16_get_bits(prof->control,
7153 					IEEE80211_MLE_STA_CONTROL_LINK_ID);
7154 		/* need a valid link ID, but also not our own, both AP bugs */
7155 		if (link_id == reporting_link_id ||
7156 		    link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7157 			continue;
7158 
7159 		link = sdata_dereference(sdata->link[link_id], sdata);
7160 		if (!link)
7161 			continue;
7162 
7163 		len = cfg80211_defragment_element(sub, subelems, subelems_len,
7164 						  NULL, 0,
7165 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
7166 		if (WARN_ON(len < 0))
7167 			continue;
7168 
7169 		sta_profiles[link_id] = sub;
7170 		sta_profiles_len[link_id] = len;
7171 	}
7172 
7173 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
7174 		struct ieee80211_mle_per_sta_profile *prof;
7175 		struct ieee802_11_elems *prof_elems;
7176 		struct ieee80211_link_data *link;
7177 		ssize_t len;
7178 
7179 		if (link_id == reporting_link_id)
7180 			continue;
7181 
7182 		link = sdata_dereference(sdata->link[link_id], sdata);
7183 		if (!link)
7184 			continue;
7185 
7186 		if (!sta_profiles[link_id]) {
7187 			prof_elems = NULL;
7188 			goto handle;
7189 		}
7190 
7191 		/* we can defragment in-place, won't use the buffer again */
7192 		len = cfg80211_defragment_element(sta_profiles[link_id],
7193 						  subelems, subelems_len,
7194 						  (void *)sta_profiles[link_id],
7195 						  sta_profiles_len[link_id],
7196 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
7197 		if (WARN_ON(len != sta_profiles_len[link_id]))
7198 			continue;
7199 
7200 		prof = (void *)sta_profiles[link_id];
7201 		prof_elems = ieee802_11_parse_elems(prof->variable +
7202 						    (prof->sta_info_len - 1),
7203 						    len -
7204 						    (prof->sta_info_len - 1),
7205 						    false, NULL);
7206 
7207 		/* memory allocation failed - let's hope that's transient */
7208 		if (!prof_elems)
7209 			continue;
7210 
7211 handle:
7212 		/*
7213 		 * FIXME: the timings here are obviously incorrect,
7214 		 * but only older Intel drivers seem to care, and
7215 		 * those don't have MLO. If you really need this,
7216 		 * the problem is having to calculate it with the
7217 		 * TSF offset etc. The device_timestamp is still
7218 		 * correct, of course.
7219 		 */
7220 		ieee80211_sta_process_chanswitch(link, 0, 0, elems, prof_elems,
7221 						 IEEE80211_CSA_SOURCE_OTHER_LINK);
7222 		kfree(prof_elems);
7223 	}
7224 }
7225 
7226 static bool ieee80211_mgd_ssid_mismatch(struct ieee80211_sub_if_data *sdata,
7227 					const struct ieee802_11_elems *elems)
7228 {
7229 	struct ieee80211_vif_cfg *cfg = &sdata->vif.cfg;
7230 	static u8 zero_ssid[IEEE80211_MAX_SSID_LEN];
7231 
7232 	if (!elems->ssid)
7233 		return false;
7234 
7235 	/* hidden SSID: zero length */
7236 	if (elems->ssid_len == 0)
7237 		return false;
7238 
7239 	if (elems->ssid_len != cfg->ssid_len)
7240 		return true;
7241 
7242 	/* hidden SSID: zeroed out */
7243 	if (!memcmp(elems->ssid, zero_ssid, elems->ssid_len))
7244 		return false;
7245 
7246 	return memcmp(elems->ssid, cfg->ssid, cfg->ssid_len);
7247 }
7248 
7249 static void ieee80211_rx_mgmt_beacon(struct ieee80211_link_data *link,
7250 				     struct ieee80211_hdr *hdr, size_t len,
7251 				     struct ieee80211_rx_status *rx_status)
7252 {
7253 	struct ieee80211_sub_if_data *sdata = link->sdata;
7254 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7255 	struct ieee80211_bss_conf *bss_conf = link->conf;
7256 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
7257 	struct ieee80211_mgmt *mgmt = (void *) hdr;
7258 	size_t baselen;
7259 	struct ieee802_11_elems *elems;
7260 	struct ieee80211_local *local = sdata->local;
7261 	struct ieee80211_chanctx_conf *chanctx_conf;
7262 	struct ieee80211_supported_band *sband;
7263 	struct ieee80211_channel *chan;
7264 	struct link_sta_info *link_sta;
7265 	struct sta_info *sta;
7266 	u64 changed = 0;
7267 	bool erp_valid;
7268 	u8 erp_value = 0;
7269 	u32 ncrc = 0;
7270 	u8 *bssid, *variable = mgmt->u.beacon.variable;
7271 	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
7272 	struct ieee80211_elems_parse_params parse_params = {
7273 		.mode = link->u.mgd.conn.mode,
7274 		.link_id = -1,
7275 		.from_ap = true,
7276 	};
7277 
7278 	lockdep_assert_wiphy(local->hw.wiphy);
7279 
7280 	/* Process beacon from the current BSS */
7281 	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
7282 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
7283 		struct ieee80211_ext *ext = (void *) mgmt;
7284 		variable = ext->u.s1g_beacon.variable +
7285 			   ieee80211_s1g_optional_len(ext->frame_control);
7286 	}
7287 
7288 	baselen = (u8 *) variable - (u8 *) mgmt;
7289 	if (baselen > len)
7290 		return;
7291 
7292 	parse_params.start = variable;
7293 	parse_params.len = len - baselen;
7294 
7295 	rcu_read_lock();
7296 	chanctx_conf = rcu_dereference(bss_conf->chanctx_conf);
7297 	if (!chanctx_conf) {
7298 		rcu_read_unlock();
7299 		return;
7300 	}
7301 
7302 	if (ieee80211_rx_status_to_khz(rx_status) !=
7303 	    ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
7304 		rcu_read_unlock();
7305 		return;
7306 	}
7307 	chan = chanctx_conf->def.chan;
7308 	rcu_read_unlock();
7309 
7310 	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
7311 	    !WARN_ON(ieee80211_vif_is_mld(&sdata->vif)) &&
7312 	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->link[0].bss)) {
7313 		parse_params.bss = ifmgd->assoc_data->link[0].bss;
7314 		elems = ieee802_11_parse_elems_full(&parse_params);
7315 		if (!elems)
7316 			return;
7317 
7318 		ieee80211_rx_bss_info(link, mgmt, len, rx_status);
7319 
7320 		if (elems->dtim_period)
7321 			link->u.mgd.dtim_period = elems->dtim_period;
7322 		link->u.mgd.have_beacon = true;
7323 		ifmgd->assoc_data->need_beacon = false;
7324 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
7325 		    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
7326 			bss_conf->sync_tsf =
7327 				le64_to_cpu(mgmt->u.beacon.timestamp);
7328 			bss_conf->sync_device_ts =
7329 				rx_status->device_timestamp;
7330 			bss_conf->sync_dtim_count = elems->dtim_count;
7331 		}
7332 
7333 		if (elems->mbssid_config_ie)
7334 			bss_conf->profile_periodicity =
7335 				elems->mbssid_config_ie->profile_periodicity;
7336 		else
7337 			bss_conf->profile_periodicity = 0;
7338 
7339 		if (elems->ext_capab_len >= 11 &&
7340 		    (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
7341 			bss_conf->ema_ap = true;
7342 		else
7343 			bss_conf->ema_ap = false;
7344 
7345 		/* continue assoc process */
7346 		ifmgd->assoc_data->timeout = jiffies;
7347 		ifmgd->assoc_data->timeout_started = true;
7348 		run_again(sdata, ifmgd->assoc_data->timeout);
7349 		kfree(elems);
7350 		return;
7351 	}
7352 
7353 	if (!ifmgd->associated ||
7354 	    !ieee80211_rx_our_beacon(bssid, bss_conf->bss))
7355 		return;
7356 	bssid = link->u.mgd.bssid;
7357 
7358 	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
7359 		ieee80211_handle_beacon_sig(link, ifmgd, bss_conf,
7360 					    local, rx_status);
7361 
7362 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
7363 		mlme_dbg_ratelimited(sdata,
7364 				     "cancelling AP probe due to a received beacon\n");
7365 		ieee80211_reset_ap_probe(sdata);
7366 	}
7367 
7368 	/*
7369 	 * Push the beacon loss detection into the future since
7370 	 * we are processing a beacon from the AP just now.
7371 	 */
7372 	ieee80211_sta_reset_beacon_monitor(sdata);
7373 
7374 	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
7375 	 * element (which carries the beacon interval). Don't forget to add a
7376 	 * bit to care_about_ies[] above if mac80211 is interested in a
7377 	 * changing S1G element.
7378 	 */
7379 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
7380 		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
7381 	parse_params.bss = bss_conf->bss;
7382 	parse_params.filter = care_about_ies;
7383 	parse_params.crc = ncrc;
7384 	elems = ieee802_11_parse_elems_full(&parse_params);
7385 	if (!elems)
7386 		return;
7387 
7388 	if (rx_status->flag & RX_FLAG_DECRYPTED &&
7389 	    ieee80211_mgd_ssid_mismatch(sdata, elems)) {
7390 		sdata_info(sdata, "SSID mismatch for AP %pM, disconnect\n",
7391 			   sdata->vif.cfg.ap_addr);
7392 		__ieee80211_disconnect(sdata);
7393 		return;
7394 	}
7395 
7396 	ncrc = elems->crc;
7397 
7398 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
7399 	    ieee80211_check_tim(elems->tim, elems->tim_len, vif_cfg->aid)) {
7400 		if (local->hw.conf.dynamic_ps_timeout > 0) {
7401 			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
7402 				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
7403 				ieee80211_hw_config(local, -1,
7404 						    IEEE80211_CONF_CHANGE_PS);
7405 			}
7406 			ieee80211_send_nullfunc(local, sdata, false);
7407 		} else if (!local->pspolling && sdata->u.mgd.powersave) {
7408 			local->pspolling = true;
7409 
7410 			/*
7411 			 * Here is assumed that the driver will be
7412 			 * able to send ps-poll frame and receive a
7413 			 * response even though power save mode is
7414 			 * enabled, but some drivers might require
7415 			 * to disable power save here. This needs
7416 			 * to be investigated.
7417 			 */
7418 			ieee80211_send_pspoll(local, sdata);
7419 		}
7420 	}
7421 
7422 	if (sdata->vif.p2p ||
7423 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
7424 		struct ieee80211_p2p_noa_attr noa = {};
7425 		int ret;
7426 
7427 		ret = cfg80211_get_p2p_attr(variable,
7428 					    len - baselen,
7429 					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
7430 					    (u8 *) &noa, sizeof(noa));
7431 		if (ret >= 2) {
7432 			if (link->u.mgd.p2p_noa_index != noa.index) {
7433 				/* valid noa_attr and index changed */
7434 				link->u.mgd.p2p_noa_index = noa.index;
7435 				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
7436 				changed |= BSS_CHANGED_P2P_PS;
7437 				/*
7438 				 * make sure we update all information, the CRC
7439 				 * mechanism doesn't look at P2P attributes.
7440 				 */
7441 				link->u.mgd.beacon_crc_valid = false;
7442 			}
7443 		} else if (link->u.mgd.p2p_noa_index != -1) {
7444 			/* noa_attr not found and we had valid noa_attr before */
7445 			link->u.mgd.p2p_noa_index = -1;
7446 			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
7447 			changed |= BSS_CHANGED_P2P_PS;
7448 			link->u.mgd.beacon_crc_valid = false;
7449 		}
7450 	}
7451 
7452 	/*
7453 	 * Update beacon timing and dtim count on every beacon appearance. This
7454 	 * will allow the driver to use the most updated values. Do it before
7455 	 * comparing this one with last received beacon.
7456 	 * IMPORTANT: These parameters would possibly be out of sync by the time
7457 	 * the driver will use them. The synchronized view is currently
7458 	 * guaranteed only in certain callbacks.
7459 	 */
7460 	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
7461 	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
7462 		bss_conf->sync_tsf =
7463 			le64_to_cpu(mgmt->u.beacon.timestamp);
7464 		bss_conf->sync_device_ts =
7465 			rx_status->device_timestamp;
7466 		bss_conf->sync_dtim_count = elems->dtim_count;
7467 	}
7468 
7469 	if ((ncrc == link->u.mgd.beacon_crc && link->u.mgd.beacon_crc_valid) ||
7470 	    ieee80211_is_s1g_short_beacon(mgmt->frame_control))
7471 		goto free;
7472 	link->u.mgd.beacon_crc = ncrc;
7473 	link->u.mgd.beacon_crc_valid = true;
7474 
7475 	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
7476 
7477 	ieee80211_sta_process_chanswitch(link, rx_status->mactime,
7478 					 rx_status->device_timestamp,
7479 					 elems, elems,
7480 					 IEEE80211_CSA_SOURCE_BEACON);
7481 
7482 	/* note that after this elems->ml_basic can no longer be used fully */
7483 	ieee80211_mgd_check_cross_link_csa(sdata, rx_status->link_id, elems);
7484 
7485 	ieee80211_mgd_update_bss_param_ch_cnt(sdata, bss_conf, elems);
7486 
7487 	if (!sdata->u.mgd.epcs.enabled &&
7488 	    !link->u.mgd.disable_wmm_tracking &&
7489 	    ieee80211_sta_wmm_params(local, link, elems->wmm_param,
7490 				     elems->wmm_param_len,
7491 				     elems->mu_edca_param_set))
7492 		changed |= BSS_CHANGED_QOS;
7493 
7494 	/*
7495 	 * If we haven't had a beacon before, tell the driver about the
7496 	 * DTIM period (and beacon timing if desired) now.
7497 	 */
7498 	if (!link->u.mgd.have_beacon) {
7499 		/* a few bogus AP send dtim_period = 0 or no TIM IE */
7500 		bss_conf->dtim_period = elems->dtim_period ?: 1;
7501 
7502 		changed |= BSS_CHANGED_BEACON_INFO;
7503 		link->u.mgd.have_beacon = true;
7504 
7505 		ieee80211_recalc_ps(local);
7506 
7507 		ieee80211_recalc_ps_vif(sdata);
7508 	}
7509 
7510 	if (elems->erp_info) {
7511 		erp_valid = true;
7512 		erp_value = elems->erp_info[0];
7513 	} else {
7514 		erp_valid = false;
7515 	}
7516 
7517 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
7518 		changed |= ieee80211_handle_bss_capability(link,
7519 				le16_to_cpu(mgmt->u.beacon.capab_info),
7520 				erp_valid, erp_value);
7521 
7522 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
7523 	if (WARN_ON(!sta)) {
7524 		goto free;
7525 	}
7526 	link_sta = rcu_dereference_protected(sta->link[link->link_id],
7527 					     lockdep_is_held(&local->hw.wiphy->mtx));
7528 	if (WARN_ON(!link_sta)) {
7529 		goto free;
7530 	}
7531 
7532 	if (WARN_ON(!bss_conf->chanreq.oper.chan))
7533 		goto free;
7534 
7535 	sband = local->hw.wiphy->bands[bss_conf->chanreq.oper.chan->band];
7536 
7537 	changed |= ieee80211_recalc_twt_req(sdata, sband, link, link_sta, elems);
7538 
7539 	if (ieee80211_config_bw(link, elems, true, &changed, "beacon")) {
7540 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
7541 				       WLAN_REASON_DEAUTH_LEAVING,
7542 				       true, deauth_buf);
7543 		ieee80211_report_disconnect(sdata, deauth_buf,
7544 					    sizeof(deauth_buf), true,
7545 					    WLAN_REASON_DEAUTH_LEAVING,
7546 					    false);
7547 		goto free;
7548 	}
7549 
7550 	if (elems->opmode_notif)
7551 		ieee80211_vht_handle_opmode(sdata, link_sta,
7552 					    *elems->opmode_notif,
7553 					    rx_status->band);
7554 
7555 	changed |= ieee80211_handle_pwr_constr(link, chan, mgmt,
7556 					       elems->country_elem,
7557 					       elems->country_elem_len,
7558 					       elems->pwr_constr_elem,
7559 					       elems->cisco_dtpc_elem);
7560 
7561 	ieee80211_ml_reconfiguration(sdata, elems);
7562 	ieee80211_process_adv_ttlm(sdata, elems,
7563 				      le64_to_cpu(mgmt->u.beacon.timestamp));
7564 
7565 	ieee80211_link_info_change_notify(sdata, link, changed);
7566 free:
7567 	kfree(elems);
7568 }
7569 
7570 static void ieee80211_apply_neg_ttlm(struct ieee80211_sub_if_data *sdata,
7571 				     struct ieee80211_neg_ttlm neg_ttlm)
7572 {
7573 	u16 new_active_links, new_dormant_links, new_suspended_links, map = 0;
7574 	u8 i;
7575 
7576 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++)
7577 		map |= neg_ttlm.downlink[i] | neg_ttlm.uplink[i];
7578 
7579 	/* If there is an active TTLM, unset previously suspended links */
7580 	if (sdata->vif.neg_ttlm.valid)
7581 		sdata->vif.dormant_links &= ~sdata->vif.suspended_links;
7582 
7583 	/* exclude links that are already disabled by advertised TTLM */
7584 	new_active_links =
7585 		map & sdata->vif.valid_links & ~sdata->vif.dormant_links;
7586 	new_suspended_links =
7587 		(~map & sdata->vif.valid_links) & ~sdata->vif.dormant_links;
7588 	new_dormant_links = sdata->vif.dormant_links | new_suspended_links;
7589 	if (ieee80211_ttlm_set_links(sdata, new_active_links,
7590 				     new_dormant_links, new_suspended_links))
7591 		return;
7592 
7593 	sdata->vif.neg_ttlm = neg_ttlm;
7594 	sdata->vif.neg_ttlm.valid = true;
7595 }
7596 
7597 static void ieee80211_neg_ttlm_timeout_work(struct wiphy *wiphy,
7598 					    struct wiphy_work *work)
7599 {
7600 	struct ieee80211_sub_if_data *sdata =
7601 		container_of(work, struct ieee80211_sub_if_data,
7602 			     u.mgd.neg_ttlm_timeout_work.work);
7603 
7604 	sdata_info(sdata,
7605 		   "No negotiated TTLM response from AP, disconnecting.\n");
7606 
7607 	__ieee80211_disconnect(sdata);
7608 }
7609 
7610 static void
7611 ieee80211_neg_ttlm_add_suggested_map(struct sk_buff *skb,
7612 				     struct ieee80211_neg_ttlm *neg_ttlm)
7613 {
7614 	u8 i, direction[IEEE80211_TTLM_MAX_CNT];
7615 
7616 	if (memcmp(neg_ttlm->downlink, neg_ttlm->uplink,
7617 		   sizeof(neg_ttlm->downlink))) {
7618 		direction[0] = IEEE80211_TTLM_DIRECTION_DOWN;
7619 		direction[1] = IEEE80211_TTLM_DIRECTION_UP;
7620 	} else {
7621 		direction[0] = IEEE80211_TTLM_DIRECTION_BOTH;
7622 	}
7623 
7624 	for (i = 0; i < ARRAY_SIZE(direction); i++) {
7625 		u8 tid, len, map_ind = 0, *len_pos, *map_ind_pos, *pos;
7626 		__le16 map;
7627 
7628 		len = sizeof(struct ieee80211_ttlm_elem) + 1 + 1;
7629 
7630 		pos = skb_put(skb, len + 2);
7631 		*pos++ = WLAN_EID_EXTENSION;
7632 		len_pos = pos++;
7633 		*pos++ = WLAN_EID_EXT_TID_TO_LINK_MAPPING;
7634 		*pos++ = direction[i];
7635 		map_ind_pos = pos++;
7636 		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
7637 			map = direction[i] == IEEE80211_TTLM_DIRECTION_UP ?
7638 				cpu_to_le16(neg_ttlm->uplink[tid]) :
7639 				cpu_to_le16(neg_ttlm->downlink[tid]);
7640 			if (!map)
7641 				continue;
7642 
7643 			len += 2;
7644 			map_ind |= BIT(tid);
7645 			skb_put_data(skb, &map, sizeof(map));
7646 		}
7647 
7648 		*map_ind_pos = map_ind;
7649 		*len_pos = len;
7650 
7651 		if (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH)
7652 			break;
7653 	}
7654 }
7655 
7656 static void
7657 ieee80211_send_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
7658 			    struct ieee80211_neg_ttlm *neg_ttlm,
7659 			    u8 dialog_token)
7660 {
7661 	struct ieee80211_local *local = sdata->local;
7662 	struct ieee80211_mgmt *mgmt;
7663 	struct sk_buff *skb;
7664 	int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.ttlm_req);
7665 	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
7666 		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
7667 
7668 	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
7669 	if (!skb)
7670 		return;
7671 
7672 	skb_reserve(skb, local->tx_headroom);
7673 	mgmt = skb_put_zero(skb, hdr_len);
7674 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
7675 					  IEEE80211_STYPE_ACTION);
7676 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
7677 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
7678 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7679 
7680 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
7681 	mgmt->u.action.u.ttlm_req.action_code =
7682 		WLAN_PROTECTED_EHT_ACTION_TTLM_REQ;
7683 	mgmt->u.action.u.ttlm_req.dialog_token = dialog_token;
7684 	ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
7685 	ieee80211_tx_skb(sdata, skb);
7686 }
7687 
7688 int ieee80211_req_neg_ttlm(struct ieee80211_sub_if_data *sdata,
7689 			   struct cfg80211_ttlm_params *params)
7690 {
7691 	struct ieee80211_neg_ttlm neg_ttlm = {};
7692 	u8 i;
7693 
7694 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
7695 	    !(sdata->vif.cfg.mld_capa_op &
7696 	      IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP))
7697 		return -EINVAL;
7698 
7699 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
7700 		if ((params->dlink[i] & ~sdata->vif.valid_links) ||
7701 		    (params->ulink[i] & ~sdata->vif.valid_links))
7702 			return -EINVAL;
7703 
7704 		neg_ttlm.downlink[i] = params->dlink[i];
7705 		neg_ttlm.uplink[i] = params->ulink[i];
7706 	}
7707 
7708 	if (drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm) !=
7709 	    NEG_TTLM_RES_ACCEPT)
7710 		return -EINVAL;
7711 
7712 	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
7713 	sdata->u.mgd.dialog_token_alloc++;
7714 	ieee80211_send_neg_ttlm_req(sdata, &sdata->vif.neg_ttlm,
7715 				    sdata->u.mgd.dialog_token_alloc);
7716 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
7717 				  &sdata->u.mgd.neg_ttlm_timeout_work);
7718 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
7719 				 &sdata->u.mgd.neg_ttlm_timeout_work,
7720 				 IEEE80211_NEG_TTLM_REQ_TIMEOUT);
7721 	return 0;
7722 }
7723 
7724 static void
7725 ieee80211_send_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
7726 			    enum ieee80211_neg_ttlm_res ttlm_res,
7727 			    u8 dialog_token,
7728 			    struct ieee80211_neg_ttlm *neg_ttlm)
7729 {
7730 	struct ieee80211_local *local = sdata->local;
7731 	struct ieee80211_mgmt *mgmt;
7732 	struct sk_buff *skb;
7733 	int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.ttlm_res);
7734 	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
7735 		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
7736 	u16 status_code;
7737 
7738 	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
7739 	if (!skb)
7740 		return;
7741 
7742 	skb_reserve(skb, local->tx_headroom);
7743 	mgmt = skb_put_zero(skb, hdr_len);
7744 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
7745 					  IEEE80211_STYPE_ACTION);
7746 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
7747 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
7748 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7749 
7750 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
7751 	mgmt->u.action.u.ttlm_res.action_code =
7752 		WLAN_PROTECTED_EHT_ACTION_TTLM_RES;
7753 	mgmt->u.action.u.ttlm_res.dialog_token = dialog_token;
7754 	switch (ttlm_res) {
7755 	default:
7756 		WARN_ON(1);
7757 		fallthrough;
7758 	case NEG_TTLM_RES_REJECT:
7759 		status_code = WLAN_STATUS_DENIED_TID_TO_LINK_MAPPING;
7760 		break;
7761 	case NEG_TTLM_RES_ACCEPT:
7762 		status_code = WLAN_STATUS_SUCCESS;
7763 		break;
7764 	case NEG_TTLM_RES_SUGGEST_PREFERRED:
7765 		status_code = WLAN_STATUS_PREF_TID_TO_LINK_MAPPING_SUGGESTED;
7766 		ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
7767 		break;
7768 	}
7769 
7770 	mgmt->u.action.u.ttlm_res.status_code = cpu_to_le16(status_code);
7771 	ieee80211_tx_skb(sdata, skb);
7772 }
7773 
7774 static int
7775 ieee80211_parse_neg_ttlm(struct ieee80211_sub_if_data *sdata,
7776 			 const struct ieee80211_ttlm_elem *ttlm,
7777 			 struct ieee80211_neg_ttlm *neg_ttlm,
7778 			 u8 *direction)
7779 {
7780 	u8 control, link_map_presence, map_size, tid;
7781 	u8 *pos;
7782 
7783 	/* The element size was already validated in
7784 	 * ieee80211_tid_to_link_map_size_ok()
7785 	 */
7786 	pos = (void *)ttlm->optional;
7787 
7788 	control = ttlm->control;
7789 
7790 	/* mapping switch time and expected duration fields are not expected
7791 	 * in case of negotiated TTLM
7792 	 */
7793 	if (control & (IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT |
7794 		       IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT)) {
7795 		mlme_dbg(sdata,
7796 			 "Invalid TTLM element in negotiated TTLM request\n");
7797 		return -EINVAL;
7798 	}
7799 
7800 	if (control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) {
7801 		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
7802 			neg_ttlm->downlink[tid] = sdata->vif.valid_links;
7803 			neg_ttlm->uplink[tid] = sdata->vif.valid_links;
7804 		}
7805 		*direction = IEEE80211_TTLM_DIRECTION_BOTH;
7806 		return 0;
7807 	}
7808 
7809 	*direction = u8_get_bits(control, IEEE80211_TTLM_CONTROL_DIRECTION);
7810 	if (*direction != IEEE80211_TTLM_DIRECTION_DOWN &&
7811 	    *direction != IEEE80211_TTLM_DIRECTION_UP &&
7812 	    *direction != IEEE80211_TTLM_DIRECTION_BOTH)
7813 		return -EINVAL;
7814 
7815 	link_map_presence = *pos;
7816 	pos++;
7817 
7818 	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
7819 		map_size = 1;
7820 	else
7821 		map_size = 2;
7822 
7823 	for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
7824 		u16 map;
7825 
7826 		if (link_map_presence & BIT(tid)) {
7827 			map = ieee80211_get_ttlm(map_size, pos);
7828 			if (!map) {
7829 				mlme_dbg(sdata,
7830 					 "No active links for TID %d", tid);
7831 				return -EINVAL;
7832 			}
7833 		} else {
7834 			map = 0;
7835 		}
7836 
7837 		switch (*direction) {
7838 		case IEEE80211_TTLM_DIRECTION_BOTH:
7839 			neg_ttlm->downlink[tid] = map;
7840 			neg_ttlm->uplink[tid] = map;
7841 			break;
7842 		case IEEE80211_TTLM_DIRECTION_DOWN:
7843 			neg_ttlm->downlink[tid] = map;
7844 			break;
7845 		case IEEE80211_TTLM_DIRECTION_UP:
7846 			neg_ttlm->uplink[tid] = map;
7847 			break;
7848 		default:
7849 			return -EINVAL;
7850 		}
7851 		pos += map_size;
7852 	}
7853 	return 0;
7854 }
7855 
7856 void ieee80211_process_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
7857 				    struct ieee80211_mgmt *mgmt, size_t len)
7858 {
7859 	u8 dialog_token, direction[IEEE80211_TTLM_MAX_CNT] = {}, i;
7860 	size_t ies_len;
7861 	enum ieee80211_neg_ttlm_res ttlm_res = NEG_TTLM_RES_ACCEPT;
7862 	struct ieee802_11_elems *elems = NULL;
7863 	struct ieee80211_neg_ttlm neg_ttlm = {};
7864 
7865 	BUILD_BUG_ON(ARRAY_SIZE(direction) != ARRAY_SIZE(elems->ttlm));
7866 
7867 	if (!ieee80211_vif_is_mld(&sdata->vif))
7868 		return;
7869 
7870 	dialog_token = mgmt->u.action.u.ttlm_req.dialog_token;
7871 	ies_len  = len - offsetof(struct ieee80211_mgmt,
7872 				  u.action.u.ttlm_req.variable);
7873 	elems = ieee802_11_parse_elems(mgmt->u.action.u.ttlm_req.variable,
7874 				       ies_len, true, NULL);
7875 	if (!elems) {
7876 		ttlm_res = NEG_TTLM_RES_REJECT;
7877 		goto out;
7878 	}
7879 
7880 	for (i = 0; i < elems->ttlm_num; i++) {
7881 		if (ieee80211_parse_neg_ttlm(sdata, elems->ttlm[i],
7882 					     &neg_ttlm, &direction[i]) ||
7883 		    (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH &&
7884 		     elems->ttlm_num != 1)) {
7885 			ttlm_res = NEG_TTLM_RES_REJECT;
7886 			goto out;
7887 		}
7888 	}
7889 
7890 	if (!elems->ttlm_num ||
7891 	    (elems->ttlm_num == 2 && direction[0] == direction[1])) {
7892 		ttlm_res = NEG_TTLM_RES_REJECT;
7893 		goto out;
7894 	}
7895 
7896 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
7897 		if ((neg_ttlm.downlink[i] &&
7898 		     (neg_ttlm.downlink[i] & ~sdata->vif.valid_links)) ||
7899 		    (neg_ttlm.uplink[i] &&
7900 		     (neg_ttlm.uplink[i] & ~sdata->vif.valid_links))) {
7901 			ttlm_res = NEG_TTLM_RES_REJECT;
7902 			goto out;
7903 		}
7904 	}
7905 
7906 	ttlm_res = drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm);
7907 
7908 	if (ttlm_res != NEG_TTLM_RES_ACCEPT)
7909 		goto out;
7910 
7911 	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
7912 out:
7913 	kfree(elems);
7914 	ieee80211_send_neg_ttlm_res(sdata, ttlm_res, dialog_token, &neg_ttlm);
7915 }
7916 
7917 void ieee80211_process_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
7918 				    struct ieee80211_mgmt *mgmt, size_t len)
7919 {
7920 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
7921 	    mgmt->u.action.u.ttlm_req.dialog_token !=
7922 	    sdata->u.mgd.dialog_token_alloc)
7923 		return;
7924 
7925 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
7926 				  &sdata->u.mgd.neg_ttlm_timeout_work);
7927 
7928 	/* MLD station sends a TID to link mapping request, mainly to handle
7929 	 * BTM (BSS transition management) request, in which case it needs to
7930 	 * restrict the active links set.
7931 	 * In this case it's not expected that the MLD AP will reject the
7932 	 * negotiated TTLM request.
7933 	 * This can be better implemented in the future, to handle request
7934 	 * rejections.
7935 	 */
7936 	if (le16_to_cpu(mgmt->u.action.u.ttlm_res.status_code) != WLAN_STATUS_SUCCESS)
7937 		__ieee80211_disconnect(sdata);
7938 }
7939 
7940 void ieee80211_process_ttlm_teardown(struct ieee80211_sub_if_data *sdata)
7941 {
7942 	u16 new_dormant_links;
7943 
7944 	if (!sdata->vif.neg_ttlm.valid)
7945 		return;
7946 
7947 	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
7948 	new_dormant_links =
7949 		sdata->vif.dormant_links & ~sdata->vif.suspended_links;
7950 	sdata->vif.suspended_links = 0;
7951 	ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
7952 				new_dormant_links);
7953 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_TTLM |
7954 					       BSS_CHANGED_MLD_VALID_LINKS);
7955 }
7956 
7957 static void ieee80211_teardown_ttlm_work(struct wiphy *wiphy,
7958 					 struct wiphy_work *work)
7959 {
7960 	struct ieee80211_sub_if_data *sdata =
7961 		container_of(work, struct ieee80211_sub_if_data,
7962 			     u.mgd.teardown_ttlm_work);
7963 
7964 	ieee80211_process_ttlm_teardown(sdata);
7965 }
7966 
7967 void ieee80211_send_teardown_neg_ttlm(struct ieee80211_vif *vif)
7968 {
7969 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
7970 	struct ieee80211_local *local = sdata->local;
7971 	struct ieee80211_mgmt *mgmt;
7972 	struct sk_buff *skb;
7973 	int frame_len = offsetofend(struct ieee80211_mgmt,
7974 				  u.action.u.ttlm_tear_down);
7975 	struct ieee80211_tx_info *info;
7976 
7977 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
7978 	if (!skb)
7979 		return;
7980 
7981 	skb_reserve(skb, local->hw.extra_tx_headroom);
7982 	mgmt = skb_put_zero(skb, frame_len);
7983 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
7984 					  IEEE80211_STYPE_ACTION);
7985 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
7986 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
7987 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7988 
7989 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
7990 	mgmt->u.action.u.ttlm_tear_down.action_code =
7991 		WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN;
7992 
7993 	info = IEEE80211_SKB_CB(skb);
7994 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
7995 	info->status_data = IEEE80211_STATUS_TYPE_NEG_TTLM;
7996 	ieee80211_tx_skb(sdata, skb);
7997 }
7998 EXPORT_SYMBOL(ieee80211_send_teardown_neg_ttlm);
7999 
8000 void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
8001 				 struct sk_buff *skb)
8002 {
8003 	struct ieee80211_link_data *link = &sdata->deflink;
8004 	struct ieee80211_rx_status *rx_status;
8005 	struct ieee80211_hdr *hdr;
8006 	u16 fc;
8007 
8008 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8009 
8010 	rx_status = (struct ieee80211_rx_status *) skb->cb;
8011 	hdr = (struct ieee80211_hdr *) skb->data;
8012 	fc = le16_to_cpu(hdr->frame_control);
8013 
8014 	switch (fc & IEEE80211_FCTL_STYPE) {
8015 	case IEEE80211_STYPE_S1G_BEACON:
8016 		ieee80211_rx_mgmt_beacon(link, hdr, skb->len, rx_status);
8017 		break;
8018 	}
8019 }
8020 
8021 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
8022 				  struct sk_buff *skb)
8023 {
8024 	struct ieee80211_link_data *link = &sdata->deflink;
8025 	struct ieee80211_rx_status *rx_status;
8026 	struct ieee802_11_elems *elems;
8027 	struct ieee80211_mgmt *mgmt;
8028 	u16 fc;
8029 	int ies_len;
8030 
8031 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8032 
8033 	rx_status = (struct ieee80211_rx_status *) skb->cb;
8034 	mgmt = (struct ieee80211_mgmt *) skb->data;
8035 	fc = le16_to_cpu(mgmt->frame_control);
8036 
8037 	if (rx_status->link_valid) {
8038 		link = sdata_dereference(sdata->link[rx_status->link_id],
8039 					 sdata);
8040 		if (!link)
8041 			return;
8042 	}
8043 
8044 	switch (fc & IEEE80211_FCTL_STYPE) {
8045 	case IEEE80211_STYPE_BEACON:
8046 		ieee80211_rx_mgmt_beacon(link, (void *)mgmt,
8047 					 skb->len, rx_status);
8048 		break;
8049 	case IEEE80211_STYPE_PROBE_RESP:
8050 		ieee80211_rx_mgmt_probe_resp(link, skb);
8051 		break;
8052 	case IEEE80211_STYPE_AUTH:
8053 		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
8054 		break;
8055 	case IEEE80211_STYPE_DEAUTH:
8056 		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
8057 		break;
8058 	case IEEE80211_STYPE_DISASSOC:
8059 		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
8060 		break;
8061 	case IEEE80211_STYPE_ASSOC_RESP:
8062 	case IEEE80211_STYPE_REASSOC_RESP:
8063 		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
8064 		break;
8065 	case IEEE80211_STYPE_ACTION:
8066 		if (!sdata->u.mgd.associated ||
8067 		    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
8068 			break;
8069 
8070 		switch (mgmt->u.action.category) {
8071 		case WLAN_CATEGORY_SPECTRUM_MGMT:
8072 			ies_len = skb->len -
8073 				  offsetof(struct ieee80211_mgmt,
8074 					   u.action.u.chan_switch.variable);
8075 
8076 			if (ies_len < 0)
8077 				break;
8078 
8079 			/* CSA IE cannot be overridden, no need for BSSID */
8080 			elems = ieee802_11_parse_elems(
8081 					mgmt->u.action.u.chan_switch.variable,
8082 					ies_len, true, NULL);
8083 
8084 			if (elems && !elems->parse_error) {
8085 				enum ieee80211_csa_source src =
8086 					IEEE80211_CSA_SOURCE_PROT_ACTION;
8087 
8088 				ieee80211_sta_process_chanswitch(link,
8089 								 rx_status->mactime,
8090 								 rx_status->device_timestamp,
8091 								 elems, elems,
8092 								 src);
8093 			}
8094 			kfree(elems);
8095 			break;
8096 		case WLAN_CATEGORY_PUBLIC:
8097 		case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
8098 			ies_len = skb->len -
8099 				  offsetof(struct ieee80211_mgmt,
8100 					   u.action.u.ext_chan_switch.variable);
8101 
8102 			if (ies_len < 0)
8103 				break;
8104 
8105 			/*
8106 			 * extended CSA IE can't be overridden, no need for
8107 			 * BSSID
8108 			 */
8109 			elems = ieee802_11_parse_elems(
8110 					mgmt->u.action.u.ext_chan_switch.variable,
8111 					ies_len, true, NULL);
8112 
8113 			if (elems && !elems->parse_error) {
8114 				enum ieee80211_csa_source src;
8115 
8116 				if (mgmt->u.action.category ==
8117 						WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
8118 					src = IEEE80211_CSA_SOURCE_PROT_ACTION;
8119 				else
8120 					src = IEEE80211_CSA_SOURCE_UNPROT_ACTION;
8121 
8122 				/* for the handling code pretend it was an IE */
8123 				elems->ext_chansw_ie =
8124 					&mgmt->u.action.u.ext_chan_switch.data;
8125 
8126 				ieee80211_sta_process_chanswitch(link,
8127 								 rx_status->mactime,
8128 								 rx_status->device_timestamp,
8129 								 elems, elems,
8130 								 src);
8131 			}
8132 
8133 			kfree(elems);
8134 			break;
8135 		}
8136 		break;
8137 	}
8138 }
8139 
8140 static void ieee80211_sta_timer(struct timer_list *t)
8141 {
8142 	struct ieee80211_sub_if_data *sdata =
8143 		timer_container_of(sdata, t, u.mgd.timer);
8144 
8145 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
8146 }
8147 
8148 void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
8149 				   u8 reason, bool tx)
8150 {
8151 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
8152 
8153 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
8154 			       tx, frame_buf);
8155 
8156 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
8157 				    reason, false);
8158 }
8159 
8160 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
8161 {
8162 	struct ieee80211_local *local = sdata->local;
8163 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8164 	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
8165 	u32 tx_flags = 0;
8166 	u16 trans = 1;
8167 	u16 status = 0;
8168 	struct ieee80211_prep_tx_info info = {
8169 		.subtype = IEEE80211_STYPE_AUTH,
8170 	};
8171 
8172 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8173 
8174 	if (WARN_ON_ONCE(!auth_data))
8175 		return -EINVAL;
8176 
8177 	auth_data->tries++;
8178 
8179 	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
8180 		sdata_info(sdata, "authentication with %pM timed out\n",
8181 			   auth_data->ap_addr);
8182 
8183 		/*
8184 		 * Most likely AP is not in the range so remove the
8185 		 * bss struct for that AP.
8186 		 */
8187 		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
8188 
8189 		return -ETIMEDOUT;
8190 	}
8191 
8192 	if (auth_data->algorithm == WLAN_AUTH_SAE)
8193 		info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
8194 
8195 	info.link_id = auth_data->link_id;
8196 	drv_mgd_prepare_tx(local, sdata, &info);
8197 
8198 	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
8199 		   auth_data->ap_addr, auth_data->tries,
8200 		   IEEE80211_AUTH_MAX_TRIES);
8201 
8202 	auth_data->expected_transaction = 2;
8203 
8204 	if (auth_data->algorithm == WLAN_AUTH_SAE) {
8205 		trans = auth_data->sae_trans;
8206 		status = auth_data->sae_status;
8207 		auth_data->expected_transaction = trans;
8208 	}
8209 
8210 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
8211 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
8212 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
8213 
8214 	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
8215 			    auth_data->data, auth_data->data_len,
8216 			    auth_data->ap_addr, auth_data->ap_addr,
8217 			    NULL, 0, 0, tx_flags);
8218 
8219 	if (tx_flags == 0) {
8220 		if (auth_data->algorithm == WLAN_AUTH_SAE)
8221 			auth_data->timeout = jiffies +
8222 				IEEE80211_AUTH_TIMEOUT_SAE;
8223 		else
8224 			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
8225 	} else {
8226 		auth_data->timeout =
8227 			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
8228 	}
8229 
8230 	auth_data->timeout_started = true;
8231 	run_again(sdata, auth_data->timeout);
8232 
8233 	return 0;
8234 }
8235 
8236 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
8237 {
8238 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
8239 	struct ieee80211_local *local = sdata->local;
8240 	int ret;
8241 
8242 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8243 
8244 	assoc_data->tries++;
8245 	assoc_data->comeback = false;
8246 	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
8247 		sdata_info(sdata, "association with %pM timed out\n",
8248 			   assoc_data->ap_addr);
8249 
8250 		/*
8251 		 * Most likely AP is not in the range so remove the
8252 		 * bss struct for that AP.
8253 		 */
8254 		cfg80211_unlink_bss(local->hw.wiphy,
8255 				    assoc_data->link[assoc_data->assoc_link_id].bss);
8256 
8257 		return -ETIMEDOUT;
8258 	}
8259 
8260 	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
8261 		   assoc_data->ap_addr, assoc_data->tries,
8262 		   IEEE80211_ASSOC_MAX_TRIES);
8263 	ret = ieee80211_send_assoc(sdata);
8264 	if (ret)
8265 		return ret;
8266 
8267 	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
8268 		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
8269 		assoc_data->timeout_started = true;
8270 		run_again(sdata, assoc_data->timeout);
8271 	} else {
8272 		assoc_data->timeout =
8273 			round_jiffies_up(jiffies +
8274 					 IEEE80211_ASSOC_TIMEOUT_LONG);
8275 		assoc_data->timeout_started = true;
8276 		run_again(sdata, assoc_data->timeout);
8277 	}
8278 
8279 	return 0;
8280 }
8281 
8282 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
8283 				  __le16 fc, bool acked)
8284 {
8285 	struct ieee80211_local *local = sdata->local;
8286 
8287 	sdata->u.mgd.status_fc = fc;
8288 	sdata->u.mgd.status_acked = acked;
8289 	sdata->u.mgd.status_received = true;
8290 
8291 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
8292 }
8293 
8294 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
8295 {
8296 	struct ieee80211_local *local = sdata->local;
8297 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8298 
8299 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8300 
8301 	if (ifmgd->status_received) {
8302 		__le16 fc = ifmgd->status_fc;
8303 		bool status_acked = ifmgd->status_acked;
8304 
8305 		ifmgd->status_received = false;
8306 		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
8307 			if (status_acked) {
8308 				if (ifmgd->auth_data->algorithm ==
8309 				    WLAN_AUTH_SAE)
8310 					ifmgd->auth_data->timeout =
8311 						jiffies +
8312 						IEEE80211_AUTH_TIMEOUT_SAE;
8313 				else
8314 					ifmgd->auth_data->timeout =
8315 						jiffies +
8316 						IEEE80211_AUTH_TIMEOUT_SHORT;
8317 				run_again(sdata, ifmgd->auth_data->timeout);
8318 			} else {
8319 				ifmgd->auth_data->timeout = jiffies - 1;
8320 			}
8321 			ifmgd->auth_data->timeout_started = true;
8322 		} else if (ifmgd->assoc_data &&
8323 			   !ifmgd->assoc_data->comeback &&
8324 			   (ieee80211_is_assoc_req(fc) ||
8325 			    ieee80211_is_reassoc_req(fc))) {
8326 			/*
8327 			 * Update association timeout based on the TX status
8328 			 * for the (Re)Association Request frame. Skip this if
8329 			 * we have already processed a (Re)Association Response
8330 			 * frame that indicated need for association comeback
8331 			 * at a specific time in the future. This could happen
8332 			 * if the TX status information is delayed enough for
8333 			 * the response to be received and processed first.
8334 			 */
8335 			if (status_acked) {
8336 				ifmgd->assoc_data->timeout =
8337 					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
8338 				run_again(sdata, ifmgd->assoc_data->timeout);
8339 			} else {
8340 				ifmgd->assoc_data->timeout = jiffies - 1;
8341 			}
8342 			ifmgd->assoc_data->timeout_started = true;
8343 		}
8344 	}
8345 
8346 	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
8347 	    time_after(jiffies, ifmgd->auth_data->timeout)) {
8348 		if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
8349 			/*
8350 			 * ok ... we waited for assoc or continuation but
8351 			 * userspace didn't do it, so kill the auth data
8352 			 */
8353 			ieee80211_destroy_auth_data(sdata, false);
8354 		} else if (ieee80211_auth(sdata)) {
8355 			u8 ap_addr[ETH_ALEN];
8356 			struct ieee80211_event event = {
8357 				.type = MLME_EVENT,
8358 				.u.mlme.data = AUTH_EVENT,
8359 				.u.mlme.status = MLME_TIMEOUT,
8360 			};
8361 
8362 			memcpy(ap_addr, ifmgd->auth_data->ap_addr, ETH_ALEN);
8363 
8364 			ieee80211_destroy_auth_data(sdata, false);
8365 
8366 			cfg80211_auth_timeout(sdata->dev, ap_addr);
8367 			drv_event_callback(sdata->local, sdata, &event);
8368 		}
8369 	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
8370 		run_again(sdata, ifmgd->auth_data->timeout);
8371 
8372 	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
8373 	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
8374 		if ((ifmgd->assoc_data->need_beacon &&
8375 		     !sdata->deflink.u.mgd.have_beacon) ||
8376 		    ieee80211_do_assoc(sdata)) {
8377 			struct ieee80211_event event = {
8378 				.type = MLME_EVENT,
8379 				.u.mlme.data = ASSOC_EVENT,
8380 				.u.mlme.status = MLME_TIMEOUT,
8381 			};
8382 
8383 			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
8384 			drv_event_callback(sdata->local, sdata, &event);
8385 		}
8386 	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
8387 		run_again(sdata, ifmgd->assoc_data->timeout);
8388 
8389 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
8390 	    ifmgd->associated) {
8391 		u8 *bssid = sdata->deflink.u.mgd.bssid;
8392 		int max_tries;
8393 
8394 		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
8395 			max_tries = max_nullfunc_tries;
8396 		else
8397 			max_tries = max_probe_tries;
8398 
8399 		/* ACK received for nullfunc probing frame */
8400 		if (!ifmgd->probe_send_count)
8401 			ieee80211_reset_ap_probe(sdata);
8402 		else if (ifmgd->nullfunc_failed) {
8403 			if (ifmgd->probe_send_count < max_tries) {
8404 				mlme_dbg(sdata,
8405 					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
8406 					 bssid, ifmgd->probe_send_count,
8407 					 max_tries);
8408 				ieee80211_mgd_probe_ap_send(sdata);
8409 			} else {
8410 				mlme_dbg(sdata,
8411 					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
8412 					 bssid);
8413 				ieee80211_sta_connection_lost(sdata,
8414 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
8415 					false);
8416 			}
8417 		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
8418 			run_again(sdata, ifmgd->probe_timeout);
8419 		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
8420 			mlme_dbg(sdata,
8421 				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
8422 				 bssid, probe_wait_ms);
8423 			ieee80211_sta_connection_lost(sdata,
8424 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
8425 		} else if (ifmgd->probe_send_count < max_tries) {
8426 			mlme_dbg(sdata,
8427 				 "No probe response from AP %pM after %dms, try %d/%i\n",
8428 				 bssid, probe_wait_ms,
8429 				 ifmgd->probe_send_count, max_tries);
8430 			ieee80211_mgd_probe_ap_send(sdata);
8431 		} else {
8432 			/*
8433 			 * We actually lost the connection ... or did we?
8434 			 * Let's make sure!
8435 			 */
8436 			mlme_dbg(sdata,
8437 				 "No probe response from AP %pM after %dms, disconnecting.\n",
8438 				 bssid, probe_wait_ms);
8439 
8440 			ieee80211_sta_connection_lost(sdata,
8441 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
8442 		}
8443 	}
8444 }
8445 
8446 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
8447 {
8448 	struct ieee80211_sub_if_data *sdata =
8449 		timer_container_of(sdata, t, u.mgd.bcn_mon_timer);
8450 
8451 	if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif)))
8452 		return;
8453 
8454 	if (sdata->vif.bss_conf.csa_active &&
8455 	    !sdata->deflink.u.mgd.csa.waiting_bcn)
8456 		return;
8457 
8458 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
8459 		return;
8460 
8461 	sdata->u.mgd.connection_loss = false;
8462 	wiphy_work_queue(sdata->local->hw.wiphy,
8463 			 &sdata->u.mgd.beacon_connection_loss_work);
8464 }
8465 
8466 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
8467 {
8468 	struct ieee80211_sub_if_data *sdata =
8469 		timer_container_of(sdata, t, u.mgd.conn_mon_timer);
8470 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8471 	struct ieee80211_local *local = sdata->local;
8472 	struct sta_info *sta;
8473 	unsigned long timeout;
8474 
8475 	if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif)))
8476 		return;
8477 
8478 	if (sdata->vif.bss_conf.csa_active &&
8479 	    !sdata->deflink.u.mgd.csa.waiting_bcn)
8480 		return;
8481 
8482 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
8483 	if (!sta)
8484 		return;
8485 
8486 	timeout = sta->deflink.status_stats.last_ack;
8487 	if (time_before(sta->deflink.status_stats.last_ack, sta->deflink.rx_stats.last_rx))
8488 		timeout = sta->deflink.rx_stats.last_rx;
8489 	timeout += IEEE80211_CONNECTION_IDLE_TIME;
8490 
8491 	/* If timeout is after now, then update timer to fire at
8492 	 * the later date, but do not actually probe at this time.
8493 	 */
8494 	if (time_is_after_jiffies(timeout)) {
8495 		mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
8496 		return;
8497 	}
8498 
8499 	wiphy_work_queue(local->hw.wiphy, &sdata->u.mgd.monitor_work);
8500 }
8501 
8502 static void ieee80211_sta_monitor_work(struct wiphy *wiphy,
8503 				       struct wiphy_work *work)
8504 {
8505 	struct ieee80211_sub_if_data *sdata =
8506 		container_of(work, struct ieee80211_sub_if_data,
8507 			     u.mgd.monitor_work);
8508 
8509 	ieee80211_mgd_probe_ap(sdata, false);
8510 }
8511 
8512 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
8513 {
8514 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
8515 		__ieee80211_stop_poll(sdata);
8516 
8517 		/* let's probe the connection once */
8518 		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
8519 			wiphy_work_queue(sdata->local->hw.wiphy,
8520 					 &sdata->u.mgd.monitor_work);
8521 	}
8522 }
8523 
8524 #ifdef CONFIG_PM
8525 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
8526 {
8527 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8528 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
8529 
8530 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8531 
8532 	if (ifmgd->auth_data || ifmgd->assoc_data) {
8533 		const u8 *ap_addr = ifmgd->auth_data ?
8534 				ifmgd->auth_data->ap_addr :
8535 				ifmgd->assoc_data->ap_addr;
8536 
8537 		/*
8538 		 * If we are trying to authenticate / associate while suspending,
8539 		 * cfg80211 won't know and won't actually abort those attempts,
8540 		 * thus we need to do that ourselves.
8541 		 */
8542 		ieee80211_send_deauth_disassoc(sdata, ap_addr, ap_addr,
8543 					       IEEE80211_STYPE_DEAUTH,
8544 					       WLAN_REASON_DEAUTH_LEAVING,
8545 					       false, frame_buf);
8546 		if (ifmgd->assoc_data)
8547 			ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
8548 		if (ifmgd->auth_data)
8549 			ieee80211_destroy_auth_data(sdata, false);
8550 		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
8551 				      IEEE80211_DEAUTH_FRAME_LEN,
8552 				      false);
8553 	}
8554 
8555 	/* This is a bit of a hack - we should find a better and more generic
8556 	 * solution to this. Normally when suspending, cfg80211 will in fact
8557 	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
8558 	 * auth (not so important) or assoc (this is the problem) process.
8559 	 *
8560 	 * As a consequence, it can happen that we are in the process of both
8561 	 * associating and suspending, and receive an association response
8562 	 * after cfg80211 has checked if it needs to disconnect, but before
8563 	 * we actually set the flag to drop incoming frames. This will then
8564 	 * cause the workqueue flush to process the association response in
8565 	 * the suspend, resulting in a successful association just before it
8566 	 * tries to remove the interface from the driver, which now though
8567 	 * has a channel context assigned ... this results in issues.
8568 	 *
8569 	 * To work around this (for now) simply deauth here again if we're
8570 	 * now connected.
8571 	 */
8572 	if (ifmgd->associated && !sdata->local->wowlan) {
8573 		u8 bssid[ETH_ALEN];
8574 		struct cfg80211_deauth_request req = {
8575 			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
8576 			.bssid = bssid,
8577 		};
8578 
8579 		memcpy(bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
8580 		ieee80211_mgd_deauth(sdata, &req);
8581 	}
8582 }
8583 #endif
8584 
8585 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
8586 {
8587 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8588 
8589 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8590 
8591 	if (!ifmgd->associated)
8592 		return;
8593 
8594 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
8595 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
8596 		mlme_dbg(sdata, "driver requested disconnect after resume\n");
8597 		ieee80211_sta_connection_lost(sdata,
8598 					      WLAN_REASON_UNSPECIFIED,
8599 					      true);
8600 		return;
8601 	}
8602 
8603 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_HW_RESTART) {
8604 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_HW_RESTART;
8605 		mlme_dbg(sdata, "driver requested disconnect after hardware restart\n");
8606 		ieee80211_sta_connection_lost(sdata,
8607 					      WLAN_REASON_UNSPECIFIED,
8608 					      true);
8609 		return;
8610 	}
8611 }
8612 
8613 static void ieee80211_request_smps_mgd_work(struct wiphy *wiphy,
8614 					    struct wiphy_work *work)
8615 {
8616 	struct ieee80211_link_data *link =
8617 		container_of(work, struct ieee80211_link_data,
8618 			     u.mgd.request_smps_work);
8619 
8620 	__ieee80211_request_smps_mgd(link->sdata, link,
8621 				     link->u.mgd.driver_smps_mode);
8622 }
8623 
8624 static void ieee80211_ml_sta_reconf_timeout(struct wiphy *wiphy,
8625 					    struct wiphy_work *work)
8626 {
8627 	struct ieee80211_sub_if_data *sdata =
8628 		container_of(work, struct ieee80211_sub_if_data,
8629 			     u.mgd.reconf.wk.work);
8630 
8631 	if (!sdata->u.mgd.reconf.added_links &&
8632 	    !sdata->u.mgd.reconf.removed_links)
8633 		return;
8634 
8635 	sdata_info(sdata,
8636 		   "mlo: reconf: timeout: added=0x%x, removed=0x%x\n",
8637 		   sdata->u.mgd.reconf.added_links,
8638 		   sdata->u.mgd.reconf.removed_links);
8639 
8640 	__ieee80211_disconnect(sdata);
8641 }
8642 
8643 /* interface setup */
8644 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
8645 {
8646 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8647 
8648 	wiphy_work_init(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
8649 	wiphy_work_init(&ifmgd->beacon_connection_loss_work,
8650 			ieee80211_beacon_connection_loss_work);
8651 	wiphy_work_init(&ifmgd->csa_connection_drop_work,
8652 			ieee80211_csa_connection_drop_work);
8653 	wiphy_delayed_work_init(&ifmgd->tdls_peer_del_work,
8654 				ieee80211_tdls_peer_del_work);
8655 	wiphy_delayed_work_init(&ifmgd->ml_reconf_work,
8656 				ieee80211_ml_reconf_work);
8657 	wiphy_delayed_work_init(&ifmgd->reconf.wk,
8658 				ieee80211_ml_sta_reconf_timeout);
8659 	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
8660 	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
8661 	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
8662 	wiphy_delayed_work_init(&ifmgd->tx_tspec_wk,
8663 				ieee80211_sta_handle_tspec_ac_params_wk);
8664 	wiphy_delayed_work_init(&ifmgd->ttlm_work,
8665 				ieee80211_tid_to_link_map_work);
8666 	wiphy_delayed_work_init(&ifmgd->neg_ttlm_timeout_work,
8667 				ieee80211_neg_ttlm_timeout_work);
8668 	wiphy_work_init(&ifmgd->teardown_ttlm_work,
8669 			ieee80211_teardown_ttlm_work);
8670 
8671 	ifmgd->flags = 0;
8672 	ifmgd->powersave = sdata->wdev.ps;
8673 	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
8674 	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
8675 	/* Setup TDLS data */
8676 	spin_lock_init(&ifmgd->teardown_lock);
8677 	ifmgd->teardown_skb = NULL;
8678 	ifmgd->orig_teardown_skb = NULL;
8679 	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
8680 }
8681 
8682 static void ieee80211_recalc_smps_work(struct wiphy *wiphy,
8683 				       struct wiphy_work *work)
8684 {
8685 	struct ieee80211_link_data *link =
8686 		container_of(work, struct ieee80211_link_data,
8687 			     u.mgd.recalc_smps);
8688 
8689 	ieee80211_recalc_smps(link->sdata, link);
8690 }
8691 
8692 void ieee80211_mgd_setup_link(struct ieee80211_link_data *link)
8693 {
8694 	struct ieee80211_sub_if_data *sdata = link->sdata;
8695 	struct ieee80211_local *local = sdata->local;
8696 	unsigned int link_id = link->link_id;
8697 
8698 	link->u.mgd.p2p_noa_index = -1;
8699 	link->conf->bssid = link->u.mgd.bssid;
8700 	link->smps_mode = IEEE80211_SMPS_OFF;
8701 
8702 	wiphy_work_init(&link->u.mgd.request_smps_work,
8703 			ieee80211_request_smps_mgd_work);
8704 	wiphy_work_init(&link->u.mgd.recalc_smps,
8705 			ieee80211_recalc_smps_work);
8706 	if (local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
8707 		link->u.mgd.req_smps = IEEE80211_SMPS_AUTOMATIC;
8708 	else
8709 		link->u.mgd.req_smps = IEEE80211_SMPS_OFF;
8710 
8711 	wiphy_delayed_work_init(&link->u.mgd.csa.switch_work,
8712 				ieee80211_csa_switch_work);
8713 
8714 	ieee80211_clear_tpe(&link->conf->tpe);
8715 
8716 	if (sdata->u.mgd.assoc_data)
8717 		ether_addr_copy(link->conf->addr,
8718 				sdata->u.mgd.assoc_data->link[link_id].addr);
8719 	else if (sdata->u.mgd.reconf.add_links_data)
8720 		ether_addr_copy(link->conf->addr,
8721 				sdata->u.mgd.reconf.add_links_data->link[link_id].addr);
8722 	else if (!is_valid_ether_addr(link->conf->addr))
8723 		eth_random_addr(link->conf->addr);
8724 }
8725 
8726 /* scan finished notification */
8727 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
8728 {
8729 	struct ieee80211_sub_if_data *sdata;
8730 
8731 	/* Restart STA timers */
8732 	rcu_read_lock();
8733 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
8734 		if (ieee80211_sdata_running(sdata))
8735 			ieee80211_restart_sta_timer(sdata);
8736 	}
8737 	rcu_read_unlock();
8738 }
8739 
8740 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
8741 				     struct cfg80211_bss *cbss, s8 link_id,
8742 				     const u8 *ap_mld_addr, bool assoc,
8743 				     struct ieee80211_conn_settings *conn,
8744 				     bool override,
8745 				     unsigned long *userspace_selectors)
8746 {
8747 	struct ieee80211_local *local = sdata->local;
8748 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8749 	struct ieee80211_bss *bss = (void *)cbss->priv;
8750 	struct sta_info *new_sta = NULL;
8751 	struct ieee80211_link_data *link;
8752 	bool have_sta = false;
8753 	bool mlo;
8754 	int err;
8755 	u16 new_links;
8756 
8757 	if (link_id >= 0) {
8758 		mlo = true;
8759 		if (WARN_ON(!ap_mld_addr))
8760 			return -EINVAL;
8761 		new_links = BIT(link_id);
8762 	} else {
8763 		if (WARN_ON(ap_mld_addr))
8764 			return -EINVAL;
8765 		ap_mld_addr = cbss->bssid;
8766 		new_links = 0;
8767 		link_id = 0;
8768 		mlo = false;
8769 	}
8770 
8771 	if (assoc) {
8772 		rcu_read_lock();
8773 		have_sta = sta_info_get(sdata, ap_mld_addr);
8774 		rcu_read_unlock();
8775 	}
8776 
8777 	if (mlo && !have_sta &&
8778 	    WARN_ON(sdata->vif.valid_links || sdata->vif.active_links))
8779 		return -EINVAL;
8780 
8781 	err = ieee80211_vif_set_links(sdata, new_links, 0);
8782 	if (err)
8783 		return err;
8784 
8785 	link = sdata_dereference(sdata->link[link_id], sdata);
8786 	if (WARN_ON(!link)) {
8787 		err = -ENOLINK;
8788 		goto out_err;
8789 	}
8790 
8791 	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data)) {
8792 		err = -EINVAL;
8793 		goto out_err;
8794 	}
8795 
8796 	/* If a reconfig is happening, bail out */
8797 	if (local->in_reconfig) {
8798 		err = -EBUSY;
8799 		goto out_err;
8800 	}
8801 
8802 	if (!have_sta) {
8803 		if (mlo)
8804 			new_sta = sta_info_alloc_with_link(sdata, ap_mld_addr,
8805 							   link_id, cbss->bssid,
8806 							   GFP_KERNEL);
8807 		else
8808 			new_sta = sta_info_alloc(sdata, ap_mld_addr, GFP_KERNEL);
8809 
8810 		if (!new_sta) {
8811 			err = -ENOMEM;
8812 			goto out_err;
8813 		}
8814 
8815 		new_sta->sta.mlo = mlo;
8816 	}
8817 
8818 	/*
8819 	 * Set up the information for the new channel before setting the
8820 	 * new channel. We can't - completely race-free - change the basic
8821 	 * rates bitmap and the channel (sband) that it refers to, but if
8822 	 * we set it up before we at least avoid calling into the driver's
8823 	 * bss_info_changed() method with invalid information (since we do
8824 	 * call that from changing the channel - only for IDLE and perhaps
8825 	 * some others, but ...).
8826 	 *
8827 	 * So to avoid that, just set up all the new information before the
8828 	 * channel, but tell the driver to apply it only afterwards, since
8829 	 * it might need the new channel for that.
8830 	 */
8831 	if (new_sta) {
8832 		const struct cfg80211_bss_ies *ies;
8833 		struct link_sta_info *link_sta;
8834 
8835 		rcu_read_lock();
8836 		link_sta = rcu_dereference(new_sta->link[link_id]);
8837 		if (WARN_ON(!link_sta)) {
8838 			rcu_read_unlock();
8839 			sta_info_free(local, new_sta);
8840 			err = -EINVAL;
8841 			goto out_err;
8842 		}
8843 
8844 		err = ieee80211_mgd_setup_link_sta(link, new_sta,
8845 						   link_sta, cbss);
8846 		if (err) {
8847 			rcu_read_unlock();
8848 			sta_info_free(local, new_sta);
8849 			goto out_err;
8850 		}
8851 
8852 		memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
8853 
8854 		/* set timing information */
8855 		link->conf->beacon_int = cbss->beacon_interval;
8856 		ies = rcu_dereference(cbss->beacon_ies);
8857 		if (ies) {
8858 			link->conf->sync_tsf = ies->tsf;
8859 			link->conf->sync_device_ts =
8860 				bss->device_ts_beacon;
8861 
8862 			ieee80211_get_dtim(ies,
8863 					   &link->conf->sync_dtim_count,
8864 					   NULL);
8865 		} else if (!ieee80211_hw_check(&sdata->local->hw,
8866 					       TIMING_BEACON_ONLY)) {
8867 			ies = rcu_dereference(cbss->proberesp_ies);
8868 			/* must be non-NULL since beacon IEs were NULL */
8869 			link->conf->sync_tsf = ies->tsf;
8870 			link->conf->sync_device_ts =
8871 				bss->device_ts_presp;
8872 			link->conf->sync_dtim_count = 0;
8873 		} else {
8874 			link->conf->sync_tsf = 0;
8875 			link->conf->sync_device_ts = 0;
8876 			link->conf->sync_dtim_count = 0;
8877 		}
8878 		rcu_read_unlock();
8879 	}
8880 
8881 	if (new_sta || override) {
8882 		/*
8883 		 * Only set this if we're also going to calculate the AP
8884 		 * settings etc., otherwise this was set before in a
8885 		 * previous call. Note override is set to %true in assoc
8886 		 * if the settings were changed.
8887 		 */
8888 		link->u.mgd.conn = *conn;
8889 		err = ieee80211_prep_channel(sdata, link, link->link_id, cbss,
8890 					     mlo, &link->u.mgd.conn,
8891 					     userspace_selectors);
8892 		if (err) {
8893 			if (new_sta)
8894 				sta_info_free(local, new_sta);
8895 			goto out_err;
8896 		}
8897 		/* pass out for use in assoc */
8898 		*conn = link->u.mgd.conn;
8899 	}
8900 
8901 	if (new_sta) {
8902 		/*
8903 		 * tell driver about BSSID, basic rates and timing
8904 		 * this was set up above, before setting the channel
8905 		 */
8906 		ieee80211_link_info_change_notify(sdata, link,
8907 						  BSS_CHANGED_BSSID |
8908 						  BSS_CHANGED_BASIC_RATES |
8909 						  BSS_CHANGED_BEACON_INT);
8910 
8911 		if (assoc)
8912 			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
8913 
8914 		err = sta_info_insert(new_sta);
8915 		new_sta = NULL;
8916 		if (err) {
8917 			sdata_info(sdata,
8918 				   "failed to insert STA entry for the AP (error %d)\n",
8919 				   err);
8920 			goto out_release_chan;
8921 		}
8922 	} else
8923 		WARN_ON_ONCE(!ether_addr_equal(link->u.mgd.bssid, cbss->bssid));
8924 
8925 	/* Cancel scan to ensure that nothing interferes with connection */
8926 	if (local->scanning)
8927 		ieee80211_scan_cancel(local);
8928 
8929 	return 0;
8930 
8931 out_release_chan:
8932 	ieee80211_link_release_channel(link);
8933 out_err:
8934 	ieee80211_vif_set_links(sdata, 0, 0);
8935 	return err;
8936 }
8937 
8938 static bool ieee80211_mgd_csa_present(struct ieee80211_sub_if_data *sdata,
8939 				      const struct cfg80211_bss_ies *ies,
8940 				      u8 cur_channel, bool ignore_ecsa)
8941 {
8942 	const struct element *csa_elem, *ecsa_elem;
8943 	struct ieee80211_channel_sw_ie *csa = NULL;
8944 	struct ieee80211_ext_chansw_ie *ecsa = NULL;
8945 
8946 	if (!ies)
8947 		return false;
8948 
8949 	csa_elem = cfg80211_find_elem(WLAN_EID_CHANNEL_SWITCH,
8950 				      ies->data, ies->len);
8951 	if (csa_elem && csa_elem->datalen == sizeof(*csa))
8952 		csa = (void *)csa_elem->data;
8953 
8954 	ecsa_elem = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
8955 				       ies->data, ies->len);
8956 	if (ecsa_elem && ecsa_elem->datalen == sizeof(*ecsa))
8957 		ecsa = (void *)ecsa_elem->data;
8958 
8959 	if (csa && csa->count == 0)
8960 		csa = NULL;
8961 	if (csa && !csa->mode && csa->new_ch_num == cur_channel)
8962 		csa = NULL;
8963 
8964 	if (ecsa && ecsa->count == 0)
8965 		ecsa = NULL;
8966 	if (ecsa && !ecsa->mode && ecsa->new_ch_num == cur_channel)
8967 		ecsa = NULL;
8968 
8969 	if (ignore_ecsa && ecsa) {
8970 		sdata_info(sdata,
8971 			   "Ignoring ECSA in probe response - was considered stuck!\n");
8972 		return csa;
8973 	}
8974 
8975 	return csa || ecsa;
8976 }
8977 
8978 static bool ieee80211_mgd_csa_in_process(struct ieee80211_sub_if_data *sdata,
8979 					 struct cfg80211_bss *bss)
8980 {
8981 	u8 cur_channel;
8982 	bool ret;
8983 
8984 	cur_channel = ieee80211_frequency_to_channel(bss->channel->center_freq);
8985 
8986 	rcu_read_lock();
8987 	if (ieee80211_mgd_csa_present(sdata,
8988 				      rcu_dereference(bss->beacon_ies),
8989 				      cur_channel, false)) {
8990 		ret = true;
8991 		goto out;
8992 	}
8993 
8994 	if (ieee80211_mgd_csa_present(sdata,
8995 				      rcu_dereference(bss->proberesp_ies),
8996 				      cur_channel, bss->proberesp_ecsa_stuck)) {
8997 		ret = true;
8998 		goto out;
8999 	}
9000 
9001 	ret = false;
9002 out:
9003 	rcu_read_unlock();
9004 	return ret;
9005 }
9006 
9007 static void ieee80211_parse_cfg_selectors(unsigned long *userspace_selectors,
9008 					  const u8 *supported_selectors,
9009 					  u8 supported_selectors_len)
9010 {
9011 	if (supported_selectors) {
9012 		for (int i = 0; i < supported_selectors_len; i++) {
9013 			set_bit(supported_selectors[i],
9014 				userspace_selectors);
9015 		}
9016 	} else {
9017 		/* Assume SAE_H2E support for backward compatibility. */
9018 		set_bit(BSS_MEMBERSHIP_SELECTOR_SAE_H2E,
9019 			userspace_selectors);
9020 	}
9021 }
9022 
9023 /* config hooks */
9024 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
9025 		       struct cfg80211_auth_request *req)
9026 {
9027 	struct ieee80211_local *local = sdata->local;
9028 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9029 	struct ieee80211_mgd_auth_data *auth_data;
9030 	struct ieee80211_conn_settings conn;
9031 	struct ieee80211_link_data *link;
9032 	struct ieee80211_supported_band *sband;
9033 	struct ieee80211_bss *bss;
9034 	u16 auth_alg;
9035 	int err;
9036 	bool cont_auth, wmm_used;
9037 
9038 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
9039 
9040 	/* prepare auth data structure */
9041 
9042 	switch (req->auth_type) {
9043 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
9044 		auth_alg = WLAN_AUTH_OPEN;
9045 		break;
9046 	case NL80211_AUTHTYPE_SHARED_KEY:
9047 		if (fips_enabled)
9048 			return -EOPNOTSUPP;
9049 		auth_alg = WLAN_AUTH_SHARED_KEY;
9050 		break;
9051 	case NL80211_AUTHTYPE_FT:
9052 		auth_alg = WLAN_AUTH_FT;
9053 		break;
9054 	case NL80211_AUTHTYPE_NETWORK_EAP:
9055 		auth_alg = WLAN_AUTH_LEAP;
9056 		break;
9057 	case NL80211_AUTHTYPE_SAE:
9058 		auth_alg = WLAN_AUTH_SAE;
9059 		break;
9060 	case NL80211_AUTHTYPE_FILS_SK:
9061 		auth_alg = WLAN_AUTH_FILS_SK;
9062 		break;
9063 	case NL80211_AUTHTYPE_FILS_SK_PFS:
9064 		auth_alg = WLAN_AUTH_FILS_SK_PFS;
9065 		break;
9066 	case NL80211_AUTHTYPE_FILS_PK:
9067 		auth_alg = WLAN_AUTH_FILS_PK;
9068 		break;
9069 	default:
9070 		return -EOPNOTSUPP;
9071 	}
9072 
9073 	if (ifmgd->assoc_data)
9074 		return -EBUSY;
9075 
9076 	if (ieee80211_mgd_csa_in_process(sdata, req->bss)) {
9077 		sdata_info(sdata, "AP is in CSA process, reject auth\n");
9078 		return -EINVAL;
9079 	}
9080 
9081 	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
9082 			    req->ie_len, GFP_KERNEL);
9083 	if (!auth_data)
9084 		return -ENOMEM;
9085 
9086 	memcpy(auth_data->ap_addr,
9087 	       req->ap_mld_addr ?: req->bss->bssid,
9088 	       ETH_ALEN);
9089 	auth_data->bss = req->bss;
9090 	auth_data->link_id = req->link_id;
9091 
9092 	if (req->auth_data_len >= 4) {
9093 		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
9094 			__le16 *pos = (__le16 *) req->auth_data;
9095 
9096 			auth_data->sae_trans = le16_to_cpu(pos[0]);
9097 			auth_data->sae_status = le16_to_cpu(pos[1]);
9098 		}
9099 		memcpy(auth_data->data, req->auth_data + 4,
9100 		       req->auth_data_len - 4);
9101 		auth_data->data_len += req->auth_data_len - 4;
9102 	}
9103 
9104 	/* Check if continuing authentication or trying to authenticate with the
9105 	 * same BSS that we were in the process of authenticating with and avoid
9106 	 * removal and re-addition of the STA entry in
9107 	 * ieee80211_prep_connection().
9108 	 */
9109 	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss &&
9110 		    ifmgd->auth_data->link_id == req->link_id;
9111 
9112 	if (req->ie && req->ie_len) {
9113 		memcpy(&auth_data->data[auth_data->data_len],
9114 		       req->ie, req->ie_len);
9115 		auth_data->data_len += req->ie_len;
9116 	}
9117 
9118 	if (req->key && req->key_len) {
9119 		auth_data->key_len = req->key_len;
9120 		auth_data->key_idx = req->key_idx;
9121 		memcpy(auth_data->key, req->key, req->key_len);
9122 	}
9123 
9124 	ieee80211_parse_cfg_selectors(auth_data->userspace_selectors,
9125 				      req->supported_selectors,
9126 				      req->supported_selectors_len);
9127 
9128 	auth_data->algorithm = auth_alg;
9129 
9130 	/* try to authenticate/probe */
9131 
9132 	if (ifmgd->auth_data) {
9133 		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
9134 			auth_data->peer_confirmed =
9135 				ifmgd->auth_data->peer_confirmed;
9136 		}
9137 		ieee80211_destroy_auth_data(sdata, cont_auth);
9138 	}
9139 
9140 	/* prep auth_data so we don't go into idle on disassoc */
9141 	ifmgd->auth_data = auth_data;
9142 
9143 	/* If this is continuation of an ongoing SAE authentication exchange
9144 	 * (i.e., request to send SAE Confirm) and the peer has already
9145 	 * confirmed, mark authentication completed since we are about to send
9146 	 * out SAE Confirm.
9147 	 */
9148 	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
9149 	    auth_data->peer_confirmed && auth_data->sae_trans == 2)
9150 		ieee80211_mark_sta_auth(sdata);
9151 
9152 	if (ifmgd->associated) {
9153 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9154 
9155 		sdata_info(sdata,
9156 			   "disconnect from AP %pM for new auth to %pM\n",
9157 			   sdata->vif.cfg.ap_addr, auth_data->ap_addr);
9158 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
9159 				       WLAN_REASON_UNSPECIFIED,
9160 				       false, frame_buf);
9161 
9162 		ieee80211_report_disconnect(sdata, frame_buf,
9163 					    sizeof(frame_buf), true,
9164 					    WLAN_REASON_UNSPECIFIED,
9165 					    false);
9166 	}
9167 
9168 	/* needed for transmitting the auth frame(s) properly */
9169 	memcpy(sdata->vif.cfg.ap_addr, auth_data->ap_addr, ETH_ALEN);
9170 
9171 	bss = (void *)req->bss->priv;
9172 	wmm_used = bss->wmm_used && (local->hw.queues >= IEEE80211_NUM_ACS);
9173 
9174 	sband = local->hw.wiphy->bands[req->bss->channel->band];
9175 
9176 	ieee80211_determine_our_sta_mode_auth(sdata, sband, req, wmm_used,
9177 					      &conn);
9178 
9179 	err = ieee80211_prep_connection(sdata, req->bss, req->link_id,
9180 					req->ap_mld_addr, cont_auth,
9181 					&conn, false,
9182 					auth_data->userspace_selectors);
9183 	if (err)
9184 		goto err_clear;
9185 
9186 	if (req->link_id >= 0)
9187 		link = sdata_dereference(sdata->link[req->link_id], sdata);
9188 	else
9189 		link = &sdata->deflink;
9190 
9191 	if (WARN_ON(!link)) {
9192 		err = -ENOLINK;
9193 		goto err_clear;
9194 	}
9195 
9196 	sdata_info(sdata, "authenticate with %pM (local address=%pM)\n",
9197 		   auth_data->ap_addr, link->conf->addr);
9198 
9199 	err = ieee80211_auth(sdata);
9200 	if (err) {
9201 		sta_info_destroy_addr(sdata, auth_data->ap_addr);
9202 		goto err_clear;
9203 	}
9204 
9205 	/* hold our own reference */
9206 	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
9207 	return 0;
9208 
9209  err_clear:
9210 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
9211 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
9212 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
9213 						  BSS_CHANGED_BSSID);
9214 		ieee80211_link_release_channel(&sdata->deflink);
9215 	}
9216 	ifmgd->auth_data = NULL;
9217 	kfree(auth_data);
9218 	return err;
9219 }
9220 
9221 static void
9222 ieee80211_setup_assoc_link(struct ieee80211_sub_if_data *sdata,
9223 			   struct ieee80211_mgd_assoc_data *assoc_data,
9224 			   struct cfg80211_assoc_request *req,
9225 			   struct ieee80211_conn_settings *conn,
9226 			   unsigned int link_id)
9227 {
9228 	struct ieee80211_local *local = sdata->local;
9229 	const struct cfg80211_bss_ies *bss_ies;
9230 	struct ieee80211_supported_band *sband;
9231 	struct ieee80211_link_data *link;
9232 	struct cfg80211_bss *cbss;
9233 	struct ieee80211_bss *bss;
9234 
9235 	cbss = assoc_data->link[link_id].bss;
9236 	if (WARN_ON(!cbss))
9237 		return;
9238 
9239 	bss = (void *)cbss->priv;
9240 
9241 	sband = local->hw.wiphy->bands[cbss->channel->band];
9242 	if (WARN_ON(!sband))
9243 		return;
9244 
9245 	link = sdata_dereference(sdata->link[link_id], sdata);
9246 	if (WARN_ON(!link))
9247 		return;
9248 
9249 	/* for MLO connections assume advertising all rates is OK */
9250 	if (!req->ap_mld_addr) {
9251 		assoc_data->supp_rates = bss->supp_rates;
9252 		assoc_data->supp_rates_len = bss->supp_rates_len;
9253 	}
9254 
9255 	/* copy and link elems for the STA profile */
9256 	if (req->links[link_id].elems_len) {
9257 		memcpy(assoc_data->ie_pos, req->links[link_id].elems,
9258 		       req->links[link_id].elems_len);
9259 		assoc_data->link[link_id].elems = assoc_data->ie_pos;
9260 		assoc_data->link[link_id].elems_len = req->links[link_id].elems_len;
9261 		assoc_data->ie_pos += req->links[link_id].elems_len;
9262 	}
9263 
9264 	link->u.mgd.beacon_crc_valid = false;
9265 	link->u.mgd.dtim_period = 0;
9266 	link->u.mgd.have_beacon = false;
9267 
9268 	/* override HT configuration only if the AP and we support it */
9269 	if (conn->mode >= IEEE80211_CONN_MODE_HT) {
9270 		struct ieee80211_sta_ht_cap sta_ht_cap;
9271 
9272 		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
9273 		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
9274 	}
9275 
9276 	rcu_read_lock();
9277 	bss_ies = rcu_dereference(cbss->beacon_ies);
9278 	if (bss_ies) {
9279 		u8 dtim_count = 0;
9280 
9281 		ieee80211_get_dtim(bss_ies, &dtim_count,
9282 				   &link->u.mgd.dtim_period);
9283 
9284 		sdata->deflink.u.mgd.have_beacon = true;
9285 
9286 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
9287 			link->conf->sync_tsf = bss_ies->tsf;
9288 			link->conf->sync_device_ts = bss->device_ts_beacon;
9289 			link->conf->sync_dtim_count = dtim_count;
9290 		}
9291 	} else {
9292 		bss_ies = rcu_dereference(cbss->ies);
9293 	}
9294 
9295 	if (bss_ies) {
9296 		const struct element *elem;
9297 
9298 		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
9299 					      bss_ies->data, bss_ies->len);
9300 		if (elem && elem->datalen >= 3)
9301 			link->conf->profile_periodicity = elem->data[2];
9302 		else
9303 			link->conf->profile_periodicity = 0;
9304 
9305 		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
9306 					  bss_ies->data, bss_ies->len);
9307 		if (elem && elem->datalen >= 11 &&
9308 		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
9309 			link->conf->ema_ap = true;
9310 		else
9311 			link->conf->ema_ap = false;
9312 	}
9313 	rcu_read_unlock();
9314 
9315 	if (bss->corrupt_data) {
9316 		char *corrupt_type = "data";
9317 
9318 		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
9319 			if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
9320 				corrupt_type = "beacon and probe response";
9321 			else
9322 				corrupt_type = "beacon";
9323 		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) {
9324 			corrupt_type = "probe response";
9325 		}
9326 		sdata_info(sdata, "associating to AP %pM with corrupt %s\n",
9327 			   cbss->bssid, corrupt_type);
9328 	}
9329 
9330 	if (link->u.mgd.req_smps == IEEE80211_SMPS_AUTOMATIC) {
9331 		if (sdata->u.mgd.powersave)
9332 			link->smps_mode = IEEE80211_SMPS_DYNAMIC;
9333 		else
9334 			link->smps_mode = IEEE80211_SMPS_OFF;
9335 	} else {
9336 		link->smps_mode = link->u.mgd.req_smps;
9337 	}
9338 }
9339 
9340 static int
9341 ieee80211_mgd_get_ap_ht_vht_capa(struct ieee80211_sub_if_data *sdata,
9342 				 struct ieee80211_mgd_assoc_data *assoc_data,
9343 				 int link_id)
9344 {
9345 	struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
9346 	enum nl80211_band band = cbss->channel->band;
9347 	struct ieee80211_supported_band *sband;
9348 	const struct element *elem;
9349 	int err;
9350 
9351 	/* neither HT nor VHT elements used on 6 GHz */
9352 	if (band == NL80211_BAND_6GHZ)
9353 		return 0;
9354 
9355 	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_HT)
9356 		return 0;
9357 
9358 	rcu_read_lock();
9359 	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_OPERATION);
9360 	if (!elem || elem->datalen < sizeof(struct ieee80211_ht_operation)) {
9361 		mlme_link_id_dbg(sdata, link_id, "no HT operation on BSS %pM\n",
9362 				 cbss->bssid);
9363 		err = -EINVAL;
9364 		goto out_rcu;
9365 	}
9366 	assoc_data->link[link_id].ap_ht_param =
9367 		((struct ieee80211_ht_operation *)(elem->data))->ht_param;
9368 	rcu_read_unlock();
9369 
9370 	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_VHT)
9371 		return 0;
9372 
9373 	/* some drivers want to support VHT on 2.4 GHz even */
9374 	sband = sdata->local->hw.wiphy->bands[band];
9375 	if (!sband->vht_cap.vht_supported)
9376 		return 0;
9377 
9378 	rcu_read_lock();
9379 	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
9380 	/* but even then accept it not being present on the AP */
9381 	if (!elem && band == NL80211_BAND_2GHZ) {
9382 		err = 0;
9383 		goto out_rcu;
9384 	}
9385 	if (!elem || elem->datalen < sizeof(struct ieee80211_vht_cap)) {
9386 		mlme_link_id_dbg(sdata, link_id, "no VHT capa on BSS %pM\n",
9387 				 cbss->bssid);
9388 		err = -EINVAL;
9389 		goto out_rcu;
9390 	}
9391 	memcpy(&assoc_data->link[link_id].ap_vht_cap, elem->data,
9392 	       sizeof(struct ieee80211_vht_cap));
9393 	rcu_read_unlock();
9394 
9395 	return 0;
9396 out_rcu:
9397 	rcu_read_unlock();
9398 	return err;
9399 }
9400 
9401 static bool
9402 ieee80211_mgd_assoc_bss_has_mld_ext_capa_ops(struct cfg80211_assoc_request *req)
9403 {
9404 	const struct cfg80211_bss_ies *ies;
9405 	struct cfg80211_bss *bss;
9406 	const struct element *ml;
9407 
9408 	/* not an MLO connection if link_id < 0, so irrelevant */
9409 	if (req->link_id < 0)
9410 		return false;
9411 
9412 	bss = req->links[req->link_id].bss;
9413 
9414 	guard(rcu)();
9415 	ies = rcu_dereference(bss->ies);
9416 	for_each_element_extid(ml, WLAN_EID_EXT_EHT_MULTI_LINK,
9417 			       ies->data, ies->len) {
9418 		const struct ieee80211_multi_link_elem *mle;
9419 
9420 		if (!ieee80211_mle_type_ok(ml->data + 1,
9421 					   IEEE80211_ML_CONTROL_TYPE_BASIC,
9422 					   ml->datalen - 1))
9423 			continue;
9424 
9425 		mle = (void *)(ml->data + 1);
9426 		if (mle->control & cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP))
9427 			return true;
9428 	}
9429 
9430 	return false;
9431 
9432 }
9433 
9434 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
9435 			struct cfg80211_assoc_request *req)
9436 {
9437 	unsigned int assoc_link_id = req->link_id < 0 ? 0 : req->link_id;
9438 	struct ieee80211_local *local = sdata->local;
9439 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9440 	struct ieee80211_mgd_assoc_data *assoc_data;
9441 	const struct element *ssid_elem;
9442 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
9443 	struct ieee80211_link_data *link;
9444 	struct cfg80211_bss *cbss;
9445 	bool override, uapsd_supported;
9446 	bool match_auth;
9447 	int i, err;
9448 	size_t size = sizeof(*assoc_data) + req->ie_len;
9449 
9450 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
9451 		size += req->links[i].elems_len;
9452 
9453 	/* FIXME: no support for 4-addr MLO yet */
9454 	if (sdata->u.mgd.use_4addr && req->link_id >= 0)
9455 		return -EOPNOTSUPP;
9456 
9457 	assoc_data = kzalloc(size, GFP_KERNEL);
9458 	if (!assoc_data)
9459 		return -ENOMEM;
9460 
9461 	cbss = req->link_id < 0 ? req->bss : req->links[req->link_id].bss;
9462 
9463 	if (ieee80211_mgd_csa_in_process(sdata, cbss)) {
9464 		sdata_info(sdata, "AP is in CSA process, reject assoc\n");
9465 		err = -EINVAL;
9466 		goto err_free;
9467 	}
9468 
9469 	rcu_read_lock();
9470 	ssid_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
9471 	if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
9472 		rcu_read_unlock();
9473 		err = -EINVAL;
9474 		goto err_free;
9475 	}
9476 
9477 	memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
9478 	assoc_data->ssid_len = ssid_elem->datalen;
9479 	rcu_read_unlock();
9480 
9481 	if (req->ap_mld_addr)
9482 		memcpy(assoc_data->ap_addr, req->ap_mld_addr, ETH_ALEN);
9483 	else
9484 		memcpy(assoc_data->ap_addr, cbss->bssid, ETH_ALEN);
9485 
9486 	/*
9487 	 * Many APs have broken parsing of the extended MLD capa/ops field,
9488 	 * dropping (re-)association request frames or replying with association
9489 	 * response with a failure status if it's present.
9490 	 * Set our value from the userspace request only in strict mode or if
9491 	 * the AP also had that field present.
9492 	 */
9493 	if (ieee80211_hw_check(&local->hw, STRICT) ||
9494 	    ieee80211_mgd_assoc_bss_has_mld_ext_capa_ops(req))
9495 		assoc_data->ext_mld_capa_ops =
9496 			cpu_to_le16(req->ext_mld_capa_ops);
9497 
9498 	if (ifmgd->associated) {
9499 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9500 
9501 		sdata_info(sdata,
9502 			   "disconnect from AP %pM for new assoc to %pM\n",
9503 			   sdata->vif.cfg.ap_addr, assoc_data->ap_addr);
9504 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
9505 				       WLAN_REASON_UNSPECIFIED,
9506 				       false, frame_buf);
9507 
9508 		ieee80211_report_disconnect(sdata, frame_buf,
9509 					    sizeof(frame_buf), true,
9510 					    WLAN_REASON_UNSPECIFIED,
9511 					    false);
9512 	}
9513 
9514 	memset(sdata->u.mgd.userspace_selectors, 0,
9515 	       sizeof(sdata->u.mgd.userspace_selectors));
9516 	ieee80211_parse_cfg_selectors(sdata->u.mgd.userspace_selectors,
9517 				      req->supported_selectors,
9518 				      req->supported_selectors_len);
9519 
9520 	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
9521 	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
9522 	       sizeof(ifmgd->ht_capa_mask));
9523 
9524 	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
9525 	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
9526 	       sizeof(ifmgd->vht_capa_mask));
9527 
9528 	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
9529 	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
9530 	       sizeof(ifmgd->s1g_capa_mask));
9531 
9532 	/* keep some setup (AP STA, channel, ...) if matching */
9533 	match_auth = ifmgd->auth_data &&
9534 		     ether_addr_equal(ifmgd->auth_data->ap_addr,
9535 				      assoc_data->ap_addr) &&
9536 		     ifmgd->auth_data->link_id == req->link_id;
9537 
9538 	if (req->ap_mld_addr) {
9539 		uapsd_supported = true;
9540 
9541 		if (req->flags & (ASSOC_REQ_DISABLE_HT |
9542 				  ASSOC_REQ_DISABLE_VHT |
9543 				  ASSOC_REQ_DISABLE_HE |
9544 				  ASSOC_REQ_DISABLE_EHT)) {
9545 			err = -EINVAL;
9546 			goto err_free;
9547 		}
9548 
9549 		for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
9550 			struct ieee80211_supported_band *sband;
9551 			struct cfg80211_bss *link_cbss = req->links[i].bss;
9552 			struct ieee80211_bss *bss;
9553 
9554 			if (!link_cbss)
9555 				continue;
9556 
9557 			bss = (void *)link_cbss->priv;
9558 
9559 			if (!bss->wmm_used) {
9560 				err = -EINVAL;
9561 				req->links[i].error = err;
9562 				goto err_free;
9563 			}
9564 
9565 			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
9566 				err = -EINVAL;
9567 				req->links[i].error = err;
9568 				goto err_free;
9569 			}
9570 
9571 			link = sdata_dereference(sdata->link[i], sdata);
9572 			if (link)
9573 				ether_addr_copy(assoc_data->link[i].addr,
9574 						link->conf->addr);
9575 			else
9576 				eth_random_addr(assoc_data->link[i].addr);
9577 			sband = local->hw.wiphy->bands[link_cbss->channel->band];
9578 
9579 			if (match_auth && i == assoc_link_id && link)
9580 				assoc_data->link[i].conn = link->u.mgd.conn;
9581 			else
9582 				assoc_data->link[i].conn =
9583 					ieee80211_conn_settings_unlimited;
9584 			ieee80211_determine_our_sta_mode_assoc(sdata, sband,
9585 							       req, true, i,
9586 							       &assoc_data->link[i].conn);
9587 			assoc_data->link[i].bss = link_cbss;
9588 			assoc_data->link[i].disabled = req->links[i].disabled;
9589 
9590 			if (!bss->uapsd_supported)
9591 				uapsd_supported = false;
9592 
9593 			if (assoc_data->link[i].conn.mode < IEEE80211_CONN_MODE_EHT) {
9594 				err = -EINVAL;
9595 				req->links[i].error = err;
9596 				goto err_free;
9597 			}
9598 
9599 			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata,
9600 							       assoc_data, i);
9601 			if (err) {
9602 				err = -EINVAL;
9603 				req->links[i].error = err;
9604 				goto err_free;
9605 			}
9606 		}
9607 
9608 		assoc_data->wmm = true;
9609 	} else {
9610 		struct ieee80211_supported_band *sband;
9611 		struct ieee80211_bss *bss = (void *)cbss->priv;
9612 
9613 		memcpy(assoc_data->link[0].addr, sdata->vif.addr, ETH_ALEN);
9614 		assoc_data->s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
9615 
9616 		assoc_data->wmm = bss->wmm_used &&
9617 				  (local->hw.queues >= IEEE80211_NUM_ACS);
9618 
9619 		if (cbss->channel->band == NL80211_BAND_6GHZ &&
9620 		    req->flags & (ASSOC_REQ_DISABLE_HT |
9621 				  ASSOC_REQ_DISABLE_VHT |
9622 				  ASSOC_REQ_DISABLE_HE)) {
9623 			err = -EINVAL;
9624 			goto err_free;
9625 		}
9626 
9627 		sband = local->hw.wiphy->bands[cbss->channel->band];
9628 
9629 		assoc_data->link[0].bss = cbss;
9630 
9631 		if (match_auth)
9632 			assoc_data->link[0].conn = sdata->deflink.u.mgd.conn;
9633 		else
9634 			assoc_data->link[0].conn =
9635 				ieee80211_conn_settings_unlimited;
9636 		ieee80211_determine_our_sta_mode_assoc(sdata, sband, req,
9637 						       assoc_data->wmm, 0,
9638 						       &assoc_data->link[0].conn);
9639 
9640 		uapsd_supported = bss->uapsd_supported;
9641 
9642 		err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, assoc_data, 0);
9643 		if (err)
9644 			goto err_free;
9645 	}
9646 
9647 	assoc_data->spp_amsdu = req->flags & ASSOC_REQ_SPP_AMSDU;
9648 
9649 	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
9650 		err = -EBUSY;
9651 		goto err_free;
9652 	}
9653 
9654 	if (ifmgd->assoc_data) {
9655 		err = -EBUSY;
9656 		goto err_free;
9657 	}
9658 
9659 	/* Cleanup is delayed if auth_data matches */
9660 	if (ifmgd->auth_data && !match_auth)
9661 		ieee80211_destroy_auth_data(sdata, false);
9662 
9663 	if (req->ie && req->ie_len) {
9664 		memcpy(assoc_data->ie, req->ie, req->ie_len);
9665 		assoc_data->ie_len = req->ie_len;
9666 		assoc_data->ie_pos = assoc_data->ie + assoc_data->ie_len;
9667 	} else {
9668 		assoc_data->ie_pos = assoc_data->ie;
9669 	}
9670 
9671 	if (req->fils_kek) {
9672 		/* should already be checked in cfg80211 - so warn */
9673 		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
9674 			err = -EINVAL;
9675 			goto err_free;
9676 		}
9677 		memcpy(assoc_data->fils_kek, req->fils_kek,
9678 		       req->fils_kek_len);
9679 		assoc_data->fils_kek_len = req->fils_kek_len;
9680 	}
9681 
9682 	if (req->fils_nonces)
9683 		memcpy(assoc_data->fils_nonces, req->fils_nonces,
9684 		       2 * FILS_NONCE_LEN);
9685 
9686 	/* default timeout */
9687 	assoc_data->timeout = jiffies;
9688 	assoc_data->timeout_started = true;
9689 
9690 	assoc_data->assoc_link_id = assoc_link_id;
9691 
9692 	if (req->ap_mld_addr) {
9693 		/* if there was no authentication, set up the link */
9694 		err = ieee80211_vif_set_links(sdata, BIT(assoc_link_id), 0);
9695 		if (err)
9696 			goto err_clear;
9697 	}
9698 
9699 	link = sdata_dereference(sdata->link[assoc_link_id], sdata);
9700 	if (WARN_ON(!link)) {
9701 		err = -EINVAL;
9702 		goto err_clear;
9703 	}
9704 
9705 	override = link->u.mgd.conn.mode !=
9706 			assoc_data->link[assoc_link_id].conn.mode ||
9707 		   link->u.mgd.conn.bw_limit !=
9708 			assoc_data->link[assoc_link_id].conn.bw_limit;
9709 	link->u.mgd.conn = assoc_data->link[assoc_link_id].conn;
9710 
9711 	ieee80211_setup_assoc_link(sdata, assoc_data, req, &link->u.mgd.conn,
9712 				   assoc_link_id);
9713 
9714 	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
9715 		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
9716 	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
9717 		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
9718 
9719 	if (assoc_data->wmm && uapsd_supported &&
9720 	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
9721 		assoc_data->uapsd = true;
9722 		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
9723 	} else {
9724 		assoc_data->uapsd = false;
9725 		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
9726 	}
9727 
9728 	if (req->prev_bssid)
9729 		memcpy(assoc_data->prev_ap_addr, req->prev_bssid, ETH_ALEN);
9730 
9731 	if (req->use_mfp) {
9732 		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
9733 		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
9734 	} else {
9735 		ifmgd->mfp = IEEE80211_MFP_DISABLED;
9736 		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
9737 	}
9738 
9739 	if (req->flags & ASSOC_REQ_USE_RRM)
9740 		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
9741 	else
9742 		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
9743 
9744 	if (req->crypto.control_port)
9745 		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
9746 	else
9747 		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
9748 
9749 	sdata->control_port_protocol = req->crypto.control_port_ethertype;
9750 	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
9751 	sdata->control_port_over_nl80211 =
9752 					req->crypto.control_port_over_nl80211;
9753 	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
9754 
9755 	/* kick off associate process */
9756 	ifmgd->assoc_data = assoc_data;
9757 
9758 	for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) {
9759 		if (!assoc_data->link[i].bss)
9760 			continue;
9761 		if (i == assoc_data->assoc_link_id)
9762 			continue;
9763 		/* only calculate the mode, hence link == NULL */
9764 		err = ieee80211_prep_channel(sdata, NULL, i,
9765 					     assoc_data->link[i].bss, true,
9766 					     &assoc_data->link[i].conn,
9767 					     sdata->u.mgd.userspace_selectors);
9768 		if (err) {
9769 			req->links[i].error = err;
9770 			goto err_clear;
9771 		}
9772 	}
9773 
9774 	memcpy(vif_cfg->ssid, assoc_data->ssid, assoc_data->ssid_len);
9775 	vif_cfg->ssid_len = assoc_data->ssid_len;
9776 
9777 	/* needed for transmitting the assoc frames properly */
9778 	memcpy(sdata->vif.cfg.ap_addr, assoc_data->ap_addr, ETH_ALEN);
9779 
9780 	err = ieee80211_prep_connection(sdata, cbss, req->link_id,
9781 					req->ap_mld_addr, true,
9782 					&assoc_data->link[assoc_link_id].conn,
9783 					override,
9784 					sdata->u.mgd.userspace_selectors);
9785 	if (err)
9786 		goto err_clear;
9787 
9788 	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC)) {
9789 		const struct cfg80211_bss_ies *beacon_ies;
9790 
9791 		rcu_read_lock();
9792 		beacon_ies = rcu_dereference(req->bss->beacon_ies);
9793 		if (!beacon_ies) {
9794 			/*
9795 			 * Wait up to one beacon interval ...
9796 			 * should this be more if we miss one?
9797 			 */
9798 			sdata_info(sdata, "waiting for beacon from %pM\n",
9799 				   link->u.mgd.bssid);
9800 			assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
9801 			assoc_data->timeout_started = true;
9802 			assoc_data->need_beacon = true;
9803 		}
9804 		rcu_read_unlock();
9805 	}
9806 
9807 	run_again(sdata, assoc_data->timeout);
9808 
9809 	/* We are associating, clean up auth_data */
9810 	if (ifmgd->auth_data)
9811 		ieee80211_destroy_auth_data(sdata, true);
9812 
9813 	return 0;
9814  err_clear:
9815 	if (!ifmgd->auth_data) {
9816 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
9817 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
9818 						  BSS_CHANGED_BSSID);
9819 	}
9820 	ifmgd->assoc_data = NULL;
9821  err_free:
9822 	kfree(assoc_data);
9823 	return err;
9824 }
9825 
9826 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
9827 			 struct cfg80211_deauth_request *req)
9828 {
9829 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9830 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9831 	bool tx = !req->local_state_change;
9832 	struct ieee80211_prep_tx_info info = {
9833 		.subtype = IEEE80211_STYPE_DEAUTH,
9834 	};
9835 
9836 	if (ifmgd->auth_data &&
9837 	    ether_addr_equal(ifmgd->auth_data->ap_addr, req->bssid)) {
9838 		sdata_info(sdata,
9839 			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
9840 			   req->bssid, req->reason_code,
9841 			   ieee80211_get_reason_code_string(req->reason_code));
9842 
9843 		info.link_id = ifmgd->auth_data->link_id;
9844 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
9845 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
9846 					       IEEE80211_STYPE_DEAUTH,
9847 					       req->reason_code, tx,
9848 					       frame_buf);
9849 		ieee80211_destroy_auth_data(sdata, false);
9850 		ieee80211_report_disconnect(sdata, frame_buf,
9851 					    sizeof(frame_buf), true,
9852 					    req->reason_code, false);
9853 		drv_mgd_complete_tx(sdata->local, sdata, &info);
9854 		return 0;
9855 	}
9856 
9857 	if (ifmgd->assoc_data &&
9858 	    ether_addr_equal(ifmgd->assoc_data->ap_addr, req->bssid)) {
9859 		sdata_info(sdata,
9860 			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
9861 			   req->bssid, req->reason_code,
9862 			   ieee80211_get_reason_code_string(req->reason_code));
9863 
9864 		info.link_id = ifmgd->assoc_data->assoc_link_id;
9865 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
9866 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
9867 					       IEEE80211_STYPE_DEAUTH,
9868 					       req->reason_code, tx,
9869 					       frame_buf);
9870 		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
9871 		ieee80211_report_disconnect(sdata, frame_buf,
9872 					    sizeof(frame_buf), true,
9873 					    req->reason_code, false);
9874 		drv_mgd_complete_tx(sdata->local, sdata, &info);
9875 		return 0;
9876 	}
9877 
9878 	if (ifmgd->associated &&
9879 	    ether_addr_equal(sdata->vif.cfg.ap_addr, req->bssid)) {
9880 		sdata_info(sdata,
9881 			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
9882 			   req->bssid, req->reason_code,
9883 			   ieee80211_get_reason_code_string(req->reason_code));
9884 
9885 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
9886 				       req->reason_code, tx, frame_buf);
9887 		ieee80211_report_disconnect(sdata, frame_buf,
9888 					    sizeof(frame_buf), true,
9889 					    req->reason_code, false);
9890 		return 0;
9891 	}
9892 
9893 	return -ENOTCONN;
9894 }
9895 
9896 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
9897 			   struct cfg80211_disassoc_request *req)
9898 {
9899 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9900 
9901 	if (!sdata->u.mgd.associated ||
9902 	    memcmp(sdata->vif.cfg.ap_addr, req->ap_addr, ETH_ALEN))
9903 		return -ENOTCONN;
9904 
9905 	sdata_info(sdata,
9906 		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
9907 		   req->ap_addr, req->reason_code,
9908 		   ieee80211_get_reason_code_string(req->reason_code));
9909 
9910 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
9911 			       req->reason_code, !req->local_state_change,
9912 			       frame_buf);
9913 
9914 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
9915 				    req->reason_code, false);
9916 
9917 	return 0;
9918 }
9919 
9920 void ieee80211_mgd_stop_link(struct ieee80211_link_data *link)
9921 {
9922 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
9923 			  &link->u.mgd.request_smps_work);
9924 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
9925 			  &link->u.mgd.recalc_smps);
9926 	wiphy_delayed_work_cancel(link->sdata->local->hw.wiphy,
9927 				  &link->u.mgd.csa.switch_work);
9928 }
9929 
9930 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
9931 {
9932 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9933 
9934 	/*
9935 	 * Make sure some work items will not run after this,
9936 	 * they will not do anything but might not have been
9937 	 * cancelled when disconnecting.
9938 	 */
9939 	wiphy_work_cancel(sdata->local->hw.wiphy,
9940 			  &ifmgd->monitor_work);
9941 	wiphy_work_cancel(sdata->local->hw.wiphy,
9942 			  &ifmgd->beacon_connection_loss_work);
9943 	wiphy_work_cancel(sdata->local->hw.wiphy,
9944 			  &ifmgd->csa_connection_drop_work);
9945 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
9946 				  &ifmgd->tdls_peer_del_work);
9947 
9948 	if (ifmgd->assoc_data)
9949 		ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
9950 	if (ifmgd->auth_data)
9951 		ieee80211_destroy_auth_data(sdata, false);
9952 	spin_lock_bh(&ifmgd->teardown_lock);
9953 	if (ifmgd->teardown_skb) {
9954 		kfree_skb(ifmgd->teardown_skb);
9955 		ifmgd->teardown_skb = NULL;
9956 		ifmgd->orig_teardown_skb = NULL;
9957 	}
9958 	kfree(ifmgd->assoc_req_ies);
9959 	ifmgd->assoc_req_ies = NULL;
9960 	ifmgd->assoc_req_ies_len = 0;
9961 	spin_unlock_bh(&ifmgd->teardown_lock);
9962 	timer_delete_sync(&ifmgd->timer);
9963 }
9964 
9965 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
9966 			       enum nl80211_cqm_rssi_threshold_event rssi_event,
9967 			       s32 rssi_level,
9968 			       gfp_t gfp)
9969 {
9970 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
9971 
9972 	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
9973 
9974 	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
9975 }
9976 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
9977 
9978 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
9979 {
9980 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
9981 
9982 	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
9983 
9984 	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
9985 }
9986 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
9987 
9988 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
9989 					    int rssi_min_thold,
9990 					    int rssi_max_thold)
9991 {
9992 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
9993 
9994 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
9995 		return;
9996 
9997 	/*
9998 	 * Scale up threshold values before storing it, as the RSSI averaging
9999 	 * algorithm uses a scaled up value as well. Change this scaling
10000 	 * factor if the RSSI averaging algorithm changes.
10001 	 */
10002 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
10003 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
10004 }
10005 
10006 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
10007 				    int rssi_min_thold,
10008 				    int rssi_max_thold)
10009 {
10010 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10011 
10012 	WARN_ON(rssi_min_thold == rssi_max_thold ||
10013 		rssi_min_thold > rssi_max_thold);
10014 
10015 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
10016 				       rssi_max_thold);
10017 }
10018 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
10019 
10020 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
10021 {
10022 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10023 
10024 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
10025 }
10026 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
10027 
10028 void ieee80211_process_ml_reconf_resp(struct ieee80211_sub_if_data *sdata,
10029 				      struct ieee80211_mgmt *mgmt, size_t len)
10030 {
10031 	struct ieee80211_local *local = sdata->local;
10032 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10033 	struct ieee80211_mgd_assoc_data *add_links_data =
10034 		ifmgd->reconf.add_links_data;
10035 	struct sta_info *sta;
10036 	struct cfg80211_mlo_reconf_done_data done_data = {};
10037 	u16 sta_changed_links = sdata->u.mgd.reconf.added_links |
10038 		                sdata->u.mgd.reconf.removed_links;
10039 	u16 link_mask, valid_links;
10040 	unsigned int link_id;
10041 	size_t orig_len = len;
10042 	u8 i, group_key_data_len;
10043 	u8 *pos;
10044 
10045 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
10046 	    len < offsetofend(typeof(*mgmt), u.action.u.ml_reconf_resp) ||
10047 	    mgmt->u.action.u.ml_reconf_resp.dialog_token !=
10048 	    sdata->u.mgd.reconf.dialog_token ||
10049 	    !sta_changed_links)
10050 		return;
10051 
10052 	pos = mgmt->u.action.u.ml_reconf_resp.variable;
10053 	len -= offsetofend(typeof(*mgmt), u.action.u.ml_reconf_resp);
10054 
10055 	/* each status duple is 3 octets */
10056 	if (len < mgmt->u.action.u.ml_reconf_resp.count * 3) {
10057 		sdata_info(sdata,
10058 			   "mlo: reconf: unexpected len=%zu, count=%u\n",
10059 			   len, mgmt->u.action.u.ml_reconf_resp.count);
10060 		goto disconnect;
10061 	}
10062 
10063 	link_mask = sta_changed_links;
10064 	for (i = 0; i < mgmt->u.action.u.ml_reconf_resp.count; i++) {
10065 		u16 status = get_unaligned_le16(pos + 1);
10066 
10067 		link_id = *pos;
10068 
10069 		if (!(link_mask & BIT(link_id))) {
10070 			sdata_info(sdata,
10071 				   "mlo: reconf: unexpected link: %u, changed=0x%x\n",
10072 				   link_id, sta_changed_links);
10073 			goto disconnect;
10074 		}
10075 
10076 		/* clear the corresponding link, to detect the case that
10077 		 * the same link was included more than one time
10078 		 */
10079 		link_mask &= ~BIT(link_id);
10080 
10081 		/* Handle failure to remove links here. Failure to remove added
10082 		 * links will be done later in the flow.
10083 		 */
10084 		if (status != WLAN_STATUS_SUCCESS) {
10085 			sdata_info(sdata,
10086 				   "mlo: reconf: failed on link=%u, status=%u\n",
10087 				   link_id, status);
10088 
10089 			/* The AP MLD failed to remove a link that was already
10090 			 * removed locally. As this is not expected behavior,
10091 			 * disconnect
10092 			 */
10093 			if (sdata->u.mgd.reconf.removed_links & BIT(link_id))
10094 				goto disconnect;
10095 
10096 			/* The AP MLD failed to add a link. Remove it from the
10097 			 * added links.
10098 			 */
10099 			sdata->u.mgd.reconf.added_links &= ~BIT(link_id);
10100 		}
10101 
10102 		pos += 3;
10103 		len -= 3;
10104 	}
10105 
10106 	if (link_mask) {
10107 		sdata_info(sdata,
10108 			   "mlo: reconf: no response for links=0x%x\n",
10109 			   link_mask);
10110 		goto disconnect;
10111 	}
10112 
10113 	if (!sdata->u.mgd.reconf.added_links)
10114 		goto out;
10115 
10116 	if (len < 1 || len < 1 + *pos) {
10117 		sdata_info(sdata,
10118 			   "mlo: reconf: invalid group key data length");
10119 		goto disconnect;
10120 	}
10121 
10122 	/* The Group Key Data field must be present when links are added. This
10123 	 * field should be processed by userland.
10124 	 */
10125 	group_key_data_len = *pos++;
10126 
10127 	pos += group_key_data_len;
10128 	len -= group_key_data_len + 1;
10129 
10130 	/* Process the information for the added links */
10131 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
10132 	if (WARN_ON(!sta))
10133 		goto disconnect;
10134 
10135 	valid_links = sdata->vif.valid_links;
10136 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10137 		if (!add_links_data->link[link_id].bss ||
10138 		    !(sdata->u.mgd.reconf.added_links & BIT(link_id)))
10139 			continue;
10140 
10141 		valid_links |= BIT(link_id);
10142 		if (ieee80211_sta_allocate_link(sta, link_id))
10143 			goto disconnect;
10144 	}
10145 
10146 	ieee80211_vif_set_links(sdata, valid_links, sdata->vif.dormant_links);
10147 	link_mask = 0;
10148 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10149 		struct cfg80211_bss *cbss = add_links_data->link[link_id].bss;
10150 		struct ieee80211_link_data *link;
10151 		struct link_sta_info *link_sta;
10152 		u64 changed = 0;
10153 
10154 		if (!cbss)
10155 			continue;
10156 
10157 		link = sdata_dereference(sdata->link[link_id], sdata);
10158 		if (WARN_ON(!link))
10159 			goto disconnect;
10160 
10161 		link_info(link,
10162 			  "mlo: reconf: local address %pM, AP link address %pM\n",
10163 			  add_links_data->link[link_id].addr,
10164 			  add_links_data->link[link_id].bss->bssid);
10165 
10166 		link_sta = rcu_dereference_protected(sta->link[link_id],
10167 						     lockdep_is_held(&local->hw.wiphy->mtx));
10168 		if (WARN_ON(!link_sta))
10169 			goto disconnect;
10170 
10171 		if (!link->u.mgd.have_beacon) {
10172 			const struct cfg80211_bss_ies *ies;
10173 
10174 			rcu_read_lock();
10175 			ies = rcu_dereference(cbss->beacon_ies);
10176 			if (ies)
10177 				link->u.mgd.have_beacon = true;
10178 			else
10179 				ies = rcu_dereference(cbss->ies);
10180 			ieee80211_get_dtim(ies,
10181 					   &link->conf->sync_dtim_count,
10182 					   &link->u.mgd.dtim_period);
10183 			link->conf->beacon_int = cbss->beacon_interval;
10184 			rcu_read_unlock();
10185 		}
10186 
10187 		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
10188 
10189 		link->u.mgd.conn = add_links_data->link[link_id].conn;
10190 		if (ieee80211_prep_channel(sdata, link, link_id, cbss,
10191 					   true, &link->u.mgd.conn,
10192 					   sdata->u.mgd.userspace_selectors)) {
10193 			link_info(link, "mlo: reconf: prep_channel failed\n");
10194 			goto disconnect;
10195 		}
10196 
10197 		if (ieee80211_mgd_setup_link_sta(link, sta, link_sta,
10198 						 add_links_data->link[link_id].bss))
10199 			goto disconnect;
10200 
10201 		if (!ieee80211_assoc_config_link(link, link_sta,
10202 						 add_links_data->link[link_id].bss,
10203 						 mgmt, pos, len,
10204 						 &changed))
10205 			goto disconnect;
10206 
10207 		/* The AP MLD indicated success for this link, but the station
10208 		 * profile status indicated otherwise. Since there is an
10209 		 * inconsistency in the ML reconfiguration response, disconnect
10210 		 */
10211 		if (add_links_data->link[link_id].status != WLAN_STATUS_SUCCESS)
10212 			goto disconnect;
10213 
10214 		ieee80211_sta_init_nss(link_sta);
10215 		if (ieee80211_sta_activate_link(sta, link_id))
10216 			goto disconnect;
10217 
10218 		changed |= ieee80211_link_set_associated(link, cbss);
10219 		ieee80211_link_info_change_notify(sdata, link, changed);
10220 
10221 		ieee80211_recalc_smps(sdata, link);
10222 		link_mask |= BIT(link_id);
10223 	}
10224 
10225 	sdata_info(sdata,
10226 		   "mlo: reconf: current valid_links=0x%x, added=0x%x\n",
10227 		   valid_links, link_mask);
10228 
10229 	/* links might have changed due to rejected ones, set them again */
10230 	ieee80211_vif_set_links(sdata, valid_links, sdata->vif.dormant_links);
10231 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
10232 
10233 	ieee80211_recalc_ps(local);
10234 	ieee80211_recalc_ps_vif(sdata);
10235 
10236 	done_data.buf = (const u8 *)mgmt;
10237 	done_data.len = orig_len;
10238 	done_data.added_links = link_mask;
10239 
10240 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10241 		done_data.links[link_id].bss = add_links_data->link[link_id].bss;
10242 		done_data.links[link_id].addr =
10243 			add_links_data->link[link_id].addr;
10244 	}
10245 
10246 	cfg80211_mlo_reconf_add_done(sdata->dev, &done_data);
10247 	kfree(sdata->u.mgd.reconf.add_links_data);
10248 	sdata->u.mgd.reconf.add_links_data = NULL;
10249 out:
10250 	ieee80211_ml_reconf_reset(sdata);
10251 	return;
10252 
10253 disconnect:
10254 	__ieee80211_disconnect(sdata);
10255 }
10256 
10257 static struct sk_buff *
10258 ieee80211_build_ml_reconf_req(struct ieee80211_sub_if_data *sdata,
10259 			      struct ieee80211_mgd_assoc_data *add_links_data,
10260 			      u16 removed_links, __le16 ext_mld_capa_ops)
10261 {
10262 	struct ieee80211_local *local = sdata->local;
10263 	struct ieee80211_mgmt *mgmt;
10264 	struct ieee80211_multi_link_elem *ml_elem;
10265 	struct ieee80211_mle_basic_common_info *common;
10266 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
10267 	struct sk_buff *skb;
10268 	size_t size;
10269 	unsigned int link_id;
10270 	__le16 eml_capa = 0, mld_capa_ops = 0;
10271 	struct ieee80211_tx_info *info;
10272 	u8 common_size, var_common_size;
10273 	u8 *ml_elem_len;
10274 	u16 capab = 0;
10275 
10276 	size = local->hw.extra_tx_headroom + sizeof(*mgmt);
10277 
10278 	/* Consider the maximal length of the reconfiguration ML element */
10279 	size += sizeof(struct ieee80211_multi_link_elem);
10280 
10281 	/* The Basic ML element and the Reconfiguration ML element have the same
10282 	 * fixed common information fields in the context of ML reconfiguration
10283 	 * action frame. The AP MLD MAC address must always be present
10284 	 */
10285 	common_size = sizeof(*common);
10286 
10287 	/* when adding links, the MLD capabilities must be present */
10288 	var_common_size = 0;
10289 	if (add_links_data) {
10290 		const struct wiphy_iftype_ext_capab *ift_ext_capa =
10291 			cfg80211_get_iftype_ext_capa(local->hw.wiphy,
10292 						     ieee80211_vif_type_p2p(&sdata->vif));
10293 
10294 		if (ift_ext_capa) {
10295 			eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities);
10296 			mld_capa_ops =
10297 				cpu_to_le16(ift_ext_capa->mld_capa_and_ops);
10298 		}
10299 
10300 		/* MLD capabilities and operation */
10301 		var_common_size += 2;
10302 
10303 		/* EML capabilities */
10304 		if (eml_capa & cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
10305 					    IEEE80211_EML_CAP_EMLMR_SUPPORT)))
10306 			var_common_size += 2;
10307 	}
10308 
10309 	if (ext_mld_capa_ops)
10310 		var_common_size += 2;
10311 
10312 	/* Add the common information length */
10313 	size += common_size + var_common_size;
10314 
10315 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10316 		struct cfg80211_bss *cbss;
10317 		size_t elems_len;
10318 
10319 		if (removed_links & BIT(link_id)) {
10320 			size += sizeof(struct ieee80211_mle_per_sta_profile) +
10321 				ETH_ALEN;
10322 			continue;
10323 		}
10324 
10325 		if (!add_links_data || !add_links_data->link[link_id].bss)
10326 			continue;
10327 
10328 		elems_len = add_links_data->link[link_id].elems_len;
10329 		cbss = add_links_data->link[link_id].bss;
10330 
10331 		/* should be the same across all BSSes */
10332 		if (cbss->capability & WLAN_CAPABILITY_PRIVACY)
10333 			capab |= WLAN_CAPABILITY_PRIVACY;
10334 
10335 		size += 2 + sizeof(struct ieee80211_mle_per_sta_profile) +
10336 			ETH_ALEN;
10337 
10338 		/* WMM */
10339 		size += 9;
10340 		size += ieee80211_link_common_elems_size(sdata, iftype, cbss,
10341 							 elems_len);
10342 	}
10343 
10344 	skb = alloc_skb(size, GFP_KERNEL);
10345 	if (!skb)
10346 		return NULL;
10347 
10348 	skb_reserve(skb, local->hw.extra_tx_headroom);
10349 	mgmt = skb_put_zero(skb, offsetofend(struct ieee80211_mgmt,
10350 					     u.action.u.ml_reconf_req));
10351 
10352 	/* Add the MAC header */
10353 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
10354 					  IEEE80211_STYPE_ACTION);
10355 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
10356 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
10357 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
10358 
10359 	/* Add the action frame fixed fields */
10360 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
10361 	mgmt->u.action.u.ml_reconf_req.action_code =
10362 		WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_REQ;
10363 
10364 	/* allocate a dialog token and store it */
10365 	sdata->u.mgd.reconf.dialog_token = ++sdata->u.mgd.dialog_token_alloc;
10366 	mgmt->u.action.u.ml_reconf_req.dialog_token =
10367 		sdata->u.mgd.reconf.dialog_token;
10368 
10369 	/* Add the ML reconfiguration element and the common information  */
10370 	skb_put_u8(skb, WLAN_EID_EXTENSION);
10371 	ml_elem_len = skb_put(skb, 1);
10372 	skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
10373 	ml_elem = skb_put(skb, sizeof(*ml_elem));
10374 	ml_elem->control =
10375 		cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_RECONF |
10376 			    IEEE80211_MLC_RECONF_PRES_MLD_MAC_ADDR);
10377 	common = skb_put(skb, common_size);
10378 	common->len = common_size + var_common_size;
10379 	memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
10380 
10381 	if (add_links_data) {
10382 		if (eml_capa &
10383 		    cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
10384 				 IEEE80211_EML_CAP_EMLMR_SUPPORT))) {
10385 			ml_elem->control |=
10386 				cpu_to_le16(IEEE80211_MLC_RECONF_PRES_EML_CAPA);
10387 			skb_put_data(skb, &eml_capa, sizeof(eml_capa));
10388 		}
10389 
10390 		ml_elem->control |=
10391 			cpu_to_le16(IEEE80211_MLC_RECONF_PRES_MLD_CAPA_OP);
10392 
10393 		skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops));
10394 	}
10395 
10396 	if (ext_mld_capa_ops) {
10397 		ml_elem->control |=
10398 			cpu_to_le16(IEEE80211_MLC_RECONF_PRES_EXT_MLD_CAPA_OP);
10399 		skb_put_data(skb, &ext_mld_capa_ops, sizeof(ext_mld_capa_ops));
10400 	}
10401 
10402 	if (sdata->u.mgd.flags & IEEE80211_STA_ENABLE_RRM)
10403 		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
10404 
10405 	/* Add the per station profile */
10406 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10407 		u8 *subelem_len = NULL;
10408 		u16 ctrl;
10409 		const u8 *addr;
10410 
10411 		/* Skip links that are not changing */
10412 		if (!(removed_links & BIT(link_id)) &&
10413 		    (!add_links_data || !add_links_data->link[link_id].bss))
10414 			continue;
10415 
10416 		ctrl = link_id |
10417 		       IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT;
10418 
10419 		if (removed_links & BIT(link_id)) {
10420 			struct ieee80211_bss_conf *conf =
10421 				sdata_dereference(sdata->vif.link_conf[link_id],
10422 						  sdata);
10423 			if (!conf)
10424 				continue;
10425 
10426 			addr = conf->addr;
10427 			ctrl |= u16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_DEL_LINK,
10428 						IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
10429 		} else {
10430 			addr = add_links_data->link[link_id].addr;
10431 			ctrl |= IEEE80211_MLE_STA_RECONF_CONTROL_COMPLETE_PROFILE |
10432 				u16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_ADD_LINK,
10433 						IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
10434 		}
10435 
10436 		skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
10437 		subelem_len = skb_put(skb, 1);
10438 
10439 		put_unaligned_le16(ctrl, skb_put(skb, sizeof(ctrl)));
10440 		skb_put_u8(skb, 1 + ETH_ALEN);
10441 		skb_put_data(skb, addr, ETH_ALEN);
10442 
10443 		if (!(removed_links & BIT(link_id))) {
10444 			u16 link_present_elems[PRESENT_ELEMS_MAX] = {};
10445 			size_t extra_used;
10446 			void *capab_pos;
10447 			u8 qos_info;
10448 
10449 			capab_pos = skb_put(skb, 2);
10450 
10451 			extra_used =
10452 				ieee80211_add_link_elems(sdata, skb, &capab, NULL,
10453 							 add_links_data->link[link_id].elems,
10454 							 add_links_data->link[link_id].elems_len,
10455 							 link_id, NULL,
10456 							 link_present_elems,
10457 							 add_links_data);
10458 
10459 			if (add_links_data->link[link_id].elems)
10460 				skb_put_data(skb,
10461 					     add_links_data->link[link_id].elems +
10462 					     extra_used,
10463 					     add_links_data->link[link_id].elems_len -
10464 					     extra_used);
10465 			if (sdata->u.mgd.flags & IEEE80211_STA_UAPSD_ENABLED) {
10466 				qos_info = sdata->u.mgd.uapsd_queues;
10467 				qos_info |= (sdata->u.mgd.uapsd_max_sp_len <<
10468 					     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
10469 			} else {
10470 				qos_info = 0;
10471 			}
10472 
10473 			ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
10474 			put_unaligned_le16(capab, capab_pos);
10475 		}
10476 
10477 		ieee80211_fragment_element(skb, subelem_len,
10478 					   IEEE80211_MLE_SUBELEM_FRAGMENT);
10479 	}
10480 
10481 	ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT);
10482 
10483 	info = IEEE80211_SKB_CB(skb);
10484 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
10485 
10486 	return skb;
10487 }
10488 
10489 int ieee80211_mgd_assoc_ml_reconf(struct ieee80211_sub_if_data *sdata,
10490 				  struct cfg80211_ml_reconf_req *req)
10491 {
10492 	struct ieee80211_local *local = sdata->local;
10493 	struct ieee80211_mgd_assoc_data *data = NULL;
10494 	struct sta_info *sta;
10495 	struct sk_buff *skb;
10496 	u16 added_links, new_valid_links;
10497 	int link_id, err;
10498 
10499 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
10500 	    !(sdata->vif.cfg.mld_capa_op &
10501 	      IEEE80211_MLD_CAP_OP_LINK_RECONF_SUPPORT))
10502 		return -EINVAL;
10503 
10504 	/* No support for concurrent ML reconfiguration operation */
10505 	if (sdata->u.mgd.reconf.added_links ||
10506 	    sdata->u.mgd.reconf.removed_links)
10507 		return -EBUSY;
10508 
10509 	added_links = 0;
10510 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10511 		if (!req->add_links[link_id].bss)
10512 			continue;
10513 
10514 		added_links |= BIT(link_id);
10515 	}
10516 
10517 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
10518 	if (WARN_ON(!sta))
10519 		return -ENOLINK;
10520 
10521 	/* Adding links to the set of valid link is done only after a successful
10522 	 * ML reconfiguration frame exchange. Here prepare the data for the ML
10523 	 * reconfiguration frame construction and allocate the required
10524 	 * resources
10525 	 */
10526 	if (added_links) {
10527 		bool uapsd_supported;
10528 
10529 		data = kzalloc(sizeof(*data), GFP_KERNEL);
10530 		if (!data)
10531 			return -ENOMEM;
10532 
10533 		data->assoc_link_id = -1;
10534 		data->wmm = true;
10535 
10536 		uapsd_supported = true;
10537 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
10538 		     link_id++) {
10539 			struct ieee80211_supported_band *sband;
10540 			struct cfg80211_bss *link_cbss =
10541 				req->add_links[link_id].bss;
10542 			struct ieee80211_bss *bss;
10543 
10544 			if (!link_cbss)
10545 				continue;
10546 
10547 			bss = (void *)link_cbss->priv;
10548 
10549 			if (!bss->wmm_used) {
10550 				err = -EINVAL;
10551 				goto err_free;
10552 			}
10553 
10554 			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
10555 				err = -EINVAL;
10556 				goto err_free;
10557 			}
10558 
10559 			eth_random_addr(data->link[link_id].addr);
10560 			data->link[link_id].conn =
10561 				ieee80211_conn_settings_unlimited;
10562 			sband =
10563 				local->hw.wiphy->bands[link_cbss->channel->band];
10564 
10565 			ieee80211_determine_our_sta_mode(sdata, sband,
10566 							 NULL, true, link_id,
10567 							 &data->link[link_id].conn);
10568 
10569 			data->link[link_id].bss = link_cbss;
10570 			data->link[link_id].disabled =
10571 				req->add_links[link_id].disabled;
10572 			data->link[link_id].elems =
10573 				(u8 *)req->add_links[link_id].elems;
10574 			data->link[link_id].elems_len =
10575 				req->add_links[link_id].elems_len;
10576 
10577 			if (!bss->uapsd_supported)
10578 				uapsd_supported = false;
10579 
10580 			if (data->link[link_id].conn.mode <
10581 			    IEEE80211_CONN_MODE_EHT) {
10582 				err = -EINVAL;
10583 				goto err_free;
10584 			}
10585 
10586 			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, data,
10587 							       link_id);
10588 			if (err) {
10589 				err = -EINVAL;
10590 				goto err_free;
10591 			}
10592 		}
10593 
10594 		/* Require U-APSD support if we enabled it */
10595 		if (sdata->u.mgd.flags & IEEE80211_STA_UAPSD_ENABLED &&
10596 		    !uapsd_supported) {
10597 			err = -EINVAL;
10598 			sdata_info(sdata, "U-APSD on but not available on (all) new links\n");
10599 			goto err_free;
10600 		}
10601 
10602 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
10603 		     link_id++) {
10604 			if (!data->link[link_id].bss)
10605 				continue;
10606 
10607 			/* only used to verify the mode, nothing is allocated */
10608 			err = ieee80211_prep_channel(sdata, NULL, link_id,
10609 						     data->link[link_id].bss,
10610 						     true,
10611 						     &data->link[link_id].conn,
10612 						     sdata->u.mgd.userspace_selectors);
10613 			if (err)
10614 				goto err_free;
10615 		}
10616 	}
10617 
10618 	/* link removal is done before the ML reconfiguration frame exchange so
10619 	 * that these links will not be used between their removal by the AP MLD
10620 	 * and before the station got the ML reconfiguration response. Based on
10621 	 * Section 35.3.6.4 in Draft P802.11be_D7.0 the AP MLD should accept the
10622 	 * link removal request.
10623 	 */
10624 	if (req->rem_links) {
10625 		u16 new_active_links =
10626 			sdata->vif.active_links & ~req->rem_links;
10627 
10628 		new_valid_links = sdata->vif.valid_links & ~req->rem_links;
10629 
10630 		/* Should not be left with no valid links to perform the
10631 		 * ML reconfiguration
10632 		 */
10633 		if (!new_valid_links ||
10634 		    !(new_valid_links & ~sdata->vif.dormant_links)) {
10635 			sdata_info(sdata, "mlo: reconf: no valid links\n");
10636 			err = -EINVAL;
10637 			goto err_free;
10638 		}
10639 
10640 		if (new_active_links != sdata->vif.active_links) {
10641 			if (!new_active_links)
10642 				new_active_links =
10643 					BIT(__ffs(new_valid_links &
10644 						  ~sdata->vif.dormant_links));
10645 
10646 			err = ieee80211_set_active_links(&sdata->vif,
10647 							 new_active_links);
10648 			if (err) {
10649 				sdata_info(sdata,
10650 					   "mlo: reconf: failed set active links\n");
10651 				goto err_free;
10652 			}
10653 		}
10654 	}
10655 
10656 	/* Build the SKB before the link removal as the construction of the
10657 	 * station info for removed links requires the local address.
10658 	 * Invalidate the removed links, so that the transmission of the ML
10659 	 * reconfiguration request frame would not be done using them, as the AP
10660 	 * is expected to send the ML reconfiguration response frame on the link
10661 	 * on which the request was received.
10662 	 */
10663 	skb = ieee80211_build_ml_reconf_req(sdata, data, req->rem_links,
10664 					    cpu_to_le16(req->ext_mld_capa_ops));
10665 	if (!skb) {
10666 		err = -ENOMEM;
10667 		goto err_free;
10668 	}
10669 
10670 	if (req->rem_links) {
10671 		u16 new_dormant_links =
10672 			sdata->vif.dormant_links & ~req->rem_links;
10673 
10674 		err = ieee80211_vif_set_links(sdata, new_valid_links,
10675 					      new_dormant_links);
10676 		if (err) {
10677 			sdata_info(sdata,
10678 				   "mlo: reconf: failed set valid links\n");
10679 			kfree_skb(skb);
10680 			goto err_free;
10681 		}
10682 
10683 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
10684 		     link_id++) {
10685 			if (!(req->rem_links & BIT(link_id)))
10686 				continue;
10687 
10688 			ieee80211_sta_remove_link(sta, link_id);
10689 		}
10690 
10691 		/* notify the driver and upper layers */
10692 		ieee80211_vif_cfg_change_notify(sdata,
10693 						BSS_CHANGED_MLD_VALID_LINKS);
10694 		cfg80211_links_removed(sdata->dev, req->rem_links);
10695 	}
10696 
10697 	sdata_info(sdata, "mlo: reconf: adding=0x%x, removed=0x%x\n",
10698 		   added_links, req->rem_links);
10699 
10700 	ieee80211_tx_skb(sdata, skb);
10701 
10702 	sdata->u.mgd.reconf.added_links = added_links;
10703 	sdata->u.mgd.reconf.add_links_data = data;
10704 	sdata->u.mgd.reconf.removed_links = req->rem_links;
10705 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
10706 				 &sdata->u.mgd.reconf.wk,
10707 				 IEEE80211_ASSOC_TIMEOUT_SHORT);
10708 	return 0;
10709 
10710  err_free:
10711 	kfree(data);
10712 	return err;
10713 }
10714 
10715 static bool ieee80211_mgd_epcs_supp(struct ieee80211_sub_if_data *sdata)
10716 {
10717 	unsigned long valid_links = sdata->vif.valid_links;
10718 	u8 link_id;
10719 
10720 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
10721 
10722 	if (!ieee80211_vif_is_mld(&sdata->vif))
10723 		return false;
10724 
10725 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
10726 		struct ieee80211_bss_conf *bss_conf =
10727 			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
10728 
10729 		if (WARN_ON(!bss_conf) || !bss_conf->epcs_support)
10730 			return false;
10731 	}
10732 
10733 	return true;
10734 }
10735 
10736 int ieee80211_mgd_set_epcs(struct ieee80211_sub_if_data *sdata, bool enable)
10737 {
10738 	struct ieee80211_local *local = sdata->local;
10739 	struct ieee80211_mgmt *mgmt;
10740 	struct sk_buff *skb;
10741 	int frame_len = offsetofend(struct ieee80211_mgmt,
10742 				    u.action.u.epcs) + (enable ? 1 : 0);
10743 
10744 	if (!ieee80211_mgd_epcs_supp(sdata))
10745 		return -EINVAL;
10746 
10747 	if (sdata->u.mgd.epcs.enabled == enable &&
10748 	    !sdata->u.mgd.epcs.dialog_token)
10749 		return 0;
10750 
10751 	/* Do not allow enabling EPCS if the AP didn't respond yet.
10752 	 * However, allow disabling EPCS in such a case.
10753 	 */
10754 	if (sdata->u.mgd.epcs.dialog_token && enable)
10755 		return -EALREADY;
10756 
10757 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
10758 	if (!skb)
10759 		return -ENOBUFS;
10760 
10761 	skb_reserve(skb, local->hw.extra_tx_headroom);
10762 	mgmt = skb_put_zero(skb, frame_len);
10763 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
10764 					  IEEE80211_STYPE_ACTION);
10765 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
10766 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
10767 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
10768 
10769 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
10770 	if (enable) {
10771 		u8 *pos = mgmt->u.action.u.epcs.variable;
10772 
10773 		mgmt->u.action.u.epcs.action_code =
10774 			WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_REQ;
10775 
10776 		*pos = ++sdata->u.mgd.dialog_token_alloc;
10777 		sdata->u.mgd.epcs.dialog_token = *pos;
10778 	} else {
10779 		mgmt->u.action.u.epcs.action_code =
10780 			WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN;
10781 
10782 		ieee80211_epcs_teardown(sdata);
10783 		ieee80211_epcs_changed(sdata, false);
10784 	}
10785 
10786 	ieee80211_tx_skb(sdata, skb);
10787 	return 0;
10788 }
10789 
10790 static void ieee80211_ml_epcs(struct ieee80211_sub_if_data *sdata,
10791 			      struct ieee802_11_elems *elems)
10792 {
10793 	const struct element *sub;
10794 	size_t scratch_len = elems->ml_epcs_len;
10795 	u8 *scratch __free(kfree) = kzalloc(scratch_len, GFP_KERNEL);
10796 
10797 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
10798 
10799 	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_epcs)
10800 		return;
10801 
10802 	if (WARN_ON(!scratch))
10803 		return;
10804 
10805 	/* Directly parse the sub elements as the common information doesn't
10806 	 * hold any useful information.
10807 	 */
10808 	for_each_mle_subelement(sub, (const u8 *)elems->ml_epcs,
10809 				elems->ml_epcs_len) {
10810 		struct ieee80211_link_data *link;
10811 		struct ieee802_11_elems *link_elems __free(kfree);
10812 		u8 *pos = (void *)sub->data;
10813 		u16 control;
10814 		ssize_t len;
10815 		u8 link_id;
10816 
10817 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
10818 			continue;
10819 
10820 		if (sub->datalen < sizeof(control))
10821 			break;
10822 
10823 		control = get_unaligned_le16(pos);
10824 		link_id = control & IEEE80211_MLE_STA_EPCS_CONTROL_LINK_ID;
10825 
10826 		link = sdata_dereference(sdata->link[link_id], sdata);
10827 		if (!link)
10828 			continue;
10829 
10830 		len = cfg80211_defragment_element(sub, (u8 *)elems->ml_epcs,
10831 						  elems->ml_epcs_len,
10832 						  scratch, scratch_len,
10833 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
10834 		if (len < (ssize_t)sizeof(control))
10835 			continue;
10836 
10837 		pos = scratch + sizeof(control);
10838 		len -= sizeof(control);
10839 
10840 		link_elems = ieee802_11_parse_elems(pos, len, false, NULL);
10841 		if (!link_elems)
10842 			continue;
10843 
10844 		if (ieee80211_sta_wmm_params(sdata->local, link,
10845 					     link_elems->wmm_param,
10846 					     link_elems->wmm_param_len,
10847 					     link_elems->mu_edca_param_set))
10848 			ieee80211_link_info_change_notify(sdata, link,
10849 							  BSS_CHANGED_QOS);
10850 	}
10851 }
10852 
10853 void ieee80211_process_epcs_ena_resp(struct ieee80211_sub_if_data *sdata,
10854 				     struct ieee80211_mgmt *mgmt, size_t len)
10855 {
10856 	struct ieee802_11_elems *elems __free(kfree) = NULL;
10857 	size_t ies_len;
10858 	u16 status_code;
10859 	u8 *pos, dialog_token;
10860 
10861 	if (!ieee80211_mgd_epcs_supp(sdata))
10862 		return;
10863 
10864 	/* Handle dialog token and status code */
10865 	pos = mgmt->u.action.u.epcs.variable;
10866 	dialog_token = *pos;
10867 	status_code = get_unaligned_le16(pos + 1);
10868 
10869 	/* An EPCS enable response with dialog token == 0 is an unsolicited
10870 	 * notification from the AP MLD. In such a case, EPCS should already be
10871 	 * enabled and status must be success
10872 	 */
10873 	if (!dialog_token &&
10874 	    (!sdata->u.mgd.epcs.enabled ||
10875 	     status_code != WLAN_STATUS_SUCCESS))
10876 		return;
10877 
10878 	if (sdata->u.mgd.epcs.dialog_token != dialog_token)
10879 		return;
10880 
10881 	sdata->u.mgd.epcs.dialog_token = 0;
10882 
10883 	if (status_code != WLAN_STATUS_SUCCESS)
10884 		return;
10885 
10886 	pos += IEEE80211_EPCS_ENA_RESP_BODY_LEN;
10887 	ies_len = len - offsetof(struct ieee80211_mgmt,
10888 				 u.action.u.epcs.variable) -
10889 		IEEE80211_EPCS_ENA_RESP_BODY_LEN;
10890 
10891 	elems = ieee802_11_parse_elems(pos, ies_len, true, NULL);
10892 	if (!elems)
10893 		return;
10894 
10895 	ieee80211_ml_epcs(sdata, elems);
10896 	ieee80211_epcs_changed(sdata, true);
10897 }
10898 
10899 void ieee80211_process_epcs_teardown(struct ieee80211_sub_if_data *sdata,
10900 				     struct ieee80211_mgmt *mgmt, size_t len)
10901 {
10902 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
10903 	    !sdata->u.mgd.epcs.enabled)
10904 		return;
10905 
10906 	ieee80211_epcs_teardown(sdata);
10907 	ieee80211_epcs_changed(sdata, false);
10908 }
10909