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