xref: /linux/net/mac80211/mlme.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
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 		.was_assoc = true,
3525 		.link_id = ffs(sdata->vif.active_links) - 1,
3526 	};
3527 
3528 	lockdep_assert_wiphy(local->hw.wiphy);
3529 
3530 	if (WARN_ON_ONCE(tx && !frame_buf))
3531 		return;
3532 
3533 	if (WARN_ON(!ifmgd->associated))
3534 		return;
3535 
3536 	ieee80211_stop_poll(sdata);
3537 
3538 	ifmgd->associated = false;
3539 
3540 	/* other links will be destroyed */
3541 	sdata->deflink.conf->bss = NULL;
3542 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
3543 
3544 	netif_carrier_off(sdata->dev);
3545 
3546 	/*
3547 	 * if we want to get out of ps before disassoc (why?) we have
3548 	 * to do it before sending disassoc, as otherwise the null-packet
3549 	 * won't be valid.
3550 	 */
3551 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3552 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3553 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
3554 	}
3555 	local->ps_sdata = NULL;
3556 
3557 	/* disable per-vif ps */
3558 	ieee80211_recalc_ps_vif(sdata);
3559 
3560 	/* make sure ongoing transmission finishes */
3561 	synchronize_net();
3562 
3563 	/*
3564 	 * drop any frame before deauth/disassoc, this can be data or
3565 	 * management frame. Since we are disconnecting, we should not
3566 	 * insist sending these frames which can take time and delay
3567 	 * the disconnection and possible the roaming.
3568 	 */
3569 	if (tx)
3570 		ieee80211_flush_queues(local, sdata, true);
3571 
3572 	/* deauthenticate/disassociate now */
3573 	if (tx || frame_buf) {
3574 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
3575 
3576 		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
3577 					       sdata->vif.cfg.ap_addr, stype,
3578 					       reason, tx, frame_buf);
3579 	}
3580 
3581 	/* flush out frame - make sure the deauth was actually sent */
3582 	if (tx)
3583 		ieee80211_flush_queues(local, sdata, false);
3584 
3585 	drv_mgd_complete_tx(sdata->local, sdata, &info);
3586 
3587 	/* clear AP addr only after building the needed mgmt frames */
3588 	eth_zero_addr(sdata->deflink.u.mgd.bssid);
3589 	eth_zero_addr(sdata->vif.cfg.ap_addr);
3590 
3591 	sdata->vif.cfg.ssid_len = 0;
3592 
3593 	/* remove AP and TDLS peers */
3594 	sta_info_flush(sdata, -1);
3595 
3596 	/* finally reset all BSS / config parameters */
3597 	if (!ieee80211_vif_is_mld(&sdata->vif))
3598 		changed |= ieee80211_reset_erp_info(sdata);
3599 
3600 	ieee80211_led_assoc(local, 0);
3601 	changed |= BSS_CHANGED_ASSOC;
3602 	sdata->vif.cfg.assoc = false;
3603 
3604 	sdata->deflink.u.mgd.p2p_noa_index = -1;
3605 	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
3606 	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
3607 
3608 	/* on the next assoc, re-program HT/VHT parameters */
3609 	memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
3610 	memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
3611 	memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
3612 	memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
3613 
3614 	/*
3615 	 * reset MU-MIMO ownership and group data in default link,
3616 	 * if used, other links are destroyed
3617 	 */
3618 	memset(sdata->vif.bss_conf.mu_group.membership, 0,
3619 	       sizeof(sdata->vif.bss_conf.mu_group.membership));
3620 	memset(sdata->vif.bss_conf.mu_group.position, 0,
3621 	       sizeof(sdata->vif.bss_conf.mu_group.position));
3622 	if (!ieee80211_vif_is_mld(&sdata->vif))
3623 		changed |= BSS_CHANGED_MU_GROUPS;
3624 	sdata->vif.bss_conf.mu_mimo_owner = false;
3625 
3626 	sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
3627 
3628 	del_timer_sync(&local->dynamic_ps_timer);
3629 	wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
3630 
3631 	/* Disable ARP filtering */
3632 	if (sdata->vif.cfg.arp_addr_cnt)
3633 		changed |= BSS_CHANGED_ARP_FILTER;
3634 
3635 	sdata->vif.bss_conf.qos = false;
3636 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
3637 		changed |= BSS_CHANGED_QOS;
3638 		/* The BSSID (not really interesting) and HT changed */
3639 		changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
3640 		ieee80211_bss_info_change_notify(sdata, changed);
3641 	} else {
3642 		ieee80211_vif_cfg_change_notify(sdata, changed);
3643 	}
3644 
3645 	/* disassociated - set to defaults now */
3646 	ieee80211_set_wmm_default(&sdata->deflink, false, false);
3647 
3648 	del_timer_sync(&sdata->u.mgd.conn_mon_timer);
3649 	del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
3650 	del_timer_sync(&sdata->u.mgd.timer);
3651 
3652 	sdata->vif.bss_conf.dtim_period = 0;
3653 	sdata->vif.bss_conf.beacon_rate = NULL;
3654 
3655 	sdata->deflink.u.mgd.have_beacon = false;
3656 	sdata->deflink.u.mgd.tracking_signal_avg = false;
3657 	sdata->deflink.u.mgd.disable_wmm_tracking = false;
3658 
3659 	ifmgd->flags = 0;
3660 
3661 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
3662 		struct ieee80211_link_data *link;
3663 
3664 		link = sdata_dereference(sdata->link[link_id], sdata);
3665 		if (!link)
3666 			continue;
3667 		ieee80211_link_release_channel(link);
3668 	}
3669 
3670 	sdata->vif.bss_conf.csa_active = false;
3671 	sdata->deflink.u.mgd.csa.blocked_tx = false;
3672 	sdata->deflink.u.mgd.csa.waiting_bcn = false;
3673 	sdata->deflink.u.mgd.csa.ignored_same_chan = false;
3674 	ieee80211_vif_unblock_queues_csa(sdata);
3675 
3676 	/* existing TX TSPEC sessions no longer exist */
3677 	memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
3678 	wiphy_delayed_work_cancel(local->hw.wiphy, &ifmgd->tx_tspec_wk);
3679 
3680 	sdata->vif.bss_conf.power_type = IEEE80211_REG_UNSET_AP;
3681 	sdata->vif.bss_conf.pwr_reduction = 0;
3682 	ieee80211_clear_tpe(&sdata->vif.bss_conf.tpe);
3683 
3684 	sdata->vif.cfg.eml_cap = 0;
3685 	sdata->vif.cfg.eml_med_sync_delay = 0;
3686 	sdata->vif.cfg.mld_capa_op = 0;
3687 
3688 	memset(&sdata->u.mgd.ttlm_info, 0,
3689 	       sizeof(sdata->u.mgd.ttlm_info));
3690 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy, &ifmgd->ttlm_work);
3691 
3692 	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
3693 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
3694 				  &ifmgd->neg_ttlm_timeout_work);
3695 
3696 	sdata->u.mgd.removed_links = 0;
3697 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
3698 				  &sdata->u.mgd.ml_reconf_work);
3699 
3700 	wiphy_work_cancel(sdata->local->hw.wiphy,
3701 			  &ifmgd->teardown_ttlm_work);
3702 
3703 	ieee80211_vif_set_links(sdata, 0, 0);
3704 
3705 	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
3706 }
3707 
3708 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
3709 {
3710 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3711 	struct ieee80211_local *local = sdata->local;
3712 
3713 	lockdep_assert_wiphy(local->hw.wiphy);
3714 
3715 	if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
3716 		return;
3717 
3718 	__ieee80211_stop_poll(sdata);
3719 
3720 	ieee80211_recalc_ps(local);
3721 
3722 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
3723 		return;
3724 
3725 	/*
3726 	 * We've received a probe response, but are not sure whether
3727 	 * we have or will be receiving any beacons or data, so let's
3728 	 * schedule the timers again, just in case.
3729 	 */
3730 	ieee80211_sta_reset_beacon_monitor(sdata);
3731 
3732 	mod_timer(&ifmgd->conn_mon_timer,
3733 		  round_jiffies_up(jiffies +
3734 				   IEEE80211_CONNECTION_IDLE_TIME));
3735 }
3736 
3737 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
3738 					   struct ieee80211_hdr *hdr,
3739 					   u16 tx_time)
3740 {
3741 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3742 	u16 tid;
3743 	int ac;
3744 	struct ieee80211_sta_tx_tspec *tx_tspec;
3745 	unsigned long now = jiffies;
3746 
3747 	if (!ieee80211_is_data_qos(hdr->frame_control))
3748 		return;
3749 
3750 	tid = ieee80211_get_tid(hdr);
3751 	ac = ieee80211_ac_from_tid(tid);
3752 	tx_tspec = &ifmgd->tx_tspec[ac];
3753 
3754 	if (likely(!tx_tspec->admitted_time))
3755 		return;
3756 
3757 	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
3758 		tx_tspec->consumed_tx_time = 0;
3759 		tx_tspec->time_slice_start = now;
3760 
3761 		if (tx_tspec->downgraded) {
3762 			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
3763 			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
3764 						 &ifmgd->tx_tspec_wk, 0);
3765 		}
3766 	}
3767 
3768 	if (tx_tspec->downgraded)
3769 		return;
3770 
3771 	tx_tspec->consumed_tx_time += tx_time;
3772 
3773 	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
3774 		tx_tspec->downgraded = true;
3775 		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
3776 		wiphy_delayed_work_queue(sdata->local->hw.wiphy,
3777 					 &ifmgd->tx_tspec_wk, 0);
3778 	}
3779 }
3780 
3781 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
3782 			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
3783 {
3784 	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
3785 
3786 	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
3787 	    !sdata->u.mgd.probe_send_count)
3788 		return;
3789 
3790 	if (ack)
3791 		sdata->u.mgd.probe_send_count = 0;
3792 	else
3793 		sdata->u.mgd.nullfunc_failed = true;
3794 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
3795 }
3796 
3797 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
3798 					  const u8 *src, const u8 *dst,
3799 					  const u8 *ssid, size_t ssid_len,
3800 					  struct ieee80211_channel *channel)
3801 {
3802 	struct sk_buff *skb;
3803 
3804 	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
3805 					ssid, ssid_len, NULL, 0,
3806 					IEEE80211_PROBE_FLAG_DIRECTED);
3807 	if (skb)
3808 		ieee80211_tx_skb(sdata, skb);
3809 }
3810 
3811 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
3812 {
3813 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3814 	u8 *dst = sdata->vif.cfg.ap_addr;
3815 	u8 unicast_limit = max(1, max_probe_tries - 3);
3816 	struct sta_info *sta;
3817 
3818 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3819 
3820 	if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif)))
3821 		return;
3822 
3823 	/*
3824 	 * Try sending broadcast probe requests for the last three
3825 	 * probe requests after the first ones failed since some
3826 	 * buggy APs only support broadcast probe requests.
3827 	 */
3828 	if (ifmgd->probe_send_count >= unicast_limit)
3829 		dst = NULL;
3830 
3831 	/*
3832 	 * When the hardware reports an accurate Tx ACK status, it's
3833 	 * better to send a nullfunc frame instead of a probe request,
3834 	 * as it will kick us off the AP quickly if we aren't associated
3835 	 * anymore. The timeout will be reset if the frame is ACKed by
3836 	 * the AP.
3837 	 */
3838 	ifmgd->probe_send_count++;
3839 
3840 	if (dst) {
3841 		sta = sta_info_get(sdata, dst);
3842 		if (!WARN_ON(!sta))
3843 			ieee80211_check_fast_rx(sta);
3844 	}
3845 
3846 	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
3847 		ifmgd->nullfunc_failed = false;
3848 		ieee80211_send_nullfunc(sdata->local, sdata, false);
3849 	} else {
3850 		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
3851 					      sdata->vif.cfg.ssid,
3852 					      sdata->vif.cfg.ssid_len,
3853 					      sdata->deflink.conf->bss->channel);
3854 	}
3855 
3856 	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
3857 	run_again(sdata, ifmgd->probe_timeout);
3858 }
3859 
3860 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
3861 				   bool beacon)
3862 {
3863 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3864 	bool already = false;
3865 
3866 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3867 
3868 	if (WARN_ON_ONCE(ieee80211_vif_is_mld(&sdata->vif)))
3869 		return;
3870 
3871 	if (!ieee80211_sdata_running(sdata))
3872 		return;
3873 
3874 	if (!ifmgd->associated)
3875 		return;
3876 
3877 	if (sdata->local->tmp_channel || sdata->local->scanning)
3878 		return;
3879 
3880 	if (sdata->local->suspending) {
3881 		/* reschedule after resume */
3882 		ieee80211_reset_ap_probe(sdata);
3883 		return;
3884 	}
3885 
3886 	if (beacon) {
3887 		mlme_dbg_ratelimited(sdata,
3888 				     "detected beacon loss from AP (missed %d beacons) - probing\n",
3889 				     beacon_loss_count);
3890 
3891 		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
3892 	}
3893 
3894 	/*
3895 	 * The driver/our work has already reported this event or the
3896 	 * connection monitoring has kicked in and we have already sent
3897 	 * a probe request. Or maybe the AP died and the driver keeps
3898 	 * reporting until we disassociate...
3899 	 *
3900 	 * In either case we have to ignore the current call to this
3901 	 * function (except for setting the correct probe reason bit)
3902 	 * because otherwise we would reset the timer every time and
3903 	 * never check whether we received a probe response!
3904 	 */
3905 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
3906 		already = true;
3907 
3908 	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
3909 
3910 	if (already)
3911 		return;
3912 
3913 	ieee80211_recalc_ps(sdata->local);
3914 
3915 	ifmgd->probe_send_count = 0;
3916 	ieee80211_mgd_probe_ap_send(sdata);
3917 }
3918 
3919 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
3920 					  struct ieee80211_vif *vif)
3921 {
3922 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3923 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3924 	struct cfg80211_bss *cbss;
3925 	struct sk_buff *skb;
3926 	const struct element *ssid;
3927 	int ssid_len;
3928 
3929 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3930 
3931 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
3932 		    ieee80211_vif_is_mld(&sdata->vif)))
3933 		return NULL;
3934 
3935 	if (ifmgd->associated)
3936 		cbss = sdata->deflink.conf->bss;
3937 	else if (ifmgd->auth_data)
3938 		cbss = ifmgd->auth_data->bss;
3939 	else if (ifmgd->assoc_data && ifmgd->assoc_data->link[0].bss)
3940 		cbss = ifmgd->assoc_data->link[0].bss;
3941 	else
3942 		return NULL;
3943 
3944 	rcu_read_lock();
3945 	ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
3946 	if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
3947 		      "invalid SSID element (len=%d)",
3948 		      ssid ? ssid->datalen : -1))
3949 		ssid_len = 0;
3950 	else
3951 		ssid_len = ssid->datalen;
3952 
3953 	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
3954 					(u32) -1, cbss->channel,
3955 					ssid->data, ssid_len,
3956 					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
3957 	rcu_read_unlock();
3958 
3959 	return skb;
3960 }
3961 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
3962 
3963 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
3964 					const u8 *buf, size_t len, bool tx,
3965 					u16 reason, bool reconnect)
3966 {
3967 	struct ieee80211_event event = {
3968 		.type = MLME_EVENT,
3969 		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
3970 		.u.mlme.reason = reason,
3971 	};
3972 
3973 	if (tx)
3974 		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
3975 	else
3976 		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
3977 
3978 	drv_event_callback(sdata->local, sdata, &event);
3979 }
3980 
3981 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
3982 {
3983 	struct ieee80211_local *local = sdata->local;
3984 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3985 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3986 	bool tx = false;
3987 
3988 	lockdep_assert_wiphy(local->hw.wiphy);
3989 
3990 	if (!ifmgd->associated)
3991 		return;
3992 
3993 	/* only transmit if we have a link that makes that worthwhile */
3994 	for (unsigned int link_id = 0;
3995 	     link_id < ARRAY_SIZE(sdata->link);
3996 	     link_id++) {
3997 		struct ieee80211_link_data *link;
3998 
3999 		if (!ieee80211_vif_link_active(&sdata->vif, link_id))
4000 			continue;
4001 
4002 		link = sdata_dereference(sdata->link[link_id], sdata);
4003 		if (WARN_ON_ONCE(!link))
4004 			continue;
4005 
4006 		if (link->u.mgd.csa.blocked_tx)
4007 			continue;
4008 
4009 		tx = true;
4010 		break;
4011 	}
4012 
4013 	if (!ifmgd->driver_disconnect) {
4014 		unsigned int link_id;
4015 
4016 		/*
4017 		 * AP is probably out of range (or not reachable for another
4018 		 * reason) so remove the bss structs for that AP. In the case
4019 		 * of multi-link, it's not clear that all of them really are
4020 		 * out of range, but if they weren't the driver likely would
4021 		 * have switched to just have a single link active?
4022 		 */
4023 		for (link_id = 0;
4024 		     link_id < ARRAY_SIZE(sdata->link);
4025 		     link_id++) {
4026 			struct ieee80211_link_data *link;
4027 
4028 			link = sdata_dereference(sdata->link[link_id], sdata);
4029 			if (!link)
4030 				continue;
4031 			cfg80211_unlink_bss(local->hw.wiphy, link->conf->bss);
4032 			link->conf->bss = NULL;
4033 		}
4034 	}
4035 
4036 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4037 			       ifmgd->driver_disconnect ?
4038 					WLAN_REASON_DEAUTH_LEAVING :
4039 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4040 			       tx, frame_buf);
4041 	/* the other links will be destroyed */
4042 	sdata->vif.bss_conf.csa_active = false;
4043 	sdata->deflink.u.mgd.csa.waiting_bcn = false;
4044 	sdata->deflink.u.mgd.csa.blocked_tx = false;
4045 	ieee80211_vif_unblock_queues_csa(sdata);
4046 
4047 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
4048 				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4049 				    ifmgd->reconnect);
4050 	ifmgd->reconnect = false;
4051 }
4052 
4053 static void ieee80211_beacon_connection_loss_work(struct wiphy *wiphy,
4054 						  struct wiphy_work *work)
4055 {
4056 	struct ieee80211_sub_if_data *sdata =
4057 		container_of(work, struct ieee80211_sub_if_data,
4058 			     u.mgd.beacon_connection_loss_work);
4059 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4060 
4061 	if (ifmgd->connection_loss) {
4062 		sdata_info(sdata, "Connection to AP %pM lost\n",
4063 			   sdata->vif.cfg.ap_addr);
4064 		__ieee80211_disconnect(sdata);
4065 		ifmgd->connection_loss = false;
4066 	} else if (ifmgd->driver_disconnect) {
4067 		sdata_info(sdata,
4068 			   "Driver requested disconnection from AP %pM\n",
4069 			   sdata->vif.cfg.ap_addr);
4070 		__ieee80211_disconnect(sdata);
4071 		ifmgd->driver_disconnect = false;
4072 	} else {
4073 		if (ifmgd->associated)
4074 			sdata->deflink.u.mgd.beacon_loss_count++;
4075 		ieee80211_mgd_probe_ap(sdata, true);
4076 	}
4077 }
4078 
4079 static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
4080 					       struct wiphy_work *work)
4081 {
4082 	struct ieee80211_sub_if_data *sdata =
4083 		container_of(work, struct ieee80211_sub_if_data,
4084 			     u.mgd.csa_connection_drop_work);
4085 
4086 	__ieee80211_disconnect(sdata);
4087 }
4088 
4089 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
4090 {
4091 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4092 	struct ieee80211_hw *hw = &sdata->local->hw;
4093 
4094 	trace_api_beacon_loss(sdata);
4095 
4096 	sdata->u.mgd.connection_loss = false;
4097 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
4098 }
4099 EXPORT_SYMBOL(ieee80211_beacon_loss);
4100 
4101 void ieee80211_connection_loss(struct ieee80211_vif *vif)
4102 {
4103 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4104 	struct ieee80211_hw *hw = &sdata->local->hw;
4105 
4106 	trace_api_connection_loss(sdata);
4107 
4108 	sdata->u.mgd.connection_loss = true;
4109 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
4110 }
4111 EXPORT_SYMBOL(ieee80211_connection_loss);
4112 
4113 void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
4114 {
4115 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4116 	struct ieee80211_hw *hw = &sdata->local->hw;
4117 
4118 	trace_api_disconnect(sdata, reconnect);
4119 
4120 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4121 		return;
4122 
4123 	sdata->u.mgd.driver_disconnect = true;
4124 	sdata->u.mgd.reconnect = reconnect;
4125 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
4126 }
4127 EXPORT_SYMBOL(ieee80211_disconnect);
4128 
4129 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
4130 					bool assoc)
4131 {
4132 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
4133 
4134 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4135 
4136 	if (!assoc) {
4137 		/*
4138 		 * we are not authenticated yet, the only timer that could be
4139 		 * running is the timeout for the authentication response which
4140 		 * which is not relevant anymore.
4141 		 */
4142 		del_timer_sync(&sdata->u.mgd.timer);
4143 		sta_info_destroy_addr(sdata, auth_data->ap_addr);
4144 
4145 		/* other links are destroyed */
4146 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
4147 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
4148 						  BSS_CHANGED_BSSID);
4149 		sdata->u.mgd.flags = 0;
4150 
4151 		ieee80211_link_release_channel(&sdata->deflink);
4152 		ieee80211_vif_set_links(sdata, 0, 0);
4153 	}
4154 
4155 	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
4156 	kfree(auth_data);
4157 	sdata->u.mgd.auth_data = NULL;
4158 }
4159 
4160 enum assoc_status {
4161 	ASSOC_SUCCESS,
4162 	ASSOC_REJECTED,
4163 	ASSOC_TIMEOUT,
4164 	ASSOC_ABANDON,
4165 };
4166 
4167 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
4168 					 enum assoc_status status)
4169 {
4170 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4171 
4172 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4173 
4174 	if (status != ASSOC_SUCCESS) {
4175 		/*
4176 		 * we are not associated yet, the only timer that could be
4177 		 * running is the timeout for the association response which
4178 		 * which is not relevant anymore.
4179 		 */
4180 		del_timer_sync(&sdata->u.mgd.timer);
4181 		sta_info_destroy_addr(sdata, assoc_data->ap_addr);
4182 
4183 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
4184 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
4185 						  BSS_CHANGED_BSSID);
4186 		sdata->u.mgd.flags = 0;
4187 		sdata->vif.bss_conf.mu_mimo_owner = false;
4188 
4189 		if (status != ASSOC_REJECTED) {
4190 			struct cfg80211_assoc_failure data = {
4191 				.timeout = status == ASSOC_TIMEOUT,
4192 			};
4193 			int i;
4194 
4195 			BUILD_BUG_ON(ARRAY_SIZE(data.bss) !=
4196 				     ARRAY_SIZE(assoc_data->link));
4197 
4198 			for (i = 0; i < ARRAY_SIZE(data.bss); i++)
4199 				data.bss[i] = assoc_data->link[i].bss;
4200 
4201 			if (ieee80211_vif_is_mld(&sdata->vif))
4202 				data.ap_mld_addr = assoc_data->ap_addr;
4203 
4204 			cfg80211_assoc_failure(sdata->dev, &data);
4205 		}
4206 
4207 		ieee80211_link_release_channel(&sdata->deflink);
4208 		ieee80211_vif_set_links(sdata, 0, 0);
4209 	}
4210 
4211 	kfree(assoc_data);
4212 	sdata->u.mgd.assoc_data = NULL;
4213 }
4214 
4215 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
4216 				     struct ieee80211_mgmt *mgmt, size_t len)
4217 {
4218 	struct ieee80211_local *local = sdata->local;
4219 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
4220 	const struct element *challenge;
4221 	u8 *pos;
4222 	u32 tx_flags = 0;
4223 	struct ieee80211_prep_tx_info info = {
4224 		.subtype = IEEE80211_STYPE_AUTH,
4225 		.link_id = auth_data->link_id,
4226 	};
4227 
4228 	pos = mgmt->u.auth.variable;
4229 	challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
4230 				       len - (pos - (u8 *)mgmt));
4231 	if (!challenge)
4232 		return;
4233 	auth_data->expected_transaction = 4;
4234 	drv_mgd_prepare_tx(sdata->local, sdata, &info);
4235 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4236 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4237 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
4238 	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
4239 			    (void *)challenge,
4240 			    challenge->datalen + sizeof(*challenge),
4241 			    auth_data->ap_addr, auth_data->ap_addr,
4242 			    auth_data->key, auth_data->key_len,
4243 			    auth_data->key_idx, tx_flags);
4244 }
4245 
4246 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata)
4247 {
4248 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4249 	const u8 *ap_addr = ifmgd->auth_data->ap_addr;
4250 	struct sta_info *sta;
4251 
4252 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4253 
4254 	sdata_info(sdata, "authenticated\n");
4255 	ifmgd->auth_data->done = true;
4256 	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
4257 	ifmgd->auth_data->timeout_started = true;
4258 	run_again(sdata, ifmgd->auth_data->timeout);
4259 
4260 	/* move station state to auth */
4261 	sta = sta_info_get(sdata, ap_addr);
4262 	if (!sta) {
4263 		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, ap_addr);
4264 		return false;
4265 	}
4266 	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
4267 		sdata_info(sdata, "failed moving %pM to auth\n", ap_addr);
4268 		return false;
4269 	}
4270 
4271 	return true;
4272 }
4273 
4274 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
4275 				   struct ieee80211_mgmt *mgmt, size_t len)
4276 {
4277 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4278 	u16 auth_alg, auth_transaction, status_code;
4279 	struct ieee80211_event event = {
4280 		.type = MLME_EVENT,
4281 		.u.mlme.data = AUTH_EVENT,
4282 	};
4283 	struct ieee80211_prep_tx_info info = {
4284 		.subtype = IEEE80211_STYPE_AUTH,
4285 	};
4286 
4287 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4288 
4289 	if (len < 24 + 6)
4290 		return;
4291 
4292 	if (!ifmgd->auth_data || ifmgd->auth_data->done)
4293 		return;
4294 
4295 	if (!ether_addr_equal(ifmgd->auth_data->ap_addr, mgmt->bssid))
4296 		return;
4297 
4298 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
4299 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
4300 	status_code = le16_to_cpu(mgmt->u.auth.status_code);
4301 
4302 	if (auth_alg != ifmgd->auth_data->algorithm ||
4303 	    (auth_alg != WLAN_AUTH_SAE &&
4304 	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
4305 	    (auth_alg == WLAN_AUTH_SAE &&
4306 	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
4307 	      auth_transaction > 2))) {
4308 		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
4309 			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
4310 			   auth_transaction,
4311 			   ifmgd->auth_data->expected_transaction);
4312 		goto notify_driver;
4313 	}
4314 
4315 	if (status_code != WLAN_STATUS_SUCCESS) {
4316 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
4317 
4318 		if (auth_alg == WLAN_AUTH_SAE &&
4319 		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
4320 		     (auth_transaction == 1 &&
4321 		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
4322 		       status_code == WLAN_STATUS_SAE_PK)))) {
4323 			/* waiting for userspace now */
4324 			ifmgd->auth_data->waiting = true;
4325 			ifmgd->auth_data->timeout =
4326 				jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
4327 			ifmgd->auth_data->timeout_started = true;
4328 			run_again(sdata, ifmgd->auth_data->timeout);
4329 			goto notify_driver;
4330 		}
4331 
4332 		sdata_info(sdata, "%pM denied authentication (status %d)\n",
4333 			   mgmt->sa, status_code);
4334 		ieee80211_destroy_auth_data(sdata, false);
4335 		event.u.mlme.status = MLME_DENIED;
4336 		event.u.mlme.reason = status_code;
4337 		drv_event_callback(sdata->local, sdata, &event);
4338 		goto notify_driver;
4339 	}
4340 
4341 	switch (ifmgd->auth_data->algorithm) {
4342 	case WLAN_AUTH_OPEN:
4343 	case WLAN_AUTH_LEAP:
4344 	case WLAN_AUTH_FT:
4345 	case WLAN_AUTH_SAE:
4346 	case WLAN_AUTH_FILS_SK:
4347 	case WLAN_AUTH_FILS_SK_PFS:
4348 	case WLAN_AUTH_FILS_PK:
4349 		break;
4350 	case WLAN_AUTH_SHARED_KEY:
4351 		if (ifmgd->auth_data->expected_transaction != 4) {
4352 			ieee80211_auth_challenge(sdata, mgmt, len);
4353 			/* need another frame */
4354 			return;
4355 		}
4356 		break;
4357 	default:
4358 		WARN_ONCE(1, "invalid auth alg %d",
4359 			  ifmgd->auth_data->algorithm);
4360 		goto notify_driver;
4361 	}
4362 
4363 	event.u.mlme.status = MLME_SUCCESS;
4364 	info.success = 1;
4365 	drv_event_callback(sdata->local, sdata, &event);
4366 	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
4367 	    (auth_transaction == 2 &&
4368 	     ifmgd->auth_data->expected_transaction == 2)) {
4369 		if (!ieee80211_mark_sta_auth(sdata))
4370 			return; /* ignore frame -- wait for timeout */
4371 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
4372 		   auth_transaction == 2) {
4373 		sdata_info(sdata, "SAE peer confirmed\n");
4374 		ifmgd->auth_data->peer_confirmed = true;
4375 	}
4376 
4377 	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
4378 notify_driver:
4379 	drv_mgd_complete_tx(sdata->local, sdata, &info);
4380 }
4381 
4382 #define case_WLAN(type) \
4383 	case WLAN_REASON_##type: return #type
4384 
4385 const char *ieee80211_get_reason_code_string(u16 reason_code)
4386 {
4387 	switch (reason_code) {
4388 	case_WLAN(UNSPECIFIED);
4389 	case_WLAN(PREV_AUTH_NOT_VALID);
4390 	case_WLAN(DEAUTH_LEAVING);
4391 	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
4392 	case_WLAN(DISASSOC_AP_BUSY);
4393 	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
4394 	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
4395 	case_WLAN(DISASSOC_STA_HAS_LEFT);
4396 	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
4397 	case_WLAN(DISASSOC_BAD_POWER);
4398 	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
4399 	case_WLAN(INVALID_IE);
4400 	case_WLAN(MIC_FAILURE);
4401 	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
4402 	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
4403 	case_WLAN(IE_DIFFERENT);
4404 	case_WLAN(INVALID_GROUP_CIPHER);
4405 	case_WLAN(INVALID_PAIRWISE_CIPHER);
4406 	case_WLAN(INVALID_AKMP);
4407 	case_WLAN(UNSUPP_RSN_VERSION);
4408 	case_WLAN(INVALID_RSN_IE_CAP);
4409 	case_WLAN(IEEE8021X_FAILED);
4410 	case_WLAN(CIPHER_SUITE_REJECTED);
4411 	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
4412 	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
4413 	case_WLAN(DISASSOC_LOW_ACK);
4414 	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
4415 	case_WLAN(QSTA_LEAVE_QBSS);
4416 	case_WLAN(QSTA_NOT_USE);
4417 	case_WLAN(QSTA_REQUIRE_SETUP);
4418 	case_WLAN(QSTA_TIMEOUT);
4419 	case_WLAN(QSTA_CIPHER_NOT_SUPP);
4420 	case_WLAN(MESH_PEER_CANCELED);
4421 	case_WLAN(MESH_MAX_PEERS);
4422 	case_WLAN(MESH_CONFIG);
4423 	case_WLAN(MESH_CLOSE);
4424 	case_WLAN(MESH_MAX_RETRIES);
4425 	case_WLAN(MESH_CONFIRM_TIMEOUT);
4426 	case_WLAN(MESH_INVALID_GTK);
4427 	case_WLAN(MESH_INCONSISTENT_PARAM);
4428 	case_WLAN(MESH_INVALID_SECURITY);
4429 	case_WLAN(MESH_PATH_ERROR);
4430 	case_WLAN(MESH_PATH_NOFORWARD);
4431 	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
4432 	case_WLAN(MAC_EXISTS_IN_MBSS);
4433 	case_WLAN(MESH_CHAN_REGULATORY);
4434 	case_WLAN(MESH_CHAN);
4435 	default: return "<unknown>";
4436 	}
4437 }
4438 
4439 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
4440 				     struct ieee80211_mgmt *mgmt, size_t len)
4441 {
4442 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4443 	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
4444 
4445 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4446 
4447 	if (len < 24 + 2)
4448 		return;
4449 
4450 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
4451 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
4452 		return;
4453 	}
4454 
4455 	if (ifmgd->associated &&
4456 	    ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
4457 		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
4458 			   sdata->vif.cfg.ap_addr, reason_code,
4459 			   ieee80211_get_reason_code_string(reason_code));
4460 
4461 		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
4462 
4463 		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
4464 					    reason_code, false);
4465 		return;
4466 	}
4467 
4468 	if (ifmgd->assoc_data &&
4469 	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->ap_addr)) {
4470 		sdata_info(sdata,
4471 			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
4472 			   ifmgd->assoc_data->ap_addr, reason_code,
4473 			   ieee80211_get_reason_code_string(reason_code));
4474 
4475 		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
4476 
4477 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
4478 		return;
4479 	}
4480 }
4481 
4482 
4483 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
4484 				       struct ieee80211_mgmt *mgmt, size_t len)
4485 {
4486 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4487 	u16 reason_code;
4488 
4489 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4490 
4491 	if (len < 24 + 2)
4492 		return;
4493 
4494 	if (!ifmgd->associated ||
4495 	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
4496 		return;
4497 
4498 	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
4499 
4500 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
4501 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
4502 		return;
4503 	}
4504 
4505 	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
4506 		   sdata->vif.cfg.ap_addr, reason_code,
4507 		   ieee80211_get_reason_code_string(reason_code));
4508 
4509 	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
4510 
4511 	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
4512 				    false);
4513 }
4514 
4515 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
4516 				u8 *supp_rates, unsigned int supp_rates_len,
4517 				u32 *rates, u32 *basic_rates,
4518 				bool *have_higher_than_11mbit,
4519 				int *min_rate, int *min_rate_index)
4520 {
4521 	int i, j;
4522 
4523 	for (i = 0; i < supp_rates_len; i++) {
4524 		int rate = supp_rates[i] & 0x7f;
4525 		bool is_basic = !!(supp_rates[i] & 0x80);
4526 
4527 		if ((rate * 5) > 110)
4528 			*have_higher_than_11mbit = true;
4529 
4530 		/*
4531 		 * Skip HT, VHT, HE, EHT and SAE H2E only BSS membership
4532 		 * selectors since they're not rates.
4533 		 *
4534 		 * Note: Even though the membership selector and the basic
4535 		 *	 rate flag share the same bit, they are not exactly
4536 		 *	 the same.
4537 		 */
4538 		if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
4539 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
4540 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY) ||
4541 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_EHT_PHY) ||
4542 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E))
4543 			continue;
4544 
4545 		for (j = 0; j < sband->n_bitrates; j++) {
4546 			struct ieee80211_rate *br;
4547 			int brate;
4548 
4549 			br = &sband->bitrates[j];
4550 
4551 			brate = DIV_ROUND_UP(br->bitrate, 5);
4552 			if (brate == rate) {
4553 				*rates |= BIT(j);
4554 				if (is_basic)
4555 					*basic_rates |= BIT(j);
4556 				if ((rate * 5) < *min_rate) {
4557 					*min_rate = rate * 5;
4558 					*min_rate_index = j;
4559 				}
4560 				break;
4561 			}
4562 		}
4563 	}
4564 }
4565 
4566 static bool ieee80211_twt_req_supported(struct ieee80211_sub_if_data *sdata,
4567 					struct ieee80211_supported_band *sband,
4568 					const struct link_sta_info *link_sta,
4569 					const struct ieee802_11_elems *elems)
4570 {
4571 	const struct ieee80211_sta_he_cap *own_he_cap =
4572 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
4573 
4574 	if (elems->ext_capab_len < 10)
4575 		return false;
4576 
4577 	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
4578 		return false;
4579 
4580 	return link_sta->pub->he_cap.he_cap_elem.mac_cap_info[0] &
4581 		IEEE80211_HE_MAC_CAP0_TWT_RES &&
4582 		own_he_cap &&
4583 		(own_he_cap->he_cap_elem.mac_cap_info[0] &
4584 			IEEE80211_HE_MAC_CAP0_TWT_REQ);
4585 }
4586 
4587 static u64 ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
4588 				    struct ieee80211_supported_band *sband,
4589 				    struct ieee80211_link_data *link,
4590 				    struct link_sta_info *link_sta,
4591 				    struct ieee802_11_elems *elems)
4592 {
4593 	bool twt = ieee80211_twt_req_supported(sdata, sband, link_sta, elems);
4594 
4595 	if (link->conf->twt_requester != twt) {
4596 		link->conf->twt_requester = twt;
4597 		return BSS_CHANGED_TWT;
4598 	}
4599 	return 0;
4600 }
4601 
4602 static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
4603 					struct ieee80211_bss_conf *bss_conf,
4604 					struct ieee80211_supported_band *sband,
4605 					struct link_sta_info *link_sta)
4606 {
4607 	const struct ieee80211_sta_he_cap *own_he_cap =
4608 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
4609 
4610 	return bss_conf->he_support &&
4611 		(link_sta->pub->he_cap.he_cap_elem.mac_cap_info[2] &
4612 			IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
4613 		own_he_cap &&
4614 		(own_he_cap->he_cap_elem.mac_cap_info[2] &
4615 			IEEE80211_HE_MAC_CAP2_BCAST_TWT);
4616 }
4617 
4618 static bool ieee80211_assoc_config_link(struct ieee80211_link_data *link,
4619 					struct link_sta_info *link_sta,
4620 					struct cfg80211_bss *cbss,
4621 					struct ieee80211_mgmt *mgmt,
4622 					const u8 *elem_start,
4623 					unsigned int elem_len,
4624 					u64 *changed)
4625 {
4626 	struct ieee80211_sub_if_data *sdata = link->sdata;
4627 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4628 	struct ieee80211_bss_conf *bss_conf = link->conf;
4629 	struct ieee80211_local *local = sdata->local;
4630 	unsigned int link_id = link->link_id;
4631 	struct ieee80211_elems_parse_params parse_params = {
4632 		.mode = link->u.mgd.conn.mode,
4633 		.start = elem_start,
4634 		.len = elem_len,
4635 		.link_id = link_id == assoc_data->assoc_link_id ? -1 : link_id,
4636 		.from_ap = true,
4637 	};
4638 	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
4639 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
4640 	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
4641 	const struct cfg80211_bss_ies *bss_ies = NULL;
4642 	struct ieee80211_supported_band *sband;
4643 	struct ieee802_11_elems *elems;
4644 	const __le16 prof_bss_param_ch_present =
4645 		cpu_to_le16(IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT);
4646 	u16 capab_info;
4647 	bool ret;
4648 
4649 	elems = ieee802_11_parse_elems_full(&parse_params);
4650 	if (!elems)
4651 		return false;
4652 
4653 	if (link_id == assoc_data->assoc_link_id) {
4654 		capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
4655 
4656 		/*
4657 		 * we should not get to this flow unless the association was
4658 		 * successful, so set the status directly to success
4659 		 */
4660 		assoc_data->link[link_id].status = WLAN_STATUS_SUCCESS;
4661 		if (elems->ml_basic) {
4662 			int bss_param_ch_cnt =
4663 				ieee80211_mle_get_bss_param_ch_cnt((const void *)elems->ml_basic);
4664 
4665 			if (bss_param_ch_cnt < 0) {
4666 				ret = false;
4667 				goto out;
4668 			}
4669 			link->u.mgd.bss_param_ch_cnt = bss_param_ch_cnt;
4670 		}
4671 	} else if (elems->parse_error & IEEE80211_PARSE_ERR_DUP_NEST_ML_BASIC ||
4672 		   !elems->prof ||
4673 		   !(elems->prof->control & prof_bss_param_ch_present)) {
4674 		ret = false;
4675 		goto out;
4676 	} else {
4677 		const u8 *ptr = elems->prof->variable +
4678 				elems->prof->sta_info_len - 1;
4679 
4680 		/*
4681 		 * During parsing, we validated that these fields exist,
4682 		 * otherwise elems->prof would have been set to NULL.
4683 		 */
4684 		capab_info = get_unaligned_le16(ptr);
4685 		assoc_data->link[link_id].status = get_unaligned_le16(ptr + 2);
4686 		link->u.mgd.bss_param_ch_cnt =
4687 			ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(elems->prof);
4688 
4689 		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
4690 			link_info(link, "association response status code=%u\n",
4691 				  assoc_data->link[link_id].status);
4692 			ret = true;
4693 			goto out;
4694 		}
4695 	}
4696 
4697 	if (!is_s1g && !elems->supp_rates) {
4698 		sdata_info(sdata, "no SuppRates element in AssocResp\n");
4699 		ret = false;
4700 		goto out;
4701 	}
4702 
4703 	link->u.mgd.tdls_chan_switch_prohibited =
4704 		elems->ext_capab && elems->ext_capab_len >= 5 &&
4705 		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
4706 
4707 	/*
4708 	 * Some APs are erroneously not including some information in their
4709 	 * (re)association response frames. Try to recover by using the data
4710 	 * from the beacon or probe response. This seems to afflict mobile
4711 	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
4712 	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
4713 	 */
4714 	if (!is_6ghz &&
4715 	    ((assoc_data->wmm && !elems->wmm_param) ||
4716 	     (link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
4717 	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
4718 	     (link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
4719 	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
4720 		const struct cfg80211_bss_ies *ies;
4721 		struct ieee802_11_elems *bss_elems;
4722 
4723 		rcu_read_lock();
4724 		ies = rcu_dereference(cbss->ies);
4725 		if (ies)
4726 			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
4727 					  GFP_ATOMIC);
4728 		rcu_read_unlock();
4729 		if (!bss_ies) {
4730 			ret = false;
4731 			goto out;
4732 		}
4733 
4734 		parse_params.start = bss_ies->data;
4735 		parse_params.len = bss_ies->len;
4736 		parse_params.bss = cbss;
4737 		bss_elems = ieee802_11_parse_elems_full(&parse_params);
4738 		if (!bss_elems) {
4739 			ret = false;
4740 			goto out;
4741 		}
4742 
4743 		if (assoc_data->wmm &&
4744 		    !elems->wmm_param && bss_elems->wmm_param) {
4745 			elems->wmm_param = bss_elems->wmm_param;
4746 			sdata_info(sdata,
4747 				   "AP bug: WMM param missing from AssocResp\n");
4748 		}
4749 
4750 		/*
4751 		 * Also check if we requested HT/VHT, otherwise the AP doesn't
4752 		 * have to include the IEs in the (re)association response.
4753 		 */
4754 		if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
4755 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
4756 			elems->ht_cap_elem = bss_elems->ht_cap_elem;
4757 			sdata_info(sdata,
4758 				   "AP bug: HT capability missing from AssocResp\n");
4759 		}
4760 		if (!elems->ht_operation && bss_elems->ht_operation &&
4761 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
4762 			elems->ht_operation = bss_elems->ht_operation;
4763 			sdata_info(sdata,
4764 				   "AP bug: HT operation missing from AssocResp\n");
4765 		}
4766 		if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
4767 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
4768 			elems->vht_cap_elem = bss_elems->vht_cap_elem;
4769 			sdata_info(sdata,
4770 				   "AP bug: VHT capa missing from AssocResp\n");
4771 		}
4772 		if (!elems->vht_operation && bss_elems->vht_operation &&
4773 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
4774 			elems->vht_operation = bss_elems->vht_operation;
4775 			sdata_info(sdata,
4776 				   "AP bug: VHT operation missing from AssocResp\n");
4777 		}
4778 
4779 		kfree(bss_elems);
4780 	}
4781 
4782 	/*
4783 	 * We previously checked these in the beacon/probe response, so
4784 	 * they should be present here. This is just a safety net.
4785 	 * Note that the ieee80211_config_bw() below would also check
4786 	 * for this (and more), but this has better error reporting.
4787 	 */
4788 	if (!is_6ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
4789 	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
4790 		sdata_info(sdata,
4791 			   "HT AP is missing WMM params or HT capability/operation\n");
4792 		ret = false;
4793 		goto out;
4794 	}
4795 
4796 	if (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
4797 	    (!elems->vht_cap_elem || !elems->vht_operation)) {
4798 		sdata_info(sdata,
4799 			   "VHT AP is missing VHT capability/operation\n");
4800 		ret = false;
4801 		goto out;
4802 	}
4803 
4804 	/* check/update if AP changed anything in assoc response vs. scan */
4805 	if (ieee80211_config_bw(link, elems,
4806 				link_id == assoc_data->assoc_link_id,
4807 				changed)) {
4808 		ret = false;
4809 		goto out;
4810 	}
4811 
4812 	if (WARN_ON(!link->conf->chanreq.oper.chan)) {
4813 		ret = false;
4814 		goto out;
4815 	}
4816 	sband = local->hw.wiphy->bands[link->conf->chanreq.oper.chan->band];
4817 
4818 	/* Set up internal HT/VHT capabilities */
4819 	if (elems->ht_cap_elem && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT)
4820 		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
4821 						  elems->ht_cap_elem,
4822 						  link_sta);
4823 
4824 	if (elems->vht_cap_elem &&
4825 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
4826 		const struct ieee80211_vht_cap *bss_vht_cap = NULL;
4827 		const struct cfg80211_bss_ies *ies;
4828 
4829 		/*
4830 		 * Cisco AP module 9115 with FW 17.3 has a bug and sends a
4831 		 * too large maximum MPDU length in the association response
4832 		 * (indicating 12k) that it cannot actually process ...
4833 		 * Work around that.
4834 		 */
4835 		rcu_read_lock();
4836 		ies = rcu_dereference(cbss->ies);
4837 		if (ies) {
4838 			const struct element *elem;
4839 
4840 			elem = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY,
4841 						  ies->data, ies->len);
4842 			if (elem && elem->datalen >= sizeof(*bss_vht_cap))
4843 				bss_vht_cap = (const void *)elem->data;
4844 		}
4845 
4846 		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
4847 						    elems->vht_cap_elem,
4848 						    bss_vht_cap, link_sta);
4849 		rcu_read_unlock();
4850 	}
4851 
4852 	if (elems->he_operation &&
4853 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE &&
4854 	    elems->he_cap) {
4855 		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
4856 						  elems->he_cap,
4857 						  elems->he_cap_len,
4858 						  elems->he_6ghz_capa,
4859 						  link_sta);
4860 
4861 		bss_conf->he_support = link_sta->pub->he_cap.has_he;
4862 		if (elems->rsnx && elems->rsnx_len &&
4863 		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
4864 		    wiphy_ext_feature_isset(local->hw.wiphy,
4865 					    NL80211_EXT_FEATURE_PROTECTED_TWT))
4866 			bss_conf->twt_protected = true;
4867 		else
4868 			bss_conf->twt_protected = false;
4869 
4870 		*changed |= ieee80211_recalc_twt_req(sdata, sband, link,
4871 						     link_sta, elems);
4872 
4873 		if (elems->eht_operation && elems->eht_cap &&
4874 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_EHT) {
4875 			ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband,
4876 							    elems->he_cap,
4877 							    elems->he_cap_len,
4878 							    elems->eht_cap,
4879 							    elems->eht_cap_len,
4880 							    link_sta);
4881 
4882 			bss_conf->eht_support = link_sta->pub->eht_cap.has_eht;
4883 		} else {
4884 			bss_conf->eht_support = false;
4885 		}
4886 	} else {
4887 		bss_conf->he_support = false;
4888 		bss_conf->twt_requester = false;
4889 		bss_conf->twt_protected = false;
4890 		bss_conf->eht_support = false;
4891 	}
4892 
4893 	bss_conf->twt_broadcast =
4894 		ieee80211_twt_bcast_support(sdata, bss_conf, sband, link_sta);
4895 
4896 	if (bss_conf->he_support) {
4897 		bss_conf->he_bss_color.color =
4898 			le32_get_bits(elems->he_operation->he_oper_params,
4899 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
4900 		bss_conf->he_bss_color.partial =
4901 			le32_get_bits(elems->he_operation->he_oper_params,
4902 				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
4903 		bss_conf->he_bss_color.enabled =
4904 			!le32_get_bits(elems->he_operation->he_oper_params,
4905 				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
4906 
4907 		if (bss_conf->he_bss_color.enabled)
4908 			*changed |= BSS_CHANGED_HE_BSS_COLOR;
4909 
4910 		bss_conf->htc_trig_based_pkt_ext =
4911 			le32_get_bits(elems->he_operation->he_oper_params,
4912 				      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
4913 		bss_conf->frame_time_rts_th =
4914 			le32_get_bits(elems->he_operation->he_oper_params,
4915 				      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
4916 
4917 		bss_conf->uora_exists = !!elems->uora_element;
4918 		if (elems->uora_element)
4919 			bss_conf->uora_ocw_range = elems->uora_element[0];
4920 
4921 		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
4922 		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
4923 		/* TODO: OPEN: what happens if BSS color disable is set? */
4924 	}
4925 
4926 	if (cbss->transmitted_bss) {
4927 		bss_conf->nontransmitted = true;
4928 		ether_addr_copy(bss_conf->transmitter_bssid,
4929 				cbss->transmitted_bss->bssid);
4930 		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
4931 		bss_conf->bssid_index = cbss->bssid_index;
4932 	}
4933 
4934 	/*
4935 	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
4936 	 * in their association response, so ignore that data for our own
4937 	 * configuration. If it changed since the last beacon, we'll get the
4938 	 * next beacon and update then.
4939 	 */
4940 
4941 	/*
4942 	 * If an operating mode notification IE is present, override the
4943 	 * NSS calculation (that would be done in rate_control_rate_init())
4944 	 * and use the # of streams from that element.
4945 	 */
4946 	if (elems->opmode_notif &&
4947 	    !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
4948 		u8 nss;
4949 
4950 		nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
4951 		nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
4952 		nss += 1;
4953 		link_sta->pub->rx_nss = nss;
4954 	}
4955 
4956 	/*
4957 	 * Always handle WMM once after association regardless
4958 	 * of the first value the AP uses. Setting -1 here has
4959 	 * that effect because the AP values is an unsigned
4960 	 * 4-bit value.
4961 	 */
4962 	link->u.mgd.wmm_last_param_set = -1;
4963 	link->u.mgd.mu_edca_last_param_set = -1;
4964 
4965 	if (link->u.mgd.disable_wmm_tracking) {
4966 		ieee80211_set_wmm_default(link, false, false);
4967 	} else if (!ieee80211_sta_wmm_params(local, link, elems->wmm_param,
4968 					     elems->wmm_param_len,
4969 					     elems->mu_edca_param_set)) {
4970 		/* still enable QoS since we might have HT/VHT */
4971 		ieee80211_set_wmm_default(link, false, true);
4972 		/* disable WMM tracking in this case to disable
4973 		 * tracking WMM parameter changes in the beacon if
4974 		 * the parameters weren't actually valid. Doing so
4975 		 * avoids changing parameters very strangely when
4976 		 * the AP is going back and forth between valid and
4977 		 * invalid parameters.
4978 		 */
4979 		link->u.mgd.disable_wmm_tracking = true;
4980 	}
4981 
4982 	if (elems->max_idle_period_ie) {
4983 		bss_conf->max_idle_period =
4984 			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
4985 		bss_conf->protected_keep_alive =
4986 			!!(elems->max_idle_period_ie->idle_options &
4987 			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
4988 		*changed |= BSS_CHANGED_KEEP_ALIVE;
4989 	} else {
4990 		bss_conf->max_idle_period = 0;
4991 		bss_conf->protected_keep_alive = false;
4992 	}
4993 
4994 	/* set assoc capability (AID was already set earlier),
4995 	 * ieee80211_set_associated() will tell the driver */
4996 	bss_conf->assoc_capability = capab_info;
4997 
4998 	ret = true;
4999 out:
5000 	kfree(elems);
5001 	kfree(bss_ies);
5002 	return ret;
5003 }
5004 
5005 static int ieee80211_mgd_setup_link_sta(struct ieee80211_link_data *link,
5006 					struct sta_info *sta,
5007 					struct link_sta_info *link_sta,
5008 					struct cfg80211_bss *cbss)
5009 {
5010 	struct ieee80211_sub_if_data *sdata = link->sdata;
5011 	struct ieee80211_local *local = sdata->local;
5012 	struct ieee80211_bss *bss = (void *)cbss->priv;
5013 	u32 rates = 0, basic_rates = 0;
5014 	bool have_higher_than_11mbit = false;
5015 	int min_rate = INT_MAX, min_rate_index = -1;
5016 	struct ieee80211_supported_band *sband;
5017 
5018 	memcpy(link_sta->addr, cbss->bssid, ETH_ALEN);
5019 	memcpy(link_sta->pub->addr, cbss->bssid, ETH_ALEN);
5020 
5021 	/* TODO: S1G Basic Rate Set is expressed elsewhere */
5022 	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5023 		ieee80211_s1g_sta_rate_init(sta);
5024 		return 0;
5025 	}
5026 
5027 	sband = local->hw.wiphy->bands[cbss->channel->band];
5028 
5029 	ieee80211_get_rates(sband, bss->supp_rates, bss->supp_rates_len,
5030 			    &rates, &basic_rates, &have_higher_than_11mbit,
5031 			    &min_rate, &min_rate_index);
5032 
5033 	/*
5034 	 * This used to be a workaround for basic rates missing
5035 	 * in the association response frame. Now that we no
5036 	 * longer use the basic rates from there, it probably
5037 	 * doesn't happen any more, but keep the workaround so
5038 	 * in case some *other* APs are buggy in different ways
5039 	 * we can connect -- with a warning.
5040 	 * Allow this workaround only in case the AP provided at least
5041 	 * one rate.
5042 	 */
5043 	if (min_rate_index < 0) {
5044 		link_info(link, "No legacy rates in association response\n");
5045 		return -EINVAL;
5046 	} else if (!basic_rates) {
5047 		link_info(link, "No basic rates, using min rate instead\n");
5048 		basic_rates = BIT(min_rate_index);
5049 	}
5050 
5051 	if (rates)
5052 		link_sta->pub->supp_rates[cbss->channel->band] = rates;
5053 	else
5054 		link_info(link, "No rates found, keeping mandatory only\n");
5055 
5056 	link->conf->basic_rates = basic_rates;
5057 
5058 	/* cf. IEEE 802.11 9.2.12 */
5059 	link->operating_11g_mode = sband->band == NL80211_BAND_2GHZ &&
5060 				   have_higher_than_11mbit;
5061 
5062 	return 0;
5063 }
5064 
5065 static u8 ieee80211_max_rx_chains(struct ieee80211_link_data *link,
5066 				  struct cfg80211_bss *cbss)
5067 {
5068 	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
5069 	const struct element *ht_cap_elem, *vht_cap_elem;
5070 	const struct cfg80211_bss_ies *ies;
5071 	const struct ieee80211_ht_cap *ht_cap;
5072 	const struct ieee80211_vht_cap *vht_cap;
5073 	const struct ieee80211_he_cap_elem *he_cap;
5074 	const struct element *he_cap_elem;
5075 	u16 mcs_80_map, mcs_160_map;
5076 	int i, mcs_nss_size;
5077 	bool support_160;
5078 	u8 chains = 1;
5079 
5080 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HT)
5081 		return chains;
5082 
5083 	ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
5084 	if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
5085 		ht_cap = (void *)ht_cap_elem->data;
5086 		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
5087 		/*
5088 		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
5089 		 *	 "Tx Unequal Modulation Supported" fields.
5090 		 */
5091 	}
5092 
5093 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_VHT)
5094 		return chains;
5095 
5096 	vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
5097 	if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
5098 		u8 nss;
5099 		u16 tx_mcs_map;
5100 
5101 		vht_cap = (void *)vht_cap_elem->data;
5102 		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
5103 		for (nss = 8; nss > 0; nss--) {
5104 			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
5105 					IEEE80211_VHT_MCS_NOT_SUPPORTED)
5106 				break;
5107 		}
5108 		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
5109 		chains = max(chains, nss);
5110 	}
5111 
5112 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HE)
5113 		return chains;
5114 
5115 	ies = rcu_dereference(cbss->ies);
5116 	he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
5117 					     ies->data, ies->len);
5118 
5119 	if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap))
5120 		return chains;
5121 
5122 	/* skip one byte ext_tag_id */
5123 	he_cap = (void *)(he_cap_elem->data + 1);
5124 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
5125 
5126 	/* invalid HE IE */
5127 	if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap))
5128 		return chains;
5129 
5130 	/* mcs_nss is right after he_cap info */
5131 	he_mcs_nss_supp = (void *)(he_cap + 1);
5132 
5133 	mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
5134 
5135 	for (i = 7; i >= 0; i--) {
5136 		u8 mcs_80 = mcs_80_map >> (2 * i) & 3;
5137 
5138 		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
5139 			chains = max_t(u8, chains, i + 1);
5140 			break;
5141 		}
5142 	}
5143 
5144 	support_160 = he_cap->phy_cap_info[0] &
5145 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
5146 
5147 	if (!support_160)
5148 		return chains;
5149 
5150 	mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160);
5151 	for (i = 7; i >= 0; i--) {
5152 		u8 mcs_160 = mcs_160_map >> (2 * i) & 3;
5153 
5154 		if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
5155 			chains = max_t(u8, chains, i + 1);
5156 			break;
5157 		}
5158 	}
5159 
5160 	return chains;
5161 }
5162 
5163 static void
5164 ieee80211_determine_our_sta_mode(struct ieee80211_sub_if_data *sdata,
5165 				 struct ieee80211_supported_band *sband,
5166 				 struct cfg80211_assoc_request *req,
5167 				 bool wmm_used, int link_id,
5168 				 struct ieee80211_conn_settings *conn)
5169 {
5170 	struct ieee80211_sta_ht_cap sta_ht_cap = sband->ht_cap;
5171 	bool is_5ghz = sband->band == NL80211_BAND_5GHZ;
5172 	bool is_6ghz = sband->band == NL80211_BAND_6GHZ;
5173 	const struct ieee80211_sta_he_cap *he_cap;
5174 	const struct ieee80211_sta_eht_cap *eht_cap;
5175 	struct ieee80211_sta_vht_cap vht_cap;
5176 
5177 	if (sband->band == NL80211_BAND_S1GHZ) {
5178 		conn->mode = IEEE80211_CONN_MODE_S1G;
5179 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5180 		mlme_dbg(sdata, "operating as S1G STA\n");
5181 		return;
5182 	}
5183 
5184 	conn->mode = IEEE80211_CONN_MODE_LEGACY;
5185 	conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5186 
5187 	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5188 
5189 	if (req && req->flags & ASSOC_REQ_DISABLE_HT) {
5190 		mlme_link_id_dbg(sdata, link_id,
5191 				 "HT disabled by flag, limiting to legacy\n");
5192 		goto out;
5193 	}
5194 
5195 	if (!wmm_used) {
5196 		mlme_link_id_dbg(sdata, link_id,
5197 				 "WMM/QoS not supported, limiting to legacy\n");
5198 		goto out;
5199 	}
5200 
5201 	if (req) {
5202 		unsigned int i;
5203 
5204 		for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5205 			if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5206 			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5207 			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5208 				netdev_info(sdata->dev,
5209 					    "WEP/TKIP use, limiting to legacy\n");
5210 				goto out;
5211 			}
5212 		}
5213 	}
5214 
5215 	if (!sta_ht_cap.ht_supported && !is_6ghz) {
5216 		mlme_link_id_dbg(sdata, link_id,
5217 				 "HT not supported (and not on 6 GHz), limiting to legacy\n");
5218 		goto out;
5219 	}
5220 
5221 	/* HT is fine */
5222 	conn->mode = IEEE80211_CONN_MODE_HT;
5223 	conn->bw_limit = sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
5224 		IEEE80211_CONN_BW_LIMIT_40 :
5225 		IEEE80211_CONN_BW_LIMIT_20;
5226 
5227 	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
5228 	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
5229 
5230 	if (req && req->flags & ASSOC_REQ_DISABLE_VHT) {
5231 		mlme_link_id_dbg(sdata, link_id,
5232 				 "VHT disabled by flag, limiting to HT\n");
5233 		goto out;
5234 	}
5235 
5236 	if (vht_cap.vht_supported && is_5ghz) {
5237 		bool have_80mhz = false;
5238 		unsigned int i;
5239 
5240 		if (conn->bw_limit == IEEE80211_CONN_BW_LIMIT_20) {
5241 			mlme_link_id_dbg(sdata, link_id,
5242 					 "no 40 MHz support on 5 GHz, limiting to HT\n");
5243 			goto out;
5244 		}
5245 
5246 		/* Allow VHT if at least one channel on the sband supports 80 MHz */
5247 		for (i = 0; i < sband->n_channels; i++) {
5248 			if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5249 							IEEE80211_CHAN_NO_80MHZ))
5250 				continue;
5251 
5252 			have_80mhz = true;
5253 			break;
5254 		}
5255 
5256 		if (!have_80mhz) {
5257 			mlme_link_id_dbg(sdata, link_id,
5258 					 "no 80 MHz channel support on 5 GHz, limiting to HT\n");
5259 			goto out;
5260 		}
5261 	} else if (is_5ghz) { /* !vht_supported but on 5 GHz */
5262 		mlme_link_id_dbg(sdata, link_id,
5263 				 "no VHT support on 5 GHz, limiting to HT\n");
5264 		goto out;
5265 	}
5266 
5267 	/* VHT - if we have - is fine, including 80 MHz, check 160 below again */
5268 	if (sband->band != NL80211_BAND_2GHZ) {
5269 		conn->mode = IEEE80211_CONN_MODE_VHT;
5270 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_160;
5271 	}
5272 
5273 	if (is_5ghz &&
5274 	    !(vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
5275 			     IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
5276 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_80;
5277 		mlme_link_id_dbg(sdata, link_id,
5278 				 "no VHT 160 MHz capability on 5 GHz, limiting to 80 MHz");
5279 	}
5280 
5281 	if (req && req->flags & ASSOC_REQ_DISABLE_HE) {
5282 		mlme_link_id_dbg(sdata, link_id,
5283 				 "HE disabled by flag, limiting to HT/VHT\n");
5284 		goto out;
5285 	}
5286 
5287 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
5288 	if (!he_cap) {
5289 		WARN_ON(is_6ghz);
5290 		mlme_link_id_dbg(sdata, link_id,
5291 				 "no HE support, limiting to HT/VHT\n");
5292 		goto out;
5293 	}
5294 
5295 	/* so we have HE */
5296 	conn->mode = IEEE80211_CONN_MODE_HE;
5297 
5298 	/* check bandwidth */
5299 	switch (sband->band) {
5300 	default:
5301 	case NL80211_BAND_2GHZ:
5302 		if (he_cap->he_cap_elem.phy_cap_info[0] &
5303 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
5304 			break;
5305 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5306 		mlme_link_id_dbg(sdata, link_id,
5307 				 "no 40 MHz HE cap in 2.4 GHz, limiting to 20 MHz\n");
5308 		break;
5309 	case NL80211_BAND_5GHZ:
5310 		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
5311 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
5312 			conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
5313 			mlme_link_id_dbg(sdata, link_id,
5314 					 "no 40/80 MHz HE cap in 5 GHz, limiting to 20 MHz\n");
5315 			break;
5316 		}
5317 		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
5318 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
5319 			conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
5320 					       conn->bw_limit,
5321 					       IEEE80211_CONN_BW_LIMIT_80);
5322 			mlme_link_id_dbg(sdata, link_id,
5323 					 "no 160 MHz HE cap in 5 GHz, limiting to 80 MHz\n");
5324 		}
5325 		break;
5326 	case NL80211_BAND_6GHZ:
5327 		if (he_cap->he_cap_elem.phy_cap_info[0] &
5328 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
5329 			break;
5330 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
5331 				       conn->bw_limit,
5332 				       IEEE80211_CONN_BW_LIMIT_80);
5333 		mlme_link_id_dbg(sdata, link_id,
5334 				 "no 160 MHz HE cap in 6 GHz, limiting to 80 MHz\n");
5335 		break;
5336 	}
5337 
5338 	if (req && req->flags & ASSOC_REQ_DISABLE_EHT) {
5339 		mlme_link_id_dbg(sdata, link_id,
5340 				 "EHT disabled by flag, limiting to HE\n");
5341 		goto out;
5342 	}
5343 
5344 	eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
5345 	if (!eht_cap) {
5346 		mlme_link_id_dbg(sdata, link_id,
5347 				 "no EHT support, limiting to HE\n");
5348 		goto out;
5349 	}
5350 
5351 	/* we have EHT */
5352 
5353 	conn->mode = IEEE80211_CONN_MODE_EHT;
5354 
5355 	/* check bandwidth */
5356 	if (is_6ghz &&
5357 	    eht_cap->eht_cap_elem.phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
5358 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_320;
5359 	else if (is_6ghz)
5360 		mlme_link_id_dbg(sdata, link_id,
5361 				 "no EHT 320 MHz cap in 6 GHz, limiting to 160 MHz\n");
5362 
5363 out:
5364 	mlme_link_id_dbg(sdata, link_id,
5365 			 "determined local STA to be %s, BW limited to %d MHz\n",
5366 			 ieee80211_conn_mode_str(conn->mode),
5367 			 20 * (1 << conn->bw_limit));
5368 }
5369 
5370 static void
5371 ieee80211_determine_our_sta_mode_auth(struct ieee80211_sub_if_data *sdata,
5372 				      struct ieee80211_supported_band *sband,
5373 				      struct cfg80211_auth_request *req,
5374 				      bool wmm_used,
5375 				      struct ieee80211_conn_settings *conn)
5376 {
5377 	ieee80211_determine_our_sta_mode(sdata, sband, NULL, wmm_used,
5378 					 req->link_id > 0 ? req->link_id : 0,
5379 					 conn);
5380 }
5381 
5382 static void
5383 ieee80211_determine_our_sta_mode_assoc(struct ieee80211_sub_if_data *sdata,
5384 				       struct ieee80211_supported_band *sband,
5385 				       struct cfg80211_assoc_request *req,
5386 				       bool wmm_used, int link_id,
5387 				       struct ieee80211_conn_settings *conn)
5388 {
5389 	struct ieee80211_conn_settings tmp;
5390 
5391 	WARN_ON(!req);
5392 
5393 	ieee80211_determine_our_sta_mode(sdata, sband, req, wmm_used, link_id,
5394 					 &tmp);
5395 
5396 	conn->mode = min_t(enum ieee80211_conn_mode,
5397 			   conn->mode, tmp.mode);
5398 	conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
5399 			       conn->bw_limit, tmp.bw_limit);
5400 }
5401 
5402 static enum ieee80211_ap_reg_power
5403 ieee80211_ap_power_type(u8 control)
5404 {
5405 	switch (u8_get_bits(control, IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
5406 	case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
5407 	case IEEE80211_6GHZ_CTRL_REG_INDOOR_LPI_AP:
5408 		return IEEE80211_REG_LPI_AP;
5409 	case IEEE80211_6GHZ_CTRL_REG_SP_AP:
5410 	case IEEE80211_6GHZ_CTRL_REG_INDOOR_SP_AP:
5411 		return IEEE80211_REG_SP_AP;
5412 	case IEEE80211_6GHZ_CTRL_REG_VLP_AP:
5413 		return IEEE80211_REG_VLP_AP;
5414 	default:
5415 		return IEEE80211_REG_UNSET_AP;
5416 	}
5417 }
5418 
5419 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
5420 				  struct ieee80211_link_data *link,
5421 				  int link_id,
5422 				  struct cfg80211_bss *cbss, bool mlo,
5423 				  struct ieee80211_conn_settings *conn)
5424 {
5425 	struct ieee80211_local *local = sdata->local;
5426 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
5427 	struct ieee80211_chan_req chanreq = {};
5428 	struct cfg80211_chan_def ap_chandef;
5429 	struct ieee802_11_elems *elems;
5430 	int ret;
5431 
5432 	lockdep_assert_wiphy(local->hw.wiphy);
5433 
5434 	rcu_read_lock();
5435 	elems = ieee80211_determine_chan_mode(sdata, conn, cbss, link_id,
5436 					      &chanreq, &ap_chandef);
5437 
5438 	if (IS_ERR(elems)) {
5439 		rcu_read_unlock();
5440 		return PTR_ERR(elems);
5441 	}
5442 
5443 	if (mlo && !elems->ml_basic) {
5444 		sdata_info(sdata, "Rejecting MLO as it is not supported by AP\n");
5445 		rcu_read_unlock();
5446 		kfree(elems);
5447 		return -EINVAL;
5448 	}
5449 
5450 	if (link && is_6ghz && conn->mode >= IEEE80211_CONN_MODE_HE) {
5451 		const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
5452 
5453 		if (elems->pwr_constr_elem)
5454 			link->conf->pwr_reduction = *elems->pwr_constr_elem;
5455 
5456 		he_6ghz_oper = ieee80211_he_6ghz_oper(elems->he_operation);
5457 		if (he_6ghz_oper)
5458 			link->conf->power_type =
5459 				ieee80211_ap_power_type(he_6ghz_oper->control);
5460 		else
5461 			link_info(link,
5462 				  "HE 6 GHz operation missing (on %d MHz), expect issues\n",
5463 				  cbss->channel->center_freq);
5464 
5465 		link->conf->tpe = elems->tpe;
5466 		ieee80211_rearrange_tpe(&link->conf->tpe, &ap_chandef,
5467 					&chanreq.oper);
5468 	}
5469 	rcu_read_unlock();
5470 	/* the element data was RCU protected so no longer valid anyway */
5471 	kfree(elems);
5472 	elems = NULL;
5473 
5474 	if (!link)
5475 		return 0;
5476 
5477 	rcu_read_lock();
5478 	link->needed_rx_chains = min(ieee80211_max_rx_chains(link, cbss),
5479 				     local->rx_chains);
5480 	rcu_read_unlock();
5481 
5482 	/*
5483 	 * If this fails (possibly due to channel context sharing
5484 	 * on incompatible channels, e.g. 80+80 and 160 sharing the
5485 	 * same control channel) try to use a smaller bandwidth.
5486 	 */
5487 	ret = ieee80211_link_use_channel(link, &chanreq,
5488 					 IEEE80211_CHANCTX_SHARED);
5489 
5490 	/* don't downgrade for 5 and 10 MHz channels, though. */
5491 	if (chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
5492 	    chanreq.oper.width == NL80211_CHAN_WIDTH_10)
5493 		return ret;
5494 
5495 	while (ret && chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT) {
5496 		ieee80211_chanreq_downgrade(&chanreq, conn);
5497 
5498 		ret = ieee80211_link_use_channel(link, &chanreq,
5499 						 IEEE80211_CHANCTX_SHARED);
5500 	}
5501 
5502 	return ret;
5503 }
5504 
5505 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5506 			       u8 *dtim_count, u8 *dtim_period)
5507 {
5508 	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5509 	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5510 					 ies->len);
5511 	const struct ieee80211_tim_ie *tim = NULL;
5512 	const struct ieee80211_bssid_index *idx;
5513 	bool valid = tim_ie && tim_ie[1] >= 2;
5514 
5515 	if (valid)
5516 		tim = (void *)(tim_ie + 2);
5517 
5518 	if (dtim_count)
5519 		*dtim_count = valid ? tim->dtim_count : 0;
5520 
5521 	if (dtim_period)
5522 		*dtim_period = valid ? tim->dtim_period : 0;
5523 
5524 	/* Check if value is overridden by non-transmitted profile */
5525 	if (!idx_ie || idx_ie[1] < 3)
5526 		return valid;
5527 
5528 	idx = (void *)(idx_ie + 2);
5529 
5530 	if (dtim_count)
5531 		*dtim_count = idx->dtim_count;
5532 
5533 	if (dtim_period)
5534 		*dtim_period = idx->dtim_period;
5535 
5536 	return true;
5537 }
5538 
5539 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
5540 				    struct ieee80211_mgmt *mgmt,
5541 				    struct ieee802_11_elems *elems,
5542 				    const u8 *elem_start, unsigned int elem_len)
5543 {
5544 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5545 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
5546 	struct ieee80211_local *local = sdata->local;
5547 	unsigned int link_id;
5548 	struct sta_info *sta;
5549 	u64 changed[IEEE80211_MLD_MAX_NUM_LINKS] = {};
5550 	u16 valid_links = 0, dormant_links = 0;
5551 	int err;
5552 
5553 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5554 	/*
5555 	 * station info was already allocated and inserted before
5556 	 * the association and should be available to us
5557 	 */
5558 	sta = sta_info_get(sdata, assoc_data->ap_addr);
5559 	if (WARN_ON(!sta))
5560 		goto out_err;
5561 
5562 	sta->sta.spp_amsdu = assoc_data->spp_amsdu;
5563 
5564 	if (ieee80211_vif_is_mld(&sdata->vif)) {
5565 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
5566 			if (!assoc_data->link[link_id].bss)
5567 				continue;
5568 
5569 			valid_links |= BIT(link_id);
5570 			if (assoc_data->link[link_id].disabled)
5571 				dormant_links |= BIT(link_id);
5572 
5573 			if (link_id != assoc_data->assoc_link_id) {
5574 				err = ieee80211_sta_allocate_link(sta, link_id);
5575 				if (err)
5576 					goto out_err;
5577 			}
5578 		}
5579 
5580 		ieee80211_vif_set_links(sdata, valid_links, dormant_links);
5581 	}
5582 
5583 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
5584 		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
5585 		struct ieee80211_link_data *link;
5586 		struct link_sta_info *link_sta;
5587 
5588 		if (!cbss)
5589 			continue;
5590 
5591 		link = sdata_dereference(sdata->link[link_id], sdata);
5592 		if (WARN_ON(!link))
5593 			goto out_err;
5594 
5595 		if (ieee80211_vif_is_mld(&sdata->vif))
5596 			link_info(link,
5597 				  "local address %pM, AP link address %pM%s\n",
5598 				  link->conf->addr,
5599 				  assoc_data->link[link_id].bss->bssid,
5600 				  link_id == assoc_data->assoc_link_id ?
5601 					" (assoc)" : "");
5602 
5603 		link_sta = rcu_dereference_protected(sta->link[link_id],
5604 						     lockdep_is_held(&local->hw.wiphy->mtx));
5605 		if (WARN_ON(!link_sta))
5606 			goto out_err;
5607 
5608 		if (!link->u.mgd.have_beacon) {
5609 			const struct cfg80211_bss_ies *ies;
5610 
5611 			rcu_read_lock();
5612 			ies = rcu_dereference(cbss->beacon_ies);
5613 			if (ies)
5614 				link->u.mgd.have_beacon = true;
5615 			else
5616 				ies = rcu_dereference(cbss->ies);
5617 			ieee80211_get_dtim(ies,
5618 					   &link->conf->sync_dtim_count,
5619 					   &link->u.mgd.dtim_period);
5620 			link->conf->beacon_int = cbss->beacon_interval;
5621 			rcu_read_unlock();
5622 		}
5623 
5624 		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
5625 
5626 		if (link_id != assoc_data->assoc_link_id) {
5627 			link->u.mgd.conn = assoc_data->link[link_id].conn;
5628 
5629 			err = ieee80211_prep_channel(sdata, link, link_id, cbss,
5630 						     true, &link->u.mgd.conn);
5631 			if (err) {
5632 				link_info(link, "prep_channel failed\n");
5633 				goto out_err;
5634 			}
5635 		}
5636 
5637 		err = ieee80211_mgd_setup_link_sta(link, sta, link_sta,
5638 						   assoc_data->link[link_id].bss);
5639 		if (err)
5640 			goto out_err;
5641 
5642 		if (!ieee80211_assoc_config_link(link, link_sta,
5643 						 assoc_data->link[link_id].bss,
5644 						 mgmt, elem_start, elem_len,
5645 						 &changed[link_id]))
5646 			goto out_err;
5647 
5648 		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
5649 			valid_links &= ~BIT(link_id);
5650 			ieee80211_sta_remove_link(sta, link_id);
5651 			continue;
5652 		}
5653 
5654 		if (link_id != assoc_data->assoc_link_id) {
5655 			err = ieee80211_sta_activate_link(sta, link_id);
5656 			if (err)
5657 				goto out_err;
5658 		}
5659 	}
5660 
5661 	/* links might have changed due to rejected ones, set them again */
5662 	ieee80211_vif_set_links(sdata, valid_links, dormant_links);
5663 
5664 	rate_control_rate_init(sta);
5665 
5666 	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
5667 		set_sta_flag(sta, WLAN_STA_MFP);
5668 		sta->sta.mfp = true;
5669 	} else {
5670 		sta->sta.mfp = false;
5671 	}
5672 
5673 	ieee80211_sta_set_max_amsdu_subframes(sta, elems->ext_capab,
5674 					      elems->ext_capab_len);
5675 
5676 	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
5677 		       local->hw.queues >= IEEE80211_NUM_ACS;
5678 
5679 	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
5680 	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
5681 		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
5682 	if (err) {
5683 		sdata_info(sdata,
5684 			   "failed to move station %pM to desired state\n",
5685 			   sta->sta.addr);
5686 		WARN_ON(__sta_info_destroy(sta));
5687 		goto out_err;
5688 	}
5689 
5690 	if (sdata->wdev.use_4addr)
5691 		drv_sta_set_4addr(local, sdata, &sta->sta, true);
5692 
5693 	ieee80211_set_associated(sdata, assoc_data, changed);
5694 
5695 	/*
5696 	 * If we're using 4-addr mode, let the AP know that we're
5697 	 * doing so, so that it can create the STA VLAN on its side
5698 	 */
5699 	if (ifmgd->use_4addr)
5700 		ieee80211_send_4addr_nullfunc(local, sdata);
5701 
5702 	/*
5703 	 * Start timer to probe the connection to the AP now.
5704 	 * Also start the timer that will detect beacon loss.
5705 	 */
5706 	ieee80211_sta_reset_beacon_monitor(sdata);
5707 	ieee80211_sta_reset_conn_monitor(sdata);
5708 
5709 	return true;
5710 out_err:
5711 	eth_zero_addr(sdata->vif.cfg.ap_addr);
5712 	return false;
5713 }
5714 
5715 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
5716 					 struct ieee80211_mgmt *mgmt,
5717 					 size_t len)
5718 {
5719 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5720 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
5721 	u16 capab_info, status_code, aid;
5722 	struct ieee80211_elems_parse_params parse_params = {
5723 		.bss = NULL,
5724 		.link_id = -1,
5725 		.from_ap = true,
5726 	};
5727 	struct ieee802_11_elems *elems;
5728 	int ac;
5729 	const u8 *elem_start;
5730 	unsigned int elem_len;
5731 	bool reassoc;
5732 	struct ieee80211_event event = {
5733 		.type = MLME_EVENT,
5734 		.u.mlme.data = ASSOC_EVENT,
5735 	};
5736 	struct ieee80211_prep_tx_info info = {};
5737 	struct cfg80211_rx_assoc_resp_data resp = {
5738 		.uapsd_queues = -1,
5739 	};
5740 	u8 ap_mld_addr[ETH_ALEN] __aligned(2);
5741 	unsigned int link_id;
5742 
5743 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5744 
5745 	if (!assoc_data)
5746 		return;
5747 
5748 	parse_params.mode =
5749 		assoc_data->link[assoc_data->assoc_link_id].conn.mode;
5750 
5751 	if (!ether_addr_equal(assoc_data->ap_addr, mgmt->bssid) ||
5752 	    !ether_addr_equal(assoc_data->ap_addr, mgmt->sa))
5753 		return;
5754 
5755 	/*
5756 	 * AssocResp and ReassocResp have identical structure, so process both
5757 	 * of them in this function.
5758 	 */
5759 
5760 	if (len < 24 + 6)
5761 		return;
5762 
5763 	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
5764 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
5765 	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
5766 	if (assoc_data->s1g)
5767 		elem_start = mgmt->u.s1g_assoc_resp.variable;
5768 	else
5769 		elem_start = mgmt->u.assoc_resp.variable;
5770 
5771 	/*
5772 	 * Note: this may not be perfect, AP might misbehave - if
5773 	 * anyone needs to rely on perfect complete notification
5774 	 * with the exact right subtype, then we need to track what
5775 	 * we actually transmitted.
5776 	 */
5777 	info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
5778 				 IEEE80211_STYPE_ASSOC_REQ;
5779 
5780 	if (assoc_data->fils_kek_len &&
5781 	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
5782 		return;
5783 
5784 	elem_len = len - (elem_start - (u8 *)mgmt);
5785 	parse_params.start = elem_start;
5786 	parse_params.len = elem_len;
5787 	elems = ieee802_11_parse_elems_full(&parse_params);
5788 	if (!elems)
5789 		goto notify_driver;
5790 
5791 	if (elems->aid_resp)
5792 		aid = le16_to_cpu(elems->aid_resp->aid);
5793 	else if (assoc_data->s1g)
5794 		aid = 0; /* TODO */
5795 	else
5796 		aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
5797 
5798 	/*
5799 	 * The 5 MSB of the AID field are reserved
5800 	 * (802.11-2016 9.4.1.8 AID field)
5801 	 */
5802 	aid &= 0x7ff;
5803 
5804 	sdata_info(sdata,
5805 		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
5806 		   reassoc ? "Rea" : "A", assoc_data->ap_addr,
5807 		   capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
5808 
5809 	ifmgd->broken_ap = false;
5810 
5811 	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
5812 	    elems->timeout_int &&
5813 	    elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
5814 		u32 tu, ms;
5815 
5816 		cfg80211_assoc_comeback(sdata->dev, assoc_data->ap_addr,
5817 					le32_to_cpu(elems->timeout_int->value));
5818 
5819 		tu = le32_to_cpu(elems->timeout_int->value);
5820 		ms = tu * 1024 / 1000;
5821 		sdata_info(sdata,
5822 			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
5823 			   assoc_data->ap_addr, tu, ms);
5824 		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
5825 		assoc_data->timeout_started = true;
5826 		assoc_data->comeback = true;
5827 		if (ms > IEEE80211_ASSOC_TIMEOUT)
5828 			run_again(sdata, assoc_data->timeout);
5829 		goto notify_driver;
5830 	}
5831 
5832 	if (status_code != WLAN_STATUS_SUCCESS) {
5833 		sdata_info(sdata, "%pM denied association (code=%d)\n",
5834 			   assoc_data->ap_addr, status_code);
5835 		event.u.mlme.status = MLME_DENIED;
5836 		event.u.mlme.reason = status_code;
5837 		drv_event_callback(sdata->local, sdata, &event);
5838 	} else {
5839 		if (aid == 0 || aid > IEEE80211_MAX_AID) {
5840 			sdata_info(sdata,
5841 				   "invalid AID value %d (out of range), turn off PS\n",
5842 				   aid);
5843 			aid = 0;
5844 			ifmgd->broken_ap = true;
5845 		}
5846 
5847 		if (ieee80211_vif_is_mld(&sdata->vif)) {
5848 			struct ieee80211_mle_basic_common_info *common;
5849 
5850 			if (!elems->ml_basic) {
5851 				sdata_info(sdata,
5852 					   "MLO association with %pM but no (basic) multi-link element in response!\n",
5853 					   assoc_data->ap_addr);
5854 				goto abandon_assoc;
5855 			}
5856 
5857 			common = (void *)elems->ml_basic->variable;
5858 
5859 			if (memcmp(assoc_data->ap_addr,
5860 				   common->mld_mac_addr, ETH_ALEN)) {
5861 				sdata_info(sdata,
5862 					   "AP MLD MAC address mismatch: got %pM expected %pM\n",
5863 					   common->mld_mac_addr,
5864 					   assoc_data->ap_addr);
5865 				goto abandon_assoc;
5866 			}
5867 
5868 			sdata->vif.cfg.eml_cap =
5869 				ieee80211_mle_get_eml_cap((const void *)elems->ml_basic);
5870 			sdata->vif.cfg.eml_med_sync_delay =
5871 				ieee80211_mle_get_eml_med_sync_delay((const void *)elems->ml_basic);
5872 			sdata->vif.cfg.mld_capa_op =
5873 				ieee80211_mle_get_mld_capa_op((const void *)elems->ml_basic);
5874 		}
5875 
5876 		sdata->vif.cfg.aid = aid;
5877 
5878 		if (!ieee80211_assoc_success(sdata, mgmt, elems,
5879 					     elem_start, elem_len)) {
5880 			/* oops -- internal error -- send timeout for now */
5881 			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
5882 			goto notify_driver;
5883 		}
5884 		event.u.mlme.status = MLME_SUCCESS;
5885 		drv_event_callback(sdata->local, sdata, &event);
5886 		sdata_info(sdata, "associated\n");
5887 
5888 		info.success = 1;
5889 	}
5890 
5891 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
5892 		struct ieee80211_link_data *link;
5893 
5894 		if (!assoc_data->link[link_id].bss)
5895 			continue;
5896 
5897 		resp.links[link_id].bss = assoc_data->link[link_id].bss;
5898 		ether_addr_copy(resp.links[link_id].addr,
5899 				assoc_data->link[link_id].addr);
5900 		resp.links[link_id].status = assoc_data->link[link_id].status;
5901 
5902 		link = sdata_dereference(sdata->link[link_id], sdata);
5903 		if (!link)
5904 			continue;
5905 
5906 		/* get uapsd queues configuration - same for all links */
5907 		resp.uapsd_queues = 0;
5908 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
5909 			if (link->tx_conf[ac].uapsd)
5910 				resp.uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
5911 	}
5912 
5913 	if (ieee80211_vif_is_mld(&sdata->vif)) {
5914 		ether_addr_copy(ap_mld_addr, sdata->vif.cfg.ap_addr);
5915 		resp.ap_mld_addr = ap_mld_addr;
5916 	}
5917 
5918 	ieee80211_destroy_assoc_data(sdata,
5919 				     status_code == WLAN_STATUS_SUCCESS ?
5920 					ASSOC_SUCCESS :
5921 					ASSOC_REJECTED);
5922 
5923 	resp.buf = (u8 *)mgmt;
5924 	resp.len = len;
5925 	resp.req_ies = ifmgd->assoc_req_ies;
5926 	resp.req_ies_len = ifmgd->assoc_req_ies_len;
5927 	cfg80211_rx_assoc_resp(sdata->dev, &resp);
5928 notify_driver:
5929 	drv_mgd_complete_tx(sdata->local, sdata, &info);
5930 	kfree(elems);
5931 	return;
5932 abandon_assoc:
5933 	ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
5934 	goto notify_driver;
5935 }
5936 
5937 static void ieee80211_rx_bss_info(struct ieee80211_link_data *link,
5938 				  struct ieee80211_mgmt *mgmt, size_t len,
5939 				  struct ieee80211_rx_status *rx_status)
5940 {
5941 	struct ieee80211_sub_if_data *sdata = link->sdata;
5942 	struct ieee80211_local *local = sdata->local;
5943 	struct ieee80211_bss *bss;
5944 	struct ieee80211_channel *channel;
5945 
5946 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5947 
5948 	channel = ieee80211_get_channel_khz(local->hw.wiphy,
5949 					ieee80211_rx_status_to_khz(rx_status));
5950 	if (!channel)
5951 		return;
5952 
5953 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
5954 	if (bss) {
5955 		link->conf->beacon_rate = bss->beacon_rate;
5956 		ieee80211_rx_bss_put(local, bss);
5957 	}
5958 }
5959 
5960 
5961 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_link_data *link,
5962 					 struct sk_buff *skb)
5963 {
5964 	struct ieee80211_sub_if_data *sdata = link->sdata;
5965 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
5966 	struct ieee80211_if_managed *ifmgd;
5967 	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
5968 	struct ieee80211_channel *channel;
5969 	size_t baselen, len = skb->len;
5970 
5971 	ifmgd = &sdata->u.mgd;
5972 
5973 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5974 
5975 	/*
5976 	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
5977 	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
5978 	 * a Probe Response frame [..], the Address 1 field of the Probe
5979 	 * Response frame shall be set to the broadcast address [..]"
5980 	 * So, on 6GHz band we should also accept broadcast responses.
5981 	 */
5982 	channel = ieee80211_get_channel(sdata->local->hw.wiphy,
5983 					rx_status->freq);
5984 	if (!channel)
5985 		return;
5986 
5987 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
5988 	    (channel->band != NL80211_BAND_6GHZ ||
5989 	     !is_broadcast_ether_addr(mgmt->da)))
5990 		return; /* ignore ProbeResp to foreign address */
5991 
5992 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
5993 	if (baselen > len)
5994 		return;
5995 
5996 	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
5997 
5998 	if (ifmgd->associated &&
5999 	    ether_addr_equal(mgmt->bssid, link->u.mgd.bssid))
6000 		ieee80211_reset_ap_probe(sdata);
6001 }
6002 
6003 /*
6004  * This is the canonical list of information elements we care about,
6005  * the filter code also gives us all changes to the Microsoft OUI
6006  * (00:50:F2) vendor IE which is used for WMM which we need to track,
6007  * as well as the DTPC IE (part of the Cisco OUI) used for signaling
6008  * changes to requested client power.
6009  *
6010  * We implement beacon filtering in software since that means we can
6011  * avoid processing the frame here and in cfg80211, and userspace
6012  * will not be able to tell whether the hardware supports it or not.
6013  *
6014  * XXX: This list needs to be dynamic -- userspace needs to be able to
6015  *	add items it requires. It also needs to be able to tell us to
6016  *	look out for other vendor IEs.
6017  */
6018 static const u64 care_about_ies =
6019 	(1ULL << WLAN_EID_COUNTRY) |
6020 	(1ULL << WLAN_EID_ERP_INFO) |
6021 	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
6022 	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
6023 	(1ULL << WLAN_EID_HT_CAPABILITY) |
6024 	(1ULL << WLAN_EID_HT_OPERATION) |
6025 	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
6026 
6027 static void ieee80211_handle_beacon_sig(struct ieee80211_link_data *link,
6028 					struct ieee80211_if_managed *ifmgd,
6029 					struct ieee80211_bss_conf *bss_conf,
6030 					struct ieee80211_local *local,
6031 					struct ieee80211_rx_status *rx_status)
6032 {
6033 	struct ieee80211_sub_if_data *sdata = link->sdata;
6034 
6035 	/* Track average RSSI from the Beacon frames of the current AP */
6036 
6037 	if (!link->u.mgd.tracking_signal_avg) {
6038 		link->u.mgd.tracking_signal_avg = true;
6039 		ewma_beacon_signal_init(&link->u.mgd.ave_beacon_signal);
6040 		link->u.mgd.last_cqm_event_signal = 0;
6041 		link->u.mgd.count_beacon_signal = 1;
6042 		link->u.mgd.last_ave_beacon_signal = 0;
6043 	} else {
6044 		link->u.mgd.count_beacon_signal++;
6045 	}
6046 
6047 	ewma_beacon_signal_add(&link->u.mgd.ave_beacon_signal,
6048 			       -rx_status->signal);
6049 
6050 	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
6051 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
6052 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
6053 		int last_sig = link->u.mgd.last_ave_beacon_signal;
6054 		struct ieee80211_event event = {
6055 			.type = RSSI_EVENT,
6056 		};
6057 
6058 		/*
6059 		 * if signal crosses either of the boundaries, invoke callback
6060 		 * with appropriate parameters
6061 		 */
6062 		if (sig > ifmgd->rssi_max_thold &&
6063 		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
6064 			link->u.mgd.last_ave_beacon_signal = sig;
6065 			event.u.rssi.data = RSSI_EVENT_HIGH;
6066 			drv_event_callback(local, sdata, &event);
6067 		} else if (sig < ifmgd->rssi_min_thold &&
6068 			   (last_sig >= ifmgd->rssi_max_thold ||
6069 			   last_sig == 0)) {
6070 			link->u.mgd.last_ave_beacon_signal = sig;
6071 			event.u.rssi.data = RSSI_EVENT_LOW;
6072 			drv_event_callback(local, sdata, &event);
6073 		}
6074 	}
6075 
6076 	if (bss_conf->cqm_rssi_thold &&
6077 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
6078 	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
6079 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
6080 		int last_event = link->u.mgd.last_cqm_event_signal;
6081 		int thold = bss_conf->cqm_rssi_thold;
6082 		int hyst = bss_conf->cqm_rssi_hyst;
6083 
6084 		if (sig < thold &&
6085 		    (last_event == 0 || sig < last_event - hyst)) {
6086 			link->u.mgd.last_cqm_event_signal = sig;
6087 			ieee80211_cqm_rssi_notify(
6088 				&sdata->vif,
6089 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
6090 				sig, GFP_KERNEL);
6091 		} else if (sig > thold &&
6092 			   (last_event == 0 || sig > last_event + hyst)) {
6093 			link->u.mgd.last_cqm_event_signal = sig;
6094 			ieee80211_cqm_rssi_notify(
6095 				&sdata->vif,
6096 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
6097 				sig, GFP_KERNEL);
6098 		}
6099 	}
6100 
6101 	if (bss_conf->cqm_rssi_low &&
6102 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
6103 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
6104 		int last_event = link->u.mgd.last_cqm_event_signal;
6105 		int low = bss_conf->cqm_rssi_low;
6106 		int high = bss_conf->cqm_rssi_high;
6107 
6108 		if (sig < low &&
6109 		    (last_event == 0 || last_event >= low)) {
6110 			link->u.mgd.last_cqm_event_signal = sig;
6111 			ieee80211_cqm_rssi_notify(
6112 				&sdata->vif,
6113 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
6114 				sig, GFP_KERNEL);
6115 		} else if (sig > high &&
6116 			   (last_event == 0 || last_event <= high)) {
6117 			link->u.mgd.last_cqm_event_signal = sig;
6118 			ieee80211_cqm_rssi_notify(
6119 				&sdata->vif,
6120 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
6121 				sig, GFP_KERNEL);
6122 		}
6123 	}
6124 }
6125 
6126 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
6127 				    struct cfg80211_bss *bss)
6128 {
6129 	if (ether_addr_equal(tx_bssid, bss->bssid))
6130 		return true;
6131 	if (!bss->transmitted_bss)
6132 		return false;
6133 	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
6134 }
6135 
6136 static void ieee80211_ml_reconf_work(struct wiphy *wiphy,
6137 				     struct wiphy_work *work)
6138 {
6139 	struct ieee80211_sub_if_data *sdata =
6140 		container_of(work, struct ieee80211_sub_if_data,
6141 			     u.mgd.ml_reconf_work.work);
6142 	u16 new_valid_links, new_active_links, new_dormant_links;
6143 	int ret;
6144 
6145 	if (!sdata->u.mgd.removed_links)
6146 		return;
6147 
6148 	sdata_info(sdata,
6149 		   "MLO Reconfiguration: work: valid=0x%x, removed=0x%x\n",
6150 		   sdata->vif.valid_links, sdata->u.mgd.removed_links);
6151 
6152 	new_valid_links = sdata->vif.valid_links & ~sdata->u.mgd.removed_links;
6153 	if (new_valid_links == sdata->vif.valid_links)
6154 		return;
6155 
6156 	if (!new_valid_links ||
6157 	    !(new_valid_links & ~sdata->vif.dormant_links)) {
6158 		sdata_info(sdata, "No valid links after reconfiguration\n");
6159 		ret = -EINVAL;
6160 		goto out;
6161 	}
6162 
6163 	new_active_links = sdata->vif.active_links & ~sdata->u.mgd.removed_links;
6164 	if (new_active_links != sdata->vif.active_links) {
6165 		if (!new_active_links)
6166 			new_active_links =
6167 				BIT(ffs(new_valid_links &
6168 					~sdata->vif.dormant_links) - 1);
6169 
6170 		ret = ieee80211_set_active_links(&sdata->vif, new_active_links);
6171 		if (ret) {
6172 			sdata_info(sdata,
6173 				   "Failed setting active links\n");
6174 			goto out;
6175 		}
6176 	}
6177 
6178 	new_dormant_links = sdata->vif.dormant_links & ~sdata->u.mgd.removed_links;
6179 
6180 	ret = ieee80211_vif_set_links(sdata, new_valid_links,
6181 				      new_dormant_links);
6182 	if (ret)
6183 		sdata_info(sdata, "Failed setting valid links\n");
6184 
6185 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
6186 
6187 out:
6188 	if (!ret)
6189 		cfg80211_links_removed(sdata->dev, sdata->u.mgd.removed_links);
6190 	else
6191 		__ieee80211_disconnect(sdata);
6192 
6193 	sdata->u.mgd.removed_links = 0;
6194 }
6195 
6196 static void ieee80211_ml_reconfiguration(struct ieee80211_sub_if_data *sdata,
6197 					 struct ieee802_11_elems *elems)
6198 {
6199 	const struct element *sub;
6200 	unsigned long removed_links = 0;
6201 	u16 link_removal_timeout[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6202 	u8 link_id;
6203 	u32 delay;
6204 
6205 	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_reconf)
6206 		return;
6207 
6208 	/* Directly parse the sub elements as the common information doesn't
6209 	 * hold any useful information.
6210 	 */
6211 	for_each_mle_subelement(sub, (const u8 *)elems->ml_reconf,
6212 				elems->ml_reconf_len) {
6213 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
6214 		u8 *pos = prof->variable;
6215 		u16 control;
6216 
6217 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
6218 			continue;
6219 
6220 		if (!ieee80211_mle_reconf_sta_prof_size_ok(sub->data,
6221 							   sub->datalen))
6222 			return;
6223 
6224 		control = le16_to_cpu(prof->control);
6225 		link_id = control & IEEE80211_MLE_STA_RECONF_CONTROL_LINK_ID;
6226 
6227 		removed_links |= BIT(link_id);
6228 
6229 		/* the MAC address should not be included, but handle it */
6230 		if (control &
6231 		    IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT)
6232 			pos += 6;
6233 
6234 		/* According to Draft P802.11be_D3.0, the control should
6235 		 * include the AP Removal Timer present. If the AP Removal Timer
6236 		 * is not present assume immediate removal.
6237 		 */
6238 		if (control &
6239 		    IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT)
6240 			link_removal_timeout[link_id] = get_unaligned_le16(pos);
6241 	}
6242 
6243 	removed_links &= sdata->vif.valid_links;
6244 	if (!removed_links) {
6245 		/* In case the removal was cancelled, abort it */
6246 		if (sdata->u.mgd.removed_links) {
6247 			sdata->u.mgd.removed_links = 0;
6248 			wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
6249 						  &sdata->u.mgd.ml_reconf_work);
6250 		}
6251 		return;
6252 	}
6253 
6254 	delay = 0;
6255 	for_each_set_bit(link_id, &removed_links, IEEE80211_MLD_MAX_NUM_LINKS) {
6256 		struct ieee80211_bss_conf *link_conf =
6257 			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
6258 		u32 link_delay;
6259 
6260 		if (!link_conf) {
6261 			removed_links &= ~BIT(link_id);
6262 			continue;
6263 		}
6264 
6265 		if (link_removal_timeout[link_id] < 1)
6266 			link_delay = 0;
6267 		else
6268 			link_delay = link_conf->beacon_int *
6269 				(link_removal_timeout[link_id] - 1);
6270 
6271 		if (!delay)
6272 			delay = link_delay;
6273 		else
6274 			delay = min(delay, link_delay);
6275 	}
6276 
6277 	sdata->u.mgd.removed_links = removed_links;
6278 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
6279 				 &sdata->u.mgd.ml_reconf_work,
6280 				 TU_TO_JIFFIES(delay));
6281 }
6282 
6283 static int ieee80211_ttlm_set_links(struct ieee80211_sub_if_data *sdata,
6284 				    u16 active_links, u16 dormant_links,
6285 				    u16 suspended_links)
6286 {
6287 	u64 changed = 0;
6288 	int ret;
6289 
6290 	if (!active_links) {
6291 		ret = -EINVAL;
6292 		goto out;
6293 	}
6294 
6295 	/* If there is an active negotiated TTLM, it should be discarded by
6296 	 * the new negotiated/advertised TTLM.
6297 	 */
6298 	if (sdata->vif.neg_ttlm.valid) {
6299 		memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
6300 		sdata->vif.suspended_links = 0;
6301 		changed = BSS_CHANGED_MLD_TTLM;
6302 	}
6303 
6304 	if (sdata->vif.active_links != active_links) {
6305 		/* usable links are affected when active_links are changed,
6306 		 * so notify the driver about the status change
6307 		 */
6308 		changed |= BSS_CHANGED_MLD_VALID_LINKS;
6309 		active_links &= sdata->vif.active_links;
6310 		if (!active_links)
6311 			active_links =
6312 				BIT(__ffs(sdata->vif.valid_links &
6313 				    ~dormant_links));
6314 		ret = ieee80211_set_active_links(&sdata->vif, active_links);
6315 		if (ret) {
6316 			sdata_info(sdata, "Failed to set TTLM active links\n");
6317 			goto out;
6318 		}
6319 	}
6320 
6321 	ret = ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
6322 				      dormant_links);
6323 	if (ret) {
6324 		sdata_info(sdata, "Failed to set TTLM dormant links\n");
6325 		goto out;
6326 	}
6327 
6328 	sdata->vif.suspended_links = suspended_links;
6329 	if (sdata->vif.suspended_links)
6330 		changed |= BSS_CHANGED_MLD_TTLM;
6331 
6332 	ieee80211_vif_cfg_change_notify(sdata, changed);
6333 
6334 out:
6335 	if (ret)
6336 		ieee80211_disconnect(&sdata->vif, false);
6337 
6338 	return ret;
6339 }
6340 
6341 static void ieee80211_tid_to_link_map_work(struct wiphy *wiphy,
6342 					   struct wiphy_work *work)
6343 {
6344 	u16 new_active_links, new_dormant_links;
6345 	struct ieee80211_sub_if_data *sdata =
6346 		container_of(work, struct ieee80211_sub_if_data,
6347 			     u.mgd.ttlm_work.work);
6348 
6349 	new_active_links = sdata->u.mgd.ttlm_info.map &
6350 			   sdata->vif.valid_links;
6351 	new_dormant_links = ~sdata->u.mgd.ttlm_info.map &
6352 			    sdata->vif.valid_links;
6353 
6354 	ieee80211_vif_set_links(sdata, sdata->vif.valid_links, 0);
6355 	if (ieee80211_ttlm_set_links(sdata, new_active_links, new_dormant_links,
6356 				     0))
6357 		return;
6358 
6359 	sdata->u.mgd.ttlm_info.active = true;
6360 	sdata->u.mgd.ttlm_info.switch_time = 0;
6361 }
6362 
6363 static u16 ieee80211_get_ttlm(u8 bm_size, u8 *data)
6364 {
6365 	if (bm_size == 1)
6366 		return *data;
6367 	else
6368 		return get_unaligned_le16(data);
6369 }
6370 
6371 static int
6372 ieee80211_parse_adv_t2l(struct ieee80211_sub_if_data *sdata,
6373 			const struct ieee80211_ttlm_elem *ttlm,
6374 			struct ieee80211_adv_ttlm_info *ttlm_info)
6375 {
6376 	/* The element size was already validated in
6377 	 * ieee80211_tid_to_link_map_size_ok()
6378 	 */
6379 	u8 control, link_map_presence, map_size, tid;
6380 	u8 *pos;
6381 
6382 	memset(ttlm_info, 0, sizeof(*ttlm_info));
6383 	pos = (void *)ttlm->optional;
6384 	control	= ttlm->control;
6385 
6386 	if ((control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) ||
6387 	    !(control & IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT))
6388 		return 0;
6389 
6390 	if ((control & IEEE80211_TTLM_CONTROL_DIRECTION) !=
6391 	    IEEE80211_TTLM_DIRECTION_BOTH) {
6392 		sdata_info(sdata, "Invalid advertised T2L map direction\n");
6393 		return -EINVAL;
6394 	}
6395 
6396 	link_map_presence = *pos;
6397 	pos++;
6398 
6399 	ttlm_info->switch_time = get_unaligned_le16(pos);
6400 
6401 	/* Since ttlm_info->switch_time == 0 means no switch time, bump it
6402 	 * by 1.
6403 	 */
6404 	if (!ttlm_info->switch_time)
6405 		ttlm_info->switch_time = 1;
6406 
6407 	pos += 2;
6408 
6409 	if (control & IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT) {
6410 		ttlm_info->duration = pos[0] | pos[1] << 8 | pos[2] << 16;
6411 		pos += 3;
6412 	}
6413 
6414 	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
6415 		map_size = 1;
6416 	else
6417 		map_size = 2;
6418 
6419 	/* According to Draft P802.11be_D3.0 clause 35.3.7.1.7, an AP MLD shall
6420 	 * not advertise a TID-to-link mapping that does not map all TIDs to the
6421 	 * same link set, reject frame if not all links have mapping
6422 	 */
6423 	if (link_map_presence != 0xff) {
6424 		sdata_info(sdata,
6425 			   "Invalid advertised T2L mapping presence indicator\n");
6426 		return -EINVAL;
6427 	}
6428 
6429 	ttlm_info->map = ieee80211_get_ttlm(map_size, pos);
6430 	if (!ttlm_info->map) {
6431 		sdata_info(sdata,
6432 			   "Invalid advertised T2L map for TID 0\n");
6433 		return -EINVAL;
6434 	}
6435 
6436 	pos += map_size;
6437 
6438 	for (tid = 1; tid < 8; tid++) {
6439 		u16 map = ieee80211_get_ttlm(map_size, pos);
6440 
6441 		if (map != ttlm_info->map) {
6442 			sdata_info(sdata, "Invalid advertised T2L map for tid %d\n",
6443 				   tid);
6444 			return -EINVAL;
6445 		}
6446 
6447 		pos += map_size;
6448 	}
6449 	return 0;
6450 }
6451 
6452 static void ieee80211_process_adv_ttlm(struct ieee80211_sub_if_data *sdata,
6453 					  struct ieee802_11_elems *elems,
6454 					  u64 beacon_ts)
6455 {
6456 	u8 i;
6457 	int ret;
6458 
6459 	if (!ieee80211_vif_is_mld(&sdata->vif))
6460 		return;
6461 
6462 	if (!elems->ttlm_num) {
6463 		if (sdata->u.mgd.ttlm_info.switch_time) {
6464 			/* if a planned TID-to-link mapping was cancelled -
6465 			 * abort it
6466 			 */
6467 			wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
6468 						  &sdata->u.mgd.ttlm_work);
6469 		} else if (sdata->u.mgd.ttlm_info.active) {
6470 			/* if no TID-to-link element, set to default mapping in
6471 			 * which all TIDs are mapped to all setup links
6472 			 */
6473 			ret = ieee80211_vif_set_links(sdata,
6474 						      sdata->vif.valid_links,
6475 						      0);
6476 			if (ret) {
6477 				sdata_info(sdata, "Failed setting valid/dormant links\n");
6478 				return;
6479 			}
6480 			ieee80211_vif_cfg_change_notify(sdata,
6481 							BSS_CHANGED_MLD_VALID_LINKS);
6482 		}
6483 		memset(&sdata->u.mgd.ttlm_info, 0,
6484 		       sizeof(sdata->u.mgd.ttlm_info));
6485 		return;
6486 	}
6487 
6488 	for (i = 0; i < elems->ttlm_num; i++) {
6489 		struct ieee80211_adv_ttlm_info ttlm_info;
6490 		u32 res;
6491 
6492 		res = ieee80211_parse_adv_t2l(sdata, elems->ttlm[i],
6493 					      &ttlm_info);
6494 
6495 		if (res) {
6496 			__ieee80211_disconnect(sdata);
6497 			return;
6498 		}
6499 
6500 		if (ttlm_info.switch_time) {
6501 			u16 beacon_ts_tu, st_tu, delay;
6502 			u32 delay_jiffies;
6503 			u64 mask;
6504 
6505 			/* The t2l map switch time is indicated with a partial
6506 			 * TSF value (bits 10 to 25), get the partial beacon TS
6507 			 * as well, and calc the delay to the start time.
6508 			 */
6509 			mask = GENMASK_ULL(25, 10);
6510 			beacon_ts_tu = (beacon_ts & mask) >> 10;
6511 			st_tu = ttlm_info.switch_time;
6512 			delay = st_tu - beacon_ts_tu;
6513 
6514 			/*
6515 			 * If the switch time is far in the future, then it
6516 			 * could also be the previous switch still being
6517 			 * announced.
6518 			 * We can simply ignore it for now, if it is a future
6519 			 * switch the AP will continue to announce it anyway.
6520 			 */
6521 			if (delay > IEEE80211_ADV_TTLM_ST_UNDERFLOW)
6522 				return;
6523 
6524 			delay_jiffies = TU_TO_JIFFIES(delay);
6525 
6526 			/* Link switching can take time, so schedule it
6527 			 * 100ms before to be ready on time
6528 			 */
6529 			if (delay_jiffies > IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS)
6530 				delay_jiffies -=
6531 					IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS;
6532 			else
6533 				delay_jiffies = 0;
6534 
6535 			sdata->u.mgd.ttlm_info = ttlm_info;
6536 			wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
6537 						  &sdata->u.mgd.ttlm_work);
6538 			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
6539 						 &sdata->u.mgd.ttlm_work,
6540 						 delay_jiffies);
6541 			return;
6542 		}
6543 	}
6544 }
6545 
6546 static void
6547 ieee80211_mgd_check_cross_link_csa(struct ieee80211_sub_if_data *sdata,
6548 				   int reporting_link_id,
6549 				   struct ieee802_11_elems *elems)
6550 {
6551 	const struct element *sta_profiles[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6552 	ssize_t sta_profiles_len[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6553 	const struct element *sub;
6554 	const u8 *subelems;
6555 	size_t subelems_len;
6556 	u8 common_size;
6557 	int link_id;
6558 
6559 	if (!ieee80211_mle_size_ok((u8 *)elems->ml_basic, elems->ml_basic_len))
6560 		return;
6561 
6562 	common_size = ieee80211_mle_common_size((u8 *)elems->ml_basic);
6563 	subelems = (u8 *)elems->ml_basic + common_size;
6564 	subelems_len = elems->ml_basic_len - common_size;
6565 
6566 	for_each_element_id(sub, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
6567 			    subelems, subelems_len) {
6568 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
6569 		struct ieee80211_link_data *link;
6570 		ssize_t len;
6571 
6572 		if (!ieee80211_mle_basic_sta_prof_size_ok(sub->data,
6573 							  sub->datalen))
6574 			continue;
6575 
6576 		link_id = le16_get_bits(prof->control,
6577 					IEEE80211_MLE_STA_CONTROL_LINK_ID);
6578 		/* need a valid link ID, but also not our own, both AP bugs */
6579 		if (link_id == reporting_link_id ||
6580 		    link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
6581 			continue;
6582 
6583 		link = sdata_dereference(sdata->link[link_id], sdata);
6584 		if (!link)
6585 			continue;
6586 
6587 		len = cfg80211_defragment_element(sub, subelems, subelems_len,
6588 						  NULL, 0,
6589 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
6590 		if (WARN_ON(len < 0))
6591 			continue;
6592 
6593 		sta_profiles[link_id] = sub;
6594 		sta_profiles_len[link_id] = len;
6595 	}
6596 
6597 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
6598 		struct ieee80211_mle_per_sta_profile *prof;
6599 		struct ieee802_11_elems *prof_elems;
6600 		struct ieee80211_link_data *link;
6601 		ssize_t len;
6602 
6603 		if (link_id == reporting_link_id)
6604 			continue;
6605 
6606 		link = sdata_dereference(sdata->link[link_id], sdata);
6607 		if (!link)
6608 			continue;
6609 
6610 		if (!sta_profiles[link_id]) {
6611 			prof_elems = NULL;
6612 			goto handle;
6613 		}
6614 
6615 		/* we can defragment in-place, won't use the buffer again */
6616 		len = cfg80211_defragment_element(sta_profiles[link_id],
6617 						  subelems, subelems_len,
6618 						  (void *)sta_profiles[link_id],
6619 						  sta_profiles_len[link_id],
6620 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
6621 		if (WARN_ON(len != sta_profiles_len[link_id]))
6622 			continue;
6623 
6624 		prof = (void *)sta_profiles[link_id];
6625 		prof_elems = ieee802_11_parse_elems(prof->variable +
6626 						    (prof->sta_info_len - 1),
6627 						    len -
6628 						    (prof->sta_info_len - 1),
6629 						    false, NULL);
6630 
6631 		/* memory allocation failed - let's hope that's transient */
6632 		if (!prof_elems)
6633 			continue;
6634 
6635 handle:
6636 		/*
6637 		 * FIXME: the timings here are obviously incorrect,
6638 		 * but only older Intel drivers seem to care, and
6639 		 * those don't have MLO. If you really need this,
6640 		 * the problem is having to calculate it with the
6641 		 * TSF offset etc. The device_timestamp is still
6642 		 * correct, of course.
6643 		 */
6644 		ieee80211_sta_process_chanswitch(link, 0, 0, elems, prof_elems,
6645 						 IEEE80211_CSA_SOURCE_OTHER_LINK);
6646 		kfree(prof_elems);
6647 	}
6648 }
6649 
6650 static bool ieee80211_mgd_ssid_mismatch(struct ieee80211_sub_if_data *sdata,
6651 					const struct ieee802_11_elems *elems)
6652 {
6653 	struct ieee80211_vif_cfg *cfg = &sdata->vif.cfg;
6654 	static u8 zero_ssid[IEEE80211_MAX_SSID_LEN];
6655 
6656 	if (!elems->ssid)
6657 		return false;
6658 
6659 	/* hidden SSID: zero length */
6660 	if (elems->ssid_len == 0)
6661 		return false;
6662 
6663 	if (elems->ssid_len != cfg->ssid_len)
6664 		return true;
6665 
6666 	/* hidden SSID: zeroed out */
6667 	if (!memcmp(elems->ssid, zero_ssid, elems->ssid_len))
6668 		return false;
6669 
6670 	return memcmp(elems->ssid, cfg->ssid, cfg->ssid_len);
6671 }
6672 
6673 static void ieee80211_rx_mgmt_beacon(struct ieee80211_link_data *link,
6674 				     struct ieee80211_hdr *hdr, size_t len,
6675 				     struct ieee80211_rx_status *rx_status)
6676 {
6677 	struct ieee80211_sub_if_data *sdata = link->sdata;
6678 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6679 	struct ieee80211_bss_conf *bss_conf = link->conf;
6680 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
6681 	struct ieee80211_mgmt *mgmt = (void *) hdr;
6682 	size_t baselen;
6683 	struct ieee802_11_elems *elems;
6684 	struct ieee80211_local *local = sdata->local;
6685 	struct ieee80211_chanctx_conf *chanctx_conf;
6686 	struct ieee80211_supported_band *sband;
6687 	struct ieee80211_channel *chan;
6688 	struct link_sta_info *link_sta;
6689 	struct sta_info *sta;
6690 	u64 changed = 0;
6691 	bool erp_valid;
6692 	u8 erp_value = 0;
6693 	u32 ncrc = 0;
6694 	u8 *bssid, *variable = mgmt->u.beacon.variable;
6695 	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
6696 	struct ieee80211_elems_parse_params parse_params = {
6697 		.mode = link->u.mgd.conn.mode,
6698 		.link_id = -1,
6699 		.from_ap = true,
6700 	};
6701 
6702 	lockdep_assert_wiphy(local->hw.wiphy);
6703 
6704 	/* Process beacon from the current BSS */
6705 	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
6706 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
6707 		struct ieee80211_ext *ext = (void *) mgmt;
6708 
6709 		if (ieee80211_is_s1g_short_beacon(ext->frame_control))
6710 			variable = ext->u.s1g_short_beacon.variable;
6711 		else
6712 			variable = ext->u.s1g_beacon.variable;
6713 	}
6714 
6715 	baselen = (u8 *) variable - (u8 *) mgmt;
6716 	if (baselen > len)
6717 		return;
6718 
6719 	parse_params.start = variable;
6720 	parse_params.len = len - baselen;
6721 
6722 	rcu_read_lock();
6723 	chanctx_conf = rcu_dereference(bss_conf->chanctx_conf);
6724 	if (!chanctx_conf) {
6725 		rcu_read_unlock();
6726 		return;
6727 	}
6728 
6729 	if (ieee80211_rx_status_to_khz(rx_status) !=
6730 	    ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
6731 		rcu_read_unlock();
6732 		return;
6733 	}
6734 	chan = chanctx_conf->def.chan;
6735 	rcu_read_unlock();
6736 
6737 	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
6738 	    !WARN_ON(ieee80211_vif_is_mld(&sdata->vif)) &&
6739 	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->link[0].bss)) {
6740 		parse_params.bss = ifmgd->assoc_data->link[0].bss;
6741 		elems = ieee802_11_parse_elems_full(&parse_params);
6742 		if (!elems)
6743 			return;
6744 
6745 		ieee80211_rx_bss_info(link, mgmt, len, rx_status);
6746 
6747 		if (elems->dtim_period)
6748 			link->u.mgd.dtim_period = elems->dtim_period;
6749 		link->u.mgd.have_beacon = true;
6750 		ifmgd->assoc_data->need_beacon = false;
6751 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
6752 		    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
6753 			bss_conf->sync_tsf =
6754 				le64_to_cpu(mgmt->u.beacon.timestamp);
6755 			bss_conf->sync_device_ts =
6756 				rx_status->device_timestamp;
6757 			bss_conf->sync_dtim_count = elems->dtim_count;
6758 		}
6759 
6760 		if (elems->mbssid_config_ie)
6761 			bss_conf->profile_periodicity =
6762 				elems->mbssid_config_ie->profile_periodicity;
6763 		else
6764 			bss_conf->profile_periodicity = 0;
6765 
6766 		if (elems->ext_capab_len >= 11 &&
6767 		    (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
6768 			bss_conf->ema_ap = true;
6769 		else
6770 			bss_conf->ema_ap = false;
6771 
6772 		/* continue assoc process */
6773 		ifmgd->assoc_data->timeout = jiffies;
6774 		ifmgd->assoc_data->timeout_started = true;
6775 		run_again(sdata, ifmgd->assoc_data->timeout);
6776 		kfree(elems);
6777 		return;
6778 	}
6779 
6780 	if (!ifmgd->associated ||
6781 	    !ieee80211_rx_our_beacon(bssid, bss_conf->bss))
6782 		return;
6783 	bssid = link->u.mgd.bssid;
6784 
6785 	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
6786 		ieee80211_handle_beacon_sig(link, ifmgd, bss_conf,
6787 					    local, rx_status);
6788 
6789 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
6790 		mlme_dbg_ratelimited(sdata,
6791 				     "cancelling AP probe due to a received beacon\n");
6792 		ieee80211_reset_ap_probe(sdata);
6793 	}
6794 
6795 	/*
6796 	 * Push the beacon loss detection into the future since
6797 	 * we are processing a beacon from the AP just now.
6798 	 */
6799 	ieee80211_sta_reset_beacon_monitor(sdata);
6800 
6801 	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
6802 	 * element (which carries the beacon interval). Don't forget to add a
6803 	 * bit to care_about_ies[] above if mac80211 is interested in a
6804 	 * changing S1G element.
6805 	 */
6806 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
6807 		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
6808 	parse_params.bss = bss_conf->bss;
6809 	parse_params.filter = care_about_ies;
6810 	parse_params.crc = ncrc;
6811 	elems = ieee802_11_parse_elems_full(&parse_params);
6812 	if (!elems)
6813 		return;
6814 
6815 	if (rx_status->flag & RX_FLAG_DECRYPTED &&
6816 	    ieee80211_mgd_ssid_mismatch(sdata, elems)) {
6817 		sdata_info(sdata, "SSID mismatch for AP %pM, disconnect\n",
6818 			   sdata->vif.cfg.ap_addr);
6819 		__ieee80211_disconnect(sdata);
6820 		return;
6821 	}
6822 
6823 	ncrc = elems->crc;
6824 
6825 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
6826 	    ieee80211_check_tim(elems->tim, elems->tim_len, vif_cfg->aid)) {
6827 		if (local->hw.conf.dynamic_ps_timeout > 0) {
6828 			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
6829 				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
6830 				ieee80211_hw_config(local,
6831 						    IEEE80211_CONF_CHANGE_PS);
6832 			}
6833 			ieee80211_send_nullfunc(local, sdata, false);
6834 		} else if (!local->pspolling && sdata->u.mgd.powersave) {
6835 			local->pspolling = true;
6836 
6837 			/*
6838 			 * Here is assumed that the driver will be
6839 			 * able to send ps-poll frame and receive a
6840 			 * response even though power save mode is
6841 			 * enabled, but some drivers might require
6842 			 * to disable power save here. This needs
6843 			 * to be investigated.
6844 			 */
6845 			ieee80211_send_pspoll(local, sdata);
6846 		}
6847 	}
6848 
6849 	if (sdata->vif.p2p ||
6850 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
6851 		struct ieee80211_p2p_noa_attr noa = {};
6852 		int ret;
6853 
6854 		ret = cfg80211_get_p2p_attr(variable,
6855 					    len - baselen,
6856 					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
6857 					    (u8 *) &noa, sizeof(noa));
6858 		if (ret >= 2) {
6859 			if (link->u.mgd.p2p_noa_index != noa.index) {
6860 				/* valid noa_attr and index changed */
6861 				link->u.mgd.p2p_noa_index = noa.index;
6862 				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
6863 				changed |= BSS_CHANGED_P2P_PS;
6864 				/*
6865 				 * make sure we update all information, the CRC
6866 				 * mechanism doesn't look at P2P attributes.
6867 				 */
6868 				link->u.mgd.beacon_crc_valid = false;
6869 			}
6870 		} else if (link->u.mgd.p2p_noa_index != -1) {
6871 			/* noa_attr not found and we had valid noa_attr before */
6872 			link->u.mgd.p2p_noa_index = -1;
6873 			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
6874 			changed |= BSS_CHANGED_P2P_PS;
6875 			link->u.mgd.beacon_crc_valid = false;
6876 		}
6877 	}
6878 
6879 	/*
6880 	 * Update beacon timing and dtim count on every beacon appearance. This
6881 	 * will allow the driver to use the most updated values. Do it before
6882 	 * comparing this one with last received beacon.
6883 	 * IMPORTANT: These parameters would possibly be out of sync by the time
6884 	 * the driver will use them. The synchronized view is currently
6885 	 * guaranteed only in certain callbacks.
6886 	 */
6887 	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
6888 	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
6889 		bss_conf->sync_tsf =
6890 			le64_to_cpu(mgmt->u.beacon.timestamp);
6891 		bss_conf->sync_device_ts =
6892 			rx_status->device_timestamp;
6893 		bss_conf->sync_dtim_count = elems->dtim_count;
6894 	}
6895 
6896 	if ((ncrc == link->u.mgd.beacon_crc && link->u.mgd.beacon_crc_valid) ||
6897 	    ieee80211_is_s1g_short_beacon(mgmt->frame_control))
6898 		goto free;
6899 	link->u.mgd.beacon_crc = ncrc;
6900 	link->u.mgd.beacon_crc_valid = true;
6901 
6902 	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
6903 
6904 	ieee80211_sta_process_chanswitch(link, rx_status->mactime,
6905 					 rx_status->device_timestamp,
6906 					 elems, elems,
6907 					 IEEE80211_CSA_SOURCE_BEACON);
6908 
6909 	/* note that after this elems->ml_basic can no longer be used fully */
6910 	ieee80211_mgd_check_cross_link_csa(sdata, rx_status->link_id, elems);
6911 
6912 	if (!link->u.mgd.disable_wmm_tracking &&
6913 	    ieee80211_sta_wmm_params(local, link, elems->wmm_param,
6914 				     elems->wmm_param_len,
6915 				     elems->mu_edca_param_set))
6916 		changed |= BSS_CHANGED_QOS;
6917 
6918 	/*
6919 	 * If we haven't had a beacon before, tell the driver about the
6920 	 * DTIM period (and beacon timing if desired) now.
6921 	 */
6922 	if (!link->u.mgd.have_beacon) {
6923 		/* a few bogus AP send dtim_period = 0 or no TIM IE */
6924 		bss_conf->dtim_period = elems->dtim_period ?: 1;
6925 
6926 		changed |= BSS_CHANGED_BEACON_INFO;
6927 		link->u.mgd.have_beacon = true;
6928 
6929 		ieee80211_recalc_ps(local);
6930 
6931 		ieee80211_recalc_ps_vif(sdata);
6932 	}
6933 
6934 	if (elems->erp_info) {
6935 		erp_valid = true;
6936 		erp_value = elems->erp_info[0];
6937 	} else {
6938 		erp_valid = false;
6939 	}
6940 
6941 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
6942 		changed |= ieee80211_handle_bss_capability(link,
6943 				le16_to_cpu(mgmt->u.beacon.capab_info),
6944 				erp_valid, erp_value);
6945 
6946 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
6947 	if (WARN_ON(!sta)) {
6948 		goto free;
6949 	}
6950 	link_sta = rcu_dereference_protected(sta->link[link->link_id],
6951 					     lockdep_is_held(&local->hw.wiphy->mtx));
6952 	if (WARN_ON(!link_sta)) {
6953 		goto free;
6954 	}
6955 
6956 	if (WARN_ON(!bss_conf->chanreq.oper.chan))
6957 		goto free;
6958 
6959 	sband = local->hw.wiphy->bands[bss_conf->chanreq.oper.chan->band];
6960 
6961 	changed |= ieee80211_recalc_twt_req(sdata, sband, link, link_sta, elems);
6962 
6963 	if (ieee80211_config_bw(link, elems, true, &changed)) {
6964 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
6965 				       WLAN_REASON_DEAUTH_LEAVING,
6966 				       true, deauth_buf);
6967 		ieee80211_report_disconnect(sdata, deauth_buf,
6968 					    sizeof(deauth_buf), true,
6969 					    WLAN_REASON_DEAUTH_LEAVING,
6970 					    false);
6971 		goto free;
6972 	}
6973 
6974 	if (elems->opmode_notif)
6975 		ieee80211_vht_handle_opmode(sdata, link_sta,
6976 					    *elems->opmode_notif,
6977 					    rx_status->band);
6978 
6979 	changed |= ieee80211_handle_pwr_constr(link, chan, mgmt,
6980 					       elems->country_elem,
6981 					       elems->country_elem_len,
6982 					       elems->pwr_constr_elem,
6983 					       elems->cisco_dtpc_elem);
6984 
6985 	ieee80211_ml_reconfiguration(sdata, elems);
6986 	ieee80211_process_adv_ttlm(sdata, elems,
6987 				      le64_to_cpu(mgmt->u.beacon.timestamp));
6988 
6989 	ieee80211_link_info_change_notify(sdata, link, changed);
6990 free:
6991 	kfree(elems);
6992 }
6993 
6994 static void ieee80211_apply_neg_ttlm(struct ieee80211_sub_if_data *sdata,
6995 				     struct ieee80211_neg_ttlm neg_ttlm)
6996 {
6997 	u16 new_active_links, new_dormant_links, new_suspended_links, map = 0;
6998 	u8 i;
6999 
7000 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++)
7001 		map |= neg_ttlm.downlink[i] | neg_ttlm.uplink[i];
7002 
7003 	/* If there is an active TTLM, unset previously suspended links */
7004 	if (sdata->vif.neg_ttlm.valid)
7005 		sdata->vif.dormant_links &= ~sdata->vif.suspended_links;
7006 
7007 	/* exclude links that are already disabled by advertised TTLM */
7008 	new_active_links =
7009 		map & sdata->vif.valid_links & ~sdata->vif.dormant_links;
7010 	new_suspended_links =
7011 		(~map & sdata->vif.valid_links) & ~sdata->vif.dormant_links;
7012 	new_dormant_links = sdata->vif.dormant_links | new_suspended_links;
7013 	if (ieee80211_ttlm_set_links(sdata, new_active_links,
7014 				     new_dormant_links, new_suspended_links))
7015 		return;
7016 
7017 	sdata->vif.neg_ttlm = neg_ttlm;
7018 	sdata->vif.neg_ttlm.valid = true;
7019 }
7020 
7021 static void ieee80211_neg_ttlm_timeout_work(struct wiphy *wiphy,
7022 					    struct wiphy_work *work)
7023 {
7024 	struct ieee80211_sub_if_data *sdata =
7025 		container_of(work, struct ieee80211_sub_if_data,
7026 			     u.mgd.neg_ttlm_timeout_work.work);
7027 
7028 	sdata_info(sdata,
7029 		   "No negotiated TTLM response from AP, disconnecting.\n");
7030 
7031 	__ieee80211_disconnect(sdata);
7032 }
7033 
7034 static void
7035 ieee80211_neg_ttlm_add_suggested_map(struct sk_buff *skb,
7036 				     struct ieee80211_neg_ttlm *neg_ttlm)
7037 {
7038 	u8 i, direction[IEEE80211_TTLM_MAX_CNT];
7039 
7040 	if (memcmp(neg_ttlm->downlink, neg_ttlm->uplink,
7041 		   sizeof(neg_ttlm->downlink))) {
7042 		direction[0] = IEEE80211_TTLM_DIRECTION_DOWN;
7043 		direction[1] = IEEE80211_TTLM_DIRECTION_UP;
7044 	} else {
7045 		direction[0] = IEEE80211_TTLM_DIRECTION_BOTH;
7046 	}
7047 
7048 	for (i = 0; i < ARRAY_SIZE(direction); i++) {
7049 		u8 tid, len, map_ind = 0, *len_pos, *map_ind_pos, *pos;
7050 		__le16 map;
7051 
7052 		len = sizeof(struct ieee80211_ttlm_elem) + 1 + 1;
7053 
7054 		pos = skb_put(skb, len + 2);
7055 		*pos++ = WLAN_EID_EXTENSION;
7056 		len_pos = pos++;
7057 		*pos++ = WLAN_EID_EXT_TID_TO_LINK_MAPPING;
7058 		*pos++ = direction[i];
7059 		map_ind_pos = pos++;
7060 		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
7061 			map = direction[i] == IEEE80211_TTLM_DIRECTION_UP ?
7062 				cpu_to_le16(neg_ttlm->uplink[tid]) :
7063 				cpu_to_le16(neg_ttlm->downlink[tid]);
7064 			if (!map)
7065 				continue;
7066 
7067 			len += 2;
7068 			map_ind |= BIT(tid);
7069 			skb_put_data(skb, &map, sizeof(map));
7070 		}
7071 
7072 		*map_ind_pos = map_ind;
7073 		*len_pos = len;
7074 
7075 		if (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH)
7076 			break;
7077 	}
7078 }
7079 
7080 static void
7081 ieee80211_send_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
7082 			    struct ieee80211_neg_ttlm *neg_ttlm,
7083 			    u8 dialog_token)
7084 {
7085 	struct ieee80211_local *local = sdata->local;
7086 	struct ieee80211_mgmt *mgmt;
7087 	struct sk_buff *skb;
7088 	int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.ttlm_req);
7089 	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
7090 		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
7091 
7092 	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
7093 	if (!skb)
7094 		return;
7095 
7096 	skb_reserve(skb, local->tx_headroom);
7097 	mgmt = skb_put_zero(skb, hdr_len);
7098 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
7099 					  IEEE80211_STYPE_ACTION);
7100 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
7101 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
7102 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7103 
7104 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
7105 	mgmt->u.action.u.ttlm_req.action_code =
7106 		WLAN_PROTECTED_EHT_ACTION_TTLM_REQ;
7107 	mgmt->u.action.u.ttlm_req.dialog_token = dialog_token;
7108 	ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
7109 	ieee80211_tx_skb(sdata, skb);
7110 }
7111 
7112 int ieee80211_req_neg_ttlm(struct ieee80211_sub_if_data *sdata,
7113 			   struct cfg80211_ttlm_params *params)
7114 {
7115 	struct ieee80211_neg_ttlm neg_ttlm = {};
7116 	u8 i;
7117 
7118 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
7119 	    !(sdata->vif.cfg.mld_capa_op &
7120 	      IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP))
7121 		return -EINVAL;
7122 
7123 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
7124 		if ((params->dlink[i] & ~sdata->vif.valid_links) ||
7125 		    (params->ulink[i] & ~sdata->vif.valid_links))
7126 			return -EINVAL;
7127 
7128 		neg_ttlm.downlink[i] = params->dlink[i];
7129 		neg_ttlm.uplink[i] = params->ulink[i];
7130 	}
7131 
7132 	if (drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm) !=
7133 	    NEG_TTLM_RES_ACCEPT)
7134 		return -EINVAL;
7135 
7136 	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
7137 	sdata->u.mgd.dialog_token_alloc++;
7138 	ieee80211_send_neg_ttlm_req(sdata, &sdata->vif.neg_ttlm,
7139 				    sdata->u.mgd.dialog_token_alloc);
7140 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
7141 				  &sdata->u.mgd.neg_ttlm_timeout_work);
7142 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
7143 				 &sdata->u.mgd.neg_ttlm_timeout_work,
7144 				 IEEE80211_NEG_TTLM_REQ_TIMEOUT);
7145 	return 0;
7146 }
7147 
7148 static void
7149 ieee80211_send_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
7150 			    enum ieee80211_neg_ttlm_res ttlm_res,
7151 			    u8 dialog_token,
7152 			    struct ieee80211_neg_ttlm *neg_ttlm)
7153 {
7154 	struct ieee80211_local *local = sdata->local;
7155 	struct ieee80211_mgmt *mgmt;
7156 	struct sk_buff *skb;
7157 	int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.ttlm_res);
7158 	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
7159 		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
7160 
7161 	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
7162 	if (!skb)
7163 		return;
7164 
7165 	skb_reserve(skb, local->tx_headroom);
7166 	mgmt = skb_put_zero(skb, hdr_len);
7167 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
7168 					  IEEE80211_STYPE_ACTION);
7169 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
7170 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
7171 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7172 
7173 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
7174 	mgmt->u.action.u.ttlm_res.action_code =
7175 		WLAN_PROTECTED_EHT_ACTION_TTLM_RES;
7176 	mgmt->u.action.u.ttlm_res.dialog_token = dialog_token;
7177 	switch (ttlm_res) {
7178 	default:
7179 		WARN_ON(1);
7180 		fallthrough;
7181 	case NEG_TTLM_RES_REJECT:
7182 		mgmt->u.action.u.ttlm_res.status_code =
7183 			WLAN_STATUS_DENIED_TID_TO_LINK_MAPPING;
7184 		break;
7185 	case NEG_TTLM_RES_ACCEPT:
7186 		mgmt->u.action.u.ttlm_res.status_code = WLAN_STATUS_SUCCESS;
7187 		break;
7188 	case NEG_TTLM_RES_SUGGEST_PREFERRED:
7189 		mgmt->u.action.u.ttlm_res.status_code =
7190 			WLAN_STATUS_PREF_TID_TO_LINK_MAPPING_SUGGESTED;
7191 		ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
7192 		break;
7193 	}
7194 
7195 	ieee80211_tx_skb(sdata, skb);
7196 }
7197 
7198 static int
7199 ieee80211_parse_neg_ttlm(struct ieee80211_sub_if_data *sdata,
7200 			 const struct ieee80211_ttlm_elem *ttlm,
7201 			 struct ieee80211_neg_ttlm *neg_ttlm,
7202 			 u8 *direction)
7203 {
7204 	u8 control, link_map_presence, map_size, tid;
7205 	u8 *pos;
7206 
7207 	/* The element size was already validated in
7208 	 * ieee80211_tid_to_link_map_size_ok()
7209 	 */
7210 	pos = (void *)ttlm->optional;
7211 
7212 	control = ttlm->control;
7213 
7214 	/* mapping switch time and expected duration fields are not expected
7215 	 * in case of negotiated TTLM
7216 	 */
7217 	if (control & (IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT |
7218 		       IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT)) {
7219 		mlme_dbg(sdata,
7220 			 "Invalid TTLM element in negotiated TTLM request\n");
7221 		return -EINVAL;
7222 	}
7223 
7224 	if (control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) {
7225 		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
7226 			neg_ttlm->downlink[tid] = sdata->vif.valid_links;
7227 			neg_ttlm->uplink[tid] = sdata->vif.valid_links;
7228 		}
7229 		*direction = IEEE80211_TTLM_DIRECTION_BOTH;
7230 		return 0;
7231 	}
7232 
7233 	*direction = u8_get_bits(control, IEEE80211_TTLM_CONTROL_DIRECTION);
7234 	if (*direction != IEEE80211_TTLM_DIRECTION_DOWN &&
7235 	    *direction != IEEE80211_TTLM_DIRECTION_UP &&
7236 	    *direction != IEEE80211_TTLM_DIRECTION_BOTH)
7237 		return -EINVAL;
7238 
7239 	link_map_presence = *pos;
7240 	pos++;
7241 
7242 	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
7243 		map_size = 1;
7244 	else
7245 		map_size = 2;
7246 
7247 	for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
7248 		u16 map;
7249 
7250 		if (link_map_presence & BIT(tid)) {
7251 			map = ieee80211_get_ttlm(map_size, pos);
7252 			if (!map) {
7253 				mlme_dbg(sdata,
7254 					 "No active links for TID %d", tid);
7255 				return -EINVAL;
7256 			}
7257 		} else {
7258 			map = 0;
7259 		}
7260 
7261 		switch (*direction) {
7262 		case IEEE80211_TTLM_DIRECTION_BOTH:
7263 			neg_ttlm->downlink[tid] = map;
7264 			neg_ttlm->uplink[tid] = map;
7265 			break;
7266 		case IEEE80211_TTLM_DIRECTION_DOWN:
7267 			neg_ttlm->downlink[tid] = map;
7268 			break;
7269 		case IEEE80211_TTLM_DIRECTION_UP:
7270 			neg_ttlm->uplink[tid] = map;
7271 			break;
7272 		default:
7273 			return -EINVAL;
7274 		}
7275 		pos += map_size;
7276 	}
7277 	return 0;
7278 }
7279 
7280 void ieee80211_process_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
7281 				    struct ieee80211_mgmt *mgmt, size_t len)
7282 {
7283 	u8 dialog_token, direction[IEEE80211_TTLM_MAX_CNT] = {}, i;
7284 	size_t ies_len;
7285 	enum ieee80211_neg_ttlm_res ttlm_res = NEG_TTLM_RES_ACCEPT;
7286 	struct ieee802_11_elems *elems = NULL;
7287 	struct ieee80211_neg_ttlm neg_ttlm = {};
7288 
7289 	BUILD_BUG_ON(ARRAY_SIZE(direction) != ARRAY_SIZE(elems->ttlm));
7290 
7291 	if (!ieee80211_vif_is_mld(&sdata->vif))
7292 		return;
7293 
7294 	dialog_token = mgmt->u.action.u.ttlm_req.dialog_token;
7295 	ies_len  = len - offsetof(struct ieee80211_mgmt,
7296 				  u.action.u.ttlm_req.variable);
7297 	elems = ieee802_11_parse_elems(mgmt->u.action.u.ttlm_req.variable,
7298 				       ies_len, true, NULL);
7299 	if (!elems) {
7300 		ttlm_res = NEG_TTLM_RES_REJECT;
7301 		goto out;
7302 	}
7303 
7304 	for (i = 0; i < elems->ttlm_num; i++) {
7305 		if (ieee80211_parse_neg_ttlm(sdata, elems->ttlm[i],
7306 					     &neg_ttlm, &direction[i]) ||
7307 		    (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH &&
7308 		     elems->ttlm_num != 1)) {
7309 			ttlm_res = NEG_TTLM_RES_REJECT;
7310 			goto out;
7311 		}
7312 	}
7313 
7314 	if (!elems->ttlm_num ||
7315 	    (elems->ttlm_num == 2 && direction[0] == direction[1])) {
7316 		ttlm_res = NEG_TTLM_RES_REJECT;
7317 		goto out;
7318 	}
7319 
7320 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
7321 		if ((neg_ttlm.downlink[i] &&
7322 		     (neg_ttlm.downlink[i] & ~sdata->vif.valid_links)) ||
7323 		    (neg_ttlm.uplink[i] &&
7324 		     (neg_ttlm.uplink[i] & ~sdata->vif.valid_links))) {
7325 			ttlm_res = NEG_TTLM_RES_REJECT;
7326 			goto out;
7327 		}
7328 	}
7329 
7330 	ttlm_res = drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm);
7331 
7332 	if (ttlm_res != NEG_TTLM_RES_ACCEPT)
7333 		goto out;
7334 
7335 	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
7336 out:
7337 	kfree(elems);
7338 	ieee80211_send_neg_ttlm_res(sdata, ttlm_res, dialog_token, &neg_ttlm);
7339 }
7340 
7341 void ieee80211_process_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
7342 				    struct ieee80211_mgmt *mgmt, size_t len)
7343 {
7344 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
7345 	    mgmt->u.action.u.ttlm_req.dialog_token !=
7346 	    sdata->u.mgd.dialog_token_alloc)
7347 		return;
7348 
7349 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
7350 				  &sdata->u.mgd.neg_ttlm_timeout_work);
7351 
7352 	/* MLD station sends a TID to link mapping request, mainly to handle
7353 	 * BTM (BSS transition management) request, in which case it needs to
7354 	 * restrict the active links set.
7355 	 * In this case it's not expected that the MLD AP will reject the
7356 	 * negotiated TTLM request.
7357 	 * This can be better implemented in the future, to handle request
7358 	 * rejections.
7359 	 */
7360 	if (mgmt->u.action.u.ttlm_res.status_code != WLAN_STATUS_SUCCESS)
7361 		__ieee80211_disconnect(sdata);
7362 }
7363 
7364 static void ieee80211_teardown_ttlm_work(struct wiphy *wiphy,
7365 					 struct wiphy_work *work)
7366 {
7367 	u16 new_dormant_links;
7368 	struct ieee80211_sub_if_data *sdata =
7369 		container_of(work, struct ieee80211_sub_if_data,
7370 			     u.mgd.teardown_ttlm_work);
7371 
7372 	if (!sdata->vif.neg_ttlm.valid)
7373 		return;
7374 
7375 	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
7376 	new_dormant_links =
7377 		sdata->vif.dormant_links & ~sdata->vif.suspended_links;
7378 	sdata->vif.suspended_links = 0;
7379 	ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
7380 				new_dormant_links);
7381 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_TTLM |
7382 					       BSS_CHANGED_MLD_VALID_LINKS);
7383 }
7384 
7385 void ieee80211_send_teardown_neg_ttlm(struct ieee80211_vif *vif)
7386 {
7387 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
7388 	struct ieee80211_local *local = sdata->local;
7389 	struct ieee80211_mgmt *mgmt;
7390 	struct sk_buff *skb;
7391 	int frame_len = offsetofend(struct ieee80211_mgmt,
7392 				  u.action.u.ttlm_tear_down);
7393 	struct ieee80211_tx_info *info;
7394 
7395 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
7396 	if (!skb)
7397 		return;
7398 
7399 	skb_reserve(skb, local->hw.extra_tx_headroom);
7400 	mgmt = skb_put_zero(skb, frame_len);
7401 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
7402 					  IEEE80211_STYPE_ACTION);
7403 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
7404 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
7405 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7406 
7407 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
7408 	mgmt->u.action.u.ttlm_tear_down.action_code =
7409 		WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN;
7410 
7411 	info = IEEE80211_SKB_CB(skb);
7412 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
7413 	info->status_data = IEEE80211_STATUS_TYPE_NEG_TTLM;
7414 	ieee80211_tx_skb(sdata, skb);
7415 }
7416 EXPORT_SYMBOL(ieee80211_send_teardown_neg_ttlm);
7417 
7418 void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
7419 				 struct sk_buff *skb)
7420 {
7421 	struct ieee80211_link_data *link = &sdata->deflink;
7422 	struct ieee80211_rx_status *rx_status;
7423 	struct ieee80211_hdr *hdr;
7424 	u16 fc;
7425 
7426 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7427 
7428 	rx_status = (struct ieee80211_rx_status *) skb->cb;
7429 	hdr = (struct ieee80211_hdr *) skb->data;
7430 	fc = le16_to_cpu(hdr->frame_control);
7431 
7432 	switch (fc & IEEE80211_FCTL_STYPE) {
7433 	case IEEE80211_STYPE_S1G_BEACON:
7434 		ieee80211_rx_mgmt_beacon(link, hdr, skb->len, rx_status);
7435 		break;
7436 	}
7437 }
7438 
7439 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
7440 				  struct sk_buff *skb)
7441 {
7442 	struct ieee80211_link_data *link = &sdata->deflink;
7443 	struct ieee80211_rx_status *rx_status;
7444 	struct ieee802_11_elems *elems;
7445 	struct ieee80211_mgmt *mgmt;
7446 	u16 fc;
7447 	int ies_len;
7448 
7449 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7450 
7451 	rx_status = (struct ieee80211_rx_status *) skb->cb;
7452 	mgmt = (struct ieee80211_mgmt *) skb->data;
7453 	fc = le16_to_cpu(mgmt->frame_control);
7454 
7455 	if (rx_status->link_valid) {
7456 		link = sdata_dereference(sdata->link[rx_status->link_id],
7457 					 sdata);
7458 		if (!link)
7459 			return;
7460 	}
7461 
7462 	switch (fc & IEEE80211_FCTL_STYPE) {
7463 	case IEEE80211_STYPE_BEACON:
7464 		ieee80211_rx_mgmt_beacon(link, (void *)mgmt,
7465 					 skb->len, rx_status);
7466 		break;
7467 	case IEEE80211_STYPE_PROBE_RESP:
7468 		ieee80211_rx_mgmt_probe_resp(link, skb);
7469 		break;
7470 	case IEEE80211_STYPE_AUTH:
7471 		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
7472 		break;
7473 	case IEEE80211_STYPE_DEAUTH:
7474 		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
7475 		break;
7476 	case IEEE80211_STYPE_DISASSOC:
7477 		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
7478 		break;
7479 	case IEEE80211_STYPE_ASSOC_RESP:
7480 	case IEEE80211_STYPE_REASSOC_RESP:
7481 		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
7482 		break;
7483 	case IEEE80211_STYPE_ACTION:
7484 		if (!sdata->u.mgd.associated ||
7485 		    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
7486 			break;
7487 
7488 		switch (mgmt->u.action.category) {
7489 		case WLAN_CATEGORY_SPECTRUM_MGMT:
7490 			ies_len = skb->len -
7491 				  offsetof(struct ieee80211_mgmt,
7492 					   u.action.u.chan_switch.variable);
7493 
7494 			if (ies_len < 0)
7495 				break;
7496 
7497 			/* CSA IE cannot be overridden, no need for BSSID */
7498 			elems = ieee802_11_parse_elems(
7499 					mgmt->u.action.u.chan_switch.variable,
7500 					ies_len, true, NULL);
7501 
7502 			if (elems && !elems->parse_error) {
7503 				enum ieee80211_csa_source src =
7504 					IEEE80211_CSA_SOURCE_PROT_ACTION;
7505 
7506 				ieee80211_sta_process_chanswitch(link,
7507 								 rx_status->mactime,
7508 								 rx_status->device_timestamp,
7509 								 elems, elems,
7510 								 src);
7511 			}
7512 			kfree(elems);
7513 			break;
7514 		case WLAN_CATEGORY_PUBLIC:
7515 		case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
7516 			ies_len = skb->len -
7517 				  offsetof(struct ieee80211_mgmt,
7518 					   u.action.u.ext_chan_switch.variable);
7519 
7520 			if (ies_len < 0)
7521 				break;
7522 
7523 			/*
7524 			 * extended CSA IE can't be overridden, no need for
7525 			 * BSSID
7526 			 */
7527 			elems = ieee802_11_parse_elems(
7528 					mgmt->u.action.u.ext_chan_switch.variable,
7529 					ies_len, true, NULL);
7530 
7531 			if (elems && !elems->parse_error) {
7532 				enum ieee80211_csa_source src;
7533 
7534 				if (mgmt->u.action.category ==
7535 						WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
7536 					src = IEEE80211_CSA_SOURCE_PROT_ACTION;
7537 				else
7538 					src = IEEE80211_CSA_SOURCE_UNPROT_ACTION;
7539 
7540 				/* for the handling code pretend it was an IE */
7541 				elems->ext_chansw_ie =
7542 					&mgmt->u.action.u.ext_chan_switch.data;
7543 
7544 				ieee80211_sta_process_chanswitch(link,
7545 								 rx_status->mactime,
7546 								 rx_status->device_timestamp,
7547 								 elems, elems,
7548 								 src);
7549 			}
7550 
7551 			kfree(elems);
7552 			break;
7553 		}
7554 		break;
7555 	}
7556 }
7557 
7558 static void ieee80211_sta_timer(struct timer_list *t)
7559 {
7560 	struct ieee80211_sub_if_data *sdata =
7561 		from_timer(sdata, t, u.mgd.timer);
7562 
7563 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
7564 }
7565 
7566 void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
7567 				   u8 reason, bool tx)
7568 {
7569 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
7570 
7571 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
7572 			       tx, frame_buf);
7573 
7574 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
7575 				    reason, false);
7576 }
7577 
7578 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
7579 {
7580 	struct ieee80211_local *local = sdata->local;
7581 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7582 	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
7583 	u32 tx_flags = 0;
7584 	u16 trans = 1;
7585 	u16 status = 0;
7586 	struct ieee80211_prep_tx_info info = {
7587 		.subtype = IEEE80211_STYPE_AUTH,
7588 	};
7589 
7590 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7591 
7592 	if (WARN_ON_ONCE(!auth_data))
7593 		return -EINVAL;
7594 
7595 	auth_data->tries++;
7596 
7597 	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
7598 		sdata_info(sdata, "authentication with %pM timed out\n",
7599 			   auth_data->ap_addr);
7600 
7601 		/*
7602 		 * Most likely AP is not in the range so remove the
7603 		 * bss struct for that AP.
7604 		 */
7605 		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
7606 
7607 		return -ETIMEDOUT;
7608 	}
7609 
7610 	if (auth_data->algorithm == WLAN_AUTH_SAE)
7611 		info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
7612 
7613 	info.link_id = auth_data->link_id;
7614 	drv_mgd_prepare_tx(local, sdata, &info);
7615 
7616 	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
7617 		   auth_data->ap_addr, auth_data->tries,
7618 		   IEEE80211_AUTH_MAX_TRIES);
7619 
7620 	auth_data->expected_transaction = 2;
7621 
7622 	if (auth_data->algorithm == WLAN_AUTH_SAE) {
7623 		trans = auth_data->sae_trans;
7624 		status = auth_data->sae_status;
7625 		auth_data->expected_transaction = trans;
7626 	}
7627 
7628 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
7629 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
7630 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
7631 
7632 	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
7633 			    auth_data->data, auth_data->data_len,
7634 			    auth_data->ap_addr, auth_data->ap_addr,
7635 			    NULL, 0, 0, tx_flags);
7636 
7637 	if (tx_flags == 0) {
7638 		if (auth_data->algorithm == WLAN_AUTH_SAE)
7639 			auth_data->timeout = jiffies +
7640 				IEEE80211_AUTH_TIMEOUT_SAE;
7641 		else
7642 			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
7643 	} else {
7644 		auth_data->timeout =
7645 			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
7646 	}
7647 
7648 	auth_data->timeout_started = true;
7649 	run_again(sdata, auth_data->timeout);
7650 
7651 	return 0;
7652 }
7653 
7654 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
7655 {
7656 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
7657 	struct ieee80211_local *local = sdata->local;
7658 	int ret;
7659 
7660 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7661 
7662 	assoc_data->tries++;
7663 	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
7664 		sdata_info(sdata, "association with %pM timed out\n",
7665 			   assoc_data->ap_addr);
7666 
7667 		/*
7668 		 * Most likely AP is not in the range so remove the
7669 		 * bss struct for that AP.
7670 		 */
7671 		cfg80211_unlink_bss(local->hw.wiphy,
7672 				    assoc_data->link[assoc_data->assoc_link_id].bss);
7673 
7674 		return -ETIMEDOUT;
7675 	}
7676 
7677 	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
7678 		   assoc_data->ap_addr, assoc_data->tries,
7679 		   IEEE80211_ASSOC_MAX_TRIES);
7680 	ret = ieee80211_send_assoc(sdata);
7681 	if (ret)
7682 		return ret;
7683 
7684 	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
7685 		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
7686 		assoc_data->timeout_started = true;
7687 		run_again(sdata, assoc_data->timeout);
7688 	} else {
7689 		assoc_data->timeout =
7690 			round_jiffies_up(jiffies +
7691 					 IEEE80211_ASSOC_TIMEOUT_LONG);
7692 		assoc_data->timeout_started = true;
7693 		run_again(sdata, assoc_data->timeout);
7694 	}
7695 
7696 	return 0;
7697 }
7698 
7699 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
7700 				  __le16 fc, bool acked)
7701 {
7702 	struct ieee80211_local *local = sdata->local;
7703 
7704 	sdata->u.mgd.status_fc = fc;
7705 	sdata->u.mgd.status_acked = acked;
7706 	sdata->u.mgd.status_received = true;
7707 
7708 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
7709 }
7710 
7711 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
7712 {
7713 	struct ieee80211_local *local = sdata->local;
7714 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7715 
7716 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7717 
7718 	if (ifmgd->status_received) {
7719 		__le16 fc = ifmgd->status_fc;
7720 		bool status_acked = ifmgd->status_acked;
7721 
7722 		ifmgd->status_received = false;
7723 		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
7724 			if (status_acked) {
7725 				if (ifmgd->auth_data->algorithm ==
7726 				    WLAN_AUTH_SAE)
7727 					ifmgd->auth_data->timeout =
7728 						jiffies +
7729 						IEEE80211_AUTH_TIMEOUT_SAE;
7730 				else
7731 					ifmgd->auth_data->timeout =
7732 						jiffies +
7733 						IEEE80211_AUTH_TIMEOUT_SHORT;
7734 				run_again(sdata, ifmgd->auth_data->timeout);
7735 			} else {
7736 				ifmgd->auth_data->timeout = jiffies - 1;
7737 			}
7738 			ifmgd->auth_data->timeout_started = true;
7739 		} else if (ifmgd->assoc_data &&
7740 			   !ifmgd->assoc_data->comeback &&
7741 			   (ieee80211_is_assoc_req(fc) ||
7742 			    ieee80211_is_reassoc_req(fc))) {
7743 			/*
7744 			 * Update association timeout based on the TX status
7745 			 * for the (Re)Association Request frame. Skip this if
7746 			 * we have already processed a (Re)Association Response
7747 			 * frame that indicated need for association comeback
7748 			 * at a specific time in the future. This could happen
7749 			 * if the TX status information is delayed enough for
7750 			 * the response to be received and processed first.
7751 			 */
7752 			if (status_acked) {
7753 				ifmgd->assoc_data->timeout =
7754 					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
7755 				run_again(sdata, ifmgd->assoc_data->timeout);
7756 			} else {
7757 				ifmgd->assoc_data->timeout = jiffies - 1;
7758 			}
7759 			ifmgd->assoc_data->timeout_started = true;
7760 		}
7761 	}
7762 
7763 	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
7764 	    time_after(jiffies, ifmgd->auth_data->timeout)) {
7765 		if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
7766 			/*
7767 			 * ok ... we waited for assoc or continuation but
7768 			 * userspace didn't do it, so kill the auth data
7769 			 */
7770 			ieee80211_destroy_auth_data(sdata, false);
7771 		} else if (ieee80211_auth(sdata)) {
7772 			u8 ap_addr[ETH_ALEN];
7773 			struct ieee80211_event event = {
7774 				.type = MLME_EVENT,
7775 				.u.mlme.data = AUTH_EVENT,
7776 				.u.mlme.status = MLME_TIMEOUT,
7777 			};
7778 
7779 			memcpy(ap_addr, ifmgd->auth_data->ap_addr, ETH_ALEN);
7780 
7781 			ieee80211_destroy_auth_data(sdata, false);
7782 
7783 			cfg80211_auth_timeout(sdata->dev, ap_addr);
7784 			drv_event_callback(sdata->local, sdata, &event);
7785 		}
7786 	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
7787 		run_again(sdata, ifmgd->auth_data->timeout);
7788 
7789 	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
7790 	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
7791 		if ((ifmgd->assoc_data->need_beacon &&
7792 		     !sdata->deflink.u.mgd.have_beacon) ||
7793 		    ieee80211_do_assoc(sdata)) {
7794 			struct ieee80211_event event = {
7795 				.type = MLME_EVENT,
7796 				.u.mlme.data = ASSOC_EVENT,
7797 				.u.mlme.status = MLME_TIMEOUT,
7798 			};
7799 
7800 			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
7801 			drv_event_callback(sdata->local, sdata, &event);
7802 		}
7803 	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
7804 		run_again(sdata, ifmgd->assoc_data->timeout);
7805 
7806 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
7807 	    ifmgd->associated) {
7808 		u8 *bssid = sdata->deflink.u.mgd.bssid;
7809 		int max_tries;
7810 
7811 		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
7812 			max_tries = max_nullfunc_tries;
7813 		else
7814 			max_tries = max_probe_tries;
7815 
7816 		/* ACK received for nullfunc probing frame */
7817 		if (!ifmgd->probe_send_count)
7818 			ieee80211_reset_ap_probe(sdata);
7819 		else if (ifmgd->nullfunc_failed) {
7820 			if (ifmgd->probe_send_count < max_tries) {
7821 				mlme_dbg(sdata,
7822 					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
7823 					 bssid, ifmgd->probe_send_count,
7824 					 max_tries);
7825 				ieee80211_mgd_probe_ap_send(sdata);
7826 			} else {
7827 				mlme_dbg(sdata,
7828 					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
7829 					 bssid);
7830 				ieee80211_sta_connection_lost(sdata,
7831 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
7832 					false);
7833 			}
7834 		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
7835 			run_again(sdata, ifmgd->probe_timeout);
7836 		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
7837 			mlme_dbg(sdata,
7838 				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
7839 				 bssid, probe_wait_ms);
7840 			ieee80211_sta_connection_lost(sdata,
7841 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
7842 		} else if (ifmgd->probe_send_count < max_tries) {
7843 			mlme_dbg(sdata,
7844 				 "No probe response from AP %pM after %dms, try %d/%i\n",
7845 				 bssid, probe_wait_ms,
7846 				 ifmgd->probe_send_count, max_tries);
7847 			ieee80211_mgd_probe_ap_send(sdata);
7848 		} else {
7849 			/*
7850 			 * We actually lost the connection ... or did we?
7851 			 * Let's make sure!
7852 			 */
7853 			mlme_dbg(sdata,
7854 				 "No probe response from AP %pM after %dms, disconnecting.\n",
7855 				 bssid, probe_wait_ms);
7856 
7857 			ieee80211_sta_connection_lost(sdata,
7858 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
7859 		}
7860 	}
7861 }
7862 
7863 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
7864 {
7865 	struct ieee80211_sub_if_data *sdata =
7866 		from_timer(sdata, t, u.mgd.bcn_mon_timer);
7867 
7868 	if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif)))
7869 		return;
7870 
7871 	if (sdata->vif.bss_conf.csa_active &&
7872 	    !sdata->deflink.u.mgd.csa.waiting_bcn)
7873 		return;
7874 
7875 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
7876 		return;
7877 
7878 	sdata->u.mgd.connection_loss = false;
7879 	wiphy_work_queue(sdata->local->hw.wiphy,
7880 			 &sdata->u.mgd.beacon_connection_loss_work);
7881 }
7882 
7883 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
7884 {
7885 	struct ieee80211_sub_if_data *sdata =
7886 		from_timer(sdata, t, u.mgd.conn_mon_timer);
7887 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7888 	struct ieee80211_local *local = sdata->local;
7889 	struct sta_info *sta;
7890 	unsigned long timeout;
7891 
7892 	if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif)))
7893 		return;
7894 
7895 	if (sdata->vif.bss_conf.csa_active &&
7896 	    !sdata->deflink.u.mgd.csa.waiting_bcn)
7897 		return;
7898 
7899 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
7900 	if (!sta)
7901 		return;
7902 
7903 	timeout = sta->deflink.status_stats.last_ack;
7904 	if (time_before(sta->deflink.status_stats.last_ack, sta->deflink.rx_stats.last_rx))
7905 		timeout = sta->deflink.rx_stats.last_rx;
7906 	timeout += IEEE80211_CONNECTION_IDLE_TIME;
7907 
7908 	/* If timeout is after now, then update timer to fire at
7909 	 * the later date, but do not actually probe at this time.
7910 	 */
7911 	if (time_is_after_jiffies(timeout)) {
7912 		mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
7913 		return;
7914 	}
7915 
7916 	wiphy_work_queue(local->hw.wiphy, &sdata->u.mgd.monitor_work);
7917 }
7918 
7919 static void ieee80211_sta_monitor_work(struct wiphy *wiphy,
7920 				       struct wiphy_work *work)
7921 {
7922 	struct ieee80211_sub_if_data *sdata =
7923 		container_of(work, struct ieee80211_sub_if_data,
7924 			     u.mgd.monitor_work);
7925 
7926 	ieee80211_mgd_probe_ap(sdata, false);
7927 }
7928 
7929 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
7930 {
7931 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
7932 		__ieee80211_stop_poll(sdata);
7933 
7934 		/* let's probe the connection once */
7935 		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
7936 			wiphy_work_queue(sdata->local->hw.wiphy,
7937 					 &sdata->u.mgd.monitor_work);
7938 	}
7939 }
7940 
7941 #ifdef CONFIG_PM
7942 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
7943 {
7944 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7945 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
7946 
7947 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7948 
7949 	if (ifmgd->auth_data || ifmgd->assoc_data) {
7950 		const u8 *ap_addr = ifmgd->auth_data ?
7951 				ifmgd->auth_data->ap_addr :
7952 				ifmgd->assoc_data->ap_addr;
7953 
7954 		/*
7955 		 * If we are trying to authenticate / associate while suspending,
7956 		 * cfg80211 won't know and won't actually abort those attempts,
7957 		 * thus we need to do that ourselves.
7958 		 */
7959 		ieee80211_send_deauth_disassoc(sdata, ap_addr, ap_addr,
7960 					       IEEE80211_STYPE_DEAUTH,
7961 					       WLAN_REASON_DEAUTH_LEAVING,
7962 					       false, frame_buf);
7963 		if (ifmgd->assoc_data)
7964 			ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
7965 		if (ifmgd->auth_data)
7966 			ieee80211_destroy_auth_data(sdata, false);
7967 		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
7968 				      IEEE80211_DEAUTH_FRAME_LEN,
7969 				      false);
7970 	}
7971 
7972 	/* This is a bit of a hack - we should find a better and more generic
7973 	 * solution to this. Normally when suspending, cfg80211 will in fact
7974 	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
7975 	 * auth (not so important) or assoc (this is the problem) process.
7976 	 *
7977 	 * As a consequence, it can happen that we are in the process of both
7978 	 * associating and suspending, and receive an association response
7979 	 * after cfg80211 has checked if it needs to disconnect, but before
7980 	 * we actually set the flag to drop incoming frames. This will then
7981 	 * cause the workqueue flush to process the association response in
7982 	 * the suspend, resulting in a successful association just before it
7983 	 * tries to remove the interface from the driver, which now though
7984 	 * has a channel context assigned ... this results in issues.
7985 	 *
7986 	 * To work around this (for now) simply deauth here again if we're
7987 	 * now connected.
7988 	 */
7989 	if (ifmgd->associated && !sdata->local->wowlan) {
7990 		u8 bssid[ETH_ALEN];
7991 		struct cfg80211_deauth_request req = {
7992 			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
7993 			.bssid = bssid,
7994 		};
7995 
7996 		memcpy(bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
7997 		ieee80211_mgd_deauth(sdata, &req);
7998 	}
7999 }
8000 #endif
8001 
8002 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
8003 {
8004 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8005 
8006 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8007 
8008 	if (!ifmgd->associated)
8009 		return;
8010 
8011 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
8012 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
8013 		mlme_dbg(sdata, "driver requested disconnect after resume\n");
8014 		ieee80211_sta_connection_lost(sdata,
8015 					      WLAN_REASON_UNSPECIFIED,
8016 					      true);
8017 		return;
8018 	}
8019 
8020 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_HW_RESTART) {
8021 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_HW_RESTART;
8022 		mlme_dbg(sdata, "driver requested disconnect after hardware restart\n");
8023 		ieee80211_sta_connection_lost(sdata,
8024 					      WLAN_REASON_UNSPECIFIED,
8025 					      true);
8026 		return;
8027 	}
8028 }
8029 
8030 static void ieee80211_request_smps_mgd_work(struct wiphy *wiphy,
8031 					    struct wiphy_work *work)
8032 {
8033 	struct ieee80211_link_data *link =
8034 		container_of(work, struct ieee80211_link_data,
8035 			     u.mgd.request_smps_work);
8036 
8037 	__ieee80211_request_smps_mgd(link->sdata, link,
8038 				     link->u.mgd.driver_smps_mode);
8039 }
8040 
8041 /* interface setup */
8042 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
8043 {
8044 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8045 
8046 	wiphy_work_init(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
8047 	wiphy_work_init(&ifmgd->beacon_connection_loss_work,
8048 			ieee80211_beacon_connection_loss_work);
8049 	wiphy_work_init(&ifmgd->csa_connection_drop_work,
8050 			ieee80211_csa_connection_drop_work);
8051 	wiphy_delayed_work_init(&ifmgd->tdls_peer_del_work,
8052 				ieee80211_tdls_peer_del_work);
8053 	wiphy_delayed_work_init(&ifmgd->ml_reconf_work,
8054 				ieee80211_ml_reconf_work);
8055 	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
8056 	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
8057 	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
8058 	wiphy_delayed_work_init(&ifmgd->tx_tspec_wk,
8059 				ieee80211_sta_handle_tspec_ac_params_wk);
8060 	wiphy_delayed_work_init(&ifmgd->ttlm_work,
8061 				ieee80211_tid_to_link_map_work);
8062 	wiphy_delayed_work_init(&ifmgd->neg_ttlm_timeout_work,
8063 				ieee80211_neg_ttlm_timeout_work);
8064 	wiphy_work_init(&ifmgd->teardown_ttlm_work,
8065 			ieee80211_teardown_ttlm_work);
8066 
8067 	ifmgd->flags = 0;
8068 	ifmgd->powersave = sdata->wdev.ps;
8069 	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
8070 	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
8071 	/* Setup TDLS data */
8072 	spin_lock_init(&ifmgd->teardown_lock);
8073 	ifmgd->teardown_skb = NULL;
8074 	ifmgd->orig_teardown_skb = NULL;
8075 	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
8076 }
8077 
8078 static void ieee80211_recalc_smps_work(struct wiphy *wiphy,
8079 				       struct wiphy_work *work)
8080 {
8081 	struct ieee80211_link_data *link =
8082 		container_of(work, struct ieee80211_link_data,
8083 			     u.mgd.recalc_smps);
8084 
8085 	ieee80211_recalc_smps(link->sdata, link);
8086 }
8087 
8088 void ieee80211_mgd_setup_link(struct ieee80211_link_data *link)
8089 {
8090 	struct ieee80211_sub_if_data *sdata = link->sdata;
8091 	struct ieee80211_local *local = sdata->local;
8092 	unsigned int link_id = link->link_id;
8093 
8094 	link->u.mgd.p2p_noa_index = -1;
8095 	link->conf->bssid = link->u.mgd.bssid;
8096 	link->smps_mode = IEEE80211_SMPS_OFF;
8097 
8098 	wiphy_work_init(&link->u.mgd.request_smps_work,
8099 			ieee80211_request_smps_mgd_work);
8100 	wiphy_work_init(&link->u.mgd.recalc_smps,
8101 			ieee80211_recalc_smps_work);
8102 	if (local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
8103 		link->u.mgd.req_smps = IEEE80211_SMPS_AUTOMATIC;
8104 	else
8105 		link->u.mgd.req_smps = IEEE80211_SMPS_OFF;
8106 
8107 	wiphy_delayed_work_init(&link->u.mgd.csa.switch_work,
8108 				ieee80211_csa_switch_work);
8109 
8110 	ieee80211_clear_tpe(&link->conf->tpe);
8111 
8112 	if (sdata->u.mgd.assoc_data)
8113 		ether_addr_copy(link->conf->addr,
8114 				sdata->u.mgd.assoc_data->link[link_id].addr);
8115 	else if (!is_valid_ether_addr(link->conf->addr))
8116 		eth_random_addr(link->conf->addr);
8117 }
8118 
8119 /* scan finished notification */
8120 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
8121 {
8122 	struct ieee80211_sub_if_data *sdata;
8123 
8124 	/* Restart STA timers */
8125 	rcu_read_lock();
8126 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
8127 		if (ieee80211_sdata_running(sdata))
8128 			ieee80211_restart_sta_timer(sdata);
8129 	}
8130 	rcu_read_unlock();
8131 }
8132 
8133 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
8134 				     struct cfg80211_bss *cbss, s8 link_id,
8135 				     const u8 *ap_mld_addr, bool assoc,
8136 				     struct ieee80211_conn_settings *conn,
8137 				     bool override)
8138 {
8139 	struct ieee80211_local *local = sdata->local;
8140 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8141 	struct ieee80211_bss *bss = (void *)cbss->priv;
8142 	struct sta_info *new_sta = NULL;
8143 	struct ieee80211_link_data *link;
8144 	bool have_sta = false;
8145 	bool mlo;
8146 	int err;
8147 
8148 	if (link_id >= 0) {
8149 		mlo = true;
8150 		if (WARN_ON(!ap_mld_addr))
8151 			return -EINVAL;
8152 		err = ieee80211_vif_set_links(sdata, BIT(link_id), 0);
8153 	} else {
8154 		if (WARN_ON(ap_mld_addr))
8155 			return -EINVAL;
8156 		ap_mld_addr = cbss->bssid;
8157 		err = ieee80211_vif_set_links(sdata, 0, 0);
8158 		link_id = 0;
8159 		mlo = false;
8160 	}
8161 
8162 	if (err)
8163 		return err;
8164 
8165 	link = sdata_dereference(sdata->link[link_id], sdata);
8166 	if (WARN_ON(!link)) {
8167 		err = -ENOLINK;
8168 		goto out_err;
8169 	}
8170 
8171 	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data)) {
8172 		err = -EINVAL;
8173 		goto out_err;
8174 	}
8175 
8176 	/* If a reconfig is happening, bail out */
8177 	if (local->in_reconfig) {
8178 		err = -EBUSY;
8179 		goto out_err;
8180 	}
8181 
8182 	if (assoc) {
8183 		rcu_read_lock();
8184 		have_sta = sta_info_get(sdata, ap_mld_addr);
8185 		rcu_read_unlock();
8186 	}
8187 
8188 	if (!have_sta) {
8189 		if (mlo)
8190 			new_sta = sta_info_alloc_with_link(sdata, ap_mld_addr,
8191 							   link_id, cbss->bssid,
8192 							   GFP_KERNEL);
8193 		else
8194 			new_sta = sta_info_alloc(sdata, ap_mld_addr, GFP_KERNEL);
8195 
8196 		if (!new_sta) {
8197 			err = -ENOMEM;
8198 			goto out_err;
8199 		}
8200 
8201 		new_sta->sta.mlo = mlo;
8202 	}
8203 
8204 	/*
8205 	 * Set up the information for the new channel before setting the
8206 	 * new channel. We can't - completely race-free - change the basic
8207 	 * rates bitmap and the channel (sband) that it refers to, but if
8208 	 * we set it up before we at least avoid calling into the driver's
8209 	 * bss_info_changed() method with invalid information (since we do
8210 	 * call that from changing the channel - only for IDLE and perhaps
8211 	 * some others, but ...).
8212 	 *
8213 	 * So to avoid that, just set up all the new information before the
8214 	 * channel, but tell the driver to apply it only afterwards, since
8215 	 * it might need the new channel for that.
8216 	 */
8217 	if (new_sta) {
8218 		const struct cfg80211_bss_ies *ies;
8219 		struct link_sta_info *link_sta;
8220 
8221 		rcu_read_lock();
8222 		link_sta = rcu_dereference(new_sta->link[link_id]);
8223 		if (WARN_ON(!link_sta)) {
8224 			rcu_read_unlock();
8225 			sta_info_free(local, new_sta);
8226 			err = -EINVAL;
8227 			goto out_err;
8228 		}
8229 
8230 		err = ieee80211_mgd_setup_link_sta(link, new_sta,
8231 						   link_sta, cbss);
8232 		if (err) {
8233 			rcu_read_unlock();
8234 			sta_info_free(local, new_sta);
8235 			goto out_err;
8236 		}
8237 
8238 		memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
8239 
8240 		/* set timing information */
8241 		link->conf->beacon_int = cbss->beacon_interval;
8242 		ies = rcu_dereference(cbss->beacon_ies);
8243 		if (ies) {
8244 			link->conf->sync_tsf = ies->tsf;
8245 			link->conf->sync_device_ts =
8246 				bss->device_ts_beacon;
8247 
8248 			ieee80211_get_dtim(ies,
8249 					   &link->conf->sync_dtim_count,
8250 					   NULL);
8251 		} else if (!ieee80211_hw_check(&sdata->local->hw,
8252 					       TIMING_BEACON_ONLY)) {
8253 			ies = rcu_dereference(cbss->proberesp_ies);
8254 			/* must be non-NULL since beacon IEs were NULL */
8255 			link->conf->sync_tsf = ies->tsf;
8256 			link->conf->sync_device_ts =
8257 				bss->device_ts_presp;
8258 			link->conf->sync_dtim_count = 0;
8259 		} else {
8260 			link->conf->sync_tsf = 0;
8261 			link->conf->sync_device_ts = 0;
8262 			link->conf->sync_dtim_count = 0;
8263 		}
8264 		rcu_read_unlock();
8265 	}
8266 
8267 	if (new_sta || override) {
8268 		/*
8269 		 * Only set this if we're also going to calculate the AP
8270 		 * settings etc., otherwise this was set before in a
8271 		 * previous call. Note override is set to %true in assoc
8272 		 * if the settings were changed.
8273 		 */
8274 		link->u.mgd.conn = *conn;
8275 		err = ieee80211_prep_channel(sdata, link, link->link_id, cbss,
8276 					     mlo, &link->u.mgd.conn);
8277 		if (err) {
8278 			if (new_sta)
8279 				sta_info_free(local, new_sta);
8280 			goto out_err;
8281 		}
8282 		/* pass out for use in assoc */
8283 		*conn = link->u.mgd.conn;
8284 	}
8285 
8286 	if (new_sta) {
8287 		/*
8288 		 * tell driver about BSSID, basic rates and timing
8289 		 * this was set up above, before setting the channel
8290 		 */
8291 		ieee80211_link_info_change_notify(sdata, link,
8292 						  BSS_CHANGED_BSSID |
8293 						  BSS_CHANGED_BASIC_RATES |
8294 						  BSS_CHANGED_BEACON_INT);
8295 
8296 		if (assoc)
8297 			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
8298 
8299 		err = sta_info_insert(new_sta);
8300 		new_sta = NULL;
8301 		if (err) {
8302 			sdata_info(sdata,
8303 				   "failed to insert STA entry for the AP (error %d)\n",
8304 				   err);
8305 			goto out_release_chan;
8306 		}
8307 	} else
8308 		WARN_ON_ONCE(!ether_addr_equal(link->u.mgd.bssid, cbss->bssid));
8309 
8310 	/* Cancel scan to ensure that nothing interferes with connection */
8311 	if (local->scanning)
8312 		ieee80211_scan_cancel(local);
8313 
8314 	return 0;
8315 
8316 out_release_chan:
8317 	ieee80211_link_release_channel(link);
8318 out_err:
8319 	ieee80211_vif_set_links(sdata, 0, 0);
8320 	return err;
8321 }
8322 
8323 static bool ieee80211_mgd_csa_present(struct ieee80211_sub_if_data *sdata,
8324 				      const struct cfg80211_bss_ies *ies,
8325 				      u8 cur_channel, bool ignore_ecsa)
8326 {
8327 	const struct element *csa_elem, *ecsa_elem;
8328 	struct ieee80211_channel_sw_ie *csa = NULL;
8329 	struct ieee80211_ext_chansw_ie *ecsa = NULL;
8330 
8331 	if (!ies)
8332 		return false;
8333 
8334 	csa_elem = cfg80211_find_elem(WLAN_EID_CHANNEL_SWITCH,
8335 				      ies->data, ies->len);
8336 	if (csa_elem && csa_elem->datalen == sizeof(*csa))
8337 		csa = (void *)csa_elem->data;
8338 
8339 	ecsa_elem = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
8340 				       ies->data, ies->len);
8341 	if (ecsa_elem && ecsa_elem->datalen == sizeof(*ecsa))
8342 		ecsa = (void *)ecsa_elem->data;
8343 
8344 	if (csa && csa->count == 0)
8345 		csa = NULL;
8346 	if (csa && !csa->mode && csa->new_ch_num == cur_channel)
8347 		csa = NULL;
8348 
8349 	if (ecsa && ecsa->count == 0)
8350 		ecsa = NULL;
8351 	if (ecsa && !ecsa->mode && ecsa->new_ch_num == cur_channel)
8352 		ecsa = NULL;
8353 
8354 	if (ignore_ecsa && ecsa) {
8355 		sdata_info(sdata,
8356 			   "Ignoring ECSA in probe response - was considered stuck!\n");
8357 		return csa;
8358 	}
8359 
8360 	return csa || ecsa;
8361 }
8362 
8363 static bool ieee80211_mgd_csa_in_process(struct ieee80211_sub_if_data *sdata,
8364 					 struct cfg80211_bss *bss)
8365 {
8366 	u8 cur_channel;
8367 	bool ret;
8368 
8369 	cur_channel = ieee80211_frequency_to_channel(bss->channel->center_freq);
8370 
8371 	rcu_read_lock();
8372 	if (ieee80211_mgd_csa_present(sdata,
8373 				      rcu_dereference(bss->beacon_ies),
8374 				      cur_channel, false)) {
8375 		ret = true;
8376 		goto out;
8377 	}
8378 
8379 	if (ieee80211_mgd_csa_present(sdata,
8380 				      rcu_dereference(bss->proberesp_ies),
8381 				      cur_channel, bss->proberesp_ecsa_stuck)) {
8382 		ret = true;
8383 		goto out;
8384 	}
8385 
8386 	ret = false;
8387 out:
8388 	rcu_read_unlock();
8389 	return ret;
8390 }
8391 
8392 /* config hooks */
8393 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
8394 		       struct cfg80211_auth_request *req)
8395 {
8396 	struct ieee80211_local *local = sdata->local;
8397 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8398 	struct ieee80211_mgd_auth_data *auth_data;
8399 	struct ieee80211_conn_settings conn;
8400 	struct ieee80211_link_data *link;
8401 	struct ieee80211_supported_band *sband;
8402 	struct ieee80211_bss *bss;
8403 	u16 auth_alg;
8404 	int err;
8405 	bool cont_auth, wmm_used;
8406 
8407 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8408 
8409 	/* prepare auth data structure */
8410 
8411 	switch (req->auth_type) {
8412 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
8413 		auth_alg = WLAN_AUTH_OPEN;
8414 		break;
8415 	case NL80211_AUTHTYPE_SHARED_KEY:
8416 		if (fips_enabled)
8417 			return -EOPNOTSUPP;
8418 		auth_alg = WLAN_AUTH_SHARED_KEY;
8419 		break;
8420 	case NL80211_AUTHTYPE_FT:
8421 		auth_alg = WLAN_AUTH_FT;
8422 		break;
8423 	case NL80211_AUTHTYPE_NETWORK_EAP:
8424 		auth_alg = WLAN_AUTH_LEAP;
8425 		break;
8426 	case NL80211_AUTHTYPE_SAE:
8427 		auth_alg = WLAN_AUTH_SAE;
8428 		break;
8429 	case NL80211_AUTHTYPE_FILS_SK:
8430 		auth_alg = WLAN_AUTH_FILS_SK;
8431 		break;
8432 	case NL80211_AUTHTYPE_FILS_SK_PFS:
8433 		auth_alg = WLAN_AUTH_FILS_SK_PFS;
8434 		break;
8435 	case NL80211_AUTHTYPE_FILS_PK:
8436 		auth_alg = WLAN_AUTH_FILS_PK;
8437 		break;
8438 	default:
8439 		return -EOPNOTSUPP;
8440 	}
8441 
8442 	if (ifmgd->assoc_data)
8443 		return -EBUSY;
8444 
8445 	if (ieee80211_mgd_csa_in_process(sdata, req->bss)) {
8446 		sdata_info(sdata, "AP is in CSA process, reject auth\n");
8447 		return -EINVAL;
8448 	}
8449 
8450 	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
8451 			    req->ie_len, GFP_KERNEL);
8452 	if (!auth_data)
8453 		return -ENOMEM;
8454 
8455 	memcpy(auth_data->ap_addr,
8456 	       req->ap_mld_addr ?: req->bss->bssid,
8457 	       ETH_ALEN);
8458 	auth_data->bss = req->bss;
8459 	auth_data->link_id = req->link_id;
8460 
8461 	if (req->auth_data_len >= 4) {
8462 		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
8463 			__le16 *pos = (__le16 *) req->auth_data;
8464 
8465 			auth_data->sae_trans = le16_to_cpu(pos[0]);
8466 			auth_data->sae_status = le16_to_cpu(pos[1]);
8467 		}
8468 		memcpy(auth_data->data, req->auth_data + 4,
8469 		       req->auth_data_len - 4);
8470 		auth_data->data_len += req->auth_data_len - 4;
8471 	}
8472 
8473 	/* Check if continuing authentication or trying to authenticate with the
8474 	 * same BSS that we were in the process of authenticating with and avoid
8475 	 * removal and re-addition of the STA entry in
8476 	 * ieee80211_prep_connection().
8477 	 */
8478 	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss &&
8479 		    ifmgd->auth_data->link_id == req->link_id;
8480 
8481 	if (req->ie && req->ie_len) {
8482 		memcpy(&auth_data->data[auth_data->data_len],
8483 		       req->ie, req->ie_len);
8484 		auth_data->data_len += req->ie_len;
8485 	}
8486 
8487 	if (req->key && req->key_len) {
8488 		auth_data->key_len = req->key_len;
8489 		auth_data->key_idx = req->key_idx;
8490 		memcpy(auth_data->key, req->key, req->key_len);
8491 	}
8492 
8493 	auth_data->algorithm = auth_alg;
8494 
8495 	/* try to authenticate/probe */
8496 
8497 	if (ifmgd->auth_data) {
8498 		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
8499 			auth_data->peer_confirmed =
8500 				ifmgd->auth_data->peer_confirmed;
8501 		}
8502 		ieee80211_destroy_auth_data(sdata, cont_auth);
8503 	}
8504 
8505 	/* prep auth_data so we don't go into idle on disassoc */
8506 	ifmgd->auth_data = auth_data;
8507 
8508 	/* If this is continuation of an ongoing SAE authentication exchange
8509 	 * (i.e., request to send SAE Confirm) and the peer has already
8510 	 * confirmed, mark authentication completed since we are about to send
8511 	 * out SAE Confirm.
8512 	 */
8513 	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
8514 	    auth_data->peer_confirmed && auth_data->sae_trans == 2)
8515 		ieee80211_mark_sta_auth(sdata);
8516 
8517 	if (ifmgd->associated) {
8518 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
8519 
8520 		sdata_info(sdata,
8521 			   "disconnect from AP %pM for new auth to %pM\n",
8522 			   sdata->vif.cfg.ap_addr, auth_data->ap_addr);
8523 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
8524 				       WLAN_REASON_UNSPECIFIED,
8525 				       false, frame_buf);
8526 
8527 		ieee80211_report_disconnect(sdata, frame_buf,
8528 					    sizeof(frame_buf), true,
8529 					    WLAN_REASON_UNSPECIFIED,
8530 					    false);
8531 	}
8532 
8533 	/* needed for transmitting the auth frame(s) properly */
8534 	memcpy(sdata->vif.cfg.ap_addr, auth_data->ap_addr, ETH_ALEN);
8535 
8536 	bss = (void *)req->bss->priv;
8537 	wmm_used = bss->wmm_used && (local->hw.queues >= IEEE80211_NUM_ACS);
8538 
8539 	sband = local->hw.wiphy->bands[req->bss->channel->band];
8540 
8541 	ieee80211_determine_our_sta_mode_auth(sdata, sband, req, wmm_used,
8542 					      &conn);
8543 
8544 	err = ieee80211_prep_connection(sdata, req->bss, req->link_id,
8545 					req->ap_mld_addr, cont_auth,
8546 					&conn, false);
8547 	if (err)
8548 		goto err_clear;
8549 
8550 	if (req->link_id >= 0)
8551 		link = sdata_dereference(sdata->link[req->link_id], sdata);
8552 	else
8553 		link = &sdata->deflink;
8554 
8555 	if (WARN_ON(!link)) {
8556 		err = -ENOLINK;
8557 		goto err_clear;
8558 	}
8559 
8560 	sdata_info(sdata, "authenticate with %pM (local address=%pM)\n",
8561 		   auth_data->ap_addr, link->conf->addr);
8562 
8563 	err = ieee80211_auth(sdata);
8564 	if (err) {
8565 		sta_info_destroy_addr(sdata, auth_data->ap_addr);
8566 		goto err_clear;
8567 	}
8568 
8569 	/* hold our own reference */
8570 	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
8571 	return 0;
8572 
8573  err_clear:
8574 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
8575 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
8576 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
8577 						  BSS_CHANGED_BSSID);
8578 		ieee80211_link_release_channel(&sdata->deflink);
8579 	}
8580 	ifmgd->auth_data = NULL;
8581 	kfree(auth_data);
8582 	return err;
8583 }
8584 
8585 static void
8586 ieee80211_setup_assoc_link(struct ieee80211_sub_if_data *sdata,
8587 			   struct ieee80211_mgd_assoc_data *assoc_data,
8588 			   struct cfg80211_assoc_request *req,
8589 			   struct ieee80211_conn_settings *conn,
8590 			   unsigned int link_id)
8591 {
8592 	struct ieee80211_local *local = sdata->local;
8593 	const struct cfg80211_bss_ies *bss_ies;
8594 	struct ieee80211_supported_band *sband;
8595 	struct ieee80211_link_data *link;
8596 	struct cfg80211_bss *cbss;
8597 	struct ieee80211_bss *bss;
8598 
8599 	cbss = assoc_data->link[link_id].bss;
8600 	if (WARN_ON(!cbss))
8601 		return;
8602 
8603 	bss = (void *)cbss->priv;
8604 
8605 	sband = local->hw.wiphy->bands[cbss->channel->band];
8606 	if (WARN_ON(!sband))
8607 		return;
8608 
8609 	link = sdata_dereference(sdata->link[link_id], sdata);
8610 	if (WARN_ON(!link))
8611 		return;
8612 
8613 	/* for MLO connections assume advertising all rates is OK */
8614 	if (!req->ap_mld_addr) {
8615 		assoc_data->supp_rates = bss->supp_rates;
8616 		assoc_data->supp_rates_len = bss->supp_rates_len;
8617 	}
8618 
8619 	/* copy and link elems for the STA profile */
8620 	if (req->links[link_id].elems_len) {
8621 		memcpy(assoc_data->ie_pos, req->links[link_id].elems,
8622 		       req->links[link_id].elems_len);
8623 		assoc_data->link[link_id].elems = assoc_data->ie_pos;
8624 		assoc_data->link[link_id].elems_len = req->links[link_id].elems_len;
8625 		assoc_data->ie_pos += req->links[link_id].elems_len;
8626 	}
8627 
8628 	link->u.mgd.beacon_crc_valid = false;
8629 	link->u.mgd.dtim_period = 0;
8630 	link->u.mgd.have_beacon = false;
8631 
8632 	/* override HT configuration only if the AP and we support it */
8633 	if (conn->mode >= IEEE80211_CONN_MODE_HT) {
8634 		struct ieee80211_sta_ht_cap sta_ht_cap;
8635 
8636 		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
8637 		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
8638 	}
8639 
8640 	rcu_read_lock();
8641 	bss_ies = rcu_dereference(cbss->beacon_ies);
8642 	if (bss_ies) {
8643 		u8 dtim_count = 0;
8644 
8645 		ieee80211_get_dtim(bss_ies, &dtim_count,
8646 				   &link->u.mgd.dtim_period);
8647 
8648 		sdata->deflink.u.mgd.have_beacon = true;
8649 
8650 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
8651 			link->conf->sync_tsf = bss_ies->tsf;
8652 			link->conf->sync_device_ts = bss->device_ts_beacon;
8653 			link->conf->sync_dtim_count = dtim_count;
8654 		}
8655 	} else {
8656 		bss_ies = rcu_dereference(cbss->ies);
8657 	}
8658 
8659 	if (bss_ies) {
8660 		const struct element *elem;
8661 
8662 		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
8663 					      bss_ies->data, bss_ies->len);
8664 		if (elem && elem->datalen >= 3)
8665 			link->conf->profile_periodicity = elem->data[2];
8666 		else
8667 			link->conf->profile_periodicity = 0;
8668 
8669 		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
8670 					  bss_ies->data, bss_ies->len);
8671 		if (elem && elem->datalen >= 11 &&
8672 		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
8673 			link->conf->ema_ap = true;
8674 		else
8675 			link->conf->ema_ap = false;
8676 	}
8677 	rcu_read_unlock();
8678 
8679 	if (bss->corrupt_data) {
8680 		char *corrupt_type = "data";
8681 
8682 		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
8683 			if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
8684 				corrupt_type = "beacon and probe response";
8685 			else
8686 				corrupt_type = "beacon";
8687 		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) {
8688 			corrupt_type = "probe response";
8689 		}
8690 		sdata_info(sdata, "associating to AP %pM with corrupt %s\n",
8691 			   cbss->bssid, corrupt_type);
8692 	}
8693 
8694 	if (link->u.mgd.req_smps == IEEE80211_SMPS_AUTOMATIC) {
8695 		if (sdata->u.mgd.powersave)
8696 			link->smps_mode = IEEE80211_SMPS_DYNAMIC;
8697 		else
8698 			link->smps_mode = IEEE80211_SMPS_OFF;
8699 	} else {
8700 		link->smps_mode = link->u.mgd.req_smps;
8701 	}
8702 }
8703 
8704 static int
8705 ieee80211_mgd_get_ap_ht_vht_capa(struct ieee80211_sub_if_data *sdata,
8706 				 struct ieee80211_mgd_assoc_data *assoc_data,
8707 				 int link_id)
8708 {
8709 	struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
8710 	enum nl80211_band band = cbss->channel->band;
8711 	struct ieee80211_supported_band *sband;
8712 	const struct element *elem;
8713 	int err;
8714 
8715 	/* neither HT nor VHT elements used on 6 GHz */
8716 	if (band == NL80211_BAND_6GHZ)
8717 		return 0;
8718 
8719 	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_HT)
8720 		return 0;
8721 
8722 	rcu_read_lock();
8723 	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_OPERATION);
8724 	if (!elem || elem->datalen < sizeof(struct ieee80211_ht_operation)) {
8725 		mlme_link_id_dbg(sdata, link_id, "no HT operation on BSS %pM\n",
8726 				 cbss->bssid);
8727 		err = -EINVAL;
8728 		goto out_rcu;
8729 	}
8730 	assoc_data->link[link_id].ap_ht_param =
8731 		((struct ieee80211_ht_operation *)(elem->data))->ht_param;
8732 	rcu_read_unlock();
8733 
8734 	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_VHT)
8735 		return 0;
8736 
8737 	/* some drivers want to support VHT on 2.4 GHz even */
8738 	sband = sdata->local->hw.wiphy->bands[band];
8739 	if (!sband->vht_cap.vht_supported)
8740 		return 0;
8741 
8742 	rcu_read_lock();
8743 	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
8744 	/* but even then accept it not being present on the AP */
8745 	if (!elem && band == NL80211_BAND_2GHZ) {
8746 		err = 0;
8747 		goto out_rcu;
8748 	}
8749 	if (!elem || elem->datalen < sizeof(struct ieee80211_vht_cap)) {
8750 		mlme_link_id_dbg(sdata, link_id, "no VHT capa on BSS %pM\n",
8751 				 cbss->bssid);
8752 		err = -EINVAL;
8753 		goto out_rcu;
8754 	}
8755 	memcpy(&assoc_data->link[link_id].ap_vht_cap, elem->data,
8756 	       sizeof(struct ieee80211_vht_cap));
8757 	rcu_read_unlock();
8758 
8759 	return 0;
8760 out_rcu:
8761 	rcu_read_unlock();
8762 	return err;
8763 }
8764 
8765 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
8766 			struct cfg80211_assoc_request *req)
8767 {
8768 	unsigned int assoc_link_id = req->link_id < 0 ? 0 : req->link_id;
8769 	struct ieee80211_local *local = sdata->local;
8770 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8771 	struct ieee80211_mgd_assoc_data *assoc_data;
8772 	const struct element *ssid_elem;
8773 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
8774 	struct ieee80211_link_data *link;
8775 	struct cfg80211_bss *cbss;
8776 	bool override, uapsd_supported;
8777 	bool match_auth;
8778 	int i, err;
8779 	size_t size = sizeof(*assoc_data) + req->ie_len;
8780 
8781 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
8782 		size += req->links[i].elems_len;
8783 
8784 	/* FIXME: no support for 4-addr MLO yet */
8785 	if (sdata->u.mgd.use_4addr && req->link_id >= 0)
8786 		return -EOPNOTSUPP;
8787 
8788 	assoc_data = kzalloc(size, GFP_KERNEL);
8789 	if (!assoc_data)
8790 		return -ENOMEM;
8791 
8792 	cbss = req->link_id < 0 ? req->bss : req->links[req->link_id].bss;
8793 
8794 	if (ieee80211_mgd_csa_in_process(sdata, cbss)) {
8795 		sdata_info(sdata, "AP is in CSA process, reject assoc\n");
8796 		err = -EINVAL;
8797 		goto err_free;
8798 	}
8799 
8800 	rcu_read_lock();
8801 	ssid_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
8802 	if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
8803 		rcu_read_unlock();
8804 		err = -EINVAL;
8805 		goto err_free;
8806 	}
8807 
8808 	memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
8809 	assoc_data->ssid_len = ssid_elem->datalen;
8810 	rcu_read_unlock();
8811 
8812 	if (req->ap_mld_addr)
8813 		memcpy(assoc_data->ap_addr, req->ap_mld_addr, ETH_ALEN);
8814 	else
8815 		memcpy(assoc_data->ap_addr, cbss->bssid, ETH_ALEN);
8816 
8817 	if (ifmgd->associated) {
8818 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
8819 
8820 		sdata_info(sdata,
8821 			   "disconnect from AP %pM for new assoc to %pM\n",
8822 			   sdata->vif.cfg.ap_addr, assoc_data->ap_addr);
8823 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
8824 				       WLAN_REASON_UNSPECIFIED,
8825 				       false, frame_buf);
8826 
8827 		ieee80211_report_disconnect(sdata, frame_buf,
8828 					    sizeof(frame_buf), true,
8829 					    WLAN_REASON_UNSPECIFIED,
8830 					    false);
8831 	}
8832 
8833 	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
8834 	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
8835 	       sizeof(ifmgd->ht_capa_mask));
8836 
8837 	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
8838 	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
8839 	       sizeof(ifmgd->vht_capa_mask));
8840 
8841 	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
8842 	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
8843 	       sizeof(ifmgd->s1g_capa_mask));
8844 
8845 	/* keep some setup (AP STA, channel, ...) if matching */
8846 	match_auth = ifmgd->auth_data &&
8847 		     ether_addr_equal(ifmgd->auth_data->ap_addr,
8848 				      assoc_data->ap_addr) &&
8849 		     ifmgd->auth_data->link_id == req->link_id;
8850 
8851 	if (req->ap_mld_addr) {
8852 		uapsd_supported = true;
8853 
8854 		if (req->flags & (ASSOC_REQ_DISABLE_HT |
8855 				  ASSOC_REQ_DISABLE_VHT |
8856 				  ASSOC_REQ_DISABLE_HE |
8857 				  ASSOC_REQ_DISABLE_EHT)) {
8858 			err = -EINVAL;
8859 			goto err_free;
8860 		}
8861 
8862 		for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
8863 			struct ieee80211_supported_band *sband;
8864 			struct cfg80211_bss *link_cbss = req->links[i].bss;
8865 			struct ieee80211_bss *bss;
8866 
8867 			if (!link_cbss)
8868 				continue;
8869 
8870 			bss = (void *)link_cbss->priv;
8871 
8872 			if (!bss->wmm_used) {
8873 				err = -EINVAL;
8874 				req->links[i].error = err;
8875 				goto err_free;
8876 			}
8877 
8878 			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
8879 				err = -EINVAL;
8880 				req->links[i].error = err;
8881 				goto err_free;
8882 			}
8883 
8884 			link = sdata_dereference(sdata->link[i], sdata);
8885 			if (link)
8886 				ether_addr_copy(assoc_data->link[i].addr,
8887 						link->conf->addr);
8888 			else
8889 				eth_random_addr(assoc_data->link[i].addr);
8890 			sband = local->hw.wiphy->bands[link_cbss->channel->band];
8891 
8892 			if (match_auth && i == assoc_link_id && link)
8893 				assoc_data->link[i].conn = link->u.mgd.conn;
8894 			else
8895 				assoc_data->link[i].conn =
8896 					ieee80211_conn_settings_unlimited;
8897 			ieee80211_determine_our_sta_mode_assoc(sdata, sband,
8898 							       req, true, i,
8899 							       &assoc_data->link[i].conn);
8900 			assoc_data->link[i].bss = link_cbss;
8901 			assoc_data->link[i].disabled = req->links[i].disabled;
8902 
8903 			if (!bss->uapsd_supported)
8904 				uapsd_supported = false;
8905 
8906 			if (assoc_data->link[i].conn.mode < IEEE80211_CONN_MODE_EHT) {
8907 				err = -EINVAL;
8908 				req->links[i].error = err;
8909 				goto err_free;
8910 			}
8911 
8912 			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata,
8913 							       assoc_data, i);
8914 			if (err) {
8915 				err = -EINVAL;
8916 				req->links[i].error = err;
8917 				goto err_free;
8918 			}
8919 		}
8920 
8921 		assoc_data->wmm = true;
8922 	} else {
8923 		struct ieee80211_supported_band *sband;
8924 		struct ieee80211_bss *bss = (void *)cbss->priv;
8925 
8926 		memcpy(assoc_data->link[0].addr, sdata->vif.addr, ETH_ALEN);
8927 		assoc_data->s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
8928 
8929 		assoc_data->wmm = bss->wmm_used &&
8930 				  (local->hw.queues >= IEEE80211_NUM_ACS);
8931 
8932 		if (cbss->channel->band == NL80211_BAND_6GHZ &&
8933 		    req->flags & (ASSOC_REQ_DISABLE_HT |
8934 				  ASSOC_REQ_DISABLE_VHT |
8935 				  ASSOC_REQ_DISABLE_HE)) {
8936 			err = -EINVAL;
8937 			goto err_free;
8938 		}
8939 
8940 		sband = local->hw.wiphy->bands[cbss->channel->band];
8941 
8942 		assoc_data->link[0].bss = cbss;
8943 
8944 		if (match_auth)
8945 			assoc_data->link[0].conn = sdata->deflink.u.mgd.conn;
8946 		else
8947 			assoc_data->link[0].conn =
8948 				ieee80211_conn_settings_unlimited;
8949 		ieee80211_determine_our_sta_mode_assoc(sdata, sband, req,
8950 						       assoc_data->wmm, 0,
8951 						       &assoc_data->link[0].conn);
8952 
8953 		uapsd_supported = bss->uapsd_supported;
8954 
8955 		err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, assoc_data, 0);
8956 		if (err)
8957 			goto err_free;
8958 	}
8959 
8960 	assoc_data->spp_amsdu = req->flags & ASSOC_REQ_SPP_AMSDU;
8961 
8962 	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
8963 		err = -EBUSY;
8964 		goto err_free;
8965 	}
8966 
8967 	if (ifmgd->assoc_data) {
8968 		err = -EBUSY;
8969 		goto err_free;
8970 	}
8971 
8972 	/* Cleanup is delayed if auth_data matches */
8973 	if (ifmgd->auth_data && !match_auth)
8974 		ieee80211_destroy_auth_data(sdata, false);
8975 
8976 	if (req->ie && req->ie_len) {
8977 		memcpy(assoc_data->ie, req->ie, req->ie_len);
8978 		assoc_data->ie_len = req->ie_len;
8979 		assoc_data->ie_pos = assoc_data->ie + assoc_data->ie_len;
8980 	} else {
8981 		assoc_data->ie_pos = assoc_data->ie;
8982 	}
8983 
8984 	if (req->fils_kek) {
8985 		/* should already be checked in cfg80211 - so warn */
8986 		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
8987 			err = -EINVAL;
8988 			goto err_free;
8989 		}
8990 		memcpy(assoc_data->fils_kek, req->fils_kek,
8991 		       req->fils_kek_len);
8992 		assoc_data->fils_kek_len = req->fils_kek_len;
8993 	}
8994 
8995 	if (req->fils_nonces)
8996 		memcpy(assoc_data->fils_nonces, req->fils_nonces,
8997 		       2 * FILS_NONCE_LEN);
8998 
8999 	/* default timeout */
9000 	assoc_data->timeout = jiffies;
9001 	assoc_data->timeout_started = true;
9002 
9003 	assoc_data->assoc_link_id = assoc_link_id;
9004 
9005 	if (req->ap_mld_addr) {
9006 		/* if there was no authentication, set up the link */
9007 		err = ieee80211_vif_set_links(sdata, BIT(assoc_link_id), 0);
9008 		if (err)
9009 			goto err_clear;
9010 	}
9011 
9012 	link = sdata_dereference(sdata->link[assoc_link_id], sdata);
9013 	if (WARN_ON(!link)) {
9014 		err = -EINVAL;
9015 		goto err_clear;
9016 	}
9017 
9018 	override = link->u.mgd.conn.mode !=
9019 			assoc_data->link[assoc_link_id].conn.mode ||
9020 		   link->u.mgd.conn.bw_limit !=
9021 			assoc_data->link[assoc_link_id].conn.bw_limit;
9022 	link->u.mgd.conn = assoc_data->link[assoc_link_id].conn;
9023 
9024 	ieee80211_setup_assoc_link(sdata, assoc_data, req, &link->u.mgd.conn,
9025 				   assoc_link_id);
9026 
9027 	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
9028 		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
9029 	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
9030 		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
9031 
9032 	if (assoc_data->wmm && uapsd_supported &&
9033 	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
9034 		assoc_data->uapsd = true;
9035 		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
9036 	} else {
9037 		assoc_data->uapsd = false;
9038 		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
9039 	}
9040 
9041 	if (req->prev_bssid)
9042 		memcpy(assoc_data->prev_ap_addr, req->prev_bssid, ETH_ALEN);
9043 
9044 	if (req->use_mfp) {
9045 		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
9046 		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
9047 	} else {
9048 		ifmgd->mfp = IEEE80211_MFP_DISABLED;
9049 		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
9050 	}
9051 
9052 	if (req->flags & ASSOC_REQ_USE_RRM)
9053 		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
9054 	else
9055 		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
9056 
9057 	if (req->crypto.control_port)
9058 		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
9059 	else
9060 		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
9061 
9062 	sdata->control_port_protocol = req->crypto.control_port_ethertype;
9063 	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
9064 	sdata->control_port_over_nl80211 =
9065 					req->crypto.control_port_over_nl80211;
9066 	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
9067 
9068 	/* kick off associate process */
9069 	ifmgd->assoc_data = assoc_data;
9070 
9071 	for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) {
9072 		if (!assoc_data->link[i].bss)
9073 			continue;
9074 		if (i == assoc_data->assoc_link_id)
9075 			continue;
9076 		/* only calculate the mode, hence link == NULL */
9077 		err = ieee80211_prep_channel(sdata, NULL, i,
9078 					     assoc_data->link[i].bss, true,
9079 					     &assoc_data->link[i].conn);
9080 		if (err) {
9081 			req->links[i].error = err;
9082 			goto err_clear;
9083 		}
9084 	}
9085 
9086 	memcpy(vif_cfg->ssid, assoc_data->ssid, assoc_data->ssid_len);
9087 	vif_cfg->ssid_len = assoc_data->ssid_len;
9088 
9089 	/* needed for transmitting the assoc frames properly */
9090 	memcpy(sdata->vif.cfg.ap_addr, assoc_data->ap_addr, ETH_ALEN);
9091 
9092 	err = ieee80211_prep_connection(sdata, cbss, req->link_id,
9093 					req->ap_mld_addr, true,
9094 					&assoc_data->link[assoc_link_id].conn,
9095 					override);
9096 	if (err)
9097 		goto err_clear;
9098 
9099 	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC)) {
9100 		const struct cfg80211_bss_ies *beacon_ies;
9101 
9102 		rcu_read_lock();
9103 		beacon_ies = rcu_dereference(req->bss->beacon_ies);
9104 		if (!beacon_ies) {
9105 			/*
9106 			 * Wait up to one beacon interval ...
9107 			 * should this be more if we miss one?
9108 			 */
9109 			sdata_info(sdata, "waiting for beacon from %pM\n",
9110 				   link->u.mgd.bssid);
9111 			assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
9112 			assoc_data->timeout_started = true;
9113 			assoc_data->need_beacon = true;
9114 		}
9115 		rcu_read_unlock();
9116 	}
9117 
9118 	run_again(sdata, assoc_data->timeout);
9119 
9120 	/* We are associating, clean up auth_data */
9121 	if (ifmgd->auth_data)
9122 		ieee80211_destroy_auth_data(sdata, true);
9123 
9124 	return 0;
9125  err_clear:
9126 	if (!ifmgd->auth_data) {
9127 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
9128 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
9129 						  BSS_CHANGED_BSSID);
9130 	}
9131 	ifmgd->assoc_data = NULL;
9132  err_free:
9133 	kfree(assoc_data);
9134 	return err;
9135 }
9136 
9137 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
9138 			 struct cfg80211_deauth_request *req)
9139 {
9140 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9141 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9142 	bool tx = !req->local_state_change;
9143 	struct ieee80211_prep_tx_info info = {
9144 		.subtype = IEEE80211_STYPE_DEAUTH,
9145 	};
9146 
9147 	if (ifmgd->auth_data &&
9148 	    ether_addr_equal(ifmgd->auth_data->ap_addr, req->bssid)) {
9149 		sdata_info(sdata,
9150 			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
9151 			   req->bssid, req->reason_code,
9152 			   ieee80211_get_reason_code_string(req->reason_code));
9153 
9154 		info.link_id = ifmgd->auth_data->link_id;
9155 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
9156 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
9157 					       IEEE80211_STYPE_DEAUTH,
9158 					       req->reason_code, tx,
9159 					       frame_buf);
9160 		ieee80211_destroy_auth_data(sdata, false);
9161 		ieee80211_report_disconnect(sdata, frame_buf,
9162 					    sizeof(frame_buf), true,
9163 					    req->reason_code, false);
9164 		drv_mgd_complete_tx(sdata->local, sdata, &info);
9165 		return 0;
9166 	}
9167 
9168 	if (ifmgd->assoc_data &&
9169 	    ether_addr_equal(ifmgd->assoc_data->ap_addr, req->bssid)) {
9170 		sdata_info(sdata,
9171 			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
9172 			   req->bssid, req->reason_code,
9173 			   ieee80211_get_reason_code_string(req->reason_code));
9174 
9175 		info.link_id = ifmgd->assoc_data->assoc_link_id;
9176 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
9177 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
9178 					       IEEE80211_STYPE_DEAUTH,
9179 					       req->reason_code, tx,
9180 					       frame_buf);
9181 		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
9182 		ieee80211_report_disconnect(sdata, frame_buf,
9183 					    sizeof(frame_buf), true,
9184 					    req->reason_code, false);
9185 		drv_mgd_complete_tx(sdata->local, sdata, &info);
9186 		return 0;
9187 	}
9188 
9189 	if (ifmgd->associated &&
9190 	    ether_addr_equal(sdata->vif.cfg.ap_addr, req->bssid)) {
9191 		sdata_info(sdata,
9192 			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
9193 			   req->bssid, req->reason_code,
9194 			   ieee80211_get_reason_code_string(req->reason_code));
9195 
9196 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
9197 				       req->reason_code, tx, frame_buf);
9198 		ieee80211_report_disconnect(sdata, frame_buf,
9199 					    sizeof(frame_buf), true,
9200 					    req->reason_code, false);
9201 		drv_mgd_complete_tx(sdata->local, sdata, &info);
9202 		return 0;
9203 	}
9204 
9205 	return -ENOTCONN;
9206 }
9207 
9208 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
9209 			   struct cfg80211_disassoc_request *req)
9210 {
9211 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9212 
9213 	if (!sdata->u.mgd.associated ||
9214 	    memcmp(sdata->vif.cfg.ap_addr, req->ap_addr, ETH_ALEN))
9215 		return -ENOTCONN;
9216 
9217 	sdata_info(sdata,
9218 		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
9219 		   req->ap_addr, req->reason_code,
9220 		   ieee80211_get_reason_code_string(req->reason_code));
9221 
9222 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
9223 			       req->reason_code, !req->local_state_change,
9224 			       frame_buf);
9225 
9226 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
9227 				    req->reason_code, false);
9228 
9229 	return 0;
9230 }
9231 
9232 void ieee80211_mgd_stop_link(struct ieee80211_link_data *link)
9233 {
9234 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
9235 			  &link->u.mgd.request_smps_work);
9236 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
9237 			  &link->u.mgd.recalc_smps);
9238 	wiphy_delayed_work_cancel(link->sdata->local->hw.wiphy,
9239 				  &link->u.mgd.csa.switch_work);
9240 }
9241 
9242 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
9243 {
9244 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9245 
9246 	/*
9247 	 * Make sure some work items will not run after this,
9248 	 * they will not do anything but might not have been
9249 	 * cancelled when disconnecting.
9250 	 */
9251 	wiphy_work_cancel(sdata->local->hw.wiphy,
9252 			  &ifmgd->monitor_work);
9253 	wiphy_work_cancel(sdata->local->hw.wiphy,
9254 			  &ifmgd->beacon_connection_loss_work);
9255 	wiphy_work_cancel(sdata->local->hw.wiphy,
9256 			  &ifmgd->csa_connection_drop_work);
9257 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
9258 				  &ifmgd->tdls_peer_del_work);
9259 
9260 	if (ifmgd->assoc_data)
9261 		ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
9262 	if (ifmgd->auth_data)
9263 		ieee80211_destroy_auth_data(sdata, false);
9264 	spin_lock_bh(&ifmgd->teardown_lock);
9265 	if (ifmgd->teardown_skb) {
9266 		kfree_skb(ifmgd->teardown_skb);
9267 		ifmgd->teardown_skb = NULL;
9268 		ifmgd->orig_teardown_skb = NULL;
9269 	}
9270 	kfree(ifmgd->assoc_req_ies);
9271 	ifmgd->assoc_req_ies = NULL;
9272 	ifmgd->assoc_req_ies_len = 0;
9273 	spin_unlock_bh(&ifmgd->teardown_lock);
9274 	del_timer_sync(&ifmgd->timer);
9275 }
9276 
9277 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
9278 			       enum nl80211_cqm_rssi_threshold_event rssi_event,
9279 			       s32 rssi_level,
9280 			       gfp_t gfp)
9281 {
9282 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
9283 
9284 	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
9285 
9286 	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
9287 }
9288 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
9289 
9290 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
9291 {
9292 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
9293 
9294 	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
9295 
9296 	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
9297 }
9298 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
9299 
9300 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
9301 					    int rssi_min_thold,
9302 					    int rssi_max_thold)
9303 {
9304 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
9305 
9306 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
9307 		return;
9308 
9309 	/*
9310 	 * Scale up threshold values before storing it, as the RSSI averaging
9311 	 * algorithm uses a scaled up value as well. Change this scaling
9312 	 * factor if the RSSI averaging algorithm changes.
9313 	 */
9314 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
9315 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
9316 }
9317 
9318 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
9319 				    int rssi_min_thold,
9320 				    int rssi_max_thold)
9321 {
9322 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
9323 
9324 	WARN_ON(rssi_min_thold == rssi_max_thold ||
9325 		rssi_min_thold > rssi_max_thold);
9326 
9327 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
9328 				       rssi_max_thold);
9329 }
9330 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
9331 
9332 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
9333 {
9334 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
9335 
9336 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
9337 }
9338 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
9339