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