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