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