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