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