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