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