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