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