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