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