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