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