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