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