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