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