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