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