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