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