• 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;
2497 	int ac;
2498 	struct ieee80211_sta_tx_tspec *tx_tspec;
2499 	unsigned long now = jiffies;
2500 
2501 	if (!ieee80211_is_data_qos(hdr->frame_control))
2502 		return;
2503 
2504 	tid = ieee80211_get_tid(hdr);
2505 	ac = ieee80211_ac_from_tid(tid);
2506 	tx_tspec = &ifmgd->tx_tspec[ac];
2507 
2508 	if (likely(!tx_tspec->admitted_time))
2509 		return;
2510 
2511 	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2512 		tx_tspec->consumed_tx_time = 0;
2513 		tx_tspec->time_slice_start = now;
2514 
2515 		if (tx_tspec->downgraded) {
2516 			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2517 			schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2518 		}
2519 	}
2520 
2521 	if (tx_tspec->downgraded)
2522 		return;
2523 
2524 	tx_tspec->consumed_tx_time += tx_time;
2525 
2526 	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2527 		tx_tspec->downgraded = true;
2528 		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2529 		schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2530 	}
2531 }
2532 
ieee80211_sta_tx_notify(struct ieee80211_sub_if_data * sdata,struct ieee80211_hdr * hdr,bool ack,u16 tx_time)2533 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2534 			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2535 {
2536 	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2537 
2538 	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
2539 	    !sdata->u.mgd.probe_send_count)
2540 		return;
2541 
2542 	if (ack)
2543 		sdata->u.mgd.probe_send_count = 0;
2544 	else
2545 		sdata->u.mgd.nullfunc_failed = true;
2546 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2547 }
2548 
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)2549 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2550 					  const u8 *src, const u8 *dst,
2551 					  const u8 *ssid, size_t ssid_len,
2552 					  struct ieee80211_channel *channel)
2553 {
2554 	struct sk_buff *skb;
2555 
2556 	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2557 					ssid, ssid_len, NULL, 0,
2558 					IEEE80211_PROBE_FLAG_DIRECTED);
2559 	if (skb)
2560 		ieee80211_tx_skb(sdata, skb);
2561 }
2562 
ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data * sdata)2563 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2564 {
2565 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2566 	const u8 *ssid;
2567 	u8 *dst = ifmgd->associated->bssid;
2568 	u8 unicast_limit = max(1, max_probe_tries - 3);
2569 	struct sta_info *sta;
2570 
2571 	/*
2572 	 * Try sending broadcast probe requests for the last three
2573 	 * probe requests after the first ones failed since some
2574 	 * buggy APs only support broadcast probe requests.
2575 	 */
2576 	if (ifmgd->probe_send_count >= unicast_limit)
2577 		dst = NULL;
2578 
2579 	/*
2580 	 * When the hardware reports an accurate Tx ACK status, it's
2581 	 * better to send a nullfunc frame instead of a probe request,
2582 	 * as it will kick us off the AP quickly if we aren't associated
2583 	 * anymore. The timeout will be reset if the frame is ACKed by
2584 	 * the AP.
2585 	 */
2586 	ifmgd->probe_send_count++;
2587 
2588 	if (dst) {
2589 		mutex_lock(&sdata->local->sta_mtx);
2590 		sta = sta_info_get(sdata, dst);
2591 		if (!WARN_ON(!sta))
2592 			ieee80211_check_fast_rx(sta);
2593 		mutex_unlock(&sdata->local->sta_mtx);
2594 	}
2595 
2596 	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2597 		ifmgd->nullfunc_failed = false;
2598 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2599 			ifmgd->probe_send_count--;
2600 		else
2601 			ieee80211_send_nullfunc(sdata->local, sdata, false);
2602 	} else {
2603 		int ssid_len;
2604 
2605 		rcu_read_lock();
2606 		ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2607 		if (WARN_ON_ONCE(ssid == NULL))
2608 			ssid_len = 0;
2609 		else
2610 			ssid_len = ssid[1];
2611 
2612 		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2613 					      ssid + 2, ssid_len,
2614 					      ifmgd->associated->channel);
2615 		rcu_read_unlock();
2616 	}
2617 
2618 	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2619 	run_again(sdata, ifmgd->probe_timeout);
2620 }
2621 
ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data * sdata,bool beacon)2622 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2623 				   bool beacon)
2624 {
2625 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2626 	bool already = false;
2627 
2628 	if (!ieee80211_sdata_running(sdata))
2629 		return;
2630 
2631 	sdata_lock(sdata);
2632 
2633 	if (!ifmgd->associated)
2634 		goto out;
2635 
2636 	mutex_lock(&sdata->local->mtx);
2637 
2638 	if (sdata->local->tmp_channel || sdata->local->scanning) {
2639 		mutex_unlock(&sdata->local->mtx);
2640 		goto out;
2641 	}
2642 
2643 	if (beacon) {
2644 		mlme_dbg_ratelimited(sdata,
2645 				     "detected beacon loss from AP (missed %d beacons) - probing\n",
2646 				     beacon_loss_count);
2647 
2648 		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2649 	}
2650 
2651 	/*
2652 	 * The driver/our work has already reported this event or the
2653 	 * connection monitoring has kicked in and we have already sent
2654 	 * a probe request. Or maybe the AP died and the driver keeps
2655 	 * reporting until we disassociate...
2656 	 *
2657 	 * In either case we have to ignore the current call to this
2658 	 * function (except for setting the correct probe reason bit)
2659 	 * because otherwise we would reset the timer every time and
2660 	 * never check whether we received a probe response!
2661 	 */
2662 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2663 		already = true;
2664 
2665 	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2666 
2667 	mutex_unlock(&sdata->local->mtx);
2668 
2669 	if (already)
2670 		goto out;
2671 
2672 	mutex_lock(&sdata->local->iflist_mtx);
2673 	ieee80211_recalc_ps(sdata->local);
2674 	mutex_unlock(&sdata->local->iflist_mtx);
2675 
2676 	ifmgd->probe_send_count = 0;
2677 	ieee80211_mgd_probe_ap_send(sdata);
2678  out:
2679 	sdata_unlock(sdata);
2680 }
2681 
ieee80211_ap_probereq_get(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2682 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2683 					  struct ieee80211_vif *vif)
2684 {
2685 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2686 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2687 	struct cfg80211_bss *cbss;
2688 	struct sk_buff *skb;
2689 	const u8 *ssid;
2690 	int ssid_len;
2691 
2692 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2693 		return NULL;
2694 
2695 	sdata_assert_lock(sdata);
2696 
2697 	if (ifmgd->associated)
2698 		cbss = ifmgd->associated;
2699 	else if (ifmgd->auth_data)
2700 		cbss = ifmgd->auth_data->bss;
2701 	else if (ifmgd->assoc_data)
2702 		cbss = ifmgd->assoc_data->bss;
2703 	else
2704 		return NULL;
2705 
2706 	rcu_read_lock();
2707 	ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2708 	if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2709 		      "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
2710 		ssid_len = 0;
2711 	else
2712 		ssid_len = ssid[1];
2713 
2714 	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2715 					(u32) -1, cbss->channel,
2716 					ssid + 2, ssid_len,
2717 					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2718 	rcu_read_unlock();
2719 
2720 	return skb;
2721 }
2722 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2723 
ieee80211_report_disconnect(struct ieee80211_sub_if_data * sdata,const u8 * buf,size_t len,bool tx,u16 reason)2724 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2725 					const u8 *buf, size_t len, bool tx,
2726 					u16 reason)
2727 {
2728 	struct ieee80211_event event = {
2729 		.type = MLME_EVENT,
2730 		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2731 		.u.mlme.reason = reason,
2732 	};
2733 
2734 	if (tx)
2735 		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2736 	else
2737 		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2738 
2739 	drv_event_callback(sdata->local, sdata, &event);
2740 }
2741 
__ieee80211_disconnect(struct ieee80211_sub_if_data * sdata)2742 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2743 {
2744 	struct ieee80211_local *local = sdata->local;
2745 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2746 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2747 	bool tx;
2748 
2749 	sdata_lock(sdata);
2750 	if (!ifmgd->associated) {
2751 		sdata_unlock(sdata);
2752 		return;
2753 	}
2754 
2755 	tx = !sdata->csa_block_tx;
2756 
2757 	/* AP is probably out of range (or not reachable for another reason) so
2758 	 * remove the bss struct for that AP.
2759 	 */
2760 	cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2761 
2762 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2763 			       WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2764 			       tx, frame_buf);
2765 	mutex_lock(&local->mtx);
2766 	sdata->vif.csa_active = false;
2767 	ifmgd->csa_waiting_bcn = false;
2768 	if (sdata->csa_block_tx) {
2769 		ieee80211_wake_vif_queues(local, sdata,
2770 					  IEEE80211_QUEUE_STOP_REASON_CSA);
2771 		sdata->csa_block_tx = false;
2772 	}
2773 	mutex_unlock(&local->mtx);
2774 
2775 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
2776 				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2777 
2778 	sdata_unlock(sdata);
2779 }
2780 
ieee80211_beacon_connection_loss_work(struct work_struct * work)2781 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2782 {
2783 	struct ieee80211_sub_if_data *sdata =
2784 		container_of(work, struct ieee80211_sub_if_data,
2785 			     u.mgd.beacon_connection_loss_work);
2786 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2787 
2788 	if (ifmgd->associated)
2789 		ifmgd->beacon_loss_count++;
2790 
2791 	if (ifmgd->connection_loss) {
2792 		sdata_info(sdata, "Connection to AP %pM lost\n",
2793 			   ifmgd->bssid);
2794 		__ieee80211_disconnect(sdata);
2795 	} else {
2796 		ieee80211_mgd_probe_ap(sdata, true);
2797 	}
2798 }
2799 
ieee80211_csa_connection_drop_work(struct work_struct * work)2800 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2801 {
2802 	struct ieee80211_sub_if_data *sdata =
2803 		container_of(work, struct ieee80211_sub_if_data,
2804 			     u.mgd.csa_connection_drop_work);
2805 
2806 	__ieee80211_disconnect(sdata);
2807 }
2808 
ieee80211_beacon_loss(struct ieee80211_vif * vif)2809 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2810 {
2811 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2812 	struct ieee80211_hw *hw = &sdata->local->hw;
2813 
2814 	trace_api_beacon_loss(sdata);
2815 
2816 	sdata->u.mgd.connection_loss = false;
2817 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2818 }
2819 EXPORT_SYMBOL(ieee80211_beacon_loss);
2820 
ieee80211_connection_loss(struct ieee80211_vif * vif)2821 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2822 {
2823 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2824 	struct ieee80211_hw *hw = &sdata->local->hw;
2825 
2826 	trace_api_connection_loss(sdata);
2827 
2828 	sdata->u.mgd.connection_loss = true;
2829 	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2830 }
2831 EXPORT_SYMBOL(ieee80211_connection_loss);
2832 
2833 
ieee80211_destroy_auth_data(struct ieee80211_sub_if_data * sdata,bool assoc)2834 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2835 					bool assoc)
2836 {
2837 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2838 
2839 	sdata_assert_lock(sdata);
2840 
2841 	if (!assoc) {
2842 		/*
2843 		 * we are not authenticated yet, the only timer that could be
2844 		 * running is the timeout for the authentication response which
2845 		 * which is not relevant anymore.
2846 		 */
2847 		del_timer_sync(&sdata->u.mgd.timer);
2848 		sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2849 
2850 		eth_zero_addr(sdata->u.mgd.bssid);
2851 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2852 		sdata->u.mgd.flags = 0;
2853 		mutex_lock(&sdata->local->mtx);
2854 		ieee80211_vif_release_channel(sdata);
2855 		mutex_unlock(&sdata->local->mtx);
2856 	}
2857 
2858 	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2859 	kfree(auth_data);
2860 	sdata->u.mgd.auth_data = NULL;
2861 }
2862 
ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data * sdata,bool assoc,bool abandon)2863 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2864 					 bool assoc, bool abandon)
2865 {
2866 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2867 
2868 	sdata_assert_lock(sdata);
2869 
2870 	if (!assoc) {
2871 		/*
2872 		 * we are not associated yet, the only timer that could be
2873 		 * running is the timeout for the association response which
2874 		 * which is not relevant anymore.
2875 		 */
2876 		del_timer_sync(&sdata->u.mgd.timer);
2877 		sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2878 
2879 		eth_zero_addr(sdata->u.mgd.bssid);
2880 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2881 		sdata->u.mgd.flags = 0;
2882 		sdata->vif.mu_mimo_owner = false;
2883 
2884 		mutex_lock(&sdata->local->mtx);
2885 		ieee80211_vif_release_channel(sdata);
2886 		mutex_unlock(&sdata->local->mtx);
2887 
2888 		if (abandon)
2889 			cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2890 	}
2891 
2892 	kfree(assoc_data);
2893 	sdata->u.mgd.assoc_data = NULL;
2894 }
2895 
ieee80211_auth_challenge(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)2896 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2897 				     struct ieee80211_mgmt *mgmt, size_t len)
2898 {
2899 	struct ieee80211_local *local = sdata->local;
2900 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2901 	u8 *pos;
2902 	struct ieee802_11_elems elems;
2903 	u32 tx_flags = 0;
2904 
2905 	pos = mgmt->u.auth.variable;
2906 	ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2907 			       mgmt->bssid, auth_data->bss->bssid);
2908 	if (!elems.challenge)
2909 		return;
2910 	auth_data->expected_transaction = 4;
2911 	drv_mgd_prepare_tx(sdata->local, sdata, 0);
2912 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2913 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2914 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
2915 	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2916 			    elems.challenge - 2, elems.challenge_len + 2,
2917 			    auth_data->bss->bssid, auth_data->bss->bssid,
2918 			    auth_data->key, auth_data->key_len,
2919 			    auth_data->key_idx, tx_flags);
2920 }
2921 
ieee80211_mark_sta_auth(struct ieee80211_sub_if_data * sdata,const u8 * bssid)2922 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2923 				    const u8 *bssid)
2924 {
2925 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2926 	struct sta_info *sta;
2927 	bool result = true;
2928 
2929 	sdata_info(sdata, "authenticated\n");
2930 	ifmgd->auth_data->done = true;
2931 	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2932 	ifmgd->auth_data->timeout_started = true;
2933 	run_again(sdata, ifmgd->auth_data->timeout);
2934 
2935 	/* move station state to auth */
2936 	mutex_lock(&sdata->local->sta_mtx);
2937 	sta = sta_info_get(sdata, bssid);
2938 	if (!sta) {
2939 		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2940 		result = false;
2941 		goto out;
2942 	}
2943 	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2944 		sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2945 		result = false;
2946 		goto out;
2947 	}
2948 
2949 out:
2950 	mutex_unlock(&sdata->local->sta_mtx);
2951 	return result;
2952 }
2953 
ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)2954 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2955 				   struct ieee80211_mgmt *mgmt, size_t len)
2956 {
2957 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2958 	u8 bssid[ETH_ALEN];
2959 	u16 auth_alg, auth_transaction, status_code;
2960 	struct ieee80211_event event = {
2961 		.type = MLME_EVENT,
2962 		.u.mlme.data = AUTH_EVENT,
2963 	};
2964 
2965 	sdata_assert_lock(sdata);
2966 
2967 	if (len < 24 + 6)
2968 		return;
2969 
2970 	if (!ifmgd->auth_data || ifmgd->auth_data->done)
2971 		return;
2972 
2973 	memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2974 
2975 	if (!ether_addr_equal(bssid, mgmt->bssid))
2976 		return;
2977 
2978 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2979 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2980 	status_code = le16_to_cpu(mgmt->u.auth.status_code);
2981 
2982 	if (auth_alg != ifmgd->auth_data->algorithm ||
2983 	    (auth_alg != WLAN_AUTH_SAE &&
2984 	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
2985 	    (auth_alg == WLAN_AUTH_SAE &&
2986 	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
2987 	      auth_transaction > 2))) {
2988 		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2989 			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2990 			   auth_transaction,
2991 			   ifmgd->auth_data->expected_transaction);
2992 		return;
2993 	}
2994 
2995 	if (status_code != WLAN_STATUS_SUCCESS) {
2996 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2997 
2998 		if (auth_alg == WLAN_AUTH_SAE &&
2999 		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
3000 		     (auth_transaction == 1 &&
3001 		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
3002 		       status_code == WLAN_STATUS_SAE_PK))))
3003 			return;
3004 
3005 		sdata_info(sdata, "%pM denied authentication (status %d)\n",
3006 			   mgmt->sa, status_code);
3007 		ieee80211_destroy_auth_data(sdata, false);
3008 		event.u.mlme.status = MLME_DENIED;
3009 		event.u.mlme.reason = status_code;
3010 		drv_event_callback(sdata->local, sdata, &event);
3011 		return;
3012 	}
3013 
3014 	switch (ifmgd->auth_data->algorithm) {
3015 	case WLAN_AUTH_OPEN:
3016 	case WLAN_AUTH_LEAP:
3017 	case WLAN_AUTH_FT:
3018 	case WLAN_AUTH_SAE:
3019 	case WLAN_AUTH_FILS_SK:
3020 	case WLAN_AUTH_FILS_SK_PFS:
3021 	case WLAN_AUTH_FILS_PK:
3022 		break;
3023 	case WLAN_AUTH_SHARED_KEY:
3024 		if (ifmgd->auth_data->expected_transaction != 4) {
3025 			ieee80211_auth_challenge(sdata, mgmt, len);
3026 			/* need another frame */
3027 			return;
3028 		}
3029 		break;
3030 	default:
3031 		WARN_ONCE(1, "invalid auth alg %d",
3032 			  ifmgd->auth_data->algorithm);
3033 		return;
3034 	}
3035 
3036 	event.u.mlme.status = MLME_SUCCESS;
3037 	drv_event_callback(sdata->local, sdata, &event);
3038 	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3039 	    (auth_transaction == 2 &&
3040 	     ifmgd->auth_data->expected_transaction == 2)) {
3041 		if (!ieee80211_mark_sta_auth(sdata, bssid))
3042 			return; /* ignore frame -- wait for timeout */
3043 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3044 		   auth_transaction == 2) {
3045 		sdata_info(sdata, "SAE peer confirmed\n");
3046 		ifmgd->auth_data->peer_confirmed = true;
3047 	}
3048 
3049 	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3050 }
3051 
3052 #define case_WLAN(type) \
3053 	case WLAN_REASON_##type: return #type
3054 
ieee80211_get_reason_code_string(u16 reason_code)3055 const char *ieee80211_get_reason_code_string(u16 reason_code)
3056 {
3057 	switch (reason_code) {
3058 	case_WLAN(UNSPECIFIED);
3059 	case_WLAN(PREV_AUTH_NOT_VALID);
3060 	case_WLAN(DEAUTH_LEAVING);
3061 	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3062 	case_WLAN(DISASSOC_AP_BUSY);
3063 	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3064 	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3065 	case_WLAN(DISASSOC_STA_HAS_LEFT);
3066 	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3067 	case_WLAN(DISASSOC_BAD_POWER);
3068 	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3069 	case_WLAN(INVALID_IE);
3070 	case_WLAN(MIC_FAILURE);
3071 	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3072 	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3073 	case_WLAN(IE_DIFFERENT);
3074 	case_WLAN(INVALID_GROUP_CIPHER);
3075 	case_WLAN(INVALID_PAIRWISE_CIPHER);
3076 	case_WLAN(INVALID_AKMP);
3077 	case_WLAN(UNSUPP_RSN_VERSION);
3078 	case_WLAN(INVALID_RSN_IE_CAP);
3079 	case_WLAN(IEEE8021X_FAILED);
3080 	case_WLAN(CIPHER_SUITE_REJECTED);
3081 	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3082 	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3083 	case_WLAN(DISASSOC_LOW_ACK);
3084 	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3085 	case_WLAN(QSTA_LEAVE_QBSS);
3086 	case_WLAN(QSTA_NOT_USE);
3087 	case_WLAN(QSTA_REQUIRE_SETUP);
3088 	case_WLAN(QSTA_TIMEOUT);
3089 	case_WLAN(QSTA_CIPHER_NOT_SUPP);
3090 	case_WLAN(MESH_PEER_CANCELED);
3091 	case_WLAN(MESH_MAX_PEERS);
3092 	case_WLAN(MESH_CONFIG);
3093 	case_WLAN(MESH_CLOSE);
3094 	case_WLAN(MESH_MAX_RETRIES);
3095 	case_WLAN(MESH_CONFIRM_TIMEOUT);
3096 	case_WLAN(MESH_INVALID_GTK);
3097 	case_WLAN(MESH_INCONSISTENT_PARAM);
3098 	case_WLAN(MESH_INVALID_SECURITY);
3099 	case_WLAN(MESH_PATH_ERROR);
3100 	case_WLAN(MESH_PATH_NOFORWARD);
3101 	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3102 	case_WLAN(MAC_EXISTS_IN_MBSS);
3103 	case_WLAN(MESH_CHAN_REGULATORY);
3104 	case_WLAN(MESH_CHAN);
3105 	default: return "<unknown>";
3106 	}
3107 }
3108 
ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)3109 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3110 				     struct ieee80211_mgmt *mgmt, size_t len)
3111 {
3112 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3113 	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3114 
3115 	sdata_assert_lock(sdata);
3116 
3117 	if (len < 24 + 2)
3118 		return;
3119 
3120 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3121 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3122 		return;
3123 	}
3124 
3125 	if (ifmgd->associated &&
3126 	    ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3127 		const u8 *bssid = ifmgd->associated->bssid;
3128 
3129 		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3130 			   bssid, reason_code,
3131 			   ieee80211_get_reason_code_string(reason_code));
3132 
3133 		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3134 
3135 		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3136 					    reason_code);
3137 		return;
3138 	}
3139 
3140 	if (ifmgd->assoc_data &&
3141 	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3142 		const u8 *bssid = ifmgd->assoc_data->bss->bssid;
3143 
3144 		sdata_info(sdata,
3145 			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3146 			   bssid, reason_code,
3147 			   ieee80211_get_reason_code_string(reason_code));
3148 
3149 		ieee80211_destroy_assoc_data(sdata, false, true);
3150 
3151 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3152 		return;
3153 	}
3154 }
3155 
3156 
ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)3157 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3158 				       struct ieee80211_mgmt *mgmt, size_t len)
3159 {
3160 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3161 	u16 reason_code;
3162 
3163 	sdata_assert_lock(sdata);
3164 
3165 	if (len < 24 + 2)
3166 		return;
3167 
3168 	if (!ifmgd->associated ||
3169 	    !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3170 		return;
3171 
3172 	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3173 
3174 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3175 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3176 		return;
3177 	}
3178 
3179 	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3180 		   mgmt->sa, reason_code,
3181 		   ieee80211_get_reason_code_string(reason_code));
3182 
3183 	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3184 
3185 	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
3186 }
3187 
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)3188 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3189 				u8 *supp_rates, unsigned int supp_rates_len,
3190 				u32 *rates, u32 *basic_rates,
3191 				bool *have_higher_than_11mbit,
3192 				int *min_rate, int *min_rate_index,
3193 				int shift)
3194 {
3195 	int i, j;
3196 
3197 	for (i = 0; i < supp_rates_len; i++) {
3198 		int rate = supp_rates[i] & 0x7f;
3199 		bool is_basic = !!(supp_rates[i] & 0x80);
3200 
3201 		if ((rate * 5 * (1 << shift)) > 110)
3202 			*have_higher_than_11mbit = true;
3203 
3204 		/*
3205 		 * Skip HT, VHT and HE BSS membership selectors since they're
3206 		 * not rates.
3207 		 *
3208 		 * Note: Even though the membership selector and the basic
3209 		 *	 rate flag share the same bit, they are not exactly
3210 		 *	 the same.
3211 		 */
3212 		if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3213 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3214 		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY))
3215 			continue;
3216 
3217 		for (j = 0; j < sband->n_bitrates; j++) {
3218 			struct ieee80211_rate *br;
3219 			int brate;
3220 
3221 			br = &sband->bitrates[j];
3222 
3223 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3224 			if (brate == rate) {
3225 				*rates |= BIT(j);
3226 				if (is_basic)
3227 					*basic_rates |= BIT(j);
3228 				if ((rate * 5) < *min_rate) {
3229 					*min_rate = rate * 5;
3230 					*min_rate_index = j;
3231 				}
3232 				break;
3233 			}
3234 		}
3235 	}
3236 }
3237 
ieee80211_twt_req_supported(const struct sta_info * sta,const struct ieee802_11_elems * elems)3238 static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3239 					const struct ieee802_11_elems *elems)
3240 {
3241 	if (elems->ext_capab_len < 10)
3242 		return false;
3243 
3244 	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3245 		return false;
3246 
3247 	return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3248 		IEEE80211_HE_MAC_CAP0_TWT_RES;
3249 }
3250 
ieee80211_recalc_twt_req(struct ieee80211_sub_if_data * sdata,struct sta_info * sta,struct ieee802_11_elems * elems)3251 static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3252 				    struct sta_info *sta,
3253 				    struct ieee802_11_elems *elems)
3254 {
3255 	bool twt = ieee80211_twt_req_supported(sta, elems);
3256 
3257 	if (sdata->vif.bss_conf.twt_requester != twt) {
3258 		sdata->vif.bss_conf.twt_requester = twt;
3259 		return BSS_CHANGED_TWT;
3260 	}
3261 	return 0;
3262 }
3263 
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)3264 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3265 				    struct cfg80211_bss *cbss,
3266 				    struct ieee80211_mgmt *mgmt, size_t len,
3267 				    struct ieee802_11_elems *elems)
3268 {
3269 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3270 	struct ieee80211_local *local = sdata->local;
3271 	struct ieee80211_supported_band *sband;
3272 	struct sta_info *sta;
3273 	u16 capab_info, aid;
3274 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3275 	const struct cfg80211_bss_ies *bss_ies = NULL;
3276 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3277 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
3278 	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
3279 	u32 changed = 0;
3280 	u8 *pos;
3281 	int err;
3282 	bool ret;
3283 
3284 	/* AssocResp and ReassocResp have identical structure */
3285 
3286 	pos = mgmt->u.assoc_resp.variable;
3287 	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3288 	if (is_s1g) {
3289 		pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3290 		aid = 0; /* TODO */
3291 	}
3292 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3293 	ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, elems,
3294 			       mgmt->bssid, assoc_data->bss->bssid);
3295 
3296 	if (elems->aid_resp)
3297 		aid = le16_to_cpu(elems->aid_resp->aid);
3298 
3299 	/*
3300 	 * The 5 MSB of the AID field are reserved
3301 	 * (802.11-2016 9.4.1.8 AID field)
3302 	 */
3303 	aid &= 0x7ff;
3304 
3305 	ifmgd->broken_ap = false;
3306 
3307 	if (aid == 0 || aid > IEEE80211_MAX_AID) {
3308 		sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3309 			   aid);
3310 		aid = 0;
3311 		ifmgd->broken_ap = true;
3312 	}
3313 
3314 	if (!is_s1g && !elems->supp_rates) {
3315 		sdata_info(sdata, "no SuppRates element in AssocResp\n");
3316 		return false;
3317 	}
3318 
3319 	sdata->vif.bss_conf.aid = aid;
3320 	ifmgd->tdls_chan_switch_prohibited =
3321 		elems->ext_capab && elems->ext_capab_len >= 5 &&
3322 		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3323 
3324 	/*
3325 	 * Some APs are erroneously not including some information in their
3326 	 * (re)association response frames. Try to recover by using the data
3327 	 * from the beacon or probe response. This seems to afflict mobile
3328 	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3329 	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3330 	 */
3331 	if (!is_6ghz &&
3332 	    ((assoc_data->wmm && !elems->wmm_param) ||
3333 	     (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3334 	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
3335 	     (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3336 	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
3337 		const struct cfg80211_bss_ies *ies;
3338 		struct ieee802_11_elems bss_elems;
3339 
3340 		rcu_read_lock();
3341 		ies = rcu_dereference(cbss->ies);
3342 		if (ies)
3343 			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3344 					  GFP_ATOMIC);
3345 		rcu_read_unlock();
3346 		if (!bss_ies)
3347 			return false;
3348 
3349 		ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3350 				       false, &bss_elems,
3351 				       mgmt->bssid,
3352 				       assoc_data->bss->bssid);
3353 		if (assoc_data->wmm &&
3354 		    !elems->wmm_param && bss_elems.wmm_param) {
3355 			elems->wmm_param = bss_elems.wmm_param;
3356 			sdata_info(sdata,
3357 				   "AP bug: WMM param missing from AssocResp\n");
3358 		}
3359 
3360 		/*
3361 		 * Also check if we requested HT/VHT, otherwise the AP doesn't
3362 		 * have to include the IEs in the (re)association response.
3363 		 */
3364 		if (!elems->ht_cap_elem && bss_elems.ht_cap_elem &&
3365 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3366 			elems->ht_cap_elem = bss_elems.ht_cap_elem;
3367 			sdata_info(sdata,
3368 				   "AP bug: HT capability missing from AssocResp\n");
3369 		}
3370 		if (!elems->ht_operation && bss_elems.ht_operation &&
3371 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3372 			elems->ht_operation = bss_elems.ht_operation;
3373 			sdata_info(sdata,
3374 				   "AP bug: HT operation missing from AssocResp\n");
3375 		}
3376 		if (!elems->vht_cap_elem && bss_elems.vht_cap_elem &&
3377 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3378 			elems->vht_cap_elem = bss_elems.vht_cap_elem;
3379 			sdata_info(sdata,
3380 				   "AP bug: VHT capa missing from AssocResp\n");
3381 		}
3382 		if (!elems->vht_operation && bss_elems.vht_operation &&
3383 		    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3384 			elems->vht_operation = bss_elems.vht_operation;
3385 			sdata_info(sdata,
3386 				   "AP bug: VHT operation missing from AssocResp\n");
3387 		}
3388 		kfree(bss_elems.nontx_profile);
3389 	}
3390 
3391 	/*
3392 	 * We previously checked these in the beacon/probe response, so
3393 	 * they should be present here. This is just a safety net.
3394 	 */
3395 	if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3396 	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
3397 		sdata_info(sdata,
3398 			   "HT AP is missing WMM params or HT capability/operation\n");
3399 		ret = false;
3400 		goto out;
3401 	}
3402 
3403 	if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3404 	    (!elems->vht_cap_elem || !elems->vht_operation)) {
3405 		sdata_info(sdata,
3406 			   "VHT AP is missing VHT capability/operation\n");
3407 		ret = false;
3408 		goto out;
3409 	}
3410 
3411 	if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3412 	    !elems->he_6ghz_capa) {
3413 		sdata_info(sdata,
3414 			   "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3415 		ret = false;
3416 		goto out;
3417 	}
3418 
3419 	mutex_lock(&sdata->local->sta_mtx);
3420 	/*
3421 	 * station info was already allocated and inserted before
3422 	 * the association and should be available to us
3423 	 */
3424 	sta = sta_info_get(sdata, cbss->bssid);
3425 	if (WARN_ON(!sta)) {
3426 		mutex_unlock(&sdata->local->sta_mtx);
3427 		ret = false;
3428 		goto out;
3429 	}
3430 
3431 	sband = ieee80211_get_sband(sdata);
3432 	if (!sband) {
3433 		mutex_unlock(&sdata->local->sta_mtx);
3434 		ret = false;
3435 		goto out;
3436 	}
3437 
3438 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3439 	    (!elems->he_cap || !elems->he_operation)) {
3440 		mutex_unlock(&sdata->local->sta_mtx);
3441 		sdata_info(sdata,
3442 			   "HE AP is missing HE capability/operation\n");
3443 		ret = false;
3444 		goto out;
3445 	}
3446 
3447 	/* Set up internal HT/VHT capabilities */
3448 	if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3449 		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3450 						  elems->ht_cap_elem, sta);
3451 
3452 	if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3453 		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3454 						    elems->vht_cap_elem, sta);
3455 
3456 	if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3457 	    elems->he_cap) {
3458 		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3459 						  elems->he_cap,
3460 						  elems->he_cap_len,
3461 						  elems->he_6ghz_capa,
3462 						  sta);
3463 
3464 		bss_conf->he_support = sta->sta.he_cap.has_he;
3465 		if (elems->rsnx && elems->rsnx_len &&
3466 		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3467 		    wiphy_ext_feature_isset(local->hw.wiphy,
3468 					    NL80211_EXT_FEATURE_PROTECTED_TWT))
3469 			bss_conf->twt_protected = true;
3470 		else
3471 			bss_conf->twt_protected = false;
3472 
3473 		changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
3474 	} else {
3475 		bss_conf->he_support = false;
3476 		bss_conf->twt_requester = false;
3477 		bss_conf->twt_protected = false;
3478 	}
3479 
3480 	if (bss_conf->he_support) {
3481 		bss_conf->he_bss_color.color =
3482 			le32_get_bits(elems->he_operation->he_oper_params,
3483 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3484 		bss_conf->he_bss_color.partial =
3485 			le32_get_bits(elems->he_operation->he_oper_params,
3486 				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
3487 		bss_conf->he_bss_color.enabled =
3488 			!le32_get_bits(elems->he_operation->he_oper_params,
3489 				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3490 
3491 		if (bss_conf->he_bss_color.enabled)
3492 			changed |= BSS_CHANGED_HE_BSS_COLOR;
3493 
3494 		bss_conf->htc_trig_based_pkt_ext =
3495 			le32_get_bits(elems->he_operation->he_oper_params,
3496 			      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
3497 		bss_conf->frame_time_rts_th =
3498 			le32_get_bits(elems->he_operation->he_oper_params,
3499 			      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3500 
3501 		bss_conf->multi_sta_back_32bit =
3502 			sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3503 			IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3504 
3505 		bss_conf->ack_enabled =
3506 			sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3507 			IEEE80211_HE_MAC_CAP2_ACK_EN;
3508 
3509 		bss_conf->uora_exists = !!elems->uora_element;
3510 		if (elems->uora_element)
3511 			bss_conf->uora_ocw_range = elems->uora_element[0];
3512 
3513 		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3514 		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
3515 		/* TODO: OPEN: what happens if BSS color disable is set? */
3516 	}
3517 
3518 	if (cbss->transmitted_bss) {
3519 		bss_conf->nontransmitted = true;
3520 		ether_addr_copy(bss_conf->transmitter_bssid,
3521 				cbss->transmitted_bss->bssid);
3522 		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3523 		bss_conf->bssid_index = cbss->bssid_index;
3524 	}
3525 
3526 	/*
3527 	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3528 	 * in their association response, so ignore that data for our own
3529 	 * configuration. If it changed since the last beacon, we'll get the
3530 	 * next beacon and update then.
3531 	 */
3532 
3533 	/*
3534 	 * If an operating mode notification IE is present, override the
3535 	 * NSS calculation (that would be done in rate_control_rate_init())
3536 	 * and use the # of streams from that element.
3537 	 */
3538 	if (elems->opmode_notif &&
3539 	    !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3540 		u8 nss;
3541 
3542 		nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3543 		nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3544 		nss += 1;
3545 		sta->sta.rx_nss = nss;
3546 	}
3547 
3548 	rate_control_rate_init(sta);
3549 
3550 	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3551 		set_sta_flag(sta, WLAN_STA_MFP);
3552 		sta->sta.mfp = true;
3553 	} else {
3554 		sta->sta.mfp = false;
3555 	}
3556 
3557 	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
3558 		       local->hw.queues >= IEEE80211_NUM_ACS;
3559 
3560 	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3561 	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3562 		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3563 	if (err) {
3564 		sdata_info(sdata,
3565 			   "failed to move station %pM to desired state\n",
3566 			   sta->sta.addr);
3567 		WARN_ON(__sta_info_destroy(sta));
3568 		mutex_unlock(&sdata->local->sta_mtx);
3569 		ret = false;
3570 		goto out;
3571 	}
3572 
3573 	if (sdata->wdev.use_4addr)
3574 		drv_sta_set_4addr(local, sdata, &sta->sta, true);
3575 
3576 	mutex_unlock(&sdata->local->sta_mtx);
3577 
3578 	/*
3579 	 * Always handle WMM once after association regardless
3580 	 * of the first value the AP uses. Setting -1 here has
3581 	 * that effect because the AP values is an unsigned
3582 	 * 4-bit value.
3583 	 */
3584 	ifmgd->wmm_last_param_set = -1;
3585 	ifmgd->mu_edca_last_param_set = -1;
3586 
3587 	if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3588 		ieee80211_set_wmm_default(sdata, false, false);
3589 	} else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3590 					     elems->wmm_param_len,
3591 					     elems->mu_edca_param_set)) {
3592 		/* still enable QoS since we might have HT/VHT */
3593 		ieee80211_set_wmm_default(sdata, false, true);
3594 		/* set the disable-WMM flag in this case to disable
3595 		 * tracking WMM parameter changes in the beacon if
3596 		 * the parameters weren't actually valid. Doing so
3597 		 * avoids changing parameters very strangely when
3598 		 * the AP is going back and forth between valid and
3599 		 * invalid parameters.
3600 		 */
3601 		ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3602 	}
3603 	changed |= BSS_CHANGED_QOS;
3604 
3605 	if (elems->max_idle_period_ie) {
3606 		bss_conf->max_idle_period =
3607 			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
3608 		bss_conf->protected_keep_alive =
3609 			!!(elems->max_idle_period_ie->idle_options &
3610 			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3611 		changed |= BSS_CHANGED_KEEP_ALIVE;
3612 	} else {
3613 		bss_conf->max_idle_period = 0;
3614 		bss_conf->protected_keep_alive = false;
3615 	}
3616 
3617 	/* set assoc capability (AID was already set earlier),
3618 	 * ieee80211_set_associated() will tell the driver */
3619 	bss_conf->assoc_capability = capab_info;
3620 	ieee80211_set_associated(sdata, cbss, changed);
3621 
3622 	/*
3623 	 * If we're using 4-addr mode, let the AP know that we're
3624 	 * doing so, so that it can create the STA VLAN on its side
3625 	 */
3626 	if (ifmgd->use_4addr)
3627 		ieee80211_send_4addr_nullfunc(local, sdata);
3628 
3629 	/*
3630 	 * Start timer to probe the connection to the AP now.
3631 	 * Also start the timer that will detect beacon loss.
3632 	 */
3633 	ieee80211_sta_reset_beacon_monitor(sdata);
3634 	ieee80211_sta_reset_conn_monitor(sdata);
3635 
3636 	ret = true;
3637  out:
3638 	kfree(bss_ies);
3639 	return ret;
3640 }
3641 
ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)3642 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3643 					 struct ieee80211_mgmt *mgmt,
3644 					 size_t len)
3645 {
3646 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3647 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3648 	u16 capab_info, status_code, aid;
3649 	struct ieee802_11_elems elems;
3650 	int ac, uapsd_queues = -1;
3651 	u8 *pos;
3652 	bool reassoc;
3653 	struct cfg80211_bss *cbss;
3654 	struct ieee80211_event event = {
3655 		.type = MLME_EVENT,
3656 		.u.mlme.data = ASSOC_EVENT,
3657 	};
3658 
3659 	sdata_assert_lock(sdata);
3660 
3661 	if (!assoc_data)
3662 		return;
3663 
3664 	if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
3665 		return;
3666 
3667 	cbss = assoc_data->bss;
3668 
3669 	/*
3670 	 * AssocResp and ReassocResp have identical structure, so process both
3671 	 * of them in this function.
3672 	 */
3673 
3674 	if (len < 24 + 6)
3675 		return;
3676 
3677 	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
3678 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3679 	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3680 	pos = mgmt->u.assoc_resp.variable;
3681 	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3682 	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
3683 		pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3684 		aid = 0; /* TODO */
3685 	}
3686 
3687 	sdata_info(sdata,
3688 		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3689 		   reassoc ? "Rea" : "A", mgmt->sa,
3690 		   capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
3691 
3692 	if (assoc_data->fils_kek_len &&
3693 	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3694 		return;
3695 
3696 	ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3697 			       mgmt->bssid, assoc_data->bss->bssid);
3698 
3699 	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
3700 	    elems.timeout_int &&
3701 	    elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
3702 		u32 tu, ms;
3703 		tu = le32_to_cpu(elems.timeout_int->value);
3704 		ms = tu * 1024 / 1000;
3705 		sdata_info(sdata,
3706 			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3707 			   mgmt->sa, tu, ms);
3708 		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
3709 		assoc_data->timeout_started = true;
3710 		if (ms > IEEE80211_ASSOC_TIMEOUT)
3711 			run_again(sdata, assoc_data->timeout);
3712 		return;
3713 	}
3714 
3715 	if (status_code != WLAN_STATUS_SUCCESS) {
3716 		sdata_info(sdata, "%pM denied association (code=%d)\n",
3717 			   mgmt->sa, status_code);
3718 		ieee80211_destroy_assoc_data(sdata, false, false);
3719 		event.u.mlme.status = MLME_DENIED;
3720 		event.u.mlme.reason = status_code;
3721 		drv_event_callback(sdata->local, sdata, &event);
3722 	} else {
3723 		if (!ieee80211_assoc_success(sdata, cbss, mgmt, len, &elems)) {
3724 			/* oops -- internal error -- send timeout for now */
3725 			ieee80211_destroy_assoc_data(sdata, false, false);
3726 			cfg80211_assoc_timeout(sdata->dev, cbss);
3727 			return;
3728 		}
3729 		event.u.mlme.status = MLME_SUCCESS;
3730 		drv_event_callback(sdata->local, sdata, &event);
3731 		sdata_info(sdata, "associated\n");
3732 
3733 		/*
3734 		 * destroy assoc_data afterwards, as otherwise an idle
3735 		 * recalc after assoc_data is NULL but before associated
3736 		 * is set can cause the interface to go idle
3737 		 */
3738 		ieee80211_destroy_assoc_data(sdata, true, false);
3739 
3740 		/* get uapsd queues configuration */
3741 		uapsd_queues = 0;
3742 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3743 			if (sdata->tx_conf[ac].uapsd)
3744 				uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
3745 	}
3746 
3747 	cfg80211_rx_assoc_resp(sdata->dev, cbss, (u8 *)mgmt, len, uapsd_queues,
3748 			       ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
3749 }
3750 
ieee80211_rx_bss_info(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status)3751 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
3752 				  struct ieee80211_mgmt *mgmt, size_t len,
3753 				  struct ieee80211_rx_status *rx_status)
3754 {
3755 	struct ieee80211_local *local = sdata->local;
3756 	struct ieee80211_bss *bss;
3757 	struct ieee80211_channel *channel;
3758 
3759 	sdata_assert_lock(sdata);
3760 
3761 	channel = ieee80211_get_channel_khz(local->hw.wiphy,
3762 					ieee80211_rx_status_to_khz(rx_status));
3763 	if (!channel)
3764 		return;
3765 
3766 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
3767 	if (bss) {
3768 		sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
3769 		ieee80211_rx_bss_put(local, bss);
3770 	}
3771 }
3772 
3773 
ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)3774 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
3775 					 struct sk_buff *skb)
3776 {
3777 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
3778 	struct ieee80211_if_managed *ifmgd;
3779 	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3780 	struct ieee80211_channel *channel;
3781 	size_t baselen, len = skb->len;
3782 
3783 	ifmgd = &sdata->u.mgd;
3784 
3785 	sdata_assert_lock(sdata);
3786 
3787 	/*
3788 	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3789 	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
3790 	 * a Probe Response frame [..], the Address 1 field of the Probe
3791 	 * Response frame shall be set to the broadcast address [..]"
3792 	 * So, on 6GHz band we should also accept broadcast responses.
3793 	 */
3794 	channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3795 					rx_status->freq);
3796 	if (!channel)
3797 		return;
3798 
3799 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3800 	    (channel->band != NL80211_BAND_6GHZ ||
3801 	     !is_broadcast_ether_addr(mgmt->da)))
3802 		return; /* ignore ProbeResp to foreign address */
3803 
3804 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3805 	if (baselen > len)
3806 		return;
3807 
3808 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3809 
3810 	if (ifmgd->associated &&
3811 	    ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3812 		ieee80211_reset_ap_probe(sdata);
3813 }
3814 
3815 /*
3816  * This is the canonical list of information elements we care about,
3817  * the filter code also gives us all changes to the Microsoft OUI
3818  * (00:50:F2) vendor IE which is used for WMM which we need to track,
3819  * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3820  * changes to requested client power.
3821  *
3822  * We implement beacon filtering in software since that means we can
3823  * avoid processing the frame here and in cfg80211, and userspace
3824  * will not be able to tell whether the hardware supports it or not.
3825  *
3826  * XXX: This list needs to be dynamic -- userspace needs to be able to
3827  *	add items it requires. It also needs to be able to tell us to
3828  *	look out for other vendor IEs.
3829  */
3830 static const u64 care_about_ies =
3831 	(1ULL << WLAN_EID_COUNTRY) |
3832 	(1ULL << WLAN_EID_ERP_INFO) |
3833 	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
3834 	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
3835 	(1ULL << WLAN_EID_HT_CAPABILITY) |
3836 	(1ULL << WLAN_EID_HT_OPERATION) |
3837 	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
3838 
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)3839 static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3840 					struct ieee80211_if_managed *ifmgd,
3841 					struct ieee80211_bss_conf *bss_conf,
3842 					struct ieee80211_local *local,
3843 					struct ieee80211_rx_status *rx_status)
3844 {
3845 	/* Track average RSSI from the Beacon frames of the current AP */
3846 
3847 	if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3848 		ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3849 		ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
3850 		ifmgd->last_cqm_event_signal = 0;
3851 		ifmgd->count_beacon_signal = 1;
3852 		ifmgd->last_ave_beacon_signal = 0;
3853 	} else {
3854 		ifmgd->count_beacon_signal++;
3855 	}
3856 
3857 	ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3858 
3859 	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3860 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3861 		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3862 		int last_sig = ifmgd->last_ave_beacon_signal;
3863 		struct ieee80211_event event = {
3864 			.type = RSSI_EVENT,
3865 		};
3866 
3867 		/*
3868 		 * if signal crosses either of the boundaries, invoke callback
3869 		 * with appropriate parameters
3870 		 */
3871 		if (sig > ifmgd->rssi_max_thold &&
3872 		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3873 			ifmgd->last_ave_beacon_signal = sig;
3874 			event.u.rssi.data = RSSI_EVENT_HIGH;
3875 			drv_event_callback(local, sdata, &event);
3876 		} else if (sig < ifmgd->rssi_min_thold &&
3877 			   (last_sig >= ifmgd->rssi_max_thold ||
3878 			   last_sig == 0)) {
3879 			ifmgd->last_ave_beacon_signal = sig;
3880 			event.u.rssi.data = RSSI_EVENT_LOW;
3881 			drv_event_callback(local, sdata, &event);
3882 		}
3883 	}
3884 
3885 	if (bss_conf->cqm_rssi_thold &&
3886 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3887 	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3888 		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3889 		int last_event = ifmgd->last_cqm_event_signal;
3890 		int thold = bss_conf->cqm_rssi_thold;
3891 		int hyst = bss_conf->cqm_rssi_hyst;
3892 
3893 		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_LOW,
3899 				sig, GFP_KERNEL);
3900 		} else if (sig > thold &&
3901 			   (last_event == 0 || sig > last_event + hyst)) {
3902 			ifmgd->last_cqm_event_signal = sig;
3903 			ieee80211_cqm_rssi_notify(
3904 				&sdata->vif,
3905 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3906 				sig, GFP_KERNEL);
3907 		}
3908 	}
3909 
3910 	if (bss_conf->cqm_rssi_low &&
3911 	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3912 		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3913 		int last_event = ifmgd->last_cqm_event_signal;
3914 		int low = bss_conf->cqm_rssi_low;
3915 		int high = bss_conf->cqm_rssi_high;
3916 
3917 		if (sig < low &&
3918 		    (last_event == 0 || last_event >= low)) {
3919 			ifmgd->last_cqm_event_signal = sig;
3920 			ieee80211_cqm_rssi_notify(
3921 				&sdata->vif,
3922 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3923 				sig, GFP_KERNEL);
3924 		} else if (sig > high &&
3925 			   (last_event == 0 || last_event <= high)) {
3926 			ifmgd->last_cqm_event_signal = sig;
3927 			ieee80211_cqm_rssi_notify(
3928 				&sdata->vif,
3929 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3930 				sig, GFP_KERNEL);
3931 		}
3932 	}
3933 }
3934 
ieee80211_rx_our_beacon(const u8 * tx_bssid,struct cfg80211_bss * bss)3935 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3936 				    struct cfg80211_bss *bss)
3937 {
3938 	if (ether_addr_equal(tx_bssid, bss->bssid))
3939 		return true;
3940 	if (!bss->transmitted_bss)
3941 		return false;
3942 	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3943 }
3944 
ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data * sdata,struct ieee80211_hdr * hdr,size_t len,struct ieee80211_rx_status * rx_status)3945 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3946 				     struct ieee80211_hdr *hdr, size_t len,
3947 				     struct ieee80211_rx_status *rx_status)
3948 {
3949 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3950 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3951 	struct ieee80211_mgmt *mgmt = (void *) hdr;
3952 	size_t baselen;
3953 	struct ieee802_11_elems elems;
3954 	struct ieee80211_local *local = sdata->local;
3955 	struct ieee80211_chanctx_conf *chanctx_conf;
3956 	struct ieee80211_channel *chan;
3957 	struct sta_info *sta;
3958 	u32 changed = 0;
3959 	bool erp_valid;
3960 	u8 erp_value = 0;
3961 	u32 ncrc = 0;
3962 	u8 *bssid, *variable = mgmt->u.beacon.variable;
3963 	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3964 
3965 	sdata_assert_lock(sdata);
3966 
3967 	/* Process beacon from the current BSS */
3968 	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
3969 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
3970 		struct ieee80211_ext *ext = (void *) mgmt;
3971 
3972 		if (ieee80211_is_s1g_short_beacon(ext->frame_control))
3973 			variable = ext->u.s1g_short_beacon.variable;
3974 		else
3975 			variable = ext->u.s1g_beacon.variable;
3976 	}
3977 
3978 	baselen = (u8 *) variable - (u8 *) mgmt;
3979 	if (baselen > len)
3980 		return;
3981 
3982 	rcu_read_lock();
3983 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3984 	if (!chanctx_conf) {
3985 		rcu_read_unlock();
3986 		return;
3987 	}
3988 
3989 	if (ieee80211_rx_status_to_khz(rx_status) !=
3990 	    ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
3991 		rcu_read_unlock();
3992 		return;
3993 	}
3994 	chan = chanctx_conf->def.chan;
3995 	rcu_read_unlock();
3996 
3997 	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
3998 	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->bss)) {
3999 		ieee802_11_parse_elems(variable,
4000 				       len - baselen, false, &elems,
4001 				       bssid,
4002 				       ifmgd->assoc_data->bss->bssid);
4003 
4004 		ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4005 
4006 		if (elems.dtim_period)
4007 			ifmgd->dtim_period = elems.dtim_period;
4008 		ifmgd->have_beacon = true;
4009 		ifmgd->assoc_data->need_beacon = false;
4010 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
4011 			sdata->vif.bss_conf.sync_tsf =
4012 				le64_to_cpu(mgmt->u.beacon.timestamp);
4013 			sdata->vif.bss_conf.sync_device_ts =
4014 				rx_status->device_timestamp;
4015 			sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
4016 		}
4017 
4018 		if (elems.mbssid_config_ie)
4019 			bss_conf->profile_periodicity =
4020 				elems.mbssid_config_ie->profile_periodicity;
4021 		else
4022 			bss_conf->profile_periodicity = 0;
4023 
4024 		if (elems.ext_capab_len >= 11 &&
4025 		    (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
4026 			bss_conf->ema_ap = true;
4027 		else
4028 			bss_conf->ema_ap = false;
4029 
4030 		/* continue assoc process */
4031 		ifmgd->assoc_data->timeout = jiffies;
4032 		ifmgd->assoc_data->timeout_started = true;
4033 		run_again(sdata, ifmgd->assoc_data->timeout);
4034 		kfree(elems.nontx_profile);
4035 		return;
4036 	}
4037 
4038 	if (!ifmgd->associated ||
4039 	    !ieee80211_rx_our_beacon(bssid,  ifmgd->associated))
4040 		return;
4041 	bssid = ifmgd->associated->bssid;
4042 
4043 	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4044 		ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
4045 					    local, rx_status);
4046 
4047 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
4048 		mlme_dbg_ratelimited(sdata,
4049 				     "cancelling AP probe due to a received beacon\n");
4050 		ieee80211_reset_ap_probe(sdata);
4051 	}
4052 
4053 	/*
4054 	 * Push the beacon loss detection into the future since
4055 	 * we are processing a beacon from the AP just now.
4056 	 */
4057 	ieee80211_sta_reset_beacon_monitor(sdata);
4058 
4059 	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
4060 	 * element (which carries the beacon interval). Don't forget to add a
4061 	 * bit to care_about_ies[] above if mac80211 is interested in a
4062 	 * changing S1G element.
4063 	 */
4064 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4065 		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4066 	ncrc = ieee802_11_parse_elems_crc(variable,
4067 					  len - baselen, false, &elems,
4068 					  care_about_ies, ncrc,
4069 					  mgmt->bssid, bssid);
4070 
4071 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
4072 	    ieee80211_check_tim(elems.tim, elems.tim_len, bss_conf->aid)) {
4073 		if (local->hw.conf.dynamic_ps_timeout > 0) {
4074 			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4075 				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4076 				ieee80211_hw_config(local,
4077 						    IEEE80211_CONF_CHANGE_PS);
4078 			}
4079 			ieee80211_send_nullfunc(local, sdata, false);
4080 		} else if (!local->pspolling && sdata->u.mgd.powersave) {
4081 			local->pspolling = true;
4082 
4083 			/*
4084 			 * Here is assumed that the driver will be
4085 			 * able to send ps-poll frame and receive a
4086 			 * response even though power save mode is
4087 			 * enabled, but some drivers might require
4088 			 * to disable power save here. This needs
4089 			 * to be investigated.
4090 			 */
4091 			ieee80211_send_pspoll(local, sdata);
4092 		}
4093 	}
4094 
4095 	if (sdata->vif.p2p ||
4096 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
4097 		struct ieee80211_p2p_noa_attr noa = {};
4098 		int ret;
4099 
4100 		ret = cfg80211_get_p2p_attr(variable,
4101 					    len - baselen,
4102 					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
4103 					    (u8 *) &noa, sizeof(noa));
4104 		if (ret >= 2) {
4105 			if (sdata->u.mgd.p2p_noa_index != noa.index) {
4106 				/* valid noa_attr and index changed */
4107 				sdata->u.mgd.p2p_noa_index = noa.index;
4108 				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4109 				changed |= BSS_CHANGED_P2P_PS;
4110 				/*
4111 				 * make sure we update all information, the CRC
4112 				 * mechanism doesn't look at P2P attributes.
4113 				 */
4114 				ifmgd->beacon_crc_valid = false;
4115 			}
4116 		} else if (sdata->u.mgd.p2p_noa_index != -1) {
4117 			/* noa_attr not found and we had valid noa_attr before */
4118 			sdata->u.mgd.p2p_noa_index = -1;
4119 			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
4120 			changed |= BSS_CHANGED_P2P_PS;
4121 			ifmgd->beacon_crc_valid = false;
4122 		}
4123 	}
4124 
4125 	if (ifmgd->csa_waiting_bcn)
4126 		ieee80211_chswitch_post_beacon(sdata);
4127 
4128 	/*
4129 	 * Update beacon timing and dtim count on every beacon appearance. This
4130 	 * will allow the driver to use the most updated values. Do it before
4131 	 * comparing this one with last received beacon.
4132 	 * IMPORTANT: These parameters would possibly be out of sync by the time
4133 	 * the driver will use them. The synchronized view is currently
4134 	 * guaranteed only in certain callbacks.
4135 	 */
4136 	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
4137 	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
4138 		sdata->vif.bss_conf.sync_tsf =
4139 			le64_to_cpu(mgmt->u.beacon.timestamp);
4140 		sdata->vif.bss_conf.sync_device_ts =
4141 			rx_status->device_timestamp;
4142 		sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
4143 	}
4144 
4145 	if ((ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) ||
4146 	    ieee80211_is_s1g_short_beacon(mgmt->frame_control))
4147 		return;
4148 	ifmgd->beacon_crc = ncrc;
4149 	ifmgd->beacon_crc_valid = true;
4150 
4151 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4152 
4153 	ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
4154 					 rx_status->device_timestamp,
4155 					 &elems, true);
4156 
4157 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4158 	    ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
4159 				     elems.wmm_param_len,
4160 				     elems.mu_edca_param_set))
4161 		changed |= BSS_CHANGED_QOS;
4162 
4163 	/*
4164 	 * If we haven't had a beacon before, tell the driver about the
4165 	 * DTIM period (and beacon timing if desired) now.
4166 	 */
4167 	if (!ifmgd->have_beacon) {
4168 		/* a few bogus AP send dtim_period = 0 or no TIM IE */
4169 		bss_conf->dtim_period = elems.dtim_period ?: 1;
4170 
4171 		changed |= BSS_CHANGED_BEACON_INFO;
4172 		ifmgd->have_beacon = true;
4173 
4174 		mutex_lock(&local->iflist_mtx);
4175 		ieee80211_recalc_ps(local);
4176 		mutex_unlock(&local->iflist_mtx);
4177 
4178 		ieee80211_recalc_ps_vif(sdata);
4179 	}
4180 
4181 	if (elems.erp_info) {
4182 		erp_valid = true;
4183 		erp_value = elems.erp_info[0];
4184 	} else {
4185 		erp_valid = false;
4186 	}
4187 
4188 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4189 		changed |= ieee80211_handle_bss_capability(sdata,
4190 				le16_to_cpu(mgmt->u.beacon.capab_info),
4191 				erp_valid, erp_value);
4192 
4193 	mutex_lock(&local->sta_mtx);
4194 	sta = sta_info_get(sdata, bssid);
4195 
4196 	changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4197 
4198 	if (ieee80211_config_bw(sdata, sta, elems.ht_cap_elem,
4199 				elems.vht_cap_elem, elems.ht_operation,
4200 				elems.vht_operation, elems.he_operation,
4201 				elems.s1g_oper, bssid, &changed)) {
4202 		mutex_unlock(&local->sta_mtx);
4203 		sdata_info(sdata,
4204 			   "failed to follow AP %pM bandwidth change, disconnect\n",
4205 			   bssid);
4206 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4207 				       WLAN_REASON_DEAUTH_LEAVING,
4208 				       true, deauth_buf);
4209 		ieee80211_report_disconnect(sdata, deauth_buf,
4210 					    sizeof(deauth_buf), true,
4211 					    WLAN_REASON_DEAUTH_LEAVING);
4212 		goto free;
4213 	}
4214 
4215 	if (sta && elems.opmode_notif)
4216 		ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
4217 					    rx_status->band);
4218 	mutex_unlock(&local->sta_mtx);
4219 
4220 	changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4221 					       elems.country_elem,
4222 					       elems.country_elem_len,
4223 					       elems.pwr_constr_elem,
4224 					       elems.cisco_dtpc_elem);
4225 
4226 	ieee80211_bss_info_change_notify(sdata, changed);
4227 free:
4228 	kfree(elems.nontx_profile);
4229 }
4230 
ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)4231 void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
4232 				 struct sk_buff *skb)
4233 {
4234 	struct ieee80211_rx_status *rx_status;
4235 	struct ieee80211_hdr *hdr;
4236 	u16 fc;
4237 
4238 	rx_status = (struct ieee80211_rx_status *) skb->cb;
4239 	hdr = (struct ieee80211_hdr *) skb->data;
4240 	fc = le16_to_cpu(hdr->frame_control);
4241 
4242 	sdata_lock(sdata);
4243 	switch (fc & IEEE80211_FCTL_STYPE) {
4244 	case IEEE80211_STYPE_S1G_BEACON:
4245 		ieee80211_rx_mgmt_beacon(sdata, hdr, skb->len, rx_status);
4246 		break;
4247 	}
4248 	sdata_unlock(sdata);
4249 }
4250 
ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)4251 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4252 				  struct sk_buff *skb)
4253 {
4254 	struct ieee80211_rx_status *rx_status;
4255 	struct ieee80211_mgmt *mgmt;
4256 	u16 fc;
4257 	struct ieee802_11_elems elems;
4258 	int ies_len;
4259 
4260 	rx_status = (struct ieee80211_rx_status *) skb->cb;
4261 	mgmt = (struct ieee80211_mgmt *) skb->data;
4262 	fc = le16_to_cpu(mgmt->frame_control);
4263 
4264 	sdata_lock(sdata);
4265 
4266 	switch (fc & IEEE80211_FCTL_STYPE) {
4267 	case IEEE80211_STYPE_BEACON:
4268 		ieee80211_rx_mgmt_beacon(sdata, (void *)mgmt,
4269 					 skb->len, rx_status);
4270 		break;
4271 	case IEEE80211_STYPE_PROBE_RESP:
4272 		ieee80211_rx_mgmt_probe_resp(sdata, skb);
4273 		break;
4274 	case IEEE80211_STYPE_AUTH:
4275 		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
4276 		break;
4277 	case IEEE80211_STYPE_DEAUTH:
4278 		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
4279 		break;
4280 	case IEEE80211_STYPE_DISASSOC:
4281 		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
4282 		break;
4283 	case IEEE80211_STYPE_ASSOC_RESP:
4284 	case IEEE80211_STYPE_REASSOC_RESP:
4285 		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
4286 		break;
4287 	case IEEE80211_STYPE_ACTION:
4288 		if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
4289 			ies_len = skb->len -
4290 				  offsetof(struct ieee80211_mgmt,
4291 					   u.action.u.chan_switch.variable);
4292 
4293 			if (ies_len < 0)
4294 				break;
4295 
4296 			/* CSA IE cannot be overridden, no need for BSSID */
4297 			ieee802_11_parse_elems(
4298 				mgmt->u.action.u.chan_switch.variable,
4299 				ies_len, true, &elems, mgmt->bssid, NULL);
4300 
4301 			if (elems.parse_error)
4302 				break;
4303 
4304 			ieee80211_sta_process_chanswitch(sdata,
4305 						 rx_status->mactime,
4306 						 rx_status->device_timestamp,
4307 						 &elems, false);
4308 		} else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4309 			ies_len = skb->len -
4310 				  offsetof(struct ieee80211_mgmt,
4311 					   u.action.u.ext_chan_switch.variable);
4312 
4313 			if (ies_len < 0)
4314 				break;
4315 
4316 			/*
4317 			 * extended CSA IE can't be overridden, no need for
4318 			 * BSSID
4319 			 */
4320 			ieee802_11_parse_elems(
4321 				mgmt->u.action.u.ext_chan_switch.variable,
4322 				ies_len, true, &elems, mgmt->bssid, NULL);
4323 
4324 			if (elems.parse_error)
4325 				break;
4326 
4327 			/* for the handling code pretend this was also an IE */
4328 			elems.ext_chansw_ie =
4329 				&mgmt->u.action.u.ext_chan_switch.data;
4330 
4331 			ieee80211_sta_process_chanswitch(sdata,
4332 						 rx_status->mactime,
4333 						 rx_status->device_timestamp,
4334 						 &elems, false);
4335 		}
4336 		break;
4337 	}
4338 	sdata_unlock(sdata);
4339 }
4340 
ieee80211_sta_timer(struct timer_list * t)4341 static void ieee80211_sta_timer(struct timer_list *t)
4342 {
4343 	struct ieee80211_sub_if_data *sdata =
4344 		from_timer(sdata, t, u.mgd.timer);
4345 
4346 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
4347 }
4348 
ieee80211_sta_connection_lost(struct ieee80211_sub_if_data * sdata,u8 * bssid,u8 reason,bool tx)4349 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4350 					  u8 *bssid, u8 reason, bool tx)
4351 {
4352 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4353 
4354 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
4355 			       tx, frame_buf);
4356 
4357 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4358 				    reason);
4359 }
4360 
ieee80211_auth(struct ieee80211_sub_if_data * sdata)4361 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
4362 {
4363 	struct ieee80211_local *local = sdata->local;
4364 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4365 	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
4366 	u32 tx_flags = 0;
4367 	u16 trans = 1;
4368 	u16 status = 0;
4369 	u16 prepare_tx_duration = 0;
4370 
4371 	sdata_assert_lock(sdata);
4372 
4373 	if (WARN_ON_ONCE(!auth_data))
4374 		return -EINVAL;
4375 
4376 	auth_data->tries++;
4377 
4378 	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
4379 		sdata_info(sdata, "authentication with %pM timed out\n",
4380 			   auth_data->bss->bssid);
4381 
4382 		/*
4383 		 * Most likely AP is not in the range so remove the
4384 		 * bss struct for that AP.
4385 		 */
4386 		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4387 
4388 		return -ETIMEDOUT;
4389 	}
4390 
4391 	if (auth_data->algorithm == WLAN_AUTH_SAE)
4392 		prepare_tx_duration =
4393 			jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4394 
4395 	drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
4396 
4397 	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4398 		   auth_data->bss->bssid, auth_data->tries,
4399 		   IEEE80211_AUTH_MAX_TRIES);
4400 
4401 	auth_data->expected_transaction = 2;
4402 
4403 	if (auth_data->algorithm == WLAN_AUTH_SAE) {
4404 		trans = auth_data->sae_trans;
4405 		status = auth_data->sae_status;
4406 		auth_data->expected_transaction = trans;
4407 	}
4408 
4409 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4410 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4411 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
4412 
4413 	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4414 			    auth_data->data, auth_data->data_len,
4415 			    auth_data->bss->bssid,
4416 			    auth_data->bss->bssid, NULL, 0, 0,
4417 			    tx_flags);
4418 
4419 	if (tx_flags == 0) {
4420 		if (auth_data->algorithm == WLAN_AUTH_SAE)
4421 			auth_data->timeout = jiffies +
4422 				IEEE80211_AUTH_TIMEOUT_SAE;
4423 		else
4424 			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
4425 	} else {
4426 		auth_data->timeout =
4427 			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
4428 	}
4429 
4430 	auth_data->timeout_started = true;
4431 	run_again(sdata, auth_data->timeout);
4432 
4433 	return 0;
4434 }
4435 
ieee80211_do_assoc(struct ieee80211_sub_if_data * sdata)4436 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4437 {
4438 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4439 	struct ieee80211_local *local = sdata->local;
4440 
4441 	sdata_assert_lock(sdata);
4442 
4443 	assoc_data->tries++;
4444 	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
4445 		sdata_info(sdata, "association with %pM timed out\n",
4446 			   assoc_data->bss->bssid);
4447 
4448 		/*
4449 		 * Most likely AP is not in the range so remove the
4450 		 * bss struct for that AP.
4451 		 */
4452 		cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4453 
4454 		return -ETIMEDOUT;
4455 	}
4456 
4457 	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4458 		   assoc_data->bss->bssid, assoc_data->tries,
4459 		   IEEE80211_ASSOC_MAX_TRIES);
4460 	ieee80211_send_assoc(sdata);
4461 
4462 	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4463 		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
4464 		assoc_data->timeout_started = true;
4465 		run_again(sdata, assoc_data->timeout);
4466 	} else {
4467 		assoc_data->timeout =
4468 			round_jiffies_up(jiffies +
4469 					 IEEE80211_ASSOC_TIMEOUT_LONG);
4470 		assoc_data->timeout_started = true;
4471 		run_again(sdata, assoc_data->timeout);
4472 	}
4473 
4474 	return 0;
4475 }
4476 
ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data * sdata,__le16 fc,bool acked)4477 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4478 				  __le16 fc, bool acked)
4479 {
4480 	struct ieee80211_local *local = sdata->local;
4481 
4482 	sdata->u.mgd.status_fc = fc;
4483 	sdata->u.mgd.status_acked = acked;
4484 	sdata->u.mgd.status_received = true;
4485 
4486 	ieee80211_queue_work(&local->hw, &sdata->work);
4487 }
4488 
ieee80211_sta_work(struct ieee80211_sub_if_data * sdata)4489 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
4490 {
4491 	struct ieee80211_local *local = sdata->local;
4492 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4493 
4494 	sdata_lock(sdata);
4495 
4496 	if (ifmgd->status_received) {
4497 		__le16 fc = ifmgd->status_fc;
4498 		bool status_acked = ifmgd->status_acked;
4499 
4500 		ifmgd->status_received = false;
4501 		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
4502 			if (status_acked) {
4503 				if (ifmgd->auth_data->algorithm ==
4504 				    WLAN_AUTH_SAE)
4505 					ifmgd->auth_data->timeout =
4506 						jiffies +
4507 						IEEE80211_AUTH_TIMEOUT_SAE;
4508 				else
4509 					ifmgd->auth_data->timeout =
4510 						jiffies +
4511 						IEEE80211_AUTH_TIMEOUT_SHORT;
4512 				run_again(sdata, ifmgd->auth_data->timeout);
4513 			} else {
4514 				ifmgd->auth_data->timeout = jiffies - 1;
4515 			}
4516 			ifmgd->auth_data->timeout_started = true;
4517 		} else if (ifmgd->assoc_data &&
4518 			   (ieee80211_is_assoc_req(fc) ||
4519 			    ieee80211_is_reassoc_req(fc))) {
4520 			if (status_acked) {
4521 				ifmgd->assoc_data->timeout =
4522 					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
4523 				run_again(sdata, ifmgd->assoc_data->timeout);
4524 			} else {
4525 				ifmgd->assoc_data->timeout = jiffies - 1;
4526 			}
4527 			ifmgd->assoc_data->timeout_started = true;
4528 		}
4529 	}
4530 
4531 	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
4532 	    time_after(jiffies, ifmgd->auth_data->timeout)) {
4533 		if (ifmgd->auth_data->done) {
4534 			/*
4535 			 * ok ... we waited for assoc but userspace didn't,
4536 			 * so let's just kill the auth data
4537 			 */
4538 			ieee80211_destroy_auth_data(sdata, false);
4539 		} else if (ieee80211_auth(sdata)) {
4540 			u8 bssid[ETH_ALEN];
4541 			struct ieee80211_event event = {
4542 				.type = MLME_EVENT,
4543 				.u.mlme.data = AUTH_EVENT,
4544 				.u.mlme.status = MLME_TIMEOUT,
4545 			};
4546 
4547 			memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4548 
4549 			ieee80211_destroy_auth_data(sdata, false);
4550 
4551 			cfg80211_auth_timeout(sdata->dev, bssid);
4552 			drv_event_callback(sdata->local, sdata, &event);
4553 		}
4554 	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
4555 		run_again(sdata, ifmgd->auth_data->timeout);
4556 
4557 	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
4558 	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
4559 		if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
4560 		    ieee80211_do_assoc(sdata)) {
4561 			struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4562 			struct ieee80211_event event = {
4563 				.type = MLME_EVENT,
4564 				.u.mlme.data = ASSOC_EVENT,
4565 				.u.mlme.status = MLME_TIMEOUT,
4566 			};
4567 
4568 			ieee80211_destroy_assoc_data(sdata, false, false);
4569 			cfg80211_assoc_timeout(sdata->dev, bss);
4570 			drv_event_callback(sdata->local, sdata, &event);
4571 		}
4572 	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
4573 		run_again(sdata, ifmgd->assoc_data->timeout);
4574 
4575 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
4576 	    ifmgd->associated) {
4577 		u8 bssid[ETH_ALEN];
4578 		int max_tries;
4579 
4580 		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4581 
4582 		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4583 			max_tries = max_nullfunc_tries;
4584 		else
4585 			max_tries = max_probe_tries;
4586 
4587 		/* ACK received for nullfunc probing frame */
4588 		if (!ifmgd->probe_send_count)
4589 			ieee80211_reset_ap_probe(sdata);
4590 		else if (ifmgd->nullfunc_failed) {
4591 			if (ifmgd->probe_send_count < max_tries) {
4592 				mlme_dbg(sdata,
4593 					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4594 					 bssid, ifmgd->probe_send_count,
4595 					 max_tries);
4596 				ieee80211_mgd_probe_ap_send(sdata);
4597 			} else {
4598 				mlme_dbg(sdata,
4599 					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4600 					 bssid);
4601 				ieee80211_sta_connection_lost(sdata, bssid,
4602 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4603 					false);
4604 			}
4605 		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
4606 			run_again(sdata, ifmgd->probe_timeout);
4607 		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4608 			mlme_dbg(sdata,
4609 				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4610 				 bssid, probe_wait_ms);
4611 			ieee80211_sta_connection_lost(sdata, bssid,
4612 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4613 		} else if (ifmgd->probe_send_count < max_tries) {
4614 			mlme_dbg(sdata,
4615 				 "No probe response from AP %pM after %dms, try %d/%i\n",
4616 				 bssid, probe_wait_ms,
4617 				 ifmgd->probe_send_count, max_tries);
4618 			ieee80211_mgd_probe_ap_send(sdata);
4619 		} else {
4620 			/*
4621 			 * We actually lost the connection ... or did we?
4622 			 * Let's make sure!
4623 			 */
4624 			mlme_dbg(sdata,
4625 				 "No probe response from AP %pM after %dms, disconnecting.\n",
4626 				 bssid, probe_wait_ms);
4627 
4628 			ieee80211_sta_connection_lost(sdata, bssid,
4629 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4630 		}
4631 	}
4632 
4633 	sdata_unlock(sdata);
4634 }
4635 
ieee80211_sta_bcn_mon_timer(struct timer_list * t)4636 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
4637 {
4638 	struct ieee80211_sub_if_data *sdata =
4639 		from_timer(sdata, t, u.mgd.bcn_mon_timer);
4640 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4641 
4642 	if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4643 		return;
4644 
4645 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
4646 		return;
4647 
4648 	sdata->u.mgd.connection_loss = false;
4649 	ieee80211_queue_work(&sdata->local->hw,
4650 			     &sdata->u.mgd.beacon_connection_loss_work);
4651 }
4652 
ieee80211_sta_conn_mon_timer(struct timer_list * t)4653 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
4654 {
4655 	struct ieee80211_sub_if_data *sdata =
4656 		from_timer(sdata, t, u.mgd.conn_mon_timer);
4657 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4658 	struct ieee80211_local *local = sdata->local;
4659 	struct sta_info *sta;
4660 	unsigned long timeout;
4661 
4662 	if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4663 		return;
4664 
4665 	sta = sta_info_get(sdata, ifmgd->bssid);
4666 	if (!sta)
4667 		return;
4668 
4669 	timeout = sta->status_stats.last_ack;
4670 	if (time_before(sta->status_stats.last_ack, sta->rx_stats.last_rx))
4671 		timeout = sta->rx_stats.last_rx;
4672 	timeout += IEEE80211_CONNECTION_IDLE_TIME;
4673 
4674 	/* If timeout is after now, then update timer to fire at
4675 	 * the later date, but do not actually probe at this time.
4676 	 */
4677 	if (time_is_after_jiffies(timeout)) {
4678 		mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
4679 		return;
4680 	}
4681 
4682 	ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
4683 }
4684 
ieee80211_sta_monitor_work(struct work_struct * work)4685 static void ieee80211_sta_monitor_work(struct work_struct *work)
4686 {
4687 	struct ieee80211_sub_if_data *sdata =
4688 		container_of(work, struct ieee80211_sub_if_data,
4689 			     u.mgd.monitor_work);
4690 
4691 	ieee80211_mgd_probe_ap(sdata, false);
4692 }
4693 
ieee80211_restart_sta_timer(struct ieee80211_sub_if_data * sdata)4694 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4695 {
4696 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
4697 		__ieee80211_stop_poll(sdata);
4698 
4699 		/* let's probe the connection once */
4700 		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4701 			ieee80211_queue_work(&sdata->local->hw,
4702 					     &sdata->u.mgd.monitor_work);
4703 	}
4704 }
4705 
4706 #ifdef CONFIG_PM
ieee80211_mgd_quiesce(struct ieee80211_sub_if_data * sdata)4707 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4708 {
4709 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4710 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4711 
4712 	sdata_lock(sdata);
4713 
4714 	if (ifmgd->auth_data || ifmgd->assoc_data) {
4715 		const u8 *bssid = ifmgd->auth_data ?
4716 				ifmgd->auth_data->bss->bssid :
4717 				ifmgd->assoc_data->bss->bssid;
4718 
4719 		/*
4720 		 * If we are trying to authenticate / associate while suspending,
4721 		 * cfg80211 won't know and won't actually abort those attempts,
4722 		 * thus we need to do that ourselves.
4723 		 */
4724 		ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
4725 					       IEEE80211_STYPE_DEAUTH,
4726 					       WLAN_REASON_DEAUTH_LEAVING,
4727 					       false, frame_buf);
4728 		if (ifmgd->assoc_data)
4729 			ieee80211_destroy_assoc_data(sdata, false, true);
4730 		if (ifmgd->auth_data)
4731 			ieee80211_destroy_auth_data(sdata, false);
4732 		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4733 				      IEEE80211_DEAUTH_FRAME_LEN);
4734 	}
4735 
4736 	/* This is a bit of a hack - we should find a better and more generic
4737 	 * solution to this. Normally when suspending, cfg80211 will in fact
4738 	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4739 	 * auth (not so important) or assoc (this is the problem) process.
4740 	 *
4741 	 * As a consequence, it can happen that we are in the process of both
4742 	 * associating and suspending, and receive an association response
4743 	 * after cfg80211 has checked if it needs to disconnect, but before
4744 	 * we actually set the flag to drop incoming frames. This will then
4745 	 * cause the workqueue flush to process the association response in
4746 	 * the suspend, resulting in a successful association just before it
4747 	 * tries to remove the interface from the driver, which now though
4748 	 * has a channel context assigned ... this results in issues.
4749 	 *
4750 	 * To work around this (for now) simply deauth here again if we're
4751 	 * now connected.
4752 	 */
4753 	if (ifmgd->associated && !sdata->local->wowlan) {
4754 		u8 bssid[ETH_ALEN];
4755 		struct cfg80211_deauth_request req = {
4756 			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
4757 			.bssid = bssid,
4758 		};
4759 
4760 		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4761 		ieee80211_mgd_deauth(sdata, &req);
4762 	}
4763 
4764 	sdata_unlock(sdata);
4765 }
4766 
ieee80211_sta_restart(struct ieee80211_sub_if_data * sdata)4767 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4768 {
4769 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4770 
4771 	sdata_lock(sdata);
4772 	if (!ifmgd->associated) {
4773 		sdata_unlock(sdata);
4774 		return;
4775 	}
4776 
4777 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4778 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4779 		mlme_dbg(sdata, "driver requested disconnect after resume\n");
4780 		ieee80211_sta_connection_lost(sdata,
4781 					      ifmgd->associated->bssid,
4782 					      WLAN_REASON_UNSPECIFIED,
4783 					      true);
4784 		sdata_unlock(sdata);
4785 		return;
4786 	}
4787 	sdata_unlock(sdata);
4788 }
4789 #endif
4790 
4791 /* interface setup */
ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data * sdata)4792 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4793 {
4794 	struct ieee80211_if_managed *ifmgd;
4795 
4796 	ifmgd = &sdata->u.mgd;
4797 	INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
4798 	INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
4799 	INIT_WORK(&ifmgd->beacon_connection_loss_work,
4800 		  ieee80211_beacon_connection_loss_work);
4801 	INIT_WORK(&ifmgd->csa_connection_drop_work,
4802 		  ieee80211_csa_connection_drop_work);
4803 	INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
4804 	INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4805 			  ieee80211_tdls_peer_del_work);
4806 	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4807 	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4808 	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4809 	timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
4810 	INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4811 			  ieee80211_sta_handle_tspec_ac_params_wk);
4812 
4813 	ifmgd->flags = 0;
4814 	ifmgd->powersave = sdata->wdev.ps;
4815 	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4816 	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
4817 	ifmgd->p2p_noa_index = -1;
4818 
4819 	if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
4820 		ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4821 	else
4822 		ifmgd->req_smps = IEEE80211_SMPS_OFF;
4823 
4824 	/* Setup TDLS data */
4825 	spin_lock_init(&ifmgd->teardown_lock);
4826 	ifmgd->teardown_skb = NULL;
4827 	ifmgd->orig_teardown_skb = NULL;
4828 }
4829 
4830 /* scan finished notification */
ieee80211_mlme_notify_scan_completed(struct ieee80211_local * local)4831 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4832 {
4833 	struct ieee80211_sub_if_data *sdata;
4834 
4835 	/* Restart STA timers */
4836 	rcu_read_lock();
4837 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4838 		if (ieee80211_sdata_running(sdata))
4839 			ieee80211_restart_sta_timer(sdata);
4840 	}
4841 	rcu_read_unlock();
4842 }
4843 
ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data * sdata,struct cfg80211_bss * cbss)4844 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4845 				     struct cfg80211_bss *cbss)
4846 {
4847 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4848 	const u8 *ht_cap_ie, *vht_cap_ie;
4849 	const struct ieee80211_ht_cap *ht_cap;
4850 	const struct ieee80211_vht_cap *vht_cap;
4851 	u8 chains = 1;
4852 
4853 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4854 		return chains;
4855 
4856 	ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4857 	if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4858 		ht_cap = (void *)(ht_cap_ie + 2);
4859 		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4860 		/*
4861 		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4862 		 *	 "Tx Unequal Modulation Supported" fields.
4863 		 */
4864 	}
4865 
4866 	if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4867 		return chains;
4868 
4869 	vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4870 	if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4871 		u8 nss;
4872 		u16 tx_mcs_map;
4873 
4874 		vht_cap = (void *)(vht_cap_ie + 2);
4875 		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4876 		for (nss = 8; nss > 0; nss--) {
4877 			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4878 					IEEE80211_VHT_MCS_NOT_SUPPORTED)
4879 				break;
4880 		}
4881 		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4882 		chains = max(chains, nss);
4883 	}
4884 
4885 	return chains;
4886 }
4887 
4888 static bool
ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band * sband,const struct ieee80211_he_operation * he_op)4889 ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4890 				    const struct ieee80211_he_operation *he_op)
4891 {
4892 	const struct ieee80211_sta_he_cap *sta_he_cap =
4893 		ieee80211_get_he_sta_cap(sband);
4894 	u16 ap_min_req_set;
4895 	int i;
4896 
4897 	if (!sta_he_cap || !he_op)
4898 		return false;
4899 
4900 	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4901 
4902 	/* Need to go over for 80MHz, 160MHz and for 80+80 */
4903 	for (i = 0; i < 3; i++) {
4904 		const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4905 			&sta_he_cap->he_mcs_nss_supp;
4906 		u16 sta_mcs_map_rx =
4907 			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4908 		u16 sta_mcs_map_tx =
4909 			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4910 		u8 nss;
4911 		bool verified = true;
4912 
4913 		/*
4914 		 * For each band there is a maximum of 8 spatial streams
4915 		 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4916 		 * of 2 bits per NSS (1-8), with the values defined in enum
4917 		 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4918 		 * capabilities aren't less than the AP's minimum requirements
4919 		 * for this HE BSS per SS.
4920 		 * It is enough to find one such band that meets the reqs.
4921 		 */
4922 		for (nss = 8; nss > 0; nss--) {
4923 			u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4924 			u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4925 			u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4926 
4927 			if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4928 				continue;
4929 
4930 			/*
4931 			 * Make sure the HE AP doesn't require MCSs that aren't
4932 			 * supported by the client
4933 			 */
4934 			if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4935 			    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4936 			    (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4937 				verified = false;
4938 				break;
4939 			}
4940 		}
4941 
4942 		if (verified)
4943 			return true;
4944 	}
4945 
4946 	/* If here, STA doesn't meet AP's HE min requirements */
4947 	return false;
4948 }
4949 
ieee80211_prep_channel(struct ieee80211_sub_if_data * sdata,struct cfg80211_bss * cbss)4950 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4951 				  struct cfg80211_bss *cbss)
4952 {
4953 	struct ieee80211_local *local = sdata->local;
4954 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4955 	const struct ieee80211_ht_cap *ht_cap = NULL;
4956 	const struct ieee80211_ht_operation *ht_oper = NULL;
4957 	const struct ieee80211_vht_operation *vht_oper = NULL;
4958 	const struct ieee80211_he_operation *he_oper = NULL;
4959 	const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
4960 	struct ieee80211_supported_band *sband;
4961 	struct cfg80211_chan_def chandef;
4962 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
4963 	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
4964 	struct ieee80211_bss *bss = (void *)cbss->priv;
4965 	int ret;
4966 	u32 i;
4967 	bool have_80mhz;
4968 
4969 	sband = local->hw.wiphy->bands[cbss->channel->band];
4970 
4971 	ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4972 			  IEEE80211_STA_DISABLE_80P80MHZ |
4973 			  IEEE80211_STA_DISABLE_160MHZ);
4974 
4975 	/* disable HT/VHT/HE if we don't support them */
4976 	if (!sband->ht_cap.ht_supported && !is_6ghz) {
4977 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4978 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4979 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4980 	}
4981 
4982 	if (!sband->vht_cap.vht_supported && is_5ghz) {
4983 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4984 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4985 	}
4986 
4987 	if (!ieee80211_get_he_sta_cap(sband))
4988 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4989 
4990 	rcu_read_lock();
4991 
4992 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
4993 		const u8 *ht_oper_ie, *ht_cap_ie;
4994 
4995 		ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
4996 		if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4997 			ht_oper = (void *)(ht_oper_ie + 2);
4998 
4999 		ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
5000 		if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
5001 			ht_cap = (void *)(ht_cap_ie + 2);
5002 
5003 		if (!ht_cap) {
5004 			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5005 			ht_oper = NULL;
5006 		}
5007 	}
5008 
5009 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
5010 		const u8 *vht_oper_ie, *vht_cap;
5011 
5012 		vht_oper_ie = ieee80211_bss_get_ie(cbss,
5013 						   WLAN_EID_VHT_OPERATION);
5014 		if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
5015 			vht_oper = (void *)(vht_oper_ie + 2);
5016 		if (vht_oper && !ht_oper) {
5017 			vht_oper = NULL;
5018 			sdata_info(sdata,
5019 				   "AP advertised VHT without HT, disabling HT/VHT/HE\n");
5020 			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5021 			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5022 			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5023 		}
5024 
5025 		vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
5026 		if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
5027 			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5028 			vht_oper = NULL;
5029 		}
5030 	}
5031 
5032 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
5033 		const struct cfg80211_bss_ies *ies;
5034 		const u8 *he_oper_ie;
5035 
5036 		ies = rcu_dereference(cbss->ies);
5037 		he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
5038 						  ies->data, ies->len);
5039 		if (he_oper_ie &&
5040 		    he_oper_ie[1] >= ieee80211_he_oper_size(&he_oper_ie[3]))
5041 			he_oper = (void *)(he_oper_ie + 3);
5042 		else
5043 			he_oper = NULL;
5044 
5045 		if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
5046 			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5047 	}
5048 
5049 	/* Allow VHT if at least one channel on the sband supports 80 MHz */
5050 	have_80mhz = false;
5051 	for (i = 0; i < sband->n_channels; i++) {
5052 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5053 						IEEE80211_CHAN_NO_80MHZ))
5054 			continue;
5055 
5056 		have_80mhz = true;
5057 		break;
5058 	}
5059 
5060 	if (!have_80mhz)
5061 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5062 
5063 	if (sband->band == NL80211_BAND_S1GHZ) {
5064 		const u8 *s1g_oper_ie;
5065 
5066 		s1g_oper_ie = ieee80211_bss_get_ie(cbss,
5067 						   WLAN_EID_S1G_OPERATION);
5068 		if (s1g_oper_ie && s1g_oper_ie[1] >= sizeof(*s1g_oper))
5069 			s1g_oper = (void *)(s1g_oper_ie + 2);
5070 		else
5071 			sdata_info(sdata,
5072 				   "AP missing S1G operation element?\n");
5073 	}
5074 
5075 	ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
5076 						     cbss->channel,
5077 						     bss->vht_cap_info,
5078 						     ht_oper, vht_oper, he_oper,
5079 						     s1g_oper,
5080 						     &chandef, false);
5081 
5082 	sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
5083 				      local->rx_chains);
5084 
5085 	rcu_read_unlock();
5086 
5087 	if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
5088 		sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
5089 		return -EINVAL;
5090 	}
5091 
5092 	/* will change later if needed */
5093 	sdata->smps_mode = IEEE80211_SMPS_OFF;
5094 
5095 	mutex_lock(&local->mtx);
5096 	/*
5097 	 * If this fails (possibly due to channel context sharing
5098 	 * on incompatible channels, e.g. 80+80 and 160 sharing the
5099 	 * same control channel) try to use a smaller bandwidth.
5100 	 */
5101 	ret = ieee80211_vif_use_channel(sdata, &chandef,
5102 					IEEE80211_CHANCTX_SHARED);
5103 
5104 	/* don't downgrade for 5 and 10 MHz channels, though. */
5105 	if (chandef.width == NL80211_CHAN_WIDTH_5 ||
5106 	    chandef.width == NL80211_CHAN_WIDTH_10)
5107 		goto out;
5108 
5109 	while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
5110 		ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
5111 		ret = ieee80211_vif_use_channel(sdata, &chandef,
5112 						IEEE80211_CHANCTX_SHARED);
5113 	}
5114  out:
5115 	mutex_unlock(&local->mtx);
5116 	return ret;
5117 }
5118 
ieee80211_get_dtim(const struct cfg80211_bss_ies * ies,u8 * dtim_count,u8 * dtim_period)5119 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5120 			       u8 *dtim_count, u8 *dtim_period)
5121 {
5122 	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5123 	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5124 					 ies->len);
5125 	const struct ieee80211_tim_ie *tim = NULL;
5126 	const struct ieee80211_bssid_index *idx;
5127 	bool valid = tim_ie && tim_ie[1] >= 2;
5128 
5129 	if (valid)
5130 		tim = (void *)(tim_ie + 2);
5131 
5132 	if (dtim_count)
5133 		*dtim_count = valid ? tim->dtim_count : 0;
5134 
5135 	if (dtim_period)
5136 		*dtim_period = valid ? tim->dtim_period : 0;
5137 
5138 	/* Check if value is overridden by non-transmitted profile */
5139 	if (!idx_ie || idx_ie[1] < 3)
5140 		return valid;
5141 
5142 	idx = (void *)(idx_ie + 2);
5143 
5144 	if (dtim_count)
5145 		*dtim_count = idx->dtim_count;
5146 
5147 	if (dtim_period)
5148 		*dtim_period = idx->dtim_period;
5149 
5150 	return true;
5151 }
5152 
ieee80211_prep_connection(struct ieee80211_sub_if_data * sdata,struct cfg80211_bss * cbss,bool assoc,bool override)5153 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
5154 				     struct cfg80211_bss *cbss, bool assoc,
5155 				     bool override)
5156 {
5157 	struct ieee80211_local *local = sdata->local;
5158 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5159 	struct ieee80211_bss *bss = (void *)cbss->priv;
5160 	struct sta_info *new_sta = NULL;
5161 	struct ieee80211_supported_band *sband;
5162 	bool have_sta = false;
5163 	int err;
5164 
5165 	sband = local->hw.wiphy->bands[cbss->channel->band];
5166 
5167 	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5168 		return -EINVAL;
5169 
5170 	/* If a reconfig is happening, bail out */
5171 	if (local->in_reconfig)
5172 		return -EBUSY;
5173 
5174 	if (assoc) {
5175 		rcu_read_lock();
5176 		have_sta = sta_info_get(sdata, cbss->bssid);
5177 		rcu_read_unlock();
5178 	}
5179 
5180 	if (!have_sta) {
5181 		new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5182 		if (!new_sta)
5183 			return -ENOMEM;
5184 	}
5185 
5186 	/*
5187 	 * Set up the information for the new channel before setting the
5188 	 * new channel. We can't - completely race-free - change the basic
5189 	 * rates bitmap and the channel (sband) that it refers to, but if
5190 	 * we set it up before we at least avoid calling into the driver's
5191 	 * bss_info_changed() method with invalid information (since we do
5192 	 * call that from changing the channel - only for IDLE and perhaps
5193 	 * some others, but ...).
5194 	 *
5195 	 * So to avoid that, just set up all the new information before the
5196 	 * channel, but tell the driver to apply it only afterwards, since
5197 	 * it might need the new channel for that.
5198 	 */
5199 	if (new_sta) {
5200 		u32 rates = 0, basic_rates = 0;
5201 		bool have_higher_than_11mbit = false;
5202 		int min_rate = INT_MAX, min_rate_index = -1;
5203 		const struct cfg80211_bss_ies *ies;
5204 		int shift = ieee80211_vif_get_shift(&sdata->vif);
5205 
5206 		/* TODO: S1G Basic Rate Set is expressed elsewhere */
5207 		if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5208 			ieee80211_s1g_sta_rate_init(new_sta);
5209 			goto skip_rates;
5210 		}
5211 
5212 		ieee80211_get_rates(sband, bss->supp_rates,
5213 				    bss->supp_rates_len,
5214 				    &rates, &basic_rates,
5215 				    &have_higher_than_11mbit,
5216 				    &min_rate, &min_rate_index,
5217 				    shift);
5218 
5219 		/*
5220 		 * This used to be a workaround for basic rates missing
5221 		 * in the association response frame. Now that we no
5222 		 * longer use the basic rates from there, it probably
5223 		 * doesn't happen any more, but keep the workaround so
5224 		 * in case some *other* APs are buggy in different ways
5225 		 * we can connect -- with a warning.
5226 		 * Allow this workaround only in case the AP provided at least
5227 		 * one rate.
5228 		 */
5229 		if (min_rate_index < 0) {
5230 			sdata_info(sdata,
5231 				   "No legacy rates in association response\n");
5232 
5233 			sta_info_free(local, new_sta);
5234 			return -EINVAL;
5235 		} else if (!basic_rates) {
5236 			sdata_info(sdata,
5237 				   "No basic rates, using min rate instead\n");
5238 			basic_rates = BIT(min_rate_index);
5239 		}
5240 
5241 		if (rates)
5242 			new_sta->sta.supp_rates[cbss->channel->band] = rates;
5243 		else
5244 			sdata_info(sdata,
5245 				   "No rates found, keeping mandatory only\n");
5246 
5247 		sdata->vif.bss_conf.basic_rates = basic_rates;
5248 
5249 		/* cf. IEEE 802.11 9.2.12 */
5250 		if (cbss->channel->band == NL80211_BAND_2GHZ &&
5251 		    have_higher_than_11mbit)
5252 			sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5253 		else
5254 			sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5255 
5256 skip_rates:
5257 		memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5258 
5259 		/* set timing information */
5260 		sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
5261 		rcu_read_lock();
5262 		ies = rcu_dereference(cbss->beacon_ies);
5263 		if (ies) {
5264 			sdata->vif.bss_conf.sync_tsf = ies->tsf;
5265 			sdata->vif.bss_conf.sync_device_ts =
5266 				bss->device_ts_beacon;
5267 
5268 			ieee80211_get_dtim(ies,
5269 					   &sdata->vif.bss_conf.sync_dtim_count,
5270 					   NULL);
5271 		} else if (!ieee80211_hw_check(&sdata->local->hw,
5272 					       TIMING_BEACON_ONLY)) {
5273 			ies = rcu_dereference(cbss->proberesp_ies);
5274 			/* must be non-NULL since beacon IEs were NULL */
5275 			sdata->vif.bss_conf.sync_tsf = ies->tsf;
5276 			sdata->vif.bss_conf.sync_device_ts =
5277 				bss->device_ts_presp;
5278 			sdata->vif.bss_conf.sync_dtim_count = 0;
5279 		} else {
5280 			sdata->vif.bss_conf.sync_tsf = 0;
5281 			sdata->vif.bss_conf.sync_device_ts = 0;
5282 			sdata->vif.bss_conf.sync_dtim_count = 0;
5283 		}
5284 		rcu_read_unlock();
5285 	}
5286 
5287 	if (new_sta || override) {
5288 		err = ieee80211_prep_channel(sdata, cbss);
5289 		if (err) {
5290 			if (new_sta)
5291 				sta_info_free(local, new_sta);
5292 			return -EINVAL;
5293 		}
5294 	}
5295 
5296 	if (new_sta) {
5297 		/*
5298 		 * tell driver about BSSID, basic rates and timing
5299 		 * this was set up above, before setting the channel
5300 		 */
5301 		ieee80211_bss_info_change_notify(sdata,
5302 			BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5303 			BSS_CHANGED_BEACON_INT);
5304 
5305 		if (assoc)
5306 			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
5307 
5308 		err = sta_info_insert(new_sta);
5309 		new_sta = NULL;
5310 		if (err) {
5311 			sdata_info(sdata,
5312 				   "failed to insert STA entry for the AP (error %d)\n",
5313 				   err);
5314 			return err;
5315 		}
5316 	} else
5317 		WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
5318 
5319 	/* Cancel scan to ensure that nothing interferes with connection */
5320 	if (local->scanning)
5321 		ieee80211_scan_cancel(local);
5322 
5323 	return 0;
5324 }
5325 
5326 /* config hooks */
ieee80211_mgd_auth(struct ieee80211_sub_if_data * sdata,struct cfg80211_auth_request * req)5327 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5328 		       struct cfg80211_auth_request *req)
5329 {
5330 	struct ieee80211_local *local = sdata->local;
5331 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5332 	struct ieee80211_mgd_auth_data *auth_data;
5333 	u16 auth_alg;
5334 	int err;
5335 	bool cont_auth;
5336 
5337 	/* prepare auth data structure */
5338 
5339 	switch (req->auth_type) {
5340 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
5341 		auth_alg = WLAN_AUTH_OPEN;
5342 		break;
5343 	case NL80211_AUTHTYPE_SHARED_KEY:
5344 		if (fips_enabled)
5345 			return -EOPNOTSUPP;
5346 		auth_alg = WLAN_AUTH_SHARED_KEY;
5347 		break;
5348 	case NL80211_AUTHTYPE_FT:
5349 		auth_alg = WLAN_AUTH_FT;
5350 		break;
5351 	case NL80211_AUTHTYPE_NETWORK_EAP:
5352 		auth_alg = WLAN_AUTH_LEAP;
5353 		break;
5354 	case NL80211_AUTHTYPE_SAE:
5355 		auth_alg = WLAN_AUTH_SAE;
5356 		break;
5357 	case NL80211_AUTHTYPE_FILS_SK:
5358 		auth_alg = WLAN_AUTH_FILS_SK;
5359 		break;
5360 	case NL80211_AUTHTYPE_FILS_SK_PFS:
5361 		auth_alg = WLAN_AUTH_FILS_SK_PFS;
5362 		break;
5363 	case NL80211_AUTHTYPE_FILS_PK:
5364 		auth_alg = WLAN_AUTH_FILS_PK;
5365 		break;
5366 	default:
5367 		return -EOPNOTSUPP;
5368 	}
5369 
5370 	if (ifmgd->assoc_data)
5371 		return -EBUSY;
5372 
5373 	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
5374 			    req->ie_len, GFP_KERNEL);
5375 	if (!auth_data)
5376 		return -ENOMEM;
5377 
5378 	auth_data->bss = req->bss;
5379 
5380 	if (req->auth_data_len >= 4) {
5381 		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5382 			__le16 *pos = (__le16 *) req->auth_data;
5383 
5384 			auth_data->sae_trans = le16_to_cpu(pos[0]);
5385 			auth_data->sae_status = le16_to_cpu(pos[1]);
5386 		}
5387 		memcpy(auth_data->data, req->auth_data + 4,
5388 		       req->auth_data_len - 4);
5389 		auth_data->data_len += req->auth_data_len - 4;
5390 	}
5391 
5392 	/* Check if continuing authentication or trying to authenticate with the
5393 	 * same BSS that we were in the process of authenticating with and avoid
5394 	 * removal and re-addition of the STA entry in
5395 	 * ieee80211_prep_connection().
5396 	 */
5397 	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5398 
5399 	if (req->ie && req->ie_len) {
5400 		memcpy(&auth_data->data[auth_data->data_len],
5401 		       req->ie, req->ie_len);
5402 		auth_data->data_len += req->ie_len;
5403 	}
5404 
5405 	if (req->key && req->key_len) {
5406 		auth_data->key_len = req->key_len;
5407 		auth_data->key_idx = req->key_idx;
5408 		memcpy(auth_data->key, req->key, req->key_len);
5409 	}
5410 
5411 	auth_data->algorithm = auth_alg;
5412 
5413 	/* try to authenticate/probe */
5414 
5415 	if (ifmgd->auth_data) {
5416 		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5417 			auth_data->peer_confirmed =
5418 				ifmgd->auth_data->peer_confirmed;
5419 		}
5420 		ieee80211_destroy_auth_data(sdata, cont_auth);
5421 	}
5422 
5423 	/* prep auth_data so we don't go into idle on disassoc */
5424 	ifmgd->auth_data = auth_data;
5425 
5426 	/* If this is continuation of an ongoing SAE authentication exchange
5427 	 * (i.e., request to send SAE Confirm) and the peer has already
5428 	 * confirmed, mark authentication completed since we are about to send
5429 	 * out SAE Confirm.
5430 	 */
5431 	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5432 	    auth_data->peer_confirmed && auth_data->sae_trans == 2)
5433 		ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5434 
5435 	if (ifmgd->associated) {
5436 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5437 
5438 		sdata_info(sdata,
5439 			   "disconnect from AP %pM for new auth to %pM\n",
5440 			   ifmgd->associated->bssid, req->bss->bssid);
5441 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5442 				       WLAN_REASON_UNSPECIFIED,
5443 				       false, frame_buf);
5444 
5445 		ieee80211_report_disconnect(sdata, frame_buf,
5446 					    sizeof(frame_buf), true,
5447 					    WLAN_REASON_UNSPECIFIED);
5448 	}
5449 
5450 	sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
5451 
5452 	err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
5453 	if (err)
5454 		goto err_clear;
5455 
5456 	err = ieee80211_auth(sdata);
5457 	if (err) {
5458 		sta_info_destroy_addr(sdata, req->bss->bssid);
5459 		goto err_clear;
5460 	}
5461 
5462 	/* hold our own reference */
5463 	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
5464 	return 0;
5465 
5466  err_clear:
5467 	eth_zero_addr(ifmgd->bssid);
5468 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5469 	ifmgd->auth_data = NULL;
5470 	mutex_lock(&sdata->local->mtx);
5471 	ieee80211_vif_release_channel(sdata);
5472 	mutex_unlock(&sdata->local->mtx);
5473 	kfree(auth_data);
5474 	return err;
5475 }
5476 
ieee80211_mgd_assoc(struct ieee80211_sub_if_data * sdata,struct cfg80211_assoc_request * req)5477 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5478 			struct cfg80211_assoc_request *req)
5479 {
5480 	bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
5481 	bool is_5ghz = req->bss->channel->band == NL80211_BAND_5GHZ;
5482 	struct ieee80211_local *local = sdata->local;
5483 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5484 	struct ieee80211_bss *bss = (void *)req->bss->priv;
5485 	struct ieee80211_mgd_assoc_data *assoc_data;
5486 	const struct cfg80211_bss_ies *beacon_ies;
5487 	struct ieee80211_supported_band *sband;
5488 	const u8 *ssidie, *ht_ie, *vht_ie;
5489 	int i, err;
5490 	bool override = false;
5491 
5492 	assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5493 	if (!assoc_data)
5494 		return -ENOMEM;
5495 
5496 	rcu_read_lock();
5497 	ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
5498 	if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
5499 		rcu_read_unlock();
5500 		kfree(assoc_data);
5501 		return -EINVAL;
5502 	}
5503 	memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5504 	assoc_data->ssid_len = ssidie[1];
5505 	rcu_read_unlock();
5506 
5507 	if (ifmgd->associated) {
5508 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5509 
5510 		sdata_info(sdata,
5511 			   "disconnect from AP %pM for new assoc to %pM\n",
5512 			   ifmgd->associated->bssid, req->bss->bssid);
5513 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5514 				       WLAN_REASON_UNSPECIFIED,
5515 				       false, frame_buf);
5516 
5517 		ieee80211_report_disconnect(sdata, frame_buf,
5518 					    sizeof(frame_buf), true,
5519 					    WLAN_REASON_UNSPECIFIED);
5520 	}
5521 
5522 	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5523 		err = -EBUSY;
5524 		goto err_free;
5525 	}
5526 
5527 	if (ifmgd->assoc_data) {
5528 		err = -EBUSY;
5529 		goto err_free;
5530 	}
5531 
5532 	if (ifmgd->auth_data) {
5533 		bool match;
5534 
5535 		/* keep sta info, bssid if matching */
5536 		match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
5537 		ieee80211_destroy_auth_data(sdata, match);
5538 	}
5539 
5540 	/* prepare assoc data */
5541 
5542 	ifmgd->beacon_crc_valid = false;
5543 
5544 	assoc_data->wmm = bss->wmm_used &&
5545 			  (local->hw.queues >= IEEE80211_NUM_ACS);
5546 
5547 	/*
5548 	 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5549 	 * We still associate in non-HT mode (11a/b/g) if any one of these
5550 	 * ciphers is configured as pairwise.
5551 	 * We can set this to true for non-11n hardware, that'll be checked
5552 	 * separately along with the peer capabilities.
5553 	 */
5554 	for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5555 		if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5556 		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5557 		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5558 			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5559 			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5560 			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5561 			netdev_info(sdata->dev,
5562 				    "disabling HT/VHT/HE due to WEP/TKIP use\n");
5563 		}
5564 	}
5565 
5566 	sband = local->hw.wiphy->bands[req->bss->channel->band];
5567 
5568 	/* also disable HT/VHT/HE if the AP doesn't use WMM */
5569 	if (!bss->wmm_used) {
5570 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5571 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5572 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5573 		netdev_info(sdata->dev,
5574 			    "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
5575 	}
5576 
5577 	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5578 	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5579 	       sizeof(ifmgd->ht_capa_mask));
5580 
5581 	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5582 	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5583 	       sizeof(ifmgd->vht_capa_mask));
5584 
5585 	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
5586 	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
5587 	       sizeof(ifmgd->s1g_capa_mask));
5588 
5589 	if (req->ie && req->ie_len) {
5590 		memcpy(assoc_data->ie, req->ie, req->ie_len);
5591 		assoc_data->ie_len = req->ie_len;
5592 	}
5593 
5594 	if (req->fils_kek) {
5595 		/* should already be checked in cfg80211 - so warn */
5596 		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5597 			err = -EINVAL;
5598 			goto err_free;
5599 		}
5600 		memcpy(assoc_data->fils_kek, req->fils_kek,
5601 		       req->fils_kek_len);
5602 		assoc_data->fils_kek_len = req->fils_kek_len;
5603 	}
5604 
5605 	if (req->fils_nonces)
5606 		memcpy(assoc_data->fils_nonces, req->fils_nonces,
5607 		       2 * FILS_NONCE_LEN);
5608 
5609 	assoc_data->bss = req->bss;
5610 
5611 	if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5612 		if (ifmgd->powersave)
5613 			sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
5614 		else
5615 			sdata->smps_mode = IEEE80211_SMPS_OFF;
5616 	} else
5617 		sdata->smps_mode = ifmgd->req_smps;
5618 
5619 	assoc_data->capability = req->bss->capability;
5620 	assoc_data->supp_rates = bss->supp_rates;
5621 	assoc_data->supp_rates_len = bss->supp_rates_len;
5622 
5623 	rcu_read_lock();
5624 	ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5625 	if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5626 		assoc_data->ap_ht_param =
5627 			((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
5628 	else if (!is_6ghz)
5629 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5630 	vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5631 	if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5632 		memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5633 		       sizeof(struct ieee80211_vht_cap));
5634 	else if (is_5ghz)
5635 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5636 				IEEE80211_STA_DISABLE_HE;
5637 	rcu_read_unlock();
5638 
5639 	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
5640 		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
5641 	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5642 		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5643 
5644 	if (bss->wmm_used && bss->uapsd_supported &&
5645 	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
5646 		assoc_data->uapsd = true;
5647 		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5648 	} else {
5649 		assoc_data->uapsd = false;
5650 		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5651 	}
5652 
5653 	if (req->prev_bssid)
5654 		memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
5655 
5656 	if (req->use_mfp) {
5657 		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5658 		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5659 	} else {
5660 		ifmgd->mfp = IEEE80211_MFP_DISABLED;
5661 		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5662 	}
5663 
5664 	if (req->flags & ASSOC_REQ_USE_RRM)
5665 		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5666 	else
5667 		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5668 
5669 	if (req->crypto.control_port)
5670 		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5671 	else
5672 		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5673 
5674 	sdata->control_port_protocol = req->crypto.control_port_ethertype;
5675 	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
5676 	sdata->control_port_over_nl80211 =
5677 					req->crypto.control_port_over_nl80211;
5678 	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
5679 	sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5680 							sdata->vif.type);
5681 
5682 	/* kick off associate process */
5683 
5684 	ifmgd->assoc_data = assoc_data;
5685 	ifmgd->dtim_period = 0;
5686 	ifmgd->have_beacon = false;
5687 
5688 	/* override HT/VHT configuration only if the AP and we support it */
5689 	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5690 		struct ieee80211_sta_ht_cap sta_ht_cap;
5691 
5692 		if (req->flags & ASSOC_REQ_DISABLE_HT)
5693 			override = true;
5694 
5695 		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5696 		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5697 
5698 		/* check for 40 MHz disable override */
5699 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5700 		    sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5701 		    !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5702 			override = true;
5703 
5704 		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5705 		    req->flags & ASSOC_REQ_DISABLE_VHT)
5706 			override = true;
5707 	}
5708 
5709 	if (req->flags & ASSOC_REQ_DISABLE_HT) {
5710 		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5711 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5712 		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5713 	}
5714 
5715 	if (req->flags & ASSOC_REQ_DISABLE_VHT)
5716 		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5717 
5718 	err = ieee80211_prep_connection(sdata, req->bss, true, override);
5719 	if (err)
5720 		goto err_clear;
5721 
5722 	rcu_read_lock();
5723 	beacon_ies = rcu_dereference(req->bss->beacon_ies);
5724 
5725 	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
5726 	    !beacon_ies) {
5727 		/*
5728 		 * Wait up to one beacon interval ...
5729 		 * should this be more if we miss one?
5730 		 */
5731 		sdata_info(sdata, "waiting for beacon from %pM\n",
5732 			   ifmgd->bssid);
5733 		assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
5734 		assoc_data->timeout_started = true;
5735 		assoc_data->need_beacon = true;
5736 	} else if (beacon_ies) {
5737 		const struct element *elem;
5738 		u8 dtim_count = 0;
5739 
5740 		ieee80211_get_dtim(beacon_ies, &dtim_count,
5741 				   &ifmgd->dtim_period);
5742 
5743 		ifmgd->have_beacon = true;
5744 		assoc_data->timeout = jiffies;
5745 		assoc_data->timeout_started = true;
5746 
5747 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
5748 			sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5749 			sdata->vif.bss_conf.sync_device_ts =
5750 				bss->device_ts_beacon;
5751 			sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5752 		}
5753 
5754 		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5755 					      beacon_ies->data, beacon_ies->len);
5756 		if (elem && elem->datalen >= 3)
5757 			sdata->vif.bss_conf.profile_periodicity = elem->data[2];
5758 		else
5759 			sdata->vif.bss_conf.profile_periodicity = 0;
5760 
5761 		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5762 					  beacon_ies->data, beacon_ies->len);
5763 		if (elem && elem->datalen >= 11 &&
5764 		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
5765 			sdata->vif.bss_conf.ema_ap = true;
5766 		else
5767 			sdata->vif.bss_conf.ema_ap = false;
5768 	} else {
5769 		assoc_data->timeout = jiffies;
5770 		assoc_data->timeout_started = true;
5771 	}
5772 	rcu_read_unlock();
5773 
5774 	run_again(sdata, assoc_data->timeout);
5775 
5776 	if (bss->corrupt_data) {
5777 		char *corrupt_type = "data";
5778 		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5779 			if (bss->corrupt_data &
5780 					IEEE80211_BSS_CORRUPT_PROBE_RESP)
5781 				corrupt_type = "beacon and probe response";
5782 			else
5783 				corrupt_type = "beacon";
5784 		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5785 			corrupt_type = "probe response";
5786 		sdata_info(sdata, "associating with AP with corrupt %s\n",
5787 			   corrupt_type);
5788 	}
5789 
5790 	return 0;
5791  err_clear:
5792 	eth_zero_addr(ifmgd->bssid);
5793 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5794 	ifmgd->assoc_data = NULL;
5795  err_free:
5796 	kfree(assoc_data);
5797 	return err;
5798 }
5799 
ieee80211_mgd_deauth(struct ieee80211_sub_if_data * sdata,struct cfg80211_deauth_request * req)5800 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
5801 			 struct cfg80211_deauth_request *req)
5802 {
5803 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5804 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5805 	bool tx = !req->local_state_change;
5806 
5807 	if (ifmgd->auth_data &&
5808 	    ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5809 		sdata_info(sdata,
5810 			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5811 			   req->bssid, req->reason_code,
5812 			   ieee80211_get_reason_code_string(req->reason_code));
5813 
5814 		drv_mgd_prepare_tx(sdata->local, sdata, 0);
5815 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
5816 					       IEEE80211_STYPE_DEAUTH,
5817 					       req->reason_code, tx,
5818 					       frame_buf);
5819 		ieee80211_destroy_auth_data(sdata, false);
5820 		ieee80211_report_disconnect(sdata, frame_buf,
5821 					    sizeof(frame_buf), true,
5822 					    req->reason_code);
5823 
5824 		return 0;
5825 	}
5826 
5827 	if (ifmgd->assoc_data &&
5828 	    ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5829 		sdata_info(sdata,
5830 			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
5831 			   req->bssid, req->reason_code,
5832 			   ieee80211_get_reason_code_string(req->reason_code));
5833 
5834 		drv_mgd_prepare_tx(sdata->local, sdata, 0);
5835 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
5836 					       IEEE80211_STYPE_DEAUTH,
5837 					       req->reason_code, tx,
5838 					       frame_buf);
5839 		ieee80211_destroy_assoc_data(sdata, false, true);
5840 		ieee80211_report_disconnect(sdata, frame_buf,
5841 					    sizeof(frame_buf), true,
5842 					    req->reason_code);
5843 		return 0;
5844 	}
5845 
5846 	if (ifmgd->associated &&
5847 	    ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
5848 		sdata_info(sdata,
5849 			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5850 			   req->bssid, req->reason_code,
5851 			   ieee80211_get_reason_code_string(req->reason_code));
5852 
5853 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5854 				       req->reason_code, tx, frame_buf);
5855 		ieee80211_report_disconnect(sdata, frame_buf,
5856 					    sizeof(frame_buf), true,
5857 					    req->reason_code);
5858 		return 0;
5859 	}
5860 
5861 	return -ENOTCONN;
5862 }
5863 
ieee80211_mgd_disassoc(struct ieee80211_sub_if_data * sdata,struct cfg80211_disassoc_request * req)5864 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
5865 			   struct cfg80211_disassoc_request *req)
5866 {
5867 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5868 	u8 bssid[ETH_ALEN];
5869 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5870 
5871 	/*
5872 	 * cfg80211 should catch this ... but it's racy since
5873 	 * we can receive a disassoc frame, process it, hand it
5874 	 * to cfg80211 while that's in a locked section already
5875 	 * trying to tell us that the user wants to disconnect.
5876 	 */
5877 	if (ifmgd->associated != req->bss)
5878 		return -ENOLINK;
5879 
5880 	sdata_info(sdata,
5881 		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
5882 		   req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
5883 
5884 	memcpy(bssid, req->bss->bssid, ETH_ALEN);
5885 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5886 			       req->reason_code, !req->local_state_change,
5887 			       frame_buf);
5888 
5889 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5890 				    req->reason_code);
5891 
5892 	return 0;
5893 }
5894 
ieee80211_mgd_stop(struct ieee80211_sub_if_data * sdata)5895 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
5896 {
5897 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5898 
5899 	/*
5900 	 * Make sure some work items will not run after this,
5901 	 * they will not do anything but might not have been
5902 	 * cancelled when disconnecting.
5903 	 */
5904 	cancel_work_sync(&ifmgd->monitor_work);
5905 	cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5906 	cancel_work_sync(&ifmgd->request_smps_work);
5907 	cancel_work_sync(&ifmgd->csa_connection_drop_work);
5908 	cancel_work_sync(&ifmgd->chswitch_work);
5909 	cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
5910 
5911 	sdata_lock(sdata);
5912 	if (ifmgd->assoc_data) {
5913 		struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
5914 		ieee80211_destroy_assoc_data(sdata, false, false);
5915 		cfg80211_assoc_timeout(sdata->dev, bss);
5916 	}
5917 	if (ifmgd->auth_data)
5918 		ieee80211_destroy_auth_data(sdata, false);
5919 	spin_lock_bh(&ifmgd->teardown_lock);
5920 	if (ifmgd->teardown_skb) {
5921 		kfree_skb(ifmgd->teardown_skb);
5922 		ifmgd->teardown_skb = NULL;
5923 		ifmgd->orig_teardown_skb = NULL;
5924 	}
5925 	kfree(ifmgd->assoc_req_ies);
5926 	ifmgd->assoc_req_ies = NULL;
5927 	ifmgd->assoc_req_ies_len = 0;
5928 	spin_unlock_bh(&ifmgd->teardown_lock);
5929 	del_timer_sync(&ifmgd->timer);
5930 	sdata_unlock(sdata);
5931 }
5932 
ieee80211_cqm_rssi_notify(struct ieee80211_vif * vif,enum nl80211_cqm_rssi_threshold_event rssi_event,s32 rssi_level,gfp_t gfp)5933 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5934 			       enum nl80211_cqm_rssi_threshold_event rssi_event,
5935 			       s32 rssi_level,
5936 			       gfp_t gfp)
5937 {
5938 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5939 
5940 	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
5941 
5942 	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
5943 }
5944 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
5945 
ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif * vif,gfp_t gfp)5946 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5947 {
5948 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5949 
5950 	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5951 
5952 	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5953 }
5954 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
5955