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