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