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