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