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