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