xref: /linux/net/mac80211/mlme.c (revision 00389c58ffe993782a8ba4bb5a34a102b1f6fe24)
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 - 2021 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 static int max_nullfunc_tries = 2;
47 module_param(max_nullfunc_tries, int, 0644);
48 MODULE_PARM_DESC(max_nullfunc_tries,
49 		 "Maximum nullfunc tx tries before disconnecting (reason 4).");
50 
51 static int max_probe_tries = 5;
52 module_param(max_probe_tries, int, 0644);
53 MODULE_PARM_DESC(max_probe_tries,
54 		 "Maximum probe tries before disconnecting (reason 4).");
55 
56 /*
57  * Beacon loss timeout is calculated as N frames times the
58  * advertised beacon interval.  This may need to be somewhat
59  * higher than what hardware might detect to account for
60  * delays in the host processing frames. But since we also
61  * probe on beacon miss before declaring the connection lost
62  * default to what we want.
63  */
64 static int beacon_loss_count = 7;
65 module_param(beacon_loss_count, int, 0644);
66 MODULE_PARM_DESC(beacon_loss_count,
67 		 "Number of beacon intervals before we decide beacon was lost.");
68 
69 /*
70  * Time the connection can be idle before we probe
71  * it to see if we can still talk to the AP.
72  */
73 #define IEEE80211_CONNECTION_IDLE_TIME	(30 * HZ)
74 /*
75  * Time we wait for a probe response after sending
76  * a probe request because of beacon loss or for
77  * checking the connection still works.
78  */
79 static int probe_wait_ms = 500;
80 module_param(probe_wait_ms, int, 0644);
81 MODULE_PARM_DESC(probe_wait_ms,
82 		 "Maximum time(ms) to wait for probe response"
83 		 " before disconnecting (reason 4).");
84 
85 /*
86  * How many Beacon frames need to have been used in average signal strength
87  * before starting to indicate signal change events.
88  */
89 #define IEEE80211_SIGNAL_AVE_MIN_COUNT	4
90 
91 /*
92  * We can have multiple work items (and connection probing)
93  * scheduling this timer, but we need to take care to only
94  * reschedule it when it should fire _earlier_ than it was
95  * asked for before, or if it's not pending right now. This
96  * function ensures that. Note that it then is required to
97  * run this function for all timeouts after the first one
98  * has happened -- the work that runs from this timer will
99  * do that.
100  */
101 static void run_again(struct ieee80211_sub_if_data *sdata,
102 		      unsigned long timeout)
103 {
104 	sdata_assert_lock(sdata);
105 
106 	if (!timer_pending(&sdata->u.mgd.timer) ||
107 	    time_before(timeout, sdata->u.mgd.timer.expires))
108 		mod_timer(&sdata->u.mgd.timer, timeout);
109 }
110 
111 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
112 {
113 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
114 		return;
115 
116 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
117 		return;
118 
119 	mod_timer(&sdata->u.mgd.bcn_mon_timer,
120 		  round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
121 }
122 
123 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
124 {
125 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
126 
127 	if (unlikely(!ifmgd->associated))
128 		return;
129 
130 	if (ifmgd->probe_send_count)
131 		ifmgd->probe_send_count = 0;
132 
133 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
134 		return;
135 
136 	mod_timer(&ifmgd->conn_mon_timer,
137 		  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
138 }
139 
140 static int ecw2cw(int ecw)
141 {
142 	return (1 << ecw) - 1;
143 }
144 
145 static u32
146 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
147 			     struct ieee80211_supported_band *sband,
148 			     struct ieee80211_channel *channel,
149 			     u32 vht_cap_info,
150 			     const struct ieee80211_ht_operation *ht_oper,
151 			     const struct ieee80211_vht_operation *vht_oper,
152 			     const struct ieee80211_he_operation *he_oper,
153 			     const struct ieee80211_s1g_oper_ie *s1g_oper,
154 			     struct cfg80211_chan_def *chandef, bool tracking)
155 {
156 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157 	struct cfg80211_chan_def vht_chandef;
158 	struct ieee80211_sta_ht_cap sta_ht_cap;
159 	u32 ht_cfreq, ret;
160 
161 	memset(chandef, 0, sizeof(struct cfg80211_chan_def));
162 	chandef->chan = channel;
163 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
164 	chandef->center_freq1 = channel->center_freq;
165 	chandef->freq1_offset = channel->freq_offset;
166 
167 	if (channel->band == NL80211_BAND_6GHZ) {
168 		if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, chandef)) {
169 			mlme_dbg(sdata,
170 				 "bad 6 GHz operation, disabling HT/VHT/HE\n");
171 			ret = IEEE80211_STA_DISABLE_HT |
172 			      IEEE80211_STA_DISABLE_VHT |
173 			      IEEE80211_STA_DISABLE_HE;
174 		} else {
175 			ret = 0;
176 		}
177 		vht_chandef = *chandef;
178 		goto out;
179 	} else if (sband->band == NL80211_BAND_S1GHZ) {
180 		if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) {
181 			sdata_info(sdata,
182 				   "Missing S1G Operation Element? Trying operating == primary\n");
183 			chandef->width = ieee80211_s1g_channel_width(channel);
184 		}
185 
186 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_40MHZ |
187 		      IEEE80211_STA_DISABLE_VHT |
188 		      IEEE80211_STA_DISABLE_80P80MHZ |
189 		      IEEE80211_STA_DISABLE_160MHZ;
190 		goto out;
191 	}
192 
193 	memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
194 	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
195 
196 	if (!ht_oper || !sta_ht_cap.ht_supported) {
197 		mlme_dbg(sdata, "HT operation missing / HT not supported\n");
198 		ret = IEEE80211_STA_DISABLE_HT |
199 		      IEEE80211_STA_DISABLE_VHT |
200 		      IEEE80211_STA_DISABLE_HE;
201 		goto out;
202 	}
203 
204 	chandef->width = NL80211_CHAN_WIDTH_20;
205 
206 	ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
207 						  channel->band);
208 	/* check that channel matches the right operating channel */
209 	if (!tracking && channel->center_freq != ht_cfreq) {
210 		/*
211 		 * It's possible that some APs are confused here;
212 		 * Netgear WNDR3700 sometimes reports 4 higher than
213 		 * the actual channel in association responses, but
214 		 * since we look at probe response/beacon data here
215 		 * it should be OK.
216 		 */
217 		sdata_info(sdata,
218 			   "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
219 			   channel->center_freq, ht_cfreq,
220 			   ht_oper->primary_chan, channel->band);
221 		ret = IEEE80211_STA_DISABLE_HT |
222 		      IEEE80211_STA_DISABLE_VHT |
223 		      IEEE80211_STA_DISABLE_HE;
224 		goto out;
225 	}
226 
227 	/* check 40 MHz support, if we have it */
228 	if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
229 		ieee80211_chandef_ht_oper(ht_oper, chandef);
230 	} else {
231 		mlme_dbg(sdata, "40 MHz not supported\n");
232 		/* 40 MHz (and 80 MHz) must be supported for VHT */
233 		ret = IEEE80211_STA_DISABLE_VHT;
234 		/* also mark 40 MHz disabled */
235 		ret |= IEEE80211_STA_DISABLE_40MHZ;
236 		goto out;
237 	}
238 
239 	if (!vht_oper || !sband->vht_cap.vht_supported) {
240 		mlme_dbg(sdata, "VHT operation missing / VHT not supported\n");
241 		ret = IEEE80211_STA_DISABLE_VHT;
242 		goto out;
243 	}
244 
245 	vht_chandef = *chandef;
246 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
247 	    (le32_to_cpu(he_oper->he_oper_params) &
248 	     IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
249 		struct ieee80211_vht_operation he_oper_vht_cap;
250 
251 		/*
252 		 * Set only first 3 bytes (other 2 aren't used in
253 		 * ieee80211_chandef_vht_oper() anyway)
254 		 */
255 		memcpy(&he_oper_vht_cap, he_oper->optional, 3);
256 		he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
257 
258 		if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
259 						&he_oper_vht_cap, ht_oper,
260 						&vht_chandef)) {
261 			if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
262 				sdata_info(sdata,
263 					   "HE AP VHT information is invalid, disabling HE\n");
264 			ret = IEEE80211_STA_DISABLE_HE;
265 			goto out;
266 		}
267 	} else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
268 					       vht_cap_info,
269 					       vht_oper, ht_oper,
270 					       &vht_chandef)) {
271 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
272 			sdata_info(sdata,
273 				   "AP VHT information is invalid, disabling VHT\n");
274 		ret = IEEE80211_STA_DISABLE_VHT;
275 		goto out;
276 	}
277 
278 	if (!cfg80211_chandef_valid(&vht_chandef)) {
279 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
280 			sdata_info(sdata,
281 				   "AP VHT information is invalid, disabling VHT\n");
282 		ret = IEEE80211_STA_DISABLE_VHT;
283 		goto out;
284 	}
285 
286 	if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
287 		ret = 0;
288 		goto out;
289 	}
290 
291 	if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
292 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
293 			sdata_info(sdata,
294 				   "AP VHT information doesn't match HT, disabling VHT\n");
295 		ret = IEEE80211_STA_DISABLE_VHT;
296 		goto out;
297 	}
298 
299 	*chandef = vht_chandef;
300 
301 	ret = 0;
302 
303 out:
304 	/*
305 	 * When tracking the current AP, don't do any further checks if the
306 	 * new chandef is identical to the one we're currently using for the
307 	 * connection. This keeps us from playing ping-pong with regulatory,
308 	 * without it the following can happen (for example):
309 	 *  - connect to an AP with 80 MHz, world regdom allows 80 MHz
310 	 *  - AP advertises regdom US
311 	 *  - CRDA loads regdom US with 80 MHz prohibited (old database)
312 	 *  - the code below detects an unsupported channel, downgrades, and
313 	 *    we disconnect from the AP in the caller
314 	 *  - disconnect causes CRDA to reload world regdomain and the game
315 	 *    starts anew.
316 	 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
317 	 *
318 	 * It seems possible that there are still scenarios with CSA or real
319 	 * bandwidth changes where a this could happen, but those cases are
320 	 * less common and wouldn't completely prevent using the AP.
321 	 */
322 	if (tracking &&
323 	    cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
324 		return ret;
325 
326 	/* don't print the message below for VHT mismatch if VHT is disabled */
327 	if (ret & IEEE80211_STA_DISABLE_VHT)
328 		vht_chandef = *chandef;
329 
330 	/*
331 	 * Ignore the DISABLED flag when we're already connected and only
332 	 * tracking the APs beacon for bandwidth changes - otherwise we
333 	 * might get disconnected here if we connect to an AP, update our
334 	 * regulatory information based on the AP's country IE and the
335 	 * information we have is wrong/outdated and disables the channel
336 	 * that we're actually using for the connection to the AP.
337 	 */
338 	while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
339 					tracking ? 0 :
340 						   IEEE80211_CHAN_DISABLED)) {
341 		if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
342 			ret = IEEE80211_STA_DISABLE_HT |
343 			      IEEE80211_STA_DISABLE_VHT |
344 			      IEEE80211_STA_DISABLE_HE;
345 			break;
346 		}
347 
348 		ret |= ieee80211_chandef_downgrade(chandef);
349 	}
350 
351 	if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
352 						 IEEE80211_CHAN_NO_HE))
353 		ret |= IEEE80211_STA_DISABLE_HE;
354 
355 	if (chandef->width != vht_chandef.width && !tracking)
356 		sdata_info(sdata,
357 			   "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
358 
359 	WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
360 	return ret;
361 }
362 
363 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
364 			       struct sta_info *sta,
365 			       const struct ieee80211_ht_cap *ht_cap,
366 			       const struct ieee80211_vht_cap *vht_cap,
367 			       const struct ieee80211_ht_operation *ht_oper,
368 			       const struct ieee80211_vht_operation *vht_oper,
369 			       const struct ieee80211_he_operation *he_oper,
370 			       const struct ieee80211_s1g_oper_ie *s1g_oper,
371 			       const u8 *bssid, u32 *changed)
372 {
373 	struct ieee80211_local *local = sdata->local;
374 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
375 	struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
376 	struct ieee80211_supported_band *sband =
377 		local->hw.wiphy->bands[chan->band];
378 	struct cfg80211_chan_def chandef;
379 	u16 ht_opmode;
380 	u32 flags;
381 	u32 vht_cap_info = 0;
382 	int ret;
383 
384 	/* if HT was/is disabled, don't track any bandwidth changes */
385 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
386 		return 0;
387 
388 	/* don't check VHT if we associated as non-VHT station */
389 	if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
390 		vht_oper = NULL;
391 
392 	/* don't check HE if we associated as non-HE station */
393 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
394 	    !ieee80211_get_he_iftype_cap(sband,
395 					 ieee80211_vif_type_p2p(&sdata->vif)))
396 
397 		he_oper = NULL;
398 
399 	if (WARN_ON_ONCE(!sta))
400 		return -EINVAL;
401 
402 	/*
403 	 * if bss configuration changed store the new one -
404 	 * this may be applicable even if channel is identical
405 	 */
406 	ht_opmode = le16_to_cpu(ht_oper->operation_mode);
407 	if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
408 		*changed |= BSS_CHANGED_HT;
409 		sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
410 	}
411 
412 	if (vht_cap)
413 		vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
414 
415 	/* calculate new channel (type) based on HT/VHT/HE operation IEs */
416 	flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
417 					     ht_oper, vht_oper, he_oper,
418 					     s1g_oper, &chandef, true);
419 
420 	/*
421 	 * Downgrade the new channel if we associated with restricted
422 	 * capabilities. For example, if we associated as a 20 MHz STA
423 	 * to a 40 MHz AP (due to regulatory, capabilities or config
424 	 * reasons) then switching to a 40 MHz channel now won't do us
425 	 * any good -- we couldn't use it with the AP.
426 	 */
427 	if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
428 	    chandef.width == NL80211_CHAN_WIDTH_80P80)
429 		flags |= ieee80211_chandef_downgrade(&chandef);
430 	if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
431 	    chandef.width == NL80211_CHAN_WIDTH_160)
432 		flags |= ieee80211_chandef_downgrade(&chandef);
433 	if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
434 	    chandef.width > NL80211_CHAN_WIDTH_20)
435 		flags |= ieee80211_chandef_downgrade(&chandef);
436 
437 	if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
438 		return 0;
439 
440 	sdata_info(sdata,
441 		   "AP %pM changed bandwidth, new config is %d.%03d MHz, "
442 		   "width %d (%d.%03d/%d MHz)\n",
443 		   ifmgd->bssid, chandef.chan->center_freq,
444 		   chandef.chan->freq_offset, chandef.width,
445 		   chandef.center_freq1, chandef.freq1_offset,
446 		   chandef.center_freq2);
447 
448 	if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
449 				      IEEE80211_STA_DISABLE_VHT |
450 				      IEEE80211_STA_DISABLE_HE |
451 				      IEEE80211_STA_DISABLE_40MHZ |
452 				      IEEE80211_STA_DISABLE_80P80MHZ |
453 				      IEEE80211_STA_DISABLE_160MHZ)) ||
454 	    !cfg80211_chandef_valid(&chandef)) {
455 		sdata_info(sdata,
456 			   "AP %pM changed caps/bw in a way we can't support (0x%x/0x%x) - disconnect\n",
457 			   ifmgd->bssid, flags, ifmgd->flags);
458 		return -EINVAL;
459 	}
460 
461 	ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
462 
463 	if (ret) {
464 		sdata_info(sdata,
465 			   "AP %pM changed bandwidth to incompatible one - disconnect\n",
466 			   ifmgd->bssid);
467 		return ret;
468 	}
469 
470 	return 0;
471 }
472 
473 /* frame sending functions */
474 
475 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
476 				struct sk_buff *skb, u8 ap_ht_param,
477 				struct ieee80211_supported_band *sband,
478 				struct ieee80211_channel *channel,
479 				enum ieee80211_smps_mode smps)
480 {
481 	u8 *pos;
482 	u32 flags = channel->flags;
483 	u16 cap;
484 	struct ieee80211_sta_ht_cap ht_cap;
485 
486 	BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
487 
488 	memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
489 	ieee80211_apply_htcap_overrides(sdata, &ht_cap);
490 
491 	/* determine capability flags */
492 	cap = ht_cap.cap;
493 
494 	switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
495 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
496 		if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
497 			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
498 			cap &= ~IEEE80211_HT_CAP_SGI_40;
499 		}
500 		break;
501 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
502 		if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
503 			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
504 			cap &= ~IEEE80211_HT_CAP_SGI_40;
505 		}
506 		break;
507 	}
508 
509 	/*
510 	 * If 40 MHz was disabled associate as though we weren't
511 	 * capable of 40 MHz -- some broken APs will never fall
512 	 * back to trying to transmit in 20 MHz.
513 	 */
514 	if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
515 		cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
516 		cap &= ~IEEE80211_HT_CAP_SGI_40;
517 	}
518 
519 	/* set SM PS mode properly */
520 	cap &= ~IEEE80211_HT_CAP_SM_PS;
521 	switch (smps) {
522 	case IEEE80211_SMPS_AUTOMATIC:
523 	case IEEE80211_SMPS_NUM_MODES:
524 		WARN_ON(1);
525 		fallthrough;
526 	case IEEE80211_SMPS_OFF:
527 		cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
528 			IEEE80211_HT_CAP_SM_PS_SHIFT;
529 		break;
530 	case IEEE80211_SMPS_STATIC:
531 		cap |= WLAN_HT_CAP_SM_PS_STATIC <<
532 			IEEE80211_HT_CAP_SM_PS_SHIFT;
533 		break;
534 	case IEEE80211_SMPS_DYNAMIC:
535 		cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
536 			IEEE80211_HT_CAP_SM_PS_SHIFT;
537 		break;
538 	}
539 
540 	/* reserve and fill IE */
541 	pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
542 	ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
543 }
544 
545 /* This function determines vht capability flags for the association
546  * and builds the IE.
547  * Note - the function may set the owner of the MU-MIMO capability
548  */
549 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
550 				 struct sk_buff *skb,
551 				 struct ieee80211_supported_band *sband,
552 				 struct ieee80211_vht_cap *ap_vht_cap)
553 {
554 	struct ieee80211_local *local = sdata->local;
555 	u8 *pos;
556 	u32 cap;
557 	struct ieee80211_sta_vht_cap vht_cap;
558 	u32 mask, ap_bf_sts, our_bf_sts;
559 
560 	BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
561 
562 	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
563 	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
564 
565 	/* determine capability flags */
566 	cap = vht_cap.cap;
567 
568 	if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
569 		u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
570 
571 		cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
572 		if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
573 		    bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
574 			cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
575 	}
576 
577 	if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
578 		cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
579 		cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
580 	}
581 
582 	/*
583 	 * Some APs apparently get confused if our capabilities are better
584 	 * than theirs, so restrict what we advertise in the assoc request.
585 	 */
586 	if (!(ap_vht_cap->vht_cap_info &
587 			cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
588 		cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
589 			 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
590 	else if (!(ap_vht_cap->vht_cap_info &
591 			cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
592 		cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
593 
594 	/*
595 	 * If some other vif is using the MU-MIMO capability we cannot associate
596 	 * using MU-MIMO - this will lead to contradictions in the group-id
597 	 * mechanism.
598 	 * Ownership is defined since association request, in order to avoid
599 	 * simultaneous associations with MU-MIMO.
600 	 */
601 	if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
602 		bool disable_mu_mimo = false;
603 		struct ieee80211_sub_if_data *other;
604 
605 		list_for_each_entry_rcu(other, &local->interfaces, list) {
606 			if (other->vif.mu_mimo_owner) {
607 				disable_mu_mimo = true;
608 				break;
609 			}
610 		}
611 		if (disable_mu_mimo)
612 			cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
613 		else
614 			sdata->vif.mu_mimo_owner = true;
615 	}
616 
617 	mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
618 
619 	ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
620 	our_bf_sts = cap & mask;
621 
622 	if (ap_bf_sts < our_bf_sts) {
623 		cap &= ~mask;
624 		cap |= ap_bf_sts;
625 	}
626 
627 	/* reserve and fill IE */
628 	pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
629 	ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
630 }
631 
632 /* This function determines HE capability flags for the association
633  * and builds the IE.
634  */
635 static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
636 				struct sk_buff *skb,
637 				struct ieee80211_supported_band *sband)
638 {
639 	u8 *pos, *pre_he_pos;
640 	const struct ieee80211_sta_he_cap *he_cap = NULL;
641 	struct ieee80211_chanctx_conf *chanctx_conf;
642 	u8 he_cap_size;
643 	bool reg_cap = false;
644 
645 	rcu_read_lock();
646 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
647 	if (!WARN_ON_ONCE(!chanctx_conf))
648 		reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
649 						  &chanctx_conf->def,
650 						  IEEE80211_CHAN_NO_HE);
651 
652 	rcu_read_unlock();
653 
654 	he_cap = ieee80211_get_he_iftype_cap(sband,
655 					     ieee80211_vif_type_p2p(&sdata->vif));
656 	if (!he_cap || !chanctx_conf || !reg_cap)
657 		return;
658 
659 	/* get a max size estimate */
660 	he_cap_size =
661 		2 + 1 + sizeof(he_cap->he_cap_elem) +
662 		ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
663 		ieee80211_he_ppe_size(he_cap->ppe_thres[0],
664 				      he_cap->he_cap_elem.phy_cap_info);
665 	pos = skb_put(skb, he_cap_size);
666 	pre_he_pos = pos;
667 	pos = ieee80211_ie_build_he_cap(sdata->u.mgd.flags,
668 					pos, he_cap, pos + he_cap_size);
669 	/* trim excess if any */
670 	skb_trim(skb, skb->len - (pre_he_pos + he_cap_size - pos));
671 
672 	ieee80211_ie_build_he_6ghz_cap(sdata, skb);
673 }
674 
675 static int ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
676 {
677 	struct ieee80211_local *local = sdata->local;
678 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
679 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
680 	struct sk_buff *skb;
681 	struct ieee80211_mgmt *mgmt;
682 	u8 *pos, qos_info, *ie_start;
683 	size_t offset = 0, noffset;
684 	int i, count, rates_len, supp_rates_len, shift;
685 	u16 capab;
686 	struct ieee80211_supported_band *sband;
687 	struct ieee80211_chanctx_conf *chanctx_conf;
688 	struct ieee80211_channel *chan;
689 	u32 rates = 0;
690 	__le16 listen_int;
691 	struct element *ext_capa = NULL;
692 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
693 	const struct ieee80211_sband_iftype_data *iftd;
694 	struct ieee80211_prep_tx_info info = {};
695 	int ret;
696 
697 	/* we know it's writable, cast away the const */
698 	if (assoc_data->ie_len)
699 		ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
700 						      assoc_data->ie,
701 						      assoc_data->ie_len);
702 
703 	sdata_assert_lock(sdata);
704 
705 	rcu_read_lock();
706 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
707 	if (WARN_ON(!chanctx_conf)) {
708 		rcu_read_unlock();
709 		return -EINVAL;
710 	}
711 	chan = chanctx_conf->def.chan;
712 	rcu_read_unlock();
713 	sband = local->hw.wiphy->bands[chan->band];
714 	shift = ieee80211_vif_get_shift(&sdata->vif);
715 
716 	if (assoc_data->supp_rates_len) {
717 		/*
718 		 * Get all rates supported by the device and the AP as
719 		 * some APs don't like getting a superset of their rates
720 		 * in the association request (e.g. D-Link DAP 1353 in
721 		 * b-only mode)...
722 		 */
723 		rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
724 						     assoc_data->supp_rates,
725 						     assoc_data->supp_rates_len,
726 						     &rates);
727 	} else {
728 		/*
729 		 * In case AP not provide any supported rates information
730 		 * before association, we send information element(s) with
731 		 * all rates that we support.
732 		 */
733 		rates_len = 0;
734 		for (i = 0; i < sband->n_bitrates; i++) {
735 			rates |= BIT(i);
736 			rates_len++;
737 		}
738 	}
739 
740 	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
741 
742 	skb = alloc_skb(local->hw.extra_tx_headroom +
743 			sizeof(*mgmt) + /* bit too much but doesn't matter */
744 			2 + assoc_data->ssid_len + /* SSID */
745 			4 + rates_len + /* (extended) rates */
746 			4 + /* power capability */
747 			2 + 2 * sband->n_channels + /* supported channels */
748 			2 + sizeof(struct ieee80211_ht_cap) + /* HT */
749 			2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
750 			2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
751 				sizeof(struct ieee80211_he_mcs_nss_supp) +
752 				IEEE80211_HE_PPE_THRES_MAX_LEN +
753 			2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
754 			assoc_data->ie_len + /* extra IEs */
755 			(assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
756 			9 + /* WMM */
757 			(iftd ? iftd->vendor_elems.len : 0),
758 			GFP_KERNEL);
759 	if (!skb)
760 		return -ENOMEM;
761 
762 	skb_reserve(skb, local->hw.extra_tx_headroom);
763 
764 	capab = WLAN_CAPABILITY_ESS;
765 
766 	if (sband->band == NL80211_BAND_2GHZ) {
767 		capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
768 		capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
769 	}
770 
771 	if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
772 		capab |= WLAN_CAPABILITY_PRIVACY;
773 
774 	if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
775 	    ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
776 		capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
777 
778 	if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
779 		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
780 
781 	mgmt = skb_put_zero(skb, 24);
782 	memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
783 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
784 	memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
785 
786 	listen_int = cpu_to_le16(sband->band == NL80211_BAND_S1GHZ ?
787 			ieee80211_encode_usf(local->hw.conf.listen_interval) :
788 			local->hw.conf.listen_interval);
789 	if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
790 		skb_put(skb, 10);
791 		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
792 						  IEEE80211_STYPE_REASSOC_REQ);
793 		mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
794 		mgmt->u.reassoc_req.listen_interval = listen_int;
795 		memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
796 		       ETH_ALEN);
797 		info.subtype = IEEE80211_STYPE_REASSOC_REQ;
798 	} else {
799 		skb_put(skb, 4);
800 		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
801 						  IEEE80211_STYPE_ASSOC_REQ);
802 		mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
803 		mgmt->u.assoc_req.listen_interval = listen_int;
804 		info.subtype = IEEE80211_STYPE_ASSOC_REQ;
805 	}
806 
807 	/* SSID */
808 	pos = skb_put(skb, 2 + assoc_data->ssid_len);
809 	ie_start = pos;
810 	*pos++ = WLAN_EID_SSID;
811 	*pos++ = assoc_data->ssid_len;
812 	memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
813 
814 	if (sband->band == NL80211_BAND_S1GHZ)
815 		goto skip_rates;
816 
817 	/* add all rates which were marked to be used above */
818 	supp_rates_len = rates_len;
819 	if (supp_rates_len > 8)
820 		supp_rates_len = 8;
821 
822 	pos = skb_put(skb, supp_rates_len + 2);
823 	*pos++ = WLAN_EID_SUPP_RATES;
824 	*pos++ = supp_rates_len;
825 
826 	count = 0;
827 	for (i = 0; i < sband->n_bitrates; i++) {
828 		if (BIT(i) & rates) {
829 			int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
830 						5 * (1 << shift));
831 			*pos++ = (u8) rate;
832 			if (++count == 8)
833 				break;
834 		}
835 	}
836 
837 	if (rates_len > count) {
838 		pos = skb_put(skb, rates_len - count + 2);
839 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
840 		*pos++ = rates_len - count;
841 
842 		for (i++; i < sband->n_bitrates; i++) {
843 			if (BIT(i) & rates) {
844 				int rate;
845 				rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
846 						    5 * (1 << shift));
847 				*pos++ = (u8) rate;
848 			}
849 		}
850 	}
851 
852 skip_rates:
853 	if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
854 	    capab & WLAN_CAPABILITY_RADIO_MEASURE) {
855 		pos = skb_put(skb, 4);
856 		*pos++ = WLAN_EID_PWR_CAPABILITY;
857 		*pos++ = 2;
858 		*pos++ = 0; /* min tx power */
859 		 /* max tx power */
860 		*pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
861 	}
862 
863 	/*
864 	 * Per spec, we shouldn't include the list of channels if we advertise
865 	 * support for extended channel switching, but we've always done that;
866 	 * (for now?) apply this restriction only on the (new) 6 GHz band.
867 	 */
868 	if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
869 	    (sband->band != NL80211_BAND_6GHZ ||
870 	     !ext_capa || ext_capa->datalen < 1 ||
871 	     !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
872 		/* TODO: get this in reg domain format */
873 		pos = skb_put(skb, 2 * sband->n_channels + 2);
874 		*pos++ = WLAN_EID_SUPPORTED_CHANNELS;
875 		*pos++ = 2 * sband->n_channels;
876 		for (i = 0; i < sband->n_channels; i++) {
877 			*pos++ = ieee80211_frequency_to_channel(
878 					sband->channels[i].center_freq);
879 			*pos++ = 1; /* one channel in the subband*/
880 		}
881 	}
882 
883 	/* Set MBSSID support for HE AP if needed */
884 	if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
885 	    !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
886 	    ext_capa && ext_capa->datalen >= 3)
887 		ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
888 
889 	/* if present, add any custom IEs that go before HT */
890 	if (assoc_data->ie_len) {
891 		static const u8 before_ht[] = {
892 			WLAN_EID_SSID,
893 			WLAN_EID_SUPP_RATES,
894 			WLAN_EID_EXT_SUPP_RATES,
895 			WLAN_EID_PWR_CAPABILITY,
896 			WLAN_EID_SUPPORTED_CHANNELS,
897 			WLAN_EID_RSN,
898 			WLAN_EID_QOS_CAPA,
899 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
900 			WLAN_EID_MOBILITY_DOMAIN,
901 			WLAN_EID_FAST_BSS_TRANSITION,	/* reassoc only */
902 			WLAN_EID_RIC_DATA,		/* reassoc only */
903 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
904 		};
905 		static const u8 after_ric[] = {
906 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
907 			WLAN_EID_HT_CAPABILITY,
908 			WLAN_EID_BSS_COEX_2040,
909 			/* luckily this is almost always there */
910 			WLAN_EID_EXT_CAPABILITY,
911 			WLAN_EID_QOS_TRAFFIC_CAPA,
912 			WLAN_EID_TIM_BCAST_REQ,
913 			WLAN_EID_INTERWORKING,
914 			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
915 			WLAN_EID_VHT_CAPABILITY,
916 			WLAN_EID_OPMODE_NOTIF,
917 		};
918 
919 		noffset = ieee80211_ie_split_ric(assoc_data->ie,
920 						 assoc_data->ie_len,
921 						 before_ht,
922 						 ARRAY_SIZE(before_ht),
923 						 after_ric,
924 						 ARRAY_SIZE(after_ric),
925 						 offset);
926 		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
927 		offset = noffset;
928 	}
929 
930 	if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
931 			 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
932 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
933 
934 	if (sband->band != NL80211_BAND_6GHZ &&
935 	    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
936 		ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
937 				    sband, chan, sdata->smps_mode);
938 
939 	/* if present, add any custom IEs that go before VHT */
940 	if (assoc_data->ie_len) {
941 		static const u8 before_vht[] = {
942 			/*
943 			 * no need to list the ones split off before HT
944 			 * or generated here
945 			 */
946 			WLAN_EID_BSS_COEX_2040,
947 			WLAN_EID_EXT_CAPABILITY,
948 			WLAN_EID_QOS_TRAFFIC_CAPA,
949 			WLAN_EID_TIM_BCAST_REQ,
950 			WLAN_EID_INTERWORKING,
951 			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
952 		};
953 
954 		/* RIC already taken above, so no need to handle here anymore */
955 		noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
956 					     before_vht, ARRAY_SIZE(before_vht),
957 					     offset);
958 		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
959 		offset = noffset;
960 	}
961 
962 	/* if present, add any custom IEs that go before HE */
963 	if (assoc_data->ie_len) {
964 		static const u8 before_he[] = {
965 			/*
966 			 * no need to list the ones split off before VHT
967 			 * or generated here
968 			 */
969 			WLAN_EID_OPMODE_NOTIF,
970 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
971 			/* 11ai elements */
972 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
973 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
974 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
975 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
976 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
977 			/* TODO: add 11ah/11aj/11ak elements */
978 		};
979 
980 		/* RIC already taken above, so no need to handle here anymore */
981 		noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
982 					     before_he, ARRAY_SIZE(before_he),
983 					     offset);
984 		pos = skb_put(skb, noffset - offset);
985 		memcpy(pos, assoc_data->ie + offset, noffset - offset);
986 		offset = noffset;
987 	}
988 
989 	if (sband->band != NL80211_BAND_6GHZ &&
990 	    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
991 		ieee80211_add_vht_ie(sdata, skb, sband,
992 				     &assoc_data->ap_vht_cap);
993 
994 	/*
995 	 * If AP doesn't support HT, mark HE as disabled.
996 	 * If on the 5GHz band, make sure it supports VHT.
997 	 */
998 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
999 	    (sband->band == NL80211_BAND_5GHZ &&
1000 	     ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
1001 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
1002 
1003 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1004 		ieee80211_add_he_ie(sdata, skb, sband);
1005 
1006 	/* if present, add any custom non-vendor IEs that go after HE */
1007 	if (assoc_data->ie_len) {
1008 		noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1009 						    assoc_data->ie_len,
1010 						    offset);
1011 		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1012 		offset = noffset;
1013 	}
1014 
1015 	if (assoc_data->wmm) {
1016 		if (assoc_data->uapsd) {
1017 			qos_info = ifmgd->uapsd_queues;
1018 			qos_info |= (ifmgd->uapsd_max_sp_len <<
1019 				     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1020 		} else {
1021 			qos_info = 0;
1022 		}
1023 
1024 		pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
1025 	}
1026 
1027 	if (sband->band == NL80211_BAND_S1GHZ) {
1028 		ieee80211_add_aid_request_ie(sdata, skb);
1029 		ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
1030 	}
1031 
1032 	if (iftd && iftd->vendor_elems.data && iftd->vendor_elems.len)
1033 		skb_put_data(skb, iftd->vendor_elems.data, iftd->vendor_elems.len);
1034 
1035 	/* add any remaining custom (i.e. vendor specific here) IEs */
1036 	if (assoc_data->ie_len) {
1037 		noffset = assoc_data->ie_len;
1038 		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1039 	}
1040 
1041 	if (assoc_data->fils_kek_len) {
1042 		ret = fils_encrypt_assoc_req(skb, assoc_data);
1043 		if (ret < 0) {
1044 			dev_kfree_skb(skb);
1045 			return ret;
1046 		}
1047 	}
1048 
1049 	pos = skb_tail_pointer(skb);
1050 	kfree(ifmgd->assoc_req_ies);
1051 	ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1052 	if (!ifmgd->assoc_req_ies) {
1053 		dev_kfree_skb(skb);
1054 		return -ENOMEM;
1055 	}
1056 
1057 	ifmgd->assoc_req_ies_len = pos - ie_start;
1058 
1059 	drv_mgd_prepare_tx(local, sdata, &info);
1060 
1061 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1062 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1063 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1064 						IEEE80211_TX_INTFL_MLME_CONN_TX;
1065 	ieee80211_tx_skb(sdata, skb);
1066 
1067 	return 0;
1068 }
1069 
1070 void ieee80211_send_pspoll(struct ieee80211_local *local,
1071 			   struct ieee80211_sub_if_data *sdata)
1072 {
1073 	struct ieee80211_pspoll *pspoll;
1074 	struct sk_buff *skb;
1075 
1076 	skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1077 	if (!skb)
1078 		return;
1079 
1080 	pspoll = (struct ieee80211_pspoll *) skb->data;
1081 	pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1082 
1083 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1084 	ieee80211_tx_skb(sdata, skb);
1085 }
1086 
1087 void ieee80211_send_nullfunc(struct ieee80211_local *local,
1088 			     struct ieee80211_sub_if_data *sdata,
1089 			     bool powersave)
1090 {
1091 	struct sk_buff *skb;
1092 	struct ieee80211_hdr_3addr *nullfunc;
1093 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1094 
1095 	skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1096 		!ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
1097 	if (!skb)
1098 		return;
1099 
1100 	nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1101 	if (powersave)
1102 		nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1103 
1104 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1105 					IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1106 
1107 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1108 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1109 
1110 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1111 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1112 
1113 	ieee80211_tx_skb(sdata, skb);
1114 }
1115 
1116 void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1117 				   struct ieee80211_sub_if_data *sdata)
1118 {
1119 	struct sk_buff *skb;
1120 	struct ieee80211_hdr *nullfunc;
1121 	__le16 fc;
1122 
1123 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1124 		return;
1125 
1126 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1127 	if (!skb)
1128 		return;
1129 
1130 	skb_reserve(skb, local->hw.extra_tx_headroom);
1131 
1132 	nullfunc = skb_put_zero(skb, 30);
1133 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1134 			 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1135 	nullfunc->frame_control = fc;
1136 	memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1137 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1138 	memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1139 	memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1140 
1141 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1142 	ieee80211_tx_skb(sdata, skb);
1143 }
1144 
1145 /* spectrum management related things */
1146 static void ieee80211_chswitch_work(struct work_struct *work)
1147 {
1148 	struct ieee80211_sub_if_data *sdata =
1149 		container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1150 	struct ieee80211_local *local = sdata->local;
1151 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1152 	int ret;
1153 
1154 	if (!ieee80211_sdata_running(sdata))
1155 		return;
1156 
1157 	sdata_lock(sdata);
1158 	mutex_lock(&local->mtx);
1159 	mutex_lock(&local->chanctx_mtx);
1160 
1161 	if (!ifmgd->associated)
1162 		goto out;
1163 
1164 	if (!sdata->vif.csa_active)
1165 		goto out;
1166 
1167 	/*
1168 	 * using reservation isn't immediate as it may be deferred until later
1169 	 * with multi-vif. once reservation is complete it will re-schedule the
1170 	 * work with no reserved_chanctx so verify chandef to check if it
1171 	 * completed successfully
1172 	 */
1173 
1174 	if (sdata->reserved_chanctx) {
1175 		/*
1176 		 * with multi-vif csa driver may call ieee80211_csa_finish()
1177 		 * many times while waiting for other interfaces to use their
1178 		 * reservations
1179 		 */
1180 		if (sdata->reserved_ready)
1181 			goto out;
1182 
1183 		ret = ieee80211_vif_use_reserved_context(sdata);
1184 		if (ret) {
1185 			sdata_info(sdata,
1186 				   "failed to use reserved channel context, disconnecting (err=%d)\n",
1187 				   ret);
1188 			ieee80211_queue_work(&sdata->local->hw,
1189 					     &ifmgd->csa_connection_drop_work);
1190 			goto out;
1191 		}
1192 
1193 		goto out;
1194 	}
1195 
1196 	if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1197 					&sdata->csa_chandef)) {
1198 		sdata_info(sdata,
1199 			   "failed to finalize channel switch, disconnecting\n");
1200 		ieee80211_queue_work(&sdata->local->hw,
1201 				     &ifmgd->csa_connection_drop_work);
1202 		goto out;
1203 	}
1204 
1205 	ifmgd->csa_waiting_bcn = true;
1206 
1207 	ieee80211_sta_reset_beacon_monitor(sdata);
1208 	ieee80211_sta_reset_conn_monitor(sdata);
1209 
1210 out:
1211 	mutex_unlock(&local->chanctx_mtx);
1212 	mutex_unlock(&local->mtx);
1213 	sdata_unlock(sdata);
1214 }
1215 
1216 static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1217 {
1218 	struct ieee80211_local *local = sdata->local;
1219 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1220 	int ret;
1221 
1222 	sdata_assert_lock(sdata);
1223 
1224 	WARN_ON(!sdata->vif.csa_active);
1225 
1226 	if (sdata->csa_block_tx) {
1227 		ieee80211_wake_vif_queues(local, sdata,
1228 					  IEEE80211_QUEUE_STOP_REASON_CSA);
1229 		sdata->csa_block_tx = false;
1230 	}
1231 
1232 	sdata->vif.csa_active = false;
1233 	ifmgd->csa_waiting_bcn = false;
1234 	/*
1235 	 * If the CSA IE is still present on the beacon after the switch,
1236 	 * we need to consider it as a new CSA (possibly to self).
1237 	 */
1238 	ifmgd->beacon_crc_valid = false;
1239 
1240 	ret = drv_post_channel_switch(sdata);
1241 	if (ret) {
1242 		sdata_info(sdata,
1243 			   "driver post channel switch failed, disconnecting\n");
1244 		ieee80211_queue_work(&local->hw,
1245 				     &ifmgd->csa_connection_drop_work);
1246 		return;
1247 	}
1248 
1249 	cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1250 }
1251 
1252 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1253 {
1254 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1255 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1256 
1257 	trace_api_chswitch_done(sdata, success);
1258 	if (!success) {
1259 		sdata_info(sdata,
1260 			   "driver channel switch failed, disconnecting\n");
1261 		ieee80211_queue_work(&sdata->local->hw,
1262 				     &ifmgd->csa_connection_drop_work);
1263 	} else {
1264 		ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1265 	}
1266 }
1267 EXPORT_SYMBOL(ieee80211_chswitch_done);
1268 
1269 static void ieee80211_chswitch_timer(struct timer_list *t)
1270 {
1271 	struct ieee80211_sub_if_data *sdata =
1272 		from_timer(sdata, t, u.mgd.chswitch_timer);
1273 
1274 	ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1275 }
1276 
1277 static void
1278 ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1279 {
1280 	struct ieee80211_local *local = sdata->local;
1281 
1282 	if (!local->ops->abort_channel_switch)
1283 		return;
1284 
1285 	mutex_lock(&local->mtx);
1286 
1287 	mutex_lock(&local->chanctx_mtx);
1288 	ieee80211_vif_unreserve_chanctx(sdata);
1289 	mutex_unlock(&local->chanctx_mtx);
1290 
1291 	if (sdata->csa_block_tx)
1292 		ieee80211_wake_vif_queues(local, sdata,
1293 					  IEEE80211_QUEUE_STOP_REASON_CSA);
1294 
1295 	sdata->csa_block_tx = false;
1296 	sdata->vif.csa_active = false;
1297 
1298 	mutex_unlock(&local->mtx);
1299 
1300 	drv_abort_channel_switch(sdata);
1301 }
1302 
1303 static void
1304 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1305 				 u64 timestamp, u32 device_timestamp,
1306 				 struct ieee802_11_elems *elems,
1307 				 bool beacon)
1308 {
1309 	struct ieee80211_local *local = sdata->local;
1310 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1311 	struct cfg80211_bss *cbss = ifmgd->associated;
1312 	struct ieee80211_chanctx_conf *conf;
1313 	struct ieee80211_chanctx *chanctx;
1314 	enum nl80211_band current_band;
1315 	struct ieee80211_csa_ie csa_ie;
1316 	struct ieee80211_channel_switch ch_switch;
1317 	struct ieee80211_bss *bss;
1318 	int res;
1319 
1320 	sdata_assert_lock(sdata);
1321 
1322 	if (!cbss)
1323 		return;
1324 
1325 	if (local->scanning)
1326 		return;
1327 
1328 	current_band = cbss->channel->band;
1329 	bss = (void *)cbss->priv;
1330 	res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1331 					   bss->vht_cap_info,
1332 					   ifmgd->flags,
1333 					   ifmgd->associated->bssid, &csa_ie);
1334 
1335 	if (!res) {
1336 		ch_switch.timestamp = timestamp;
1337 		ch_switch.device_timestamp = device_timestamp;
1338 		ch_switch.block_tx = csa_ie.mode;
1339 		ch_switch.chandef = csa_ie.chandef;
1340 		ch_switch.count = csa_ie.count;
1341 		ch_switch.delay = csa_ie.max_switch_time;
1342 	}
1343 
1344 	if (res < 0)
1345 		goto lock_and_drop_connection;
1346 
1347 	if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1348 		if (res)
1349 			ieee80211_sta_abort_chanswitch(sdata);
1350 		else
1351 			drv_channel_switch_rx_beacon(sdata, &ch_switch);
1352 		return;
1353 	} else if (sdata->vif.csa_active || res) {
1354 		/* disregard subsequent announcements if already processing */
1355 		return;
1356 	}
1357 
1358 	if (sdata->vif.bss_conf.chandef.chan->band !=
1359 	    csa_ie.chandef.chan->band) {
1360 		sdata_info(sdata,
1361 			   "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1362 			   ifmgd->associated->bssid,
1363 			   csa_ie.chandef.chan->center_freq,
1364 			   csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1365 			   csa_ie.chandef.center_freq2);
1366 		goto lock_and_drop_connection;
1367 	}
1368 
1369 	if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1370 				     IEEE80211_CHAN_DISABLED)) {
1371 		sdata_info(sdata,
1372 			   "AP %pM switches to unsupported channel "
1373 			   "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1374 			   "disconnecting\n",
1375 			   ifmgd->associated->bssid,
1376 			   csa_ie.chandef.chan->center_freq,
1377 			   csa_ie.chandef.chan->freq_offset,
1378 			   csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1379 			   csa_ie.chandef.freq1_offset,
1380 			   csa_ie.chandef.center_freq2);
1381 		goto lock_and_drop_connection;
1382 	}
1383 
1384 	if (cfg80211_chandef_identical(&csa_ie.chandef,
1385 				       &sdata->vif.bss_conf.chandef) &&
1386 	    (!csa_ie.mode || !beacon)) {
1387 		if (ifmgd->csa_ignored_same_chan)
1388 			return;
1389 		sdata_info(sdata,
1390 			   "AP %pM tries to chanswitch to same channel, ignore\n",
1391 			   ifmgd->associated->bssid);
1392 		ifmgd->csa_ignored_same_chan = true;
1393 		return;
1394 	}
1395 
1396 	/*
1397 	 * Drop all TDLS peers - either we disconnect or move to a different
1398 	 * channel from this point on. There's no telling what our peer will do.
1399 	 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1400 	 * have an incompatible wider chandef.
1401 	 */
1402 	ieee80211_teardown_tdls_peers(sdata);
1403 
1404 	mutex_lock(&local->mtx);
1405 	mutex_lock(&local->chanctx_mtx);
1406 	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1407 					 lockdep_is_held(&local->chanctx_mtx));
1408 	if (!conf) {
1409 		sdata_info(sdata,
1410 			   "no channel context assigned to vif?, disconnecting\n");
1411 		goto drop_connection;
1412 	}
1413 
1414 	chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1415 
1416 	if (local->use_chanctx &&
1417 	    !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1418 		sdata_info(sdata,
1419 			   "driver doesn't support chan-switch with channel contexts\n");
1420 		goto drop_connection;
1421 	}
1422 
1423 	if (drv_pre_channel_switch(sdata, &ch_switch)) {
1424 		sdata_info(sdata,
1425 			   "preparing for channel switch failed, disconnecting\n");
1426 		goto drop_connection;
1427 	}
1428 
1429 	res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1430 					    chanctx->mode, false);
1431 	if (res) {
1432 		sdata_info(sdata,
1433 			   "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1434 			   res);
1435 		goto drop_connection;
1436 	}
1437 	mutex_unlock(&local->chanctx_mtx);
1438 
1439 	sdata->vif.csa_active = true;
1440 	sdata->csa_chandef = csa_ie.chandef;
1441 	sdata->csa_block_tx = csa_ie.mode;
1442 	ifmgd->csa_ignored_same_chan = false;
1443 	ifmgd->beacon_crc_valid = false;
1444 
1445 	if (sdata->csa_block_tx)
1446 		ieee80211_stop_vif_queues(local, sdata,
1447 					  IEEE80211_QUEUE_STOP_REASON_CSA);
1448 	mutex_unlock(&local->mtx);
1449 
1450 	cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1451 					  csa_ie.count, csa_ie.mode);
1452 
1453 	if (local->ops->channel_switch) {
1454 		/* use driver's channel switch callback */
1455 		drv_channel_switch(local, sdata, &ch_switch);
1456 		return;
1457 	}
1458 
1459 	/* channel switch handled in software */
1460 	if (csa_ie.count <= 1)
1461 		ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1462 	else
1463 		mod_timer(&ifmgd->chswitch_timer,
1464 			  TU_TO_EXP_TIME((csa_ie.count - 1) *
1465 					 cbss->beacon_interval));
1466 	return;
1467  lock_and_drop_connection:
1468 	mutex_lock(&local->mtx);
1469 	mutex_lock(&local->chanctx_mtx);
1470  drop_connection:
1471 	/*
1472 	 * This is just so that the disconnect flow will know that
1473 	 * we were trying to switch channel and failed. In case the
1474 	 * mode is 1 (we are not allowed to Tx), we will know not to
1475 	 * send a deauthentication frame. Those two fields will be
1476 	 * reset when the disconnection worker runs.
1477 	 */
1478 	sdata->vif.csa_active = true;
1479 	sdata->csa_block_tx = csa_ie.mode;
1480 
1481 	ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1482 	mutex_unlock(&local->chanctx_mtx);
1483 	mutex_unlock(&local->mtx);
1484 }
1485 
1486 static bool
1487 ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1488 				 struct ieee80211_channel *channel,
1489 				 const u8 *country_ie, u8 country_ie_len,
1490 				 const u8 *pwr_constr_elem,
1491 				 int *chan_pwr, int *pwr_reduction)
1492 {
1493 	struct ieee80211_country_ie_triplet *triplet;
1494 	int chan = ieee80211_frequency_to_channel(channel->center_freq);
1495 	int i, chan_increment;
1496 	bool have_chan_pwr = false;
1497 
1498 	/* Invalid IE */
1499 	if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1500 		return false;
1501 
1502 	triplet = (void *)(country_ie + 3);
1503 	country_ie_len -= 3;
1504 
1505 	switch (channel->band) {
1506 	default:
1507 		WARN_ON_ONCE(1);
1508 		fallthrough;
1509 	case NL80211_BAND_2GHZ:
1510 	case NL80211_BAND_60GHZ:
1511 	case NL80211_BAND_LC:
1512 		chan_increment = 1;
1513 		break;
1514 	case NL80211_BAND_5GHZ:
1515 		chan_increment = 4;
1516 		break;
1517 	case NL80211_BAND_6GHZ:
1518 		/*
1519 		 * In the 6 GHz band, the "maximum transmit power level"
1520 		 * field in the triplets is reserved, and thus will be
1521 		 * zero and we shouldn't use it to control TX power.
1522 		 * The actual TX power will be given in the transmit
1523 		 * power envelope element instead.
1524 		 */
1525 		return false;
1526 	}
1527 
1528 	/* find channel */
1529 	while (country_ie_len >= 3) {
1530 		u8 first_channel = triplet->chans.first_channel;
1531 
1532 		if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1533 			goto next;
1534 
1535 		for (i = 0; i < triplet->chans.num_channels; i++) {
1536 			if (first_channel + i * chan_increment == chan) {
1537 				have_chan_pwr = true;
1538 				*chan_pwr = triplet->chans.max_power;
1539 				break;
1540 			}
1541 		}
1542 		if (have_chan_pwr)
1543 			break;
1544 
1545  next:
1546 		triplet++;
1547 		country_ie_len -= 3;
1548 	}
1549 
1550 	if (have_chan_pwr && pwr_constr_elem)
1551 		*pwr_reduction = *pwr_constr_elem;
1552 	else
1553 		*pwr_reduction = 0;
1554 
1555 	return have_chan_pwr;
1556 }
1557 
1558 static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1559 				      struct ieee80211_channel *channel,
1560 				      const u8 *cisco_dtpc_ie,
1561 				      int *pwr_level)
1562 {
1563 	/* From practical testing, the first data byte of the DTPC element
1564 	 * seems to contain the requested dBm level, and the CLI on Cisco
1565 	 * APs clearly state the range is -127 to 127 dBm, which indicates
1566 	 * a signed byte, although it seemingly never actually goes negative.
1567 	 * The other byte seems to always be zero.
1568 	 */
1569 	*pwr_level = (__s8)cisco_dtpc_ie[4];
1570 }
1571 
1572 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1573 				       struct ieee80211_channel *channel,
1574 				       struct ieee80211_mgmt *mgmt,
1575 				       const u8 *country_ie, u8 country_ie_len,
1576 				       const u8 *pwr_constr_ie,
1577 				       const u8 *cisco_dtpc_ie)
1578 {
1579 	bool has_80211h_pwr = false, has_cisco_pwr = false;
1580 	int chan_pwr = 0, pwr_reduction_80211h = 0;
1581 	int pwr_level_cisco, pwr_level_80211h;
1582 	int new_ap_level;
1583 	__le16 capab = mgmt->u.probe_resp.capab_info;
1584 
1585 	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
1586 		return 0;	/* TODO */
1587 
1588 	if (country_ie &&
1589 	    (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1590 	     capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1591 		has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1592 			sdata, channel, country_ie, country_ie_len,
1593 			pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1594 		pwr_level_80211h =
1595 			max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1596 	}
1597 
1598 	if (cisco_dtpc_ie) {
1599 		ieee80211_find_cisco_dtpc(
1600 			sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1601 		has_cisco_pwr = true;
1602 	}
1603 
1604 	if (!has_80211h_pwr && !has_cisco_pwr)
1605 		return 0;
1606 
1607 	/* If we have both 802.11h and Cisco DTPC, apply both limits
1608 	 * by picking the smallest of the two power levels advertised.
1609 	 */
1610 	if (has_80211h_pwr &&
1611 	    (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1612 		new_ap_level = pwr_level_80211h;
1613 
1614 		if (sdata->ap_power_level == new_ap_level)
1615 			return 0;
1616 
1617 		sdata_dbg(sdata,
1618 			  "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1619 			  pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1620 			  sdata->u.mgd.bssid);
1621 	} else {  /* has_cisco_pwr is always true here. */
1622 		new_ap_level = pwr_level_cisco;
1623 
1624 		if (sdata->ap_power_level == new_ap_level)
1625 			return 0;
1626 
1627 		sdata_dbg(sdata,
1628 			  "Limiting TX power to %d dBm as advertised by %pM\n",
1629 			  pwr_level_cisco, sdata->u.mgd.bssid);
1630 	}
1631 
1632 	sdata->ap_power_level = new_ap_level;
1633 	if (__ieee80211_recalc_txpower(sdata))
1634 		return BSS_CHANGED_TXPOWER;
1635 	return 0;
1636 }
1637 
1638 /* powersave */
1639 static void ieee80211_enable_ps(struct ieee80211_local *local,
1640 				struct ieee80211_sub_if_data *sdata)
1641 {
1642 	struct ieee80211_conf *conf = &local->hw.conf;
1643 
1644 	/*
1645 	 * If we are scanning right now then the parameters will
1646 	 * take effect when scan finishes.
1647 	 */
1648 	if (local->scanning)
1649 		return;
1650 
1651 	if (conf->dynamic_ps_timeout > 0 &&
1652 	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1653 		mod_timer(&local->dynamic_ps_timer, jiffies +
1654 			  msecs_to_jiffies(conf->dynamic_ps_timeout));
1655 	} else {
1656 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1657 			ieee80211_send_nullfunc(local, sdata, true);
1658 
1659 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1660 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1661 			return;
1662 
1663 		conf->flags |= IEEE80211_CONF_PS;
1664 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1665 	}
1666 }
1667 
1668 static void ieee80211_change_ps(struct ieee80211_local *local)
1669 {
1670 	struct ieee80211_conf *conf = &local->hw.conf;
1671 
1672 	if (local->ps_sdata) {
1673 		ieee80211_enable_ps(local, local->ps_sdata);
1674 	} else if (conf->flags & IEEE80211_CONF_PS) {
1675 		conf->flags &= ~IEEE80211_CONF_PS;
1676 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1677 		del_timer_sync(&local->dynamic_ps_timer);
1678 		cancel_work_sync(&local->dynamic_ps_enable_work);
1679 	}
1680 }
1681 
1682 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1683 {
1684 	struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1685 	struct sta_info *sta = NULL;
1686 	bool authorized = false;
1687 
1688 	if (!mgd->powersave)
1689 		return false;
1690 
1691 	if (mgd->broken_ap)
1692 		return false;
1693 
1694 	if (!mgd->associated)
1695 		return false;
1696 
1697 	if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1698 		return false;
1699 
1700 	if (!mgd->have_beacon)
1701 		return false;
1702 
1703 	rcu_read_lock();
1704 	sta = sta_info_get(sdata, mgd->bssid);
1705 	if (sta)
1706 		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1707 	rcu_read_unlock();
1708 
1709 	return authorized;
1710 }
1711 
1712 /* need to hold RTNL or interface lock */
1713 void ieee80211_recalc_ps(struct ieee80211_local *local)
1714 {
1715 	struct ieee80211_sub_if_data *sdata, *found = NULL;
1716 	int count = 0;
1717 	int timeout;
1718 
1719 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1720 		local->ps_sdata = NULL;
1721 		return;
1722 	}
1723 
1724 	list_for_each_entry(sdata, &local->interfaces, list) {
1725 		if (!ieee80211_sdata_running(sdata))
1726 			continue;
1727 		if (sdata->vif.type == NL80211_IFTYPE_AP) {
1728 			/* If an AP vif is found, then disable PS
1729 			 * by setting the count to zero thereby setting
1730 			 * ps_sdata to NULL.
1731 			 */
1732 			count = 0;
1733 			break;
1734 		}
1735 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
1736 			continue;
1737 		found = sdata;
1738 		count++;
1739 	}
1740 
1741 	if (count == 1 && ieee80211_powersave_allowed(found)) {
1742 		u8 dtimper = found->u.mgd.dtim_period;
1743 
1744 		timeout = local->dynamic_ps_forced_timeout;
1745 		if (timeout < 0)
1746 			timeout = 100;
1747 		local->hw.conf.dynamic_ps_timeout = timeout;
1748 
1749 		/* If the TIM IE is invalid, pretend the value is 1 */
1750 		if (!dtimper)
1751 			dtimper = 1;
1752 
1753 		local->hw.conf.ps_dtim_period = dtimper;
1754 		local->ps_sdata = found;
1755 	} else {
1756 		local->ps_sdata = NULL;
1757 	}
1758 
1759 	ieee80211_change_ps(local);
1760 }
1761 
1762 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1763 {
1764 	bool ps_allowed = ieee80211_powersave_allowed(sdata);
1765 
1766 	if (sdata->vif.bss_conf.ps != ps_allowed) {
1767 		sdata->vif.bss_conf.ps = ps_allowed;
1768 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1769 	}
1770 }
1771 
1772 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1773 {
1774 	struct ieee80211_local *local =
1775 		container_of(work, struct ieee80211_local,
1776 			     dynamic_ps_disable_work);
1777 
1778 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1779 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1780 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1781 	}
1782 
1783 	ieee80211_wake_queues_by_reason(&local->hw,
1784 					IEEE80211_MAX_QUEUE_MAP,
1785 					IEEE80211_QUEUE_STOP_REASON_PS,
1786 					false);
1787 }
1788 
1789 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1790 {
1791 	struct ieee80211_local *local =
1792 		container_of(work, struct ieee80211_local,
1793 			     dynamic_ps_enable_work);
1794 	struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1795 	struct ieee80211_if_managed *ifmgd;
1796 	unsigned long flags;
1797 	int q;
1798 
1799 	/* can only happen when PS was just disabled anyway */
1800 	if (!sdata)
1801 		return;
1802 
1803 	ifmgd = &sdata->u.mgd;
1804 
1805 	if (local->hw.conf.flags & IEEE80211_CONF_PS)
1806 		return;
1807 
1808 	if (local->hw.conf.dynamic_ps_timeout > 0) {
1809 		/* don't enter PS if TX frames are pending */
1810 		if (drv_tx_frames_pending(local)) {
1811 			mod_timer(&local->dynamic_ps_timer, jiffies +
1812 				  msecs_to_jiffies(
1813 				  local->hw.conf.dynamic_ps_timeout));
1814 			return;
1815 		}
1816 
1817 		/*
1818 		 * transmission can be stopped by others which leads to
1819 		 * dynamic_ps_timer expiry. Postpone the ps timer if it
1820 		 * is not the actual idle state.
1821 		 */
1822 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1823 		for (q = 0; q < local->hw.queues; q++) {
1824 			if (local->queue_stop_reasons[q]) {
1825 				spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1826 						       flags);
1827 				mod_timer(&local->dynamic_ps_timer, jiffies +
1828 					  msecs_to_jiffies(
1829 					  local->hw.conf.dynamic_ps_timeout));
1830 				return;
1831 			}
1832 		}
1833 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1834 	}
1835 
1836 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1837 	    !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1838 		if (drv_tx_frames_pending(local)) {
1839 			mod_timer(&local->dynamic_ps_timer, jiffies +
1840 				  msecs_to_jiffies(
1841 				  local->hw.conf.dynamic_ps_timeout));
1842 		} else {
1843 			ieee80211_send_nullfunc(local, sdata, true);
1844 			/* Flush to get the tx status of nullfunc frame */
1845 			ieee80211_flush_queues(local, sdata, false);
1846 		}
1847 	}
1848 
1849 	if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1850 	      ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1851 	    (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1852 		ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1853 		local->hw.conf.flags |= IEEE80211_CONF_PS;
1854 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1855 	}
1856 }
1857 
1858 void ieee80211_dynamic_ps_timer(struct timer_list *t)
1859 {
1860 	struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1861 
1862 	ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1863 }
1864 
1865 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1866 {
1867 	struct delayed_work *delayed_work = to_delayed_work(work);
1868 	struct ieee80211_sub_if_data *sdata =
1869 		container_of(delayed_work, struct ieee80211_sub_if_data,
1870 			     dfs_cac_timer_work);
1871 	struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1872 
1873 	mutex_lock(&sdata->local->mtx);
1874 	if (sdata->wdev.cac_started) {
1875 		ieee80211_vif_release_channel(sdata);
1876 		cfg80211_cac_event(sdata->dev, &chandef,
1877 				   NL80211_RADAR_CAC_FINISHED,
1878 				   GFP_KERNEL);
1879 	}
1880 	mutex_unlock(&sdata->local->mtx);
1881 }
1882 
1883 static bool
1884 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1885 {
1886 	struct ieee80211_local *local = sdata->local;
1887 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1888 	bool ret = false;
1889 	int ac;
1890 
1891 	if (local->hw.queues < IEEE80211_NUM_ACS)
1892 		return false;
1893 
1894 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1895 		struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1896 		int non_acm_ac;
1897 		unsigned long now = jiffies;
1898 
1899 		if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1900 		    tx_tspec->admitted_time &&
1901 		    time_after(now, tx_tspec->time_slice_start + HZ)) {
1902 			tx_tspec->consumed_tx_time = 0;
1903 			tx_tspec->time_slice_start = now;
1904 
1905 			if (tx_tspec->downgraded)
1906 				tx_tspec->action =
1907 					TX_TSPEC_ACTION_STOP_DOWNGRADE;
1908 		}
1909 
1910 		switch (tx_tspec->action) {
1911 		case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1912 			/* take the original parameters */
1913 			if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1914 				sdata_err(sdata,
1915 					  "failed to set TX queue parameters for queue %d\n",
1916 					  ac);
1917 			tx_tspec->action = TX_TSPEC_ACTION_NONE;
1918 			tx_tspec->downgraded = false;
1919 			ret = true;
1920 			break;
1921 		case TX_TSPEC_ACTION_DOWNGRADE:
1922 			if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1923 				tx_tspec->action = TX_TSPEC_ACTION_NONE;
1924 				ret = true;
1925 				break;
1926 			}
1927 			/* downgrade next lower non-ACM AC */
1928 			for (non_acm_ac = ac + 1;
1929 			     non_acm_ac < IEEE80211_NUM_ACS;
1930 			     non_acm_ac++)
1931 				if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1932 					break;
1933 			/* Usually the loop will result in using BK even if it
1934 			 * requires admission control, but such a configuration
1935 			 * makes no sense and we have to transmit somehow - the
1936 			 * AC selection does the same thing.
1937 			 * If we started out trying to downgrade from BK, then
1938 			 * the extra condition here might be needed.
1939 			 */
1940 			if (non_acm_ac >= IEEE80211_NUM_ACS)
1941 				non_acm_ac = IEEE80211_AC_BK;
1942 			if (drv_conf_tx(local, sdata, ac,
1943 					&sdata->tx_conf[non_acm_ac]))
1944 				sdata_err(sdata,
1945 					  "failed to set TX queue parameters for queue %d\n",
1946 					  ac);
1947 			tx_tspec->action = TX_TSPEC_ACTION_NONE;
1948 			ret = true;
1949 			schedule_delayed_work(&ifmgd->tx_tspec_wk,
1950 				tx_tspec->time_slice_start + HZ - now + 1);
1951 			break;
1952 		case TX_TSPEC_ACTION_NONE:
1953 			/* nothing now */
1954 			break;
1955 		}
1956 	}
1957 
1958 	return ret;
1959 }
1960 
1961 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1962 {
1963 	if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1964 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1965 }
1966 
1967 static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1968 {
1969 	struct ieee80211_sub_if_data *sdata;
1970 
1971 	sdata = container_of(work, struct ieee80211_sub_if_data,
1972 			     u.mgd.tx_tspec_wk.work);
1973 	ieee80211_sta_handle_tspec_ac_params(sdata);
1974 }
1975 
1976 /* MLME */
1977 static bool
1978 ieee80211_sta_wmm_params(struct ieee80211_local *local,
1979 			 struct ieee80211_sub_if_data *sdata,
1980 			 const u8 *wmm_param, size_t wmm_param_len,
1981 			 const struct ieee80211_mu_edca_param_set *mu_edca)
1982 {
1983 	struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
1984 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1985 	size_t left;
1986 	int count, mu_edca_count, ac;
1987 	const u8 *pos;
1988 	u8 uapsd_queues = 0;
1989 
1990 	if (!local->ops->conf_tx)
1991 		return false;
1992 
1993 	if (local->hw.queues < IEEE80211_NUM_ACS)
1994 		return false;
1995 
1996 	if (!wmm_param)
1997 		return false;
1998 
1999 	if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
2000 		return false;
2001 
2002 	if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
2003 		uapsd_queues = ifmgd->uapsd_queues;
2004 
2005 	count = wmm_param[6] & 0x0f;
2006 	/* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2007 	 * if mu_edca was preset before and now it disappeared tell
2008 	 * the driver about it.
2009 	 */
2010 	mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2011 	if (count == ifmgd->wmm_last_param_set &&
2012 	    mu_edca_count == ifmgd->mu_edca_last_param_set)
2013 		return false;
2014 	ifmgd->wmm_last_param_set = count;
2015 	ifmgd->mu_edca_last_param_set = mu_edca_count;
2016 
2017 	pos = wmm_param + 8;
2018 	left = wmm_param_len - 8;
2019 
2020 	memset(&params, 0, sizeof(params));
2021 
2022 	sdata->wmm_acm = 0;
2023 	for (; left >= 4; left -= 4, pos += 4) {
2024 		int aci = (pos[0] >> 5) & 0x03;
2025 		int acm = (pos[0] >> 4) & 0x01;
2026 		bool uapsd = false;
2027 
2028 		switch (aci) {
2029 		case 1: /* AC_BK */
2030 			ac = IEEE80211_AC_BK;
2031 			if (acm)
2032 				sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
2033 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2034 				uapsd = true;
2035 			params[ac].mu_edca = !!mu_edca;
2036 			if (mu_edca)
2037 				params[ac].mu_edca_param_rec = mu_edca->ac_bk;
2038 			break;
2039 		case 2: /* AC_VI */
2040 			ac = IEEE80211_AC_VI;
2041 			if (acm)
2042 				sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
2043 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2044 				uapsd = true;
2045 			params[ac].mu_edca = !!mu_edca;
2046 			if (mu_edca)
2047 				params[ac].mu_edca_param_rec = mu_edca->ac_vi;
2048 			break;
2049 		case 3: /* AC_VO */
2050 			ac = IEEE80211_AC_VO;
2051 			if (acm)
2052 				sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
2053 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2054 				uapsd = true;
2055 			params[ac].mu_edca = !!mu_edca;
2056 			if (mu_edca)
2057 				params[ac].mu_edca_param_rec = mu_edca->ac_vo;
2058 			break;
2059 		case 0: /* AC_BE */
2060 		default:
2061 			ac = IEEE80211_AC_BE;
2062 			if (acm)
2063 				sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
2064 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2065 				uapsd = true;
2066 			params[ac].mu_edca = !!mu_edca;
2067 			if (mu_edca)
2068 				params[ac].mu_edca_param_rec = mu_edca->ac_be;
2069 			break;
2070 		}
2071 
2072 		params[ac].aifs = pos[0] & 0x0f;
2073 
2074 		if (params[ac].aifs < 2) {
2075 			sdata_info(sdata,
2076 				   "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2077 				   params[ac].aifs, aci);
2078 			params[ac].aifs = 2;
2079 		}
2080 		params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2081 		params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2082 		params[ac].txop = get_unaligned_le16(pos + 2);
2083 		params[ac].acm = acm;
2084 		params[ac].uapsd = uapsd;
2085 
2086 		if (params[ac].cw_min == 0 ||
2087 		    params[ac].cw_min > params[ac].cw_max) {
2088 			sdata_info(sdata,
2089 				   "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2090 				   params[ac].cw_min, params[ac].cw_max, aci);
2091 			return false;
2092 		}
2093 		ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
2094 	}
2095 
2096 	/* WMM specification requires all 4 ACIs. */
2097 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2098 		if (params[ac].cw_min == 0) {
2099 			sdata_info(sdata,
2100 				   "AP has invalid WMM params (missing AC %d), using defaults\n",
2101 				   ac);
2102 			return false;
2103 		}
2104 	}
2105 
2106 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2107 		mlme_dbg(sdata,
2108 			 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2109 			 ac, params[ac].acm,
2110 			 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2111 			 params[ac].txop, params[ac].uapsd,
2112 			 ifmgd->tx_tspec[ac].downgraded);
2113 		sdata->tx_conf[ac] = params[ac];
2114 		if (!ifmgd->tx_tspec[ac].downgraded &&
2115 		    drv_conf_tx(local, sdata, ac, &params[ac]))
2116 			sdata_err(sdata,
2117 				  "failed to set TX queue parameters for AC %d\n",
2118 				  ac);
2119 	}
2120 
2121 	/* enable WMM or activate new settings */
2122 	sdata->vif.bss_conf.qos = true;
2123 	return true;
2124 }
2125 
2126 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2127 {
2128 	lockdep_assert_held(&sdata->local->mtx);
2129 
2130 	sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
2131 	ieee80211_run_deferred_scan(sdata->local);
2132 }
2133 
2134 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2135 {
2136 	mutex_lock(&sdata->local->mtx);
2137 	__ieee80211_stop_poll(sdata);
2138 	mutex_unlock(&sdata->local->mtx);
2139 }
2140 
2141 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2142 					   u16 capab, bool erp_valid, u8 erp)
2143 {
2144 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2145 	struct ieee80211_supported_band *sband;
2146 	u32 changed = 0;
2147 	bool use_protection;
2148 	bool use_short_preamble;
2149 	bool use_short_slot;
2150 
2151 	sband = ieee80211_get_sband(sdata);
2152 	if (!sband)
2153 		return changed;
2154 
2155 	if (erp_valid) {
2156 		use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2157 		use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2158 	} else {
2159 		use_protection = false;
2160 		use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2161 	}
2162 
2163 	use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
2164 	if (sband->band == NL80211_BAND_5GHZ ||
2165 	    sband->band == NL80211_BAND_6GHZ)
2166 		use_short_slot = true;
2167 
2168 	if (use_protection != bss_conf->use_cts_prot) {
2169 		bss_conf->use_cts_prot = use_protection;
2170 		changed |= BSS_CHANGED_ERP_CTS_PROT;
2171 	}
2172 
2173 	if (use_short_preamble != bss_conf->use_short_preamble) {
2174 		bss_conf->use_short_preamble = use_short_preamble;
2175 		changed |= BSS_CHANGED_ERP_PREAMBLE;
2176 	}
2177 
2178 	if (use_short_slot != bss_conf->use_short_slot) {
2179 		bss_conf->use_short_slot = use_short_slot;
2180 		changed |= BSS_CHANGED_ERP_SLOT;
2181 	}
2182 
2183 	return changed;
2184 }
2185 
2186 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
2187 				     struct cfg80211_bss *cbss,
2188 				     u32 bss_info_changed)
2189 {
2190 	struct ieee80211_bss *bss = (void *)cbss->priv;
2191 	struct ieee80211_local *local = sdata->local;
2192 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2193 
2194 	bss_info_changed |= BSS_CHANGED_ASSOC;
2195 	bss_info_changed |= ieee80211_handle_bss_capability(sdata,
2196 		bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2197 
2198 	sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2199 		beacon_loss_count * bss_conf->beacon_int));
2200 
2201 	sdata->u.mgd.associated = cbss;
2202 	memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2203 
2204 	ieee80211_check_rate_mask(sdata);
2205 
2206 	sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2207 
2208 	if (sdata->vif.p2p ||
2209 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2210 		const struct cfg80211_bss_ies *ies;
2211 
2212 		rcu_read_lock();
2213 		ies = rcu_dereference(cbss->ies);
2214 		if (ies) {
2215 			int ret;
2216 
2217 			ret = cfg80211_get_p2p_attr(
2218 					ies->data, ies->len,
2219 					IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2220 					(u8 *) &bss_conf->p2p_noa_attr,
2221 					sizeof(bss_conf->p2p_noa_attr));
2222 			if (ret >= 2) {
2223 				sdata->u.mgd.p2p_noa_index =
2224 					bss_conf->p2p_noa_attr.index;
2225 				bss_info_changed |= BSS_CHANGED_P2P_PS;
2226 			}
2227 		}
2228 		rcu_read_unlock();
2229 	}
2230 
2231 	/* just to be sure */
2232 	ieee80211_stop_poll(sdata);
2233 
2234 	ieee80211_led_assoc(local, 1);
2235 
2236 	if (sdata->u.mgd.have_beacon) {
2237 		/*
2238 		 * If the AP is buggy we may get here with no DTIM period
2239 		 * known, so assume it's 1 which is the only safe assumption
2240 		 * in that case, although if the TIM IE is broken powersave
2241 		 * probably just won't work at all.
2242 		 */
2243 		bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2244 		bss_conf->beacon_rate = bss->beacon_rate;
2245 		bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2246 	} else {
2247 		bss_conf->beacon_rate = NULL;
2248 		bss_conf->dtim_period = 0;
2249 	}
2250 
2251 	bss_conf->assoc = 1;
2252 
2253 	/* Tell the driver to monitor connection quality (if supported) */
2254 	if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2255 	    bss_conf->cqm_rssi_thold)
2256 		bss_info_changed |= BSS_CHANGED_CQM;
2257 
2258 	/* Enable ARP filtering */
2259 	if (bss_conf->arp_addr_cnt)
2260 		bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2261 
2262 	ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2263 
2264 	mutex_lock(&local->iflist_mtx);
2265 	ieee80211_recalc_ps(local);
2266 	mutex_unlock(&local->iflist_mtx);
2267 
2268 	ieee80211_recalc_smps(sdata);
2269 	ieee80211_recalc_ps_vif(sdata);
2270 
2271 	netif_carrier_on(sdata->dev);
2272 }
2273 
2274 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2275 				   u16 stype, u16 reason, bool tx,
2276 				   u8 *frame_buf)
2277 {
2278 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2279 	struct ieee80211_local *local = sdata->local;
2280 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2281 	u32 changed = 0;
2282 	struct ieee80211_prep_tx_info info = {
2283 		.subtype = stype,
2284 	};
2285 
2286 	sdata_assert_lock(sdata);
2287 
2288 	if (WARN_ON_ONCE(tx && !frame_buf))
2289 		return;
2290 
2291 	if (WARN_ON(!ifmgd->associated))
2292 		return;
2293 
2294 	ieee80211_stop_poll(sdata);
2295 
2296 	ifmgd->associated = NULL;
2297 	netif_carrier_off(sdata->dev);
2298 
2299 	/*
2300 	 * if we want to get out of ps before disassoc (why?) we have
2301 	 * to do it before sending disassoc, as otherwise the null-packet
2302 	 * won't be valid.
2303 	 */
2304 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2305 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2306 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2307 	}
2308 	local->ps_sdata = NULL;
2309 
2310 	/* disable per-vif ps */
2311 	ieee80211_recalc_ps_vif(sdata);
2312 
2313 	/* make sure ongoing transmission finishes */
2314 	synchronize_net();
2315 
2316 	/*
2317 	 * drop any frame before deauth/disassoc, this can be data or
2318 	 * management frame. Since we are disconnecting, we should not
2319 	 * insist sending these frames which can take time and delay
2320 	 * the disconnection and possible the roaming.
2321 	 */
2322 	if (tx)
2323 		ieee80211_flush_queues(local, sdata, true);
2324 
2325 	/* deauthenticate/disassociate now */
2326 	if (tx || frame_buf) {
2327 		/*
2328 		 * In multi channel scenarios guarantee that the virtual
2329 		 * interface is granted immediate airtime to transmit the
2330 		 * deauthentication frame by calling mgd_prepare_tx, if the
2331 		 * driver requested so.
2332 		 */
2333 		if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2334 		    !ifmgd->have_beacon) {
2335 			drv_mgd_prepare_tx(sdata->local, sdata, &info);
2336 		}
2337 
2338 		ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2339 					       ifmgd->bssid, stype, reason,
2340 					       tx, frame_buf);
2341 	}
2342 
2343 	/* flush out frame - make sure the deauth was actually sent */
2344 	if (tx)
2345 		ieee80211_flush_queues(local, sdata, false);
2346 
2347 	drv_mgd_complete_tx(sdata->local, sdata, &info);
2348 
2349 	/* clear bssid only after building the needed mgmt frames */
2350 	eth_zero_addr(ifmgd->bssid);
2351 
2352 	sdata->vif.bss_conf.ssid_len = 0;
2353 
2354 	/* remove AP and TDLS peers */
2355 	sta_info_flush(sdata);
2356 
2357 	/* finally reset all BSS / config parameters */
2358 	changed |= ieee80211_reset_erp_info(sdata);
2359 
2360 	ieee80211_led_assoc(local, 0);
2361 	changed |= BSS_CHANGED_ASSOC;
2362 	sdata->vif.bss_conf.assoc = false;
2363 
2364 	ifmgd->p2p_noa_index = -1;
2365 	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2366 	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2367 
2368 	/* on the next assoc, re-program HT/VHT parameters */
2369 	memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2370 	memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2371 	memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2372 	memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2373 
2374 	/* reset MU-MIMO ownership and group data */
2375 	memset(sdata->vif.bss_conf.mu_group.membership, 0,
2376 	       sizeof(sdata->vif.bss_conf.mu_group.membership));
2377 	memset(sdata->vif.bss_conf.mu_group.position, 0,
2378 	       sizeof(sdata->vif.bss_conf.mu_group.position));
2379 	changed |= BSS_CHANGED_MU_GROUPS;
2380 	sdata->vif.mu_mimo_owner = false;
2381 
2382 	sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2383 
2384 	del_timer_sync(&local->dynamic_ps_timer);
2385 	cancel_work_sync(&local->dynamic_ps_enable_work);
2386 
2387 	/* Disable ARP filtering */
2388 	if (sdata->vif.bss_conf.arp_addr_cnt)
2389 		changed |= BSS_CHANGED_ARP_FILTER;
2390 
2391 	sdata->vif.bss_conf.qos = false;
2392 	changed |= BSS_CHANGED_QOS;
2393 
2394 	/* The BSSID (not really interesting) and HT changed */
2395 	changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2396 	ieee80211_bss_info_change_notify(sdata, changed);
2397 
2398 	/* disassociated - set to defaults now */
2399 	ieee80211_set_wmm_default(sdata, false, false);
2400 
2401 	del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2402 	del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2403 	del_timer_sync(&sdata->u.mgd.timer);
2404 	del_timer_sync(&sdata->u.mgd.chswitch_timer);
2405 
2406 	sdata->vif.bss_conf.dtim_period = 0;
2407 	sdata->vif.bss_conf.beacon_rate = NULL;
2408 
2409 	ifmgd->have_beacon = false;
2410 
2411 	ifmgd->flags = 0;
2412 	mutex_lock(&local->mtx);
2413 	ieee80211_vif_release_channel(sdata);
2414 
2415 	sdata->vif.csa_active = false;
2416 	ifmgd->csa_waiting_bcn = false;
2417 	ifmgd->csa_ignored_same_chan = false;
2418 	if (sdata->csa_block_tx) {
2419 		ieee80211_wake_vif_queues(local, sdata,
2420 					  IEEE80211_QUEUE_STOP_REASON_CSA);
2421 		sdata->csa_block_tx = false;
2422 	}
2423 	mutex_unlock(&local->mtx);
2424 
2425 	/* existing TX TSPEC sessions no longer exist */
2426 	memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2427 	cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2428 
2429 	sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2430 
2431 	bss_conf->pwr_reduction = 0;
2432 	bss_conf->tx_pwr_env_num = 0;
2433 	memset(bss_conf->tx_pwr_env, 0, sizeof(bss_conf->tx_pwr_env));
2434 }
2435 
2436 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2437 {
2438 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2439 	struct ieee80211_local *local = sdata->local;
2440 
2441 	mutex_lock(&local->mtx);
2442 	if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2443 		goto out;
2444 
2445 	__ieee80211_stop_poll(sdata);
2446 
2447 	mutex_lock(&local->iflist_mtx);
2448 	ieee80211_recalc_ps(local);
2449 	mutex_unlock(&local->iflist_mtx);
2450 
2451 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2452 		goto out;
2453 
2454 	/*
2455 	 * We've received a probe response, but are not sure whether
2456 	 * we have or will be receiving any beacons or data, so let's
2457 	 * schedule the timers again, just in case.
2458 	 */
2459 	ieee80211_sta_reset_beacon_monitor(sdata);
2460 
2461 	mod_timer(&ifmgd->conn_mon_timer,
2462 		  round_jiffies_up(jiffies +
2463 				   IEEE80211_CONNECTION_IDLE_TIME));
2464 out:
2465 	mutex_unlock(&local->mtx);
2466 }
2467 
2468 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2469 					   struct ieee80211_hdr *hdr,
2470 					   u16 tx_time)
2471 {
2472 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2473 	u16 tid;
2474 	int ac;
2475 	struct ieee80211_sta_tx_tspec *tx_tspec;
2476 	unsigned long now = jiffies;
2477 
2478 	if (!ieee80211_is_data_qos(hdr->frame_control))
2479 		return;
2480 
2481 	tid = ieee80211_get_tid(hdr);
2482 	ac = ieee80211_ac_from_tid(tid);
2483 	tx_tspec = &ifmgd->tx_tspec[ac];
2484 
2485 	if (likely(!tx_tspec->admitted_time))
2486 		return;
2487 
2488 	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2489 		tx_tspec->consumed_tx_time = 0;
2490 		tx_tspec->time_slice_start = now;
2491 
2492 		if (tx_tspec->downgraded) {
2493 			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2494 			schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2495 		}
2496 	}
2497 
2498 	if (tx_tspec->downgraded)
2499 		return;
2500 
2501 	tx_tspec->consumed_tx_time += tx_time;
2502 
2503 	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2504 		tx_tspec->downgraded = true;
2505 		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2506 		schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2507 	}
2508 }
2509 
2510 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2511 			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2512 {
2513 	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2514 
2515 	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
2516 	    !sdata->u.mgd.probe_send_count)
2517 		return;
2518 
2519 	if (ack)
2520 		sdata->u.mgd.probe_send_count = 0;
2521 	else
2522 		sdata->u.mgd.nullfunc_failed = true;
2523 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2524 }
2525 
2526 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2527 					  const u8 *src, const u8 *dst,
2528 					  const u8 *ssid, size_t ssid_len,
2529 					  struct ieee80211_channel *channel)
2530 {
2531 	struct sk_buff *skb;
2532 
2533 	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2534 					ssid, ssid_len, NULL, 0,
2535 					IEEE80211_PROBE_FLAG_DIRECTED);
2536 	if (skb)
2537 		ieee80211_tx_skb(sdata, skb);
2538 }
2539 
2540 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2541 {
2542 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2543 	const struct element *ssid;
2544 	u8 *dst = ifmgd->associated->bssid;
2545 	u8 unicast_limit = max(1, max_probe_tries - 3);
2546 	struct sta_info *sta;
2547 
2548 	/*
2549 	 * Try sending broadcast probe requests for the last three
2550 	 * probe requests after the first ones failed since some
2551 	 * buggy APs only support broadcast probe requests.
2552 	 */
2553 	if (ifmgd->probe_send_count >= unicast_limit)
2554 		dst = NULL;
2555 
2556 	/*
2557 	 * When the hardware reports an accurate Tx ACK status, it's
2558 	 * better to send a nullfunc frame instead of a probe request,
2559 	 * as it will kick us off the AP quickly if we aren't associated
2560 	 * anymore. The timeout will be reset if the frame is ACKed by
2561 	 * the AP.
2562 	 */
2563 	ifmgd->probe_send_count++;
2564 
2565 	if (dst) {
2566 		mutex_lock(&sdata->local->sta_mtx);
2567 		sta = sta_info_get(sdata, dst);
2568 		if (!WARN_ON(!sta))
2569 			ieee80211_check_fast_rx(sta);
2570 		mutex_unlock(&sdata->local->sta_mtx);
2571 	}
2572 
2573 	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2574 		ifmgd->nullfunc_failed = false;
2575 		ieee80211_send_nullfunc(sdata->local, sdata, false);
2576 	} else {
2577 		int ssid_len;
2578 
2579 		rcu_read_lock();
2580 		ssid = ieee80211_bss_get_elem(ifmgd->associated, WLAN_EID_SSID);
2581 		if (WARN_ON_ONCE(ssid == NULL))
2582 			ssid_len = 0;
2583 		else
2584 			ssid_len = ssid->datalen;
2585 
2586 		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2587 					      ssid->data, ssid_len,
2588 					      ifmgd->associated->channel);
2589 		rcu_read_unlock();
2590 	}
2591 
2592 	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2593 	run_again(sdata, ifmgd->probe_timeout);
2594 }
2595 
2596 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2597 				   bool beacon)
2598 {
2599 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2600 	bool already = false;
2601 
2602 	if (!ieee80211_sdata_running(sdata))
2603 		return;
2604 
2605 	sdata_lock(sdata);
2606 
2607 	if (!ifmgd->associated)
2608 		goto out;
2609 
2610 	mutex_lock(&sdata->local->mtx);
2611 
2612 	if (sdata->local->tmp_channel || sdata->local->scanning) {
2613 		mutex_unlock(&sdata->local->mtx);
2614 		goto out;
2615 	}
2616 
2617 	if (sdata->local->suspending) {
2618 		/* reschedule after resume */
2619 		mutex_unlock(&sdata->local->mtx);
2620 		ieee80211_reset_ap_probe(sdata);
2621 		goto out;
2622 	}
2623 
2624 	if (beacon) {
2625 		mlme_dbg_ratelimited(sdata,
2626 				     "detected beacon loss from AP (missed %d beacons) - probing\n",
2627 				     beacon_loss_count);
2628 
2629 		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2630 	}
2631 
2632 	/*
2633 	 * The driver/our work has already reported this event or the
2634 	 * connection monitoring has kicked in and we have already sent
2635 	 * a probe request. Or maybe the AP died and the driver keeps
2636 	 * reporting until we disassociate...
2637 	 *
2638 	 * In either case we have to ignore the current call to this
2639 	 * function (except for setting the correct probe reason bit)
2640 	 * because otherwise we would reset the timer every time and
2641 	 * never check whether we received a probe response!
2642 	 */
2643 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2644 		already = true;
2645 
2646 	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2647 
2648 	mutex_unlock(&sdata->local->mtx);
2649 
2650 	if (already)
2651 		goto out;
2652 
2653 	mutex_lock(&sdata->local->iflist_mtx);
2654 	ieee80211_recalc_ps(sdata->local);
2655 	mutex_unlock(&sdata->local->iflist_mtx);
2656 
2657 	ifmgd->probe_send_count = 0;
2658 	ieee80211_mgd_probe_ap_send(sdata);
2659  out:
2660 	sdata_unlock(sdata);
2661 }
2662 
2663 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2664 					  struct ieee80211_vif *vif)
2665 {
2666 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2667 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2668 	struct cfg80211_bss *cbss;
2669 	struct sk_buff *skb;
2670 	const struct element *ssid;
2671 	int ssid_len;
2672 
2673 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2674 		return NULL;
2675 
2676 	sdata_assert_lock(sdata);
2677 
2678 	if (ifmgd->associated)
2679 		cbss = ifmgd->associated;
2680 	else if (ifmgd->auth_data)
2681 		cbss = ifmgd->auth_data->bss;
2682 	else if (ifmgd->assoc_data)
2683 		cbss = ifmgd->assoc_data->bss;
2684 	else
2685 		return NULL;
2686 
2687 	rcu_read_lock();
2688 	ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
2689 	if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
2690 		      "invalid SSID element (len=%d)",
2691 		      ssid ? ssid->datalen : -1))
2692 		ssid_len = 0;
2693 	else
2694 		ssid_len = ssid->datalen;
2695 
2696 	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2697 					(u32) -1, cbss->channel,
2698 					ssid->data, ssid_len,
2699 					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2700 	rcu_read_unlock();
2701 
2702 	return skb;
2703 }
2704 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2705 
2706 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2707 					const u8 *buf, size_t len, bool tx,
2708 					u16 reason, bool reconnect)
2709 {
2710 	struct ieee80211_event event = {
2711 		.type = MLME_EVENT,
2712 		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2713 		.u.mlme.reason = reason,
2714 	};
2715 
2716 	if (tx)
2717 		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
2718 	else
2719 		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2720 
2721 	drv_event_callback(sdata->local, sdata, &event);
2722 }
2723 
2724 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2725 {
2726 	struct ieee80211_local *local = sdata->local;
2727 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2728 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2729 	bool tx;
2730 
2731 	sdata_lock(sdata);
2732 	if (!ifmgd->associated) {
2733 		sdata_unlock(sdata);
2734 		return;
2735 	}
2736 
2737 	tx = !sdata->csa_block_tx;
2738 
2739 	if (!ifmgd->driver_disconnect) {
2740 		/*
2741 		 * AP is probably out of range (or not reachable for another
2742 		 * reason) so remove the bss struct for that AP.
2743 		 */
2744 		cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2745 	}
2746 
2747 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2748 			       ifmgd->driver_disconnect ?
2749 					WLAN_REASON_DEAUTH_LEAVING :
2750 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2751 			       tx, frame_buf);
2752 	mutex_lock(&local->mtx);
2753 	sdata->vif.csa_active = false;
2754 	ifmgd->csa_waiting_bcn = false;
2755 	if (sdata->csa_block_tx) {
2756 		ieee80211_wake_vif_queues(local, sdata,
2757 					  IEEE80211_QUEUE_STOP_REASON_CSA);
2758 		sdata->csa_block_tx = false;
2759 	}
2760 	mutex_unlock(&local->mtx);
2761 
2762 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
2763 				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2764 				    ifmgd->reconnect);
2765 	ifmgd->reconnect = false;
2766 
2767 	sdata_unlock(sdata);
2768 }
2769 
2770 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2771 {
2772 	struct ieee80211_sub_if_data *sdata =
2773 		container_of(work, struct ieee80211_sub_if_data,
2774 			     u.mgd.beacon_connection_loss_work);
2775 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2776 
2777 	if (ifmgd->associated)
2778 		ifmgd->beacon_loss_count++;
2779 
2780 	if (ifmgd->connection_loss) {
2781 		sdata_info(sdata, "Connection to AP %pM lost\n",
2782 			   ifmgd->bssid);
2783 		__ieee80211_disconnect(sdata);
2784 		ifmgd->connection_loss = false;
2785 	} else if (ifmgd->driver_disconnect) {
2786 		sdata_info(sdata,
2787 			   "Driver requested disconnection from AP %pM\n",
2788 			   ifmgd->bssid);
2789 		__ieee80211_disconnect(sdata);
2790 		ifmgd->driver_disconnect = false;
2791 	} else {
2792 		ieee80211_mgd_probe_ap(sdata, true);
2793 	}
2794 }
2795 
2796 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2797 {
2798 	struct ieee80211_sub_if_data *sdata =
2799 		container_of(work, struct ieee80211_sub_if_data,
2800 			     u.mgd.csa_connection_drop_work);
2801 
2802 	__ieee80211_disconnect(sdata);
2803 }
2804 
2805 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2806 {
2807 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2808 	struct ieee80211_hw *hw = &sdata->local->hw;
2809 
2810 	trace_api_beacon_loss(sdata);
2811 
2812 	sdata->u.mgd.connection_loss = false;
2813 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2814 }
2815 EXPORT_SYMBOL(ieee80211_beacon_loss);
2816 
2817 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2818 {
2819 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2820 	struct ieee80211_hw *hw = &sdata->local->hw;
2821 
2822 	trace_api_connection_loss(sdata);
2823 
2824 	sdata->u.mgd.connection_loss = true;
2825 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2826 }
2827 EXPORT_SYMBOL(ieee80211_connection_loss);
2828 
2829 void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
2830 {
2831 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2832 	struct ieee80211_hw *hw = &sdata->local->hw;
2833 
2834 	trace_api_disconnect(sdata, reconnect);
2835 
2836 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2837 		return;
2838 
2839 	sdata->u.mgd.driver_disconnect = true;
2840 	sdata->u.mgd.reconnect = reconnect;
2841 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2842 }
2843 EXPORT_SYMBOL(ieee80211_disconnect);
2844 
2845 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2846 					bool assoc)
2847 {
2848 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2849 
2850 	sdata_assert_lock(sdata);
2851 
2852 	if (!assoc) {
2853 		/*
2854 		 * we are not authenticated yet, the only timer that could be
2855 		 * running is the timeout for the authentication response which
2856 		 * which is not relevant anymore.
2857 		 */
2858 		del_timer_sync(&sdata->u.mgd.timer);
2859 		sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2860 
2861 		eth_zero_addr(sdata->u.mgd.bssid);
2862 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2863 		sdata->u.mgd.flags = 0;
2864 		mutex_lock(&sdata->local->mtx);
2865 		ieee80211_vif_release_channel(sdata);
2866 		mutex_unlock(&sdata->local->mtx);
2867 	}
2868 
2869 	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2870 	kfree(auth_data);
2871 	sdata->u.mgd.auth_data = NULL;
2872 }
2873 
2874 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2875 					 bool assoc, bool abandon)
2876 {
2877 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2878 
2879 	sdata_assert_lock(sdata);
2880 
2881 	if (!assoc) {
2882 		/*
2883 		 * we are not associated yet, the only timer that could be
2884 		 * running is the timeout for the association response which
2885 		 * which is not relevant anymore.
2886 		 */
2887 		del_timer_sync(&sdata->u.mgd.timer);
2888 		sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2889 
2890 		eth_zero_addr(sdata->u.mgd.bssid);
2891 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2892 		sdata->u.mgd.flags = 0;
2893 		sdata->vif.mu_mimo_owner = false;
2894 
2895 		mutex_lock(&sdata->local->mtx);
2896 		ieee80211_vif_release_channel(sdata);
2897 		mutex_unlock(&sdata->local->mtx);
2898 
2899 		if (abandon)
2900 			cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2901 	}
2902 
2903 	kfree(assoc_data);
2904 	sdata->u.mgd.assoc_data = NULL;
2905 }
2906 
2907 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2908 				     struct ieee80211_mgmt *mgmt, size_t len)
2909 {
2910 	struct ieee80211_local *local = sdata->local;
2911 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2912 	const struct element *challenge;
2913 	u8 *pos;
2914 	u32 tx_flags = 0;
2915 	struct ieee80211_prep_tx_info info = {
2916 		.subtype = IEEE80211_STYPE_AUTH,
2917 	};
2918 
2919 	pos = mgmt->u.auth.variable;
2920 	challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
2921 				       len - (pos - (u8 *)mgmt));
2922 	if (!challenge)
2923 		return;
2924 	auth_data->expected_transaction = 4;
2925 	drv_mgd_prepare_tx(sdata->local, sdata, &info);
2926 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2927 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2928 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
2929 	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2930 			    (void *)challenge,
2931 			    challenge->datalen + sizeof(*challenge),
2932 			    auth_data->bss->bssid, auth_data->bss->bssid,
2933 			    auth_data->key, auth_data->key_len,
2934 			    auth_data->key_idx, tx_flags);
2935 }
2936 
2937 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2938 				    const u8 *bssid)
2939 {
2940 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2941 	struct sta_info *sta;
2942 	bool result = true;
2943 
2944 	sdata_info(sdata, "authenticated\n");
2945 	ifmgd->auth_data->done = true;
2946 	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2947 	ifmgd->auth_data->timeout_started = true;
2948 	run_again(sdata, ifmgd->auth_data->timeout);
2949 
2950 	/* move station state to auth */
2951 	mutex_lock(&sdata->local->sta_mtx);
2952 	sta = sta_info_get(sdata, bssid);
2953 	if (!sta) {
2954 		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2955 		result = false;
2956 		goto out;
2957 	}
2958 	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2959 		sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2960 		result = false;
2961 		goto out;
2962 	}
2963 
2964 out:
2965 	mutex_unlock(&sdata->local->sta_mtx);
2966 	return result;
2967 }
2968 
2969 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2970 				   struct ieee80211_mgmt *mgmt, size_t len)
2971 {
2972 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2973 	u8 bssid[ETH_ALEN];
2974 	u16 auth_alg, auth_transaction, status_code;
2975 	struct ieee80211_event event = {
2976 		.type = MLME_EVENT,
2977 		.u.mlme.data = AUTH_EVENT,
2978 	};
2979 	struct ieee80211_prep_tx_info info = {
2980 		.subtype = IEEE80211_STYPE_AUTH,
2981 	};
2982 
2983 	sdata_assert_lock(sdata);
2984 
2985 	if (len < 24 + 6)
2986 		return;
2987 
2988 	if (!ifmgd->auth_data || ifmgd->auth_data->done)
2989 		return;
2990 
2991 	memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2992 
2993 	if (!ether_addr_equal(bssid, mgmt->bssid))
2994 		return;
2995 
2996 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2997 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2998 	status_code = le16_to_cpu(mgmt->u.auth.status_code);
2999 
3000 	if (auth_alg != ifmgd->auth_data->algorithm ||
3001 	    (auth_alg != WLAN_AUTH_SAE &&
3002 	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
3003 	    (auth_alg == WLAN_AUTH_SAE &&
3004 	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
3005 	      auth_transaction > 2))) {
3006 		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
3007 			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
3008 			   auth_transaction,
3009 			   ifmgd->auth_data->expected_transaction);
3010 		goto notify_driver;
3011 	}
3012 
3013 	if (status_code != WLAN_STATUS_SUCCESS) {
3014 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3015 
3016 		if (auth_alg == WLAN_AUTH_SAE &&
3017 		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
3018 		     (auth_transaction == 1 &&
3019 		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
3020 		       status_code == WLAN_STATUS_SAE_PK)))) {
3021 			/* waiting for userspace now */
3022 			ifmgd->auth_data->waiting = true;
3023 			ifmgd->auth_data->timeout =
3024 				jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
3025 			ifmgd->auth_data->timeout_started = true;
3026 			run_again(sdata, ifmgd->auth_data->timeout);
3027 			goto notify_driver;
3028 		}
3029 
3030 		sdata_info(sdata, "%pM denied authentication (status %d)\n",
3031 			   mgmt->sa, status_code);
3032 		ieee80211_destroy_auth_data(sdata, false);
3033 		event.u.mlme.status = MLME_DENIED;
3034 		event.u.mlme.reason = status_code;
3035 		drv_event_callback(sdata->local, sdata, &event);
3036 		goto notify_driver;
3037 	}
3038 
3039 	switch (ifmgd->auth_data->algorithm) {
3040 	case WLAN_AUTH_OPEN:
3041 	case WLAN_AUTH_LEAP:
3042 	case WLAN_AUTH_FT:
3043 	case WLAN_AUTH_SAE:
3044 	case WLAN_AUTH_FILS_SK:
3045 	case WLAN_AUTH_FILS_SK_PFS:
3046 	case WLAN_AUTH_FILS_PK:
3047 		break;
3048 	case WLAN_AUTH_SHARED_KEY:
3049 		if (ifmgd->auth_data->expected_transaction != 4) {
3050 			ieee80211_auth_challenge(sdata, mgmt, len);
3051 			/* need another frame */
3052 			return;
3053 		}
3054 		break;
3055 	default:
3056 		WARN_ONCE(1, "invalid auth alg %d",
3057 			  ifmgd->auth_data->algorithm);
3058 		goto notify_driver;
3059 	}
3060 
3061 	event.u.mlme.status = MLME_SUCCESS;
3062 	info.success = 1;
3063 	drv_event_callback(sdata->local, sdata, &event);
3064 	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3065 	    (auth_transaction == 2 &&
3066 	     ifmgd->auth_data->expected_transaction == 2)) {
3067 		if (!ieee80211_mark_sta_auth(sdata, bssid))
3068 			return; /* ignore frame -- wait for timeout */
3069 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3070 		   auth_transaction == 2) {
3071 		sdata_info(sdata, "SAE peer confirmed\n");
3072 		ifmgd->auth_data->peer_confirmed = true;
3073 	}
3074 
3075 	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3076 notify_driver:
3077 	drv_mgd_complete_tx(sdata->local, sdata, &info);
3078 }
3079 
3080 #define case_WLAN(type) \
3081 	case WLAN_REASON_##type: return #type
3082 
3083 const char *ieee80211_get_reason_code_string(u16 reason_code)
3084 {
3085 	switch (reason_code) {
3086 	case_WLAN(UNSPECIFIED);
3087 	case_WLAN(PREV_AUTH_NOT_VALID);
3088 	case_WLAN(DEAUTH_LEAVING);
3089 	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3090 	case_WLAN(DISASSOC_AP_BUSY);
3091 	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3092 	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3093 	case_WLAN(DISASSOC_STA_HAS_LEFT);
3094 	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3095 	case_WLAN(DISASSOC_BAD_POWER);
3096 	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3097 	case_WLAN(INVALID_IE);
3098 	case_WLAN(MIC_FAILURE);
3099 	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3100 	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3101 	case_WLAN(IE_DIFFERENT);
3102 	case_WLAN(INVALID_GROUP_CIPHER);
3103 	case_WLAN(INVALID_PAIRWISE_CIPHER);
3104 	case_WLAN(INVALID_AKMP);
3105 	case_WLAN(UNSUPP_RSN_VERSION);
3106 	case_WLAN(INVALID_RSN_IE_CAP);
3107 	case_WLAN(IEEE8021X_FAILED);
3108 	case_WLAN(CIPHER_SUITE_REJECTED);
3109 	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3110 	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3111 	case_WLAN(DISASSOC_LOW_ACK);
3112 	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3113 	case_WLAN(QSTA_LEAVE_QBSS);
3114 	case_WLAN(QSTA_NOT_USE);
3115 	case_WLAN(QSTA_REQUIRE_SETUP);
3116 	case_WLAN(QSTA_TIMEOUT);
3117 	case_WLAN(QSTA_CIPHER_NOT_SUPP);
3118 	case_WLAN(MESH_PEER_CANCELED);
3119 	case_WLAN(MESH_MAX_PEERS);
3120 	case_WLAN(MESH_CONFIG);
3121 	case_WLAN(MESH_CLOSE);
3122 	case_WLAN(MESH_MAX_RETRIES);
3123 	case_WLAN(MESH_CONFIRM_TIMEOUT);
3124 	case_WLAN(MESH_INVALID_GTK);
3125 	case_WLAN(MESH_INCONSISTENT_PARAM);
3126 	case_WLAN(MESH_INVALID_SECURITY);
3127 	case_WLAN(MESH_PATH_ERROR);
3128 	case_WLAN(MESH_PATH_NOFORWARD);
3129 	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3130 	case_WLAN(MAC_EXISTS_IN_MBSS);
3131 	case_WLAN(MESH_CHAN_REGULATORY);
3132 	case_WLAN(MESH_CHAN);
3133 	default: return "<unknown>";
3134 	}
3135 }
3136 
3137 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3138 				     struct ieee80211_mgmt *mgmt, size_t len)
3139 {
3140 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3141 	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3142 
3143 	sdata_assert_lock(sdata);
3144 
3145 	if (len < 24 + 2)
3146 		return;
3147 
3148 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3149 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3150 		return;
3151 	}
3152 
3153 	if (ifmgd->associated &&
3154 	    ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3155 		const u8 *bssid = ifmgd->associated->bssid;
3156 
3157 		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3158 			   bssid, reason_code,
3159 			   ieee80211_get_reason_code_string(reason_code));
3160 
3161 		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3162 
3163 		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3164 					    reason_code, false);
3165 		return;
3166 	}
3167 
3168 	if (ifmgd->assoc_data &&
3169 	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3170 		const u8 *bssid = ifmgd->assoc_data->bss->bssid;
3171 
3172 		sdata_info(sdata,
3173 			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3174 			   bssid, reason_code,
3175 			   ieee80211_get_reason_code_string(reason_code));
3176 
3177 		ieee80211_destroy_assoc_data(sdata, false, true);
3178 
3179 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3180 		return;
3181 	}
3182 }
3183 
3184 
3185 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3186 				       struct ieee80211_mgmt *mgmt, size_t len)
3187 {
3188 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3189 	u16 reason_code;
3190 
3191 	sdata_assert_lock(sdata);
3192 
3193 	if (len < 24 + 2)
3194 		return;
3195 
3196 	if (!ifmgd->associated ||
3197 	    !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3198 		return;
3199 
3200 	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3201 
3202 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3203 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3204 		return;
3205 	}
3206 
3207 	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3208 		   mgmt->sa, reason_code,
3209 		   ieee80211_get_reason_code_string(reason_code));
3210 
3211 	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3212 
3213 	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
3214 				    false);
3215 }
3216 
3217 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3218 				u8 *supp_rates, unsigned int supp_rates_len,
3219 				u32 *rates, u32 *basic_rates,
3220 				bool *have_higher_than_11mbit,
3221 				int *min_rate, int *min_rate_index,
3222 				int shift)
3223 {
3224 	int i, j;
3225 
3226 	for (i = 0; i < supp_rates_len; i++) {
3227 		int rate = supp_rates[i] & 0x7f;
3228 		bool is_basic = !!(supp_rates[i] & 0x80);
3229 
3230 		if ((rate * 5 * (1 << shift)) > 110)
3231 			*have_higher_than_11mbit = true;
3232 
3233 		/*
3234 		 * Skip HT, VHT, HE and SAE H2E only BSS membership selectors
3235 		 * since they're not rates.
3236 		 *
3237 		 * Note: Even though the membership selector and the basic
3238 		 *	 rate flag share the same bit, they are not exactly
3239 		 *	 the same.
3240 		 */
3241 		if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3242 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3243 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY) ||
3244 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E))
3245 			continue;
3246 
3247 		for (j = 0; j < sband->n_bitrates; j++) {
3248 			struct ieee80211_rate *br;
3249 			int brate;
3250 
3251 			br = &sband->bitrates[j];
3252 
3253 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3254 			if (brate == rate) {
3255 				*rates |= BIT(j);
3256 				if (is_basic)
3257 					*basic_rates |= BIT(j);
3258 				if ((rate * 5) < *min_rate) {
3259 					*min_rate = rate * 5;
3260 					*min_rate_index = j;
3261 				}
3262 				break;
3263 			}
3264 		}
3265 	}
3266 }
3267 
3268 static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3269 					const struct ieee802_11_elems *elems)
3270 {
3271 	if (elems->ext_capab_len < 10)
3272 		return false;
3273 
3274 	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3275 		return false;
3276 
3277 	return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3278 		IEEE80211_HE_MAC_CAP0_TWT_RES;
3279 }
3280 
3281 static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3282 				    struct sta_info *sta,
3283 				    struct ieee802_11_elems *elems)
3284 {
3285 	bool twt = ieee80211_twt_req_supported(sta, elems);
3286 
3287 	if (sdata->vif.bss_conf.twt_requester != twt) {
3288 		sdata->vif.bss_conf.twt_requester = twt;
3289 		return BSS_CHANGED_TWT;
3290 	}
3291 	return 0;
3292 }
3293 
3294 static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
3295 					struct ieee80211_bss_conf *bss_conf,
3296 					struct ieee80211_supported_band *sband,
3297 					struct sta_info *sta)
3298 {
3299 	const struct ieee80211_sta_he_cap *own_he_cap =
3300 		ieee80211_get_he_iftype_cap(sband,
3301 					    ieee80211_vif_type_p2p(&sdata->vif));
3302 
3303 	return bss_conf->he_support &&
3304 		(sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3305 			IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
3306 		own_he_cap &&
3307 		(own_he_cap->he_cap_elem.mac_cap_info[2] &
3308 			IEEE80211_HE_MAC_CAP2_BCAST_TWT);
3309 }
3310 
3311 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3312 				    struct cfg80211_bss *cbss,
3313 				    struct ieee80211_mgmt *mgmt, size_t len,
3314 				    struct ieee802_11_elems *elems)
3315 {
3316 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3317 	struct ieee80211_local *local = sdata->local;
3318 	struct ieee80211_supported_band *sband;
3319 	struct sta_info *sta;
3320 	u16 capab_info, aid;
3321 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3322 	const struct cfg80211_bss_ies *bss_ies = NULL;
3323 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3324 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
3325 	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
3326 	u32 changed = 0;
3327 	u8 *pos;
3328 	int err;
3329 	bool ret;
3330 
3331 	/* AssocResp and ReassocResp have identical structure */
3332 
3333 	pos = mgmt->u.assoc_resp.variable;
3334 	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3335 	if (is_s1g) {
3336 		pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3337 		aid = 0; /* TODO */
3338 	}
3339 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3340 	elems = ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false,
3341 				       mgmt->bssid, assoc_data->bss->bssid);
3342 
3343 	if (!elems)
3344 		return false;
3345 
3346 	if (elems->aid_resp)
3347 		aid = le16_to_cpu(elems->aid_resp->aid);
3348 
3349 	/*
3350 	 * The 5 MSB of the AID field are reserved
3351 	 * (802.11-2016 9.4.1.8 AID field)
3352 	 */
3353 	aid &= 0x7ff;
3354 
3355 	ifmgd->broken_ap = false;
3356 
3357 	if (aid == 0 || aid > IEEE80211_MAX_AID) {
3358 		sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3359 			   aid);
3360 		aid = 0;
3361 		ifmgd->broken_ap = true;
3362 	}
3363 
3364 	if (!is_s1g && !elems->supp_rates) {
3365 		sdata_info(sdata, "no SuppRates element in AssocResp\n");
3366 		ret = false;
3367 		goto out;
3368 	}
3369 
3370 	sdata->vif.bss_conf.aid = aid;
3371 	ifmgd->tdls_chan_switch_prohibited =
3372 		elems->ext_capab && elems->ext_capab_len >= 5 &&
3373 		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3374 
3375 	/*
3376 	 * Some APs are erroneously not including some information in their
3377 	 * (re)association response frames. Try to recover by using the data
3378 	 * from the beacon or probe response. This seems to afflict mobile
3379 	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3380 	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3381 	 */
3382 	if (!is_6ghz &&
3383 	    ((assoc_data->wmm && !elems->wmm_param) ||
3384 	     (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3385 	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
3386 	     (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3387 	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
3388 		const struct cfg80211_bss_ies *ies;
3389 		struct ieee802_11_elems *bss_elems;
3390 
3391 		rcu_read_lock();
3392 		ies = rcu_dereference(cbss->ies);
3393 		if (ies)
3394 			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3395 					  GFP_ATOMIC);
3396 		rcu_read_unlock();
3397 		if (!bss_ies) {
3398 			ret = false;
3399 			goto out;
3400 		}
3401 
3402 		bss_elems = ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3403 						   false, mgmt->bssid,
3404 						   assoc_data->bss->bssid);
3405 		if (!bss_elems) {
3406 			ret = false;
3407 			goto out;
3408 		}
3409 
3410 		if (assoc_data->wmm &&
3411 		    !elems->wmm_param && bss_elems->wmm_param) {
3412 			elems->wmm_param = bss_elems->wmm_param;
3413 			sdata_info(sdata,
3414 				   "AP bug: WMM param missing from AssocResp\n");
3415 		}
3416 
3417 		/*
3418 		 * Also check if we requested HT/VHT, otherwise the AP doesn't
3419 		 * have to include the IEs in the (re)association response.
3420 		 */
3421 		if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
3422 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3423 			elems->ht_cap_elem = bss_elems->ht_cap_elem;
3424 			sdata_info(sdata,
3425 				   "AP bug: HT capability missing from AssocResp\n");
3426 		}
3427 		if (!elems->ht_operation && bss_elems->ht_operation &&
3428 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3429 			elems->ht_operation = bss_elems->ht_operation;
3430 			sdata_info(sdata,
3431 				   "AP bug: HT operation missing from AssocResp\n");
3432 		}
3433 		if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
3434 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3435 			elems->vht_cap_elem = bss_elems->vht_cap_elem;
3436 			sdata_info(sdata,
3437 				   "AP bug: VHT capa missing from AssocResp\n");
3438 		}
3439 		if (!elems->vht_operation && bss_elems->vht_operation &&
3440 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3441 			elems->vht_operation = bss_elems->vht_operation;
3442 			sdata_info(sdata,
3443 				   "AP bug: VHT operation missing from AssocResp\n");
3444 		}
3445 
3446 		kfree(bss_elems);
3447 	}
3448 
3449 	/*
3450 	 * We previously checked these in the beacon/probe response, so
3451 	 * they should be present here. This is just a safety net.
3452 	 */
3453 	if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3454 	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
3455 		sdata_info(sdata,
3456 			   "HT AP is missing WMM params or HT capability/operation\n");
3457 		ret = false;
3458 		goto out;
3459 	}
3460 
3461 	if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3462 	    (!elems->vht_cap_elem || !elems->vht_operation)) {
3463 		sdata_info(sdata,
3464 			   "VHT AP is missing VHT capability/operation\n");
3465 		ret = false;
3466 		goto out;
3467 	}
3468 
3469 	if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3470 	    !elems->he_6ghz_capa) {
3471 		sdata_info(sdata,
3472 			   "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3473 		ret = false;
3474 		goto out;
3475 	}
3476 
3477 	mutex_lock(&sdata->local->sta_mtx);
3478 	/*
3479 	 * station info was already allocated and inserted before
3480 	 * the association and should be available to us
3481 	 */
3482 	sta = sta_info_get(sdata, cbss->bssid);
3483 	if (WARN_ON(!sta)) {
3484 		mutex_unlock(&sdata->local->sta_mtx);
3485 		ret = false;
3486 		goto out;
3487 	}
3488 
3489 	sband = ieee80211_get_sband(sdata);
3490 	if (!sband) {
3491 		mutex_unlock(&sdata->local->sta_mtx);
3492 		ret = false;
3493 		goto out;
3494 	}
3495 
3496 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3497 	    (!elems->he_cap || !elems->he_operation)) {
3498 		mutex_unlock(&sdata->local->sta_mtx);
3499 		sdata_info(sdata,
3500 			   "HE AP is missing HE capability/operation\n");
3501 		ret = false;
3502 		goto out;
3503 	}
3504 
3505 	/* Set up internal HT/VHT capabilities */
3506 	if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3507 		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3508 						  elems->ht_cap_elem, sta);
3509 
3510 	if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3511 		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3512 						    elems->vht_cap_elem, sta);
3513 
3514 	if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3515 	    elems->he_cap) {
3516 		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3517 						  elems->he_cap,
3518 						  elems->he_cap_len,
3519 						  elems->he_6ghz_capa,
3520 						  sta);
3521 
3522 		bss_conf->he_support = sta->sta.he_cap.has_he;
3523 		if (elems->rsnx && elems->rsnx_len &&
3524 		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3525 		    wiphy_ext_feature_isset(local->hw.wiphy,
3526 					    NL80211_EXT_FEATURE_PROTECTED_TWT))
3527 			bss_conf->twt_protected = true;
3528 		else
3529 			bss_conf->twt_protected = false;
3530 
3531 		changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
3532 	} else {
3533 		bss_conf->he_support = false;
3534 		bss_conf->twt_requester = false;
3535 		bss_conf->twt_protected = false;
3536 	}
3537 
3538 	bss_conf->twt_broadcast =
3539 		ieee80211_twt_bcast_support(sdata, bss_conf, sband, sta);
3540 
3541 	if (bss_conf->he_support) {
3542 		bss_conf->he_bss_color.color =
3543 			le32_get_bits(elems->he_operation->he_oper_params,
3544 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3545 		bss_conf->he_bss_color.partial =
3546 			le32_get_bits(elems->he_operation->he_oper_params,
3547 				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
3548 		bss_conf->he_bss_color.enabled =
3549 			!le32_get_bits(elems->he_operation->he_oper_params,
3550 				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3551 
3552 		if (bss_conf->he_bss_color.enabled)
3553 			changed |= BSS_CHANGED_HE_BSS_COLOR;
3554 
3555 		bss_conf->htc_trig_based_pkt_ext =
3556 			le32_get_bits(elems->he_operation->he_oper_params,
3557 			      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
3558 		bss_conf->frame_time_rts_th =
3559 			le32_get_bits(elems->he_operation->he_oper_params,
3560 			      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3561 
3562 		bss_conf->uora_exists = !!elems->uora_element;
3563 		if (elems->uora_element)
3564 			bss_conf->uora_ocw_range = elems->uora_element[0];
3565 
3566 		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3567 		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
3568 		/* TODO: OPEN: what happens if BSS color disable is set? */
3569 	}
3570 
3571 	if (cbss->transmitted_bss) {
3572 		bss_conf->nontransmitted = true;
3573 		ether_addr_copy(bss_conf->transmitter_bssid,
3574 				cbss->transmitted_bss->bssid);
3575 		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3576 		bss_conf->bssid_index = cbss->bssid_index;
3577 	}
3578 
3579 	/*
3580 	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3581 	 * in their association response, so ignore that data for our own
3582 	 * configuration. If it changed since the last beacon, we'll get the
3583 	 * next beacon and update then.
3584 	 */
3585 
3586 	/*
3587 	 * If an operating mode notification IE is present, override the
3588 	 * NSS calculation (that would be done in rate_control_rate_init())
3589 	 * and use the # of streams from that element.
3590 	 */
3591 	if (elems->opmode_notif &&
3592 	    !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3593 		u8 nss;
3594 
3595 		nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3596 		nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3597 		nss += 1;
3598 		sta->sta.rx_nss = nss;
3599 	}
3600 
3601 	rate_control_rate_init(sta);
3602 
3603 	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3604 		set_sta_flag(sta, WLAN_STA_MFP);
3605 		sta->sta.mfp = true;
3606 	} else {
3607 		sta->sta.mfp = false;
3608 	}
3609 
3610 	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
3611 		       local->hw.queues >= IEEE80211_NUM_ACS;
3612 
3613 	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3614 	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3615 		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3616 	if (err) {
3617 		sdata_info(sdata,
3618 			   "failed to move station %pM to desired state\n",
3619 			   sta->sta.addr);
3620 		WARN_ON(__sta_info_destroy(sta));
3621 		mutex_unlock(&sdata->local->sta_mtx);
3622 		ret = false;
3623 		goto out;
3624 	}
3625 
3626 	if (sdata->wdev.use_4addr)
3627 		drv_sta_set_4addr(local, sdata, &sta->sta, true);
3628 
3629 	mutex_unlock(&sdata->local->sta_mtx);
3630 
3631 	/*
3632 	 * Always handle WMM once after association regardless
3633 	 * of the first value the AP uses. Setting -1 here has
3634 	 * that effect because the AP values is an unsigned
3635 	 * 4-bit value.
3636 	 */
3637 	ifmgd->wmm_last_param_set = -1;
3638 	ifmgd->mu_edca_last_param_set = -1;
3639 
3640 	if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3641 		ieee80211_set_wmm_default(sdata, false, false);
3642 	} else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3643 					     elems->wmm_param_len,
3644 					     elems->mu_edca_param_set)) {
3645 		/* still enable QoS since we might have HT/VHT */
3646 		ieee80211_set_wmm_default(sdata, false, true);
3647 		/* set the disable-WMM flag in this case to disable
3648 		 * tracking WMM parameter changes in the beacon if
3649 		 * the parameters weren't actually valid. Doing so
3650 		 * avoids changing parameters very strangely when
3651 		 * the AP is going back and forth between valid and
3652 		 * invalid parameters.
3653 		 */
3654 		ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3655 	}
3656 	changed |= BSS_CHANGED_QOS;
3657 
3658 	if (elems->max_idle_period_ie) {
3659 		bss_conf->max_idle_period =
3660 			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
3661 		bss_conf->protected_keep_alive =
3662 			!!(elems->max_idle_period_ie->idle_options &
3663 			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3664 		changed |= BSS_CHANGED_KEEP_ALIVE;
3665 	} else {
3666 		bss_conf->max_idle_period = 0;
3667 		bss_conf->protected_keep_alive = false;
3668 	}
3669 
3670 	/* set assoc capability (AID was already set earlier),
3671 	 * ieee80211_set_associated() will tell the driver */
3672 	bss_conf->assoc_capability = capab_info;
3673 	ieee80211_set_associated(sdata, cbss, changed);
3674 
3675 	/*
3676 	 * If we're using 4-addr mode, let the AP know that we're
3677 	 * doing so, so that it can create the STA VLAN on its side
3678 	 */
3679 	if (ifmgd->use_4addr)
3680 		ieee80211_send_4addr_nullfunc(local, sdata);
3681 
3682 	/*
3683 	 * Start timer to probe the connection to the AP now.
3684 	 * Also start the timer that will detect beacon loss.
3685 	 */
3686 	ieee80211_sta_reset_beacon_monitor(sdata);
3687 	ieee80211_sta_reset_conn_monitor(sdata);
3688 
3689 	ret = true;
3690  out:
3691 	kfree(elems);
3692 	kfree(bss_ies);
3693 	return ret;
3694 }
3695 
3696 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3697 					 struct ieee80211_mgmt *mgmt,
3698 					 size_t len)
3699 {
3700 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3701 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3702 	u16 capab_info, status_code, aid;
3703 	struct ieee802_11_elems *elems;
3704 	int ac, uapsd_queues = -1;
3705 	u8 *pos;
3706 	bool reassoc;
3707 	struct cfg80211_bss *cbss;
3708 	struct ieee80211_event event = {
3709 		.type = MLME_EVENT,
3710 		.u.mlme.data = ASSOC_EVENT,
3711 	};
3712 	struct ieee80211_prep_tx_info info = {};
3713 
3714 	sdata_assert_lock(sdata);
3715 
3716 	if (!assoc_data)
3717 		return;
3718 
3719 	if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
3720 		return;
3721 
3722 	cbss = assoc_data->bss;
3723 
3724 	/*
3725 	 * AssocResp and ReassocResp have identical structure, so process both
3726 	 * of them in this function.
3727 	 */
3728 
3729 	if (len < 24 + 6)
3730 		return;
3731 
3732 	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
3733 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3734 	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3735 	pos = mgmt->u.assoc_resp.variable;
3736 	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3737 	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
3738 		pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3739 		aid = 0; /* TODO */
3740 	}
3741 
3742 	/*
3743 	 * Note: this may not be perfect, AP might misbehave - if
3744 	 * anyone needs to rely on perfect complete notification
3745 	 * with the exact right subtype, then we need to track what
3746 	 * we actually transmitted.
3747 	 */
3748 	info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
3749 				 IEEE80211_STYPE_ASSOC_REQ;
3750 
3751 	sdata_info(sdata,
3752 		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3753 		   reassoc ? "Rea" : "A", mgmt->sa,
3754 		   capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
3755 
3756 	if (assoc_data->fils_kek_len &&
3757 	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3758 		return;
3759 
3760 	elems = ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false,
3761 				       mgmt->bssid, assoc_data->bss->bssid);
3762 	if (!elems)
3763 		goto notify_driver;
3764 
3765 	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
3766 	    elems->timeout_int &&
3767 	    elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
3768 		u32 tu, ms;
3769 
3770 		cfg80211_assoc_comeback(sdata->dev, assoc_data->bss,
3771 					le32_to_cpu(elems->timeout_int->value));
3772 
3773 		tu = le32_to_cpu(elems->timeout_int->value);
3774 		ms = tu * 1024 / 1000;
3775 		sdata_info(sdata,
3776 			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3777 			   mgmt->sa, tu, ms);
3778 		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
3779 		assoc_data->timeout_started = true;
3780 		if (ms > IEEE80211_ASSOC_TIMEOUT)
3781 			run_again(sdata, assoc_data->timeout);
3782 		goto notify_driver;
3783 	}
3784 
3785 	if (status_code != WLAN_STATUS_SUCCESS) {
3786 		sdata_info(sdata, "%pM denied association (code=%d)\n",
3787 			   mgmt->sa, status_code);
3788 		ieee80211_destroy_assoc_data(sdata, false, false);
3789 		event.u.mlme.status = MLME_DENIED;
3790 		event.u.mlme.reason = status_code;
3791 		drv_event_callback(sdata->local, sdata, &event);
3792 	} else {
3793 		if (!ieee80211_assoc_success(sdata, cbss, mgmt, len, elems)) {
3794 			/* oops -- internal error -- send timeout for now */
3795 			ieee80211_destroy_assoc_data(sdata, false, false);
3796 			cfg80211_assoc_timeout(sdata->dev, cbss);
3797 			goto notify_driver;
3798 		}
3799 		event.u.mlme.status = MLME_SUCCESS;
3800 		drv_event_callback(sdata->local, sdata, &event);
3801 		sdata_info(sdata, "associated\n");
3802 
3803 		/*
3804 		 * destroy assoc_data afterwards, as otherwise an idle
3805 		 * recalc after assoc_data is NULL but before associated
3806 		 * is set can cause the interface to go idle
3807 		 */
3808 		ieee80211_destroy_assoc_data(sdata, true, false);
3809 
3810 		/* get uapsd queues configuration */
3811 		uapsd_queues = 0;
3812 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3813 			if (sdata->tx_conf[ac].uapsd)
3814 				uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
3815 
3816 		info.success = 1;
3817 	}
3818 
3819 	cfg80211_rx_assoc_resp(sdata->dev, cbss, (u8 *)mgmt, len, uapsd_queues,
3820 			       ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
3821 notify_driver:
3822 	drv_mgd_complete_tx(sdata->local, sdata, &info);
3823 	kfree(elems);
3824 }
3825 
3826 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
3827 				  struct ieee80211_mgmt *mgmt, size_t len,
3828 				  struct ieee80211_rx_status *rx_status)
3829 {
3830 	struct ieee80211_local *local = sdata->local;
3831 	struct ieee80211_bss *bss;
3832 	struct ieee80211_channel *channel;
3833 
3834 	sdata_assert_lock(sdata);
3835 
3836 	channel = ieee80211_get_channel_khz(local->hw.wiphy,
3837 					ieee80211_rx_status_to_khz(rx_status));
3838 	if (!channel)
3839 		return;
3840 
3841 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
3842 	if (bss) {
3843 		sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
3844 		ieee80211_rx_bss_put(local, bss);
3845 	}
3846 }
3847 
3848 
3849 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
3850 					 struct sk_buff *skb)
3851 {
3852 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
3853 	struct ieee80211_if_managed *ifmgd;
3854 	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3855 	struct ieee80211_channel *channel;
3856 	size_t baselen, len = skb->len;
3857 
3858 	ifmgd = &sdata->u.mgd;
3859 
3860 	sdata_assert_lock(sdata);
3861 
3862 	/*
3863 	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3864 	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
3865 	 * a Probe Response frame [..], the Address 1 field of the Probe
3866 	 * Response frame shall be set to the broadcast address [..]"
3867 	 * So, on 6GHz band we should also accept broadcast responses.
3868 	 */
3869 	channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3870 					rx_status->freq);
3871 	if (!channel)
3872 		return;
3873 
3874 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3875 	    (channel->band != NL80211_BAND_6GHZ ||
3876 	     !is_broadcast_ether_addr(mgmt->da)))
3877 		return; /* ignore ProbeResp to foreign address */
3878 
3879 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3880 	if (baselen > len)
3881 		return;
3882 
3883 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3884 
3885 	if (ifmgd->associated &&
3886 	    ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3887 		ieee80211_reset_ap_probe(sdata);
3888 }
3889 
3890 /*
3891  * This is the canonical list of information elements we care about,
3892  * the filter code also gives us all changes to the Microsoft OUI
3893  * (00:50:F2) vendor IE which is used for WMM which we need to track,
3894  * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3895  * changes to requested client power.
3896  *
3897  * We implement beacon filtering in software since that means we can
3898  * avoid processing the frame here and in cfg80211, and userspace
3899  * will not be able to tell whether the hardware supports it or not.
3900  *
3901  * XXX: This list needs to be dynamic -- userspace needs to be able to
3902  *	add items it requires. It also needs to be able to tell us to
3903  *	look out for other vendor IEs.
3904  */
3905 static const u64 care_about_ies =
3906 	(1ULL << WLAN_EID_COUNTRY) |
3907 	(1ULL << WLAN_EID_ERP_INFO) |
3908 	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
3909 	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
3910 	(1ULL << WLAN_EID_HT_CAPABILITY) |
3911 	(1ULL << WLAN_EID_HT_OPERATION) |
3912 	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
3913 
3914 static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3915 					struct ieee80211_if_managed *ifmgd,
3916 					struct ieee80211_bss_conf *bss_conf,
3917 					struct ieee80211_local *local,
3918 					struct ieee80211_rx_status *rx_status)
3919 {
3920 	/* Track average RSSI from the Beacon frames of the current AP */
3921 
3922 	if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3923 		ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3924 		ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
3925 		ifmgd->last_cqm_event_signal = 0;
3926 		ifmgd->count_beacon_signal = 1;
3927 		ifmgd->last_ave_beacon_signal = 0;
3928 	} else {
3929 		ifmgd->count_beacon_signal++;
3930 	}
3931 
3932 	ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3933 
3934 	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3935 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3936 		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3937 		int last_sig = ifmgd->last_ave_beacon_signal;
3938 		struct ieee80211_event event = {
3939 			.type = RSSI_EVENT,
3940 		};
3941 
3942 		/*
3943 		 * if signal crosses either of the boundaries, invoke callback
3944 		 * with appropriate parameters
3945 		 */
3946 		if (sig > ifmgd->rssi_max_thold &&
3947 		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3948 			ifmgd->last_ave_beacon_signal = sig;
3949 			event.u.rssi.data = RSSI_EVENT_HIGH;
3950 			drv_event_callback(local, sdata, &event);
3951 		} else if (sig < ifmgd->rssi_min_thold &&
3952 			   (last_sig >= ifmgd->rssi_max_thold ||
3953 			   last_sig == 0)) {
3954 			ifmgd->last_ave_beacon_signal = sig;
3955 			event.u.rssi.data = RSSI_EVENT_LOW;
3956 			drv_event_callback(local, sdata, &event);
3957 		}
3958 	}
3959 
3960 	if (bss_conf->cqm_rssi_thold &&
3961 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3962 	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3963 		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3964 		int last_event = ifmgd->last_cqm_event_signal;
3965 		int thold = bss_conf->cqm_rssi_thold;
3966 		int hyst = bss_conf->cqm_rssi_hyst;
3967 
3968 		if (sig < thold &&
3969 		    (last_event == 0 || sig < last_event - hyst)) {
3970 			ifmgd->last_cqm_event_signal = sig;
3971 			ieee80211_cqm_rssi_notify(
3972 				&sdata->vif,
3973 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3974 				sig, GFP_KERNEL);
3975 		} else if (sig > thold &&
3976 			   (last_event == 0 || sig > last_event + hyst)) {
3977 			ifmgd->last_cqm_event_signal = sig;
3978 			ieee80211_cqm_rssi_notify(
3979 				&sdata->vif,
3980 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3981 				sig, GFP_KERNEL);
3982 		}
3983 	}
3984 
3985 	if (bss_conf->cqm_rssi_low &&
3986 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3987 		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3988 		int last_event = ifmgd->last_cqm_event_signal;
3989 		int low = bss_conf->cqm_rssi_low;
3990 		int high = bss_conf->cqm_rssi_high;
3991 
3992 		if (sig < low &&
3993 		    (last_event == 0 || last_event >= low)) {
3994 			ifmgd->last_cqm_event_signal = sig;
3995 			ieee80211_cqm_rssi_notify(
3996 				&sdata->vif,
3997 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3998 				sig, GFP_KERNEL);
3999 		} else if (sig > high &&
4000 			   (last_event == 0 || last_event <= high)) {
4001 			ifmgd->last_cqm_event_signal = sig;
4002 			ieee80211_cqm_rssi_notify(
4003 				&sdata->vif,
4004 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
4005 				sig, GFP_KERNEL);
4006 		}
4007 	}
4008 }
4009 
4010 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
4011 				    struct cfg80211_bss *bss)
4012 {
4013 	if (ether_addr_equal(tx_bssid, bss->bssid))
4014 		return true;
4015 	if (!bss->transmitted_bss)
4016 		return false;
4017 	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
4018 }
4019 
4020 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
4021 				     struct ieee80211_hdr *hdr, size_t len,
4022 				     struct ieee80211_rx_status *rx_status)
4023 {
4024 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4025 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
4026 	struct ieee80211_mgmt *mgmt = (void *) hdr;
4027 	size_t baselen;
4028 	struct ieee802_11_elems *elems;
4029 	struct ieee80211_local *local = sdata->local;
4030 	struct ieee80211_chanctx_conf *chanctx_conf;
4031 	struct ieee80211_channel *chan;
4032 	struct sta_info *sta;
4033 	u32 changed = 0;
4034 	bool erp_valid;
4035 	u8 erp_value = 0;
4036 	u32 ncrc = 0;
4037 	u8 *bssid, *variable = mgmt->u.beacon.variable;
4038 	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
4039 
4040 	sdata_assert_lock(sdata);
4041 
4042 	/* Process beacon from the current BSS */
4043 	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
4044 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
4045 		struct ieee80211_ext *ext = (void *) mgmt;
4046 
4047 		if (ieee80211_is_s1g_short_beacon(ext->frame_control))
4048 			variable = ext->u.s1g_short_beacon.variable;
4049 		else
4050 			variable = ext->u.s1g_beacon.variable;
4051 	}
4052 
4053 	baselen = (u8 *) variable - (u8 *) mgmt;
4054 	if (baselen > len)
4055 		return;
4056 
4057 	rcu_read_lock();
4058 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4059 	if (!chanctx_conf) {
4060 		rcu_read_unlock();
4061 		return;
4062 	}
4063 
4064 	if (ieee80211_rx_status_to_khz(rx_status) !=
4065 	    ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
4066 		rcu_read_unlock();
4067 		return;
4068 	}
4069 	chan = chanctx_conf->def.chan;
4070 	rcu_read_unlock();
4071 
4072 	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
4073 	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->bss)) {
4074 		elems = ieee802_11_parse_elems(variable, len - baselen, false,
4075 					       bssid,
4076 					       ifmgd->assoc_data->bss->bssid);
4077 		if (!elems)
4078 			return;
4079 
4080 		ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4081 
4082 		if (elems->dtim_period)
4083 			ifmgd->dtim_period = elems->dtim_period;
4084 		ifmgd->have_beacon = true;
4085 		ifmgd->assoc_data->need_beacon = false;
4086 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
4087 			sdata->vif.bss_conf.sync_tsf =
4088 				le64_to_cpu(mgmt->u.beacon.timestamp);
4089 			sdata->vif.bss_conf.sync_device_ts =
4090 				rx_status->device_timestamp;
4091 			sdata->vif.bss_conf.sync_dtim_count = elems->dtim_count;
4092 		}
4093 
4094 		if (elems->mbssid_config_ie)
4095 			bss_conf->profile_periodicity =
4096 				elems->mbssid_config_ie->profile_periodicity;
4097 		else
4098 			bss_conf->profile_periodicity = 0;
4099 
4100 		if (elems->ext_capab_len >= 11 &&
4101 		    (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
4102 			bss_conf->ema_ap = true;
4103 		else
4104 			bss_conf->ema_ap = false;
4105 
4106 		/* continue assoc process */
4107 		ifmgd->assoc_data->timeout = jiffies;
4108 		ifmgd->assoc_data->timeout_started = true;
4109 		run_again(sdata, ifmgd->assoc_data->timeout);
4110 		kfree(elems);
4111 		return;
4112 	}
4113 
4114 	if (!ifmgd->associated ||
4115 	    !ieee80211_rx_our_beacon(bssid,  ifmgd->associated))
4116 		return;
4117 	bssid = ifmgd->associated->bssid;
4118 
4119 	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4120 		ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
4121 					    local, rx_status);
4122 
4123 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
4124 		mlme_dbg_ratelimited(sdata,
4125 				     "cancelling AP probe due to a received beacon\n");
4126 		ieee80211_reset_ap_probe(sdata);
4127 	}
4128 
4129 	/*
4130 	 * Push the beacon loss detection into the future since
4131 	 * we are processing a beacon from the AP just now.
4132 	 */
4133 	ieee80211_sta_reset_beacon_monitor(sdata);
4134 
4135 	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
4136 	 * element (which carries the beacon interval). Don't forget to add a
4137 	 * bit to care_about_ies[] above if mac80211 is interested in a
4138 	 * changing S1G element.
4139 	 */
4140 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4141 		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4142 	elems = ieee802_11_parse_elems_crc(variable, len - baselen,
4143 					   false, care_about_ies, ncrc,
4144 					   mgmt->bssid, bssid);
4145 	if (!elems)
4146 		return;
4147 	ncrc = elems->crc;
4148 
4149 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
4150 	    ieee80211_check_tim(elems->tim, elems->tim_len, bss_conf->aid)) {
4151 		if (local->hw.conf.dynamic_ps_timeout > 0) {
4152 			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4153 				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4154 				ieee80211_hw_config(local,
4155 						    IEEE80211_CONF_CHANGE_PS);
4156 			}
4157 			ieee80211_send_nullfunc(local, sdata, false);
4158 		} else if (!local->pspolling && sdata->u.mgd.powersave) {
4159 			local->pspolling = true;
4160 
4161 			/*
4162 			 * Here is assumed that the driver will be
4163 			 * able to send ps-poll frame and receive a
4164 			 * response even though power save mode is
4165 			 * enabled, but some drivers might require
4166 			 * to disable power save here. This needs
4167 			 * to be investigated.
4168 			 */
4169 			ieee80211_send_pspoll(local, sdata);
4170 		}
4171 	}
4172 
4173 	if (sdata->vif.p2p ||
4174 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
4175 		struct ieee80211_p2p_noa_attr noa = {};
4176 		int ret;
4177 
4178 		ret = cfg80211_get_p2p_attr(variable,
4179 					    len - baselen,
4180 					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
4181 					    (u8 *) &noa, sizeof(noa));
4182 		if (ret >= 2) {
4183 			if (sdata->u.mgd.p2p_noa_index != noa.index) {
4184 				/* valid noa_attr and index changed */
4185 				sdata->u.mgd.p2p_noa_index = noa.index;
4186 				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4187 				changed |= BSS_CHANGED_P2P_PS;
4188 				/*
4189 				 * make sure we update all information, the CRC
4190 				 * mechanism doesn't look at P2P attributes.
4191 				 */
4192 				ifmgd->beacon_crc_valid = false;
4193 			}
4194 		} else if (sdata->u.mgd.p2p_noa_index != -1) {
4195 			/* noa_attr not found and we had valid noa_attr before */
4196 			sdata->u.mgd.p2p_noa_index = -1;
4197 			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
4198 			changed |= BSS_CHANGED_P2P_PS;
4199 			ifmgd->beacon_crc_valid = false;
4200 		}
4201 	}
4202 
4203 	if (ifmgd->csa_waiting_bcn)
4204 		ieee80211_chswitch_post_beacon(sdata);
4205 
4206 	/*
4207 	 * Update beacon timing and dtim count on every beacon appearance. This
4208 	 * will allow the driver to use the most updated values. Do it before
4209 	 * comparing this one with last received beacon.
4210 	 * IMPORTANT: These parameters would possibly be out of sync by the time
4211 	 * the driver will use them. The synchronized view is currently
4212 	 * guaranteed only in certain callbacks.
4213 	 */
4214 	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
4215 	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
4216 		sdata->vif.bss_conf.sync_tsf =
4217 			le64_to_cpu(mgmt->u.beacon.timestamp);
4218 		sdata->vif.bss_conf.sync_device_ts =
4219 			rx_status->device_timestamp;
4220 		sdata->vif.bss_conf.sync_dtim_count = elems->dtim_count;
4221 	}
4222 
4223 	if ((ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) ||
4224 	    ieee80211_is_s1g_short_beacon(mgmt->frame_control))
4225 		goto free;
4226 	ifmgd->beacon_crc = ncrc;
4227 	ifmgd->beacon_crc_valid = true;
4228 
4229 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4230 
4231 	ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
4232 					 rx_status->device_timestamp,
4233 					 elems, true);
4234 
4235 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4236 	    ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
4237 				     elems->wmm_param_len,
4238 				     elems->mu_edca_param_set))
4239 		changed |= BSS_CHANGED_QOS;
4240 
4241 	/*
4242 	 * If we haven't had a beacon before, tell the driver about the
4243 	 * DTIM period (and beacon timing if desired) now.
4244 	 */
4245 	if (!ifmgd->have_beacon) {
4246 		/* a few bogus AP send dtim_period = 0 or no TIM IE */
4247 		bss_conf->dtim_period = elems->dtim_period ?: 1;
4248 
4249 		changed |= BSS_CHANGED_BEACON_INFO;
4250 		ifmgd->have_beacon = true;
4251 
4252 		mutex_lock(&local->iflist_mtx);
4253 		ieee80211_recalc_ps(local);
4254 		mutex_unlock(&local->iflist_mtx);
4255 
4256 		ieee80211_recalc_ps_vif(sdata);
4257 	}
4258 
4259 	if (elems->erp_info) {
4260 		erp_valid = true;
4261 		erp_value = elems->erp_info[0];
4262 	} else {
4263 		erp_valid = false;
4264 	}
4265 
4266 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4267 		changed |= ieee80211_handle_bss_capability(sdata,
4268 				le16_to_cpu(mgmt->u.beacon.capab_info),
4269 				erp_valid, erp_value);
4270 
4271 	mutex_lock(&local->sta_mtx);
4272 	sta = sta_info_get(sdata, bssid);
4273 
4274 	changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
4275 
4276 	if (ieee80211_config_bw(sdata, sta, elems->ht_cap_elem,
4277 				elems->vht_cap_elem, elems->ht_operation,
4278 				elems->vht_operation, elems->he_operation,
4279 				elems->s1g_oper, bssid, &changed)) {
4280 		mutex_unlock(&local->sta_mtx);
4281 		sdata_info(sdata,
4282 			   "failed to follow AP %pM bandwidth change, disconnect\n",
4283 			   bssid);
4284 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4285 				       WLAN_REASON_DEAUTH_LEAVING,
4286 				       true, deauth_buf);
4287 		ieee80211_report_disconnect(sdata, deauth_buf,
4288 					    sizeof(deauth_buf), true,
4289 					    WLAN_REASON_DEAUTH_LEAVING,
4290 					    false);
4291 		goto free;
4292 	}
4293 
4294 	if (sta && elems->opmode_notif)
4295 		ieee80211_vht_handle_opmode(sdata, sta, *elems->opmode_notif,
4296 					    rx_status->band);
4297 	mutex_unlock(&local->sta_mtx);
4298 
4299 	changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4300 					       elems->country_elem,
4301 					       elems->country_elem_len,
4302 					       elems->pwr_constr_elem,
4303 					       elems->cisco_dtpc_elem);
4304 
4305 	ieee80211_bss_info_change_notify(sdata, changed);
4306 free:
4307 	kfree(elems);
4308 }
4309 
4310 void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
4311 				 struct sk_buff *skb)
4312 {
4313 	struct ieee80211_rx_status *rx_status;
4314 	struct ieee80211_hdr *hdr;
4315 	u16 fc;
4316 
4317 	rx_status = (struct ieee80211_rx_status *) skb->cb;
4318 	hdr = (struct ieee80211_hdr *) skb->data;
4319 	fc = le16_to_cpu(hdr->frame_control);
4320 
4321 	sdata_lock(sdata);
4322 	switch (fc & IEEE80211_FCTL_STYPE) {
4323 	case IEEE80211_STYPE_S1G_BEACON:
4324 		ieee80211_rx_mgmt_beacon(sdata, hdr, skb->len, rx_status);
4325 		break;
4326 	}
4327 	sdata_unlock(sdata);
4328 }
4329 
4330 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4331 				  struct sk_buff *skb)
4332 {
4333 	struct ieee80211_rx_status *rx_status;
4334 	struct ieee80211_mgmt *mgmt;
4335 	u16 fc;
4336 	int ies_len;
4337 
4338 	rx_status = (struct ieee80211_rx_status *) skb->cb;
4339 	mgmt = (struct ieee80211_mgmt *) skb->data;
4340 	fc = le16_to_cpu(mgmt->frame_control);
4341 
4342 	sdata_lock(sdata);
4343 
4344 	switch (fc & IEEE80211_FCTL_STYPE) {
4345 	case IEEE80211_STYPE_BEACON:
4346 		ieee80211_rx_mgmt_beacon(sdata, (void *)mgmt,
4347 					 skb->len, rx_status);
4348 		break;
4349 	case IEEE80211_STYPE_PROBE_RESP:
4350 		ieee80211_rx_mgmt_probe_resp(sdata, skb);
4351 		break;
4352 	case IEEE80211_STYPE_AUTH:
4353 		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
4354 		break;
4355 	case IEEE80211_STYPE_DEAUTH:
4356 		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
4357 		break;
4358 	case IEEE80211_STYPE_DISASSOC:
4359 		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
4360 		break;
4361 	case IEEE80211_STYPE_ASSOC_RESP:
4362 	case IEEE80211_STYPE_REASSOC_RESP:
4363 		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
4364 		break;
4365 	case IEEE80211_STYPE_ACTION:
4366 		if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
4367 			struct ieee802_11_elems *elems;
4368 
4369 			ies_len = skb->len -
4370 				  offsetof(struct ieee80211_mgmt,
4371 					   u.action.u.chan_switch.variable);
4372 
4373 			if (ies_len < 0)
4374 				break;
4375 
4376 			/* CSA IE cannot be overridden, no need for BSSID */
4377 			elems = ieee802_11_parse_elems(
4378 					mgmt->u.action.u.chan_switch.variable,
4379 					ies_len, true, mgmt->bssid, NULL);
4380 
4381 			if (elems && !elems->parse_error)
4382 				ieee80211_sta_process_chanswitch(sdata,
4383 								 rx_status->mactime,
4384 								 rx_status->device_timestamp,
4385 								 elems, false);
4386 			kfree(elems);
4387 		} else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4388 			struct ieee802_11_elems *elems;
4389 
4390 			ies_len = skb->len -
4391 				  offsetof(struct ieee80211_mgmt,
4392 					   u.action.u.ext_chan_switch.variable);
4393 
4394 			if (ies_len < 0)
4395 				break;
4396 
4397 			/*
4398 			 * extended CSA IE can't be overridden, no need for
4399 			 * BSSID
4400 			 */
4401 			elems = ieee802_11_parse_elems(
4402 					mgmt->u.action.u.ext_chan_switch.variable,
4403 					ies_len, true, mgmt->bssid, NULL);
4404 
4405 			if (elems && !elems->parse_error) {
4406 				/* for the handling code pretend it was an IE */
4407 				elems->ext_chansw_ie =
4408 					&mgmt->u.action.u.ext_chan_switch.data;
4409 
4410 				ieee80211_sta_process_chanswitch(sdata,
4411 								 rx_status->mactime,
4412 								 rx_status->device_timestamp,
4413 								 elems, false);
4414 			}
4415 
4416 			kfree(elems);
4417 		}
4418 		break;
4419 	}
4420 	sdata_unlock(sdata);
4421 }
4422 
4423 static void ieee80211_sta_timer(struct timer_list *t)
4424 {
4425 	struct ieee80211_sub_if_data *sdata =
4426 		from_timer(sdata, t, u.mgd.timer);
4427 
4428 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
4429 }
4430 
4431 void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4432 				   u8 *bssid, u8 reason, bool tx)
4433 {
4434 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4435 
4436 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
4437 			       tx, frame_buf);
4438 
4439 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4440 				    reason, false);
4441 }
4442 
4443 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
4444 {
4445 	struct ieee80211_local *local = sdata->local;
4446 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4447 	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
4448 	u32 tx_flags = 0;
4449 	u16 trans = 1;
4450 	u16 status = 0;
4451 	struct ieee80211_prep_tx_info info = {
4452 		.subtype = IEEE80211_STYPE_AUTH,
4453 	};
4454 
4455 	sdata_assert_lock(sdata);
4456 
4457 	if (WARN_ON_ONCE(!auth_data))
4458 		return -EINVAL;
4459 
4460 	auth_data->tries++;
4461 
4462 	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
4463 		sdata_info(sdata, "authentication with %pM timed out\n",
4464 			   auth_data->bss->bssid);
4465 
4466 		/*
4467 		 * Most likely AP is not in the range so remove the
4468 		 * bss struct for that AP.
4469 		 */
4470 		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4471 
4472 		return -ETIMEDOUT;
4473 	}
4474 
4475 	if (auth_data->algorithm == WLAN_AUTH_SAE)
4476 		info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4477 
4478 	drv_mgd_prepare_tx(local, sdata, &info);
4479 
4480 	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4481 		   auth_data->bss->bssid, auth_data->tries,
4482 		   IEEE80211_AUTH_MAX_TRIES);
4483 
4484 	auth_data->expected_transaction = 2;
4485 
4486 	if (auth_data->algorithm == WLAN_AUTH_SAE) {
4487 		trans = auth_data->sae_trans;
4488 		status = auth_data->sae_status;
4489 		auth_data->expected_transaction = trans;
4490 	}
4491 
4492 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4493 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4494 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
4495 
4496 	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4497 			    auth_data->data, auth_data->data_len,
4498 			    auth_data->bss->bssid,
4499 			    auth_data->bss->bssid, NULL, 0, 0,
4500 			    tx_flags);
4501 
4502 	if (tx_flags == 0) {
4503 		if (auth_data->algorithm == WLAN_AUTH_SAE)
4504 			auth_data->timeout = jiffies +
4505 				IEEE80211_AUTH_TIMEOUT_SAE;
4506 		else
4507 			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
4508 	} else {
4509 		auth_data->timeout =
4510 			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
4511 	}
4512 
4513 	auth_data->timeout_started = true;
4514 	run_again(sdata, auth_data->timeout);
4515 
4516 	return 0;
4517 }
4518 
4519 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4520 {
4521 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4522 	struct ieee80211_local *local = sdata->local;
4523 	int ret;
4524 
4525 	sdata_assert_lock(sdata);
4526 
4527 	assoc_data->tries++;
4528 	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
4529 		sdata_info(sdata, "association with %pM timed out\n",
4530 			   assoc_data->bss->bssid);
4531 
4532 		/*
4533 		 * Most likely AP is not in the range so remove the
4534 		 * bss struct for that AP.
4535 		 */
4536 		cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4537 
4538 		return -ETIMEDOUT;
4539 	}
4540 
4541 	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4542 		   assoc_data->bss->bssid, assoc_data->tries,
4543 		   IEEE80211_ASSOC_MAX_TRIES);
4544 	ret = ieee80211_send_assoc(sdata);
4545 	if (ret)
4546 		return ret;
4547 
4548 	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4549 		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
4550 		assoc_data->timeout_started = true;
4551 		run_again(sdata, assoc_data->timeout);
4552 	} else {
4553 		assoc_data->timeout =
4554 			round_jiffies_up(jiffies +
4555 					 IEEE80211_ASSOC_TIMEOUT_LONG);
4556 		assoc_data->timeout_started = true;
4557 		run_again(sdata, assoc_data->timeout);
4558 	}
4559 
4560 	return 0;
4561 }
4562 
4563 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4564 				  __le16 fc, bool acked)
4565 {
4566 	struct ieee80211_local *local = sdata->local;
4567 
4568 	sdata->u.mgd.status_fc = fc;
4569 	sdata->u.mgd.status_acked = acked;
4570 	sdata->u.mgd.status_received = true;
4571 
4572 	ieee80211_queue_work(&local->hw, &sdata->work);
4573 }
4574 
4575 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
4576 {
4577 	struct ieee80211_local *local = sdata->local;
4578 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4579 
4580 	sdata_lock(sdata);
4581 
4582 	if (ifmgd->status_received) {
4583 		__le16 fc = ifmgd->status_fc;
4584 		bool status_acked = ifmgd->status_acked;
4585 
4586 		ifmgd->status_received = false;
4587 		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
4588 			if (status_acked) {
4589 				if (ifmgd->auth_data->algorithm ==
4590 				    WLAN_AUTH_SAE)
4591 					ifmgd->auth_data->timeout =
4592 						jiffies +
4593 						IEEE80211_AUTH_TIMEOUT_SAE;
4594 				else
4595 					ifmgd->auth_data->timeout =
4596 						jiffies +
4597 						IEEE80211_AUTH_TIMEOUT_SHORT;
4598 				run_again(sdata, ifmgd->auth_data->timeout);
4599 			} else {
4600 				ifmgd->auth_data->timeout = jiffies - 1;
4601 			}
4602 			ifmgd->auth_data->timeout_started = true;
4603 		} else if (ifmgd->assoc_data &&
4604 			   (ieee80211_is_assoc_req(fc) ||
4605 			    ieee80211_is_reassoc_req(fc))) {
4606 			if (status_acked) {
4607 				ifmgd->assoc_data->timeout =
4608 					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
4609 				run_again(sdata, ifmgd->assoc_data->timeout);
4610 			} else {
4611 				ifmgd->assoc_data->timeout = jiffies - 1;
4612 			}
4613 			ifmgd->assoc_data->timeout_started = true;
4614 		}
4615 	}
4616 
4617 	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
4618 	    time_after(jiffies, ifmgd->auth_data->timeout)) {
4619 		if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
4620 			/*
4621 			 * ok ... we waited for assoc or continuation but
4622 			 * userspace didn't do it, so kill the auth data
4623 			 */
4624 			ieee80211_destroy_auth_data(sdata, false);
4625 		} else if (ieee80211_auth(sdata)) {
4626 			u8 bssid[ETH_ALEN];
4627 			struct ieee80211_event event = {
4628 				.type = MLME_EVENT,
4629 				.u.mlme.data = AUTH_EVENT,
4630 				.u.mlme.status = MLME_TIMEOUT,
4631 			};
4632 
4633 			memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4634 
4635 			ieee80211_destroy_auth_data(sdata, false);
4636 
4637 			cfg80211_auth_timeout(sdata->dev, bssid);
4638 			drv_event_callback(sdata->local, sdata, &event);
4639 		}
4640 	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
4641 		run_again(sdata, ifmgd->auth_data->timeout);
4642 
4643 	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
4644 	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
4645 		if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
4646 		    ieee80211_do_assoc(sdata)) {
4647 			struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4648 			struct ieee80211_event event = {
4649 				.type = MLME_EVENT,
4650 				.u.mlme.data = ASSOC_EVENT,
4651 				.u.mlme.status = MLME_TIMEOUT,
4652 			};
4653 
4654 			ieee80211_destroy_assoc_data(sdata, false, false);
4655 			cfg80211_assoc_timeout(sdata->dev, bss);
4656 			drv_event_callback(sdata->local, sdata, &event);
4657 		}
4658 	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
4659 		run_again(sdata, ifmgd->assoc_data->timeout);
4660 
4661 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
4662 	    ifmgd->associated) {
4663 		u8 bssid[ETH_ALEN];
4664 		int max_tries;
4665 
4666 		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4667 
4668 		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4669 			max_tries = max_nullfunc_tries;
4670 		else
4671 			max_tries = max_probe_tries;
4672 
4673 		/* ACK received for nullfunc probing frame */
4674 		if (!ifmgd->probe_send_count)
4675 			ieee80211_reset_ap_probe(sdata);
4676 		else if (ifmgd->nullfunc_failed) {
4677 			if (ifmgd->probe_send_count < max_tries) {
4678 				mlme_dbg(sdata,
4679 					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4680 					 bssid, ifmgd->probe_send_count,
4681 					 max_tries);
4682 				ieee80211_mgd_probe_ap_send(sdata);
4683 			} else {
4684 				mlme_dbg(sdata,
4685 					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4686 					 bssid);
4687 				ieee80211_sta_connection_lost(sdata, bssid,
4688 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4689 					false);
4690 			}
4691 		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
4692 			run_again(sdata, ifmgd->probe_timeout);
4693 		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4694 			mlme_dbg(sdata,
4695 				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4696 				 bssid, probe_wait_ms);
4697 			ieee80211_sta_connection_lost(sdata, bssid,
4698 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4699 		} else if (ifmgd->probe_send_count < max_tries) {
4700 			mlme_dbg(sdata,
4701 				 "No probe response from AP %pM after %dms, try %d/%i\n",
4702 				 bssid, probe_wait_ms,
4703 				 ifmgd->probe_send_count, max_tries);
4704 			ieee80211_mgd_probe_ap_send(sdata);
4705 		} else {
4706 			/*
4707 			 * We actually lost the connection ... or did we?
4708 			 * Let's make sure!
4709 			 */
4710 			mlme_dbg(sdata,
4711 				 "No probe response from AP %pM after %dms, disconnecting.\n",
4712 				 bssid, probe_wait_ms);
4713 
4714 			ieee80211_sta_connection_lost(sdata, bssid,
4715 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4716 		}
4717 	}
4718 
4719 	sdata_unlock(sdata);
4720 }
4721 
4722 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
4723 {
4724 	struct ieee80211_sub_if_data *sdata =
4725 		from_timer(sdata, t, u.mgd.bcn_mon_timer);
4726 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4727 
4728 	if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4729 		return;
4730 
4731 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
4732 		return;
4733 
4734 	sdata->u.mgd.connection_loss = false;
4735 	ieee80211_queue_work(&sdata->local->hw,
4736 			     &sdata->u.mgd.beacon_connection_loss_work);
4737 }
4738 
4739 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
4740 {
4741 	struct ieee80211_sub_if_data *sdata =
4742 		from_timer(sdata, t, u.mgd.conn_mon_timer);
4743 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4744 	struct ieee80211_local *local = sdata->local;
4745 	struct sta_info *sta;
4746 	unsigned long timeout;
4747 
4748 	if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4749 		return;
4750 
4751 	sta = sta_info_get(sdata, ifmgd->bssid);
4752 	if (!sta)
4753 		return;
4754 
4755 	timeout = sta->status_stats.last_ack;
4756 	if (time_before(sta->status_stats.last_ack, sta->rx_stats.last_rx))
4757 		timeout = sta->rx_stats.last_rx;
4758 	timeout += IEEE80211_CONNECTION_IDLE_TIME;
4759 
4760 	/* If timeout is after now, then update timer to fire at
4761 	 * the later date, but do not actually probe at this time.
4762 	 */
4763 	if (time_is_after_jiffies(timeout)) {
4764 		mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
4765 		return;
4766 	}
4767 
4768 	ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
4769 }
4770 
4771 static void ieee80211_sta_monitor_work(struct work_struct *work)
4772 {
4773 	struct ieee80211_sub_if_data *sdata =
4774 		container_of(work, struct ieee80211_sub_if_data,
4775 			     u.mgd.monitor_work);
4776 
4777 	ieee80211_mgd_probe_ap(sdata, false);
4778 }
4779 
4780 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4781 {
4782 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
4783 		__ieee80211_stop_poll(sdata);
4784 
4785 		/* let's probe the connection once */
4786 		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4787 			ieee80211_queue_work(&sdata->local->hw,
4788 					     &sdata->u.mgd.monitor_work);
4789 	}
4790 }
4791 
4792 #ifdef CONFIG_PM
4793 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4794 {
4795 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4796 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4797 
4798 	sdata_lock(sdata);
4799 
4800 	if (ifmgd->auth_data || ifmgd->assoc_data) {
4801 		const u8 *bssid = ifmgd->auth_data ?
4802 				ifmgd->auth_data->bss->bssid :
4803 				ifmgd->assoc_data->bss->bssid;
4804 
4805 		/*
4806 		 * If we are trying to authenticate / associate while suspending,
4807 		 * cfg80211 won't know and won't actually abort those attempts,
4808 		 * thus we need to do that ourselves.
4809 		 */
4810 		ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
4811 					       IEEE80211_STYPE_DEAUTH,
4812 					       WLAN_REASON_DEAUTH_LEAVING,
4813 					       false, frame_buf);
4814 		if (ifmgd->assoc_data)
4815 			ieee80211_destroy_assoc_data(sdata, false, true);
4816 		if (ifmgd->auth_data)
4817 			ieee80211_destroy_auth_data(sdata, false);
4818 		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4819 				      IEEE80211_DEAUTH_FRAME_LEN,
4820 				      false);
4821 	}
4822 
4823 	/* This is a bit of a hack - we should find a better and more generic
4824 	 * solution to this. Normally when suspending, cfg80211 will in fact
4825 	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4826 	 * auth (not so important) or assoc (this is the problem) process.
4827 	 *
4828 	 * As a consequence, it can happen that we are in the process of both
4829 	 * associating and suspending, and receive an association response
4830 	 * after cfg80211 has checked if it needs to disconnect, but before
4831 	 * we actually set the flag to drop incoming frames. This will then
4832 	 * cause the workqueue flush to process the association response in
4833 	 * the suspend, resulting in a successful association just before it
4834 	 * tries to remove the interface from the driver, which now though
4835 	 * has a channel context assigned ... this results in issues.
4836 	 *
4837 	 * To work around this (for now) simply deauth here again if we're
4838 	 * now connected.
4839 	 */
4840 	if (ifmgd->associated && !sdata->local->wowlan) {
4841 		u8 bssid[ETH_ALEN];
4842 		struct cfg80211_deauth_request req = {
4843 			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
4844 			.bssid = bssid,
4845 		};
4846 
4847 		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4848 		ieee80211_mgd_deauth(sdata, &req);
4849 	}
4850 
4851 	sdata_unlock(sdata);
4852 }
4853 
4854 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4855 {
4856 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4857 
4858 	sdata_lock(sdata);
4859 	if (!ifmgd->associated) {
4860 		sdata_unlock(sdata);
4861 		return;
4862 	}
4863 
4864 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4865 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4866 		mlme_dbg(sdata, "driver requested disconnect after resume\n");
4867 		ieee80211_sta_connection_lost(sdata,
4868 					      ifmgd->associated->bssid,
4869 					      WLAN_REASON_UNSPECIFIED,
4870 					      true);
4871 		sdata_unlock(sdata);
4872 		return;
4873 	}
4874 	sdata_unlock(sdata);
4875 }
4876 #endif
4877 
4878 /* interface setup */
4879 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4880 {
4881 	struct ieee80211_if_managed *ifmgd;
4882 
4883 	ifmgd = &sdata->u.mgd;
4884 	INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
4885 	INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
4886 	INIT_WORK(&ifmgd->beacon_connection_loss_work,
4887 		  ieee80211_beacon_connection_loss_work);
4888 	INIT_WORK(&ifmgd->csa_connection_drop_work,
4889 		  ieee80211_csa_connection_drop_work);
4890 	INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
4891 	INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4892 			  ieee80211_tdls_peer_del_work);
4893 	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4894 	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4895 	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4896 	timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
4897 	INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4898 			  ieee80211_sta_handle_tspec_ac_params_wk);
4899 
4900 	ifmgd->flags = 0;
4901 	ifmgd->powersave = sdata->wdev.ps;
4902 	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4903 	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
4904 	ifmgd->p2p_noa_index = -1;
4905 
4906 	if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
4907 		ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4908 	else
4909 		ifmgd->req_smps = IEEE80211_SMPS_OFF;
4910 
4911 	/* Setup TDLS data */
4912 	spin_lock_init(&ifmgd->teardown_lock);
4913 	ifmgd->teardown_skb = NULL;
4914 	ifmgd->orig_teardown_skb = NULL;
4915 }
4916 
4917 /* scan finished notification */
4918 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4919 {
4920 	struct ieee80211_sub_if_data *sdata;
4921 
4922 	/* Restart STA timers */
4923 	rcu_read_lock();
4924 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4925 		if (ieee80211_sdata_running(sdata))
4926 			ieee80211_restart_sta_timer(sdata);
4927 	}
4928 	rcu_read_unlock();
4929 }
4930 
4931 static u8 ieee80211_max_rx_chains(struct ieee80211_sub_if_data *sdata,
4932 				  struct cfg80211_bss *cbss)
4933 {
4934 	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
4935 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4936 	const struct element *ht_cap_elem, *vht_cap_elem;
4937 	const struct cfg80211_bss_ies *ies;
4938 	const struct ieee80211_ht_cap *ht_cap;
4939 	const struct ieee80211_vht_cap *vht_cap;
4940 	const struct ieee80211_he_cap_elem *he_cap;
4941 	const struct element *he_cap_elem;
4942 	u16 mcs_80_map, mcs_160_map;
4943 	int i, mcs_nss_size;
4944 	bool support_160;
4945 	u8 chains = 1;
4946 
4947 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4948 		return chains;
4949 
4950 	ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
4951 	if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
4952 		ht_cap = (void *)ht_cap_elem->data;
4953 		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4954 		/*
4955 		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4956 		 *	 "Tx Unequal Modulation Supported" fields.
4957 		 */
4958 	}
4959 
4960 	if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4961 		return chains;
4962 
4963 	vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
4964 	if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
4965 		u8 nss;
4966 		u16 tx_mcs_map;
4967 
4968 		vht_cap = (void *)vht_cap_elem->data;
4969 		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4970 		for (nss = 8; nss > 0; nss--) {
4971 			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4972 					IEEE80211_VHT_MCS_NOT_SUPPORTED)
4973 				break;
4974 		}
4975 		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4976 		chains = max(chains, nss);
4977 	}
4978 
4979 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HE)
4980 		return chains;
4981 
4982 	ies = rcu_dereference(cbss->ies);
4983 	he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
4984 					     ies->data, ies->len);
4985 
4986 	if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap))
4987 		return chains;
4988 
4989 	/* skip one byte ext_tag_id */
4990 	he_cap = (void *)(he_cap_elem->data + 1);
4991 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
4992 
4993 	/* invalid HE IE */
4994 	if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap))
4995 		return chains;
4996 
4997 	/* mcs_nss is right after he_cap info */
4998 	he_mcs_nss_supp = (void *)(he_cap + 1);
4999 
5000 	mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
5001 
5002 	for (i = 7; i >= 0; i--) {
5003 		u8 mcs_80 = mcs_80_map >> (2 * i) & 3;
5004 
5005 		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
5006 			chains = max_t(u8, chains, i + 1);
5007 			break;
5008 		}
5009 	}
5010 
5011 	support_160 = he_cap->phy_cap_info[0] &
5012 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
5013 
5014 	if (!support_160)
5015 		return chains;
5016 
5017 	mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160);
5018 	for (i = 7; i >= 0; i--) {
5019 		u8 mcs_160 = mcs_160_map >> (2 * i) & 3;
5020 
5021 		if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
5022 			chains = max_t(u8, chains, i + 1);
5023 			break;
5024 		}
5025 	}
5026 
5027 	return chains;
5028 }
5029 
5030 static bool
5031 ieee80211_verify_peer_he_mcs_support(struct ieee80211_sub_if_data *sdata,
5032 				     const struct cfg80211_bss_ies *ies,
5033 				     const struct ieee80211_he_operation *he_op)
5034 {
5035 	const struct element *he_cap_elem;
5036 	const struct ieee80211_he_cap_elem *he_cap;
5037 	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
5038 	u16 mcs_80_map_tx, mcs_80_map_rx;
5039 	u16 ap_min_req_set;
5040 	int mcs_nss_size;
5041 	int nss;
5042 
5043 	he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
5044 					     ies->data, ies->len);
5045 
5046 	/* invalid HE IE */
5047 	if (!he_cap_elem || he_cap_elem->datalen < 1 + sizeof(*he_cap)) {
5048 		sdata_info(sdata,
5049 			   "Invalid HE elem, Disable HE\n");
5050 		return false;
5051 	}
5052 
5053 	/* skip one byte ext_tag_id */
5054 	he_cap = (void *)(he_cap_elem->data + 1);
5055 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
5056 
5057 	/* invalid HE IE */
5058 	if (he_cap_elem->datalen < 1 + sizeof(*he_cap) + mcs_nss_size) {
5059 		sdata_info(sdata,
5060 			   "Invalid HE elem with nss size, Disable HE\n");
5061 		return false;
5062 	}
5063 
5064 	/* mcs_nss is right after he_cap info */
5065 	he_mcs_nss_supp = (void *)(he_cap + 1);
5066 
5067 	mcs_80_map_tx = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
5068 	mcs_80_map_rx = le16_to_cpu(he_mcs_nss_supp->rx_mcs_80);
5069 
5070 	/* P802.11-REVme/D0.3
5071 	 * 27.1.1 Introduction to the HE PHY
5072 	 * ...
5073 	 * An HE STA shall support the following features:
5074 	 * ...
5075 	 * Single spatial stream HE-MCSs 0 to 7 (transmit and receive) in all
5076 	 * supported channel widths for HE SU PPDUs
5077 	 */
5078 	if ((mcs_80_map_tx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED ||
5079 	    (mcs_80_map_rx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED) {
5080 		sdata_info(sdata,
5081 			   "Missing mandatory rates for 1 Nss, rx 0x%x, tx 0x%x, disable HE\n",
5082 			   mcs_80_map_tx, mcs_80_map_rx);
5083 		return false;
5084 	}
5085 
5086 	if (!he_op)
5087 		return true;
5088 
5089 	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
5090 
5091 	/* make sure the AP is consistent with itself
5092 	 *
5093 	 * P802.11-REVme/D0.3
5094 	 * 26.17.1 Basic HE BSS operation
5095 	 *
5096 	 * A STA that is operating in an HE BSS shall be able to receive and
5097 	 * transmit at each of the <HE-MCS, NSS> tuple values indicated by the
5098 	 * Basic HE-MCS And NSS Set field of the HE Operation parameter of the
5099 	 * MLME-START.request primitive and shall be able to receive at each of
5100 	 * the <HE-MCS, NSS> tuple values indicated by the Supported HE-MCS and
5101 	 * NSS Set field in the HE Capabilities parameter of the MLMESTART.request
5102 	 * primitive
5103 	 */
5104 	for (nss = 8; nss > 0; nss--) {
5105 		u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
5106 		u8 ap_rx_val;
5107 		u8 ap_tx_val;
5108 
5109 		if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
5110 			continue;
5111 
5112 		ap_rx_val = (mcs_80_map_rx >> (2 * (nss - 1))) & 3;
5113 		ap_tx_val = (mcs_80_map_tx >> (2 * (nss - 1))) & 3;
5114 
5115 		if (ap_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
5116 		    ap_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
5117 		    ap_rx_val < ap_op_val || ap_tx_val < ap_op_val) {
5118 			sdata_info(sdata,
5119 				   "Invalid rates for %d Nss, rx %d, tx %d oper %d, disable HE\n",
5120 				   nss, ap_rx_val, ap_rx_val, ap_op_val);
5121 			return false;
5122 		}
5123 	}
5124 
5125 	return true;
5126 }
5127 
5128 static bool
5129 ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata,
5130 				    struct ieee80211_supported_band *sband,
5131 				    const struct ieee80211_he_operation *he_op)
5132 {
5133 	const struct ieee80211_sta_he_cap *sta_he_cap =
5134 		ieee80211_get_he_iftype_cap(sband,
5135 					    ieee80211_vif_type_p2p(&sdata->vif));
5136 	u16 ap_min_req_set;
5137 	int i;
5138 
5139 	if (!sta_he_cap || !he_op)
5140 		return false;
5141 
5142 	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
5143 
5144 	/* Need to go over for 80MHz, 160MHz and for 80+80 */
5145 	for (i = 0; i < 3; i++) {
5146 		const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
5147 			&sta_he_cap->he_mcs_nss_supp;
5148 		u16 sta_mcs_map_rx =
5149 			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
5150 		u16 sta_mcs_map_tx =
5151 			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
5152 		u8 nss;
5153 		bool verified = true;
5154 
5155 		/*
5156 		 * For each band there is a maximum of 8 spatial streams
5157 		 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
5158 		 * of 2 bits per NSS (1-8), with the values defined in enum
5159 		 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
5160 		 * capabilities aren't less than the AP's minimum requirements
5161 		 * for this HE BSS per SS.
5162 		 * It is enough to find one such band that meets the reqs.
5163 		 */
5164 		for (nss = 8; nss > 0; nss--) {
5165 			u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
5166 			u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
5167 			u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
5168 
5169 			if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
5170 				continue;
5171 
5172 			/*
5173 			 * Make sure the HE AP doesn't require MCSs that aren't
5174 			 * supported by the client as required by spec
5175 			 *
5176 			 * P802.11-REVme/D0.3
5177 			 * 26.17.1 Basic HE BSS operation
5178 			 *
5179 			 * An HE STA shall not attempt to join * (MLME-JOIN.request primitive)
5180 			 * a BSS, unless it supports (i.e., is able to both transmit and
5181 			 * receive using) all of the <HE-MCS, NSS> tuples in the basic
5182 			 * HE-MCS and NSS set.
5183 			 */
5184 			if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
5185 			    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
5186 			    (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
5187 				verified = false;
5188 				break;
5189 			}
5190 		}
5191 
5192 		if (verified)
5193 			return true;
5194 	}
5195 
5196 	/* If here, STA doesn't meet AP's HE min requirements */
5197 	return false;
5198 }
5199 
5200 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
5201 				  struct cfg80211_bss *cbss)
5202 {
5203 	struct ieee80211_local *local = sdata->local;
5204 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5205 	const struct ieee80211_ht_cap *ht_cap = NULL;
5206 	const struct ieee80211_ht_operation *ht_oper = NULL;
5207 	const struct ieee80211_vht_operation *vht_oper = NULL;
5208 	const struct ieee80211_he_operation *he_oper = NULL;
5209 	const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
5210 	struct ieee80211_supported_band *sband;
5211 	struct cfg80211_chan_def chandef;
5212 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
5213 	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
5214 	struct ieee80211_bss *bss = (void *)cbss->priv;
5215 	struct ieee802_11_elems *elems;
5216 	const struct cfg80211_bss_ies *ies;
5217 	int ret;
5218 	u32 i;
5219 	bool have_80mhz;
5220 
5221 	rcu_read_lock();
5222 
5223 	ies = rcu_dereference(cbss->ies);
5224 	elems = ieee802_11_parse_elems(ies->data, ies->len, false,
5225 				       NULL, NULL);
5226 	if (!elems) {
5227 		rcu_read_unlock();
5228 		return -ENOMEM;
5229 	}
5230 
5231 	sband = local->hw.wiphy->bands[cbss->channel->band];
5232 
5233 	ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
5234 			  IEEE80211_STA_DISABLE_80P80MHZ |
5235 			  IEEE80211_STA_DISABLE_160MHZ);
5236 
5237 	/* disable HT/VHT/HE if we don't support them */
5238 	if (!sband->ht_cap.ht_supported && !is_6ghz) {
5239 		mlme_dbg(sdata, "HT not supported, disabling HT/VHT/HE\n");
5240 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5241 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5242 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5243 	}
5244 
5245 	if (!sband->vht_cap.vht_supported && is_5ghz) {
5246 		mlme_dbg(sdata, "VHT not supported, disabling VHT/HE\n");
5247 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5248 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5249 	}
5250 
5251 	if (!ieee80211_get_he_iftype_cap(sband,
5252 					 ieee80211_vif_type_p2p(&sdata->vif))) {
5253 		mlme_dbg(sdata, "HE not supported, disabling it\n");
5254 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5255 	}
5256 
5257 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
5258 		ht_oper = elems->ht_operation;
5259 		ht_cap = elems->ht_cap_elem;
5260 
5261 		if (!ht_cap) {
5262 			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5263 			ht_oper = NULL;
5264 		}
5265 	}
5266 
5267 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
5268 		vht_oper = elems->vht_operation;
5269 		if (vht_oper && !ht_oper) {
5270 			vht_oper = NULL;
5271 			sdata_info(sdata,
5272 				   "AP advertised VHT without HT, disabling HT/VHT/HE\n");
5273 			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5274 			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5275 			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5276 		}
5277 
5278 		if (!elems->vht_cap_elem) {
5279 			sdata_info(sdata,
5280 				   "bad VHT capabilities, disabling VHT\n");
5281 			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5282 			vht_oper = NULL;
5283 		}
5284 	}
5285 
5286 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
5287 		he_oper = elems->he_operation;
5288 
5289 		if (is_6ghz) {
5290 			struct ieee80211_bss_conf *bss_conf;
5291 			u8 i, j = 0;
5292 
5293 			bss_conf = &sdata->vif.bss_conf;
5294 
5295 			if (elems->pwr_constr_elem)
5296 				bss_conf->pwr_reduction = *elems->pwr_constr_elem;
5297 
5298 			BUILD_BUG_ON(ARRAY_SIZE(bss_conf->tx_pwr_env) !=
5299 				     ARRAY_SIZE(elems->tx_pwr_env));
5300 
5301 			for (i = 0; i < elems->tx_pwr_env_num; i++) {
5302 				if (elems->tx_pwr_env_len[i] >
5303 				    sizeof(bss_conf->tx_pwr_env[j]))
5304 					continue;
5305 
5306 				bss_conf->tx_pwr_env_num++;
5307 				memcpy(&bss_conf->tx_pwr_env[j], elems->tx_pwr_env[i],
5308 				       elems->tx_pwr_env_len[i]);
5309 				j++;
5310 			}
5311 		}
5312 
5313 		if (!ieee80211_verify_peer_he_mcs_support(sdata, ies, he_oper) ||
5314 		    !ieee80211_verify_sta_he_mcs_support(sdata, sband, he_oper))
5315 			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5316 	}
5317 
5318 	/* Allow VHT if at least one channel on the sband supports 80 MHz */
5319 	have_80mhz = false;
5320 	for (i = 0; i < sband->n_channels; i++) {
5321 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5322 						IEEE80211_CHAN_NO_80MHZ))
5323 			continue;
5324 
5325 		have_80mhz = true;
5326 		break;
5327 	}
5328 
5329 	if (!have_80mhz) {
5330 		sdata_info(sdata, "80 MHz not supported, disabling VHT\n");
5331 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5332 	}
5333 
5334 	if (sband->band == NL80211_BAND_S1GHZ) {
5335 		s1g_oper = elems->s1g_oper;
5336 		if (!s1g_oper)
5337 			sdata_info(sdata,
5338 				   "AP missing S1G operation element?\n");
5339 	}
5340 
5341 	ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
5342 						     cbss->channel,
5343 						     bss->vht_cap_info,
5344 						     ht_oper, vht_oper, he_oper,
5345 						     s1g_oper,
5346 						     &chandef, false);
5347 
5348 	sdata->needed_rx_chains = min(ieee80211_max_rx_chains(sdata, cbss),
5349 				      local->rx_chains);
5350 
5351 	rcu_read_unlock();
5352 	/* the element data was RCU protected so no longer valid anyway */
5353 	kfree(elems);
5354 	elems = NULL;
5355 
5356 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
5357 		sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
5358 		return -EINVAL;
5359 	}
5360 
5361 	/* will change later if needed */
5362 	sdata->smps_mode = IEEE80211_SMPS_OFF;
5363 
5364 	mutex_lock(&local->mtx);
5365 	/*
5366 	 * If this fails (possibly due to channel context sharing
5367 	 * on incompatible channels, e.g. 80+80 and 160 sharing the
5368 	 * same control channel) try to use a smaller bandwidth.
5369 	 */
5370 	ret = ieee80211_vif_use_channel(sdata, &chandef,
5371 					IEEE80211_CHANCTX_SHARED);
5372 
5373 	/* don't downgrade for 5 and 10 MHz channels, though. */
5374 	if (chandef.width == NL80211_CHAN_WIDTH_5 ||
5375 	    chandef.width == NL80211_CHAN_WIDTH_10)
5376 		goto out;
5377 
5378 	while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
5379 		ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
5380 		ret = ieee80211_vif_use_channel(sdata, &chandef,
5381 						IEEE80211_CHANCTX_SHARED);
5382 	}
5383  out:
5384 	mutex_unlock(&local->mtx);
5385 	return ret;
5386 }
5387 
5388 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5389 			       u8 *dtim_count, u8 *dtim_period)
5390 {
5391 	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5392 	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5393 					 ies->len);
5394 	const struct ieee80211_tim_ie *tim = NULL;
5395 	const struct ieee80211_bssid_index *idx;
5396 	bool valid = tim_ie && tim_ie[1] >= 2;
5397 
5398 	if (valid)
5399 		tim = (void *)(tim_ie + 2);
5400 
5401 	if (dtim_count)
5402 		*dtim_count = valid ? tim->dtim_count : 0;
5403 
5404 	if (dtim_period)
5405 		*dtim_period = valid ? tim->dtim_period : 0;
5406 
5407 	/* Check if value is overridden by non-transmitted profile */
5408 	if (!idx_ie || idx_ie[1] < 3)
5409 		return valid;
5410 
5411 	idx = (void *)(idx_ie + 2);
5412 
5413 	if (dtim_count)
5414 		*dtim_count = idx->dtim_count;
5415 
5416 	if (dtim_period)
5417 		*dtim_period = idx->dtim_period;
5418 
5419 	return true;
5420 }
5421 
5422 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
5423 				     struct cfg80211_bss *cbss, bool assoc,
5424 				     bool override)
5425 {
5426 	struct ieee80211_local *local = sdata->local;
5427 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5428 	struct ieee80211_bss *bss = (void *)cbss->priv;
5429 	struct sta_info *new_sta = NULL;
5430 	struct ieee80211_supported_band *sband;
5431 	bool have_sta = false;
5432 	int err;
5433 
5434 	sband = local->hw.wiphy->bands[cbss->channel->band];
5435 
5436 	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5437 		return -EINVAL;
5438 
5439 	/* If a reconfig is happening, bail out */
5440 	if (local->in_reconfig)
5441 		return -EBUSY;
5442 
5443 	if (assoc) {
5444 		rcu_read_lock();
5445 		have_sta = sta_info_get(sdata, cbss->bssid);
5446 		rcu_read_unlock();
5447 	}
5448 
5449 	if (!have_sta) {
5450 		new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5451 		if (!new_sta)
5452 			return -ENOMEM;
5453 	}
5454 
5455 	/*
5456 	 * Set up the information for the new channel before setting the
5457 	 * new channel. We can't - completely race-free - change the basic
5458 	 * rates bitmap and the channel (sband) that it refers to, but if
5459 	 * we set it up before we at least avoid calling into the driver's
5460 	 * bss_info_changed() method with invalid information (since we do
5461 	 * call that from changing the channel - only for IDLE and perhaps
5462 	 * some others, but ...).
5463 	 *
5464 	 * So to avoid that, just set up all the new information before the
5465 	 * channel, but tell the driver to apply it only afterwards, since
5466 	 * it might need the new channel for that.
5467 	 */
5468 	if (new_sta) {
5469 		u32 rates = 0, basic_rates = 0;
5470 		bool have_higher_than_11mbit = false;
5471 		int min_rate = INT_MAX, min_rate_index = -1;
5472 		const struct cfg80211_bss_ies *ies;
5473 		int shift = ieee80211_vif_get_shift(&sdata->vif);
5474 
5475 		/* TODO: S1G Basic Rate Set is expressed elsewhere */
5476 		if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5477 			ieee80211_s1g_sta_rate_init(new_sta);
5478 			goto skip_rates;
5479 		}
5480 
5481 		ieee80211_get_rates(sband, bss->supp_rates,
5482 				    bss->supp_rates_len,
5483 				    &rates, &basic_rates,
5484 				    &have_higher_than_11mbit,
5485 				    &min_rate, &min_rate_index,
5486 				    shift);
5487 
5488 		/*
5489 		 * This used to be a workaround for basic rates missing
5490 		 * in the association response frame. Now that we no
5491 		 * longer use the basic rates from there, it probably
5492 		 * doesn't happen any more, but keep the workaround so
5493 		 * in case some *other* APs are buggy in different ways
5494 		 * we can connect -- with a warning.
5495 		 * Allow this workaround only in case the AP provided at least
5496 		 * one rate.
5497 		 */
5498 		if (min_rate_index < 0) {
5499 			sdata_info(sdata,
5500 				   "No legacy rates in association response\n");
5501 
5502 			sta_info_free(local, new_sta);
5503 			return -EINVAL;
5504 		} else if (!basic_rates) {
5505 			sdata_info(sdata,
5506 				   "No basic rates, using min rate instead\n");
5507 			basic_rates = BIT(min_rate_index);
5508 		}
5509 
5510 		if (rates)
5511 			new_sta->sta.supp_rates[cbss->channel->band] = rates;
5512 		else
5513 			sdata_info(sdata,
5514 				   "No rates found, keeping mandatory only\n");
5515 
5516 		sdata->vif.bss_conf.basic_rates = basic_rates;
5517 
5518 		/* cf. IEEE 802.11 9.2.12 */
5519 		if (cbss->channel->band == NL80211_BAND_2GHZ &&
5520 		    have_higher_than_11mbit)
5521 			sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5522 		else
5523 			sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5524 
5525 skip_rates:
5526 		memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5527 
5528 		/* set timing information */
5529 		sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
5530 		rcu_read_lock();
5531 		ies = rcu_dereference(cbss->beacon_ies);
5532 		if (ies) {
5533 			sdata->vif.bss_conf.sync_tsf = ies->tsf;
5534 			sdata->vif.bss_conf.sync_device_ts =
5535 				bss->device_ts_beacon;
5536 
5537 			ieee80211_get_dtim(ies,
5538 					   &sdata->vif.bss_conf.sync_dtim_count,
5539 					   NULL);
5540 		} else if (!ieee80211_hw_check(&sdata->local->hw,
5541 					       TIMING_BEACON_ONLY)) {
5542 			ies = rcu_dereference(cbss->proberesp_ies);
5543 			/* must be non-NULL since beacon IEs were NULL */
5544 			sdata->vif.bss_conf.sync_tsf = ies->tsf;
5545 			sdata->vif.bss_conf.sync_device_ts =
5546 				bss->device_ts_presp;
5547 			sdata->vif.bss_conf.sync_dtim_count = 0;
5548 		} else {
5549 			sdata->vif.bss_conf.sync_tsf = 0;
5550 			sdata->vif.bss_conf.sync_device_ts = 0;
5551 			sdata->vif.bss_conf.sync_dtim_count = 0;
5552 		}
5553 		rcu_read_unlock();
5554 	}
5555 
5556 	if (new_sta || override) {
5557 		err = ieee80211_prep_channel(sdata, cbss);
5558 		if (err) {
5559 			if (new_sta)
5560 				sta_info_free(local, new_sta);
5561 			return -EINVAL;
5562 		}
5563 	}
5564 
5565 	if (new_sta) {
5566 		/*
5567 		 * tell driver about BSSID, basic rates and timing
5568 		 * this was set up above, before setting the channel
5569 		 */
5570 		ieee80211_bss_info_change_notify(sdata,
5571 			BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5572 			BSS_CHANGED_BEACON_INT);
5573 
5574 		if (assoc)
5575 			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
5576 
5577 		err = sta_info_insert(new_sta);
5578 		new_sta = NULL;
5579 		if (err) {
5580 			sdata_info(sdata,
5581 				   "failed to insert STA entry for the AP (error %d)\n",
5582 				   err);
5583 			return err;
5584 		}
5585 	} else
5586 		WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
5587 
5588 	/* Cancel scan to ensure that nothing interferes with connection */
5589 	if (local->scanning)
5590 		ieee80211_scan_cancel(local);
5591 
5592 	return 0;
5593 }
5594 
5595 /* config hooks */
5596 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5597 		       struct cfg80211_auth_request *req)
5598 {
5599 	struct ieee80211_local *local = sdata->local;
5600 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5601 	struct ieee80211_mgd_auth_data *auth_data;
5602 	u16 auth_alg;
5603 	int err;
5604 	bool cont_auth;
5605 
5606 	/* prepare auth data structure */
5607 
5608 	switch (req->auth_type) {
5609 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
5610 		auth_alg = WLAN_AUTH_OPEN;
5611 		break;
5612 	case NL80211_AUTHTYPE_SHARED_KEY:
5613 		if (fips_enabled)
5614 			return -EOPNOTSUPP;
5615 		auth_alg = WLAN_AUTH_SHARED_KEY;
5616 		break;
5617 	case NL80211_AUTHTYPE_FT:
5618 		auth_alg = WLAN_AUTH_FT;
5619 		break;
5620 	case NL80211_AUTHTYPE_NETWORK_EAP:
5621 		auth_alg = WLAN_AUTH_LEAP;
5622 		break;
5623 	case NL80211_AUTHTYPE_SAE:
5624 		auth_alg = WLAN_AUTH_SAE;
5625 		break;
5626 	case NL80211_AUTHTYPE_FILS_SK:
5627 		auth_alg = WLAN_AUTH_FILS_SK;
5628 		break;
5629 	case NL80211_AUTHTYPE_FILS_SK_PFS:
5630 		auth_alg = WLAN_AUTH_FILS_SK_PFS;
5631 		break;
5632 	case NL80211_AUTHTYPE_FILS_PK:
5633 		auth_alg = WLAN_AUTH_FILS_PK;
5634 		break;
5635 	default:
5636 		return -EOPNOTSUPP;
5637 	}
5638 
5639 	if (ifmgd->assoc_data)
5640 		return -EBUSY;
5641 
5642 	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
5643 			    req->ie_len, GFP_KERNEL);
5644 	if (!auth_data)
5645 		return -ENOMEM;
5646 
5647 	auth_data->bss = req->bss;
5648 
5649 	if (req->auth_data_len >= 4) {
5650 		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5651 			__le16 *pos = (__le16 *) req->auth_data;
5652 
5653 			auth_data->sae_trans = le16_to_cpu(pos[0]);
5654 			auth_data->sae_status = le16_to_cpu(pos[1]);
5655 		}
5656 		memcpy(auth_data->data, req->auth_data + 4,
5657 		       req->auth_data_len - 4);
5658 		auth_data->data_len += req->auth_data_len - 4;
5659 	}
5660 
5661 	/* Check if continuing authentication or trying to authenticate with the
5662 	 * same BSS that we were in the process of authenticating with and avoid
5663 	 * removal and re-addition of the STA entry in
5664 	 * ieee80211_prep_connection().
5665 	 */
5666 	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5667 
5668 	if (req->ie && req->ie_len) {
5669 		memcpy(&auth_data->data[auth_data->data_len],
5670 		       req->ie, req->ie_len);
5671 		auth_data->data_len += req->ie_len;
5672 	}
5673 
5674 	if (req->key && req->key_len) {
5675 		auth_data->key_len = req->key_len;
5676 		auth_data->key_idx = req->key_idx;
5677 		memcpy(auth_data->key, req->key, req->key_len);
5678 	}
5679 
5680 	auth_data->algorithm = auth_alg;
5681 
5682 	/* try to authenticate/probe */
5683 
5684 	if (ifmgd->auth_data) {
5685 		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5686 			auth_data->peer_confirmed =
5687 				ifmgd->auth_data->peer_confirmed;
5688 		}
5689 		ieee80211_destroy_auth_data(sdata, cont_auth);
5690 	}
5691 
5692 	/* prep auth_data so we don't go into idle on disassoc */
5693 	ifmgd->auth_data = auth_data;
5694 
5695 	/* If this is continuation of an ongoing SAE authentication exchange
5696 	 * (i.e., request to send SAE Confirm) and the peer has already
5697 	 * confirmed, mark authentication completed since we are about to send
5698 	 * out SAE Confirm.
5699 	 */
5700 	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5701 	    auth_data->peer_confirmed && auth_data->sae_trans == 2)
5702 		ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5703 
5704 	if (ifmgd->associated) {
5705 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5706 
5707 		sdata_info(sdata,
5708 			   "disconnect from AP %pM for new auth to %pM\n",
5709 			   ifmgd->associated->bssid, req->bss->bssid);
5710 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5711 				       WLAN_REASON_UNSPECIFIED,
5712 				       false, frame_buf);
5713 
5714 		ieee80211_report_disconnect(sdata, frame_buf,
5715 					    sizeof(frame_buf), true,
5716 					    WLAN_REASON_UNSPECIFIED,
5717 					    false);
5718 	}
5719 
5720 	sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
5721 
5722 	err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
5723 	if (err)
5724 		goto err_clear;
5725 
5726 	err = ieee80211_auth(sdata);
5727 	if (err) {
5728 		sta_info_destroy_addr(sdata, req->bss->bssid);
5729 		goto err_clear;
5730 	}
5731 
5732 	/* hold our own reference */
5733 	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
5734 	return 0;
5735 
5736  err_clear:
5737 	eth_zero_addr(ifmgd->bssid);
5738 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5739 	ifmgd->auth_data = NULL;
5740 	mutex_lock(&sdata->local->mtx);
5741 	ieee80211_vif_release_channel(sdata);
5742 	mutex_unlock(&sdata->local->mtx);
5743 	kfree(auth_data);
5744 	return err;
5745 }
5746 
5747 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5748 			struct cfg80211_assoc_request *req)
5749 {
5750 	bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
5751 	bool is_5ghz = req->bss->channel->band == NL80211_BAND_5GHZ;
5752 	struct ieee80211_local *local = sdata->local;
5753 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5754 	struct ieee80211_bss *bss = (void *)req->bss->priv;
5755 	struct ieee80211_mgd_assoc_data *assoc_data;
5756 	const struct cfg80211_bss_ies *beacon_ies;
5757 	struct ieee80211_supported_band *sband;
5758 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
5759 	const struct element *ssid_elem, *ht_elem, *vht_elem;
5760 	int i, err;
5761 	bool override = false;
5762 
5763 	assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5764 	if (!assoc_data)
5765 		return -ENOMEM;
5766 
5767 	rcu_read_lock();
5768 	ssid_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_SSID);
5769 	if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
5770 		rcu_read_unlock();
5771 		kfree(assoc_data);
5772 		return -EINVAL;
5773 	}
5774 	memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
5775 	assoc_data->ssid_len = ssid_elem->datalen;
5776 	memcpy(bss_conf->ssid, assoc_data->ssid, assoc_data->ssid_len);
5777 	bss_conf->ssid_len = assoc_data->ssid_len;
5778 	rcu_read_unlock();
5779 
5780 	if (ifmgd->associated) {
5781 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5782 
5783 		sdata_info(sdata,
5784 			   "disconnect from AP %pM for new assoc to %pM\n",
5785 			   ifmgd->associated->bssid, req->bss->bssid);
5786 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5787 				       WLAN_REASON_UNSPECIFIED,
5788 				       false, frame_buf);
5789 
5790 		ieee80211_report_disconnect(sdata, frame_buf,
5791 					    sizeof(frame_buf), true,
5792 					    WLAN_REASON_UNSPECIFIED,
5793 					    false);
5794 	}
5795 
5796 	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5797 		err = -EBUSY;
5798 		goto err_free;
5799 	}
5800 
5801 	if (ifmgd->assoc_data) {
5802 		err = -EBUSY;
5803 		goto err_free;
5804 	}
5805 
5806 	if (ifmgd->auth_data) {
5807 		bool match;
5808 
5809 		/* keep sta info, bssid if matching */
5810 		match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
5811 		ieee80211_destroy_auth_data(sdata, match);
5812 	}
5813 
5814 	/* prepare assoc data */
5815 
5816 	ifmgd->beacon_crc_valid = false;
5817 
5818 	assoc_data->wmm = bss->wmm_used &&
5819 			  (local->hw.queues >= IEEE80211_NUM_ACS);
5820 
5821 	/*
5822 	 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5823 	 * We still associate in non-HT mode (11a/b/g) if any one of these
5824 	 * ciphers is configured as pairwise.
5825 	 * We can set this to true for non-11n hardware, that'll be checked
5826 	 * separately along with the peer capabilities.
5827 	 */
5828 	for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5829 		if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5830 		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5831 		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5832 			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5833 			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5834 			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5835 			netdev_info(sdata->dev,
5836 				    "disabling HT/VHT/HE due to WEP/TKIP use\n");
5837 		}
5838 	}
5839 
5840 	sband = local->hw.wiphy->bands[req->bss->channel->band];
5841 
5842 	/* also disable HT/VHT/HE if the AP doesn't use WMM */
5843 	if (!bss->wmm_used) {
5844 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5845 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5846 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5847 		netdev_info(sdata->dev,
5848 			    "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
5849 	}
5850 
5851 	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5852 	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5853 	       sizeof(ifmgd->ht_capa_mask));
5854 
5855 	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5856 	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5857 	       sizeof(ifmgd->vht_capa_mask));
5858 
5859 	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
5860 	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
5861 	       sizeof(ifmgd->s1g_capa_mask));
5862 
5863 	if (req->ie && req->ie_len) {
5864 		memcpy(assoc_data->ie, req->ie, req->ie_len);
5865 		assoc_data->ie_len = req->ie_len;
5866 	}
5867 
5868 	if (req->fils_kek) {
5869 		/* should already be checked in cfg80211 - so warn */
5870 		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5871 			err = -EINVAL;
5872 			goto err_free;
5873 		}
5874 		memcpy(assoc_data->fils_kek, req->fils_kek,
5875 		       req->fils_kek_len);
5876 		assoc_data->fils_kek_len = req->fils_kek_len;
5877 	}
5878 
5879 	if (req->fils_nonces)
5880 		memcpy(assoc_data->fils_nonces, req->fils_nonces,
5881 		       2 * FILS_NONCE_LEN);
5882 
5883 	assoc_data->bss = req->bss;
5884 	assoc_data->capability = req->bss->capability;
5885 	assoc_data->supp_rates = bss->supp_rates;
5886 	assoc_data->supp_rates_len = bss->supp_rates_len;
5887 
5888 	rcu_read_lock();
5889 	ht_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_HT_OPERATION);
5890 	if (ht_elem && ht_elem->datalen >= sizeof(struct ieee80211_ht_operation))
5891 		assoc_data->ap_ht_param =
5892 			((struct ieee80211_ht_operation *)(ht_elem->data))->ht_param;
5893 	else if (!is_6ghz)
5894 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5895 	vht_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_VHT_CAPABILITY);
5896 	if (vht_elem && vht_elem->datalen >= sizeof(struct ieee80211_vht_cap)) {
5897 		memcpy(&assoc_data->ap_vht_cap, vht_elem->data,
5898 		       sizeof(struct ieee80211_vht_cap));
5899 	} else if (is_5ghz) {
5900 		sdata_info(sdata, "VHT capa missing/short, disabling VHT/HE\n");
5901 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5902 				IEEE80211_STA_DISABLE_HE;
5903 	}
5904 	rcu_read_unlock();
5905 
5906 	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
5907 		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
5908 	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5909 		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5910 
5911 	if (bss->wmm_used && bss->uapsd_supported &&
5912 	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
5913 		assoc_data->uapsd = true;
5914 		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5915 	} else {
5916 		assoc_data->uapsd = false;
5917 		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5918 	}
5919 
5920 	if (req->prev_bssid)
5921 		memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
5922 
5923 	if (req->use_mfp) {
5924 		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5925 		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5926 	} else {
5927 		ifmgd->mfp = IEEE80211_MFP_DISABLED;
5928 		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5929 	}
5930 
5931 	if (req->flags & ASSOC_REQ_USE_RRM)
5932 		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5933 	else
5934 		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5935 
5936 	if (req->crypto.control_port)
5937 		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5938 	else
5939 		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5940 
5941 	sdata->control_port_protocol = req->crypto.control_port_ethertype;
5942 	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
5943 	sdata->control_port_over_nl80211 =
5944 					req->crypto.control_port_over_nl80211;
5945 	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
5946 	sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5947 							sdata->vif.type);
5948 
5949 	/* kick off associate process */
5950 
5951 	ifmgd->assoc_data = assoc_data;
5952 	ifmgd->dtim_period = 0;
5953 	ifmgd->have_beacon = false;
5954 
5955 	/* override HT/VHT configuration only if the AP and we support it */
5956 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5957 		struct ieee80211_sta_ht_cap sta_ht_cap;
5958 
5959 		if (req->flags & ASSOC_REQ_DISABLE_HT)
5960 			override = true;
5961 
5962 		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5963 		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5964 
5965 		/* check for 40 MHz disable override */
5966 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5967 		    sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5968 		    !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5969 			override = true;
5970 
5971 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5972 		    req->flags & ASSOC_REQ_DISABLE_VHT)
5973 			override = true;
5974 	}
5975 
5976 	if (req->flags & ASSOC_REQ_DISABLE_HT) {
5977 		mlme_dbg(sdata, "HT disabled by flag, disabling HT/VHT/HE\n");
5978 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5979 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5980 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5981 	}
5982 
5983 	if (req->flags & ASSOC_REQ_DISABLE_VHT) {
5984 		mlme_dbg(sdata, "VHT disabled by flag, disabling VHT\n");
5985 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5986 	}
5987 
5988 	if (req->flags & ASSOC_REQ_DISABLE_HE) {
5989 		mlme_dbg(sdata, "HE disabled by flag, disabling VHT\n");
5990 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5991 	}
5992 
5993 	err = ieee80211_prep_connection(sdata, req->bss, true, override);
5994 	if (err)
5995 		goto err_clear;
5996 
5997 	if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5998 		if (ifmgd->powersave)
5999 			sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
6000 		else
6001 			sdata->smps_mode = IEEE80211_SMPS_OFF;
6002 	} else {
6003 		sdata->smps_mode = ifmgd->req_smps;
6004 	}
6005 
6006 	rcu_read_lock();
6007 	beacon_ies = rcu_dereference(req->bss->beacon_ies);
6008 
6009 	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
6010 	    !beacon_ies) {
6011 		/*
6012 		 * Wait up to one beacon interval ...
6013 		 * should this be more if we miss one?
6014 		 */
6015 		sdata_info(sdata, "waiting for beacon from %pM\n",
6016 			   ifmgd->bssid);
6017 		assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
6018 		assoc_data->timeout_started = true;
6019 		assoc_data->need_beacon = true;
6020 	} else if (beacon_ies) {
6021 		const struct element *elem;
6022 		u8 dtim_count = 0;
6023 
6024 		ieee80211_get_dtim(beacon_ies, &dtim_count,
6025 				   &ifmgd->dtim_period);
6026 
6027 		ifmgd->have_beacon = true;
6028 		assoc_data->timeout = jiffies;
6029 		assoc_data->timeout_started = true;
6030 
6031 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
6032 			sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
6033 			sdata->vif.bss_conf.sync_device_ts =
6034 				bss->device_ts_beacon;
6035 			sdata->vif.bss_conf.sync_dtim_count = dtim_count;
6036 		}
6037 
6038 		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
6039 					      beacon_ies->data, beacon_ies->len);
6040 		if (elem && elem->datalen >= 3)
6041 			sdata->vif.bss_conf.profile_periodicity = elem->data[2];
6042 		else
6043 			sdata->vif.bss_conf.profile_periodicity = 0;
6044 
6045 		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
6046 					  beacon_ies->data, beacon_ies->len);
6047 		if (elem && elem->datalen >= 11 &&
6048 		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
6049 			sdata->vif.bss_conf.ema_ap = true;
6050 		else
6051 			sdata->vif.bss_conf.ema_ap = false;
6052 	} else {
6053 		assoc_data->timeout = jiffies;
6054 		assoc_data->timeout_started = true;
6055 	}
6056 	rcu_read_unlock();
6057 
6058 	run_again(sdata, assoc_data->timeout);
6059 
6060 	if (bss->corrupt_data) {
6061 		char *corrupt_type = "data";
6062 		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
6063 			if (bss->corrupt_data &
6064 					IEEE80211_BSS_CORRUPT_PROBE_RESP)
6065 				corrupt_type = "beacon and probe response";
6066 			else
6067 				corrupt_type = "beacon";
6068 		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
6069 			corrupt_type = "probe response";
6070 		sdata_info(sdata, "associating with AP with corrupt %s\n",
6071 			   corrupt_type);
6072 	}
6073 
6074 	return 0;
6075  err_clear:
6076 	eth_zero_addr(ifmgd->bssid);
6077 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
6078 	ifmgd->assoc_data = NULL;
6079  err_free:
6080 	kfree(assoc_data);
6081 	return err;
6082 }
6083 
6084 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
6085 			 struct cfg80211_deauth_request *req)
6086 {
6087 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6088 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6089 	bool tx = !req->local_state_change;
6090 	struct ieee80211_prep_tx_info info = {
6091 		.subtype = IEEE80211_STYPE_DEAUTH,
6092 	};
6093 
6094 	if (ifmgd->auth_data &&
6095 	    ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
6096 		sdata_info(sdata,
6097 			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
6098 			   req->bssid, req->reason_code,
6099 			   ieee80211_get_reason_code_string(req->reason_code));
6100 
6101 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
6102 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
6103 					       IEEE80211_STYPE_DEAUTH,
6104 					       req->reason_code, tx,
6105 					       frame_buf);
6106 		ieee80211_destroy_auth_data(sdata, false);
6107 		ieee80211_report_disconnect(sdata, frame_buf,
6108 					    sizeof(frame_buf), true,
6109 					    req->reason_code, false);
6110 		drv_mgd_complete_tx(sdata->local, sdata, &info);
6111 		return 0;
6112 	}
6113 
6114 	if (ifmgd->assoc_data &&
6115 	    ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
6116 		sdata_info(sdata,
6117 			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
6118 			   req->bssid, req->reason_code,
6119 			   ieee80211_get_reason_code_string(req->reason_code));
6120 
6121 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
6122 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
6123 					       IEEE80211_STYPE_DEAUTH,
6124 					       req->reason_code, tx,
6125 					       frame_buf);
6126 		ieee80211_destroy_assoc_data(sdata, false, true);
6127 		ieee80211_report_disconnect(sdata, frame_buf,
6128 					    sizeof(frame_buf), true,
6129 					    req->reason_code, false);
6130 		return 0;
6131 	}
6132 
6133 	if (ifmgd->associated &&
6134 	    ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
6135 		sdata_info(sdata,
6136 			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
6137 			   req->bssid, req->reason_code,
6138 			   ieee80211_get_reason_code_string(req->reason_code));
6139 
6140 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
6141 				       req->reason_code, tx, frame_buf);
6142 		ieee80211_report_disconnect(sdata, frame_buf,
6143 					    sizeof(frame_buf), true,
6144 					    req->reason_code, false);
6145 		drv_mgd_complete_tx(sdata->local, sdata, &info);
6146 		return 0;
6147 	}
6148 
6149 	return -ENOTCONN;
6150 }
6151 
6152 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
6153 			   struct cfg80211_disassoc_request *req)
6154 {
6155 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6156 	u8 bssid[ETH_ALEN];
6157 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6158 
6159 	/*
6160 	 * cfg80211 should catch this ... but it's racy since
6161 	 * we can receive a disassoc frame, process it, hand it
6162 	 * to cfg80211 while that's in a locked section already
6163 	 * trying to tell us that the user wants to disconnect.
6164 	 */
6165 	if (ifmgd->associated != req->bss)
6166 		return -ENOLINK;
6167 
6168 	sdata_info(sdata,
6169 		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
6170 		   req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
6171 
6172 	memcpy(bssid, req->bss->bssid, ETH_ALEN);
6173 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
6174 			       req->reason_code, !req->local_state_change,
6175 			       frame_buf);
6176 
6177 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
6178 				    req->reason_code, false);
6179 
6180 	return 0;
6181 }
6182 
6183 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
6184 {
6185 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6186 
6187 	/*
6188 	 * Make sure some work items will not run after this,
6189 	 * they will not do anything but might not have been
6190 	 * cancelled when disconnecting.
6191 	 */
6192 	cancel_work_sync(&ifmgd->monitor_work);
6193 	cancel_work_sync(&ifmgd->beacon_connection_loss_work);
6194 	cancel_work_sync(&ifmgd->request_smps_work);
6195 	cancel_work_sync(&ifmgd->csa_connection_drop_work);
6196 	cancel_work_sync(&ifmgd->chswitch_work);
6197 	cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
6198 
6199 	sdata_lock(sdata);
6200 	if (ifmgd->assoc_data) {
6201 		struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
6202 		ieee80211_destroy_assoc_data(sdata, false, false);
6203 		cfg80211_assoc_timeout(sdata->dev, bss);
6204 	}
6205 	if (ifmgd->auth_data)
6206 		ieee80211_destroy_auth_data(sdata, false);
6207 	spin_lock_bh(&ifmgd->teardown_lock);
6208 	if (ifmgd->teardown_skb) {
6209 		kfree_skb(ifmgd->teardown_skb);
6210 		ifmgd->teardown_skb = NULL;
6211 		ifmgd->orig_teardown_skb = NULL;
6212 	}
6213 	kfree(ifmgd->assoc_req_ies);
6214 	ifmgd->assoc_req_ies = NULL;
6215 	ifmgd->assoc_req_ies_len = 0;
6216 	spin_unlock_bh(&ifmgd->teardown_lock);
6217 	del_timer_sync(&ifmgd->timer);
6218 	sdata_unlock(sdata);
6219 }
6220 
6221 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
6222 			       enum nl80211_cqm_rssi_threshold_event rssi_event,
6223 			       s32 rssi_level,
6224 			       gfp_t gfp)
6225 {
6226 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6227 
6228 	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
6229 
6230 	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
6231 }
6232 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
6233 
6234 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
6235 {
6236 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6237 
6238 	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
6239 
6240 	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
6241 }
6242 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
6243