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