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