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