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