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