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