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