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