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