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