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