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