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