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