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