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