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