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