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