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