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