xref: /linux/net/mac80211/mlme.c (revision fafb66e5903c2bcfc7b7e259042a8282f18a6faa)
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 - 2026 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 <linux/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 #include <kunit/static_stub.h>
35 
36 #define IEEE80211_AUTH_TIMEOUT		(HZ / 5)
37 #define IEEE80211_AUTH_TIMEOUT_LONG	(HZ / 2)
38 #define IEEE80211_AUTH_TIMEOUT_SHORT	(HZ / 10)
39 #define IEEE80211_AUTH_TIMEOUT_SAE	(HZ * 2)
40 #define IEEE80211_AUTH_MAX_TRIES	3
41 #define IEEE80211_AUTH_WAIT_ASSOC	(HZ * 5)
42 #define IEEE80211_AUTH_WAIT_SAE_RETRY	(HZ * 2)
43 #define IEEE80211_ASSOC_TIMEOUT		(HZ / 5)
44 #define IEEE80211_ASSOC_TIMEOUT_LONG	(HZ / 2)
45 #define IEEE80211_ASSOC_TIMEOUT_SHORT	(HZ / 10)
46 #define IEEE80211_ASSOC_MAX_TRIES	3
47 
48 #define IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS (100 * USEC_PER_MSEC)
49 #define IEEE80211_ADV_TTLM_ST_UNDERFLOW 0xff00
50 
51 #define IEEE80211_NEG_TTLM_REQ_TIMEOUT (HZ / 5)
52 
53 static int max_nullfunc_tries = 2;
54 module_param(max_nullfunc_tries, int, 0644);
55 MODULE_PARM_DESC(max_nullfunc_tries,
56 		 "Maximum nullfunc tx tries before disconnecting (reason 4).");
57 
58 static int max_probe_tries = 5;
59 module_param(max_probe_tries, int, 0644);
60 MODULE_PARM_DESC(max_probe_tries,
61 		 "Maximum probe tries before disconnecting (reason 4).");
62 
63 /*
64  * Beacon loss timeout is calculated as N frames times the
65  * advertised beacon interval.  This may need to be somewhat
66  * higher than what hardware might detect to account for
67  * delays in the host processing frames. But since we also
68  * probe on beacon miss before declaring the connection lost
69  * default to what we want.
70  */
71 static int beacon_loss_count = 7;
72 module_param(beacon_loss_count, int, 0644);
73 MODULE_PARM_DESC(beacon_loss_count,
74 		 "Number of beacon intervals before we decide beacon was lost.");
75 
76 /*
77  * Time the connection can be idle before we probe
78  * it to see if we can still talk to the AP.
79  */
80 #define IEEE80211_CONNECTION_IDLE_TIME	(30 * HZ)
81 /*
82  * Time we wait for a probe response after sending
83  * a probe request because of beacon loss or for
84  * checking the connection still works.
85  */
86 static int probe_wait_ms = 500;
87 module_param(probe_wait_ms, int, 0644);
88 MODULE_PARM_DESC(probe_wait_ms,
89 		 "Maximum time(ms) to wait for probe response"
90 		 " before disconnecting (reason 4).");
91 
92 /*
93  * How many Beacon frames need to have been used in average signal strength
94  * before starting to indicate signal change events.
95  */
96 #define IEEE80211_SIGNAL_AVE_MIN_COUNT	4
97 
98 /*
99  * We can have multiple work items (and connection probing)
100  * scheduling this timer, but we need to take care to only
101  * reschedule it when it should fire _earlier_ than it was
102  * asked for before, or if it's not pending right now. This
103  * function ensures that. Note that it then is required to
104  * run this function for all timeouts after the first one
105  * has happened -- the work that runs from this timer will
106  * do that.
107  */
108 static void run_again(struct ieee80211_sub_if_data *sdata,
109 		      unsigned long timeout)
110 {
111 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
112 
113 	if (!timer_pending(&sdata->u.mgd.timer) ||
114 	    time_before(timeout, sdata->u.mgd.timer.expires))
115 		mod_timer(&sdata->u.mgd.timer, timeout);
116 }
117 
118 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
119 {
120 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
121 		return;
122 
123 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
124 		return;
125 
126 	mod_timer(&sdata->u.mgd.bcn_mon_timer,
127 		  round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
128 }
129 
130 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
131 {
132 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
133 
134 	if (unlikely(!ifmgd->associated))
135 		return;
136 
137 	if (ifmgd->probe_send_count)
138 		ifmgd->probe_send_count = 0;
139 
140 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
141 		return;
142 
143 	mod_timer(&ifmgd->conn_mon_timer,
144 		  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
145 }
146 
147 static int ecw2cw(int ecw)
148 {
149 	return (1 << ecw) - 1;
150 }
151 
152 static bool ieee80211_chandef_usable(struct ieee80211_sub_if_data *sdata,
153 				     const struct cfg80211_chan_def *chandef,
154 				     u32 prohibited_flags)
155 {
156 	if (!cfg80211_chandef_usable(sdata->local->hw.wiphy,
157 				     chandef, prohibited_flags))
158 		return false;
159 
160 	if (chandef->punctured &&
161 	    ieee80211_hw_check(&sdata->local->hw, DISALLOW_PUNCTURING))
162 		return false;
163 
164 	return true;
165 }
166 
167 struct ieee80211_determine_ap_chan_data {
168 	/* input data */
169 	struct ieee80211_channel *channel;
170 	const struct ieee802_11_elems *elems;
171 	const struct ieee80211_conn_settings *conn;
172 	u32 vht_cap_info;
173 	bool ignore_ht_channel_mismatch;
174 	const struct cfg80211_chan_def *cur_chandef;
175 	bool cur_dbe_used;
176 
177 	/* target chandef is filled in */
178 	struct cfg80211_chan_def *chandef;
179 };
180 
181 struct ieee80211_determine_ap_chan_output {
182 	/* filled to indicate UHR DBE was used */
183 	bool dbe_used;
184 	/* and need to know non-DBE width */
185 	enum nl80211_chan_width non_dbe_width;
186 };
187 
188 static enum ieee80211_conn_mode
189 ieee80211_determine_ap_chan(struct ieee80211_sub_if_data *sdata,
190 			    const struct ieee80211_determine_ap_chan_data *data,
191 			    struct ieee80211_determine_ap_chan_output *out)
192 {
193 	bool ignore_ht_channel_mismatch = data->ignore_ht_channel_mismatch;
194 	const struct ieee802_11_elems *elems = data->elems;
195 	const struct ieee80211_ht_operation *ht_oper = elems->ht_operation;
196 	const struct ieee80211_vht_operation *vht_oper = elems->vht_operation;
197 	const struct ieee80211_he_operation *he_oper = elems->he_operation;
198 	const struct ieee80211_eht_operation *eht_oper = elems->eht_operation;
199 	const struct ieee80211_uhr_operation *uhr_oper = elems->uhr_operation;
200 	const struct ieee80211_conn_settings *conn = data->conn;
201 	struct ieee80211_channel *channel = data->channel;
202 	struct cfg80211_chan_def *chandef = data->chandef;
203 	struct ieee80211_supported_band *sband =
204 		sdata->local->hw.wiphy->bands[channel->band];
205 	struct cfg80211_chan_def vht_chandef;
206 	bool no_vht = false;
207 	u32 ht_cfreq;
208 
209 	memset(out, 0, sizeof(*out));
210 
211 	if (ieee80211_hw_check(&sdata->local->hw, STRICT))
212 		ignore_ht_channel_mismatch = false;
213 
214 	*chandef = (struct cfg80211_chan_def) {
215 		.chan = channel,
216 		.width = NL80211_CHAN_WIDTH_20_NOHT,
217 		.center_freq1 = channel->center_freq,
218 		.freq1_offset = channel->freq_offset,
219 	};
220 
221 	/* get special S1G case out of the way */
222 	if (sband->band == NL80211_BAND_S1GHZ) {
223 		if (!ieee80211_chandef_s1g_oper(sdata->local, elems->s1g_oper,
224 						chandef)) {
225 			/* Fallback to default 1MHz */
226 			chandef->width = NL80211_CHAN_WIDTH_1;
227 			chandef->s1g_primary_2mhz = false;
228 		}
229 
230 		return IEEE80211_CONN_MODE_S1G;
231 	}
232 
233 	/* get special 6 GHz case out of the way */
234 	if (sband->band == NL80211_BAND_6GHZ) {
235 		enum ieee80211_conn_mode mode = IEEE80211_CONN_MODE_HIGHEST;
236 
237 		/* this is an error */
238 		if (conn->mode < IEEE80211_CONN_MODE_HE)
239 			return IEEE80211_CONN_MODE_LEGACY;
240 
241 		if (!elems->he_6ghz_capa || !elems->he_cap) {
242 			sdata_info(sdata,
243 				   "HE 6 GHz AP is missing HE/HE 6 GHz band capability\n");
244 			return IEEE80211_CONN_MODE_LEGACY;
245 		}
246 
247 		if (!eht_oper || !elems->eht_cap) {
248 			eht_oper = NULL;
249 			mode = IEEE80211_CONN_MODE_HE;
250 		}
251 
252 		if (!ieee80211_chandef_he_6ghz_oper(sdata->local, he_oper,
253 						    eht_oper, chandef)) {
254 			sdata_info(sdata, "bad HE/EHT 6 GHz operation\n");
255 			return IEEE80211_CONN_MODE_LEGACY;
256 		}
257 
258 		if (eht_oper && ieee80211_hw_check(&sdata->local->hw, STRICT)) {
259 			struct cfg80211_chan_def he_chandef = *chandef;
260 
261 			if (!ieee80211_chandef_he_6ghz_oper(sdata->local,
262 							    he_oper, NULL,
263 							    &he_chandef)) {
264 				sdata_info(sdata,
265 					   "bad HE operation in EHT AP\n");
266 				return IEEE80211_CONN_MODE_LEGACY;
267 			}
268 
269 			if (!cfg80211_chandef_compatible(chandef,
270 							 &he_chandef)) {
271 				sdata_info(sdata, "HE/EHT incompatible\n");
272 				return IEEE80211_CONN_MODE_LEGACY;
273 			}
274 		}
275 
276 		if (mode <= IEEE80211_CONN_MODE_EHT)
277 			return mode;
278 		goto check_uhr;
279 	}
280 
281 	/* now we have the progression HT, VHT, ... */
282 	if (conn->mode < IEEE80211_CONN_MODE_HT)
283 		return IEEE80211_CONN_MODE_LEGACY;
284 
285 	if (!ht_oper || !elems->ht_cap_elem)
286 		return IEEE80211_CONN_MODE_LEGACY;
287 
288 	chandef->width = NL80211_CHAN_WIDTH_20;
289 
290 	ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
291 						  channel->band);
292 	/* check that channel matches the right operating channel */
293 	if (!ignore_ht_channel_mismatch && channel->center_freq != ht_cfreq) {
294 		/*
295 		 * It's possible that some APs are confused here;
296 		 * Netgear WNDR3700 sometimes reports 4 higher than
297 		 * the actual channel in association responses, but
298 		 * since we look at probe response/beacon data here
299 		 * it should be OK.
300 		 */
301 		sdata_info(sdata,
302 			   "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
303 			   channel->center_freq, ht_cfreq,
304 			   ht_oper->primary_chan, channel->band);
305 		return IEEE80211_CONN_MODE_LEGACY;
306 	}
307 
308 	ieee80211_chandef_ht_oper(ht_oper, chandef);
309 
310 	if (conn->mode < IEEE80211_CONN_MODE_VHT)
311 		return IEEE80211_CONN_MODE_HT;
312 
313 	vht_chandef = *chandef;
314 
315 	/*
316 	 * having he_cap/he_oper parsed out implies we're at
317 	 * least operating as HE STA
318 	 */
319 	if (elems->he_cap && he_oper &&
320 	    he_oper->he_oper_params & cpu_to_le32(IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
321 		struct ieee80211_vht_operation he_oper_vht_cap;
322 
323 		/*
324 		 * Set only first 3 bytes (other 2 aren't used in
325 		 * ieee80211_chandef_vht_oper() anyway)
326 		 */
327 		memcpy(&he_oper_vht_cap, he_oper->optional, 3);
328 		he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
329 
330 		if (!ieee80211_chandef_vht_oper(&sdata->local->hw, data->vht_cap_info,
331 						&he_oper_vht_cap, ht_oper,
332 						&vht_chandef)) {
333 			sdata_info(sdata,
334 				   "HE AP VHT information is invalid, disabling HE\n");
335 			/* this will cause us to re-parse as VHT STA */
336 			return IEEE80211_CONN_MODE_VHT;
337 		}
338 	} else if (!vht_oper || !elems->vht_cap_elem) {
339 		if (sband->band == NL80211_BAND_5GHZ)
340 			return IEEE80211_CONN_MODE_HT;
341 		no_vht = true;
342 	} else if (sband->band == NL80211_BAND_2GHZ) {
343 		no_vht = true;
344 	} else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
345 					       data->vht_cap_info,
346 					       vht_oper, ht_oper,
347 					       &vht_chandef)) {
348 		sdata_info(sdata,
349 			   "AP VHT information is invalid, disabling VHT\n");
350 		return IEEE80211_CONN_MODE_HT;
351 	}
352 
353 	if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
354 		sdata_info(sdata,
355 			   "AP VHT information doesn't match HT, disabling VHT\n");
356 		return IEEE80211_CONN_MODE_HT;
357 	}
358 
359 	*chandef = vht_chandef;
360 
361 	/* stick to current max mode if we or the AP don't have HE */
362 	if (conn->mode < IEEE80211_CONN_MODE_HE ||
363 	    !elems->he_operation || !elems->he_cap) {
364 		if (no_vht)
365 			return IEEE80211_CONN_MODE_HT;
366 		return IEEE80211_CONN_MODE_VHT;
367 	}
368 
369 	/* stick to HE if we or the AP don't have EHT */
370 	if (conn->mode < IEEE80211_CONN_MODE_EHT ||
371 	    !eht_oper || !elems->eht_cap)
372 		return IEEE80211_CONN_MODE_HE;
373 
374 	/*
375 	 * handle the case that the EHT operation indicates that it holds EHT
376 	 * operation information (in case that the channel width differs from
377 	 * the channel width reported in HT/VHT/HE).
378 	 */
379 	if (eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT) {
380 		struct cfg80211_chan_def eht_chandef = *chandef;
381 
382 		ieee80211_chandef_eht_oper((const void *)eht_oper->optional,
383 					   &eht_chandef);
384 
385 		eht_chandef.punctured =
386 			ieee80211_eht_oper_dis_subchan_bitmap(eht_oper);
387 
388 		if (!cfg80211_chandef_valid(&eht_chandef)) {
389 			sdata_info(sdata,
390 				   "AP EHT information is invalid, disabling EHT\n");
391 			return IEEE80211_CONN_MODE_HE;
392 		}
393 
394 		if (!cfg80211_chandef_compatible(chandef, &eht_chandef)) {
395 			sdata_info(sdata,
396 				   "AP EHT information doesn't match HT/VHT/HE, disabling EHT\n");
397 			return IEEE80211_CONN_MODE_HE;
398 		}
399 
400 		*chandef = eht_chandef;
401 	}
402 
403 check_uhr:
404 	if (conn->mode < IEEE80211_CONN_MODE_UHR || !uhr_oper || !elems->ml_basic)
405 		return IEEE80211_CONN_MODE_EHT;
406 
407 	if (elems->frame_type != (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON)) {
408 		struct cfg80211_chan_def npca_chandef = *chandef;
409 		const struct ieee80211_sta_uhr_cap *uhr_cap;
410 		const struct ieee80211_uhr_npca_info *npca;
411 		const struct ieee80211_uhr_dbe_info *dbe;
412 		struct cfg80211_chan_def dbe_chandef;
413 
414 		/* frames other than beacons carry UHR capability too */
415 		if (!elems->uhr_cap)
416 			return IEEE80211_CONN_MODE_EHT;
417 
418 		npca = ieee80211_uhr_npca_info(uhr_oper);
419 
420 		if (npca && !(elems->uhr_cap->mac.mac_cap[0] &
421 				IEEE80211_UHR_MAC_CAP0_NPCA_SUPP)) {
422 			sdata_info(sdata,
423 				   "AP without UHR NPCA capability uses it, disabling UHR\n");
424 			return IEEE80211_CONN_MODE_EHT;
425 		}
426 
427 		/* DBE is not considered yet, so this works */
428 		if (!cfg80211_chandef_npca_valid(sdata->local->hw.wiphy,
429 						 &npca_chandef, npca) ||
430 		    cfg80211_chandef_add_npca(sdata->local->hw.wiphy,
431 					      &npca_chandef, npca)) {
432 			sdata_info(sdata,
433 				   "AP UHR NPCA settings invalid, disabling UHR\n");
434 			return IEEE80211_CONN_MODE_EHT;
435 		}
436 
437 		uhr_cap = ieee80211_get_uhr_iftype_cap_vif(sband, &sdata->vif);
438 		/* can't happen since we must have UHR to parse the elems */
439 		if (WARN_ON(!uhr_cap))
440 			return IEEE80211_CONN_MODE_EHT;
441 
442 		if (uhr_cap->mac.mac_cap[0] & IEEE80211_UHR_MAC_CAP0_NPCA_SUPP)
443 			*chandef = npca_chandef;
444 
445 		dbe = ieee80211_uhr_oper_dbe_info(uhr_oper);
446 		if (dbe) {
447 			const struct ieee80211_uhr_cap_dbe *dbe_cap;
448 			u8 dbe_bw_oper;
449 			u8 dbe_bw_cap;
450 
451 			dbe_cap = ieee80211_uhr_dbe_cap(elems->uhr_cap);
452 
453 			if (!dbe_cap) {
454 				sdata_info(sdata,
455 					   "AP without UHR DBE capability uses it, disabling UHR\n");
456 				return IEEE80211_CONN_MODE_EHT;
457 			}
458 
459 			dbe_bw_oper = u8_get_bits(dbe->params,
460 						  IEEE80211_UHR_DBE_OPER_BANDWIDTH);
461 
462 			if (le16_get_bits(uhr_oper->params,
463 					  IEEE80211_UHR_OPER_PARAMS_DBE_BW) != dbe_bw_oper) {
464 				sdata_info(sdata,
465 					   "AP UHR DBE settings mismatch, disabling UHR\n");
466 				return IEEE80211_CONN_MODE_EHT;
467 			}
468 
469 			if (ieee80211_uhr_dbe_bw_mhz(dbe_bw_oper) < 0) {
470 				sdata_info(sdata,
471 					   "AP UHR DBE bandwidth invalid, disabling UHR\n");
472 				return IEEE80211_CONN_MODE_EHT;
473 			}
474 
475 			dbe_bw_cap = u8_get_bits(dbe_cap->cap,
476 						 IEEE80211_UHR_MAC_CAP_DBE_MAX_BW);
477 
478 			switch (dbe_bw_cap) {
479 			case IEEE80211_UHR_DBE_MAX_BW_40:
480 			case IEEE80211_UHR_DBE_MAX_BW_80:
481 			case IEEE80211_UHR_DBE_MAX_BW_160:
482 			case IEEE80211_UHR_DBE_MAX_BW_320:
483 				break;
484 			default:
485 				sdata_info(sdata,
486 					   "AP UHR DBE capability invalid, disabling UHR\n");
487 				return IEEE80211_CONN_MODE_EHT;
488 			}
489 
490 			/* 1-4 are same in DBE capabilities, map 320-2 to 320 */
491 			if (dbe_bw_oper == IEEE80211_UHR_DBE_OPER_BW_320_2)
492 				dbe_bw_oper = IEEE80211_UHR_DBE_MAX_BW_320;
493 			if (dbe_bw_oper > dbe_bw_cap) {
494 				sdata_info(sdata,
495 					   "AP UHR DBE wider than capability, disabling UHR\n");
496 				return IEEE80211_CONN_MODE_EHT;
497 			}
498 		}
499 
500 		dbe_chandef = *chandef;
501 
502 		if (cfg80211_chandef_add_dbe(&dbe_chandef, dbe)) {
503 			sdata_info(sdata,
504 				   "AP UHR DBE settings invalid, disabling UHR\n");
505 			return IEEE80211_CONN_MODE_EHT;
506 		}
507 
508 		if (dbe &&
509 		    /* maybe driver would like to never use DBE */
510 		    uhr_cap->mac.mac_cap[1] & IEEE80211_UHR_MAC_CAP1_DBE_SUPP &&
511 		    ieee80211_chandef_usable(sdata, &dbe_chandef,
512 					     IEEE80211_CHAN_DISABLED)) {
513 			out->non_dbe_width = chandef->width;
514 			*chandef = dbe_chandef;
515 			out->dbe_used = true;
516 		}
517 	} else if (data->cur_chandef && data->cur_dbe_used &&
518 		   cfg80211_chandef_compatible(chandef, data->cur_chandef)) {
519 		u8 dbe_bw = le16_get_bits(uhr_oper->params,
520 					  IEEE80211_UHR_OPER_PARAMS_DBE_BW);
521 		int dbe_bw_mhz;
522 
523 		dbe_bw_mhz = ieee80211_uhr_dbe_bw_mhz(dbe_bw);
524 		if (dbe_bw_mhz < 0) {
525 			sdata_info(sdata,
526 				   "AP UHR DBE bandwidth invalid, drop UHR\n");
527 			return IEEE80211_CONN_MODE_EHT;
528 		}
529 
530 		if (cfg80211_chandef_get_width(data->cur_chandef) == dbe_bw_mhz) {
531 			*chandef = *data->cur_chandef;
532 			out->dbe_used = true;
533 		}
534 	}
535 
536 	return IEEE80211_CONN_MODE_UHR;
537 }
538 
539 static bool
540 ieee80211_verify_sta_ht_mcs_support(struct ieee80211_sub_if_data *sdata,
541 				    struct ieee80211_supported_band *sband,
542 				    const struct ieee80211_ht_operation *ht_op)
543 {
544 	struct ieee80211_sta_ht_cap sta_ht_cap;
545 	int i;
546 
547 	if (sband->band == NL80211_BAND_6GHZ)
548 		return true;
549 
550 	if (!ht_op)
551 		return false;
552 
553 	memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
554 	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
555 
556 	/*
557 	 * Some Xfinity XB8 firmware advertises >1 spatial stream MCS indexes in
558 	 * their basic HT-MCS set. On cards with lower spatial streams, the check
559 	 * would fail, and we'd be stuck with no HT when it in fact work fine with
560 	 * its own supported rate. So check it only in strict mode.
561 	 */
562 	if (!ieee80211_hw_check(&sdata->local->hw, STRICT))
563 		return true;
564 
565 	/*
566 	 * P802.11REVme/D7.0 - 6.5.4.2.4
567 	 * ...
568 	 * If the MLME of an HT STA receives an MLME-JOIN.request primitive
569 	 * with the SelectedBSS parameter containing a Basic HT-MCS Set field
570 	 * in the HT Operation parameter that contains any unsupported MCSs,
571 	 * the MLME response in the resulting MLME-JOIN.confirm primitive shall
572 	 * contain a ResultCode parameter that is not set to the value SUCCESS.
573 	 * ...
574 	 */
575 
576 	/* Simply check that all basic rates are in the STA RX mask */
577 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
578 		if ((ht_op->basic_set[i] & sta_ht_cap.mcs.rx_mask[i]) !=
579 		    ht_op->basic_set[i])
580 			return false;
581 	}
582 
583 	return true;
584 }
585 
586 static bool
587 ieee80211_verify_sta_vht_mcs_support(struct ieee80211_sub_if_data *sdata,
588 				     int link_id,
589 				     struct ieee80211_supported_band *sband,
590 				     const struct ieee80211_vht_operation *vht_op)
591 {
592 	struct ieee80211_sta_vht_cap sta_vht_cap;
593 	u16 ap_min_req_set, sta_rx_mcs_map, sta_tx_mcs_map;
594 	int nss;
595 
596 	if (sband->band != NL80211_BAND_5GHZ)
597 		return true;
598 
599 	if (!vht_op)
600 		return false;
601 
602 	memcpy(&sta_vht_cap, &sband->vht_cap, sizeof(sta_vht_cap));
603 	ieee80211_apply_vhtcap_overrides(sdata, &sta_vht_cap);
604 
605 	ap_min_req_set = le16_to_cpu(vht_op->basic_mcs_set);
606 	sta_rx_mcs_map = le16_to_cpu(sta_vht_cap.vht_mcs.rx_mcs_map);
607 	sta_tx_mcs_map = le16_to_cpu(sta_vht_cap.vht_mcs.tx_mcs_map);
608 
609 	/*
610 	 * Many APs are incorrectly advertising an all-zero value here,
611 	 * which really means MCS 0-7 are required for 1-8 streams, but
612 	 * they don't really mean it that way.
613 	 * Some other APs are incorrectly advertising 3 spatial streams
614 	 * with MCS 0-7 are required, but don't really mean it that way
615 	 * and we'll connect only with HT, rather than even HE.
616 	 * As a result, unfortunately the VHT basic MCS/NSS set cannot
617 	 * be used at all, so check it only in strict mode.
618 	 */
619 	if (!ieee80211_hw_check(&sdata->local->hw, STRICT))
620 		return true;
621 
622 	/*
623 	 * P802.11REVme/D7.0 - 6.5.4.2.4
624 	 * ...
625 	 * If the MLME of a VHT STA receives an MLME-JOIN.request primitive
626 	 * with a SelectedBSS parameter containing a Basic VHT-MCS And NSS Set
627 	 * field in the VHT Operation parameter that contains any unsupported
628 	 * <VHT-MCS, NSS> tuple, the MLME response in the resulting
629 	 * MLME-JOIN.confirm primitive shall contain a ResultCode parameter
630 	 * that is not set to the value SUCCESS.
631 	 * ...
632 	 */
633 	for (nss = 8; nss > 0; nss--) {
634 		u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
635 		u8 sta_rx_val;
636 		u8 sta_tx_val;
637 
638 		if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
639 			continue;
640 
641 		sta_rx_val = (sta_rx_mcs_map >> (2 * (nss - 1))) & 3;
642 		sta_tx_val = (sta_tx_mcs_map >> (2 * (nss - 1))) & 3;
643 
644 		if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
645 		    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
646 		    sta_rx_val < ap_op_val || sta_tx_val < ap_op_val) {
647 			link_id_info(sdata, link_id,
648 				     "Missing mandatory rates for %d Nss, rx %d, tx %d oper %d, disable VHT\n",
649 				     nss, sta_rx_val, sta_tx_val, ap_op_val);
650 			return false;
651 		}
652 	}
653 
654 	return true;
655 }
656 
657 static bool
658 ieee80211_verify_peer_he_mcs_support(struct ieee80211_sub_if_data *sdata,
659 				     int link_id,
660 				     const struct ieee80211_he_cap_elem *he_cap,
661 				     const struct ieee80211_he_operation *he_op)
662 {
663 	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
664 	u16 mcs_80_map_tx, mcs_80_map_rx;
665 	u16 ap_min_req_set;
666 	int nss;
667 
668 	if (!he_cap)
669 		return false;
670 
671 	/* mcs_nss is right after he_cap info */
672 	he_mcs_nss_supp = (void *)(he_cap + 1);
673 
674 	mcs_80_map_tx = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
675 	mcs_80_map_rx = le16_to_cpu(he_mcs_nss_supp->rx_mcs_80);
676 
677 	/* P802.11-REVme/D0.3
678 	 * 27.1.1 Introduction to the HE PHY
679 	 * ...
680 	 * An HE STA shall support the following features:
681 	 * ...
682 	 * Single spatial stream HE-MCSs 0 to 7 (transmit and receive) in all
683 	 * supported channel widths for HE SU PPDUs
684 	 */
685 	if ((mcs_80_map_tx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED ||
686 	    (mcs_80_map_rx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED) {
687 		link_id_info(sdata, link_id,
688 			     "Missing mandatory rates for 1 Nss, rx 0x%x, tx 0x%x, disable HE\n",
689 			     mcs_80_map_tx, mcs_80_map_rx);
690 		return false;
691 	}
692 
693 	if (!he_op)
694 		return true;
695 
696 	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
697 
698 	/*
699 	 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all
700 	 * zeroes, which is nonsense, and completely inconsistent with itself
701 	 * (it doesn't have 8 streams). Accept the settings in this case anyway.
702 	 */
703 	if (!ieee80211_hw_check(&sdata->local->hw, STRICT) && !ap_min_req_set)
704 		return true;
705 
706 	/* make sure the AP is consistent with itself
707 	 *
708 	 * P802.11-REVme/D0.3
709 	 * 26.17.1 Basic HE BSS operation
710 	 *
711 	 * A STA that is operating in an HE BSS shall be able to receive and
712 	 * transmit at each of the <HE-MCS, NSS> tuple values indicated by the
713 	 * Basic HE-MCS And NSS Set field of the HE Operation parameter of the
714 	 * MLME-START.request primitive and shall be able to receive at each of
715 	 * the <HE-MCS, NSS> tuple values indicated by the Supported HE-MCS and
716 	 * NSS Set field in the HE Capabilities parameter of the MLMESTART.request
717 	 * primitive
718 	 */
719 	for (nss = 8; nss > 0; nss--) {
720 		u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
721 		u8 ap_rx_val;
722 		u8 ap_tx_val;
723 
724 		if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
725 			continue;
726 
727 		ap_rx_val = (mcs_80_map_rx >> (2 * (nss - 1))) & 3;
728 		ap_tx_val = (mcs_80_map_tx >> (2 * (nss - 1))) & 3;
729 
730 		if (ap_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
731 		    ap_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
732 		    ap_rx_val < ap_op_val || ap_tx_val < ap_op_val) {
733 			link_id_info(sdata, link_id,
734 				     "Invalid rates for %d Nss, rx %d, tx %d oper %d, disable HE\n",
735 				     nss, ap_rx_val, ap_tx_val, ap_op_val);
736 			return false;
737 		}
738 	}
739 
740 	return true;
741 }
742 
743 static bool
744 ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata,
745 				    struct ieee80211_supported_band *sband,
746 				    const struct ieee80211_he_operation *he_op)
747 {
748 	const struct ieee80211_sta_he_cap *sta_he_cap =
749 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
750 	u16 ap_min_req_set;
751 	int i;
752 
753 	if (!sta_he_cap || !he_op)
754 		return false;
755 
756 	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
757 
758 	/*
759 	 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all
760 	 * zeroes, which is nonsense, and completely inconsistent with itself
761 	 * (it doesn't have 8 streams). Accept the settings in this case anyway.
762 	 */
763 	if (!ieee80211_hw_check(&sdata->local->hw, STRICT) && !ap_min_req_set)
764 		return true;
765 
766 	/* Need to go over for 80MHz, 160MHz and for 80+80 */
767 	for (i = 0; i < 3; i++) {
768 		const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
769 			&sta_he_cap->he_mcs_nss_supp;
770 		u16 sta_mcs_map_rx =
771 			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
772 		u16 sta_mcs_map_tx =
773 			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
774 		u8 nss;
775 		bool verified = true;
776 
777 		/*
778 		 * For each band there is a maximum of 8 spatial streams
779 		 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
780 		 * of 2 bits per NSS (1-8), with the values defined in enum
781 		 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
782 		 * capabilities aren't less than the AP's minimum requirements
783 		 * for this HE BSS per SS.
784 		 * It is enough to find one such band that meets the reqs.
785 		 */
786 		for (nss = 8; nss > 0; nss--) {
787 			u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
788 			u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
789 			u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
790 
791 			if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
792 				continue;
793 
794 			/*
795 			 * Make sure the HE AP doesn't require MCSs that aren't
796 			 * supported by the client as required by spec
797 			 *
798 			 * P802.11-REVme/D0.3
799 			 * 26.17.1 Basic HE BSS operation
800 			 *
801 			 * An HE STA shall not attempt to join * (MLME-JOIN.request primitive)
802 			 * a BSS, unless it supports (i.e., is able to both transmit and
803 			 * receive using) all of the <HE-MCS, NSS> tuples in the basic
804 			 * HE-MCS and NSS set.
805 			 */
806 			if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
807 			    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
808 			    (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
809 				verified = false;
810 				break;
811 			}
812 		}
813 
814 		if (verified)
815 			return true;
816 	}
817 
818 	/* If here, STA doesn't meet AP's HE min requirements */
819 	return false;
820 }
821 
822 static u8
823 ieee80211_get_eht_cap_mcs_nss(const struct ieee80211_sta_he_cap *sta_he_cap,
824 			      const struct ieee80211_sta_eht_cap *sta_eht_cap,
825 			      unsigned int idx, int bw)
826 {
827 	u8 he_phy_cap0 = sta_he_cap->he_cap_elem.phy_cap_info[0];
828 	u8 eht_phy_cap0 = sta_eht_cap->eht_cap_elem.phy_cap_info[0];
829 
830 	/* handle us being a 20 MHz-only EHT STA - with four values
831 	 * for MCS 0-7, 8-9, 10-11, 12-13.
832 	 */
833 	if (!(he_phy_cap0 & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL))
834 		return sta_eht_cap->eht_mcs_nss_supp.only_20mhz.rx_tx_max_nss[idx];
835 
836 	/* the others have MCS 0-9 together, rather than separately from 0-7 */
837 	if (idx > 0)
838 		idx--;
839 
840 	switch (bw) {
841 	case 0:
842 		return sta_eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_max_nss[idx];
843 	case 1:
844 		if (!(he_phy_cap0 &
845 		      (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
846 		       IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)))
847 			return 0xff; /* pass check */
848 		return sta_eht_cap->eht_mcs_nss_supp.bw._160.rx_tx_max_nss[idx];
849 	case 2:
850 		if (!(eht_phy_cap0 & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ))
851 			return 0xff; /* pass check */
852 		return sta_eht_cap->eht_mcs_nss_supp.bw._320.rx_tx_max_nss[idx];
853 	}
854 
855 	WARN_ON(1);
856 	return 0;
857 }
858 
859 static bool
860 ieee80211_verify_sta_eht_mcs_support(struct ieee80211_sub_if_data *sdata,
861 				     struct ieee80211_supported_band *sband,
862 				     const struct ieee80211_eht_operation *eht_op)
863 {
864 	const struct ieee80211_sta_he_cap *sta_he_cap =
865 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
866 	const struct ieee80211_sta_eht_cap *sta_eht_cap =
867 		ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
868 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *req;
869 	unsigned int i;
870 
871 	if (!sta_he_cap || !sta_eht_cap || !eht_op)
872 		return false;
873 
874 	req = &eht_op->basic_mcs_nss;
875 
876 	for (i = 0; i < ARRAY_SIZE(req->rx_tx_max_nss); i++) {
877 		u8 req_rx_nss, req_tx_nss;
878 		unsigned int bw;
879 
880 		req_rx_nss = u8_get_bits(req->rx_tx_max_nss[i],
881 					 IEEE80211_EHT_MCS_NSS_RX);
882 		req_tx_nss = u8_get_bits(req->rx_tx_max_nss[i],
883 					 IEEE80211_EHT_MCS_NSS_TX);
884 
885 		for (bw = 0; bw < 3; bw++) {
886 			u8 have, have_rx_nss, have_tx_nss;
887 
888 			have = ieee80211_get_eht_cap_mcs_nss(sta_he_cap,
889 							     sta_eht_cap,
890 							     i, bw);
891 			have_rx_nss = u8_get_bits(have,
892 						  IEEE80211_EHT_MCS_NSS_RX);
893 			have_tx_nss = u8_get_bits(have,
894 						  IEEE80211_EHT_MCS_NSS_TX);
895 
896 			if (req_rx_nss > have_rx_nss ||
897 			    req_tx_nss > have_tx_nss)
898 				return false;
899 		}
900 	}
901 
902 	return true;
903 }
904 
905 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
906 				const u8 *supp_rates,
907 				unsigned int supp_rates_len,
908 				const u8 *ext_supp_rates,
909 				unsigned int ext_supp_rates_len,
910 				u32 *rates, u32 *basic_rates,
911 				unsigned long *unknown_rates_selectors,
912 				bool *have_higher_than_11mbit,
913 				int *min_rate, int *min_rate_index)
914 {
915 	int i, j;
916 
917 	for (i = 0; i < supp_rates_len + ext_supp_rates_len; i++) {
918 		u8 supp_rate = i < supp_rates_len ?
919 				supp_rates[i] :
920 				ext_supp_rates[i - supp_rates_len];
921 		int rate = supp_rate & 0x7f;
922 		bool is_basic = !!(supp_rate & 0x80);
923 
924 		if ((rate * 5) > 110 && have_higher_than_11mbit)
925 			*have_higher_than_11mbit = true;
926 
927 		/*
928 		 * Skip membership selectors since they're not rates.
929 		 *
930 		 * Note: Even though the membership selector and the basic
931 		 *	 rate flag share the same bit, they are not exactly
932 		 *	 the same.
933 		 */
934 		if (is_basic && rate >= BSS_MEMBERSHIP_SELECTOR_MIN) {
935 			if (unknown_rates_selectors)
936 				set_bit(rate, unknown_rates_selectors);
937 			continue;
938 		}
939 
940 		for (j = 0; j < sband->n_bitrates; j++) {
941 			struct ieee80211_rate *br;
942 			int brate;
943 
944 			br = &sband->bitrates[j];
945 
946 			brate = DIV_ROUND_UP(br->bitrate, 5);
947 			if (brate == rate) {
948 				if (rates)
949 					*rates |= BIT(j);
950 				if (is_basic && basic_rates)
951 					*basic_rates |= BIT(j);
952 				if (min_rate && (rate * 5) < *min_rate) {
953 					*min_rate = rate * 5;
954 					if (min_rate_index)
955 						*min_rate_index = j;
956 				}
957 				break;
958 			}
959 		}
960 
961 		/* Handle an unknown entry as if it is an unknown selector */
962 		if (is_basic && unknown_rates_selectors && j == sband->n_bitrates)
963 			set_bit(rate, unknown_rates_selectors);
964 	}
965 }
966 
967 static int ieee80211_chandef_num_subchans(const struct cfg80211_chan_def *c)
968 {
969 	if (c->width == NL80211_CHAN_WIDTH_80P80)
970 		return 4 + 4;
971 
972 	return cfg80211_chandef_get_width(c) / 20;
973 }
974 
975 static int ieee80211_chandef_num_widths(const struct cfg80211_chan_def *c)
976 {
977 	switch (c->width) {
978 	case NL80211_CHAN_WIDTH_20:
979 	case NL80211_CHAN_WIDTH_20_NOHT:
980 		return 1;
981 	case NL80211_CHAN_WIDTH_40:
982 		return 2;
983 	case NL80211_CHAN_WIDTH_80P80:
984 	case NL80211_CHAN_WIDTH_80:
985 		return 3;
986 	case NL80211_CHAN_WIDTH_160:
987 		return 4;
988 	case NL80211_CHAN_WIDTH_320:
989 		return 5;
990 	default:
991 		WARN_ON(1);
992 		return 0;
993 	}
994 }
995 
996 VISIBLE_IF_MAC80211_KUNIT int
997 ieee80211_calc_chandef_subchan_offset(const struct cfg80211_chan_def *ap,
998 				      u8 n_partial_subchans)
999 {
1000 	int n = ieee80211_chandef_num_subchans(ap);
1001 	struct cfg80211_chan_def tmp = *ap;
1002 	int offset = 0;
1003 
1004 	/*
1005 	 * Given a chandef (in this context, it's the AP's) and a number
1006 	 * of subchannels that we want to look at ('n_partial_subchans'),
1007 	 * calculate the offset in number of subchannels between the full
1008 	 * and the subset with the desired width.
1009 	 */
1010 
1011 	/* same number of subchannels means no offset, obviously */
1012 	if (n == n_partial_subchans)
1013 		return 0;
1014 
1015 	/* don't WARN - misconfigured APs could cause this if their N > width */
1016 	if (n < n_partial_subchans)
1017 		return 0;
1018 
1019 	while (ieee80211_chandef_num_subchans(&tmp) > n_partial_subchans) {
1020 		u32 prev = tmp.center_freq1;
1021 
1022 		ieee80211_chandef_downgrade(&tmp, NULL);
1023 
1024 		/*
1025 		 * if center_freq moved up, half the original channels
1026 		 * are gone now but were below, so increase offset
1027 		 */
1028 		if (prev < tmp.center_freq1)
1029 			offset += ieee80211_chandef_num_subchans(&tmp);
1030 	}
1031 
1032 	/*
1033 	 * 80+80 with secondary 80 below primary - four subchannels for it
1034 	 * (we cannot downgrade *to* 80+80, so no need to consider 'tmp')
1035 	 */
1036 	if (ap->width == NL80211_CHAN_WIDTH_80P80 &&
1037 	    ap->center_freq2 < ap->center_freq1)
1038 		offset += 4;
1039 
1040 	return offset;
1041 }
1042 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_calc_chandef_subchan_offset);
1043 
1044 VISIBLE_IF_MAC80211_KUNIT void
1045 ieee80211_rearrange_tpe_psd(struct ieee80211_parsed_tpe_psd *psd,
1046 			    const struct cfg80211_chan_def *ap,
1047 			    const struct cfg80211_chan_def *used)
1048 {
1049 	u8 needed = ieee80211_chandef_num_subchans(used);
1050 	u8 have = ieee80211_chandef_num_subchans(ap);
1051 	u8 tmp[IEEE80211_TPE_PSD_ENTRIES_320MHZ];
1052 	u8 offset;
1053 
1054 	if (!psd->valid)
1055 		return;
1056 
1057 	/* if N is zero, all defaults were used, no point in rearranging */
1058 	if (!psd->n)
1059 		goto out;
1060 
1061 	BUILD_BUG_ON(sizeof(tmp) != sizeof(psd->power));
1062 
1063 	/*
1064 	 * This assumes that 'N' is consistent with the HE channel, as
1065 	 * it should be (otherwise the AP is broken).
1066 	 *
1067 	 * In psd->power we have values in the order 0..N, 0..K, where
1068 	 * N+K should cover the entire channel per 'ap', but even if it
1069 	 * doesn't then we've pre-filled 'unlimited' as defaults.
1070 	 *
1071 	 * But this is all the wrong order, we want to have them in the
1072 	 * order of the 'used' channel.
1073 	 *
1074 	 * So for example, we could have a 320 MHz EHT AP, which has the
1075 	 * HE channel as 80 MHz (e.g. due to puncturing, which doesn't
1076 	 * seem to be considered for the TPE), as follows:
1077 	 *
1078 	 * EHT  320:   |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
1079 	 * HE    80:                           |  |  |  |  |
1080 	 * used 160:                           |  |  |  |  |  |  |  |  |
1081 	 *
1082 	 * N entries:                          |--|--|--|--|
1083 	 * K entries:  |--|--|--|--|--|--|--|--|           |--|--|--|--|
1084 	 * power idx:   4  5  6  7  8  9  10 11 0  1  2  3  12 13 14 15
1085 	 * full chan:   0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
1086 	 * used chan:                           0  1  2  3  4  5  6  7
1087 	 *
1088 	 * The idx in the power array ('power idx') is like this since it
1089 	 * comes directly from the element's N and K entries in their
1090 	 * element order, and those are this way for HE compatibility.
1091 	 *
1092 	 * Rearrange them as desired here, first by putting them into the
1093 	 * 'full chan' order, and then selecting the necessary subset for
1094 	 * the 'used chan'.
1095 	 */
1096 
1097 	/* first reorder according to AP channel */
1098 	offset = ieee80211_calc_chandef_subchan_offset(ap, psd->n);
1099 	for (int i = 0; i < have; i++) {
1100 		if (i < offset)
1101 			tmp[i] = psd->power[i + psd->n];
1102 		else if (i < offset + psd->n)
1103 			tmp[i] = psd->power[i - offset];
1104 		else
1105 			tmp[i] = psd->power[i];
1106 	}
1107 
1108 	/*
1109 	 * and then select the subset for the used channel
1110 	 * (set everything to defaults first in case a driver is confused)
1111 	 */
1112 	memset(psd->power, IEEE80211_TPE_PSD_NO_LIMIT, sizeof(psd->power));
1113 	offset = ieee80211_calc_chandef_subchan_offset(ap, needed);
1114 	for (int i = 0; i < needed; i++)
1115 		psd->power[i] = tmp[offset + i];
1116 
1117 out:
1118 	/* limit, but don't lie if there are defaults in the data */
1119 	if (needed < psd->count)
1120 		psd->count = needed;
1121 }
1122 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_rearrange_tpe_psd);
1123 
1124 static void ieee80211_rearrange_tpe(struct ieee80211_parsed_tpe *tpe,
1125 				    const struct cfg80211_chan_def *ap,
1126 				    const struct cfg80211_chan_def *used)
1127 {
1128 	/* ignore this completely for narrow/invalid channels */
1129 	if (!ieee80211_chandef_num_subchans(ap) ||
1130 	    !ieee80211_chandef_num_subchans(used)) {
1131 		ieee80211_clear_tpe(tpe);
1132 		return;
1133 	}
1134 
1135 	for (int i = 0; i < 2; i++) {
1136 		int needed_pwr_count;
1137 
1138 		ieee80211_rearrange_tpe_psd(&tpe->psd_local[i], ap, used);
1139 		ieee80211_rearrange_tpe_psd(&tpe->psd_reg_client[i], ap, used);
1140 
1141 		/* limit this to the widths we actually need */
1142 		needed_pwr_count = ieee80211_chandef_num_widths(used);
1143 		if (needed_pwr_count < tpe->max_local[i].count)
1144 			tpe->max_local[i].count = needed_pwr_count;
1145 		if (needed_pwr_count < tpe->max_reg_client[i].count)
1146 			tpe->max_reg_client[i].count = needed_pwr_count;
1147 	}
1148 }
1149 
1150 /*
1151  * The AP part of the channel request is used to distinguish settings
1152  * to the device used for wider bandwidth OFDMA. This is used in the
1153  * channel context code to assign two channel contexts even if they're
1154  * both for the same channel, if the AP bandwidths are incompatible.
1155  * If not EHT (or driver override) then ap.chan == NULL indicates that
1156  * there's no wider BW OFDMA used.
1157  */
1158 static void ieee80211_set_chanreq_ap(struct ieee80211_sub_if_data *sdata,
1159 				     struct ieee80211_chan_req *chanreq,
1160 				     struct ieee80211_conn_settings *conn,
1161 				     struct cfg80211_chan_def *ap_chandef)
1162 {
1163 	chanreq->ap.chan = NULL;
1164 
1165 	if (conn->mode < IEEE80211_CONN_MODE_EHT)
1166 		return;
1167 	if (sdata->vif.driver_flags & IEEE80211_VIF_IGNORE_OFDMA_WIDER_BW)
1168 		return;
1169 
1170 	chanreq->ap = *ap_chandef;
1171 }
1172 
1173 VISIBLE_IF_MAC80211_KUNIT struct ieee802_11_elems *
1174 ieee80211_determine_chan_mode(struct ieee80211_sub_if_data *sdata,
1175 			      struct ieee80211_conn_settings *conn,
1176 			      struct cfg80211_bss *cbss,
1177 			      struct link_sta_info *link_sta, int link_id,
1178 			      struct ieee80211_chan_req *chanreq,
1179 			      struct cfg80211_chan_def *ap_chandef,
1180 			      unsigned long *userspace_selectors)
1181 {
1182 	const struct cfg80211_bss_ies *ies = rcu_dereference(cbss->ies);
1183 	struct ieee80211_bss *bss = (void *)cbss->priv;
1184 	struct ieee80211_channel *channel = cbss->channel;
1185 	/*
1186 	 * This is for parsing a beacon or probe response here, but it's
1187 	 * using the *BSS* elements which are synthetic for multi-BSSID,
1188 	 * created by cfg80211 based on multi-BSSID inheritance etc. As
1189 	 * a result, this sets neither .bss (since multi-BSSID is parsed
1190 	 * already) nor a valid .link_id (since it doesn't want to see
1191 	 * the data from another link.)
1192 	 */
1193 	struct ieee80211_elems_parse_params parse_params = {
1194 		.link_id = -1,
1195 		.from_ap = true,
1196 		.start = ies->data,
1197 		.len = ies->len,
1198 		.type = ies->from_beacon ?
1199 			IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON :
1200 			IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_RESP,
1201 	};
1202 	struct ieee802_11_elems *elems;
1203 	struct ieee80211_supported_band *sband;
1204 	enum ieee80211_conn_mode ap_mode;
1205 	unsigned long unknown_rates_selectors[BITS_TO_LONGS(128)] = {};
1206 	unsigned long sta_selectors[BITS_TO_LONGS(128)] = {};
1207 	struct ieee80211_determine_ap_chan_output ap_chan_out;
1208 	struct ieee80211_determine_ap_chan_data ap_chan_data = {
1209 		.channel = channel,
1210 		.vht_cap_info = bss->vht_cap_info,
1211 		.ignore_ht_channel_mismatch = false,
1212 		.chandef = ap_chandef,
1213 		.conn = conn,
1214 	};
1215 	int ret;
1216 
1217 again:
1218 	parse_params.mode = conn->mode;
1219 	elems = ieee802_11_parse_elems_full(&parse_params);
1220 	if (!elems)
1221 		return ERR_PTR(-ENOMEM);
1222 
1223 	ap_chan_data.elems = elems;
1224 	ap_mode = ieee80211_determine_ap_chan(sdata, &ap_chan_data,
1225 					      &ap_chan_out);
1226 	conn->dbe_enabled = ap_chan_out.dbe_used;
1227 
1228 	/* this should be impossible since parsing depends on our mode */
1229 	if (WARN_ON(ap_mode > conn->mode)) {
1230 		ret = -EINVAL;
1231 		goto free;
1232 	}
1233 
1234 	if (conn->mode != ap_mode) {
1235 		conn->mode = ap_mode;
1236 		kfree(elems);
1237 		goto again;
1238 	}
1239 
1240 	mlme_link_id_dbg(sdata, link_id, "determined AP %pM to be %s\n",
1241 			 cbss->bssid, ieee80211_conn_mode_str(ap_mode));
1242 
1243 	sband = sdata->local->hw.wiphy->bands[channel->band];
1244 
1245 	ieee80211_get_rates(sband, elems->supp_rates, elems->supp_rates_len,
1246 			    elems->ext_supp_rates, elems->ext_supp_rates_len,
1247 			    NULL, NULL, unknown_rates_selectors, NULL, NULL,
1248 			    NULL);
1249 
1250 	switch (channel->band) {
1251 	case NL80211_BAND_S1GHZ:
1252 		if (WARN_ON(ap_mode != IEEE80211_CONN_MODE_S1G)) {
1253 			ret = -EINVAL;
1254 			goto free;
1255 		}
1256 
1257 		chanreq->oper = *ap_chandef;
1258 		if (!cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
1259 					     IEEE80211_CHAN_DISABLED)) {
1260 			ret = -EINVAL;
1261 			goto free;
1262 		}
1263 
1264 		return elems;
1265 	case NL80211_BAND_6GHZ:
1266 		if (ap_mode < IEEE80211_CONN_MODE_HE) {
1267 			link_id_info(sdata, link_id,
1268 				     "Rejecting non-HE 6/7 GHz connection");
1269 			ret = -EINVAL;
1270 			goto free;
1271 		}
1272 		break;
1273 	default:
1274 		if (WARN_ON(ap_mode == IEEE80211_CONN_MODE_S1G)) {
1275 			ret = -EINVAL;
1276 			goto free;
1277 		}
1278 	}
1279 
1280 	switch (ap_mode) {
1281 	case IEEE80211_CONN_MODE_S1G:
1282 		WARN_ON(1);
1283 		ret = -EINVAL;
1284 		goto free;
1285 	case IEEE80211_CONN_MODE_LEGACY:
1286 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
1287 		break;
1288 	case IEEE80211_CONN_MODE_HT:
1289 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1290 				       conn->bw_limit,
1291 				       IEEE80211_CONN_BW_LIMIT_40);
1292 		break;
1293 	case IEEE80211_CONN_MODE_VHT:
1294 	case IEEE80211_CONN_MODE_HE:
1295 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1296 				       conn->bw_limit,
1297 				       IEEE80211_CONN_BW_LIMIT_160);
1298 		break;
1299 	case IEEE80211_CONN_MODE_EHT:
1300 	case IEEE80211_CONN_MODE_UHR:
1301 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1302 				       conn->bw_limit,
1303 				       IEEE80211_CONN_BW_LIMIT_320);
1304 		break;
1305 	}
1306 
1307 	chanreq->oper = *ap_chandef;
1308 
1309 	bitmap_copy(sta_selectors, userspace_selectors, 128);
1310 	if (conn->mode >= IEEE80211_CONN_MODE_HT)
1311 		set_bit(BSS_MEMBERSHIP_SELECTOR_HT_PHY, sta_selectors);
1312 	if (conn->mode >= IEEE80211_CONN_MODE_VHT)
1313 		set_bit(BSS_MEMBERSHIP_SELECTOR_VHT_PHY, sta_selectors);
1314 	if (conn->mode >= IEEE80211_CONN_MODE_HE)
1315 		set_bit(BSS_MEMBERSHIP_SELECTOR_HE_PHY, sta_selectors);
1316 	if (conn->mode >= IEEE80211_CONN_MODE_EHT)
1317 		set_bit(BSS_MEMBERSHIP_SELECTOR_EHT_PHY, sta_selectors);
1318 	if (conn->mode >= IEEE80211_CONN_MODE_UHR)
1319 		set_bit(BSS_MEMBERSHIP_SELECTOR_UHR_PHY, sta_selectors);
1320 
1321 	/*
1322 	 * We do not support EPD or GLK so never add them.
1323 	 * SAE_H2E is handled through userspace_selectors.
1324 	 */
1325 
1326 	/* Check if we support all required features */
1327 	if (!bitmap_subset(unknown_rates_selectors, sta_selectors, 128)) {
1328 		link_id_info(sdata, link_id,
1329 			     "required basic rate or BSS membership selectors not supported or disabled, rejecting connection\n");
1330 		ret = -EINVAL;
1331 		goto free;
1332 	}
1333 
1334 	ieee80211_set_chanreq_ap(sdata, chanreq, conn, ap_chandef);
1335 
1336 	while (!ieee80211_chandef_usable(sdata, &chanreq->oper,
1337 					 IEEE80211_CHAN_DISABLED)) {
1338 		if (chanreq->oper.width == NL80211_CHAN_WIDTH_20_NOHT) {
1339 			link_id_info(sdata, link_id,
1340 				     "unusable channel (%d MHz) for connection\n",
1341 				     chanreq->oper.chan->center_freq);
1342 			ret = -EINVAL;
1343 			goto free;
1344 		}
1345 
1346 		ieee80211_chanreq_downgrade(chanreq, conn);
1347 	}
1348 
1349 	if (conn->mode >= IEEE80211_CONN_MODE_HE &&
1350 	    !cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
1351 				     IEEE80211_CHAN_NO_HE)) {
1352 		conn->mode = IEEE80211_CONN_MODE_VHT;
1353 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1354 				       conn->bw_limit,
1355 				       IEEE80211_CONN_BW_LIMIT_160);
1356 	}
1357 
1358 	if (conn->mode >= IEEE80211_CONN_MODE_EHT &&
1359 	    !cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
1360 				     IEEE80211_CHAN_NO_EHT)) {
1361 		conn->mode = IEEE80211_CONN_MODE_HE;
1362 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1363 				       conn->bw_limit,
1364 				       IEEE80211_CONN_BW_LIMIT_160);
1365 	}
1366 
1367 	if (conn->mode >= IEEE80211_CONN_MODE_UHR &&
1368 	    !cfg80211_chandef_usable(sdata->wdev.wiphy, &chanreq->oper,
1369 				     IEEE80211_CHAN_NO_UHR))
1370 		conn->mode = IEEE80211_CONN_MODE_EHT;
1371 
1372 	if (chanreq->oper.width != ap_chandef->width || ap_mode != conn->mode)
1373 		link_id_info(sdata, link_id,
1374 			     "regulatory prevented using AP config, downgraded\n");
1375 
1376 	if (conn->mode >= IEEE80211_CONN_MODE_HT &&
1377 	    !ieee80211_verify_sta_ht_mcs_support(sdata, sband,
1378 						 elems->ht_operation)) {
1379 		conn->mode = IEEE80211_CONN_MODE_LEGACY;
1380 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
1381 		link_id_info(sdata, link_id,
1382 			     "required MCSes not supported, disabling HT\n");
1383 	}
1384 
1385 	if (conn->mode >= IEEE80211_CONN_MODE_VHT &&
1386 	    !ieee80211_verify_sta_vht_mcs_support(sdata, link_id, sband,
1387 						  elems->vht_operation)) {
1388 		conn->mode = IEEE80211_CONN_MODE_HT;
1389 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1390 				       conn->bw_limit,
1391 				       IEEE80211_CONN_BW_LIMIT_40);
1392 		link_id_info(sdata, link_id,
1393 			     "required MCSes not supported, disabling VHT\n");
1394 	}
1395 
1396 	if (conn->mode >= IEEE80211_CONN_MODE_HE &&
1397 	    (!ieee80211_verify_peer_he_mcs_support(sdata, link_id,
1398 						   (void *)elems->he_cap,
1399 						   elems->he_operation) ||
1400 	     !ieee80211_verify_sta_he_mcs_support(sdata, sband,
1401 						  elems->he_operation))) {
1402 		conn->mode = IEEE80211_CONN_MODE_VHT;
1403 		link_id_info(sdata, link_id,
1404 			     "required MCSes not supported, disabling HE\n");
1405 	}
1406 
1407 	if (conn->mode >= IEEE80211_CONN_MODE_EHT &&
1408 	    !ieee80211_verify_sta_eht_mcs_support(sdata, sband,
1409 						  elems->eht_operation)) {
1410 		conn->mode = IEEE80211_CONN_MODE_HE;
1411 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
1412 				       conn->bw_limit,
1413 				       IEEE80211_CONN_BW_LIMIT_160);
1414 		link_id_info(sdata, link_id,
1415 			     "required MCSes not supported, disabling EHT\n");
1416 	}
1417 
1418 	if (conn->mode >= IEEE80211_CONN_MODE_EHT &&
1419 	    channel->band != NL80211_BAND_2GHZ &&
1420 	    conn->bw_limit == IEEE80211_CONN_BW_LIMIT_40) {
1421 		conn->mode = IEEE80211_CONN_MODE_HE;
1422 		link_id_info(sdata, link_id,
1423 			     "required bandwidth not supported, disabling EHT\n");
1424 	}
1425 
1426 	/* the mode can only decrease, so this must terminate */
1427 	if (ap_mode != conn->mode) {
1428 		kfree(elems);
1429 		goto again;
1430 	}
1431 
1432 	mlme_link_id_dbg(sdata, link_id,
1433 			 "connecting with %s mode, max bandwidth %d MHz\n",
1434 			 ieee80211_conn_mode_str(conn->mode),
1435 			 20 * (1 << conn->bw_limit));
1436 
1437 	if (WARN_ON_ONCE(!cfg80211_chandef_valid(&chanreq->oper))) {
1438 		ret = -EINVAL;
1439 		goto free;
1440 	}
1441 
1442 	if (conn->dbe_enabled && link_sta)
1443 		link_sta->uhr_usable_tx_width =
1444 			ieee80211_chan_width_to_rx_bw(ap_chan_out.non_dbe_width);
1445 
1446 	return elems;
1447 free:
1448 	kfree(elems);
1449 	return ERR_PTR(ret);
1450 }
1451 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_determine_chan_mode);
1452 
1453 static void ieee80211_send_uhr_omp_req_dbe(struct ieee80211_sub_if_data *sdata,
1454 					   u16 link_mask, bool initial)
1455 {
1456 	struct ieee80211_mle_basic_common_info *common;
1457 	struct ieee80211_mle_per_sta_profile *per_sta;
1458 	struct ieee80211_uhr_mode_change_tuple *tuple;
1459 	struct ieee80211_local *local = sdata->local;
1460 	struct ieee80211_multi_link_elem *mle;
1461 	struct ieee80211_link_data *link;
1462 	struct ieee80211_tx_info *info;
1463 	struct ieee80211_mgmt *mgmt;
1464 	struct sk_buff *skb;
1465 	u8 *ml_elem_len;
1466 	size_t size;
1467 
1468 	if (initial) {
1469 		bool enabled = false;
1470 
1471 		for_each_link_data(sdata, link) {
1472 			if (!(link_mask & BIT(link->link_id)))
1473 				continue;
1474 			if (link->u.mgd.conn.dbe_enabled) {
1475 				enabled = true;
1476 				break;
1477 			}
1478 		}
1479 
1480 		if (!enabled)
1481 			return;
1482 	}
1483 
1484 	if (sdata->u.mgd.uhr_omp.links) {
1485 		if (initial)
1486 			sdata->u.mgd.uhr_omp.pending_init |= link_mask;
1487 		else
1488 			sdata->u.mgd.uhr_omp.pending |= link_mask;
1489 		return;
1490 	}
1491 
1492 	size = local->hw.extra_tx_headroom +
1493 	       IEEE80211_MIN_ACTION_SIZE(uhr_link_reconf_req) +
1494 	       3 + sizeof(*mle) + sizeof(*common) +
1495 	       IEEE80211_MLD_MAX_NUM_LINKS *
1496 		(2 + sizeof(*per_sta) +
1497 		 3 + sizeof(*tuple) /* single tuple for each link */);
1498 
1499 	skb = alloc_skb(size, GFP_KERNEL);
1500 	if (!skb)
1501 		return;
1502 
1503 	skb_reserve(skb, local->hw.extra_tx_headroom);
1504 
1505 	mgmt = skb_put_zero(skb, IEEE80211_MIN_ACTION_SIZE(uhr_link_reconf_req));
1506 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1507 					  IEEE80211_STYPE_ACTION);
1508 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
1509 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1510 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
1511 
1512 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_UHR;
1513 	mgmt->u.action.action_code =
1514 		IEEE80211_PROTECTED_UHR_ACTION_LINK_RECONFIG_REQUEST;
1515 
1516 	sdata->u.mgd.dialog_token_alloc++;
1517 	/*
1518 	 * NOTE:
1519 	 * Driver and FW might both send these frames, and iwlwifi
1520 	 * decided that the driver uses odd numbers, FW uses even
1521 	 * numbers. For now, hardcode that here, until it matters
1522 	 * to some other driver.
1523 	 *
1524 	 * Note also that there's currently no real synchronisation
1525 	 * in this case, but it's not valid that both send such a
1526 	 * frame at the same time, i.e. while waiting for a response
1527 	 * there can't be another frame sent. This needs addressing
1528 	 * in the future.
1529 	 */
1530 	if (sdata->u.mgd.dialog_token_alloc % 2 == 0)
1531 		sdata->u.mgd.dialog_token_alloc++;
1532 
1533 	sdata->u.mgd.uhr_omp.dialog_token = sdata->u.mgd.dialog_token_alloc;
1534 	mgmt->u.action.uhr_link_reconf_req.dialog_token =
1535 		sdata->u.mgd.uhr_omp.dialog_token;
1536 	mgmt->u.action.uhr_link_reconf_req.type =
1537 		IEEE80211_UHR_LINK_RECONFIG_REQUEST_OMP_REQUEST;
1538 
1539 	skb_put_u8(skb, WLAN_EID_EXTENSION);
1540 	ml_elem_len = skb_put(skb, 1);
1541 	skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
1542 	mle = skb_put_zero(skb, sizeof(*mle));
1543 	mle->control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_RECONF |
1544 				   IEEE80211_MLC_RECONF_PRES_MLD_MAC_ADDR);
1545 
1546 	common = skb_put(skb, sizeof(*common));
1547 	common->len = sizeof(*common);
1548 	memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1549 
1550 	for_each_link_data(sdata, link) {
1551 		u8 *subelem_len;
1552 
1553 		if (!(link_mask & BIT(link->link_id)))
1554 			continue;
1555 
1556 		sdata->u.mgd.uhr_omp.links |= BIT(link->link_id);
1557 
1558 		skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
1559 		subelem_len = skb_put(skb, 1);
1560 		per_sta = skb_put_zero(skb, sizeof(*per_sta));
1561 		per_sta->control =
1562 			le16_encode_bits(link->link_id,
1563 					 IEEE80211_MLE_STA_CONTROL_LINK_ID) |
1564 			le16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_UHR_OMP_UPD,
1565 					 IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
1566 		per_sta->sta_info_len = 1; /* includes itself */
1567 
1568 		skb_put_u8(skb, WLAN_EID_EXTENSION);
1569 		skb_put_u8(skb, 1 + sizeof(*tuple));
1570 		skb_put_u8(skb, WLAN_EID_EXT_UHR_MODE_CHG);
1571 		tuple = skb_put_zero(skb, sizeof(*tuple));
1572 		tuple->control =
1573 			le16_encode_bits(IEEE80211_UHR_MODE_CHANGE_MODE_ID_DBE,
1574 					 IEEE80211_UHR_MODE_CHANGE_CONTROL_MODE_ID);
1575 
1576 		if (link->u.mgd.conn.dbe_enabled)
1577 			tuple->control |=
1578 				cpu_to_le16(IEEE80211_UHR_MODE_CHANGE_CONTROL_MODE_ENABLE);
1579 
1580 		ieee80211_fragment_element(skb, subelem_len,
1581 					   IEEE80211_MLE_SUBELEM_FRAGMENT);
1582 	}
1583 
1584 	ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT);
1585 
1586 	info = IEEE80211_SKB_CB(skb);
1587 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1588 	info->status_data = IEEE80211_STATUS_TYPE_UHR_OMP;
1589 	ieee80211_tx_skb(sdata, skb);
1590 }
1591 
1592 static int ieee80211_config_bw(struct ieee80211_link_data *link,
1593 			       struct ieee802_11_elems *elems,
1594 			       bool update, u64 *changed, u16 stype)
1595 {
1596 	struct ieee80211_channel *channel = link->conf->chanreq.oper.chan;
1597 	struct cfg80211_chan_def ap_chandef;
1598 	struct ieee80211_determine_ap_chan_output ap_chan_out;
1599 	struct ieee80211_determine_ap_chan_data ap_chan_data = {
1600 		.channel = channel,
1601 		.vht_cap_info = 0,
1602 		.ignore_ht_channel_mismatch = true,
1603 		.chandef = &ap_chandef,
1604 		.elems = elems,
1605 		.conn = &link->u.mgd.conn,
1606 		.cur_chandef = &link->conf->chanreq.oper,
1607 		.cur_dbe_used = link->u.mgd.conn.dbe_enabled,
1608 	};
1609 	struct ieee80211_sub_if_data *sdata = link->sdata;
1610 	struct ieee80211_chanctx_conf *chanctx_conf;
1611 	struct ieee80211_chan_req chanreq = {};
1612 	enum ieee80211_conn_mode ap_mode;
1613 	const char *frame;
1614 	u16 ht_opmode;
1615 	int ret;
1616 
1617 	switch (stype) {
1618 	case IEEE80211_STYPE_BEACON:
1619 		frame = "beacon";
1620 		break;
1621 	case IEEE80211_STYPE_ASSOC_RESP:
1622 		frame = "assoc response";
1623 		break;
1624 	case IEEE80211_STYPE_REASSOC_RESP:
1625 		frame = "reassoc response";
1626 		break;
1627 	case IEEE80211_STYPE_ACTION:
1628 		/* the only action frame that gets here */
1629 		frame = "ML reconf response";
1630 		break;
1631 	default:
1632 		return -EINVAL;
1633 	}
1634 
1635 	/* don't track any bandwidth changes in legacy/S1G modes */
1636 	if (link->u.mgd.conn.mode == IEEE80211_CONN_MODE_LEGACY ||
1637 	    link->u.mgd.conn.mode == IEEE80211_CONN_MODE_S1G)
1638 		return 0;
1639 
1640 	if (elems->vht_cap_elem)
1641 		ap_chan_data.vht_cap_info =
1642 			le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1643 
1644 	ap_mode = ieee80211_determine_ap_chan(sdata, &ap_chan_data,
1645 					      &ap_chan_out);
1646 	link->u.mgd.conn.dbe_enabled = ap_chan_out.dbe_used;
1647 
1648 	if (ap_mode != link->u.mgd.conn.mode) {
1649 		link_info(link,
1650 			  "AP %pM appears to change mode (expected %s, found %s) in %s, disconnect\n",
1651 			  link->u.mgd.bssid,
1652 			  ieee80211_conn_mode_str(link->u.mgd.conn.mode),
1653 			  ieee80211_conn_mode_str(ap_mode), frame);
1654 		return -EINVAL;
1655 	}
1656 
1657 	chanreq.oper = ap_chandef;
1658 	ieee80211_set_chanreq_ap(sdata, &chanreq, &link->u.mgd.conn,
1659 				 &ap_chandef);
1660 
1661 	/*
1662 	 * if HT operation mode changed store the new one -
1663 	 * this may be applicable even if channel is identical
1664 	 */
1665 	if (elems->ht_operation) {
1666 		ht_opmode = le16_to_cpu(elems->ht_operation->operation_mode);
1667 		if (link->conf->ht_operation_mode != ht_opmode) {
1668 			*changed |= BSS_CHANGED_HT;
1669 			link->conf->ht_operation_mode = ht_opmode;
1670 		}
1671 	}
1672 
1673 	/*
1674 	 * Downgrade the new channel if we associated with restricted
1675 	 * bandwidth capabilities. For example, if we associated as a
1676 	 * 20 MHz STA to a 40 MHz AP (due to regulatory, capabilities
1677 	 * or config reasons) then switching to a 40 MHz channel now
1678 	 * won't do us any good -- we couldn't use it with the AP.
1679 	 */
1680 	while (link->u.mgd.conn.bw_limit <
1681 			ieee80211_min_bw_limit_from_chandef(&chanreq.oper))
1682 		ieee80211_chandef_downgrade(&chanreq.oper, NULL);
1683 
1684 	/* TPE element is not present in (re)assoc/ML reconfig response */
1685 	if (stype == IEEE80211_STYPE_BEACON &&
1686 	    ap_chandef.chan->band == NL80211_BAND_6GHZ &&
1687 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE) {
1688 		ieee80211_rearrange_tpe(&elems->tpe, &ap_chandef,
1689 					&chanreq.oper);
1690 		if (memcmp(&link->conf->tpe, &elems->tpe, sizeof(elems->tpe))) {
1691 			link->conf->tpe = elems->tpe;
1692 			*changed |= BSS_CHANGED_TPE;
1693 		}
1694 	}
1695 
1696 	/*
1697 	 * Beacons don't have the full information - we need to track
1698 	 * critical updates for NPCA parameters etc. For now only handle
1699 	 * association and link reconfiguration response.
1700 	 */
1701 	if (stype != IEEE80211_STYPE_BEACON &&
1702 	    chanreq.oper.npca_chan && elems->uhr_operation &&
1703 	    ieee80211_uhr_oper_size_ok((const void *)elems->uhr_operation,
1704 				       elems->uhr_operation_len,
1705 				       false)) {
1706 		const struct ieee80211_uhr_npca_info *npca;
1707 		struct ieee80211_bss_npca_params params = {};
1708 
1709 		npca = ieee80211_uhr_npca_info(elems->uhr_operation);
1710 		if (!npca) {
1711 			chanreq.oper.npca_chan = NULL;
1712 			chanreq.oper.npca_punctured = 0;
1713 		} else {
1714 			params.min_dur_thresh =
1715 				le32_get_bits(npca->params,
1716 					      IEEE80211_UHR_NPCA_PARAMS_MIN_DUR_THRESH);
1717 			params.switch_delay =
1718 				le32_get_bits(npca->params,
1719 					      IEEE80211_UHR_NPCA_PARAMS_SWITCH_DELAY);
1720 			params.switch_back_delay =
1721 				le32_get_bits(npca->params,
1722 					      IEEE80211_UHR_NPCA_PARAMS_SWITCH_BACK_DELAY);
1723 			params.init_qsrc =
1724 				le32_get_bits(npca->params,
1725 					      IEEE80211_UHR_NPCA_PARAMS_INIT_QSRC);
1726 			params.moplen =
1727 				le32_get_bits(npca->params,
1728 					      IEEE80211_UHR_NPCA_PARAMS_MOPLEN);
1729 			/* don't change the enabled bit yet */
1730 			params.enabled = link->conf->npca.enabled;
1731 		}
1732 
1733 		if (memcmp(&params, &link->conf->npca, sizeof(params)) ||
1734 		    !update) {
1735 			link->conf->npca = params;
1736 			*changed |= BSS_CHANGED_NPCA;
1737 		}
1738 	}
1739 
1740 	if (ieee80211_chanreq_identical(&chanreq, &link->conf->chanreq)) {
1741 		if (update)
1742 			goto update_npca;
1743 		return 0;
1744 	}
1745 
1746 	link_info(link,
1747 		  "AP %pM changed bandwidth in %s, new used config is %d.%03d MHz, width %d (%d.%03d/%d MHz)\n",
1748 		  link->u.mgd.bssid, frame, chanreq.oper.chan->center_freq,
1749 		  chanreq.oper.chan->freq_offset, chanreq.oper.width,
1750 		  chanreq.oper.center_freq1, chanreq.oper.freq1_offset,
1751 		  chanreq.oper.center_freq2);
1752 
1753 	if (!cfg80211_chandef_valid(&chanreq.oper)) {
1754 		sdata_info(sdata,
1755 			   "AP %pM changed caps/bw in %s in a way we can't support - disconnect\n",
1756 			   link->u.mgd.bssid, frame);
1757 		return -EINVAL;
1758 	}
1759 
1760 	if (!update) {
1761 		link->conf->chanreq = chanreq;
1762 		return 0;
1763 	}
1764 
1765 	/*
1766 	 * We're tracking the current AP here, so don't do any further checks
1767 	 * here. This keeps us from playing ping-pong with regulatory, without
1768 	 * it the following can happen (for example):
1769 	 *  - connect to an AP with 80 MHz, world regdom allows 80 MHz
1770 	 *  - AP advertises regdom US
1771 	 *  - CRDA loads regdom US with 80 MHz prohibited (old database)
1772 	 *  - we detect an unsupported channel and disconnect
1773 	 *  - disconnect causes CRDA to reload world regdomain and the game
1774 	 *    starts anew.
1775 	 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
1776 	 *
1777 	 * It seems possible that there are still scenarios with CSA or real
1778 	 * bandwidth changes where a this could happen, but those cases are
1779 	 * less common and wouldn't completely prevent using the AP.
1780 	 */
1781 
1782 	ret = ieee80211_link_change_chanreq(link, &chanreq, changed);
1783 	if (ret) {
1784 		sdata_info(sdata,
1785 			   "AP %pM changed bandwidth in %s to incompatible one - disconnect\n",
1786 			   link->u.mgd.bssid, frame);
1787 		return ret;
1788 	}
1789 
1790 	cfg80211_schedule_channels_check(&sdata->wdev);
1791 
1792 update_npca:
1793 	chanctx_conf = sdata_dereference(link->conf->chanctx_conf, sdata);
1794 	/* must be non-NULL when update is true */
1795 	if (WARN_ON(!chanctx_conf))
1796 		return -EINVAL;
1797 
1798 	/*
1799 	 * If we're not associated yet (i.e. in the process associating)
1800 	 * then the chanctx code won't have enabled NPCA in the link, so
1801 	 * if the channel context was set up with NPCA for us, enable it.
1802 	 */
1803 	if (chanreq.oper.npca_chan && chanctx_conf->def.npca_chan &&
1804 	    !link->conf->npca.enabled && !sdata->vif.cfg.assoc) {
1805 		link->conf->npca.enabled = true;
1806 		*changed |= BSS_CHANGED_NPCA;
1807 	}
1808 
1809 	return 0;
1810 }
1811 
1812 /* frame sending functions */
1813 
1814 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
1815 				struct sk_buff *skb, u8 ap_ht_param,
1816 				struct ieee80211_supported_band *sband,
1817 				struct ieee80211_channel *channel,
1818 				enum ieee80211_smps_mode smps,
1819 				const struct ieee80211_conn_settings *conn)
1820 {
1821 	u8 *pos;
1822 	u32 flags = channel->flags;
1823 	u16 cap;
1824 	struct ieee80211_sta_ht_cap ht_cap;
1825 
1826 	BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
1827 
1828 	memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
1829 	ieee80211_apply_htcap_overrides(sdata, &ht_cap);
1830 
1831 	/* determine capability flags */
1832 	cap = ht_cap.cap;
1833 
1834 	switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
1835 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1836 		if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
1837 			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1838 			cap &= ~IEEE80211_HT_CAP_SGI_40;
1839 		}
1840 		break;
1841 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1842 		if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
1843 			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1844 			cap &= ~IEEE80211_HT_CAP_SGI_40;
1845 		}
1846 		break;
1847 	}
1848 
1849 	/*
1850 	 * If 40 MHz was disabled associate as though we weren't
1851 	 * capable of 40 MHz -- some broken APs will never fall
1852 	 * back to trying to transmit in 20 MHz.
1853 	 */
1854 	if (conn->bw_limit <= IEEE80211_CONN_BW_LIMIT_20) {
1855 		cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1856 		cap &= ~IEEE80211_HT_CAP_SGI_40;
1857 	}
1858 
1859 	/* set SM PS mode properly */
1860 	cap &= ~IEEE80211_HT_CAP_SM_PS;
1861 	switch (smps) {
1862 	case IEEE80211_SMPS_AUTOMATIC:
1863 	case IEEE80211_SMPS_NUM_MODES:
1864 		WARN_ON(1);
1865 		fallthrough;
1866 	case IEEE80211_SMPS_OFF:
1867 		cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
1868 			IEEE80211_HT_CAP_SM_PS_SHIFT;
1869 		break;
1870 	case IEEE80211_SMPS_STATIC:
1871 		cap |= WLAN_HT_CAP_SM_PS_STATIC <<
1872 			IEEE80211_HT_CAP_SM_PS_SHIFT;
1873 		break;
1874 	case IEEE80211_SMPS_DYNAMIC:
1875 		cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
1876 			IEEE80211_HT_CAP_SM_PS_SHIFT;
1877 		break;
1878 	}
1879 
1880 	/* reserve and fill IE */
1881 	pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
1882 	ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
1883 }
1884 
1885 /* This function determines vht capability flags for the association
1886  * and builds the IE.
1887  * Note - the function returns true to own the MU-MIMO capability
1888  */
1889 static bool ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
1890 				 struct sk_buff *skb,
1891 				 struct ieee80211_supported_band *sband,
1892 				 struct ieee80211_vht_cap *ap_vht_cap,
1893 				 const struct ieee80211_conn_settings *conn)
1894 {
1895 	struct ieee80211_local *local = sdata->local;
1896 	u8 *pos;
1897 	u32 cap;
1898 	struct ieee80211_sta_vht_cap vht_cap;
1899 	u32 mask, ap_bf_sts, our_bf_sts;
1900 	bool mu_mimo_owner = false;
1901 
1902 	BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
1903 
1904 	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
1905 	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
1906 
1907 	/* determine capability flags */
1908 	cap = vht_cap.cap;
1909 
1910 	if (conn->bw_limit <= IEEE80211_CONN_BW_LIMIT_80) {
1911 		cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
1912 		cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1913 	}
1914 
1915 	/*
1916 	 * Some APs apparently get confused if our capabilities are better
1917 	 * than theirs, so restrict what we advertise in the assoc request.
1918 	 */
1919 	if (!ieee80211_hw_check(&local->hw, STRICT)) {
1920 		if (!(ap_vht_cap->vht_cap_info &
1921 				cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
1922 			cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1923 				 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
1924 		else if (!(ap_vht_cap->vht_cap_info &
1925 				cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
1926 			cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1927 	}
1928 
1929 	/*
1930 	 * If some other vif is using the MU-MIMO capability we cannot associate
1931 	 * using MU-MIMO - this will lead to contradictions in the group-id
1932 	 * mechanism.
1933 	 * Ownership is defined since association request, in order to avoid
1934 	 * simultaneous associations with MU-MIMO.
1935 	 */
1936 	if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
1937 		bool disable_mu_mimo = false;
1938 		struct ieee80211_sub_if_data *other;
1939 
1940 		list_for_each_entry(other, &local->interfaces, list) {
1941 			if (other->vif.bss_conf.mu_mimo_owner) {
1942 				disable_mu_mimo = true;
1943 				break;
1944 			}
1945 		}
1946 		if (disable_mu_mimo)
1947 			cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1948 		else
1949 			mu_mimo_owner = true;
1950 	}
1951 
1952 	mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
1953 
1954 	ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
1955 	our_bf_sts = cap & mask;
1956 
1957 	if (ap_bf_sts < our_bf_sts) {
1958 		cap &= ~mask;
1959 		cap |= ap_bf_sts;
1960 	}
1961 
1962 	/* reserve and fill IE */
1963 	pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
1964 	ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
1965 
1966 	return mu_mimo_owner;
1967 }
1968 
1969 static void ieee80211_assoc_add_rates(struct ieee80211_local *local,
1970 				      struct sk_buff *skb,
1971 				      enum nl80211_chan_width width,
1972 				      struct ieee80211_supported_band *sband,
1973 				      struct ieee80211_mgd_assoc_data *assoc_data)
1974 {
1975 	u32 rates;
1976 
1977 	if (assoc_data->supp_rates_len &&
1978 	    !ieee80211_hw_check(&local->hw, STRICT)) {
1979 		/*
1980 		 * Get all rates supported by the device and the AP as
1981 		 * some APs don't like getting a superset of their rates
1982 		 * in the association request (e.g. D-Link DAP 1353 in
1983 		 * b-only mode)...
1984 		 */
1985 		ieee80211_parse_bitrates(sband,
1986 					 assoc_data->supp_rates,
1987 					 assoc_data->supp_rates_len,
1988 					 &rates);
1989 	} else {
1990 		/*
1991 		 * In case AP not provide any supported rates information
1992 		 * before association, we send information element(s) with
1993 		 * all rates that we support.
1994 		 */
1995 		rates = ~0;
1996 	}
1997 
1998 	ieee80211_put_srates_elem(skb, sband, 0, ~rates,
1999 				  WLAN_EID_SUPP_RATES);
2000 	ieee80211_put_srates_elem(skb, sband, 0, ~rates,
2001 				  WLAN_EID_EXT_SUPP_RATES);
2002 }
2003 
2004 static size_t ieee80211_add_before_ht_elems(struct sk_buff *skb,
2005 					    const u8 *elems,
2006 					    size_t elems_len,
2007 					    size_t offset)
2008 {
2009 	size_t noffset;
2010 
2011 	static const u8 before_ht[] = {
2012 		WLAN_EID_SSID,
2013 		WLAN_EID_SUPP_RATES,
2014 		WLAN_EID_EXT_SUPP_RATES,
2015 		WLAN_EID_PWR_CAPABILITY,
2016 		WLAN_EID_SUPPORTED_CHANNELS,
2017 		WLAN_EID_RSN,
2018 		WLAN_EID_QOS_CAPA,
2019 		WLAN_EID_RRM_ENABLED_CAPABILITIES,
2020 		WLAN_EID_MOBILITY_DOMAIN,
2021 		WLAN_EID_FAST_BSS_TRANSITION,	/* reassoc only */
2022 		WLAN_EID_RIC_DATA,		/* reassoc only */
2023 		WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2024 	};
2025 	static const u8 after_ric[] = {
2026 		WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2027 		WLAN_EID_HT_CAPABILITY,
2028 		WLAN_EID_BSS_COEX_2040,
2029 		/* luckily this is almost always there */
2030 		WLAN_EID_EXT_CAPABILITY,
2031 		WLAN_EID_QOS_TRAFFIC_CAPA,
2032 		WLAN_EID_TIM_BCAST_REQ,
2033 		WLAN_EID_INTERWORKING,
2034 		/* 60 GHz (Multi-band, DMG, MMS) can't happen */
2035 		WLAN_EID_VHT_CAPABILITY,
2036 		WLAN_EID_OPMODE_NOTIF,
2037 	};
2038 
2039 	if (!elems_len)
2040 		return offset;
2041 
2042 	noffset = ieee80211_ie_split_ric(elems, elems_len,
2043 					 before_ht,
2044 					 ARRAY_SIZE(before_ht),
2045 					 after_ric,
2046 					 ARRAY_SIZE(after_ric),
2047 					 offset);
2048 	skb_put_data(skb, elems + offset, noffset - offset);
2049 
2050 	return noffset;
2051 }
2052 
2053 static size_t ieee80211_add_before_vht_elems(struct sk_buff *skb,
2054 					     const u8 *elems,
2055 					     size_t elems_len,
2056 					     size_t offset)
2057 {
2058 	static const u8 before_vht[] = {
2059 		/*
2060 		 * no need to list the ones split off before HT
2061 		 * or generated here
2062 		 */
2063 		WLAN_EID_BSS_COEX_2040,
2064 		WLAN_EID_EXT_CAPABILITY,
2065 		WLAN_EID_QOS_TRAFFIC_CAPA,
2066 		WLAN_EID_TIM_BCAST_REQ,
2067 		WLAN_EID_INTERWORKING,
2068 		/* 60 GHz (Multi-band, DMG, MMS) can't happen */
2069 	};
2070 	size_t noffset;
2071 
2072 	if (!elems_len)
2073 		return offset;
2074 
2075 	/* RIC already taken care of in ieee80211_add_before_ht_elems() */
2076 	noffset = ieee80211_ie_split(elems, elems_len,
2077 				     before_vht, ARRAY_SIZE(before_vht),
2078 				     offset);
2079 	skb_put_data(skb, elems + offset, noffset - offset);
2080 
2081 	return noffset;
2082 }
2083 
2084 static size_t ieee80211_add_before_he_elems(struct sk_buff *skb,
2085 					    const u8 *elems,
2086 					    size_t elems_len,
2087 					    size_t offset)
2088 {
2089 	static const u8 before_he[] = {
2090 		/*
2091 		 * no need to list the ones split off before VHT
2092 		 * or generated here
2093 		 */
2094 		WLAN_EID_OPMODE_NOTIF,
2095 		WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
2096 		/* 11ai elements */
2097 		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
2098 		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
2099 		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
2100 		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
2101 		WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
2102 		/* TODO: add 11ah/11aj/11ak elements */
2103 	};
2104 	size_t noffset;
2105 
2106 	if (!elems_len)
2107 		return offset;
2108 
2109 	/* RIC already taken care of in ieee80211_add_before_ht_elems() */
2110 	noffset = ieee80211_ie_split(elems, elems_len,
2111 				     before_he, ARRAY_SIZE(before_he),
2112 				     offset);
2113 	skb_put_data(skb, elems + offset, noffset - offset);
2114 
2115 	return noffset;
2116 }
2117 
2118 static size_t ieee80211_add_before_reg_conn(struct sk_buff *skb,
2119 					    const u8 *elems, size_t elems_len,
2120 					    size_t offset)
2121 {
2122 	static const u8 before_reg_conn[] = {
2123 		/*
2124 		 * no need to list the ones split off before HE
2125 		 * or generated here
2126 		 */
2127 		WLAN_EID_EXTENSION, WLAN_EID_EXT_DH_PARAMETER,
2128 		WLAN_EID_EXTENSION, WLAN_EID_EXT_KNOWN_STA_IDENTIFCATION,
2129 	};
2130 	size_t noffset;
2131 
2132 	if (!elems_len)
2133 		return offset;
2134 
2135 	noffset = ieee80211_ie_split(elems, elems_len, before_reg_conn,
2136 				     ARRAY_SIZE(before_reg_conn), offset);
2137 	skb_put_data(skb, elems + offset, noffset - offset);
2138 
2139 	return noffset;
2140 }
2141 
2142 #define PRESENT_ELEMS_MAX	8
2143 #define PRESENT_ELEM_EXT_OFFS	0x100
2144 
2145 static void
2146 ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata,
2147 			    struct sk_buff *skb, u16 capab,
2148 			    const struct element *ext_capa,
2149 			    const u16 *present_elems,
2150 			    struct ieee80211_mgd_assoc_data *assoc_data);
2151 
2152 static size_t
2153 ieee80211_add_link_elems(struct ieee80211_sub_if_data *sdata,
2154 			 struct sk_buff *skb, u16 *capab,
2155 			 const struct element *ext_capa,
2156 			 const u8 *extra_elems,
2157 			 size_t extra_elems_len,
2158 			 unsigned int link_id,
2159 			 struct ieee80211_link_data *link,
2160 			 u16 *present_elems,
2161 			 struct ieee80211_mgd_assoc_data *assoc_data)
2162 {
2163 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
2164 	struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
2165 	struct ieee80211_channel *chan = cbss->channel;
2166 	const struct ieee80211_sband_iftype_data *iftd;
2167 	struct ieee80211_local *local = sdata->local;
2168 	struct ieee80211_supported_band *sband;
2169 	enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20;
2170 	struct ieee80211_chanctx_conf *chanctx_conf;
2171 	enum ieee80211_smps_mode smps_mode;
2172 	u16 orig_capab = *capab;
2173 	size_t offset = 0;
2174 	int present_elems_len = 0;
2175 	u8 *pos;
2176 	int i;
2177 
2178 #define ADD_PRESENT_ELEM(id) do {					\
2179 	/* need a last for termination - we use 0 == SSID */		\
2180 	if (!WARN_ON(present_elems_len >= PRESENT_ELEMS_MAX - 1))	\
2181 		present_elems[present_elems_len++] = (id);		\
2182 } while (0)
2183 #define ADD_PRESENT_EXT_ELEM(id) ADD_PRESENT_ELEM(PRESENT_ELEM_EXT_OFFS | (id))
2184 
2185 	if (link)
2186 		smps_mode = link->smps_mode;
2187 	else if (sdata->u.mgd.powersave)
2188 		smps_mode = IEEE80211_SMPS_DYNAMIC;
2189 	else
2190 		smps_mode = IEEE80211_SMPS_OFF;
2191 
2192 	if (link) {
2193 		/*
2194 		 * 5/10 MHz scenarios are only viable without MLO, in which
2195 		 * case this pointer should be used ... All of this is a bit
2196 		 * unclear though, not sure this even works at all.
2197 		 */
2198 		rcu_read_lock();
2199 		chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
2200 		if (chanctx_conf)
2201 			width = chanctx_conf->def.width;
2202 		rcu_read_unlock();
2203 	}
2204 
2205 	sband = local->hw.wiphy->bands[chan->band];
2206 	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
2207 
2208 	if (sband->band == NL80211_BAND_2GHZ) {
2209 		*capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
2210 		*capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
2211 	}
2212 
2213 	if ((cbss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
2214 	    ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
2215 		*capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
2216 
2217 	if (sband->band != NL80211_BAND_S1GHZ)
2218 		ieee80211_assoc_add_rates(local, skb, width, sband, assoc_data);
2219 
2220 	if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
2221 	    *capab & WLAN_CAPABILITY_RADIO_MEASURE) {
2222 		struct cfg80211_chan_def chandef = {
2223 			.width = width,
2224 			.chan = chan,
2225 		};
2226 
2227 		pos = skb_put(skb, 4);
2228 		*pos++ = WLAN_EID_PWR_CAPABILITY;
2229 		*pos++ = 2;
2230 		*pos++ = 0; /* min tx power */
2231 		 /* max tx power */
2232 		*pos++ = ieee80211_chandef_max_power(&chandef);
2233 		ADD_PRESENT_ELEM(WLAN_EID_PWR_CAPABILITY);
2234 	}
2235 
2236 	/*
2237 	 * Per spec, we shouldn't include the list of channels if we advertise
2238 	 * support for extended channel switching, but we've always done that;
2239 	 * (for now?) apply this restriction only on the (new) 6 GHz band.
2240 	 */
2241 	if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
2242 	    (sband->band != NL80211_BAND_6GHZ ||
2243 	     !ext_capa || ext_capa->datalen < 1 ||
2244 	     !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
2245 		/* TODO: get this in reg domain format */
2246 		pos = skb_put(skb, 2 * sband->n_channels + 2);
2247 		*pos++ = WLAN_EID_SUPPORTED_CHANNELS;
2248 		*pos++ = 2 * sband->n_channels;
2249 		for (i = 0; i < sband->n_channels; i++) {
2250 			int cf = sband->channels[i].center_freq;
2251 
2252 			*pos++ = ieee80211_frequency_to_channel(cf);
2253 			*pos++ = 1; /* one channel in the subband*/
2254 		}
2255 		ADD_PRESENT_ELEM(WLAN_EID_SUPPORTED_CHANNELS);
2256 	}
2257 
2258 	/* if present, add any custom IEs that go before HT */
2259 	offset = ieee80211_add_before_ht_elems(skb, extra_elems,
2260 					       extra_elems_len,
2261 					       offset);
2262 
2263 	if (sband->band != NL80211_BAND_6GHZ &&
2264 	    assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_HT) {
2265 		ieee80211_add_ht_ie(sdata, skb,
2266 				    assoc_data->link[link_id].ap_ht_param,
2267 				    sband, chan, smps_mode,
2268 				    &assoc_data->link[link_id].conn);
2269 		ADD_PRESENT_ELEM(WLAN_EID_HT_CAPABILITY);
2270 	}
2271 
2272 	/* if present, add any custom IEs that go before VHT */
2273 	offset = ieee80211_add_before_vht_elems(skb, extra_elems,
2274 						extra_elems_len,
2275 						offset);
2276 
2277 	if (sband->band != NL80211_BAND_6GHZ &&
2278 	    assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_VHT &&
2279 	    sband->vht_cap.vht_supported) {
2280 		bool mu_mimo_owner =
2281 			ieee80211_add_vht_ie(sdata, skb, sband,
2282 					     &assoc_data->link[link_id].ap_vht_cap,
2283 					     &assoc_data->link[link_id].conn);
2284 
2285 		if (link)
2286 			link->conf->mu_mimo_owner = mu_mimo_owner;
2287 		ADD_PRESENT_ELEM(WLAN_EID_VHT_CAPABILITY);
2288 	}
2289 
2290 	/* if present, add any custom IEs that go before HE */
2291 	offset = ieee80211_add_before_he_elems(skb, extra_elems,
2292 					       extra_elems_len,
2293 					       offset);
2294 
2295 	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_HE) {
2296 		ieee80211_put_he_cap(skb, sdata, sband,
2297 				     &assoc_data->link[link_id].conn);
2298 		ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_HE_CAPABILITY);
2299 		if (sband->band == NL80211_BAND_6GHZ)
2300 			ieee80211_put_he_6ghz_cap(skb, sdata, smps_mode);
2301 	}
2302 
2303 	/*
2304 	 * if present, add any custom IEs that go before regulatory
2305 	 * connectivity element
2306 	 */
2307 	offset = ieee80211_add_before_reg_conn(skb, extra_elems,
2308 					       extra_elems_len, offset);
2309 
2310 	if (sband->band == NL80211_BAND_6GHZ) {
2311 		/*
2312 		 * as per Section E.2.7 of IEEE 802.11 REVme D7.0, non-AP STA
2313 		 * capable of operating on the 6 GHz band shall transmit
2314 		 * regulatory connectivity element.
2315 		 */
2316 		ieee80211_put_reg_conn(skb, chan->flags);
2317 	}
2318 
2319 	/*
2320 	 * careful - need to know about all the present elems before
2321 	 * calling ieee80211_assoc_add_ml_elem(), so add these if
2322 	 * we're going to put them after the ML element
2323 	 */
2324 	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_EHT)
2325 		ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_EHT_CAPABILITY);
2326 	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_UHR)
2327 		ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_UHR_CAPA);
2328 
2329 	if (link_id == assoc_data->assoc_link_id)
2330 		ieee80211_assoc_add_ml_elem(sdata, skb, orig_capab, ext_capa,
2331 					    present_elems, assoc_data);
2332 
2333 	/* crash if somebody gets it wrong */
2334 	present_elems = NULL;
2335 
2336 	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_EHT)
2337 		ieee80211_put_eht_cap(skb, sdata, sband,
2338 				      &assoc_data->link[link_id].conn);
2339 
2340 	if (assoc_data->link[link_id].conn.mode >= IEEE80211_CONN_MODE_UHR)
2341 		ieee80211_put_uhr_cap(skb, sdata, sband);
2342 
2343 	if (sband->band == NL80211_BAND_S1GHZ) {
2344 		ieee80211_add_aid_request_ie(sdata, skb);
2345 		ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
2346 	}
2347 
2348 	if (iftd && iftd->vendor_elems.data && iftd->vendor_elems.len)
2349 		skb_put_data(skb, iftd->vendor_elems.data, iftd->vendor_elems.len);
2350 
2351 	return offset;
2352 }
2353 
2354 static void ieee80211_add_non_inheritance_elem(struct sk_buff *skb,
2355 					       const u16 *outer,
2356 					       const u16 *inner)
2357 {
2358 	unsigned int skb_len = skb->len;
2359 	bool at_extension = false;
2360 	bool added = false;
2361 	int i, j;
2362 	u8 *len, *list_len = NULL;
2363 
2364 	skb_put_u8(skb, WLAN_EID_EXTENSION);
2365 	len = skb_put(skb, 1);
2366 	skb_put_u8(skb, WLAN_EID_EXT_NON_INHERITANCE);
2367 
2368 	for (i = 0; i < PRESENT_ELEMS_MAX && outer[i]; i++) {
2369 		u16 elem = outer[i];
2370 		bool have_inner = false;
2371 
2372 		/* should at least be sorted in the sense of normal -> ext */
2373 		WARN_ON(at_extension && elem < PRESENT_ELEM_EXT_OFFS);
2374 
2375 		/* switch to extension list */
2376 		if (!at_extension && elem >= PRESENT_ELEM_EXT_OFFS) {
2377 			at_extension = true;
2378 			if (!list_len)
2379 				skb_put_u8(skb, 0);
2380 			list_len = NULL;
2381 		}
2382 
2383 		for (j = 0; j < PRESENT_ELEMS_MAX && inner[j]; j++) {
2384 			if (elem == inner[j]) {
2385 				have_inner = true;
2386 				break;
2387 			}
2388 		}
2389 
2390 		if (have_inner)
2391 			continue;
2392 
2393 		if (!list_len) {
2394 			list_len = skb_put(skb, 1);
2395 			*list_len = 0;
2396 		}
2397 		*list_len += 1;
2398 		skb_put_u8(skb, (u8)elem);
2399 		added = true;
2400 	}
2401 
2402 	/* if we added a list but no extension list, make a zero-len one */
2403 	if (added && (!at_extension || !list_len))
2404 		skb_put_u8(skb, 0);
2405 
2406 	/* if nothing added remove extension element completely */
2407 	if (!added)
2408 		skb_trim(skb, skb_len);
2409 	else
2410 		*len = skb->len - skb_len - 2;
2411 }
2412 
2413 static void
2414 ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata,
2415 			    struct sk_buff *skb, u16 capab,
2416 			    const struct element *ext_capa,
2417 			    const u16 *outer_present_elems,
2418 			    struct ieee80211_mgd_assoc_data *assoc_data)
2419 {
2420 	struct ieee80211_local *local = sdata->local;
2421 	struct ieee80211_multi_link_elem *ml_elem;
2422 	struct ieee80211_mle_basic_common_info *common;
2423 	const struct wiphy_iftype_ext_capab *ift_ext_capa;
2424 	__le16 eml_capa = 0, mld_capa_ops = 0;
2425 	unsigned int link_id;
2426 	u8 *ml_elem_len;
2427 	void *capab_pos;
2428 
2429 	if (!ieee80211_vif_is_mld(&sdata->vif))
2430 		return;
2431 
2432 	ift_ext_capa = cfg80211_get_iftype_ext_capa(local->hw.wiphy,
2433 						    ieee80211_vif_type_p2p(&sdata->vif));
2434 	if (ift_ext_capa) {
2435 		eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities);
2436 		mld_capa_ops = cpu_to_le16(ift_ext_capa->mld_capa_and_ops);
2437 	}
2438 
2439 	skb_put_u8(skb, WLAN_EID_EXTENSION);
2440 	ml_elem_len = skb_put(skb, 1);
2441 	skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
2442 	ml_elem = skb_put(skb, sizeof(*ml_elem));
2443 	ml_elem->control =
2444 		cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC |
2445 			    IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP);
2446 	common = skb_put(skb, sizeof(*common));
2447 	common->len = sizeof(*common) +
2448 		      2;  /* MLD capa/ops */
2449 	memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
2450 
2451 	/* add EML_CAPA only if needed, see Draft P802.11be_D2.1, 35.3.17 */
2452 	if (eml_capa &
2453 	    cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
2454 			 IEEE80211_EML_CAP_EMLMR_SUPPORT))) {
2455 		common->len += 2; /* EML capabilities */
2456 		ml_elem->control |=
2457 			cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EML_CAPA);
2458 		skb_put_data(skb, &eml_capa, sizeof(eml_capa));
2459 	}
2460 	skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops));
2461 
2462 	if (assoc_data->ext_mld_capa_ops) {
2463 		ml_elem->control |=
2464 			cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP);
2465 		common->len += 2;
2466 		skb_put_data(skb, &assoc_data->ext_mld_capa_ops,
2467 			     sizeof(assoc_data->ext_mld_capa_ops));
2468 	}
2469 
2470 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
2471 		u16 link_present_elems[PRESENT_ELEMS_MAX] = {};
2472 		const u8 *extra_elems;
2473 		size_t extra_elems_len;
2474 		size_t extra_used;
2475 		u8 *subelem_len = NULL;
2476 		__le16 ctrl;
2477 
2478 		if (!assoc_data->link[link_id].bss ||
2479 		    link_id == assoc_data->assoc_link_id)
2480 			continue;
2481 
2482 		extra_elems = assoc_data->link[link_id].elems;
2483 		extra_elems_len = assoc_data->link[link_id].elems_len;
2484 
2485 		skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
2486 		subelem_len = skb_put(skb, 1);
2487 
2488 		ctrl = cpu_to_le16(link_id |
2489 				   IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE |
2490 				   IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT);
2491 		skb_put_data(skb, &ctrl, sizeof(ctrl));
2492 		skb_put_u8(skb, 1 + ETH_ALEN); /* STA Info Length */
2493 		skb_put_data(skb, assoc_data->link[link_id].addr,
2494 			     ETH_ALEN);
2495 		/*
2496 		 * Now add the contents of the (re)association request,
2497 		 * but the "listen interval" and "current AP address"
2498 		 * (if applicable) are skipped. So we only have
2499 		 * the capability field (remember the position and fill
2500 		 * later), followed by the elements added below by
2501 		 * calling ieee80211_add_link_elems().
2502 		 */
2503 		capab_pos = skb_put(skb, 2);
2504 
2505 		extra_used = ieee80211_add_link_elems(sdata, skb, &capab,
2506 						      ext_capa,
2507 						      extra_elems,
2508 						      extra_elems_len,
2509 						      link_id, NULL,
2510 						      link_present_elems,
2511 						      assoc_data);
2512 		if (extra_elems)
2513 			skb_put_data(skb, extra_elems + extra_used,
2514 				     extra_elems_len - extra_used);
2515 
2516 		put_unaligned_le16(capab, capab_pos);
2517 
2518 		ieee80211_add_non_inheritance_elem(skb, outer_present_elems,
2519 						   link_present_elems);
2520 
2521 		ieee80211_fragment_element(skb, subelem_len,
2522 					   IEEE80211_MLE_SUBELEM_FRAGMENT);
2523 	}
2524 
2525 	ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT);
2526 }
2527 
2528 static int
2529 ieee80211_link_common_elems_size(struct ieee80211_sub_if_data *sdata,
2530 				 enum nl80211_iftype iftype,
2531 				 struct cfg80211_bss *cbss,
2532 				 size_t elems_len)
2533 {
2534 	struct ieee80211_local *local = sdata->local;
2535 	const struct ieee80211_sband_iftype_data *iftd;
2536 	struct ieee80211_supported_band *sband;
2537 	size_t size = 0;
2538 
2539 	if (!cbss)
2540 		return size;
2541 
2542 	sband = local->hw.wiphy->bands[cbss->channel->band];
2543 
2544 	/* add STA profile elements length */
2545 	size += elems_len;
2546 
2547 	/* and supported rates length */
2548 	size += 4 + sband->n_bitrates;
2549 
2550 	/* supported channels */
2551 	size += 2 + 2 * sband->n_channels;
2552 
2553 	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
2554 	if (iftd)
2555 		size += iftd->vendor_elems.len;
2556 
2557 	/* power capability */
2558 	size += 4;
2559 
2560 	/* HT, VHT, HE, EHT */
2561 	size += 2 + sizeof(struct ieee80211_ht_cap);
2562 	size += 2 + sizeof(struct ieee80211_vht_cap);
2563 	size += 2 + 1 + sizeof(struct ieee80211_he_cap_elem) +
2564 		sizeof(struct ieee80211_he_mcs_nss_supp) +
2565 		IEEE80211_HE_PPE_THRES_MAX_LEN;
2566 
2567 	if (sband->band == NL80211_BAND_6GHZ) {
2568 		size += 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa);
2569 		/* reg connection */
2570 		size += 4;
2571 	}
2572 
2573 	size += 2 + 1 + sizeof(struct ieee80211_eht_cap_elem) +
2574 		sizeof(struct ieee80211_eht_mcs_nss_supp) +
2575 		IEEE80211_EHT_PPE_THRES_MAX_LEN;
2576 
2577 	size += 2 + 1 + sizeof(struct ieee80211_uhr_cap);
2578 
2579 	return size;
2580 }
2581 
2582 static int ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
2583 {
2584 	struct ieee80211_local *local = sdata->local;
2585 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2586 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2587 	struct ieee80211_link_data *link;
2588 	struct sk_buff *skb;
2589 	struct ieee80211_mgmt *mgmt;
2590 	u8 *pos, qos_info, *ie_start;
2591 	size_t offset, noffset;
2592 	u16 capab = 0, link_capab;
2593 	__le16 listen_int;
2594 	struct element *ext_capa = NULL;
2595 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
2596 	struct ieee80211_prep_tx_info info = {};
2597 	unsigned int link_id, n_links = 0;
2598 	u16 present_elems[PRESENT_ELEMS_MAX] = {};
2599 	struct sta_info *sta;
2600 	bool assoc_encrypt;
2601 	void *capab_pos;
2602 	size_t size;
2603 	int ret;
2604 
2605 	/* we know it's writable, cast away the const */
2606 	if (assoc_data->ie_len)
2607 		ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
2608 						      assoc_data->ie,
2609 						      assoc_data->ie_len);
2610 
2611 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
2612 
2613 	size = local->hw.extra_tx_headroom +
2614 	       sizeof(*mgmt) + /* bit too much but doesn't matter */
2615 	       2 + assoc_data->ssid_len + /* SSID */
2616 	       assoc_data->ie_len + /* extra IEs */
2617 	       (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
2618 	       9; /* WMM */
2619 
2620 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
2621 		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
2622 		size_t elems_len = assoc_data->link[link_id].elems_len;
2623 
2624 		if (!cbss)
2625 			continue;
2626 
2627 		n_links++;
2628 
2629 		size += ieee80211_link_common_elems_size(sdata, iftype, cbss,
2630 							 elems_len);
2631 
2632 		/* non-inheritance element */
2633 		size += 2 + 2 + PRESENT_ELEMS_MAX;
2634 
2635 		/* should be the same across all BSSes */
2636 		if (cbss->capability & WLAN_CAPABILITY_PRIVACY)
2637 			capab |= WLAN_CAPABILITY_PRIVACY;
2638 	}
2639 
2640 	if (ieee80211_vif_is_mld(&sdata->vif)) {
2641 		/* consider the multi-link element with STA profile */
2642 		size += sizeof(struct ieee80211_multi_link_elem);
2643 		/* max common info field in basic multi-link element */
2644 		size += sizeof(struct ieee80211_mle_basic_common_info) +
2645 			2 + /* capa & op */
2646 			2 + /* ext capa & op */
2647 			2; /* EML capa */
2648 
2649 		/* The capability elements were already considered above */
2650 		size += (n_links - 1) *
2651 			(1 + 1 + /* subelement ID/length */
2652 			 2 + /* STA control */
2653 			 1 + ETH_ALEN + 2 /* STA Info field */);
2654 	}
2655 
2656 	link = sdata_dereference(sdata->link[assoc_data->assoc_link_id], sdata);
2657 	if (WARN_ON(!link))
2658 		return -EINVAL;
2659 
2660 	if (WARN_ON(!assoc_data->link[assoc_data->assoc_link_id].bss))
2661 		return -EINVAL;
2662 
2663 	skb = alloc_skb(size, GFP_KERNEL);
2664 	if (!skb)
2665 		return -ENOMEM;
2666 
2667 	skb_reserve(skb, local->hw.extra_tx_headroom);
2668 
2669 	if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
2670 		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
2671 
2672 	/* Set MBSSID support for HE AP if needed */
2673 	if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
2674 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE &&
2675 	    ext_capa && ext_capa->datalen >= 3)
2676 		ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
2677 
2678 	mgmt = skb_put_zero(skb, 24);
2679 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
2680 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2681 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
2682 
2683 	listen_int = cpu_to_le16(assoc_data->s1g ?
2684 			ieee80211_encode_usf(local->hw.conf.listen_interval) :
2685 			local->hw.conf.listen_interval);
2686 	if (!is_zero_ether_addr(assoc_data->prev_ap_addr)) {
2687 		skb_put(skb, 10);
2688 		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2689 						  IEEE80211_STYPE_REASSOC_REQ);
2690 		capab_pos = &mgmt->u.reassoc_req.capab_info;
2691 		mgmt->u.reassoc_req.listen_interval = listen_int;
2692 		memcpy(mgmt->u.reassoc_req.current_ap,
2693 		       assoc_data->prev_ap_addr, ETH_ALEN);
2694 		info.subtype = IEEE80211_STYPE_REASSOC_REQ;
2695 	} else {
2696 		skb_put(skb, 4);
2697 		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2698 						  IEEE80211_STYPE_ASSOC_REQ);
2699 		capab_pos = &mgmt->u.assoc_req.capab_info;
2700 		mgmt->u.assoc_req.listen_interval = listen_int;
2701 		info.subtype = IEEE80211_STYPE_ASSOC_REQ;
2702 	}
2703 
2704 	/* SSID */
2705 	pos = skb_put(skb, 2 + assoc_data->ssid_len);
2706 	ie_start = pos;
2707 	*pos++ = WLAN_EID_SSID;
2708 	*pos++ = assoc_data->ssid_len;
2709 	memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
2710 
2711 	/*
2712 	 * This bit is technically reserved, so it shouldn't matter for either
2713 	 * the AP or us, but it also means we shouldn't set it. However, we've
2714 	 * always set it in the past, and apparently some EHT APs check that
2715 	 * we don't set it. To avoid interoperability issues with old APs that
2716 	 * for some reason check it and want it to be set, set the bit for all
2717 	 * pre-EHT connections as we used to do.
2718 	 */
2719 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_EHT &&
2720 	    !ieee80211_hw_check(&local->hw, STRICT))
2721 		capab |= WLAN_CAPABILITY_ESS;
2722 
2723 	/* add the elements for the assoc (main) link */
2724 	link_capab = capab;
2725 	offset = ieee80211_add_link_elems(sdata, skb, &link_capab,
2726 					  ext_capa,
2727 					  assoc_data->ie,
2728 					  assoc_data->ie_len,
2729 					  assoc_data->assoc_link_id, link,
2730 					  present_elems, assoc_data);
2731 	put_unaligned_le16(link_capab, capab_pos);
2732 
2733 	/* if present, add any custom non-vendor IEs */
2734 	if (assoc_data->ie_len) {
2735 		noffset = ieee80211_ie_split_vendor(assoc_data->ie,
2736 						    assoc_data->ie_len,
2737 						    offset);
2738 		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
2739 		offset = noffset;
2740 	}
2741 
2742 	if (assoc_data->wmm) {
2743 		if (assoc_data->uapsd) {
2744 			qos_info = ifmgd->uapsd_queues;
2745 			qos_info |= (ifmgd->uapsd_max_sp_len <<
2746 				     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
2747 		} else {
2748 			qos_info = 0;
2749 		}
2750 
2751 		pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
2752 	}
2753 
2754 	/* add any remaining custom (i.e. vendor specific here) IEs */
2755 	if (assoc_data->ie_len) {
2756 		noffset = assoc_data->ie_len;
2757 		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
2758 	}
2759 
2760 	if (assoc_data->fils_kek_len) {
2761 		ret = fils_encrypt_assoc_req(skb, assoc_data);
2762 		if (ret < 0) {
2763 			dev_kfree_skb(skb);
2764 			return ret;
2765 		}
2766 	}
2767 
2768 	pos = skb_tail_pointer(skb);
2769 	kfree(ifmgd->assoc_req_ies);
2770 	ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
2771 	if (!ifmgd->assoc_req_ies) {
2772 		dev_kfree_skb(skb);
2773 		return -ENOMEM;
2774 	}
2775 
2776 	ifmgd->assoc_req_ies_len = pos - ie_start;
2777 
2778 	info.link_id = assoc_data->assoc_link_id;
2779 	drv_mgd_prepare_tx(local, sdata, &info);
2780 
2781 	sta = sta_info_get_bss(sdata, sdata->vif.cfg.ap_addr);
2782 
2783 	assoc_encrypt = sta && sta->sta.epp_peer &&
2784 			wiphy_dereference(sdata->local->hw.wiphy,
2785 					  sta->ptk[sta->ptk_idx]);
2786 
2787 	if (!assoc_encrypt)
2788 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2789 
2790 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2791 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
2792 						IEEE80211_TX_INTFL_MLME_CONN_TX;
2793 	ieee80211_tx_skb(sdata, skb);
2794 
2795 	return 0;
2796 }
2797 
2798 void ieee80211_send_pspoll(struct ieee80211_local *local,
2799 			   struct ieee80211_sub_if_data *sdata)
2800 {
2801 	struct ieee80211_pspoll *pspoll;
2802 	struct sk_buff *skb;
2803 
2804 	skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
2805 	if (!skb)
2806 		return;
2807 
2808 	pspoll = (struct ieee80211_pspoll *) skb->data;
2809 	pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
2810 
2811 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2812 	ieee80211_tx_skb(sdata, skb);
2813 }
2814 
2815 void ieee80211_send_nullfunc(struct ieee80211_local *local,
2816 			     struct ieee80211_sub_if_data *sdata,
2817 			     bool powersave)
2818 {
2819 	struct sk_buff *skb;
2820 	struct ieee80211_hdr_3addr *nullfunc;
2821 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2822 
2823 	skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif, -1,
2824 				     !ieee80211_hw_check(&local->hw,
2825 							 DOESNT_SUPPORT_QOS_NDP));
2826 	if (!skb)
2827 		return;
2828 
2829 	nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
2830 	if (powersave)
2831 		nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
2832 
2833 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2834 					IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
2835 
2836 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2837 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2838 
2839 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2840 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
2841 
2842 	ieee80211_tx_skb(sdata, skb);
2843 }
2844 
2845 void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
2846 				   struct ieee80211_sub_if_data *sdata)
2847 {
2848 	struct sk_buff *skb;
2849 	struct ieee80211_hdr *nullfunc;
2850 	__le16 fc;
2851 
2852 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2853 		return;
2854 
2855 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
2856 	if (!skb)
2857 		return;
2858 
2859 	skb_reserve(skb, local->hw.extra_tx_headroom);
2860 
2861 	nullfunc = skb_put_zero(skb, 30);
2862 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2863 			 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2864 	nullfunc->frame_control = fc;
2865 	memcpy(nullfunc->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
2866 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
2867 	memcpy(nullfunc->addr3, sdata->vif.cfg.ap_addr, ETH_ALEN);
2868 	memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
2869 
2870 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2871 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
2872 	ieee80211_tx_skb(sdata, skb);
2873 }
2874 
2875 /* spectrum management related things */
2876 static void ieee80211_csa_switch_work(struct wiphy *wiphy,
2877 				      struct wiphy_work *work)
2878 {
2879 	struct ieee80211_link_data *link =
2880 		container_of(work, struct ieee80211_link_data,
2881 			     u.mgd.csa.switch_work.work);
2882 	struct ieee80211_sub_if_data *sdata = link->sdata;
2883 	struct ieee80211_local *local = sdata->local;
2884 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2885 	int ret;
2886 
2887 	if (!ieee80211_sdata_running(sdata))
2888 		return;
2889 
2890 	lockdep_assert_wiphy(local->hw.wiphy);
2891 
2892 	if (!ifmgd->associated)
2893 		return;
2894 
2895 	if (!link->conf->csa_active)
2896 		return;
2897 
2898 	/*
2899 	 * If the link isn't active (now), we cannot wait for beacons, won't
2900 	 * have a reserved chanctx, etc. Just switch over the chandef and
2901 	 * update cfg80211 directly.
2902 	 */
2903 	if (!ieee80211_vif_link_active(&sdata->vif, link->link_id)) {
2904 		struct link_sta_info *link_sta;
2905 		struct sta_info *ap_sta;
2906 
2907 		link->conf->chanreq = link->csa.chanreq;
2908 		cfg80211_ch_switch_notify(sdata->dev, &link->csa.chanreq.oper,
2909 					  link->link_id);
2910 		link->conf->csa_active = false;
2911 		link->u.mgd.conn.dbe_enabled = false;
2912 
2913 		ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
2914 		if (WARN_ON(!ap_sta))
2915 			return;
2916 
2917 		link_sta = wiphy_dereference(wiphy,
2918 					     ap_sta->link[link->link_id]);
2919 		if (WARN_ON(!link_sta))
2920 			return;
2921 
2922 		/*
2923 		 * If the link was somehow deactivated in the middle of enabling
2924 		 * DBE while waiting for a response, this could be stuck, reset.
2925 		 */
2926 		link_sta->uhr_usable_tx_width = IEEE80211_STA_RX_BW_MAX;
2927 
2928 		link_sta->pub->bandwidth =
2929 			ieee80211_sta_current_bw(link_sta,
2930 						 &link->csa.chanreq.oper,
2931 						 IEEE80211_STA_BW_TX_TO_STA);
2932 		return;
2933 	}
2934 
2935 	/*
2936 	 * using reservation isn't immediate as it may be deferred until later
2937 	 * with multi-vif. once reservation is complete it will re-schedule the
2938 	 * work with no reserved_chanctx so verify chandef to check if it
2939 	 * completed successfully
2940 	 */
2941 
2942 	if (link->reserved_chanctx) {
2943 		/*
2944 		 * with multi-vif csa driver may call ieee80211_csa_finish()
2945 		 * many times while waiting for other interfaces to use their
2946 		 * reservations
2947 		 */
2948 		if (link->reserved_ready)
2949 			return;
2950 
2951 		ret = ieee80211_link_use_reserved_context(link);
2952 		if (ret) {
2953 			link_info(link,
2954 				  "failed to use reserved channel context, disconnecting (err=%d)\n",
2955 				  ret);
2956 			wiphy_work_queue(sdata->local->hw.wiphy,
2957 					 &ifmgd->csa_connection_drop_work);
2958 		}
2959 		return;
2960 	}
2961 
2962 	if (!ieee80211_chanreq_identical(&link->conf->chanreq,
2963 					 &link->csa.chanreq)) {
2964 		link_info(link,
2965 			  "failed to finalize channel switch, disconnecting\n");
2966 		wiphy_work_queue(sdata->local->hw.wiphy,
2967 				 &ifmgd->csa_connection_drop_work);
2968 		return;
2969 	}
2970 
2971 	link->u.mgd.csa.waiting_bcn = true;
2972 
2973 	/*
2974 	 * The next beacon really should always be different, so this should
2975 	 * have no effect whatsoever. However, some APs (we observed this in
2976 	 * an Asus AXE11000), the beacon after the CSA might be identical to
2977 	 * the last beacon on the old channel - in this case we'd ignore it.
2978 	 * Resetting the CRC will lead us to handle it better (albeit with a
2979 	 * disconnect, but clearly the AP is broken.)
2980 	 */
2981 	link->u.mgd.beacon_crc_valid = false;
2982 
2983 	/* apply new TPE restrictions immediately on the new channel */
2984 	if (link->u.mgd.csa.ap_chandef.chan->band == NL80211_BAND_6GHZ &&
2985 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE) {
2986 		ieee80211_rearrange_tpe(&link->u.mgd.csa.tpe,
2987 					&link->u.mgd.csa.ap_chandef,
2988 					&link->conf->chanreq.oper);
2989 		if (memcmp(&link->conf->tpe, &link->u.mgd.csa.tpe,
2990 			   sizeof(link->u.mgd.csa.tpe))) {
2991 			link->conf->tpe = link->u.mgd.csa.tpe;
2992 			ieee80211_link_info_change_notify(sdata, link,
2993 							  BSS_CHANGED_TPE);
2994 		}
2995 	}
2996 
2997 	/*
2998 	 * It is not necessary to reset these timers if any link does not
2999 	 * have an active CSA and that link still receives the beacons
3000 	 * when other links have active CSA.
3001 	 */
3002 	for_each_link_data(sdata, link) {
3003 		if (!link->conf->csa_active)
3004 			return;
3005 	}
3006 
3007 	/*
3008 	 * Reset the beacon monitor and connection monitor timers when CSA
3009 	 * is active for all links in MLO when channel switch occurs in all
3010 	 * the links.
3011 	 */
3012 	ieee80211_sta_reset_beacon_monitor(sdata);
3013 	ieee80211_sta_reset_conn_monitor(sdata);
3014 }
3015 
3016 static void ieee80211_chswitch_post_beacon(struct ieee80211_link_data *link)
3017 {
3018 	struct ieee80211_sub_if_data *sdata = link->sdata;
3019 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3020 	struct link_sta_info *link_sta;
3021 	struct sta_info *ap_sta;
3022 	int ret;
3023 
3024 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3025 
3026 	WARN_ON(!link->conf->csa_active);
3027 
3028 	ieee80211_vif_unblock_queues_csa(sdata);
3029 
3030 	link->conf->csa_active = false;
3031 	link->u.mgd.conn.dbe_enabled = false;
3032 	link->u.mgd.csa.blocked_tx = false;
3033 	link->u.mgd.csa.waiting_bcn = false;
3034 
3035 	ret = drv_post_channel_switch(link);
3036 	if (ret) {
3037 		link_info(link,
3038 			  "driver post channel switch failed, disconnecting\n");
3039 		wiphy_work_queue(sdata->local->hw.wiphy,
3040 				 &ifmgd->csa_connection_drop_work);
3041 		return;
3042 	}
3043 
3044 	ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
3045 	if (WARN_ON(!ap_sta))
3046 		return;
3047 
3048 	link_sta = sdata_dereference(ap_sta->link[link->link_id], sdata);
3049 	if (WARN_ON(!link_sta))
3050 		return;
3051 
3052 	/*
3053 	 * If DBE was being activated and CSA happened, this could be
3054 	 * on a wrong value. Reset it.
3055 	 */
3056 	link_sta->uhr_usable_tx_width = IEEE80211_STA_RX_BW_MAX;
3057 
3058 	cfg80211_ch_switch_notify(sdata->dev, &link->conf->chanreq.oper,
3059 				  link->link_id);
3060 }
3061 
3062 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success,
3063 			     unsigned int link_id)
3064 {
3065 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3066 
3067 	trace_api_chswitch_done(sdata, success, link_id);
3068 
3069 	rcu_read_lock();
3070 
3071 	if (!success) {
3072 		sdata_info(sdata,
3073 			   "driver channel switch failed (link %d), disconnecting\n",
3074 			   link_id);
3075 		wiphy_work_queue(sdata->local->hw.wiphy,
3076 				 &sdata->u.mgd.csa_connection_drop_work);
3077 	} else {
3078 		struct ieee80211_link_data *link =
3079 			rcu_dereference(sdata->link[link_id]);
3080 
3081 		if (WARN_ON(!link)) {
3082 			rcu_read_unlock();
3083 			return;
3084 		}
3085 
3086 		wiphy_hrtimer_work_queue(sdata->local->hw.wiphy,
3087 					 &link->u.mgd.csa.switch_work, 0);
3088 	}
3089 
3090 	rcu_read_unlock();
3091 }
3092 EXPORT_SYMBOL(ieee80211_chswitch_done);
3093 
3094 static void
3095 ieee80211_sta_abort_chanswitch(struct ieee80211_link_data *link)
3096 {
3097 	struct ieee80211_sub_if_data *sdata = link->sdata;
3098 	struct ieee80211_local *local = sdata->local;
3099 
3100 	lockdep_assert_wiphy(local->hw.wiphy);
3101 
3102 	if (!local->ops->abort_channel_switch)
3103 		return;
3104 
3105 	if (rcu_access_pointer(link->conf->chanctx_conf))
3106 		ieee80211_link_unreserve_chanctx(link);
3107 
3108 	ieee80211_vif_unblock_queues_csa(sdata);
3109 
3110 	link->conf->csa_active = false;
3111 	link->u.mgd.csa.blocked_tx = false;
3112 
3113 	drv_abort_channel_switch(link);
3114 }
3115 
3116 struct sta_csa_rnr_iter_data {
3117 	struct ieee80211_link_data *link;
3118 	struct ieee80211_channel *chan;
3119 	u8 mld_id;
3120 };
3121 
3122 static enum cfg80211_rnr_iter_ret
3123 ieee80211_sta_csa_rnr_iter(void *_data, u8 type,
3124 			   const struct ieee80211_neighbor_ap_info *info,
3125 			   const u8 *tbtt_info, u8 tbtt_info_len)
3126 {
3127 	struct sta_csa_rnr_iter_data *data = _data;
3128 	struct ieee80211_link_data *link = data->link;
3129 	struct ieee80211_sub_if_data *sdata = link->sdata;
3130 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3131 	const struct ieee80211_tbtt_info_ge_11 *ti;
3132 	enum nl80211_band band;
3133 	unsigned int center_freq;
3134 	int link_id;
3135 
3136 	if (type != IEEE80211_TBTT_INFO_TYPE_TBTT)
3137 		return RNR_ITER_CONTINUE;
3138 
3139 	if (tbtt_info_len < sizeof(*ti))
3140 		return RNR_ITER_CONTINUE;
3141 
3142 	ti = (const void *)tbtt_info;
3143 
3144 	if (ti->mld_params.mld_id != data->mld_id)
3145 		return RNR_ITER_CONTINUE;
3146 
3147 	link_id = le16_get_bits(ti->mld_params.params,
3148 				IEEE80211_RNR_MLD_PARAMS_LINK_ID);
3149 	if (link_id != data->link->link_id)
3150 		return RNR_ITER_CONTINUE;
3151 
3152 	/* we found the entry for our link! */
3153 
3154 	/* this AP is confused, it had this right before ... just disconnect */
3155 	if (!ieee80211_operating_class_to_band(info->op_class, &band)) {
3156 		link_info(link,
3157 			  "AP now has invalid operating class in RNR, disconnect\n");
3158 		wiphy_work_queue(sdata->local->hw.wiphy,
3159 				 &ifmgd->csa_connection_drop_work);
3160 		return RNR_ITER_BREAK;
3161 	}
3162 
3163 	center_freq = ieee80211_channel_to_frequency(info->channel, band);
3164 	data->chan = ieee80211_get_channel(sdata->local->hw.wiphy, center_freq);
3165 
3166 	return RNR_ITER_BREAK;
3167 }
3168 
3169 static void
3170 ieee80211_sta_other_link_csa_disappeared(struct ieee80211_link_data *link,
3171 					 struct ieee802_11_elems *elems)
3172 {
3173 	struct ieee80211_sub_if_data *sdata = link->sdata;
3174 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3175 	struct sta_csa_rnr_iter_data data = {
3176 		.link = link,
3177 	};
3178 
3179 	/*
3180 	 * If we get here, we see a beacon from another link without
3181 	 * CSA still being reported for it, so now we have to check
3182 	 * if the CSA was aborted or completed. This may not even be
3183 	 * perfectly possible if the CSA was only done for changing
3184 	 * the puncturing, but in that case if the link in inactive
3185 	 * we don't really care, and if it's an active link (or when
3186 	 * it's activated later) we'll get a beacon and adjust.
3187 	 */
3188 
3189 	if (WARN_ON(!elems->ml_basic))
3190 		return;
3191 
3192 	data.mld_id = ieee80211_mle_get_mld_id((const void *)elems->ml_basic);
3193 
3194 	/*
3195 	 * So in order to do this, iterate the RNR element(s) and see
3196 	 * what channel is reported now.
3197 	 */
3198 	cfg80211_iter_rnr(elems->ie_start, elems->total_len,
3199 			  ieee80211_sta_csa_rnr_iter, &data);
3200 
3201 	if (!data.chan) {
3202 		link_info(link,
3203 			  "couldn't find (valid) channel in RNR for CSA, disconnect\n");
3204 		wiphy_work_queue(sdata->local->hw.wiphy,
3205 				 &ifmgd->csa_connection_drop_work);
3206 		return;
3207 	}
3208 
3209 	/*
3210 	 * If it doesn't match the CSA, then assume it aborted. This
3211 	 * may erroneously detect that it was _not_ aborted when it
3212 	 * was in fact aborted, but only changed the bandwidth or the
3213 	 * puncturing configuration, but we don't have enough data to
3214 	 * detect that.
3215 	 */
3216 	if (data.chan != link->csa.chanreq.oper.chan)
3217 		ieee80211_sta_abort_chanswitch(link);
3218 }
3219 
3220 enum ieee80211_csa_source {
3221 	IEEE80211_CSA_SOURCE_BEACON,
3222 	IEEE80211_CSA_SOURCE_OTHER_LINK,
3223 	IEEE80211_CSA_SOURCE_PROT_ACTION,
3224 	IEEE80211_CSA_SOURCE_UNPROT_ACTION,
3225 };
3226 
3227 static void
3228 ieee80211_sta_process_chanswitch(struct ieee80211_link_data *link,
3229 				 u64 timestamp, u32 device_timestamp,
3230 				 struct ieee802_11_elems *full_elems,
3231 				 struct ieee802_11_elems *csa_elems,
3232 				 enum ieee80211_csa_source source)
3233 {
3234 	struct ieee80211_sub_if_data *sdata = link->sdata;
3235 	struct ieee80211_local *local = sdata->local;
3236 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3237 	struct ieee80211_chanctx *chanctx = NULL;
3238 	struct ieee80211_chanctx_conf *conf;
3239 	struct ieee80211_csa_ie csa_ie = {};
3240 	struct ieee80211_channel_switch ch_switch = {
3241 		.link_id = link->link_id,
3242 		.timestamp = timestamp,
3243 		.device_timestamp = device_timestamp,
3244 	};
3245 	u32 csa_time_tu;
3246 	ktime_t now;
3247 	int res;
3248 
3249 	lockdep_assert_wiphy(local->hw.wiphy);
3250 
3251 	if (csa_elems) {
3252 		struct cfg80211_bss *cbss = link->conf->bss;
3253 		enum nl80211_band current_band;
3254 		struct ieee80211_bss *bss;
3255 
3256 		if (WARN_ON(!cbss))
3257 			return;
3258 
3259 		current_band = cbss->channel->band;
3260 		bss = (void *)cbss->priv;
3261 
3262 		res = ieee80211_parse_ch_switch_ie(sdata, csa_elems,
3263 						   current_band,
3264 						   bss->vht_cap_info,
3265 						   &link->u.mgd.conn,
3266 						   link->u.mgd.bssid,
3267 						   source == IEEE80211_CSA_SOURCE_UNPROT_ACTION,
3268 						   &csa_ie);
3269 		if (res == 0) {
3270 			ch_switch.block_tx = csa_ie.mode;
3271 			ch_switch.chandef = csa_ie.chanreq.oper;
3272 			ch_switch.count = csa_ie.count;
3273 			ch_switch.delay = csa_ie.max_switch_time;
3274 		}
3275 
3276 		link->u.mgd.csa.tpe = csa_elems->csa_tpe;
3277 	} else {
3278 		/*
3279 		 * If there was no per-STA profile for this link, we
3280 		 * get called with csa_elems == NULL. This of course means
3281 		 * there are no CSA elements, so set res=1 indicating
3282 		 * no more CSA.
3283 		 */
3284 		res = 1;
3285 	}
3286 
3287 	if (res < 0) {
3288 		/* ignore this case, not a protected frame */
3289 		if (source == IEEE80211_CSA_SOURCE_UNPROT_ACTION)
3290 			return;
3291 		goto drop_connection;
3292 	}
3293 
3294 	if (link->conf->csa_active) {
3295 		switch (source) {
3296 		case IEEE80211_CSA_SOURCE_PROT_ACTION:
3297 		case IEEE80211_CSA_SOURCE_UNPROT_ACTION:
3298 			/* already processing - disregard action frames */
3299 			return;
3300 		case IEEE80211_CSA_SOURCE_BEACON:
3301 			if (link->u.mgd.csa.waiting_bcn) {
3302 				ieee80211_chswitch_post_beacon(link);
3303 				/*
3304 				 * If the CSA is still present after the switch
3305 				 * we need to consider it as a new CSA (possibly
3306 				 * to self). This happens by not returning here
3307 				 * so we'll get to the check below.
3308 				 */
3309 			} else if (res) {
3310 				ieee80211_sta_abort_chanswitch(link);
3311 				return;
3312 			} else {
3313 				drv_channel_switch_rx_beacon(sdata, &ch_switch);
3314 				return;
3315 			}
3316 			break;
3317 		case IEEE80211_CSA_SOURCE_OTHER_LINK:
3318 			/* active link: we want to see the beacon to continue */
3319 			if (ieee80211_vif_link_active(&sdata->vif,
3320 						      link->link_id))
3321 				return;
3322 
3323 			/* switch work ran, so just complete the process */
3324 			if (link->u.mgd.csa.waiting_bcn) {
3325 				ieee80211_chswitch_post_beacon(link);
3326 				/*
3327 				 * If the CSA is still present after the switch
3328 				 * we need to consider it as a new CSA (possibly
3329 				 * to self). This happens by not returning here
3330 				 * so we'll get to the check below.
3331 				 */
3332 				break;
3333 			}
3334 
3335 			/* link still has CSA but we already know, do nothing */
3336 			if (!res)
3337 				return;
3338 
3339 			/* check in the RNR if the CSA aborted */
3340 			ieee80211_sta_other_link_csa_disappeared(link,
3341 								 full_elems);
3342 			return;
3343 		}
3344 	}
3345 
3346 	/* no active CSA nor a new one */
3347 	if (res) {
3348 		/*
3349 		 * However, we may have stopped queues when receiving a public
3350 		 * action frame that couldn't be protected, if it had the quiet
3351 		 * bit set. This is a trade-off, we want to be quiet as soon as
3352 		 * possible, but also don't trust the public action frame much,
3353 		 * as it can't be protected.
3354 		 */
3355 		if (unlikely(link->u.mgd.csa.blocked_tx)) {
3356 			link->u.mgd.csa.blocked_tx = false;
3357 			ieee80211_vif_unblock_queues_csa(sdata);
3358 		}
3359 		return;
3360 	}
3361 
3362 	/*
3363 	 * We don't really trust public action frames, but block queues (go to
3364 	 * quiet mode) for them anyway, we should get a beacon soon to either
3365 	 * know what the CSA really is, or figure out the public action frame
3366 	 * was actually an attack.
3367 	 */
3368 	if (source == IEEE80211_CSA_SOURCE_UNPROT_ACTION) {
3369 		if (csa_ie.mode) {
3370 			link->u.mgd.csa.blocked_tx = true;
3371 			ieee80211_vif_block_queues_csa(sdata);
3372 		}
3373 		return;
3374 	}
3375 
3376 	if (link->conf->chanreq.oper.chan->band !=
3377 	    csa_ie.chanreq.oper.chan->band) {
3378 		link_info(link,
3379 			  "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
3380 			  link->u.mgd.bssid,
3381 			  csa_ie.chanreq.oper.chan->center_freq,
3382 			  csa_ie.chanreq.oper.width,
3383 			  csa_ie.chanreq.oper.center_freq1,
3384 			  csa_ie.chanreq.oper.center_freq2);
3385 		goto drop_connection;
3386 	}
3387 
3388 	if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chanreq.oper,
3389 				     IEEE80211_CHAN_DISABLED)) {
3390 		link_info(link,
3391 			  "AP %pM switches to unsupported channel (%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), disconnecting\n",
3392 			  link->u.mgd.bssid,
3393 			  csa_ie.chanreq.oper.chan->center_freq,
3394 			  csa_ie.chanreq.oper.chan->freq_offset,
3395 			  csa_ie.chanreq.oper.width,
3396 			  csa_ie.chanreq.oper.center_freq1,
3397 			  csa_ie.chanreq.oper.freq1_offset,
3398 			  csa_ie.chanreq.oper.center_freq2);
3399 		goto drop_connection;
3400 	}
3401 
3402 	if (cfg80211_chandef_identical(&csa_ie.chanreq.oper,
3403 				       &link->conf->chanreq.oper) &&
3404 	    (!csa_ie.mode || source != IEEE80211_CSA_SOURCE_BEACON)) {
3405 		if (link->u.mgd.csa.ignored_same_chan)
3406 			return;
3407 		link_info(link,
3408 			  "AP %pM tries to chanswitch to same channel, ignore\n",
3409 			  link->u.mgd.bssid);
3410 		link->u.mgd.csa.ignored_same_chan = true;
3411 		return;
3412 	}
3413 
3414 	/*
3415 	 * Drop all TDLS peers on the affected link - either we disconnect or
3416 	 * move to a different channel from this point on. There's no telling
3417 	 * what our peer will do.
3418 	 * The TDLS WIDER_BW scenario is also problematic, as peers might now
3419 	 * have an incompatible wider chandef.
3420 	 */
3421 	ieee80211_teardown_tdls_peers(link);
3422 
3423 	conf = rcu_dereference_protected(link->conf->chanctx_conf,
3424 					 lockdep_is_held(&local->hw.wiphy->mtx));
3425 	if (ieee80211_vif_link_active(&sdata->vif, link->link_id) && !conf) {
3426 		link_info(link,
3427 			  "no channel context assigned to vif?, disconnecting\n");
3428 		goto drop_connection;
3429 	}
3430 
3431 	if (conf)
3432 		chanctx = container_of(conf, struct ieee80211_chanctx, conf);
3433 
3434 	if (!ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
3435 		link_info(link,
3436 			  "driver doesn't support chan-switch with channel contexts\n");
3437 		goto drop_connection;
3438 	}
3439 
3440 	if (drv_pre_channel_switch(sdata, &ch_switch)) {
3441 		link_info(link,
3442 			  "preparing for channel switch failed, disconnecting\n");
3443 		goto drop_connection;
3444 	}
3445 
3446 	link->u.mgd.csa.ap_chandef = csa_ie.chanreq.ap;
3447 
3448 	link->csa.chanreq.oper = csa_ie.chanreq.oper;
3449 	ieee80211_set_chanreq_ap(sdata, &link->csa.chanreq, &link->u.mgd.conn,
3450 				 &csa_ie.chanreq.ap);
3451 
3452 	if (chanctx) {
3453 		res = ieee80211_link_reserve_chanctx(link, &link->csa.chanreq,
3454 						     chanctx->mode, false);
3455 		if (res) {
3456 			link_info(link,
3457 				  "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
3458 				  res);
3459 			goto drop_connection;
3460 		}
3461 	}
3462 
3463 	link->conf->csa_active = true;
3464 	link->u.mgd.csa.ignored_same_chan = false;
3465 	link->u.mgd.beacon_crc_valid = false;
3466 	link->u.mgd.csa.blocked_tx = csa_ie.mode;
3467 
3468 	if (csa_ie.mode)
3469 		ieee80211_vif_block_queues_csa(sdata);
3470 
3471 	cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chanreq.oper,
3472 					  link->link_id, csa_ie.count,
3473 					  csa_ie.mode);
3474 
3475 	/* we may have to handle timeout for deactivated link in software */
3476 	now = ktime_get_boottime();
3477 	csa_time_tu = (max_t(int, csa_ie.count, 1) - 1) * link->conf->beacon_int;
3478 	link->u.mgd.csa.time = now + us_to_ktime(ieee80211_tu_to_usec(csa_time_tu));
3479 
3480 	if (ieee80211_vif_link_active(&sdata->vif, link->link_id) &&
3481 	    local->ops->channel_switch) {
3482 		/*
3483 		 * Use driver's channel switch callback, the driver will
3484 		 * later call ieee80211_chswitch_done(). It may deactivate
3485 		 * the link as well, we handle that elsewhere and queue
3486 		 * the csa.switch_work for the calculated time then.
3487 		 */
3488 		drv_channel_switch(local, sdata, &ch_switch);
3489 		return;
3490 	}
3491 
3492 	/* channel switch handled in software */
3493 	wiphy_hrtimer_work_queue(local->hw.wiphy,
3494 				 &link->u.mgd.csa.switch_work,
3495 				 link->u.mgd.csa.time - now);
3496 	return;
3497  drop_connection:
3498 	/*
3499 	 * This is just so that the disconnect flow will know that
3500 	 * we were trying to switch channel and failed. In case the
3501 	 * mode is 1 (we are not allowed to Tx), we will know not to
3502 	 * send a deauthentication frame. Those two fields will be
3503 	 * reset when the disconnection worker runs.
3504 	 */
3505 	link->conf->csa_active = true;
3506 	link->u.mgd.csa.blocked_tx = csa_ie.mode;
3507 
3508 	wiphy_work_queue(sdata->local->hw.wiphy,
3509 			 &ifmgd->csa_connection_drop_work);
3510 }
3511 
3512 struct sta_bss_param_ch_cnt_data {
3513 	struct ieee80211_sub_if_data *sdata;
3514 	u8 reporting_link_id;
3515 	u8 mld_id;
3516 };
3517 
3518 static enum cfg80211_rnr_iter_ret
3519 ieee80211_sta_bss_param_ch_cnt_iter(void *_data, u8 type,
3520 				    const struct ieee80211_neighbor_ap_info *info,
3521 				    const u8 *tbtt_info, u8 tbtt_info_len)
3522 {
3523 	struct sta_bss_param_ch_cnt_data *data = _data;
3524 	struct ieee80211_sub_if_data *sdata = data->sdata;
3525 	const struct ieee80211_tbtt_info_ge_11 *ti;
3526 	u8 bss_param_ch_cnt;
3527 	int link_id;
3528 
3529 	if (type != IEEE80211_TBTT_INFO_TYPE_TBTT)
3530 		return RNR_ITER_CONTINUE;
3531 
3532 	if (tbtt_info_len < sizeof(*ti))
3533 		return RNR_ITER_CONTINUE;
3534 
3535 	ti = (const void *)tbtt_info;
3536 
3537 	if (ti->mld_params.mld_id != data->mld_id)
3538 		return RNR_ITER_CONTINUE;
3539 
3540 	link_id = le16_get_bits(ti->mld_params.params,
3541 				IEEE80211_RNR_MLD_PARAMS_LINK_ID);
3542 	bss_param_ch_cnt =
3543 		le16_get_bits(ti->mld_params.params,
3544 			      IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT);
3545 
3546 	if (bss_param_ch_cnt != 255 &&
3547 	    link_id < ARRAY_SIZE(sdata->link)) {
3548 		struct ieee80211_link_data *link =
3549 			sdata_dereference(sdata->link[link_id], sdata);
3550 
3551 		if (link && link->conf->bss_param_ch_cnt != bss_param_ch_cnt) {
3552 			link->conf->bss_param_ch_cnt = bss_param_ch_cnt;
3553 			link->conf->bss_param_ch_cnt_link_id =
3554 				data->reporting_link_id;
3555 		}
3556 	}
3557 
3558 	return RNR_ITER_CONTINUE;
3559 }
3560 
3561 static void
3562 ieee80211_mgd_update_bss_param_ch_cnt(struct ieee80211_sub_if_data *sdata,
3563 				      struct ieee80211_bss_conf *bss_conf,
3564 				      struct ieee802_11_elems *elems)
3565 {
3566 	struct sta_bss_param_ch_cnt_data data = {
3567 		.reporting_link_id = bss_conf->link_id,
3568 		.sdata = sdata,
3569 	};
3570 	int bss_param_ch_cnt;
3571 
3572 	if (!elems->ml_basic)
3573 		return;
3574 
3575 	data.mld_id = ieee80211_mle_get_mld_id((const void *)elems->ml_basic);
3576 
3577 	cfg80211_iter_rnr(elems->ie_start, elems->total_len,
3578 			  ieee80211_sta_bss_param_ch_cnt_iter, &data);
3579 
3580 	bss_param_ch_cnt =
3581 		ieee80211_mle_get_bss_param_ch_cnt((const void *)elems->ml_basic);
3582 
3583 	/*
3584 	 * Update bss_param_ch_cnt_link_id even if bss_param_ch_cnt
3585 	 * didn't change to indicate that we got a beacon on our own
3586 	 * link.
3587 	 */
3588 	if (bss_param_ch_cnt >= 0 && bss_param_ch_cnt != 255) {
3589 		bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
3590 		bss_conf->bss_param_ch_cnt_link_id =
3591 			bss_conf->link_id;
3592 	}
3593 }
3594 
3595 static bool
3596 ieee80211_find_80211h_pwr_constr(struct ieee80211_channel *channel,
3597 				 const u8 *country_ie, u8 country_ie_len,
3598 				 const u8 *pwr_constr_elem,
3599 				 int *chan_pwr, int *pwr_reduction)
3600 {
3601 	struct ieee80211_country_ie_triplet *triplet;
3602 	int chan = ieee80211_frequency_to_channel(channel->center_freq);
3603 	int i, chan_increment;
3604 	bool have_chan_pwr = false;
3605 
3606 	/* Invalid IE */
3607 	if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
3608 		return false;
3609 
3610 	triplet = (void *)(country_ie + 3);
3611 	country_ie_len -= 3;
3612 
3613 	switch (channel->band) {
3614 	default:
3615 		WARN_ON_ONCE(1);
3616 		fallthrough;
3617 	case NL80211_BAND_2GHZ:
3618 	case NL80211_BAND_60GHZ:
3619 	case NL80211_BAND_LC:
3620 		chan_increment = 1;
3621 		break;
3622 	case NL80211_BAND_5GHZ:
3623 		chan_increment = 4;
3624 		break;
3625 	case NL80211_BAND_6GHZ:
3626 		/*
3627 		 * In the 6 GHz band, the "maximum transmit power level"
3628 		 * field in the triplets is reserved, and thus will be
3629 		 * zero and we shouldn't use it to control TX power.
3630 		 * The actual TX power will be given in the transmit
3631 		 * power envelope element instead.
3632 		 */
3633 		return false;
3634 	}
3635 
3636 	/* find channel */
3637 	while (country_ie_len >= 3) {
3638 		u8 first_channel = triplet->chans.first_channel;
3639 
3640 		if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
3641 			goto next;
3642 
3643 		for (i = 0; i < triplet->chans.num_channels; i++) {
3644 			if (first_channel + i * chan_increment == chan) {
3645 				have_chan_pwr = true;
3646 				*chan_pwr = triplet->chans.max_power;
3647 				break;
3648 			}
3649 		}
3650 		if (have_chan_pwr)
3651 			break;
3652 
3653  next:
3654 		triplet++;
3655 		country_ie_len -= 3;
3656 	}
3657 
3658 	if (have_chan_pwr && pwr_constr_elem)
3659 		*pwr_reduction = *pwr_constr_elem;
3660 	else
3661 		*pwr_reduction = 0;
3662 
3663 	return have_chan_pwr;
3664 }
3665 
3666 static void ieee80211_find_cisco_dtpc(struct ieee80211_channel *channel,
3667 				      const u8 *cisco_dtpc_ie,
3668 				      int *pwr_level)
3669 {
3670 	/* From practical testing, the first data byte of the DTPC element
3671 	 * seems to contain the requested dBm level, and the CLI on Cisco
3672 	 * APs clearly state the range is -127 to 127 dBm, which indicates
3673 	 * a signed byte, although it seemingly never actually goes negative.
3674 	 * The other byte seems to always be zero.
3675 	 */
3676 	*pwr_level = (__s8)cisco_dtpc_ie[4];
3677 }
3678 
3679 static u64 ieee80211_handle_pwr_constr(struct ieee80211_link_data *link,
3680 				       struct ieee80211_channel *channel,
3681 				       struct ieee80211_mgmt *mgmt,
3682 				       const u8 *country_ie, u8 country_ie_len,
3683 				       const u8 *pwr_constr_ie,
3684 				       const u8 *cisco_dtpc_ie)
3685 {
3686 	struct ieee80211_sub_if_data *sdata = link->sdata;
3687 	bool has_80211h_pwr = false, has_cisco_pwr = false;
3688 	int chan_pwr = 0, pwr_reduction_80211h = 0;
3689 	int pwr_level_cisco, pwr_level_80211h;
3690 	int new_ap_level;
3691 	__le16 capab = mgmt->u.probe_resp.capab_info;
3692 
3693 	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3694 		return 0;	/* TODO */
3695 
3696 	if (country_ie &&
3697 	    (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
3698 	     capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
3699 		has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
3700 			channel, country_ie, country_ie_len,
3701 			pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
3702 		pwr_level_80211h =
3703 			max_t(int, 0, chan_pwr - pwr_reduction_80211h);
3704 	}
3705 
3706 	if (cisco_dtpc_ie) {
3707 		ieee80211_find_cisco_dtpc(
3708 			channel, cisco_dtpc_ie, &pwr_level_cisco);
3709 		has_cisco_pwr = true;
3710 	}
3711 
3712 	if (!has_80211h_pwr && !has_cisco_pwr)
3713 		return 0;
3714 
3715 	/* If we have both 802.11h and Cisco DTPC, apply both limits
3716 	 * by picking the smallest of the two power levels advertised.
3717 	 */
3718 	if (has_80211h_pwr &&
3719 	    (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
3720 		new_ap_level = pwr_level_80211h;
3721 
3722 		if (link->ap_power_level == new_ap_level)
3723 			return 0;
3724 
3725 		sdata_dbg(sdata,
3726 			  "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
3727 			  pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
3728 			  link->u.mgd.bssid);
3729 	} else {  /* has_cisco_pwr is always true here. */
3730 		new_ap_level = pwr_level_cisco;
3731 
3732 		if (link->ap_power_level == new_ap_level)
3733 			return 0;
3734 
3735 		sdata_dbg(sdata,
3736 			  "Limiting TX power to %d dBm as advertised by %pM\n",
3737 			  pwr_level_cisco, link->u.mgd.bssid);
3738 	}
3739 
3740 	link->ap_power_level = new_ap_level;
3741 	if (__ieee80211_recalc_txpower(link))
3742 		return BSS_CHANGED_TXPOWER;
3743 	return 0;
3744 }
3745 
3746 /* powersave */
3747 static void ieee80211_enable_ps(struct ieee80211_local *local,
3748 				struct ieee80211_sub_if_data *sdata)
3749 {
3750 	struct ieee80211_conf *conf = &local->hw.conf;
3751 
3752 	/*
3753 	 * If we are scanning right now then the parameters will
3754 	 * take effect when scan finishes.
3755 	 */
3756 	if (local->scanning)
3757 		return;
3758 
3759 	if (conf->dynamic_ps_timeout > 0 &&
3760 	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
3761 		mod_timer(&local->dynamic_ps_timer, jiffies +
3762 			  msecs_to_jiffies(conf->dynamic_ps_timeout));
3763 	} else {
3764 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3765 			ieee80211_send_nullfunc(local, sdata, true);
3766 
3767 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
3768 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
3769 			return;
3770 
3771 		conf->flags |= IEEE80211_CONF_PS;
3772 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
3773 	}
3774 }
3775 
3776 static void ieee80211_change_ps(struct ieee80211_local *local)
3777 {
3778 	struct ieee80211_conf *conf = &local->hw.conf;
3779 
3780 	if (local->ps_sdata) {
3781 		ieee80211_enable_ps(local, local->ps_sdata);
3782 	} else if (conf->flags & IEEE80211_CONF_PS) {
3783 		conf->flags &= ~IEEE80211_CONF_PS;
3784 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
3785 		timer_delete_sync(&local->dynamic_ps_timer);
3786 		wiphy_work_cancel(local->hw.wiphy,
3787 				  &local->dynamic_ps_enable_work);
3788 	}
3789 }
3790 
3791 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
3792 {
3793 	struct ieee80211_local *local = sdata->local;
3794 	struct ieee80211_if_managed *mgd = &sdata->u.mgd;
3795 	struct sta_info *sta = NULL;
3796 	bool authorized = false;
3797 
3798 	if (!mgd->powersave)
3799 		return false;
3800 
3801 	if (mgd->broken_ap)
3802 		return false;
3803 
3804 	if (!mgd->associated)
3805 		return false;
3806 
3807 	if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
3808 		return false;
3809 
3810 	if (!(local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO) &&
3811 	    !sdata->deflink.u.mgd.have_beacon)
3812 		return false;
3813 
3814 	rcu_read_lock();
3815 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
3816 	if (sta)
3817 		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
3818 	rcu_read_unlock();
3819 
3820 	return authorized;
3821 }
3822 
3823 /* need to hold RTNL or interface lock */
3824 void ieee80211_recalc_ps(struct ieee80211_local *local)
3825 {
3826 	struct ieee80211_sub_if_data *sdata, *found = NULL;
3827 	int count = 0;
3828 	int timeout;
3829 
3830 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS) ||
3831 	    ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
3832 		local->ps_sdata = NULL;
3833 		return;
3834 	}
3835 
3836 	list_for_each_entry(sdata, &local->interfaces, list) {
3837 		if (!ieee80211_sdata_running(sdata))
3838 			continue;
3839 		if (sdata->vif.type == NL80211_IFTYPE_AP) {
3840 			/* If an AP vif is found, then disable PS
3841 			 * by setting the count to zero thereby setting
3842 			 * ps_sdata to NULL.
3843 			 */
3844 			count = 0;
3845 			break;
3846 		}
3847 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
3848 			continue;
3849 		found = sdata;
3850 		count++;
3851 	}
3852 
3853 	if (count == 1 && ieee80211_powersave_allowed(found)) {
3854 		u8 dtimper = found->deflink.u.mgd.dtim_period;
3855 
3856 		timeout = local->dynamic_ps_forced_timeout;
3857 		if (timeout < 0)
3858 			timeout = 100;
3859 		local->hw.conf.dynamic_ps_timeout = timeout;
3860 
3861 		/* If the TIM IE is invalid, pretend the value is 1 */
3862 		if (!dtimper)
3863 			dtimper = 1;
3864 
3865 		local->hw.conf.ps_dtim_period = dtimper;
3866 		local->ps_sdata = found;
3867 	} else {
3868 		local->ps_sdata = NULL;
3869 	}
3870 
3871 	ieee80211_change_ps(local);
3872 }
3873 
3874 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
3875 {
3876 	bool ps_allowed = ieee80211_powersave_allowed(sdata);
3877 
3878 	if (sdata->vif.cfg.ps != ps_allowed) {
3879 		sdata->vif.cfg.ps = ps_allowed;
3880 		ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_PS);
3881 	}
3882 }
3883 
3884 void ieee80211_dynamic_ps_disable_work(struct wiphy *wiphy,
3885 				       struct wiphy_work *work)
3886 {
3887 	struct ieee80211_local *local =
3888 		container_of(work, struct ieee80211_local,
3889 			     dynamic_ps_disable_work);
3890 
3891 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3892 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3893 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
3894 	}
3895 
3896 	ieee80211_wake_queues_by_reason(&local->hw,
3897 					IEEE80211_MAX_QUEUE_MAP,
3898 					IEEE80211_QUEUE_STOP_REASON_PS,
3899 					false);
3900 }
3901 
3902 void ieee80211_dynamic_ps_enable_work(struct wiphy *wiphy,
3903 				      struct wiphy_work *work)
3904 {
3905 	struct ieee80211_local *local =
3906 		container_of(work, struct ieee80211_local,
3907 			     dynamic_ps_enable_work);
3908 	struct ieee80211_sub_if_data *sdata = local->ps_sdata;
3909 	struct ieee80211_if_managed *ifmgd;
3910 	unsigned long flags;
3911 	int q;
3912 
3913 	/* can only happen when PS was just disabled anyway */
3914 	if (!sdata)
3915 		return;
3916 
3917 	ifmgd = &sdata->u.mgd;
3918 
3919 	if (local->hw.conf.flags & IEEE80211_CONF_PS)
3920 		return;
3921 
3922 	if (local->hw.conf.dynamic_ps_timeout > 0) {
3923 		/* don't enter PS if TX frames are pending */
3924 		if (drv_tx_frames_pending(local)) {
3925 			mod_timer(&local->dynamic_ps_timer, jiffies +
3926 				  msecs_to_jiffies(
3927 				  local->hw.conf.dynamic_ps_timeout));
3928 			return;
3929 		}
3930 
3931 		/*
3932 		 * transmission can be stopped by others which leads to
3933 		 * dynamic_ps_timer expiry. Postpone the ps timer if it
3934 		 * is not the actual idle state.
3935 		 */
3936 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3937 		for (q = 0; q < local->hw.queues; q++) {
3938 			if (local->queue_stop_reasons[q]) {
3939 				spin_unlock_irqrestore(&local->queue_stop_reason_lock,
3940 						       flags);
3941 				mod_timer(&local->dynamic_ps_timer, jiffies +
3942 					  msecs_to_jiffies(
3943 					  local->hw.conf.dynamic_ps_timeout));
3944 				return;
3945 			}
3946 		}
3947 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3948 	}
3949 
3950 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
3951 	    !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
3952 		if (drv_tx_frames_pending(local)) {
3953 			mod_timer(&local->dynamic_ps_timer, jiffies +
3954 				  msecs_to_jiffies(
3955 				  local->hw.conf.dynamic_ps_timeout));
3956 		} else {
3957 			ieee80211_send_nullfunc(local, sdata, true);
3958 			/* Flush to get the tx status of nullfunc frame */
3959 			ieee80211_flush_queues(local, sdata, false);
3960 		}
3961 	}
3962 
3963 	if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
3964 	      ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
3965 	    (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
3966 		ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
3967 		local->hw.conf.flags |= IEEE80211_CONF_PS;
3968 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
3969 	}
3970 }
3971 
3972 void ieee80211_dynamic_ps_timer(struct timer_list *t)
3973 {
3974 	struct ieee80211_local *local = timer_container_of(local, t,
3975 							   dynamic_ps_timer);
3976 
3977 	wiphy_work_queue(local->hw.wiphy, &local->dynamic_ps_enable_work);
3978 }
3979 
3980 void ieee80211_dfs_cac_timer_work(struct wiphy *wiphy, struct wiphy_work *work)
3981 {
3982 	struct ieee80211_link_data *link =
3983 		container_of(work, struct ieee80211_link_data,
3984 			     dfs_cac_timer_work.work);
3985 	struct cfg80211_chan_def chandef = link->conf->chanreq.oper;
3986 	struct ieee80211_sub_if_data *sdata = link->sdata;
3987 
3988 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3989 
3990 	if (sdata->wdev.links[link->link_id].cac_started) {
3991 		ieee80211_link_release_channel(link);
3992 		cfg80211_cac_event(sdata->dev, &chandef,
3993 				   NL80211_RADAR_CAC_FINISHED,
3994 				   GFP_KERNEL, link->link_id);
3995 	}
3996 }
3997 
3998 static bool
3999 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
4000 {
4001 	struct ieee80211_local *local = sdata->local;
4002 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4003 	bool ret = false;
4004 	int ac;
4005 
4006 	if (local->hw.queues < IEEE80211_NUM_ACS)
4007 		return false;
4008 
4009 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4010 		struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
4011 		int non_acm_ac;
4012 		unsigned long now = jiffies;
4013 
4014 		if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
4015 		    tx_tspec->admitted_time &&
4016 		    time_after(now, tx_tspec->time_slice_start + HZ)) {
4017 			tx_tspec->consumed_tx_time = 0;
4018 			tx_tspec->time_slice_start = now;
4019 
4020 			if (tx_tspec->downgraded)
4021 				tx_tspec->action =
4022 					TX_TSPEC_ACTION_STOP_DOWNGRADE;
4023 		}
4024 
4025 		switch (tx_tspec->action) {
4026 		case TX_TSPEC_ACTION_STOP_DOWNGRADE:
4027 			/* take the original parameters */
4028 			if (drv_conf_tx(local, &sdata->deflink, ac,
4029 					&sdata->deflink.tx_conf[ac]))
4030 				link_err(&sdata->deflink,
4031 					 "failed to set TX queue parameters for queue %d\n",
4032 					 ac);
4033 			tx_tspec->action = TX_TSPEC_ACTION_NONE;
4034 			tx_tspec->downgraded = false;
4035 			ret = true;
4036 			break;
4037 		case TX_TSPEC_ACTION_DOWNGRADE:
4038 			if (time_after(now, tx_tspec->time_slice_start + HZ)) {
4039 				tx_tspec->action = TX_TSPEC_ACTION_NONE;
4040 				ret = true;
4041 				break;
4042 			}
4043 			/* downgrade next lower non-ACM AC */
4044 			for (non_acm_ac = ac + 1;
4045 			     non_acm_ac < IEEE80211_NUM_ACS;
4046 			     non_acm_ac++)
4047 				if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
4048 					break;
4049 			/* Usually the loop will result in using BK even if it
4050 			 * requires admission control, but such a configuration
4051 			 * makes no sense and we have to transmit somehow - the
4052 			 * AC selection does the same thing.
4053 			 * If we started out trying to downgrade from BK, then
4054 			 * the extra condition here might be needed.
4055 			 */
4056 			if (non_acm_ac >= IEEE80211_NUM_ACS)
4057 				non_acm_ac = IEEE80211_AC_BK;
4058 			if (drv_conf_tx(local, &sdata->deflink, ac,
4059 					&sdata->deflink.tx_conf[non_acm_ac]))
4060 				link_err(&sdata->deflink,
4061 					 "failed to set TX queue parameters for queue %d\n",
4062 					 ac);
4063 			tx_tspec->action = TX_TSPEC_ACTION_NONE;
4064 			ret = true;
4065 			wiphy_delayed_work_queue(local->hw.wiphy,
4066 						 &ifmgd->tx_tspec_wk,
4067 						 tx_tspec->time_slice_start +
4068 						 HZ - now + 1);
4069 			break;
4070 		case TX_TSPEC_ACTION_NONE:
4071 			/* nothing now */
4072 			break;
4073 		}
4074 	}
4075 
4076 	return ret;
4077 }
4078 
4079 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
4080 {
4081 	if (__ieee80211_sta_handle_tspec_ac_params(sdata))
4082 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
4083 						  BSS_CHANGED_QOS);
4084 }
4085 
4086 static void ieee80211_sta_handle_tspec_ac_params_wk(struct wiphy *wiphy,
4087 						    struct wiphy_work *work)
4088 {
4089 	struct ieee80211_sub_if_data *sdata;
4090 
4091 	sdata = container_of(work, struct ieee80211_sub_if_data,
4092 			     u.mgd.tx_tspec_wk.work);
4093 	ieee80211_sta_handle_tspec_ac_params(sdata);
4094 }
4095 
4096 void ieee80211_mgd_set_link_qos_params(struct ieee80211_link_data *link)
4097 {
4098 	struct ieee80211_sub_if_data *sdata = link->sdata;
4099 	struct ieee80211_local *local = sdata->local;
4100 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4101 	struct ieee80211_tx_queue_params *params = link->tx_conf;
4102 	u8 ac;
4103 
4104 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4105 		mlme_dbg(sdata,
4106 			 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
4107 			 ac, params[ac].acm,
4108 			 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
4109 			 params[ac].txop, params[ac].uapsd,
4110 			 ifmgd->tx_tspec[ac].downgraded);
4111 		if (!ifmgd->tx_tspec[ac].downgraded &&
4112 		    drv_conf_tx(local, link, ac, &params[ac]))
4113 			link_err(link,
4114 				 "failed to set TX queue parameters for AC %d\n",
4115 				 ac);
4116 	}
4117 }
4118 
4119 /* MLME */
4120 static bool
4121 _ieee80211_sta_wmm_params(struct ieee80211_local *local,
4122 			  struct ieee80211_link_data *link,
4123 			  const u8 *wmm_param, size_t wmm_param_len,
4124 			  const struct ieee80211_mu_edca_param_set *mu_edca)
4125 {
4126 	struct ieee80211_sub_if_data *sdata = link->sdata;
4127 	struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
4128 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4129 	size_t left;
4130 	int count, mu_edca_count, ac;
4131 	const u8 *pos;
4132 	u8 uapsd_queues = 0;
4133 
4134 	if (!local->ops->conf_tx)
4135 		return false;
4136 
4137 	if (local->hw.queues < IEEE80211_NUM_ACS)
4138 		return false;
4139 
4140 	if (!wmm_param)
4141 		return false;
4142 
4143 	if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
4144 		return false;
4145 
4146 	if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
4147 		uapsd_queues = ifmgd->uapsd_queues;
4148 
4149 	count = wmm_param[6] & 0x0f;
4150 	/* -1 is the initial value of ifmgd->mu_edca_last_param_set.
4151 	 * if mu_edca was preset before and now it disappeared tell
4152 	 * the driver about it.
4153 	 */
4154 	mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
4155 	if (count == link->u.mgd.wmm_last_param_set &&
4156 	    mu_edca_count == link->u.mgd.mu_edca_last_param_set)
4157 		return false;
4158 	link->u.mgd.wmm_last_param_set = count;
4159 	link->u.mgd.mu_edca_last_param_set = mu_edca_count;
4160 
4161 	pos = wmm_param + 8;
4162 	left = wmm_param_len - 8;
4163 
4164 	memset(&params, 0, sizeof(params));
4165 
4166 	sdata->wmm_acm = 0;
4167 	for (; left >= 4; left -= 4, pos += 4) {
4168 		int aci = (pos[0] >> 5) & 0x03;
4169 		int acm = (pos[0] >> 4) & 0x01;
4170 		bool uapsd = false;
4171 
4172 		switch (aci) {
4173 		case 1: /* AC_BK */
4174 			ac = IEEE80211_AC_BK;
4175 			if (acm)
4176 				sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
4177 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
4178 				uapsd = true;
4179 			params[ac].mu_edca = !!mu_edca;
4180 			if (mu_edca)
4181 				params[ac].mu_edca_param_rec = mu_edca->ac_bk;
4182 			break;
4183 		case 2: /* AC_VI */
4184 			ac = IEEE80211_AC_VI;
4185 			if (acm)
4186 				sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
4187 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
4188 				uapsd = true;
4189 			params[ac].mu_edca = !!mu_edca;
4190 			if (mu_edca)
4191 				params[ac].mu_edca_param_rec = mu_edca->ac_vi;
4192 			break;
4193 		case 3: /* AC_VO */
4194 			ac = IEEE80211_AC_VO;
4195 			if (acm)
4196 				sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
4197 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
4198 				uapsd = true;
4199 			params[ac].mu_edca = !!mu_edca;
4200 			if (mu_edca)
4201 				params[ac].mu_edca_param_rec = mu_edca->ac_vo;
4202 			break;
4203 		case 0: /* AC_BE */
4204 		default:
4205 			ac = IEEE80211_AC_BE;
4206 			if (acm)
4207 				sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
4208 			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
4209 				uapsd = true;
4210 			params[ac].mu_edca = !!mu_edca;
4211 			if (mu_edca)
4212 				params[ac].mu_edca_param_rec = mu_edca->ac_be;
4213 			break;
4214 		}
4215 
4216 		params[ac].aifs = pos[0] & 0x0f;
4217 
4218 		if (params[ac].aifs < 2) {
4219 			link_info(link,
4220 				  "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
4221 				  params[ac].aifs, aci);
4222 			params[ac].aifs = 2;
4223 		}
4224 		params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
4225 		params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
4226 		params[ac].txop = get_unaligned_le16(pos + 2);
4227 		params[ac].acm = acm;
4228 		params[ac].uapsd = uapsd;
4229 
4230 		if (params[ac].cw_min == 0 ||
4231 		    params[ac].cw_min > params[ac].cw_max) {
4232 			link_info(link,
4233 				  "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
4234 				  params[ac].cw_min, params[ac].cw_max, aci);
4235 			return false;
4236 		}
4237 		ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
4238 	}
4239 
4240 	/* WMM specification requires all 4 ACIs. */
4241 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4242 		if (params[ac].cw_min == 0) {
4243 			link_info(link,
4244 				  "AP has invalid WMM params (missing AC %d), using defaults\n",
4245 				  ac);
4246 			return false;
4247 		}
4248 	}
4249 
4250 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
4251 		link->tx_conf[ac] = params[ac];
4252 
4253 	return true;
4254 }
4255 
4256 static bool
4257 ieee80211_sta_wmm_params(struct ieee80211_local *local,
4258 			 struct ieee80211_link_data *link,
4259 			 const u8 *wmm_param, size_t wmm_param_len,
4260 			 const struct ieee80211_mu_edca_param_set *mu_edca)
4261 {
4262 	if (!_ieee80211_sta_wmm_params(local, link, wmm_param, wmm_param_len,
4263 				       mu_edca))
4264 		return false;
4265 
4266 	ieee80211_mgd_set_link_qos_params(link);
4267 
4268 	/* enable WMM or activate new settings */
4269 	link->conf->qos = true;
4270 	return true;
4271 }
4272 
4273 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
4274 {
4275 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4276 
4277 	sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
4278 	ieee80211_run_deferred_scan(sdata->local);
4279 }
4280 
4281 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
4282 {
4283 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4284 
4285 	__ieee80211_stop_poll(sdata);
4286 }
4287 
4288 static u64 ieee80211_handle_bss_capability(struct ieee80211_link_data *link,
4289 					   u16 capab, bool erp_valid, u8 erp)
4290 {
4291 	struct ieee80211_bss_conf *bss_conf = link->conf;
4292 	struct ieee80211_supported_band *sband;
4293 	u64 changed = 0;
4294 	bool use_protection;
4295 	bool use_short_preamble;
4296 	bool use_short_slot;
4297 
4298 	sband = ieee80211_get_link_sband(link);
4299 	if (!sband)
4300 		return changed;
4301 
4302 	if (erp_valid) {
4303 		use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
4304 		use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
4305 	} else {
4306 		use_protection = false;
4307 		use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
4308 	}
4309 
4310 	use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
4311 	if (sband->band == NL80211_BAND_5GHZ ||
4312 	    sband->band == NL80211_BAND_6GHZ)
4313 		use_short_slot = true;
4314 
4315 	if (use_protection != bss_conf->use_cts_prot) {
4316 		bss_conf->use_cts_prot = use_protection;
4317 		changed |= BSS_CHANGED_ERP_CTS_PROT;
4318 	}
4319 
4320 	if (use_short_preamble != bss_conf->use_short_preamble) {
4321 		bss_conf->use_short_preamble = use_short_preamble;
4322 		changed |= BSS_CHANGED_ERP_PREAMBLE;
4323 	}
4324 
4325 	if (use_short_slot != bss_conf->use_short_slot) {
4326 		bss_conf->use_short_slot = use_short_slot;
4327 		changed |= BSS_CHANGED_ERP_SLOT;
4328 	}
4329 
4330 	return changed;
4331 }
4332 
4333 static u64 ieee80211_link_set_associated(struct ieee80211_link_data *link,
4334 					 struct cfg80211_bss *cbss)
4335 {
4336 	struct ieee80211_sub_if_data *sdata = link->sdata;
4337 	struct ieee80211_bss_conf *bss_conf = link->conf;
4338 	struct ieee80211_bss *bss = (void *)cbss->priv;
4339 	u64 changed = BSS_CHANGED_QOS;
4340 
4341 	/* not really used in MLO */
4342 	sdata->u.mgd.beacon_timeout =
4343 		usecs_to_jiffies(ieee80211_tu_to_usec(beacon_loss_count *
4344 						      bss_conf->beacon_int));
4345 
4346 	changed |= ieee80211_handle_bss_capability(link,
4347 						   bss_conf->assoc_capability,
4348 						   bss->has_erp_value,
4349 						   bss->erp_value);
4350 
4351 	ieee80211_check_rate_mask(link);
4352 
4353 	link->conf->bss = cbss;
4354 	memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
4355 
4356 	if (sdata->vif.p2p ||
4357 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
4358 		const struct cfg80211_bss_ies *ies;
4359 
4360 		rcu_read_lock();
4361 		ies = rcu_dereference(cbss->ies);
4362 		if (ies) {
4363 			int ret;
4364 
4365 			ret = cfg80211_get_p2p_attr(
4366 					ies->data, ies->len,
4367 					IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
4368 					(u8 *) &bss_conf->p2p_noa_attr,
4369 					sizeof(bss_conf->p2p_noa_attr));
4370 			if (ret >= 2) {
4371 				link->u.mgd.p2p_noa_index =
4372 					bss_conf->p2p_noa_attr.index;
4373 				changed |= BSS_CHANGED_P2P_PS;
4374 			}
4375 		}
4376 		rcu_read_unlock();
4377 	}
4378 
4379 	if (link->u.mgd.have_beacon) {
4380 		bss_conf->beacon_rate = bss->beacon_rate;
4381 		changed |= BSS_CHANGED_BEACON_INFO;
4382 	} else {
4383 		bss_conf->beacon_rate = NULL;
4384 	}
4385 
4386 	/* Tell the driver to monitor connection quality (if supported) */
4387 	if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
4388 	    bss_conf->cqm_rssi_thold)
4389 		changed |= BSS_CHANGED_CQM;
4390 
4391 	return changed;
4392 }
4393 
4394 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
4395 				     struct ieee80211_mgd_assoc_data *assoc_data,
4396 				     u64 changed[IEEE80211_MLD_MAX_NUM_LINKS])
4397 {
4398 	struct ieee80211_local *local = sdata->local;
4399 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
4400 	u64 vif_changed = BSS_CHANGED_ASSOC;
4401 	unsigned int link_id;
4402 
4403 	lockdep_assert_wiphy(local->hw.wiphy);
4404 
4405 	sdata->u.mgd.associated = true;
4406 
4407 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
4408 		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
4409 		struct ieee80211_link_data *link;
4410 
4411 		if (!cbss ||
4412 		    assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS)
4413 			continue;
4414 
4415 		if (ieee80211_vif_is_mld(&sdata->vif) &&
4416 		    !(ieee80211_vif_usable_links(&sdata->vif) & BIT(link_id)))
4417 			continue;
4418 
4419 		link = sdata_dereference(sdata->link[link_id], sdata);
4420 		if (WARN_ON(!link))
4421 			return;
4422 
4423 		changed[link_id] |= ieee80211_link_set_associated(link, cbss);
4424 	}
4425 
4426 	/* just to be sure */
4427 	ieee80211_stop_poll(sdata);
4428 
4429 	ieee80211_led_assoc(local, 1);
4430 
4431 	vif_cfg->assoc = 1;
4432 
4433 	/* Enable ARP filtering */
4434 	if (vif_cfg->arp_addr_cnt)
4435 		vif_changed |= BSS_CHANGED_ARP_FILTER;
4436 
4437 	if (ieee80211_vif_is_mld(&sdata->vif)) {
4438 		for (link_id = 0;
4439 		     link_id < IEEE80211_MLD_MAX_NUM_LINKS;
4440 		     link_id++) {
4441 			struct ieee80211_link_data *link;
4442 			struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
4443 
4444 			if (!cbss ||
4445 			    !(BIT(link_id) &
4446 			      ieee80211_vif_usable_links(&sdata->vif)) ||
4447 			    assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS)
4448 				continue;
4449 
4450 			link = sdata_dereference(sdata->link[link_id], sdata);
4451 			if (WARN_ON(!link))
4452 				return;
4453 
4454 			ieee80211_link_info_change_notify(sdata, link,
4455 							  changed[link_id]);
4456 
4457 			ieee80211_recalc_smps(sdata, link);
4458 		}
4459 
4460 		ieee80211_vif_cfg_change_notify(sdata, vif_changed);
4461 	} else {
4462 		ieee80211_bss_info_change_notify(sdata,
4463 						 vif_changed | changed[0]);
4464 	}
4465 
4466 	ieee80211_recalc_ps(local);
4467 
4468 	/* leave this here to not change ordering in non-MLO cases */
4469 	if (!ieee80211_vif_is_mld(&sdata->vif))
4470 		ieee80211_recalc_smps(sdata, &sdata->deflink);
4471 	ieee80211_recalc_ps_vif(sdata);
4472 
4473 	netif_carrier_on(sdata->dev);
4474 }
4475 
4476 static void ieee80211_ml_reconf_reset(struct ieee80211_sub_if_data *sdata)
4477 {
4478 	struct ieee80211_mgd_assoc_data *add_links_data =
4479 		sdata->u.mgd.reconf.add_links_data;
4480 
4481 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
4482 	    !(sdata->u.mgd.reconf.added_links |
4483 	      sdata->u.mgd.reconf.removed_links))
4484 		return;
4485 
4486 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
4487 				  &sdata->u.mgd.reconf.wk);
4488 	sdata->u.mgd.reconf.added_links = 0;
4489 	sdata->u.mgd.reconf.removed_links = 0;
4490 	sdata->u.mgd.reconf.dialog_token = 0;
4491 
4492 	if (add_links_data) {
4493 		struct cfg80211_mlo_reconf_done_data done_data = {};
4494 		u8 link_id;
4495 
4496 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
4497 		     link_id++)
4498 			done_data.links[link_id].bss =
4499 				add_links_data->link[link_id].bss;
4500 
4501 		cfg80211_mlo_reconf_add_done(sdata->dev, &done_data);
4502 
4503 		kfree(sdata->u.mgd.reconf.add_links_data);
4504 		sdata->u.mgd.reconf.add_links_data = NULL;
4505 	}
4506 }
4507 
4508 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
4509 				   u16 stype, u16 reason, bool tx,
4510 				   u8 *frame_buf)
4511 {
4512 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4513 	struct ieee80211_local *local = sdata->local;
4514 	struct sta_info *ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
4515 	unsigned int link_id;
4516 	u64 changed = 0;
4517 	struct ieee80211_prep_tx_info info = {
4518 		.subtype = stype,
4519 		.was_assoc = true,
4520 		.link_id = ffs(sdata->vif.active_links) - 1,
4521 	};
4522 
4523 	lockdep_assert_wiphy(local->hw.wiphy);
4524 
4525 	if (frame_buf)
4526 		memset(frame_buf, 0, IEEE80211_DEAUTH_FRAME_LEN);
4527 
4528 	if (WARN_ON(!ap_sta))
4529 		return;
4530 
4531 	if (WARN_ON_ONCE(tx && !frame_buf))
4532 		return;
4533 
4534 	if (WARN_ON(!ifmgd->associated))
4535 		return;
4536 
4537 	ieee80211_stop_poll(sdata);
4538 
4539 	ifmgd->associated = false;
4540 
4541 	if (tx) {
4542 		bool tx_link_found = false;
4543 
4544 		for (link_id = 0;
4545 		     link_id < ARRAY_SIZE(sdata->link);
4546 		     link_id++) {
4547 			struct ieee80211_link_data *link;
4548 
4549 			if (!ieee80211_vif_link_active(&sdata->vif, link_id))
4550 				continue;
4551 
4552 			link = sdata_dereference(sdata->link[link_id], sdata);
4553 			if (WARN_ON_ONCE(!link))
4554 				continue;
4555 
4556 			if (link->u.mgd.csa.blocked_tx)
4557 				continue;
4558 
4559 			tx_link_found = true;
4560 			break;
4561 		}
4562 
4563 		tx = tx_link_found;
4564 	}
4565 
4566 	/* other links will be destroyed */
4567 	sdata->deflink.conf->bss = NULL;
4568 	sdata->deflink.conf->epcs_support = false;
4569 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
4570 
4571 	netif_carrier_off(sdata->dev);
4572 
4573 	/*
4574 	 * if we want to get out of ps before disassoc (why?) we have
4575 	 * to do it before sending disassoc, as otherwise the null-packet
4576 	 * won't be valid.
4577 	 */
4578 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4579 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4580 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
4581 	}
4582 	local->ps_sdata = NULL;
4583 
4584 	/* disable per-vif ps */
4585 	ieee80211_recalc_ps_vif(sdata);
4586 
4587 	/* make sure ongoing transmission finishes */
4588 	synchronize_net();
4589 
4590 	/*
4591 	 * drop any frame before deauth/disassoc, this can be data or
4592 	 * management frame. Since we are disconnecting, we should not
4593 	 * insist sending these frames which can take time and delay
4594 	 * the disconnection and possible the roaming.
4595 	 */
4596 	ieee80211_flush_queues(local, sdata, true);
4597 
4598 	if (tx) {
4599 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
4600 
4601 		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
4602 					       sdata->vif.cfg.ap_addr, stype,
4603 					       reason, true, frame_buf);
4604 
4605 		/* flush out frame - make sure the deauth was actually sent */
4606 		ieee80211_flush_queues(local, sdata, false);
4607 
4608 		drv_mgd_complete_tx(sdata->local, sdata, &info);
4609 	} else if (frame_buf) {
4610 		ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
4611 					       sdata->vif.cfg.ap_addr, stype,
4612 					       reason, false, frame_buf);
4613 	}
4614 
4615 	/* clear AP addr only after building the needed mgmt frames */
4616 	eth_zero_addr(sdata->deflink.u.mgd.bssid);
4617 	eth_zero_addr(sdata->vif.cfg.ap_addr);
4618 
4619 	sdata->vif.cfg.ssid_len = 0;
4620 
4621 	/* Remove TDLS peers */
4622 	__sta_info_flush(sdata, false, -1, ap_sta);
4623 
4624 	if (sdata->vif.driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC) {
4625 		/* Only move the AP state */
4626 		sta_info_move_state(ap_sta, IEEE80211_STA_NONE);
4627 	} else {
4628 		/* Remove AP peer */
4629 		sta_info_flush(sdata, -1);
4630 	}
4631 
4632 	/* finally reset all BSS / config parameters */
4633 	if (!ieee80211_vif_is_mld(&sdata->vif))
4634 		changed |= ieee80211_reset_erp_info(sdata);
4635 
4636 	ieee80211_led_assoc(local, 0);
4637 	changed |= BSS_CHANGED_ASSOC;
4638 	sdata->vif.cfg.assoc = false;
4639 
4640 	sdata->deflink.u.mgd.p2p_noa_index = -1;
4641 	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
4642 	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
4643 
4644 	/* on the next assoc, re-program HT/VHT parameters */
4645 	memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
4646 	memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
4647 	memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
4648 	memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
4649 
4650 	/*
4651 	 * reset MU-MIMO ownership and group data in default link,
4652 	 * if used, other links are destroyed
4653 	 */
4654 	memset(sdata->vif.bss_conf.mu_group.membership, 0,
4655 	       sizeof(sdata->vif.bss_conf.mu_group.membership));
4656 	memset(sdata->vif.bss_conf.mu_group.position, 0,
4657 	       sizeof(sdata->vif.bss_conf.mu_group.position));
4658 	if (!ieee80211_vif_is_mld(&sdata->vif))
4659 		changed |= BSS_CHANGED_MU_GROUPS;
4660 	sdata->vif.bss_conf.mu_mimo_owner = false;
4661 
4662 	sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
4663 
4664 	timer_delete_sync(&local->dynamic_ps_timer);
4665 	wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
4666 
4667 	/* Disable ARP filtering */
4668 	if (sdata->vif.cfg.arp_addr_cnt)
4669 		changed |= BSS_CHANGED_ARP_FILTER;
4670 
4671 	sdata->vif.bss_conf.qos = false;
4672 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
4673 		changed |= BSS_CHANGED_QOS;
4674 		/* The BSSID (not really interesting) and HT changed */
4675 		changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
4676 		ieee80211_bss_info_change_notify(sdata, changed);
4677 	} else {
4678 		ieee80211_vif_cfg_change_notify(sdata, changed);
4679 	}
4680 
4681 	if (sdata->vif.driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC) {
4682 		/*
4683 		 * After notifying the driver about the disassoc,
4684 		 * remove the ap sta.
4685 		 */
4686 		sta_info_flush(sdata, -1);
4687 	}
4688 
4689 	/* disassociated - set to defaults now */
4690 	ieee80211_set_wmm_default(&sdata->deflink, false, false);
4691 
4692 	timer_delete_sync(&sdata->u.mgd.conn_mon_timer);
4693 	timer_delete_sync(&sdata->u.mgd.bcn_mon_timer);
4694 	timer_delete_sync(&sdata->u.mgd.timer);
4695 
4696 	sdata->vif.bss_conf.dtim_period = 0;
4697 	sdata->vif.bss_conf.beacon_rate = NULL;
4698 
4699 	sdata->deflink.u.mgd.have_beacon = false;
4700 	sdata->deflink.u.mgd.tracking_signal_avg = false;
4701 	sdata->deflink.u.mgd.disable_wmm_tracking = false;
4702 
4703 	ifmgd->flags = 0;
4704 
4705 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
4706 		struct ieee80211_link_data *link;
4707 
4708 		link = sdata_dereference(sdata->link[link_id], sdata);
4709 		if (!link)
4710 			continue;
4711 		ieee80211_link_release_channel(link);
4712 	}
4713 
4714 	sdata->vif.bss_conf.csa_active = false;
4715 	sdata->deflink.u.mgd.csa.blocked_tx = false;
4716 	sdata->deflink.u.mgd.csa.waiting_bcn = false;
4717 	sdata->deflink.u.mgd.csa.ignored_same_chan = false;
4718 	ieee80211_vif_unblock_queues_csa(sdata);
4719 
4720 	/* existing TX TSPEC sessions no longer exist */
4721 	memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
4722 	wiphy_delayed_work_cancel(local->hw.wiphy, &ifmgd->tx_tspec_wk);
4723 
4724 	sdata->vif.bss_conf.power_type = IEEE80211_REG_UNSET_AP;
4725 	sdata->vif.bss_conf.pwr_reduction = 0;
4726 	ieee80211_clear_tpe(&sdata->vif.bss_conf.tpe);
4727 
4728 	sdata->vif.cfg.eml_cap = 0;
4729 	sdata->vif.cfg.eml_med_sync_delay = 0;
4730 	sdata->vif.cfg.mld_capa_op = 0;
4731 
4732 	memset(&sdata->u.mgd.ttlm_info, 0,
4733 	       sizeof(sdata->u.mgd.ttlm_info));
4734 	wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy, &ifmgd->ttlm_work);
4735 
4736 	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
4737 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
4738 				  &ifmgd->neg_ttlm_timeout_work);
4739 
4740 	sdata->u.mgd.removed_links = 0;
4741 	wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
4742 				  &sdata->u.mgd.ml_reconf_work);
4743 
4744 	wiphy_work_cancel(sdata->local->hw.wiphy,
4745 			  &ifmgd->teardown_ttlm_work);
4746 
4747 	/* if disconnection happens in the middle of the ML reconfiguration
4748 	 * flow, cfg80211 must called to release the BSS references obtained
4749 	 * when the flow started.
4750 	 */
4751 	ieee80211_ml_reconf_reset(sdata);
4752 
4753 	ieee80211_vif_set_links(sdata, 0, 0);
4754 
4755 	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
4756 
4757 	ifmgd->epcs.enabled = false;
4758 	ifmgd->epcs.dialog_token = 0;
4759 
4760 	memset(ifmgd->userspace_selectors, 0,
4761 	       sizeof(ifmgd->userspace_selectors));
4762 
4763 	ifmgd->uhr_omp.pending = 0;
4764 	ifmgd->uhr_omp.pending_init = 0;
4765 }
4766 
4767 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
4768 {
4769 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4770 	struct ieee80211_local *local = sdata->local;
4771 
4772 	lockdep_assert_wiphy(local->hw.wiphy);
4773 
4774 	if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
4775 		return;
4776 
4777 	__ieee80211_stop_poll(sdata);
4778 
4779 	ieee80211_recalc_ps(local);
4780 
4781 	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4782 		return;
4783 
4784 	/*
4785 	 * We've received a probe response, but are not sure whether
4786 	 * we have or will be receiving any beacons or data, so let's
4787 	 * schedule the timers again, just in case.
4788 	 */
4789 	ieee80211_sta_reset_beacon_monitor(sdata);
4790 
4791 	mod_timer(&ifmgd->conn_mon_timer,
4792 		  round_jiffies_up(jiffies +
4793 				   IEEE80211_CONNECTION_IDLE_TIME));
4794 }
4795 
4796 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
4797 					   struct ieee80211_hdr *hdr,
4798 					   u16 tx_time)
4799 {
4800 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4801 	u16 tid;
4802 	int ac;
4803 	struct ieee80211_sta_tx_tspec *tx_tspec;
4804 	unsigned long now = jiffies;
4805 
4806 	if (!ieee80211_is_data_qos(hdr->frame_control))
4807 		return;
4808 
4809 	tid = ieee80211_get_tid(hdr);
4810 	ac = ieee80211_ac_from_tid(tid);
4811 	tx_tspec = &ifmgd->tx_tspec[ac];
4812 
4813 	if (likely(!tx_tspec->admitted_time))
4814 		return;
4815 
4816 	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
4817 		tx_tspec->consumed_tx_time = 0;
4818 		tx_tspec->time_slice_start = now;
4819 
4820 		if (tx_tspec->downgraded) {
4821 			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
4822 			wiphy_delayed_work_queue(sdata->local->hw.wiphy,
4823 						 &ifmgd->tx_tspec_wk, 0);
4824 		}
4825 	}
4826 
4827 	if (tx_tspec->downgraded)
4828 		return;
4829 
4830 	tx_tspec->consumed_tx_time += tx_time;
4831 
4832 	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
4833 		tx_tspec->downgraded = true;
4834 		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
4835 		wiphy_delayed_work_queue(sdata->local->hw.wiphy,
4836 					 &ifmgd->tx_tspec_wk, 0);
4837 	}
4838 }
4839 
4840 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
4841 			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
4842 {
4843 	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
4844 
4845 	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
4846 	    !sdata->u.mgd.probe_send_count)
4847 		return;
4848 
4849 	if (ack)
4850 		sdata->u.mgd.probe_send_count = 0;
4851 	else
4852 		sdata->u.mgd.nullfunc_failed = true;
4853 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
4854 }
4855 
4856 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
4857 					  const u8 *src, const u8 *dst,
4858 					  const u8 *ssid, size_t ssid_len,
4859 					  struct ieee80211_channel *channel)
4860 {
4861 	struct sk_buff *skb;
4862 
4863 	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
4864 					ssid, ssid_len, NULL, 0,
4865 					IEEE80211_PROBE_FLAG_DIRECTED);
4866 	if (skb)
4867 		ieee80211_tx_skb(sdata, skb);
4868 }
4869 
4870 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
4871 {
4872 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4873 	u8 *dst = sdata->vif.cfg.ap_addr;
4874 	u8 unicast_limit = max(1, max_probe_tries - 3);
4875 	struct sta_info *sta;
4876 
4877 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4878 
4879 	/*
4880 	 * Try sending broadcast probe requests for the last three
4881 	 * probe requests after the first ones failed since some
4882 	 * buggy APs only support broadcast probe requests.
4883 	 */
4884 	if (ifmgd->probe_send_count >= unicast_limit)
4885 		dst = NULL;
4886 
4887 	/*
4888 	 * When the hardware reports an accurate Tx ACK status, it's
4889 	 * better to send a nullfunc frame instead of a probe request,
4890 	 * as it will kick us off the AP quickly if we aren't associated
4891 	 * anymore. The timeout will be reset if the frame is ACKed by
4892 	 * the AP.
4893 	 */
4894 	ifmgd->probe_send_count++;
4895 
4896 	if (dst) {
4897 		sta = sta_info_get(sdata, dst);
4898 		if (!WARN_ON(!sta))
4899 			ieee80211_check_fast_rx(sta);
4900 	}
4901 
4902 	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
4903 		ifmgd->nullfunc_failed = false;
4904 		ieee80211_send_nullfunc(sdata->local, sdata, false);
4905 	} else {
4906 		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
4907 					      sdata->vif.cfg.ssid,
4908 					      sdata->vif.cfg.ssid_len,
4909 					      sdata->deflink.conf->bss->channel);
4910 	}
4911 
4912 	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
4913 	run_again(sdata, ifmgd->probe_timeout);
4914 }
4915 
4916 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
4917 				   bool beacon)
4918 {
4919 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4920 	bool already = false;
4921 
4922 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4923 
4924 	if (!ieee80211_sdata_running(sdata))
4925 		return;
4926 
4927 	if (!ifmgd->associated)
4928 		return;
4929 
4930 	if (sdata->local->tmp_channel || sdata->local->scanning)
4931 		return;
4932 
4933 	if (sdata->local->suspending) {
4934 		/* reschedule after resume */
4935 		ieee80211_reset_ap_probe(sdata);
4936 		return;
4937 	}
4938 
4939 	if (beacon) {
4940 		mlme_dbg_ratelimited(sdata,
4941 				     "detected beacon loss from AP (missed %d beacons) - probing\n",
4942 				     beacon_loss_count);
4943 
4944 		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
4945 	}
4946 
4947 	/*
4948 	 * The driver/our work has already reported this event or the
4949 	 * connection monitoring has kicked in and we have already sent
4950 	 * a probe request. Or maybe the AP died and the driver keeps
4951 	 * reporting until we disassociate...
4952 	 *
4953 	 * In either case we have to ignore the current call to this
4954 	 * function (except for setting the correct probe reason bit)
4955 	 * because otherwise we would reset the timer every time and
4956 	 * never check whether we received a probe response!
4957 	 */
4958 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
4959 		already = true;
4960 
4961 	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
4962 
4963 	if (already)
4964 		return;
4965 
4966 	ieee80211_recalc_ps(sdata->local);
4967 
4968 	ifmgd->probe_send_count = 0;
4969 	ieee80211_mgd_probe_ap_send(sdata);
4970 }
4971 
4972 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
4973 					  struct ieee80211_vif *vif)
4974 {
4975 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4976 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4977 	struct cfg80211_bss *cbss;
4978 	struct sk_buff *skb;
4979 	const struct element *ssid;
4980 	int ssid_len;
4981 
4982 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
4983 
4984 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
4985 		    ieee80211_vif_is_mld(&sdata->vif)))
4986 		return NULL;
4987 
4988 	if (ifmgd->associated)
4989 		cbss = sdata->deflink.conf->bss;
4990 	else if (ifmgd->auth_data)
4991 		cbss = ifmgd->auth_data->bss;
4992 	else if (ifmgd->assoc_data && ifmgd->assoc_data->link[0].bss)
4993 		cbss = ifmgd->assoc_data->link[0].bss;
4994 	else
4995 		return NULL;
4996 
4997 	rcu_read_lock();
4998 	ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
4999 	if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
5000 		      "invalid SSID element (len=%d)",
5001 		      ssid ? ssid->datalen : -1))
5002 		ssid_len = 0;
5003 	else
5004 		ssid_len = ssid->datalen;
5005 
5006 	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
5007 					(u32) -1, cbss->channel,
5008 					ssid->data, ssid_len,
5009 					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
5010 	rcu_read_unlock();
5011 
5012 	return skb;
5013 }
5014 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
5015 
5016 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
5017 					const u8 *buf, size_t len, bool tx,
5018 					u16 reason, bool reconnect)
5019 {
5020 	struct ieee80211_event event = {
5021 		.type = MLME_EVENT,
5022 		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
5023 		.u.mlme.reason = reason,
5024 	};
5025 
5026 	if (tx)
5027 		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
5028 	else
5029 		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
5030 
5031 	drv_event_callback(sdata->local, sdata, &event);
5032 }
5033 
5034 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
5035 {
5036 	struct ieee80211_local *local = sdata->local;
5037 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5038 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5039 
5040 	lockdep_assert_wiphy(local->hw.wiphy);
5041 
5042 	if (!ifmgd->associated)
5043 		return;
5044 
5045 	if (!ifmgd->driver_disconnect) {
5046 		unsigned int link_id;
5047 
5048 		/*
5049 		 * AP is probably out of range (or not reachable for another
5050 		 * reason) so remove the bss structs for that AP. In the case
5051 		 * of multi-link, it's not clear that all of them really are
5052 		 * out of range, but if they weren't the driver likely would
5053 		 * have switched to just have a single link active?
5054 		 */
5055 		for (link_id = 0;
5056 		     link_id < ARRAY_SIZE(sdata->link);
5057 		     link_id++) {
5058 			struct ieee80211_link_data *link;
5059 
5060 			link = sdata_dereference(sdata->link[link_id], sdata);
5061 			if (!link || !link->conf->bss)
5062 				continue;
5063 			cfg80211_unlink_bss(local->hw.wiphy, link->conf->bss);
5064 			link->conf->bss = NULL;
5065 		}
5066 	}
5067 
5068 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5069 			       ifmgd->driver_disconnect ?
5070 					WLAN_REASON_DEAUTH_LEAVING :
5071 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
5072 			       true, frame_buf);
5073 	/* the other links will be destroyed */
5074 	sdata->vif.bss_conf.csa_active = false;
5075 	sdata->deflink.u.mgd.csa.waiting_bcn = false;
5076 	sdata->deflink.u.mgd.csa.blocked_tx = false;
5077 	ieee80211_vif_unblock_queues_csa(sdata);
5078 
5079 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5080 				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
5081 				    ifmgd->reconnect);
5082 	ifmgd->reconnect = false;
5083 }
5084 
5085 static void ieee80211_uhr_omp_req_status(struct ieee80211_sub_if_data *sdata)
5086 {
5087 	bool acked = sdata->u.mgd.uhr_omp.acked;
5088 	u16 links = sdata->u.mgd.uhr_omp.links;
5089 	struct ieee80211_link_data *link;
5090 	struct sta_info *ap;
5091 
5092 	/* timer and queued RX could overlap */
5093 	if (!links)
5094 		return;
5095 
5096 	sdata->u.mgd.uhr_omp.links = 0;
5097 	sdata->u.mgd.uhr_omp.acked = false;
5098 
5099 	if (!acked) {
5100 		sdata_dbg(sdata, "UHR OMP frame not ACKed - disconnect\n");
5101 		__ieee80211_disconnect(sdata);
5102 		return;
5103 	}
5104 
5105 	ap = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
5106 	if (!ap)
5107 		return;
5108 
5109 	for_each_link_data(sdata, link) {
5110 		struct link_sta_info *link_sta;
5111 
5112 		if (!(links & BIT(link->link_id)))
5113 			continue;
5114 
5115 		/* only handle transition to enabled for now */
5116 		if (!link->u.mgd.conn.dbe_enabled)
5117 			continue;
5118 
5119 		link_sta = sdata_dereference(ap->link[link->link_id], sdata);
5120 		if (WARN_ON(!link_sta))
5121 			continue;
5122 
5123 		link_sta->uhr_usable_tx_width = IEEE80211_STA_RX_BW_MAX;
5124 		ieee80211_link_sta_update_rc_bw(link, link_sta);
5125 	}
5126 
5127 	/* next round - send pending frames if needed */
5128 
5129 	if (sdata->u.mgd.uhr_omp.pending_init) {
5130 		links = sdata->u.mgd.uhr_omp.pending_init;
5131 
5132 		sdata->u.mgd.uhr_omp.pending_init = 0;
5133 		ieee80211_send_uhr_omp_req_dbe(sdata, links, true);
5134 		return;
5135 	}
5136 
5137 	if (sdata->u.mgd.uhr_omp.pending) {
5138 		links = sdata->u.mgd.uhr_omp.pending;
5139 
5140 		sdata->u.mgd.uhr_omp.pending = 0;
5141 		ieee80211_send_uhr_omp_req_dbe(sdata, links, false);
5142 		return;
5143 	}
5144 }
5145 
5146 static void ieee80211_uhr_omp_req_status_wk(struct wiphy *wiphy,
5147 					    struct wiphy_work *work)
5148 {
5149 	struct ieee80211_sub_if_data *sdata =
5150 		container_of(work, struct ieee80211_sub_if_data,
5151 			     u.mgd.uhr_omp.status_work.work);
5152 
5153 	ieee80211_uhr_omp_req_status(sdata);
5154 }
5155 
5156 static void ieee80211_process_uhr_omp_resp(struct ieee80211_sub_if_data *sdata,
5157 					   struct ieee80211_mgmt *mgmt)
5158 {
5159 	if (mgmt->u.action.uhr_link_reconf_notif.dialog_token !=
5160 	    sdata->u.mgd.uhr_omp.dialog_token)
5161 		return;
5162 
5163 	wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
5164 				  &sdata->u.mgd.uhr_omp.status_work);
5165 	ieee80211_uhr_omp_req_status(sdata);
5166 }
5167 
5168 static void
5169 ieee80211_process_uhr_link_reconf_notif(struct ieee80211_sub_if_data *sdata,
5170 					struct ieee80211_mgmt *mgmt,
5171 					size_t len)
5172 {
5173 	switch (mgmt->u.action.uhr_link_reconf_notif.type) {
5174 	case IEEE80211_UHR_LINK_RECONFIG_NOTIFY_OMP_RESPONSE:
5175 		ieee80211_process_uhr_omp_resp(sdata, mgmt);
5176 		break;
5177 	}
5178 }
5179 
5180 static void ieee80211_beacon_connection_loss_work(struct wiphy *wiphy,
5181 						  struct wiphy_work *work)
5182 {
5183 	struct ieee80211_sub_if_data *sdata =
5184 		container_of(work, struct ieee80211_sub_if_data,
5185 			     u.mgd.beacon_connection_loss_work);
5186 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5187 
5188 	if (ifmgd->connection_loss) {
5189 		sdata_info(sdata, "Connection to AP %pM lost\n",
5190 			   sdata->vif.cfg.ap_addr);
5191 		__ieee80211_disconnect(sdata);
5192 		ifmgd->connection_loss = false;
5193 	} else if (ifmgd->driver_disconnect) {
5194 		sdata_info(sdata,
5195 			   "Driver requested disconnection from AP %pM\n",
5196 			   sdata->vif.cfg.ap_addr);
5197 		__ieee80211_disconnect(sdata);
5198 		ifmgd->driver_disconnect = false;
5199 	} else {
5200 		if (ifmgd->associated)
5201 			sdata->deflink.u.mgd.beacon_loss_count++;
5202 		ieee80211_mgd_probe_ap(sdata, true);
5203 	}
5204 }
5205 
5206 static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
5207 					       struct wiphy_work *work)
5208 {
5209 	struct ieee80211_sub_if_data *sdata =
5210 		container_of(work, struct ieee80211_sub_if_data,
5211 			     u.mgd.csa_connection_drop_work);
5212 
5213 	__ieee80211_disconnect(sdata);
5214 }
5215 
5216 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
5217 {
5218 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5219 	struct ieee80211_hw *hw = &sdata->local->hw;
5220 
5221 	trace_api_beacon_loss(sdata);
5222 
5223 	sdata->u.mgd.connection_loss = false;
5224 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
5225 }
5226 EXPORT_SYMBOL(ieee80211_beacon_loss);
5227 
5228 void ieee80211_connection_loss(struct ieee80211_vif *vif)
5229 {
5230 	struct ieee80211_sub_if_data *sdata;
5231 	struct ieee80211_hw *hw;
5232 
5233 	KUNIT_STATIC_STUB_REDIRECT(ieee80211_connection_loss, vif);
5234 
5235 	sdata = vif_to_sdata(vif);
5236 	hw = &sdata->local->hw;
5237 
5238 	trace_api_connection_loss(sdata);
5239 
5240 	sdata->u.mgd.connection_loss = true;
5241 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
5242 }
5243 EXPORT_SYMBOL(ieee80211_connection_loss);
5244 
5245 void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
5246 {
5247 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5248 	struct ieee80211_hw *hw = &sdata->local->hw;
5249 
5250 	trace_api_disconnect(sdata, reconnect);
5251 
5252 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
5253 		return;
5254 
5255 	sdata->u.mgd.driver_disconnect = true;
5256 	sdata->u.mgd.reconnect = reconnect;
5257 	wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work);
5258 }
5259 EXPORT_SYMBOL(ieee80211_disconnect);
5260 
5261 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
5262 					bool assoc)
5263 {
5264 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
5265 
5266 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5267 
5268 	sdata->u.mgd.auth_data = NULL;
5269 
5270 	if (!assoc) {
5271 		/*
5272 		 * we are not authenticated yet, the only timer that could be
5273 		 * running is the timeout for the authentication response which
5274 		 * which is not relevant anymore.
5275 		 */
5276 		timer_delete_sync(&sdata->u.mgd.timer);
5277 		sta_info_destroy_addr(sdata, auth_data->ap_addr);
5278 
5279 		/* other links are destroyed */
5280 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
5281 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
5282 						  BSS_CHANGED_BSSID);
5283 		sdata->u.mgd.flags = 0;
5284 
5285 		ieee80211_link_release_channel(&sdata->deflink);
5286 		ieee80211_vif_set_links(sdata, 0, 0);
5287 	}
5288 
5289 	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
5290 	kfree(auth_data);
5291 }
5292 
5293 enum assoc_status {
5294 	ASSOC_SUCCESS,
5295 	ASSOC_REJECTED,
5296 	ASSOC_TIMEOUT,
5297 	ASSOC_ABANDON,
5298 };
5299 
5300 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
5301 					 enum assoc_status status)
5302 {
5303 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
5304 
5305 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5306 
5307 	sdata->u.mgd.assoc_data = NULL;
5308 
5309 	if (status != ASSOC_SUCCESS) {
5310 		/*
5311 		 * we are not associated yet, the only timer that could be
5312 		 * running is the timeout for the association response which
5313 		 * which is not relevant anymore.
5314 		 */
5315 		timer_delete_sync(&sdata->u.mgd.timer);
5316 		sta_info_destroy_addr(sdata, assoc_data->ap_addr);
5317 
5318 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
5319 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
5320 						  BSS_CHANGED_BSSID);
5321 		sdata->u.mgd.flags = 0;
5322 		sdata->vif.bss_conf.mu_mimo_owner = false;
5323 
5324 		if (status != ASSOC_REJECTED) {
5325 			struct cfg80211_assoc_failure data = {
5326 				.timeout = status == ASSOC_TIMEOUT,
5327 			};
5328 			int i;
5329 
5330 			BUILD_BUG_ON(ARRAY_SIZE(data.bss) !=
5331 				     ARRAY_SIZE(assoc_data->link));
5332 
5333 			for (i = 0; i < ARRAY_SIZE(data.bss); i++)
5334 				data.bss[i] = assoc_data->link[i].bss;
5335 
5336 			if (ieee80211_vif_is_mld(&sdata->vif))
5337 				data.ap_mld_addr = assoc_data->ap_addr;
5338 
5339 			cfg80211_assoc_failure(sdata->dev, &data);
5340 		}
5341 
5342 		ieee80211_link_release_channel(&sdata->deflink);
5343 		ieee80211_vif_set_links(sdata, 0, 0);
5344 	}
5345 
5346 	kfree(assoc_data);
5347 }
5348 
5349 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
5350 				     struct ieee80211_mgmt *mgmt, size_t len)
5351 {
5352 	struct ieee80211_local *local = sdata->local;
5353 	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
5354 	const struct element *challenge;
5355 	u8 *pos;
5356 	u32 tx_flags = 0;
5357 	struct ieee80211_prep_tx_info info = {
5358 		.subtype = IEEE80211_STYPE_AUTH,
5359 		.link_id = auth_data->link_id,
5360 	};
5361 
5362 	pos = mgmt->u.auth.variable;
5363 	challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
5364 				       len - (pos - (u8 *)mgmt));
5365 	if (!challenge)
5366 		return;
5367 	auth_data->expected_transaction = 4;
5368 	drv_mgd_prepare_tx(sdata->local, sdata, &info);
5369 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
5370 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
5371 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
5372 	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
5373 			    (void *)challenge,
5374 			    challenge->datalen + sizeof(*challenge),
5375 			    auth_data->ap_addr, auth_data->ap_addr,
5376 			    auth_data->key, auth_data->key_len,
5377 			    auth_data->key_idx, tx_flags);
5378 }
5379 
5380 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata)
5381 {
5382 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5383 	const u8 *ap_addr = ifmgd->auth_data->ap_addr;
5384 	struct sta_info *sta;
5385 
5386 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5387 
5388 	sdata_info(sdata, "authenticated\n");
5389 	ifmgd->auth_data->done = true;
5390 	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
5391 	ifmgd->auth_data->timeout_started = true;
5392 	run_again(sdata, ifmgd->auth_data->timeout);
5393 
5394 	/* move station state to auth */
5395 	sta = sta_info_get(sdata, ap_addr);
5396 	if (!sta) {
5397 		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, ap_addr);
5398 		return false;
5399 	}
5400 	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
5401 		sdata_info(sdata, "failed moving %pM to auth\n", ap_addr);
5402 		return false;
5403 	}
5404 
5405 	return true;
5406 }
5407 
5408 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
5409 				   struct ieee80211_mgmt *mgmt, size_t len)
5410 {
5411 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5412 	u16 auth_alg, auth_transaction, status_code, encap_len;
5413 	struct ieee80211_event event = {
5414 		.type = MLME_EVENT,
5415 		.u.mlme.data = AUTH_EVENT,
5416 	};
5417 	struct ieee80211_prep_tx_info info = {
5418 		.subtype = IEEE80211_STYPE_AUTH,
5419 	};
5420 	bool sae_need_confirm = false;
5421 	bool auth_fail = false;
5422 
5423 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5424 
5425 	if (len < 24 + 6)
5426 		return;
5427 
5428 	if (!ifmgd->auth_data || ifmgd->auth_data->done)
5429 		return;
5430 
5431 	if (!ether_addr_equal(ifmgd->auth_data->ap_addr, mgmt->bssid))
5432 		return;
5433 
5434 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
5435 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
5436 	status_code = le16_to_cpu(mgmt->u.auth.status_code);
5437 
5438 	/*
5439 	 * IEEE 802.1X Authentication:
5440 	 * Header + Authentication Algorithm Number(2 byte) + Authentication
5441 	 * Transaction Sequence Number(2 byte) + Status Code(2 byte) +
5442 	 * Encapsulation Length(2 byte).
5443 	 */
5444 	if (auth_alg == WLAN_AUTH_IEEE8021X && len < 24 + 8)
5445 		return;
5446 
5447 	info.link_id = ifmgd->auth_data->link_id;
5448 
5449 	if (auth_alg != ifmgd->auth_data->algorithm ||
5450 	    (auth_alg != WLAN_AUTH_SAE &&
5451 	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
5452 	    (auth_alg == WLAN_AUTH_SAE &&
5453 	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
5454 	      auth_transaction > 2))) {
5455 		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
5456 			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
5457 			   auth_transaction,
5458 			   ifmgd->auth_data->expected_transaction);
5459 		goto notify_driver;
5460 	}
5461 
5462 	switch (auth_alg) {
5463 	case WLAN_AUTH_IEEE8021X:
5464 		if (status_code != WLAN_STATUS_SUCCESS &&
5465 		    status_code != WLAN_STATUS_8021X_AUTH_SUCCESS)
5466 			auth_fail = true;
5467 
5468 		if (!auth_fail) {
5469 			/* Indicates length of encapsulated EAPOL PDU */
5470 			encap_len = get_unaligned_le16(mgmt->u.auth.variable);
5471 		}
5472 		break;
5473 	default:
5474 		if (status_code != WLAN_STATUS_SUCCESS)
5475 			auth_fail = true;
5476 		break;
5477 	}
5478 
5479 	if (auth_fail) {
5480 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
5481 
5482 		if (auth_alg == WLAN_AUTH_SAE &&
5483 		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
5484 		     (auth_transaction == 1 &&
5485 		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
5486 		       status_code == WLAN_STATUS_SAE_PK)))) {
5487 			/* waiting for userspace now */
5488 			ifmgd->auth_data->waiting = true;
5489 			ifmgd->auth_data->timeout =
5490 				jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
5491 			ifmgd->auth_data->timeout_started = true;
5492 			run_again(sdata, ifmgd->auth_data->timeout);
5493 			if (auth_transaction == 1)
5494 				sae_need_confirm = true;
5495 			goto notify_driver;
5496 		}
5497 
5498 		sdata_info(sdata, "%pM denied authentication (status %d)\n",
5499 			   mgmt->sa, status_code);
5500 		ieee80211_destroy_auth_data(sdata, false);
5501 		event.u.mlme.status = MLME_DENIED;
5502 		event.u.mlme.reason = status_code;
5503 		drv_event_callback(sdata->local, sdata, &event);
5504 		goto notify_driver;
5505 	}
5506 
5507 	switch (ifmgd->auth_data->algorithm) {
5508 	case WLAN_AUTH_OPEN:
5509 	case WLAN_AUTH_LEAP:
5510 	case WLAN_AUTH_FT:
5511 	case WLAN_AUTH_SAE:
5512 	case WLAN_AUTH_FILS_SK:
5513 	case WLAN_AUTH_FILS_SK_PFS:
5514 	case WLAN_AUTH_FILS_PK:
5515 	case WLAN_AUTH_EPPKE:
5516 	case WLAN_AUTH_IEEE8021X:
5517 		break;
5518 	case WLAN_AUTH_SHARED_KEY:
5519 		if (ifmgd->auth_data->expected_transaction != 4) {
5520 			ieee80211_auth_challenge(sdata, mgmt, len);
5521 			/* need another frame */
5522 			return;
5523 		}
5524 		break;
5525 	default:
5526 		WARN_ONCE(1, "invalid auth alg %d",
5527 			  ifmgd->auth_data->algorithm);
5528 		goto notify_driver;
5529 	}
5530 
5531 	event.u.mlme.status = MLME_SUCCESS;
5532 	info.success = 1;
5533 	drv_event_callback(sdata->local, sdata, &event);
5534 	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
5535 	    (auth_transaction == 2 &&
5536 	     ifmgd->auth_data->expected_transaction == 2)) {
5537 		switch (ifmgd->auth_data->algorithm) {
5538 		case WLAN_AUTH_IEEE8021X:
5539 			/*
5540 			 * IEEE 802.1X authentication:
5541 			 * - When the full EAP handshake completes over the
5542 			 *   Authentication process, the responder sets the
5543 			 *   Status Code to WLAN_STATUS_8021X_AUTH_SUCCESS as
5544 			 *   specified in "IEEE P802.11bi/D4.0, 12.16.5".
5545 			 *
5546 			 * - In the PMKSA caching case, only two Authentication
5547 			 *   frames are exchanged if the responder (e.g., AP)
5548 			 *   identifies a valid PMKSA, then as specified in
5549 			 *   "IEEE P802.11bi/D4.0, 12.16.8.3", the responder
5550 			 *   shall set the Status Code to SUCCESS in the final
5551 			 *   Authentication frame and must not include an
5552 			 *   encapsulated EAPOL PDU.
5553 			 *
5554 			 * Both conditions are treated as successful
5555 			 * authentication, so mark the state to Authenticated.
5556 			 */
5557 			if (status_code != WLAN_STATUS_8021X_AUTH_SUCCESS &&
5558 			    !(status_code == WLAN_STATUS_SUCCESS &&
5559 			      encap_len == 0))
5560 				break;
5561 			fallthrough;
5562 		default:
5563 			if (!ieee80211_mark_sta_auth(sdata))
5564 				return; /* ignore frame -- wait for timeout */
5565 
5566 			break;
5567 		}
5568 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
5569 		   auth_transaction == 1) {
5570 		sae_need_confirm = true;
5571 	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
5572 		   auth_transaction == 2) {
5573 		sdata_info(sdata, "SAE peer confirmed\n");
5574 		ifmgd->auth_data->peer_confirmed = true;
5575 	}
5576 
5577 	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
5578 notify_driver:
5579 	if (!sae_need_confirm)
5580 		drv_mgd_complete_tx(sdata->local, sdata, &info);
5581 }
5582 
5583 #define case_WLAN(type) \
5584 	case WLAN_REASON_##type: return #type
5585 
5586 const char *ieee80211_get_reason_code_string(u16 reason_code)
5587 {
5588 	switch (reason_code) {
5589 	case_WLAN(UNSPECIFIED);
5590 	case_WLAN(PREV_AUTH_NOT_VALID);
5591 	case_WLAN(DEAUTH_LEAVING);
5592 	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
5593 	case_WLAN(DISASSOC_AP_BUSY);
5594 	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
5595 	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
5596 	case_WLAN(DISASSOC_STA_HAS_LEFT);
5597 	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
5598 	case_WLAN(DISASSOC_BAD_POWER);
5599 	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
5600 	case_WLAN(INVALID_IE);
5601 	case_WLAN(MIC_FAILURE);
5602 	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
5603 	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
5604 	case_WLAN(IE_DIFFERENT);
5605 	case_WLAN(INVALID_GROUP_CIPHER);
5606 	case_WLAN(INVALID_PAIRWISE_CIPHER);
5607 	case_WLAN(INVALID_AKMP);
5608 	case_WLAN(UNSUPP_RSN_VERSION);
5609 	case_WLAN(INVALID_RSN_IE_CAP);
5610 	case_WLAN(IEEE8021X_FAILED);
5611 	case_WLAN(CIPHER_SUITE_REJECTED);
5612 	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
5613 	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
5614 	case_WLAN(DISASSOC_LOW_ACK);
5615 	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
5616 	case_WLAN(QSTA_LEAVE_QBSS);
5617 	case_WLAN(QSTA_NOT_USE);
5618 	case_WLAN(QSTA_REQUIRE_SETUP);
5619 	case_WLAN(QSTA_TIMEOUT);
5620 	case_WLAN(QSTA_CIPHER_NOT_SUPP);
5621 	case_WLAN(MESH_PEER_CANCELED);
5622 	case_WLAN(MESH_MAX_PEERS);
5623 	case_WLAN(MESH_CONFIG);
5624 	case_WLAN(MESH_CLOSE);
5625 	case_WLAN(MESH_MAX_RETRIES);
5626 	case_WLAN(MESH_CONFIRM_TIMEOUT);
5627 	case_WLAN(MESH_INVALID_GTK);
5628 	case_WLAN(MESH_INCONSISTENT_PARAM);
5629 	case_WLAN(MESH_INVALID_SECURITY);
5630 	case_WLAN(MESH_PATH_ERROR);
5631 	case_WLAN(MESH_PATH_NOFORWARD);
5632 	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
5633 	case_WLAN(MAC_EXISTS_IN_MBSS);
5634 	case_WLAN(MESH_CHAN_REGULATORY);
5635 	case_WLAN(MESH_CHAN);
5636 	default: return "<unknown>";
5637 	}
5638 }
5639 
5640 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
5641 				     struct ieee80211_mgmt *mgmt, size_t len)
5642 {
5643 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5644 	u16 reason_code;
5645 
5646 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5647 
5648 	if (len < offsetofend(struct ieee80211_mgmt, u.deauth.reason_code))
5649 		return;
5650 
5651 	reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
5652 
5653 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
5654 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
5655 		return;
5656 	}
5657 
5658 	if (ifmgd->associated &&
5659 	    ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
5660 		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
5661 			   sdata->vif.cfg.ap_addr, reason_code,
5662 			   ieee80211_get_reason_code_string(reason_code));
5663 
5664 		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
5665 
5666 		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
5667 					    reason_code, false);
5668 		return;
5669 	}
5670 
5671 	if (ifmgd->assoc_data &&
5672 	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->ap_addr)) {
5673 		sdata_info(sdata,
5674 			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
5675 			   ifmgd->assoc_data->ap_addr, reason_code,
5676 			   ieee80211_get_reason_code_string(reason_code));
5677 
5678 		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
5679 
5680 		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
5681 		return;
5682 	}
5683 }
5684 
5685 
5686 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
5687 				       struct ieee80211_mgmt *mgmt, size_t len)
5688 {
5689 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5690 	u16 reason_code;
5691 
5692 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5693 
5694 	if (len < 24 + 2)
5695 		return;
5696 
5697 	if (!ifmgd->associated ||
5698 	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
5699 		return;
5700 
5701 	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
5702 
5703 	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
5704 		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
5705 		return;
5706 	}
5707 
5708 	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
5709 		   sdata->vif.cfg.ap_addr, reason_code,
5710 		   ieee80211_get_reason_code_string(reason_code));
5711 
5712 	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
5713 
5714 	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
5715 				    false);
5716 }
5717 
5718 static bool ieee80211_twt_req_supported(struct ieee80211_sub_if_data *sdata,
5719 					struct ieee80211_supported_band *sband,
5720 					const struct link_sta_info *link_sta,
5721 					const struct ieee802_11_elems *elems)
5722 {
5723 	const struct ieee80211_sta_he_cap *own_he_cap =
5724 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
5725 
5726 	if (elems->ext_capab_len < 10)
5727 		return false;
5728 
5729 	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
5730 		return false;
5731 
5732 	return link_sta->pub->he_cap.he_cap_elem.mac_cap_info[0] &
5733 		IEEE80211_HE_MAC_CAP0_TWT_RES &&
5734 		own_he_cap &&
5735 		(own_he_cap->he_cap_elem.mac_cap_info[0] &
5736 			IEEE80211_HE_MAC_CAP0_TWT_REQ);
5737 }
5738 
5739 static u64 ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
5740 				    struct ieee80211_supported_band *sband,
5741 				    struct ieee80211_link_data *link,
5742 				    struct link_sta_info *link_sta,
5743 				    struct ieee802_11_elems *elems)
5744 {
5745 	bool twt = ieee80211_twt_req_supported(sdata, sband, link_sta, elems);
5746 
5747 	if (link->conf->twt_requester != twt) {
5748 		link->conf->twt_requester = twt;
5749 		return BSS_CHANGED_TWT;
5750 	}
5751 	return 0;
5752 }
5753 
5754 static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
5755 					struct ieee80211_bss_conf *bss_conf,
5756 					struct ieee80211_supported_band *sband,
5757 					struct link_sta_info *link_sta)
5758 {
5759 	const struct ieee80211_sta_he_cap *own_he_cap =
5760 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
5761 
5762 	return bss_conf->he_support &&
5763 		(link_sta->pub->he_cap.he_cap_elem.mac_cap_info[2] &
5764 			IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
5765 		own_he_cap &&
5766 		(own_he_cap->he_cap_elem.mac_cap_info[2] &
5767 			IEEE80211_HE_MAC_CAP2_BCAST_TWT);
5768 }
5769 
5770 static void ieee80211_epcs_changed(struct ieee80211_sub_if_data *sdata,
5771 				   bool enabled)
5772 {
5773 	/* in any case this is called, dialog token should be reset */
5774 	sdata->u.mgd.epcs.dialog_token = 0;
5775 
5776 	if (sdata->u.mgd.epcs.enabled == enabled)
5777 		return;
5778 
5779 	sdata->u.mgd.epcs.enabled = enabled;
5780 	cfg80211_epcs_changed(sdata->dev, enabled);
5781 }
5782 
5783 static void ieee80211_epcs_teardown(struct ieee80211_sub_if_data *sdata)
5784 {
5785 	struct ieee80211_local *local = sdata->local;
5786 	u8 link_id;
5787 
5788 	if (!sdata->u.mgd.epcs.enabled)
5789 		return;
5790 
5791 	lockdep_assert_wiphy(local->hw.wiphy);
5792 
5793 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
5794 		struct ieee802_11_elems *elems;
5795 		struct ieee80211_link_data *link;
5796 		const struct cfg80211_bss_ies *ies;
5797 		bool ret;
5798 
5799 		rcu_read_lock();
5800 
5801 		link = sdata_dereference(sdata->link[link_id], sdata);
5802 		if (!link || !link->conf || !link->conf->bss) {
5803 			rcu_read_unlock();
5804 			continue;
5805 		}
5806 
5807 		if (link->u.mgd.disable_wmm_tracking) {
5808 			rcu_read_unlock();
5809 			ieee80211_set_wmm_default(link, false, false);
5810 			continue;
5811 		}
5812 
5813 		ies = rcu_dereference(link->conf->bss->beacon_ies);
5814 		if (!ies) {
5815 			rcu_read_unlock();
5816 			ieee80211_set_wmm_default(link, false, false);
5817 			continue;
5818 		}
5819 
5820 		elems = ieee802_11_parse_elems(ies->data, ies->len,
5821 					       IEEE80211_FTYPE_MGMT |
5822 					       IEEE80211_STYPE_BEACON,
5823 					       NULL);
5824 		if (!elems) {
5825 			rcu_read_unlock();
5826 			ieee80211_set_wmm_default(link, false, false);
5827 			continue;
5828 		}
5829 
5830 		ret = _ieee80211_sta_wmm_params(local, link,
5831 						elems->wmm_param,
5832 						elems->wmm_param_len,
5833 						elems->mu_edca_param_set);
5834 
5835 		kfree(elems);
5836 		rcu_read_unlock();
5837 
5838 		if (!ret) {
5839 			ieee80211_set_wmm_default(link, false, false);
5840 			continue;
5841 		}
5842 
5843 		ieee80211_mgd_set_link_qos_params(link);
5844 		ieee80211_link_info_change_notify(sdata, link, BSS_CHANGED_QOS);
5845 	}
5846 }
5847 
5848 static bool ieee80211_assoc_config_link(struct ieee80211_link_data *link,
5849 					struct link_sta_info *link_sta,
5850 					struct cfg80211_bss *cbss,
5851 					struct ieee80211_mgmt *mgmt,
5852 					const u8 *elem_start,
5853 					unsigned int elem_len,
5854 					u64 *changed)
5855 {
5856 	struct ieee80211_sub_if_data *sdata = link->sdata;
5857 	struct ieee80211_mgd_assoc_data *assoc_data =
5858 		sdata->u.mgd.assoc_data ?: sdata->u.mgd.reconf.add_links_data;
5859 	struct ieee80211_bss_conf *bss_conf = link->conf;
5860 	struct ieee80211_local *local = sdata->local;
5861 	unsigned int link_id = link->link_id;
5862 	struct ieee80211_elems_parse_params parse_params = {
5863 		.mode = link->u.mgd.conn.mode,
5864 		.start = elem_start,
5865 		.len = elem_len,
5866 		.link_id = link_id == assoc_data->assoc_link_id ? -1 : link_id,
5867 		.from_ap = true,
5868 		.type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE,
5869 	};
5870 	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
5871 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
5872 	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
5873 	const struct cfg80211_bss_ies *bss_ies = NULL;
5874 	struct ieee80211_supported_band *sband;
5875 	struct ieee802_11_elems *elems;
5876 	const __le16 prof_bss_param_ch_present =
5877 		cpu_to_le16(IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT);
5878 	u16 capab_info;
5879 	bool ret;
5880 
5881 	elems = ieee802_11_parse_elems_full(&parse_params);
5882 	if (!elems)
5883 		return false;
5884 
5885 	if (link_id == assoc_data->assoc_link_id) {
5886 		capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
5887 
5888 		/*
5889 		 * we should not get to this flow unless the association was
5890 		 * successful, so set the status directly to success
5891 		 */
5892 		assoc_data->link[link_id].status = WLAN_STATUS_SUCCESS;
5893 		if (elems->ml_basic) {
5894 			int bss_param_ch_cnt =
5895 				ieee80211_mle_get_bss_param_ch_cnt((const void *)elems->ml_basic);
5896 
5897 			if (bss_param_ch_cnt < 0) {
5898 				ret = false;
5899 				goto out;
5900 			}
5901 			bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
5902 			bss_conf->bss_param_ch_cnt_link_id = link_id;
5903 		}
5904 	} else if (elems->parse_error & IEEE80211_PARSE_ERR_DUP_NEST_ML_BASIC ||
5905 		   !elems->prof ||
5906 		   !(elems->prof->control & prof_bss_param_ch_present)) {
5907 		ret = false;
5908 		goto out;
5909 	} else {
5910 		const u8 *ptr = elems->prof->variable +
5911 				elems->prof->sta_info_len - 1;
5912 		int bss_param_ch_cnt;
5913 
5914 		/*
5915 		 * During parsing, we validated that these fields exist,
5916 		 * otherwise elems->prof would have been set to NULL.
5917 		 */
5918 		capab_info = get_unaligned_le16(ptr);
5919 		assoc_data->link[link_id].status = get_unaligned_le16(ptr + 2);
5920 		bss_param_ch_cnt =
5921 			ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(elems->prof);
5922 		bss_conf->bss_param_ch_cnt = bss_param_ch_cnt;
5923 		bss_conf->bss_param_ch_cnt_link_id = link_id;
5924 
5925 		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
5926 			link_info(link, "association response status code=%u\n",
5927 				  assoc_data->link[link_id].status);
5928 			ret = true;
5929 			goto out;
5930 		}
5931 	}
5932 
5933 	if (!is_s1g && !elems->supp_rates) {
5934 		sdata_info(sdata, "no SuppRates element in AssocResp\n");
5935 		ret = false;
5936 		goto out;
5937 	}
5938 
5939 	link->u.mgd.tdls_chan_switch_prohibited =
5940 		elems->ext_capab && elems->ext_capab_len >= 5 &&
5941 		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
5942 
5943 	/*
5944 	 * Some APs are erroneously not including some information in their
5945 	 * (re)association response frames. Try to recover by using the data
5946 	 * from the beacon or probe response. This seems to afflict mobile
5947 	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
5948 	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
5949 	 */
5950 	if (!ieee80211_hw_check(&local->hw, STRICT) && !is_6ghz &&
5951 	    ((assoc_data->wmm && !elems->wmm_param) ||
5952 	     (link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
5953 	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
5954 	     (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
5955 	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
5956 		const struct cfg80211_bss_ies *ies;
5957 		struct ieee802_11_elems *bss_elems;
5958 
5959 		rcu_read_lock();
5960 		ies = rcu_dereference(cbss->ies);
5961 		if (ies)
5962 			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
5963 					  GFP_ATOMIC);
5964 		rcu_read_unlock();
5965 		if (!bss_ies) {
5966 			ret = false;
5967 			goto out;
5968 		}
5969 
5970 		parse_params.start = bss_ies->data;
5971 		parse_params.len = bss_ies->len;
5972 		parse_params.bss = cbss;
5973 		parse_params.link_id = -1;
5974 		bss_elems = ieee802_11_parse_elems_full(&parse_params);
5975 		if (!bss_elems) {
5976 			ret = false;
5977 			goto out;
5978 		}
5979 
5980 		if (assoc_data->wmm &&
5981 		    !elems->wmm_param && bss_elems->wmm_param) {
5982 			elems->wmm_param = bss_elems->wmm_param;
5983 			sdata_info(sdata,
5984 				   "AP bug: WMM param missing from AssocResp\n");
5985 		}
5986 
5987 		/*
5988 		 * Also check if we requested HT/VHT, otherwise the AP doesn't
5989 		 * have to include the IEs in the (re)association response.
5990 		 */
5991 		if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
5992 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
5993 			elems->ht_cap_elem = bss_elems->ht_cap_elem;
5994 			sdata_info(sdata,
5995 				   "AP bug: HT capability missing from AssocResp\n");
5996 		}
5997 		if (!elems->ht_operation && bss_elems->ht_operation &&
5998 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT) {
5999 			elems->ht_operation = bss_elems->ht_operation;
6000 			sdata_info(sdata,
6001 				   "AP bug: HT operation missing from AssocResp\n");
6002 		}
6003 
6004 		if (is_5ghz) {
6005 			if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
6006 			    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
6007 				elems->vht_cap_elem = bss_elems->vht_cap_elem;
6008 				sdata_info(sdata,
6009 					   "AP bug: VHT capa missing from AssocResp\n");
6010 			}
6011 
6012 			if (!elems->vht_operation && bss_elems->vht_operation &&
6013 			    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
6014 				elems->vht_operation = bss_elems->vht_operation;
6015 				sdata_info(sdata,
6016 					   "AP bug: VHT operation missing from AssocResp\n");
6017 			}
6018 		}
6019 		kfree(bss_elems);
6020 	}
6021 
6022 	/*
6023 	 * We previously checked these in the beacon/probe response, so
6024 	 * they should be present here. This is just a safety net.
6025 	 * Note that the ieee80211_config_bw() below would also check
6026 	 * for this (and more), but this has better error reporting.
6027 	 */
6028 	if (!is_6ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT &&
6029 	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
6030 		sdata_info(sdata,
6031 			   "HT AP is missing WMM params or HT capability/operation\n");
6032 		ret = false;
6033 		goto out;
6034 	}
6035 
6036 	if (is_5ghz && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT &&
6037 	    (!elems->vht_cap_elem || !elems->vht_operation)) {
6038 		sdata_info(sdata,
6039 			   "VHT AP is missing VHT capability/operation\n");
6040 		ret = false;
6041 		goto out;
6042 	}
6043 
6044 	/* check/update if AP changed anything in assoc response vs. scan */
6045 	if (ieee80211_config_bw(link, elems,
6046 				link_id == assoc_data->assoc_link_id,
6047 				changed,
6048 				le16_to_cpu(mgmt->frame_control) &
6049 					IEEE80211_FCTL_STYPE)) {
6050 		ret = false;
6051 		goto out;
6052 	}
6053 
6054 	if (WARN_ON(!link->conf->chanreq.oper.chan)) {
6055 		ret = false;
6056 		goto out;
6057 	}
6058 	sband = local->hw.wiphy->bands[link->conf->chanreq.oper.chan->band];
6059 
6060 	/* Set up internal HT/VHT capabilities */
6061 	if (elems->ht_cap_elem && link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT)
6062 		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, &sband->ht_cap,
6063 						  elems->ht_cap_elem,
6064 						  link_sta);
6065 
6066 	if (elems->vht_cap_elem &&
6067 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_VHT) {
6068 		const struct ieee80211_vht_cap *bss_vht_cap = NULL;
6069 		const struct cfg80211_bss_ies *ies;
6070 
6071 		/*
6072 		 * Cisco AP module 9115 with FW 17.3 has a bug and sends a
6073 		 * too large maximum MPDU length in the association response
6074 		 * (indicating 12k) that it cannot actually process ...
6075 		 * Work around that.
6076 		 */
6077 		rcu_read_lock();
6078 		ies = rcu_dereference(cbss->ies);
6079 		if (ies) {
6080 			const struct element *elem;
6081 
6082 			elem = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY,
6083 						  ies->data, ies->len);
6084 			if (elem && elem->datalen >= sizeof(*bss_vht_cap))
6085 				bss_vht_cap = (const void *)elem->data;
6086 		}
6087 
6088 		if (ieee80211_hw_check(&local->hw, STRICT) &&
6089 		    (!bss_vht_cap || memcmp(bss_vht_cap, elems->vht_cap_elem,
6090 					    sizeof(*bss_vht_cap)))) {
6091 			rcu_read_unlock();
6092 			ret = false;
6093 			link_info(link, "VHT capabilities mismatch\n");
6094 			goto out;
6095 		}
6096 
6097 		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
6098 						    &sband->vht_cap,
6099 						    elems->vht_cap_elem,
6100 						    bss_vht_cap, link_sta);
6101 		rcu_read_unlock();
6102 	}
6103 
6104 	if (elems->he_operation &&
6105 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_HE &&
6106 	    elems->he_cap) {
6107 		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
6108 						  elems->he_cap,
6109 						  elems->he_cap_len,
6110 						  elems->he_6ghz_capa,
6111 						  link_sta);
6112 
6113 		bss_conf->he_support = link_sta->pub->he_cap.has_he;
6114 		if (elems->rsnx && elems->rsnx_len &&
6115 		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
6116 		    wiphy_ext_feature_isset(local->hw.wiphy,
6117 					    NL80211_EXT_FEATURE_PROTECTED_TWT))
6118 			bss_conf->twt_protected = true;
6119 		else
6120 			bss_conf->twt_protected = false;
6121 
6122 		*changed |= ieee80211_recalc_twt_req(sdata, sband, link,
6123 						     link_sta, elems);
6124 
6125 		if (elems->eht_operation && elems->eht_cap &&
6126 		    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_EHT) {
6127 			ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband,
6128 							    elems->he_cap,
6129 							    elems->he_cap_len,
6130 							    elems->eht_cap,
6131 							    elems->eht_cap_len,
6132 							    link_sta);
6133 
6134 			bss_conf->eht_support = link_sta->pub->eht_cap.has_eht;
6135 			bss_conf->epcs_support = bss_conf->eht_support &&
6136 				!!(elems->eht_cap->fixed.mac_cap_info[0] &
6137 				   IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS);
6138 
6139 			/* EPCS might be already enabled but a new added link
6140 			 * does not support EPCS. This should not really happen
6141 			 * in practice.
6142 			 */
6143 			if (sdata->u.mgd.epcs.enabled &&
6144 			    !bss_conf->epcs_support)
6145 				ieee80211_epcs_teardown(sdata);
6146 		} else {
6147 			bss_conf->eht_support = false;
6148 			bss_conf->epcs_support = false;
6149 		}
6150 	} else {
6151 		bss_conf->he_support = false;
6152 		bss_conf->twt_requester = false;
6153 		bss_conf->twt_protected = false;
6154 		bss_conf->eht_support = false;
6155 		bss_conf->epcs_support = false;
6156 	}
6157 
6158 	if (elems->uhr_operation && elems->uhr_cap &&
6159 	    link->u.mgd.conn.mode >= IEEE80211_CONN_MODE_UHR) {
6160 		int omp_to_us;
6161 
6162 		ieee80211_uhr_cap_ie_to_sta_uhr_cap(sdata, sband,
6163 						    elems->uhr_cap,
6164 						    elems->uhr_cap_len,
6165 						    link_sta);
6166 
6167 		bss_conf->uhr_support = link_sta->pub->uhr_cap.has_uhr;
6168 
6169 		/*
6170 		 * This assumes that the timeout is the same across all links,
6171 		 * maybe we should actually validate that.
6172 		 */
6173 		omp_to_us = ieee80211_uhr_capa_get_om_pu_to_us(elems->uhr_cap);
6174 		if (omp_to_us < 0) {
6175 			ret = false;
6176 			link_info(link, "Invalid UHR OMP timeout\n");
6177 			goto out;
6178 		}
6179 
6180 		sdata->u.mgd.uhr_omp.timeout_us = omp_to_us;
6181 	} else {
6182 		bss_conf->uhr_support = false;
6183 	}
6184 
6185 	if (elems->s1g_oper &&
6186 	    link->u.mgd.conn.mode == IEEE80211_CONN_MODE_S1G &&
6187 	    elems->s1g_capab)
6188 		ieee80211_s1g_cap_to_sta_s1g_cap(sdata, elems->s1g_capab,
6189 						 link_sta);
6190 
6191 	bss_conf->twt_broadcast =
6192 		ieee80211_twt_bcast_support(sdata, bss_conf, sband, link_sta);
6193 
6194 	if (bss_conf->he_support) {
6195 		bss_conf->he_bss_color.color =
6196 			le32_get_bits(elems->he_operation->he_oper_params,
6197 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
6198 		bss_conf->he_bss_color.partial =
6199 			le32_get_bits(elems->he_operation->he_oper_params,
6200 				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
6201 		bss_conf->he_bss_color.enabled =
6202 			!le32_get_bits(elems->he_operation->he_oper_params,
6203 				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
6204 
6205 		if (bss_conf->he_bss_color.enabled)
6206 			*changed |= BSS_CHANGED_HE_BSS_COLOR;
6207 
6208 		bss_conf->htc_trig_based_pkt_ext =
6209 			le32_get_bits(elems->he_operation->he_oper_params,
6210 				      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
6211 		bss_conf->frame_time_rts_th =
6212 			le32_get_bits(elems->he_operation->he_oper_params,
6213 				      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
6214 
6215 		bss_conf->uora_exists = !!elems->uora_element;
6216 		if (elems->uora_element)
6217 			bss_conf->uora_ocw_range = elems->uora_element[0];
6218 
6219 		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
6220 		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
6221 		/* TODO: OPEN: what happens if BSS color disable is set? */
6222 	}
6223 
6224 	if (cbss->transmitted_bss) {
6225 		bss_conf->nontransmitted = true;
6226 		ether_addr_copy(bss_conf->transmitter_bssid,
6227 				cbss->transmitted_bss->bssid);
6228 		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
6229 		bss_conf->bssid_index = cbss->bssid_index;
6230 	}
6231 
6232 	/*
6233 	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
6234 	 * in their association response, so ignore that data for our own
6235 	 * configuration. If it changed since the last beacon, we'll get the
6236 	 * next beacon and update then.
6237 	 */
6238 
6239 	ieee80211_sta_init_nss_bw_capa(link_sta, &bss_conf->chanreq.oper);
6240 
6241 	/* If an operating mode notification element is present, use it. */
6242 	if (elems->opmode_notif)
6243 		__ieee80211_vht_handle_opmode(sdata, link_sta,
6244 					      *elems->opmode_notif,
6245 					      sband->band);
6246 
6247 	/*
6248 	 * Always handle WMM once after association regardless
6249 	 * of the first value the AP uses. Setting -1 here has
6250 	 * that effect because the AP values is an unsigned
6251 	 * 4-bit value.
6252 	 */
6253 	link->u.mgd.wmm_last_param_set = -1;
6254 	link->u.mgd.mu_edca_last_param_set = -1;
6255 
6256 	if (link->u.mgd.disable_wmm_tracking) {
6257 		ieee80211_set_wmm_default(link, false, false);
6258 	} else if (!ieee80211_sta_wmm_params(local, link, elems->wmm_param,
6259 					     elems->wmm_param_len,
6260 					     elems->mu_edca_param_set)) {
6261 		/* still enable QoS since we might have HT/VHT */
6262 		ieee80211_set_wmm_default(link, false, true);
6263 		/* disable WMM tracking in this case to disable
6264 		 * tracking WMM parameter changes in the beacon if
6265 		 * the parameters weren't actually valid. Doing so
6266 		 * avoids changing parameters very strangely when
6267 		 * the AP is going back and forth between valid and
6268 		 * invalid parameters.
6269 		 */
6270 		link->u.mgd.disable_wmm_tracking = true;
6271 	}
6272 
6273 	if (elems->max_idle_period_ie) {
6274 		bss_conf->max_idle_period =
6275 			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
6276 		bss_conf->protected_keep_alive =
6277 			!!(elems->max_idle_period_ie->idle_options &
6278 			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
6279 		*changed |= BSS_CHANGED_KEEP_ALIVE;
6280 	} else {
6281 		bss_conf->max_idle_period = 0;
6282 		bss_conf->protected_keep_alive = false;
6283 	}
6284 
6285 	/* set assoc capability (AID was already set earlier),
6286 	 * ieee80211_set_associated() will tell the driver */
6287 	bss_conf->assoc_capability = capab_info;
6288 
6289 	ret = true;
6290 out:
6291 	kfree(elems);
6292 	kfree(bss_ies);
6293 	return ret;
6294 }
6295 
6296 static int ieee80211_mgd_setup_link_sta(struct ieee80211_link_data *link,
6297 					struct sta_info *sta,
6298 					struct link_sta_info *link_sta,
6299 					struct cfg80211_bss *cbss)
6300 {
6301 	struct ieee80211_sub_if_data *sdata = link->sdata;
6302 	struct ieee80211_local *local = sdata->local;
6303 	struct ieee80211_bss *bss = (void *)cbss->priv;
6304 	u32 rates = 0, basic_rates = 0;
6305 	bool have_higher_than_11mbit = false;
6306 	int min_rate = INT_MAX, min_rate_index = -1;
6307 	struct ieee80211_supported_band *sband;
6308 
6309 	memcpy(link_sta->addr, cbss->bssid, ETH_ALEN);
6310 	memcpy(link_sta->pub->addr, cbss->bssid, ETH_ALEN);
6311 
6312 	/* TODO: S1G Basic Rate Set is expressed elsewhere */
6313 	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
6314 		ieee80211_s1g_sta_rate_init(sta);
6315 		return 0;
6316 	}
6317 
6318 	sband = local->hw.wiphy->bands[cbss->channel->band];
6319 
6320 	ieee80211_get_rates(sband, bss->supp_rates, bss->supp_rates_len,
6321 			    NULL, 0,
6322 			    &rates, &basic_rates, NULL,
6323 			    &have_higher_than_11mbit,
6324 			    &min_rate, &min_rate_index);
6325 
6326 	/*
6327 	 * This used to be a workaround for basic rates missing
6328 	 * in the association response frame. Now that we no
6329 	 * longer use the basic rates from there, it probably
6330 	 * doesn't happen any more, but keep the workaround so
6331 	 * in case some *other* APs are buggy in different ways
6332 	 * we can connect -- with a warning.
6333 	 * Allow this workaround only in case the AP provided at least
6334 	 * one rate.
6335 	 */
6336 	if (min_rate_index < 0) {
6337 		link_info(link, "No legacy rates in association response\n");
6338 		return -EINVAL;
6339 	} else if (!basic_rates) {
6340 		link_info(link, "No basic rates, using min rate instead\n");
6341 		basic_rates = BIT(min_rate_index);
6342 	}
6343 
6344 	if (rates)
6345 		link_sta->pub->supp_rates[cbss->channel->band] = rates;
6346 	else
6347 		link_info(link, "No rates found, keeping mandatory only\n");
6348 
6349 	link->conf->basic_rates = basic_rates;
6350 
6351 	/* cf. IEEE 802.11 9.2.12 */
6352 	link->operating_11g_mode = sband->band == NL80211_BAND_2GHZ &&
6353 				   have_higher_than_11mbit;
6354 
6355 	return 0;
6356 }
6357 
6358 static u8 ieee80211_max_rx_chains(struct ieee80211_link_data *link,
6359 				  struct cfg80211_bss *cbss)
6360 {
6361 	struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
6362 	const struct element *ht_cap_elem, *vht_cap_elem;
6363 	const struct cfg80211_bss_ies *ies;
6364 	const struct ieee80211_ht_cap *ht_cap;
6365 	const struct ieee80211_vht_cap *vht_cap;
6366 	const struct ieee80211_he_cap_elem *he_cap;
6367 	const struct element *he_cap_elem;
6368 	u16 mcs_80_map, mcs_160_map;
6369 	int i, mcs_nss_size;
6370 	bool support_160;
6371 	u8 chains = 1;
6372 
6373 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HT)
6374 		return chains;
6375 
6376 	ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
6377 	if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
6378 		ht_cap = (void *)ht_cap_elem->data;
6379 		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
6380 		/*
6381 		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
6382 		 *	 "Tx Unequal Modulation Supported" fields.
6383 		 */
6384 	}
6385 
6386 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_VHT)
6387 		return chains;
6388 
6389 	vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
6390 	if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
6391 		u8 nss;
6392 		u16 tx_mcs_map;
6393 
6394 		vht_cap = (void *)vht_cap_elem->data;
6395 		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
6396 		for (nss = 8; nss > 0; nss--) {
6397 			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
6398 					IEEE80211_VHT_MCS_NOT_SUPPORTED)
6399 				break;
6400 		}
6401 		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
6402 		chains = max(chains, nss);
6403 	}
6404 
6405 	if (link->u.mgd.conn.mode < IEEE80211_CONN_MODE_HE)
6406 		return chains;
6407 
6408 	ies = rcu_dereference(cbss->ies);
6409 	he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
6410 					     ies->data, ies->len);
6411 
6412 	if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap) + 1)
6413 		return chains;
6414 
6415 	/* skip one byte ext_tag_id */
6416 	he_cap = (void *)(he_cap_elem->data + 1);
6417 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
6418 
6419 	/* invalid HE IE */
6420 	if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap))
6421 		return chains;
6422 
6423 	/* mcs_nss is right after he_cap info */
6424 	he_mcs_nss_supp = (void *)(he_cap + 1);
6425 
6426 	mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
6427 
6428 	for (i = 7; i >= 0; i--) {
6429 		u8 mcs_80 = mcs_80_map >> (2 * i) & 3;
6430 
6431 		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
6432 			chains = max_t(u8, chains, i + 1);
6433 			break;
6434 		}
6435 	}
6436 
6437 	support_160 = he_cap->phy_cap_info[0] &
6438 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
6439 
6440 	if (!support_160)
6441 		return chains;
6442 
6443 	mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160);
6444 	for (i = 7; i >= 0; i--) {
6445 		u8 mcs_160 = mcs_160_map >> (2 * i) & 3;
6446 
6447 		if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
6448 			chains = max_t(u8, chains, i + 1);
6449 			break;
6450 		}
6451 	}
6452 
6453 	return chains;
6454 }
6455 
6456 static void
6457 ieee80211_determine_our_sta_mode(struct ieee80211_sub_if_data *sdata,
6458 				 struct ieee80211_supported_band *sband,
6459 				 struct cfg80211_assoc_request *req,
6460 				 bool wmm_used, int link_id,
6461 				 struct ieee80211_conn_settings *conn)
6462 {
6463 	struct ieee80211_sta_ht_cap sta_ht_cap = sband->ht_cap;
6464 	bool is_5ghz = sband->band == NL80211_BAND_5GHZ;
6465 	bool is_6ghz = sband->band == NL80211_BAND_6GHZ;
6466 	const struct ieee80211_sta_he_cap *he_cap;
6467 	const struct ieee80211_sta_eht_cap *eht_cap;
6468 	const struct ieee80211_sta_uhr_cap *uhr_cap;
6469 	struct ieee80211_sta_vht_cap vht_cap;
6470 
6471 	if (sband->band == NL80211_BAND_S1GHZ) {
6472 		conn->mode = IEEE80211_CONN_MODE_S1G;
6473 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
6474 		mlme_dbg(sdata, "operating as S1G STA\n");
6475 		return;
6476 	}
6477 
6478 	conn->mode = IEEE80211_CONN_MODE_LEGACY;
6479 	conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
6480 
6481 	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
6482 
6483 	if (req && req->flags & ASSOC_REQ_DISABLE_HT) {
6484 		mlme_link_id_dbg(sdata, link_id,
6485 				 "HT disabled by flag, limiting to legacy\n");
6486 		goto out;
6487 	}
6488 
6489 	if (!wmm_used) {
6490 		mlme_link_id_dbg(sdata, link_id,
6491 				 "WMM/QoS not supported, limiting to legacy\n");
6492 		goto out;
6493 	}
6494 
6495 	if (req) {
6496 		unsigned int i;
6497 
6498 		for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
6499 			if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
6500 			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
6501 			    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
6502 				netdev_info(sdata->dev,
6503 					    "WEP/TKIP use, limiting to legacy\n");
6504 				goto out;
6505 			}
6506 		}
6507 	}
6508 
6509 	if (!sta_ht_cap.ht_supported && !is_6ghz) {
6510 		mlme_link_id_dbg(sdata, link_id,
6511 				 "HT not supported (and not on 6 GHz), limiting to legacy\n");
6512 		goto out;
6513 	}
6514 
6515 	/* HT is fine */
6516 	conn->mode = IEEE80211_CONN_MODE_HT;
6517 	conn->bw_limit = sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
6518 		IEEE80211_CONN_BW_LIMIT_40 :
6519 		IEEE80211_CONN_BW_LIMIT_20;
6520 
6521 	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
6522 	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
6523 
6524 	if (req && req->flags & ASSOC_REQ_DISABLE_VHT) {
6525 		mlme_link_id_dbg(sdata, link_id,
6526 				 "VHT disabled by flag, limiting to HT\n");
6527 		goto out;
6528 	}
6529 
6530 	if (vht_cap.vht_supported && is_5ghz) {
6531 		bool have_80mhz = false;
6532 		unsigned int i;
6533 
6534 		if (conn->bw_limit == IEEE80211_CONN_BW_LIMIT_20) {
6535 			mlme_link_id_dbg(sdata, link_id,
6536 					 "no 40 MHz support on 5 GHz, limiting to HT\n");
6537 			goto out;
6538 		}
6539 
6540 		/* Allow VHT if at least one channel on the sband supports 80 MHz */
6541 		for (i = 0; i < sband->n_channels; i++) {
6542 			if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
6543 							IEEE80211_CHAN_NO_80MHZ))
6544 				continue;
6545 
6546 			have_80mhz = true;
6547 			break;
6548 		}
6549 
6550 		if (!have_80mhz) {
6551 			mlme_link_id_dbg(sdata, link_id,
6552 					 "no 80 MHz channel support on 5 GHz, limiting to HT\n");
6553 			goto out;
6554 		}
6555 	} else if (is_5ghz) { /* !vht_supported but on 5 GHz */
6556 		mlme_link_id_dbg(sdata, link_id,
6557 				 "no VHT support on 5 GHz, limiting to HT\n");
6558 		goto out;
6559 	}
6560 
6561 	/* VHT - if we have - is fine, including 80 MHz, check 160 below again */
6562 	if (sband->band != NL80211_BAND_2GHZ) {
6563 		conn->mode = IEEE80211_CONN_MODE_VHT;
6564 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_160;
6565 	}
6566 
6567 	if (is_5ghz &&
6568 	    !(vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
6569 			     IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
6570 			     IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))) {
6571 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_80;
6572 		mlme_link_id_dbg(sdata, link_id,
6573 				 "no VHT 160 MHz capability on 5 GHz, limiting to 80 MHz");
6574 	}
6575 
6576 	if (req && req->flags & ASSOC_REQ_DISABLE_HE) {
6577 		mlme_link_id_dbg(sdata, link_id,
6578 				 "HE disabled by flag, limiting to HT/VHT\n");
6579 		goto out;
6580 	}
6581 
6582 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
6583 	if (!he_cap) {
6584 		WARN_ON(is_6ghz);
6585 		mlme_link_id_dbg(sdata, link_id,
6586 				 "no HE support, limiting to HT/VHT\n");
6587 		goto out;
6588 	}
6589 
6590 	/* so we have HE */
6591 	conn->mode = IEEE80211_CONN_MODE_HE;
6592 
6593 	/* check bandwidth */
6594 	switch (sband->band) {
6595 	default:
6596 	case NL80211_BAND_2GHZ:
6597 		if (he_cap->he_cap_elem.phy_cap_info[0] &
6598 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
6599 			break;
6600 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
6601 		mlme_link_id_dbg(sdata, link_id,
6602 				 "no 40 MHz HE cap in 2.4 GHz, limiting to 20 MHz\n");
6603 		break;
6604 	case NL80211_BAND_5GHZ:
6605 		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
6606 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
6607 			conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
6608 			mlme_link_id_dbg(sdata, link_id,
6609 					 "no 40/80 MHz HE cap in 5 GHz, limiting to 20 MHz\n");
6610 			break;
6611 		}
6612 		if (!(he_cap->he_cap_elem.phy_cap_info[0] &
6613 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
6614 			conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
6615 					       conn->bw_limit,
6616 					       IEEE80211_CONN_BW_LIMIT_80);
6617 			mlme_link_id_dbg(sdata, link_id,
6618 					 "no 160 MHz HE cap in 5 GHz, limiting to 80 MHz\n");
6619 		}
6620 		break;
6621 	case NL80211_BAND_6GHZ:
6622 		if (he_cap->he_cap_elem.phy_cap_info[0] &
6623 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
6624 			break;
6625 		conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
6626 				       conn->bw_limit,
6627 				       IEEE80211_CONN_BW_LIMIT_80);
6628 		mlme_link_id_dbg(sdata, link_id,
6629 				 "no 160 MHz HE cap in 6 GHz, limiting to 80 MHz\n");
6630 		break;
6631 	}
6632 
6633 	if (req && req->flags & ASSOC_REQ_DISABLE_EHT) {
6634 		mlme_link_id_dbg(sdata, link_id,
6635 				 "EHT disabled by flag, limiting to HE\n");
6636 		goto out;
6637 	}
6638 
6639 	eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
6640 	if (!eht_cap) {
6641 		mlme_link_id_dbg(sdata, link_id,
6642 				 "no EHT support, limiting to HE\n");
6643 		goto out;
6644 	}
6645 	conn->mode = IEEE80211_CONN_MODE_EHT;
6646 
6647 	/* check bandwidth */
6648 	if (is_6ghz &&
6649 	    eht_cap->eht_cap_elem.phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
6650 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_320;
6651 	else if (is_6ghz)
6652 		mlme_link_id_dbg(sdata, link_id,
6653 				 "no EHT 320 MHz cap in 6 GHz, limiting to 160 MHz\n");
6654 
6655 	if (req && req->flags & ASSOC_REQ_DISABLE_UHR) {
6656 		mlme_link_id_dbg(sdata, link_id,
6657 				 "UHR disabled by flag, limiting to EHT\n");
6658 		goto out;
6659 	}
6660 
6661 	uhr_cap = ieee80211_get_uhr_iftype_cap_vif(sband, &sdata->vif);
6662 	if (!uhr_cap) {
6663 		mlme_link_id_dbg(sdata, link_id,
6664 				 "no UHR support, limiting to EHT\n");
6665 		goto out;
6666 	}
6667 	conn->mode = IEEE80211_CONN_MODE_UHR;
6668 
6669 out:
6670 	mlme_link_id_dbg(sdata, link_id,
6671 			 "determined local STA to be %s, BW limited to %d MHz\n",
6672 			 ieee80211_conn_mode_str(conn->mode),
6673 			 20 * (1 << conn->bw_limit));
6674 }
6675 
6676 static void
6677 ieee80211_determine_our_sta_mode_auth(struct ieee80211_sub_if_data *sdata,
6678 				      struct ieee80211_supported_band *sband,
6679 				      struct cfg80211_auth_request *req,
6680 				      bool wmm_used,
6681 				      struct ieee80211_conn_settings *conn)
6682 {
6683 	ieee80211_determine_our_sta_mode(sdata, sband, NULL, wmm_used,
6684 					 req->link_id > 0 ? req->link_id : 0,
6685 					 conn);
6686 }
6687 
6688 static void
6689 ieee80211_determine_our_sta_mode_assoc(struct ieee80211_sub_if_data *sdata,
6690 				       struct ieee80211_supported_band *sband,
6691 				       struct cfg80211_assoc_request *req,
6692 				       bool wmm_used, int link_id,
6693 				       struct ieee80211_conn_settings *conn)
6694 {
6695 	struct ieee80211_conn_settings tmp;
6696 
6697 	WARN_ON(!req);
6698 
6699 	ieee80211_determine_our_sta_mode(sdata, sband, req, wmm_used, link_id,
6700 					 &tmp);
6701 
6702 	conn->mode = min_t(enum ieee80211_conn_mode,
6703 			   conn->mode, tmp.mode);
6704 	conn->bw_limit = min_t(enum ieee80211_conn_bw_limit,
6705 			       conn->bw_limit, tmp.bw_limit);
6706 }
6707 
6708 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
6709 				  struct ieee80211_link_data *link,
6710 				  struct link_sta_info *link_sta,
6711 				  int link_id,
6712 				  struct cfg80211_bss *cbss, bool mlo,
6713 				  struct ieee80211_conn_settings *conn,
6714 				  unsigned long *userspace_selectors)
6715 {
6716 	struct ieee80211_local *local = sdata->local;
6717 	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
6718 	struct ieee80211_chan_req chanreq = {};
6719 	struct cfg80211_chan_def ap_chandef;
6720 	struct ieee802_11_elems *elems;
6721 	int ret;
6722 
6723 	lockdep_assert_wiphy(local->hw.wiphy);
6724 
6725 	rcu_read_lock();
6726 	elems = ieee80211_determine_chan_mode(sdata, conn, cbss,
6727 					      link_sta, link_id,
6728 					      &chanreq, &ap_chandef,
6729 					      userspace_selectors);
6730 
6731 	if (IS_ERR(elems)) {
6732 		rcu_read_unlock();
6733 		return PTR_ERR(elems);
6734 	}
6735 
6736 	if (mlo && !elems->ml_basic) {
6737 		sdata_info(sdata, "Rejecting MLO as it is not supported by AP\n");
6738 		rcu_read_unlock();
6739 		kfree(elems);
6740 		return -EINVAL;
6741 	}
6742 
6743 	if (link && is_6ghz && conn->mode >= IEEE80211_CONN_MODE_HE) {
6744 		const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
6745 
6746 		if (elems->pwr_constr_elem)
6747 			link->conf->pwr_reduction = *elems->pwr_constr_elem;
6748 
6749 		he_6ghz_oper = ieee80211_he_6ghz_oper(elems->he_operation);
6750 		if (he_6ghz_oper)
6751 			link->conf->power_type =
6752 				cfg80211_6ghz_power_type(he_6ghz_oper->control,
6753 							 cbss->channel->flags);
6754 		else
6755 			link_info(link,
6756 				  "HE 6 GHz operation missing (on %d MHz), expect issues\n",
6757 				  cbss->channel->center_freq);
6758 
6759 		link->conf->tpe = elems->tpe;
6760 		ieee80211_rearrange_tpe(&link->conf->tpe, &ap_chandef,
6761 					&chanreq.oper);
6762 	}
6763 	rcu_read_unlock();
6764 	/* the element data was RCU protected so no longer valid anyway */
6765 	kfree(elems);
6766 	elems = NULL;
6767 
6768 	if (!link)
6769 		return 0;
6770 
6771 	rcu_read_lock();
6772 	link->needed_rx_chains = min(ieee80211_max_rx_chains(link, cbss),
6773 				     local->rx_chains);
6774 	rcu_read_unlock();
6775 
6776 	/*
6777 	 * If this fails (possibly due to channel context sharing
6778 	 * on incompatible channels, e.g. 80+80 and 160 sharing the
6779 	 * same control channel) try to use a smaller bandwidth.
6780 	 */
6781 	ret = ieee80211_link_use_channel(link, &chanreq,
6782 					 IEEE80211_CHANCTX_SHARED);
6783 
6784 	/* don't downgrade for S1G channels, though. */
6785 	if (cfg80211_chandef_is_s1g(&chanreq.oper))
6786 		return ret;
6787 
6788 	while (ret && chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT) {
6789 		ieee80211_chanreq_downgrade(&chanreq, conn);
6790 
6791 		ret = ieee80211_link_use_channel(link, &chanreq,
6792 						 IEEE80211_CHANCTX_SHARED);
6793 	}
6794 
6795 	return ret;
6796 }
6797 
6798 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
6799 			       u8 *dtim_count, u8 *dtim_period)
6800 {
6801 	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
6802 	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
6803 					 ies->len);
6804 	const struct ieee80211_tim_ie *tim = NULL;
6805 	const struct ieee80211_bssid_index *idx;
6806 	bool valid = tim_ie && tim_ie[1] >= 2;
6807 
6808 	if (valid)
6809 		tim = (void *)(tim_ie + 2);
6810 
6811 	if (dtim_count)
6812 		*dtim_count = valid ? tim->dtim_count : 0;
6813 
6814 	if (dtim_period)
6815 		*dtim_period = valid ? tim->dtim_period : 0;
6816 
6817 	/* Check if value is overridden by non-transmitted profile */
6818 	if (!idx_ie || idx_ie[1] < 3)
6819 		return valid;
6820 
6821 	idx = (void *)(idx_ie + 2);
6822 
6823 	if (dtim_count)
6824 		*dtim_count = idx->dtim_count;
6825 
6826 	if (dtim_period)
6827 		*dtim_period = idx->dtim_period;
6828 
6829 	return true;
6830 }
6831 
6832 static u16 ieee80211_get_ttlm(u8 bm_size, u8 *data)
6833 {
6834 	if (bm_size == 1)
6835 		return *data;
6836 
6837 	return get_unaligned_le16(data);
6838 }
6839 
6840 static int
6841 ieee80211_parse_adv_t2l(struct ieee80211_sub_if_data *sdata,
6842 			const struct ieee80211_ttlm_elem *ttlm,
6843 			struct ieee80211_adv_ttlm_info *ttlm_info)
6844 {
6845 	/* The element size was already validated in
6846 	 * ieee80211_tid_to_link_map_size_ok()
6847 	 */
6848 	u8 control, link_map_presence, map_size, tid;
6849 	u8 *pos;
6850 
6851 	memset(ttlm_info, 0, sizeof(*ttlm_info));
6852 	pos = (void *)ttlm->optional;
6853 	control	= ttlm->control;
6854 
6855 	if ((control & IEEE80211_TTLM_CONTROL_DIRECTION) !=
6856 	    IEEE80211_TTLM_DIRECTION_BOTH) {
6857 		sdata_info(sdata, "Invalid advertised T2L map direction\n");
6858 		return -EINVAL;
6859 	}
6860 
6861 	if (!(control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP)) {
6862 		link_map_presence = *pos;
6863 		pos++;
6864 	}
6865 
6866 	if (control & IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT) {
6867 		ttlm_info->switch_time = get_unaligned_le16(pos);
6868 
6869 		/* Since ttlm_info->switch_time == 0 means no switch time, bump
6870 		 * it by 1.
6871 		 */
6872 		if (!ttlm_info->switch_time)
6873 			ttlm_info->switch_time = 1;
6874 
6875 		pos += 2;
6876 	}
6877 
6878 	if (control & IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT) {
6879 		ttlm_info->duration = pos[0] | pos[1] << 8 | pos[2] << 16;
6880 		pos += 3;
6881 	}
6882 
6883 	if (control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) {
6884 		ttlm_info->map = 0xffff;
6885 		return 0;
6886 	}
6887 
6888 	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
6889 		map_size = 1;
6890 	else
6891 		map_size = 2;
6892 
6893 	/* According to Draft P802.11be_D3.0 clause 35.3.7.1.7, an AP MLD shall
6894 	 * not advertise a TID-to-link mapping that does not map all TIDs to the
6895 	 * same link set, reject frame if not all links have mapping
6896 	 */
6897 	if (link_map_presence != 0xff) {
6898 		sdata_info(sdata,
6899 			   "Invalid advertised T2L mapping presence indicator\n");
6900 		return -EINVAL;
6901 	}
6902 
6903 	ttlm_info->map = ieee80211_get_ttlm(map_size, pos);
6904 	if (!ttlm_info->map) {
6905 		sdata_info(sdata,
6906 			   "Invalid advertised T2L map for TID 0\n");
6907 		return -EINVAL;
6908 	}
6909 
6910 	pos += map_size;
6911 
6912 	for (tid = 1; tid < 8; tid++) {
6913 		u16 map = ieee80211_get_ttlm(map_size, pos);
6914 
6915 		if (map != ttlm_info->map) {
6916 			sdata_info(sdata, "Invalid advertised T2L map for tid %d\n",
6917 				   tid);
6918 			return -EINVAL;
6919 		}
6920 
6921 		pos += map_size;
6922 	}
6923 	return 0;
6924 }
6925 
6926 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
6927 				    struct ieee80211_mgmt *mgmt,
6928 				    struct ieee802_11_elems *elems,
6929 				    const u8 *elem_start, unsigned int elem_len)
6930 {
6931 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6932 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
6933 	struct ieee80211_local *local = sdata->local;
6934 	unsigned int link_id;
6935 	struct sta_info *sta;
6936 	u64 changed[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6937 	u16 valid_links = 0, dormant_links = 0;
6938 	int err;
6939 
6940 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6941 	/*
6942 	 * station info was already allocated and inserted before
6943 	 * the association and should be available to us
6944 	 */
6945 	sta = sta_info_get(sdata, assoc_data->ap_addr);
6946 	if (WARN_ON(!sta))
6947 		goto out_err;
6948 
6949 	sta->sta.spp_amsdu = assoc_data->spp_amsdu;
6950 
6951 	if (ieee80211_vif_is_mld(&sdata->vif)) {
6952 		if (!elems->ml_basic)
6953 			goto out_err;
6954 
6955 		sta->sta.ext_mld_capa_ops =
6956 			ieee80211_mle_get_ext_mld_capa_op((const void *)elems->ml_basic);
6957 
6958 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
6959 			if (!assoc_data->link[link_id].bss)
6960 				continue;
6961 
6962 			valid_links |= BIT(link_id);
6963 
6964 			if (link_id != assoc_data->assoc_link_id) {
6965 				err = ieee80211_sta_allocate_link(sta, link_id);
6966 				if (err)
6967 					goto out_err;
6968 			}
6969 		}
6970 
6971 		/*
6972 		 * We do not support setting a negotiated TTLM during
6973 		 * association. As such, we can assume that if there is a TTLM,
6974 		 * then it is the currently active advertised TTLM.
6975 		 * In that case, there must be exactly one TTLM that does not
6976 		 * have a switch time set. This mapping should also leave us
6977 		 * with at least one usable link.
6978 		 */
6979 		if (elems->ttlm_num > 1) {
6980 			sdata_info(sdata,
6981 				   "More than one advertised TTLM in association response\n");
6982 			goto out_err;
6983 		} else if (elems->ttlm_num == 1) {
6984 			if (ieee80211_parse_adv_t2l(sdata, elems->ttlm[0],
6985 						    &sdata->u.mgd.ttlm_info) ||
6986 			    sdata->u.mgd.ttlm_info.switch_time != 0 ||
6987 			    !(valid_links & sdata->u.mgd.ttlm_info.map)) {
6988 				sdata_info(sdata,
6989 					   "Invalid advertised TTLM in association response\n");
6990 				goto out_err;
6991 			}
6992 
6993 			sdata->u.mgd.ttlm_info.active = true;
6994 			dormant_links =
6995 				valid_links & ~sdata->u.mgd.ttlm_info.map;
6996 		}
6997 
6998 		ieee80211_vif_set_links(sdata, valid_links, dormant_links);
6999 	}
7000 
7001 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
7002 		struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
7003 		struct ieee80211_link_data *link;
7004 		struct link_sta_info *link_sta;
7005 
7006 		if (!cbss)
7007 			continue;
7008 
7009 		link = sdata_dereference(sdata->link[link_id], sdata);
7010 		if (WARN_ON(!link))
7011 			goto out_err;
7012 
7013 		if (ieee80211_vif_is_mld(&sdata->vif))
7014 			link_info(link,
7015 				  "local address %pM, AP link address %pM%s\n",
7016 				  link->conf->addr,
7017 				  assoc_data->link[link_id].bss->bssid,
7018 				  link_id == assoc_data->assoc_link_id ?
7019 					" (assoc)" : "");
7020 
7021 		link_sta = rcu_dereference_protected(sta->link[link_id],
7022 						     lockdep_is_held(&local->hw.wiphy->mtx));
7023 		if (WARN_ON(!link_sta))
7024 			goto out_err;
7025 
7026 		if (!link->u.mgd.have_beacon) {
7027 			const struct cfg80211_bss_ies *ies;
7028 
7029 			rcu_read_lock();
7030 			ies = rcu_dereference(cbss->beacon_ies);
7031 			if (ies)
7032 				link->u.mgd.have_beacon = true;
7033 			else
7034 				ies = rcu_dereference(cbss->ies);
7035 			ieee80211_get_dtim(ies,
7036 					   &link->conf->sync_dtim_count,
7037 					   &link->u.mgd.dtim_period);
7038 			link->conf->beacon_int = cbss->beacon_interval;
7039 			rcu_read_unlock();
7040 		}
7041 
7042 		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
7043 
7044 		if (link_id != assoc_data->assoc_link_id) {
7045 			link->u.mgd.conn = assoc_data->link[link_id].conn;
7046 
7047 			err = ieee80211_prep_channel(sdata, link, link_sta,
7048 						     link_id, cbss,
7049 						     true, &link->u.mgd.conn,
7050 						     sdata->u.mgd.userspace_selectors);
7051 			if (err) {
7052 				link_info(link, "prep_channel failed\n");
7053 				goto out_err;
7054 			}
7055 		}
7056 
7057 		err = ieee80211_mgd_setup_link_sta(link, sta, link_sta,
7058 						   assoc_data->link[link_id].bss);
7059 		if (err)
7060 			goto out_err;
7061 
7062 		if (!ieee80211_assoc_config_link(link, link_sta,
7063 						 assoc_data->link[link_id].bss,
7064 						 mgmt, elem_start, elem_len,
7065 						 &changed[link_id]))
7066 			goto out_err;
7067 
7068 		if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) {
7069 			valid_links &= ~BIT(link_id);
7070 			ieee80211_sta_remove_link(sta, link_id);
7071 			continue;
7072 		}
7073 
7074 		if (link_id != assoc_data->assoc_link_id) {
7075 			err = ieee80211_sta_activate_link(sta, link_id);
7076 			if (err)
7077 				goto out_err;
7078 		}
7079 	}
7080 
7081 	/* links might have changed due to rejected ones, set them again */
7082 	ieee80211_vif_set_links(sdata, valid_links, dormant_links);
7083 
7084 	rate_control_rate_init_all_links(sta);
7085 
7086 	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
7087 		set_sta_flag(sta, WLAN_STA_MFP);
7088 		sta->sta.mfp = true;
7089 	} else {
7090 		sta->sta.mfp = false;
7091 	}
7092 
7093 	ieee80211_sta_set_max_amsdu_subframes(sta, elems->ext_capab,
7094 					      elems->ext_capab_len);
7095 
7096 	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
7097 		       local->hw.queues >= IEEE80211_NUM_ACS;
7098 
7099 	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
7100 	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
7101 		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
7102 	if (err) {
7103 		sdata_info(sdata,
7104 			   "failed to move station %pM to desired state\n",
7105 			   sta->sta.addr);
7106 		WARN_ON(__sta_info_destroy(sta));
7107 		goto out_err;
7108 	}
7109 
7110 	if (sdata->wdev.use_4addr)
7111 		drv_sta_set_4addr(local, sdata, &sta->sta, true);
7112 
7113 	ieee80211_set_associated(sdata, assoc_data, changed);
7114 
7115 	/*
7116 	 * If we're using 4-addr mode, let the AP know that we're
7117 	 * doing so, so that it can create the STA VLAN on its side
7118 	 */
7119 	if (ifmgd->use_4addr)
7120 		ieee80211_send_4addr_nullfunc(local, sdata);
7121 
7122 	/*
7123 	 * Start timer to probe the connection to the AP now.
7124 	 * Also start the timer that will detect beacon loss.
7125 	 */
7126 	ieee80211_sta_reset_beacon_monitor(sdata);
7127 	ieee80211_sta_reset_conn_monitor(sdata);
7128 
7129 	ieee80211_send_uhr_omp_req_dbe(sdata, ~0, true);
7130 
7131 	return true;
7132 out_err:
7133 	eth_zero_addr(sdata->vif.cfg.ap_addr);
7134 	return false;
7135 }
7136 
7137 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
7138 					 struct ieee80211_mgmt *mgmt,
7139 					 size_t len)
7140 {
7141 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7142 	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
7143 	u16 capab_info, status_code, aid = 0;
7144 	struct ieee80211_elems_parse_params parse_params = {
7145 		.bss = NULL,
7146 		.link_id = -1,
7147 		.from_ap = true,
7148 		.type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE,
7149 	};
7150 	struct ieee802_11_elems *elems;
7151 	struct sta_info *sta;
7152 	int ac;
7153 	const u8 *elem_start;
7154 	unsigned int elem_len;
7155 	bool reassoc;
7156 	struct ieee80211_event event = {
7157 		.type = MLME_EVENT,
7158 		.u.mlme.data = ASSOC_EVENT,
7159 	};
7160 	struct ieee80211_prep_tx_info info = {};
7161 	struct cfg80211_rx_assoc_resp_data resp = {
7162 		.uapsd_queues = -1,
7163 	};
7164 	u8 ap_mld_addr[ETH_ALEN] __aligned(2);
7165 	unsigned int link_id;
7166 	u16 max_aid = IEEE80211_MAX_AID;
7167 
7168 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7169 
7170 	if (!assoc_data)
7171 		return;
7172 
7173 	info.link_id = assoc_data->assoc_link_id;
7174 
7175 	parse_params.mode =
7176 		assoc_data->link[assoc_data->assoc_link_id].conn.mode;
7177 
7178 	if (!ether_addr_equal(assoc_data->ap_addr, mgmt->bssid) ||
7179 	    !ether_addr_equal(assoc_data->ap_addr, mgmt->sa))
7180 		return;
7181 
7182 	/*
7183 	 * AssocResp and ReassocResp have identical structure, so process both
7184 	 * of them in this function.
7185 	 */
7186 
7187 	if (len < 24 + 6)
7188 		return;
7189 
7190 	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
7191 	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
7192 	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
7193 	if (assoc_data->s1g) {
7194 		elem_start = mgmt->u.s1g_assoc_resp.variable;
7195 		max_aid = IEEE80211_MAX_SUPPORTED_S1G_AID;
7196 	} else {
7197 		elem_start = mgmt->u.assoc_resp.variable;
7198 	}
7199 
7200 	/*
7201 	 * Note: this may not be perfect, AP might misbehave - if
7202 	 * anyone needs to rely on perfect complete notification
7203 	 * with the exact right subtype, then we need to track what
7204 	 * we actually transmitted.
7205 	 */
7206 	info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
7207 				 IEEE80211_STYPE_ASSOC_REQ;
7208 
7209 	if (assoc_data->fils_kek_len &&
7210 	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
7211 		return;
7212 
7213 	elem_len = len - (elem_start - (u8 *)mgmt);
7214 	parse_params.start = elem_start;
7215 	parse_params.len = elem_len;
7216 	elems = ieee802_11_parse_elems_full(&parse_params);
7217 	if (!elems)
7218 		goto notify_driver;
7219 
7220 	if (elems->aid_resp)
7221 		aid = le16_to_cpu(elems->aid_resp->aid);
7222 	else if (!assoc_data->s1g)
7223 		aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
7224 	else if (status_code == WLAN_STATUS_SUCCESS)
7225 		goto abandon_assoc;
7226 
7227 	/*
7228 	 * The 5 MSB of the AID field are reserved for a non-S1G STA. For
7229 	 * an S1G STA the 3 MSBs are reserved.
7230 	 * (802.11-2016 9.4.1.8 AID field).
7231 	 */
7232 	aid &= assoc_data->s1g ? 0x1fff : 0x7ff;
7233 
7234 	sdata_info(sdata,
7235 		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
7236 		   reassoc ? "Rea" : "A", assoc_data->ap_addr,
7237 		   capab_info, status_code, aid);
7238 
7239 	ifmgd->broken_ap = false;
7240 
7241 	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
7242 	    elems->timeout_int &&
7243 	    elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
7244 		u32 tu, ms;
7245 
7246 		cfg80211_assoc_comeback(sdata->dev, assoc_data->ap_addr,
7247 					le32_to_cpu(elems->timeout_int->value));
7248 
7249 		tu = le32_to_cpu(elems->timeout_int->value);
7250 		ms = tu * 1024 / 1000;
7251 		sdata_info(sdata,
7252 			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
7253 			   assoc_data->ap_addr, tu, ms);
7254 		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
7255 		assoc_data->timeout_started = true;
7256 		assoc_data->comeback = true;
7257 		if (ms > IEEE80211_ASSOC_TIMEOUT)
7258 			run_again(sdata, assoc_data->timeout);
7259 		goto notify_driver;
7260 	}
7261 
7262 	if (status_code != WLAN_STATUS_SUCCESS) {
7263 		sdata_info(sdata, "%pM denied association (code=%d)\n",
7264 			   assoc_data->ap_addr, status_code);
7265 		event.u.mlme.status = MLME_DENIED;
7266 		event.u.mlme.reason = status_code;
7267 		drv_event_callback(sdata->local, sdata, &event);
7268 	} else {
7269 		if (aid == 0 || aid > max_aid) {
7270 			sdata_info(sdata,
7271 				   "invalid AID value %d (out of range), turn off PS\n",
7272 				   aid);
7273 			aid = 0;
7274 			ifmgd->broken_ap = true;
7275 		}
7276 
7277 		if (ieee80211_vif_is_mld(&sdata->vif)) {
7278 			struct ieee80211_mle_basic_common_info *common;
7279 
7280 			if (!elems->ml_basic) {
7281 				sdata_info(sdata,
7282 					   "MLO association with %pM but no (basic) multi-link element in response!\n",
7283 					   assoc_data->ap_addr);
7284 				goto abandon_assoc;
7285 			}
7286 
7287 			common = (void *)elems->ml_basic->variable;
7288 
7289 			if (memcmp(assoc_data->ap_addr,
7290 				   common->mld_mac_addr, ETH_ALEN)) {
7291 				sdata_info(sdata,
7292 					   "AP MLD MAC address mismatch: got %pM expected %pM\n",
7293 					   common->mld_mac_addr,
7294 					   assoc_data->ap_addr);
7295 				goto abandon_assoc;
7296 			}
7297 
7298 			sdata->vif.cfg.eml_cap =
7299 				ieee80211_mle_get_eml_cap((const void *)elems->ml_basic);
7300 			sdata->vif.cfg.eml_med_sync_delay =
7301 				ieee80211_mle_get_eml_med_sync_delay((const void *)elems->ml_basic);
7302 			sdata->vif.cfg.mld_capa_op =
7303 				ieee80211_mle_get_mld_capa_op((const void *)elems->ml_basic);
7304 		}
7305 
7306 		sdata->vif.cfg.aid = aid;
7307 		sdata->vif.cfg.s1g = assoc_data->s1g;
7308 
7309 		if (!ieee80211_assoc_success(sdata, mgmt, elems,
7310 					     elem_start, elem_len)) {
7311 			/* oops -- internal error -- send timeout for now */
7312 			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
7313 			goto notify_driver;
7314 		}
7315 		event.u.mlme.status = MLME_SUCCESS;
7316 		drv_event_callback(sdata->local, sdata, &event);
7317 		sdata_info(sdata, "associated\n");
7318 
7319 		info.success = 1;
7320 	}
7321 
7322 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
7323 		struct ieee80211_link_data *link;
7324 
7325 		if (!assoc_data->link[link_id].bss)
7326 			continue;
7327 
7328 		resp.links[link_id].bss = assoc_data->link[link_id].bss;
7329 		ether_addr_copy(resp.links[link_id].addr,
7330 				assoc_data->link[link_id].addr);
7331 		resp.links[link_id].status = assoc_data->link[link_id].status;
7332 
7333 		link = sdata_dereference(sdata->link[link_id], sdata);
7334 		if (!link)
7335 			continue;
7336 
7337 		/* get uapsd queues configuration - same for all links */
7338 		resp.uapsd_queues = 0;
7339 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
7340 			if (link->tx_conf[ac].uapsd)
7341 				resp.uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
7342 	}
7343 
7344 	if (ieee80211_vif_is_mld(&sdata->vif)) {
7345 		ether_addr_copy(ap_mld_addr, sdata->vif.cfg.ap_addr);
7346 		resp.ap_mld_addr = ap_mld_addr;
7347 	}
7348 
7349 	/*
7350 	 * If epp_peer set, unprotected (Re)Association Request/Response frames
7351 	 * are dropped, which ensures that the (re)association exchange is
7352 	 * encrypted over the air.
7353 	 */
7354 	sta = sta_info_get_bss(sdata, sdata->vif.cfg.ap_addr);
7355 	resp.assoc_encrypted = sta && sta->sta.epp_peer;
7356 
7357 	ieee80211_destroy_assoc_data(sdata,
7358 				     status_code == WLAN_STATUS_SUCCESS ?
7359 					ASSOC_SUCCESS :
7360 					ASSOC_REJECTED);
7361 
7362 	resp.buf = (u8 *)mgmt;
7363 	resp.len = len;
7364 	resp.req_ies = ifmgd->assoc_req_ies;
7365 	resp.req_ies_len = ifmgd->assoc_req_ies_len;
7366 	cfg80211_rx_assoc_resp(sdata->dev, &resp);
7367 notify_driver:
7368 	drv_mgd_complete_tx(sdata->local, sdata, &info);
7369 	kfree(elems);
7370 	return;
7371 abandon_assoc:
7372 	ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
7373 	goto notify_driver;
7374 }
7375 
7376 static void ieee80211_rx_bss_info(struct ieee80211_link_data *link,
7377 				  struct ieee80211_mgmt *mgmt, size_t len,
7378 				  struct ieee80211_rx_status *rx_status)
7379 {
7380 	struct ieee80211_sub_if_data *sdata = link->sdata;
7381 	struct ieee80211_local *local = sdata->local;
7382 	struct ieee80211_bss *bss;
7383 	struct ieee80211_channel *channel;
7384 
7385 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7386 
7387 	channel = ieee80211_get_channel_khz(local->hw.wiphy,
7388 					ieee80211_rx_status_to_khz(rx_status));
7389 	if (!channel)
7390 		return;
7391 
7392 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
7393 	if (bss) {
7394 		link->conf->beacon_rate = bss->beacon_rate;
7395 		ieee80211_rx_bss_put(local, bss);
7396 	}
7397 }
7398 
7399 
7400 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_link_data *link,
7401 					 struct sk_buff *skb)
7402 {
7403 	struct ieee80211_sub_if_data *sdata = link->sdata;
7404 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
7405 	struct ieee80211_if_managed *ifmgd;
7406 	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
7407 	struct ieee80211_channel *channel;
7408 	size_t baselen, len = skb->len;
7409 
7410 	ifmgd = &sdata->u.mgd;
7411 
7412 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
7413 
7414 	/*
7415 	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
7416 	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
7417 	 * a Probe Response frame [..], the Address 1 field of the Probe
7418 	 * Response frame shall be set to the broadcast address [..]"
7419 	 * So, on 6GHz band we should also accept broadcast responses.
7420 	 */
7421 	channel = ieee80211_get_channel_khz(sdata->local->hw.wiphy,
7422 					    ieee80211_rx_status_to_khz(rx_status));
7423 	if (!channel)
7424 		return;
7425 
7426 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
7427 	    (channel->band != NL80211_BAND_6GHZ ||
7428 	     !is_broadcast_ether_addr(mgmt->da)))
7429 		return; /* ignore ProbeResp to foreign address */
7430 
7431 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
7432 	if (baselen > len)
7433 		return;
7434 
7435 	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
7436 
7437 	if (ifmgd->associated &&
7438 	    ether_addr_equal(mgmt->bssid, link->u.mgd.bssid))
7439 		ieee80211_reset_ap_probe(sdata);
7440 }
7441 
7442 /*
7443  * This is the canonical list of information elements we care about,
7444  * the filter code also gives us all changes to the Microsoft OUI
7445  * (00:50:F2) vendor IE which is used for WMM which we need to track,
7446  * as well as the DTPC IE (part of the Cisco OUI) used for signaling
7447  * changes to requested client power.
7448  *
7449  * We implement beacon filtering in software since that means we can
7450  * avoid processing the frame here and in cfg80211, and userspace
7451  * will not be able to tell whether the hardware supports it or not.
7452  *
7453  * XXX: This list needs to be dynamic -- userspace needs to be able to
7454  *	add items it requires. It also needs to be able to tell us to
7455  *	look out for other vendor IEs.
7456  */
7457 static const u64 care_about_ies =
7458 	(1ULL << WLAN_EID_COUNTRY) |
7459 	(1ULL << WLAN_EID_ERP_INFO) |
7460 	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
7461 	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
7462 	(1ULL << WLAN_EID_HT_CAPABILITY) |
7463 	(1ULL << WLAN_EID_HT_OPERATION) |
7464 	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
7465 
7466 static void ieee80211_handle_beacon_sig(struct ieee80211_link_data *link,
7467 					struct ieee80211_if_managed *ifmgd,
7468 					struct ieee80211_bss_conf *bss_conf,
7469 					struct ieee80211_local *local,
7470 					struct ieee80211_rx_status *rx_status)
7471 {
7472 	struct ieee80211_sub_if_data *sdata = link->sdata;
7473 
7474 	/* Track average RSSI from the Beacon frames of the current AP */
7475 
7476 	if (!link->u.mgd.tracking_signal_avg) {
7477 		link->u.mgd.tracking_signal_avg = true;
7478 		ewma_beacon_signal_init(&link->u.mgd.ave_beacon_signal);
7479 		link->u.mgd.last_cqm_event_signal = 0;
7480 		link->u.mgd.count_beacon_signal = 1;
7481 		link->u.mgd.last_ave_beacon_signal = 0;
7482 	} else {
7483 		link->u.mgd.count_beacon_signal++;
7484 	}
7485 
7486 	ewma_beacon_signal_add(&link->u.mgd.ave_beacon_signal,
7487 			       -rx_status->signal);
7488 
7489 	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
7490 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
7491 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
7492 		int last_sig = link->u.mgd.last_ave_beacon_signal;
7493 		struct ieee80211_event event = {
7494 			.type = RSSI_EVENT,
7495 		};
7496 
7497 		/*
7498 		 * if signal crosses either of the boundaries, invoke callback
7499 		 * with appropriate parameters
7500 		 */
7501 		if (sig > ifmgd->rssi_max_thold &&
7502 		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
7503 			link->u.mgd.last_ave_beacon_signal = sig;
7504 			event.u.rssi.data = RSSI_EVENT_HIGH;
7505 			drv_event_callback(local, sdata, &event);
7506 		} else if (sig < ifmgd->rssi_min_thold &&
7507 			   (last_sig >= ifmgd->rssi_max_thold ||
7508 			   last_sig == 0)) {
7509 			link->u.mgd.last_ave_beacon_signal = sig;
7510 			event.u.rssi.data = RSSI_EVENT_LOW;
7511 			drv_event_callback(local, sdata, &event);
7512 		}
7513 	}
7514 
7515 	if (bss_conf->cqm_rssi_thold &&
7516 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
7517 	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
7518 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
7519 		int last_event = link->u.mgd.last_cqm_event_signal;
7520 		int thold = bss_conf->cqm_rssi_thold;
7521 		int hyst = bss_conf->cqm_rssi_hyst;
7522 
7523 		if (sig < thold &&
7524 		    (last_event == 0 || sig < last_event - hyst)) {
7525 			link->u.mgd.last_cqm_event_signal = sig;
7526 			ieee80211_cqm_rssi_notify(
7527 				&sdata->vif,
7528 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
7529 				sig, GFP_KERNEL);
7530 		} else if (sig > thold &&
7531 			   (last_event == 0 || sig > last_event + hyst)) {
7532 			link->u.mgd.last_cqm_event_signal = sig;
7533 			ieee80211_cqm_rssi_notify(
7534 				&sdata->vif,
7535 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
7536 				sig, GFP_KERNEL);
7537 		}
7538 	}
7539 
7540 	if (bss_conf->cqm_rssi_low &&
7541 	    link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
7542 		int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
7543 		int last_event = link->u.mgd.last_cqm_event_signal;
7544 		int low = bss_conf->cqm_rssi_low;
7545 		int high = bss_conf->cqm_rssi_high;
7546 
7547 		if (sig < low &&
7548 		    (last_event == 0 || last_event >= low)) {
7549 			link->u.mgd.last_cqm_event_signal = sig;
7550 			ieee80211_cqm_rssi_notify(
7551 				&sdata->vif,
7552 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
7553 				sig, GFP_KERNEL);
7554 		} else if (sig > high &&
7555 			   (last_event == 0 || last_event <= high)) {
7556 			link->u.mgd.last_cqm_event_signal = sig;
7557 			ieee80211_cqm_rssi_notify(
7558 				&sdata->vif,
7559 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
7560 				sig, GFP_KERNEL);
7561 		}
7562 	}
7563 }
7564 
7565 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
7566 				    struct cfg80211_bss *bss)
7567 {
7568 	if (ether_addr_equal(tx_bssid, bss->bssid))
7569 		return true;
7570 	if (!bss->transmitted_bss)
7571 		return false;
7572 	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
7573 }
7574 
7575 static void ieee80211_ml_reconf_work(struct wiphy *wiphy,
7576 				     struct wiphy_work *work)
7577 {
7578 	struct ieee80211_sub_if_data *sdata =
7579 		container_of(work, struct ieee80211_sub_if_data,
7580 			     u.mgd.ml_reconf_work.work);
7581 	u16 new_valid_links, new_active_links, new_dormant_links;
7582 	int ret;
7583 
7584 	if (!sdata->u.mgd.removed_links)
7585 		return;
7586 
7587 	sdata_info(sdata,
7588 		   "MLO Reconfiguration: work: valid=0x%x, removed=0x%x\n",
7589 		   sdata->vif.valid_links, sdata->u.mgd.removed_links);
7590 
7591 	new_valid_links = sdata->vif.valid_links & ~sdata->u.mgd.removed_links;
7592 	if (new_valid_links == sdata->vif.valid_links)
7593 		return;
7594 
7595 	if (!new_valid_links ||
7596 	    !(new_valid_links & ~sdata->vif.dormant_links)) {
7597 		sdata_info(sdata, "No valid links after reconfiguration\n");
7598 		ret = -EINVAL;
7599 		goto out;
7600 	}
7601 
7602 	new_active_links = sdata->vif.active_links & ~sdata->u.mgd.removed_links;
7603 	if (new_active_links != sdata->vif.active_links) {
7604 		if (!new_active_links)
7605 			new_active_links =
7606 				BIT(ffs(new_valid_links &
7607 					~sdata->vif.dormant_links) - 1);
7608 
7609 		ret = ieee80211_set_active_links(&sdata->vif, new_active_links);
7610 		if (ret) {
7611 			sdata_info(sdata,
7612 				   "Failed setting active links\n");
7613 			goto out;
7614 		}
7615 	}
7616 
7617 	new_dormant_links = sdata->vif.dormant_links & ~sdata->u.mgd.removed_links;
7618 
7619 	ret = ieee80211_vif_set_links(sdata, new_valid_links,
7620 				      new_dormant_links);
7621 	if (ret)
7622 		sdata_info(sdata, "Failed setting valid links\n");
7623 
7624 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
7625 
7626 out:
7627 	if (!ret)
7628 		cfg80211_links_removed(sdata->dev, sdata->u.mgd.removed_links);
7629 	else
7630 		__ieee80211_disconnect(sdata);
7631 
7632 	sdata->u.mgd.removed_links = 0;
7633 }
7634 
7635 static void ieee80211_ml_reconfiguration(struct ieee80211_sub_if_data *sdata,
7636 					 struct ieee802_11_elems *elems)
7637 {
7638 	const struct element *sub;
7639 	unsigned long removed_links = 0;
7640 	u16 link_removal_timeout[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7641 	u8 link_id;
7642 	u32 delay;
7643 
7644 	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_reconf)
7645 		return;
7646 
7647 	/* Directly parse the sub elements as the common information doesn't
7648 	 * hold any useful information.
7649 	 */
7650 	for_each_mle_subelement(sub, (const u8 *)elems->ml_reconf,
7651 				elems->ml_reconf_len) {
7652 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
7653 		u8 *pos = prof->variable;
7654 		u16 control;
7655 
7656 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
7657 			continue;
7658 
7659 		if (!ieee80211_mle_reconf_sta_prof_size_ok(sub->data,
7660 							   sub->datalen))
7661 			return;
7662 
7663 		control = le16_to_cpu(prof->control);
7664 		link_id = control & IEEE80211_MLE_STA_RECONF_CONTROL_LINK_ID;
7665 
7666 		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7667 			continue;
7668 
7669 		removed_links |= BIT(link_id);
7670 
7671 		/* the MAC address should not be included, but handle it */
7672 		if (control &
7673 		    IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT)
7674 			pos += 6;
7675 
7676 		/* According to Draft P802.11be_D3.0, the control should
7677 		 * include the AP Removal Timer present. If the AP Removal Timer
7678 		 * is not present assume immediate removal.
7679 		 */
7680 		if (control &
7681 		    IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT)
7682 			link_removal_timeout[link_id] = get_unaligned_le16(pos);
7683 	}
7684 
7685 	removed_links &= sdata->vif.valid_links;
7686 	if (!removed_links) {
7687 		/* In case the removal was cancelled, abort it */
7688 		if (sdata->u.mgd.removed_links) {
7689 			sdata->u.mgd.removed_links = 0;
7690 			wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
7691 						  &sdata->u.mgd.ml_reconf_work);
7692 		}
7693 		return;
7694 	}
7695 
7696 	delay = 0;
7697 	for_each_set_bit(link_id, &removed_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7698 		struct ieee80211_bss_conf *link_conf =
7699 			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
7700 		u32 link_delay;
7701 
7702 		if (!link_conf) {
7703 			removed_links &= ~BIT(link_id);
7704 			continue;
7705 		}
7706 
7707 		if (link_removal_timeout[link_id] < 1)
7708 			link_delay = 0;
7709 		else
7710 			link_delay = link_conf->beacon_int *
7711 				(link_removal_timeout[link_id] - 1);
7712 
7713 		if (!delay)
7714 			delay = link_delay;
7715 		else
7716 			delay = min(delay, link_delay);
7717 	}
7718 
7719 	sdata->u.mgd.removed_links = removed_links;
7720 	wiphy_hrtimer_work_queue(sdata->local->hw.wiphy,
7721 				 &sdata->u.mgd.ml_reconf_work,
7722 				 us_to_ktime(ieee80211_tu_to_usec(delay)));
7723 }
7724 
7725 static int ieee80211_ttlm_set_links(struct ieee80211_sub_if_data *sdata,
7726 				    u16 active_links, u16 dormant_links,
7727 				    u16 suspended_links)
7728 {
7729 	u64 changed = 0;
7730 	int ret;
7731 
7732 	if (!active_links) {
7733 		ret = -EINVAL;
7734 		goto out;
7735 	}
7736 
7737 	/* If there is an active negotiated TTLM, it should be discarded by
7738 	 * the new negotiated/advertised TTLM.
7739 	 */
7740 	if (sdata->vif.neg_ttlm.valid) {
7741 		memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
7742 		sdata->vif.suspended_links = 0;
7743 		changed = BSS_CHANGED_MLD_TTLM;
7744 	}
7745 
7746 	if (sdata->vif.active_links != active_links) {
7747 		/* usable links are affected when active_links are changed,
7748 		 * so notify the driver about the status change
7749 		 */
7750 		changed |= BSS_CHANGED_MLD_VALID_LINKS;
7751 		active_links &= sdata->vif.active_links;
7752 		if (!active_links)
7753 			active_links =
7754 				BIT(__ffs(sdata->vif.valid_links &
7755 				    ~dormant_links));
7756 		ret = ieee80211_set_active_links(&sdata->vif, active_links);
7757 		if (ret) {
7758 			sdata_info(sdata, "Failed to set TTLM active links\n");
7759 			goto out;
7760 		}
7761 	}
7762 
7763 	ret = ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
7764 				      dormant_links);
7765 	if (ret) {
7766 		sdata_info(sdata, "Failed to set TTLM dormant links\n");
7767 		goto out;
7768 	}
7769 
7770 	sdata->vif.suspended_links = suspended_links;
7771 	if (sdata->vif.suspended_links)
7772 		changed |= BSS_CHANGED_MLD_TTLM;
7773 
7774 	ieee80211_vif_cfg_change_notify(sdata, changed);
7775 
7776 out:
7777 	if (ret)
7778 		ieee80211_disconnect(&sdata->vif, false);
7779 
7780 	return ret;
7781 }
7782 
7783 static void ieee80211_tid_to_link_map_work(struct wiphy *wiphy,
7784 					   struct wiphy_work *work)
7785 {
7786 	u16 new_active_links, new_dormant_links;
7787 	struct ieee80211_sub_if_data *sdata =
7788 		container_of(work, struct ieee80211_sub_if_data,
7789 			     u.mgd.ttlm_work.work);
7790 
7791 	new_active_links = sdata->u.mgd.ttlm_info.map &
7792 			   sdata->vif.valid_links;
7793 	new_dormant_links = ~sdata->u.mgd.ttlm_info.map &
7794 			    sdata->vif.valid_links;
7795 
7796 	ieee80211_vif_set_links(sdata, sdata->vif.valid_links, 0);
7797 	if (ieee80211_ttlm_set_links(sdata, new_active_links, new_dormant_links,
7798 				     0))
7799 		return;
7800 
7801 	sdata->u.mgd.ttlm_info.active = true;
7802 	sdata->u.mgd.ttlm_info.switch_time = 0;
7803 }
7804 
7805 static void ieee80211_process_adv_ttlm(struct ieee80211_sub_if_data *sdata,
7806 					  struct ieee802_11_elems *elems,
7807 					  u64 beacon_ts)
7808 {
7809 	u8 i;
7810 	int ret;
7811 
7812 	if (!ieee80211_vif_is_mld(&sdata->vif))
7813 		return;
7814 
7815 	if (!elems->ttlm_num) {
7816 		if (sdata->u.mgd.ttlm_info.switch_time) {
7817 			/* if a planned TID-to-link mapping was cancelled -
7818 			 * abort it
7819 			 */
7820 			wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
7821 						  &sdata->u.mgd.ttlm_work);
7822 		} else if (sdata->u.mgd.ttlm_info.active) {
7823 			/* if no TID-to-link element, set to default mapping in
7824 			 * which all TIDs are mapped to all setup links
7825 			 */
7826 			ret = ieee80211_vif_set_links(sdata,
7827 						      sdata->vif.valid_links,
7828 						      0);
7829 			if (ret) {
7830 				sdata_info(sdata, "Failed setting valid/dormant links\n");
7831 				return;
7832 			}
7833 			ieee80211_vif_cfg_change_notify(sdata,
7834 							BSS_CHANGED_MLD_VALID_LINKS);
7835 		}
7836 		memset(&sdata->u.mgd.ttlm_info, 0,
7837 		       sizeof(sdata->u.mgd.ttlm_info));
7838 		return;
7839 	}
7840 
7841 	for (i = 0; i < elems->ttlm_num; i++) {
7842 		struct ieee80211_adv_ttlm_info ttlm_info;
7843 		u32 res;
7844 
7845 		res = ieee80211_parse_adv_t2l(sdata, elems->ttlm[i],
7846 					      &ttlm_info);
7847 
7848 		if (res) {
7849 			__ieee80211_disconnect(sdata);
7850 			return;
7851 		}
7852 
7853 		if (ttlm_info.switch_time) {
7854 			u16 beacon_ts_tu, st_tu, delay;
7855 			u64 delay_usec;
7856 			u64 mask;
7857 
7858 			/* The t2l map switch time is indicated with a partial
7859 			 * TSF value (bits 10 to 25), get the partial beacon TS
7860 			 * as well, and calc the delay to the start time.
7861 			 */
7862 			mask = GENMASK_ULL(25, 10);
7863 			beacon_ts_tu = (beacon_ts & mask) >> 10;
7864 			st_tu = ttlm_info.switch_time;
7865 			delay = st_tu - beacon_ts_tu;
7866 
7867 			/*
7868 			 * If the switch time is far in the future, then it
7869 			 * could also be the previous switch still being
7870 			 * announced.
7871 			 * We can simply ignore it for now, if it is a future
7872 			 * switch the AP will continue to announce it anyway.
7873 			 */
7874 			if (delay > IEEE80211_ADV_TTLM_ST_UNDERFLOW)
7875 				return;
7876 
7877 			delay_usec = ieee80211_tu_to_usec(delay);
7878 
7879 			/* Link switching can take time, so schedule it
7880 			 * 100ms before to be ready on time
7881 			 */
7882 			if (delay_usec > IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS)
7883 				delay_usec -=
7884 					IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS;
7885 			else
7886 				delay_usec = 0;
7887 
7888 			sdata->u.mgd.ttlm_info = ttlm_info;
7889 			wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
7890 						  &sdata->u.mgd.ttlm_work);
7891 			wiphy_hrtimer_work_queue(sdata->local->hw.wiphy,
7892 						 &sdata->u.mgd.ttlm_work,
7893 						 us_to_ktime(delay_usec));
7894 			return;
7895 		}
7896 	}
7897 }
7898 
7899 static void
7900 ieee80211_mgd_check_cross_link_csa(struct ieee80211_sub_if_data *sdata,
7901 				   int reporting_link_id,
7902 				   struct ieee802_11_elems *elems)
7903 {
7904 	const struct element *sta_profiles[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7905 	ssize_t sta_profiles_len[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7906 	const struct element *sub;
7907 	const u8 *subelems;
7908 	size_t subelems_len;
7909 	u8 common_size;
7910 	int link_id;
7911 
7912 	if (!ieee80211_mle_size_ok((u8 *)elems->ml_basic, elems->ml_basic_len))
7913 		return;
7914 
7915 	common_size = ieee80211_mle_common_size((u8 *)elems->ml_basic);
7916 	subelems = (u8 *)elems->ml_basic + common_size;
7917 	subelems_len = elems->ml_basic_len - common_size;
7918 
7919 	for_each_element_id(sub, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
7920 			    subelems, subelems_len) {
7921 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
7922 		struct ieee80211_link_data *link;
7923 		ssize_t len;
7924 
7925 		if (!ieee80211_mle_basic_sta_prof_size_ok(sub->data,
7926 							  sub->datalen))
7927 			continue;
7928 
7929 		link_id = le16_get_bits(prof->control,
7930 					IEEE80211_MLE_STA_CONTROL_LINK_ID);
7931 		/* need a valid link ID, but also not our own, both AP bugs */
7932 		if (link_id == reporting_link_id ||
7933 		    link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7934 			continue;
7935 
7936 		link = sdata_dereference(sdata->link[link_id], sdata);
7937 		if (!link)
7938 			continue;
7939 
7940 		len = cfg80211_defragment_element(sub, subelems, subelems_len,
7941 						  NULL, 0,
7942 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
7943 		if (WARN_ON(len < 0))
7944 			continue;
7945 
7946 		sta_profiles[link_id] = sub;
7947 		sta_profiles_len[link_id] = len;
7948 	}
7949 
7950 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
7951 		struct ieee80211_mle_per_sta_profile *prof;
7952 		struct ieee802_11_elems *prof_elems;
7953 		struct ieee80211_link_data *link;
7954 		ssize_t len;
7955 
7956 		if (link_id == reporting_link_id)
7957 			continue;
7958 
7959 		link = sdata_dereference(sdata->link[link_id], sdata);
7960 		if (!link)
7961 			continue;
7962 
7963 		if (!sta_profiles[link_id]) {
7964 			prof_elems = NULL;
7965 			goto handle;
7966 		}
7967 
7968 		/* we can defragment in-place, won't use the buffer again */
7969 		len = cfg80211_defragment_element(sta_profiles[link_id],
7970 						  subelems, subelems_len,
7971 						  (void *)sta_profiles[link_id],
7972 						  sta_profiles_len[link_id],
7973 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
7974 		if (WARN_ON(len != sta_profiles_len[link_id]))
7975 			continue;
7976 
7977 		prof = (void *)sta_profiles[link_id];
7978 		prof_elems = ieee802_11_parse_elems(prof->variable +
7979 						    (prof->sta_info_len - 1),
7980 						    len -
7981 						    (prof->sta_info_len - 1),
7982 						    IEEE80211_FTYPE_MGMT |
7983 						    IEEE80211_STYPE_BEACON,
7984 						    NULL);
7985 
7986 		/* memory allocation failed - let's hope that's transient */
7987 		if (!prof_elems)
7988 			continue;
7989 
7990 handle:
7991 		/*
7992 		 * FIXME: the timings here are obviously incorrect,
7993 		 * but only older Intel drivers seem to care, and
7994 		 * those don't have MLO. If you really need this,
7995 		 * the problem is having to calculate it with the
7996 		 * TSF offset etc. The device_timestamp is still
7997 		 * correct, of course.
7998 		 */
7999 		ieee80211_sta_process_chanswitch(link, 0, 0, elems, prof_elems,
8000 						 IEEE80211_CSA_SOURCE_OTHER_LINK);
8001 		kfree(prof_elems);
8002 	}
8003 }
8004 
8005 static bool ieee80211_mgd_ssid_mismatch(struct ieee80211_sub_if_data *sdata,
8006 					const struct ieee802_11_elems *elems)
8007 {
8008 	struct ieee80211_vif_cfg *cfg = &sdata->vif.cfg;
8009 	static u8 zero_ssid[IEEE80211_MAX_SSID_LEN];
8010 
8011 	if (!elems->ssid)
8012 		return false;
8013 
8014 	/* hidden SSID: zero length */
8015 	if (elems->ssid_len == 0)
8016 		return false;
8017 
8018 	if (elems->ssid_len != cfg->ssid_len)
8019 		return true;
8020 
8021 	/* hidden SSID: zeroed out */
8022 	if (!memcmp(elems->ssid, zero_ssid, elems->ssid_len))
8023 		return false;
8024 
8025 	return memcmp(elems->ssid, cfg->ssid, cfg->ssid_len);
8026 }
8027 
8028 static bool
8029 ieee80211_rx_beacon_freq_valid(struct ieee80211_local *local,
8030 			       struct ieee80211_mgmt *mgmt,
8031 			       struct ieee80211_rx_status *rx_status,
8032 			       struct ieee80211_chanctx_conf *chanctx)
8033 {
8034 	u32 pri_2mhz_khz;
8035 	struct ieee80211_channel *s1g_sibling_1mhz;
8036 	u32 pri_khz = ieee80211_channel_to_khz(chanctx->def.chan);
8037 	u32 rx_khz = ieee80211_rx_status_to_khz(rx_status);
8038 
8039 	if (rx_khz == pri_khz)
8040 		return true;
8041 
8042 	if (!chanctx->def.s1g_primary_2mhz)
8043 		return false;
8044 
8045 	/*
8046 	 * If we have an S1G interface with a 2MHz primary, beacons are
8047 	 * sent on the center frequency of the 2MHz primary. Find the sibling
8048 	 * 1MHz channel and calculate the 2MHz primary center frequency.
8049 	 */
8050 	s1g_sibling_1mhz = cfg80211_s1g_get_primary_sibling(local->hw.wiphy,
8051 							    &chanctx->def);
8052 	if (!s1g_sibling_1mhz)
8053 		return false;
8054 
8055 	pri_2mhz_khz =
8056 		(pri_khz + ieee80211_channel_to_khz(s1g_sibling_1mhz)) / 2;
8057 	return rx_khz == pri_2mhz_khz;
8058 }
8059 
8060 static void ieee80211_rx_mgmt_beacon(struct ieee80211_link_data *link,
8061 				     struct ieee80211_hdr *hdr, size_t len,
8062 				     struct ieee80211_rx_status *rx_status)
8063 {
8064 	struct ieee80211_sub_if_data *sdata = link->sdata;
8065 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8066 	struct ieee80211_bss_conf *bss_conf = link->conf;
8067 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
8068 	struct ieee80211_mgmt *mgmt = (void *) hdr;
8069 	struct ieee80211_ext *ext = NULL;
8070 	size_t baselen;
8071 	struct ieee802_11_elems *elems;
8072 	struct ieee80211_local *local = sdata->local;
8073 	struct ieee80211_chanctx_conf *chanctx_conf;
8074 	struct ieee80211_supported_band *sband;
8075 	struct ieee80211_channel *chan;
8076 	struct link_sta_info *link_sta;
8077 	struct sta_info *sta;
8078 	u64 changed = 0;
8079 	u32 ncrc = 0;
8080 	u8 *bssid, *variable = mgmt->u.beacon.variable;
8081 	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
8082 	struct ieee80211_elems_parse_params parse_params = {
8083 		.mode = link->u.mgd.conn.mode,
8084 		.link_id = -1,
8085 		.from_ap = true,
8086 		.type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE,
8087 	};
8088 
8089 	lockdep_assert_wiphy(local->hw.wiphy);
8090 
8091 	/* Process beacon from the current BSS */
8092 	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
8093 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
8094 		ext = (void *)mgmt;
8095 		variable = ext->u.s1g_beacon.variable +
8096 			   ieee80211_s1g_optional_len(ext->frame_control);
8097 	}
8098 
8099 	baselen = (u8 *) variable - (u8 *) mgmt;
8100 	if (baselen > len)
8101 		return;
8102 
8103 	parse_params.start = variable;
8104 	parse_params.len = len - baselen;
8105 
8106 	rcu_read_lock();
8107 	chanctx_conf = rcu_dereference(bss_conf->chanctx_conf);
8108 	if (!chanctx_conf) {
8109 		rcu_read_unlock();
8110 		return;
8111 	}
8112 
8113 	if (!ieee80211_rx_beacon_freq_valid(local, mgmt, rx_status,
8114 					    chanctx_conf)) {
8115 		rcu_read_unlock();
8116 		return;
8117 	}
8118 	chan = chanctx_conf->def.chan;
8119 	rcu_read_unlock();
8120 
8121 	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
8122 	    !WARN_ON(ieee80211_vif_is_mld(&sdata->vif)) &&
8123 	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->link[0].bss)) {
8124 		parse_params.bss = ifmgd->assoc_data->link[0].bss;
8125 		elems = ieee802_11_parse_elems_full(&parse_params);
8126 		if (!elems)
8127 			return;
8128 
8129 		ieee80211_rx_bss_info(link, mgmt, len, rx_status);
8130 
8131 		if (elems->dtim_period)
8132 			link->u.mgd.dtim_period = elems->dtim_period;
8133 		link->u.mgd.have_beacon = true;
8134 		ifmgd->assoc_data->need_beacon = false;
8135 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
8136 		    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
8137 			bss_conf->sync_tsf =
8138 				le64_to_cpu(mgmt->u.beacon.timestamp);
8139 			bss_conf->sync_device_ts =
8140 				rx_status->device_timestamp;
8141 			bss_conf->sync_dtim_count = elems->dtim_count;
8142 		}
8143 
8144 		if (elems->mbssid_config_ie)
8145 			bss_conf->profile_periodicity =
8146 				elems->mbssid_config_ie->profile_periodicity;
8147 		else
8148 			bss_conf->profile_periodicity = 0;
8149 
8150 		if (elems->ext_capab_len >= 11 &&
8151 		    (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
8152 			bss_conf->ema_ap = true;
8153 		else
8154 			bss_conf->ema_ap = false;
8155 
8156 		/* continue assoc process */
8157 		ifmgd->assoc_data->timeout = jiffies;
8158 		ifmgd->assoc_data->timeout_started = true;
8159 		run_again(sdata, ifmgd->assoc_data->timeout);
8160 		kfree(elems);
8161 		return;
8162 	}
8163 
8164 	if (!ifmgd->associated ||
8165 	    !ieee80211_rx_our_beacon(bssid, bss_conf->bss))
8166 		return;
8167 	bssid = link->u.mgd.bssid;
8168 
8169 	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
8170 		ieee80211_handle_beacon_sig(link, ifmgd, bss_conf,
8171 					    local, rx_status);
8172 
8173 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
8174 		mlme_dbg_ratelimited(sdata,
8175 				     "cancelling AP probe due to a received beacon\n");
8176 		ieee80211_reset_ap_probe(sdata);
8177 	}
8178 
8179 	/*
8180 	 * Push the beacon loss detection into the future since
8181 	 * we are processing a beacon from the AP just now.
8182 	 */
8183 	ieee80211_sta_reset_beacon_monitor(sdata);
8184 
8185 	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
8186 	 * element (which carries the beacon interval). Don't forget to add a
8187 	 * bit to care_about_ies[] above if mac80211 is interested in a
8188 	 * changing S1G element.
8189 	 */
8190 	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
8191 		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
8192 	parse_params.bss = bss_conf->bss;
8193 	parse_params.filter = care_about_ies;
8194 	parse_params.crc = ncrc;
8195 	elems = ieee802_11_parse_elems_full(&parse_params);
8196 	if (!elems)
8197 		return;
8198 
8199 	/*
8200 	 * Note: with MBSSID and an EMA (or broken) AP, we could fail to find
8201 	 * the correct multi-BSSID profile for the non-transmitting AP we're
8202 	 * connected to. The result's elems->mbssid_nontx_profile_missing is
8203 	 * indicating that, but some things must happen regardless.
8204 	 */
8205 
8206 	if (rx_status->flag & RX_FLAG_DECRYPTED &&
8207 	    ieee80211_mgd_ssid_mismatch(sdata, elems)) {
8208 		sdata_info(sdata, "SSID mismatch for AP %pM, disconnect\n",
8209 			   sdata->vif.cfg.ap_addr);
8210 		__ieee80211_disconnect(sdata);
8211 		return;
8212 	}
8213 
8214 	ncrc = elems->crc;
8215 
8216 	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
8217 	    ieee80211_check_tim(elems->tim, elems->tim_len, vif_cfg->aid,
8218 				vif_cfg->s1g)) {
8219 		if (local->hw.conf.dynamic_ps_timeout > 0) {
8220 			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
8221 				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
8222 				ieee80211_hw_config(local, -1,
8223 						    IEEE80211_CONF_CHANGE_PS);
8224 			}
8225 			ieee80211_send_nullfunc(local, sdata, false);
8226 		} else if (!local->pspolling && sdata->u.mgd.powersave) {
8227 			local->pspolling = true;
8228 
8229 			/*
8230 			 * Here is assumed that the driver will be
8231 			 * able to send ps-poll frame and receive a
8232 			 * response even though power save mode is
8233 			 * enabled, but some drivers might require
8234 			 * to disable power save here. This needs
8235 			 * to be investigated.
8236 			 */
8237 			ieee80211_send_pspoll(local, sdata);
8238 		}
8239 	}
8240 
8241 	/*
8242 	 * P2P will almost certainly not have MBSSID, but this just
8243 	 * assumes that it would at least always inherit NoA anyway
8244 	 * since it's absent from the channel.
8245 	 */
8246 	if (sdata->vif.p2p ||
8247 	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
8248 		struct ieee80211_p2p_noa_attr noa = {};
8249 		int ret;
8250 
8251 		ret = cfg80211_get_p2p_attr(variable,
8252 					    len - baselen,
8253 					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
8254 					    (u8 *) &noa, sizeof(noa));
8255 		if (ret >= 2) {
8256 			if (link->u.mgd.p2p_noa_index != noa.index) {
8257 				/* valid noa_attr and index changed */
8258 				link->u.mgd.p2p_noa_index = noa.index;
8259 				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
8260 				changed |= BSS_CHANGED_P2P_PS;
8261 				/*
8262 				 * make sure we update all information, the CRC
8263 				 * mechanism doesn't look at P2P attributes.
8264 				 */
8265 				link->u.mgd.beacon_crc_valid = false;
8266 			}
8267 		} else if (link->u.mgd.p2p_noa_index != -1) {
8268 			/* noa_attr not found and we had valid noa_attr before */
8269 			link->u.mgd.p2p_noa_index = -1;
8270 			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
8271 			changed |= BSS_CHANGED_P2P_PS;
8272 			link->u.mgd.beacon_crc_valid = false;
8273 		}
8274 	}
8275 
8276 	/*
8277 	 * Update beacon timing and dtim count on every beacon appearance. This
8278 	 * will allow the driver to use the most updated values. Do it before
8279 	 * comparing this one with last received beacon.
8280 	 * IMPORTANT: These parameters would possibly be out of sync by the time
8281 	 * the driver will use them. The synchronized view is currently
8282 	 * guaranteed only in certain callbacks.
8283 	 */
8284 	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
8285 	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
8286 		bss_conf->sync_tsf =
8287 			le64_to_cpu(mgmt->u.beacon.timestamp);
8288 		bss_conf->sync_device_ts =
8289 			rx_status->device_timestamp;
8290 		bss_conf->sync_dtim_count = elems->dtim_count;
8291 	}
8292 
8293 	if ((ncrc == link->u.mgd.beacon_crc && link->u.mgd.beacon_crc_valid) ||
8294 	    (ext && ieee80211_is_s1g_short_beacon(ext->frame_control,
8295 						  parse_params.start,
8296 						  parse_params.len)))
8297 		goto free;
8298 	link->u.mgd.beacon_crc = ncrc;
8299 	link->u.mgd.beacon_crc_valid = true;
8300 
8301 	ieee80211_rx_bss_info(link, mgmt, len, rx_status);
8302 
8303 	/*
8304 	 * This assumes that all members of a multiple BSS set must be
8305 	 * switching together, so we can parse channel switch elements
8306 	 * from the transmitted BSS even if our non-transmitted one is
8307 	 * not present in this beacon (due to EMA.)
8308 	 */
8309 	ieee80211_sta_process_chanswitch(link, rx_status->mactime,
8310 					 rx_status->device_timestamp,
8311 					 elems, elems,
8312 					 IEEE80211_CSA_SOURCE_BEACON);
8313 
8314 	/*
8315 	 * If we haven't had a beacon before, tell the driver about the
8316 	 * DTIM period (and beacon timing if desired) now.
8317 	 */
8318 	if (!link->u.mgd.have_beacon) {
8319 		/* a few bogus AP send dtim_period = 0 or no TIM IE */
8320 		bss_conf->dtim_period = elems->dtim_period ?: 1;
8321 
8322 		changed |= BSS_CHANGED_BEACON_INFO;
8323 		link->u.mgd.have_beacon = true;
8324 
8325 		ieee80211_recalc_ps(local);
8326 
8327 		ieee80211_recalc_ps_vif(sdata);
8328 	}
8329 
8330 	/* RNR isn't inside an MBSSID profile */
8331 	ieee80211_mgd_update_bss_param_ch_cnt(sdata, bss_conf, elems);
8332 
8333 	/* assume ERP would be inherited anyway */
8334 	if (!ieee80211_is_s1g_beacon(hdr->frame_control)) {
8335 		u8 erp_value = 0;
8336 		bool erp_valid;
8337 
8338 		if (elems->erp_info) {
8339 			erp_valid = true;
8340 			erp_value = elems->erp_info[0];
8341 		} else {
8342 			erp_valid = false;
8343 		}
8344 
8345 		changed |= ieee80211_handle_bss_capability(link,
8346 				le16_to_cpu(mgmt->u.beacon.capab_info),
8347 				erp_valid, erp_value);
8348 	}
8349 
8350 	/*
8351 	 * There are some other things that we can only do when the
8352 	 * real non-transmitted profile was actually parsed, so exit
8353 	 * here before doing those.
8354 	 */
8355 	if (elems->mbssid_nontx_profile_missing)
8356 		goto apply;
8357 
8358 	/*
8359 	 * This requires multi-link element, which is from the MBSSID profile.
8360 	 * Note that after this elems->ml_basic can no longer be used fully.
8361 	 *
8362 	 * Note also that currently the parsing is incorrect, so this will
8363 	 * never actually do anything.
8364 	 */
8365 	ieee80211_mgd_check_cross_link_csa(sdata, rx_status->link_id, elems);
8366 
8367 	/*
8368 	 * EDCA parameters should be the same, but perhaps ACM can differ
8369 	 * between BSSes in an MBSSID set.
8370 	 */
8371 	if (!sdata->u.mgd.epcs.enabled &&
8372 	    !link->u.mgd.disable_wmm_tracking &&
8373 	    ieee80211_sta_wmm_params(local, link, elems->wmm_param,
8374 				     elems->wmm_param_len,
8375 				     elems->mu_edca_param_set))
8376 		changed |= BSS_CHANGED_QOS;
8377 
8378 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
8379 	if (WARN_ON(!sta)) {
8380 		goto free;
8381 	}
8382 	link_sta = rcu_dereference_protected(sta->link[link->link_id],
8383 					     lockdep_is_held(&local->hw.wiphy->mtx));
8384 	if (WARN_ON(!link_sta)) {
8385 		goto free;
8386 	}
8387 
8388 	if (WARN_ON(!bss_conf->chanreq.oper.chan))
8389 		goto free;
8390 
8391 	sband = local->hw.wiphy->bands[bss_conf->chanreq.oper.chan->band];
8392 
8393 	changed |= ieee80211_recalc_twt_req(sdata, sband, link, link_sta, elems);
8394 
8395 	if (ieee80211_config_bw(link, elems, true, &changed,
8396 				IEEE80211_STYPE_BEACON)) {
8397 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
8398 				       WLAN_REASON_DEAUTH_LEAVING,
8399 				       true, deauth_buf);
8400 		ieee80211_report_disconnect(sdata, deauth_buf,
8401 					    sizeof(deauth_buf), true,
8402 					    WLAN_REASON_DEAUTH_LEAVING,
8403 					    false);
8404 		goto free;
8405 	}
8406 
8407 	if (elems->opmode_notif)
8408 		ieee80211_vht_handle_opmode(sdata, link_sta,
8409 					    *elems->opmode_notif,
8410 					    rx_status->band);
8411 
8412 	changed |= ieee80211_handle_pwr_constr(link, chan, mgmt,
8413 					       elems->country_elem,
8414 					       elems->country_elem_len,
8415 					       elems->pwr_constr_elem,
8416 					       elems->cisco_dtpc_elem);
8417 
8418 	ieee80211_ml_reconfiguration(sdata, elems);
8419 	ieee80211_process_adv_ttlm(sdata, elems,
8420 				      le64_to_cpu(mgmt->u.beacon.timestamp));
8421 
8422 apply:
8423 	ieee80211_link_info_change_notify(sdata, link, changed);
8424 free:
8425 	kfree(elems);
8426 }
8427 
8428 static void ieee80211_apply_neg_ttlm(struct ieee80211_sub_if_data *sdata,
8429 				     struct ieee80211_neg_ttlm neg_ttlm)
8430 {
8431 	u16 new_active_links, new_dormant_links, new_suspended_links, map = 0;
8432 	u8 i;
8433 
8434 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++)
8435 		map |= neg_ttlm.downlink[i] | neg_ttlm.uplink[i];
8436 
8437 	/* If there is an active TTLM, unset previously suspended links */
8438 	if (sdata->vif.neg_ttlm.valid)
8439 		sdata->vif.dormant_links &= ~sdata->vif.suspended_links;
8440 
8441 	/* exclude links that are already disabled by advertised TTLM */
8442 	new_active_links =
8443 		map & sdata->vif.valid_links & ~sdata->vif.dormant_links;
8444 	new_suspended_links =
8445 		(~map & sdata->vif.valid_links) & ~sdata->vif.dormant_links;
8446 	new_dormant_links = sdata->vif.dormant_links | new_suspended_links;
8447 	if (ieee80211_ttlm_set_links(sdata, new_active_links,
8448 				     new_dormant_links, new_suspended_links))
8449 		return;
8450 
8451 	sdata->vif.neg_ttlm = neg_ttlm;
8452 	sdata->vif.neg_ttlm.valid = true;
8453 }
8454 
8455 static void ieee80211_neg_ttlm_timeout_work(struct wiphy *wiphy,
8456 					    struct wiphy_work *work)
8457 {
8458 	struct ieee80211_sub_if_data *sdata =
8459 		container_of(work, struct ieee80211_sub_if_data,
8460 			     u.mgd.neg_ttlm_timeout_work.work);
8461 
8462 	sdata_info(sdata,
8463 		   "No negotiated TTLM response from AP, disconnecting.\n");
8464 
8465 	__ieee80211_disconnect(sdata);
8466 }
8467 
8468 static void
8469 ieee80211_neg_ttlm_add_suggested_map(struct sk_buff *skb,
8470 				     struct ieee80211_neg_ttlm *neg_ttlm)
8471 {
8472 	u8 i, direction[IEEE80211_TTLM_MAX_CNT];
8473 
8474 	if (memcmp(neg_ttlm->downlink, neg_ttlm->uplink,
8475 		   sizeof(neg_ttlm->downlink))) {
8476 		direction[0] = IEEE80211_TTLM_DIRECTION_DOWN;
8477 		direction[1] = IEEE80211_TTLM_DIRECTION_UP;
8478 	} else {
8479 		direction[0] = IEEE80211_TTLM_DIRECTION_BOTH;
8480 	}
8481 
8482 	for (i = 0; i < ARRAY_SIZE(direction); i++) {
8483 		u8 tid, len, map_ind = 0, *len_pos, *map_ind_pos, *pos;
8484 		__le16 map;
8485 
8486 		len = sizeof(struct ieee80211_ttlm_elem) + 1 + 1;
8487 
8488 		pos = skb_put(skb, len + 2);
8489 		*pos++ = WLAN_EID_EXTENSION;
8490 		len_pos = pos++;
8491 		*pos++ = WLAN_EID_EXT_TID_TO_LINK_MAPPING;
8492 		*pos++ = direction[i];
8493 		map_ind_pos = pos++;
8494 		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
8495 			map = direction[i] == IEEE80211_TTLM_DIRECTION_UP ?
8496 				cpu_to_le16(neg_ttlm->uplink[tid]) :
8497 				cpu_to_le16(neg_ttlm->downlink[tid]);
8498 			if (!map)
8499 				continue;
8500 
8501 			len += 2;
8502 			map_ind |= BIT(tid);
8503 			skb_put_data(skb, &map, sizeof(map));
8504 		}
8505 
8506 		*map_ind_pos = map_ind;
8507 		*len_pos = len;
8508 
8509 		if (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH)
8510 			break;
8511 	}
8512 }
8513 
8514 static void
8515 ieee80211_send_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
8516 			    struct ieee80211_neg_ttlm *neg_ttlm,
8517 			    u8 dialog_token)
8518 {
8519 	struct ieee80211_local *local = sdata->local;
8520 	struct ieee80211_mgmt *mgmt;
8521 	struct sk_buff *skb;
8522 	int hdr_len = IEEE80211_MIN_ACTION_SIZE(ttlm_req);
8523 	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
8524 		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
8525 
8526 	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
8527 	if (!skb)
8528 		return;
8529 
8530 	skb_reserve(skb, local->tx_headroom);
8531 	mgmt = skb_put_zero(skb, hdr_len);
8532 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
8533 					  IEEE80211_STYPE_ACTION);
8534 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
8535 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
8536 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
8537 
8538 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
8539 	mgmt->u.action.action_code = WLAN_PROTECTED_EHT_ACTION_TTLM_REQ;
8540 	mgmt->u.action.ttlm_req.dialog_token = dialog_token;
8541 	ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
8542 	ieee80211_tx_skb(sdata, skb);
8543 }
8544 
8545 int ieee80211_req_neg_ttlm(struct ieee80211_sub_if_data *sdata,
8546 			   struct cfg80211_ttlm_params *params)
8547 {
8548 	struct ieee80211_neg_ttlm neg_ttlm = {};
8549 	u8 i;
8550 
8551 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
8552 	    !(sdata->vif.cfg.mld_capa_op &
8553 	      IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP))
8554 		return -EINVAL;
8555 
8556 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
8557 		if ((params->dlink[i] & ~sdata->vif.valid_links) ||
8558 		    (params->ulink[i] & ~sdata->vif.valid_links))
8559 			return -EINVAL;
8560 
8561 		neg_ttlm.downlink[i] = params->dlink[i];
8562 		neg_ttlm.uplink[i] = params->ulink[i];
8563 	}
8564 
8565 	if (drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm) !=
8566 	    NEG_TTLM_RES_ACCEPT)
8567 		return -EINVAL;
8568 
8569 	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
8570 	sdata->u.mgd.dialog_token_alloc++;
8571 	ieee80211_send_neg_ttlm_req(sdata, &sdata->vif.neg_ttlm,
8572 				    sdata->u.mgd.dialog_token_alloc);
8573 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
8574 				  &sdata->u.mgd.neg_ttlm_timeout_work);
8575 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
8576 				 &sdata->u.mgd.neg_ttlm_timeout_work,
8577 				 IEEE80211_NEG_TTLM_REQ_TIMEOUT);
8578 	return 0;
8579 }
8580 
8581 static void
8582 ieee80211_send_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
8583 			    enum ieee80211_neg_ttlm_res ttlm_res,
8584 			    u8 dialog_token,
8585 			    struct ieee80211_neg_ttlm *neg_ttlm)
8586 {
8587 	struct ieee80211_local *local = sdata->local;
8588 	struct ieee80211_mgmt *mgmt;
8589 	struct sk_buff *skb;
8590 	int hdr_len = IEEE80211_MIN_ACTION_SIZE(ttlm_res);
8591 	int ttlm_max_len = 2 + 1 + sizeof(struct ieee80211_ttlm_elem) + 1 +
8592 		2 * 2 * IEEE80211_TTLM_NUM_TIDS;
8593 	u16 status_code;
8594 
8595 	skb = dev_alloc_skb(local->tx_headroom + hdr_len + ttlm_max_len);
8596 	if (!skb)
8597 		return;
8598 
8599 	skb_reserve(skb, local->tx_headroom);
8600 	mgmt = skb_put_zero(skb, hdr_len);
8601 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
8602 					  IEEE80211_STYPE_ACTION);
8603 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
8604 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
8605 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
8606 
8607 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
8608 	mgmt->u.action.action_code = WLAN_PROTECTED_EHT_ACTION_TTLM_RES;
8609 	mgmt->u.action.ttlm_res.dialog_token = dialog_token;
8610 	switch (ttlm_res) {
8611 	default:
8612 		WARN_ON(1);
8613 		fallthrough;
8614 	case NEG_TTLM_RES_REJECT:
8615 		status_code = WLAN_STATUS_DENIED_TID_TO_LINK_MAPPING;
8616 		break;
8617 	case NEG_TTLM_RES_ACCEPT:
8618 		status_code = WLAN_STATUS_SUCCESS;
8619 		break;
8620 	case NEG_TTLM_RES_SUGGEST_PREFERRED:
8621 		status_code = WLAN_STATUS_PREF_TID_TO_LINK_MAPPING_SUGGESTED;
8622 		ieee80211_neg_ttlm_add_suggested_map(skb, neg_ttlm);
8623 		break;
8624 	}
8625 
8626 	mgmt->u.action.ttlm_res.status_code = cpu_to_le16(status_code);
8627 	ieee80211_tx_skb(sdata, skb);
8628 }
8629 
8630 VISIBLE_IF_MAC80211_KUNIT int
8631 ieee80211_parse_neg_ttlm(struct ieee80211_sub_if_data *sdata,
8632 			 const struct ieee80211_ttlm_elem *ttlm,
8633 			 struct ieee80211_neg_ttlm *neg_ttlm,
8634 			 u8 *direction)
8635 {
8636 	u8 control, link_map_presence, map_size, tid;
8637 	u8 *pos;
8638 
8639 	/* The element size was already validated in
8640 	 * ieee80211_tid_to_link_map_size_ok()
8641 	 */
8642 	pos = (void *)ttlm->optional;
8643 
8644 	control = ttlm->control;
8645 
8646 	/* mapping switch time and expected duration fields are not expected
8647 	 * in case of negotiated TTLM
8648 	 */
8649 	if (control & (IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT |
8650 		       IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT)) {
8651 		mlme_dbg(sdata,
8652 			 "Invalid TTLM element in negotiated TTLM request\n");
8653 		return -EINVAL;
8654 	}
8655 
8656 	if (control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) {
8657 		for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
8658 			neg_ttlm->downlink[tid] = sdata->vif.valid_links;
8659 			neg_ttlm->uplink[tid] = sdata->vif.valid_links;
8660 		}
8661 		*direction = IEEE80211_TTLM_DIRECTION_BOTH;
8662 		return 0;
8663 	}
8664 
8665 	*direction = u8_get_bits(control, IEEE80211_TTLM_CONTROL_DIRECTION);
8666 	if (*direction != IEEE80211_TTLM_DIRECTION_DOWN &&
8667 	    *direction != IEEE80211_TTLM_DIRECTION_UP &&
8668 	    *direction != IEEE80211_TTLM_DIRECTION_BOTH)
8669 		return -EINVAL;
8670 
8671 	link_map_presence = *pos;
8672 	pos++;
8673 
8674 	if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
8675 		map_size = 1;
8676 	else
8677 		map_size = 2;
8678 
8679 	for (tid = 0; tid < IEEE80211_TTLM_NUM_TIDS; tid++) {
8680 		u16 map;
8681 
8682 		if (link_map_presence & BIT(tid)) {
8683 			map = ieee80211_get_ttlm(map_size, pos);
8684 			if (!map) {
8685 				mlme_dbg(sdata,
8686 					 "No active links for TID %d", tid);
8687 				return -EINVAL;
8688 			}
8689 			pos += map_size;
8690 		} else {
8691 			map = 0;
8692 		}
8693 
8694 		switch (*direction) {
8695 		case IEEE80211_TTLM_DIRECTION_BOTH:
8696 			neg_ttlm->downlink[tid] = map;
8697 			neg_ttlm->uplink[tid] = map;
8698 			break;
8699 		case IEEE80211_TTLM_DIRECTION_DOWN:
8700 			neg_ttlm->downlink[tid] = map;
8701 			break;
8702 		case IEEE80211_TTLM_DIRECTION_UP:
8703 			neg_ttlm->uplink[tid] = map;
8704 			break;
8705 		default:
8706 			return -EINVAL;
8707 		}
8708 	}
8709 	return 0;
8710 }
8711 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_parse_neg_ttlm);
8712 
8713 static void ieee80211_process_neg_ttlm_req(struct ieee80211_sub_if_data *sdata,
8714 					   struct ieee80211_mgmt *mgmt,
8715 					   size_t len)
8716 {
8717 	u8 dialog_token, direction[IEEE80211_TTLM_MAX_CNT] = {}, i;
8718 	size_t ies_len;
8719 	enum ieee80211_neg_ttlm_res ttlm_res = NEG_TTLM_RES_ACCEPT;
8720 	struct ieee802_11_elems *elems = NULL;
8721 	struct ieee80211_neg_ttlm neg_ttlm = {};
8722 
8723 	BUILD_BUG_ON(ARRAY_SIZE(direction) != ARRAY_SIZE(elems->ttlm));
8724 
8725 	if (!ieee80211_vif_is_mld(&sdata->vif))
8726 		return;
8727 
8728 	dialog_token = mgmt->u.action.ttlm_req.dialog_token;
8729 	ies_len  = len - IEEE80211_MIN_ACTION_SIZE(ttlm_req);
8730 	elems = ieee802_11_parse_elems(mgmt->u.action.ttlm_req.variable,
8731 				       ies_len,
8732 				       IEEE80211_FTYPE_MGMT |
8733 				       IEEE80211_STYPE_ACTION,
8734 				       NULL);
8735 	if (!elems) {
8736 		ttlm_res = NEG_TTLM_RES_REJECT;
8737 		goto out;
8738 	}
8739 
8740 	for (i = 0; i < elems->ttlm_num; i++) {
8741 		if (ieee80211_parse_neg_ttlm(sdata, elems->ttlm[i],
8742 					     &neg_ttlm, &direction[i]) ||
8743 		    (direction[i] == IEEE80211_TTLM_DIRECTION_BOTH &&
8744 		     elems->ttlm_num != 1)) {
8745 			ttlm_res = NEG_TTLM_RES_REJECT;
8746 			goto out;
8747 		}
8748 	}
8749 
8750 	if (!elems->ttlm_num ||
8751 	    (elems->ttlm_num == 2 && direction[0] == direction[1])) {
8752 		ttlm_res = NEG_TTLM_RES_REJECT;
8753 		goto out;
8754 	}
8755 
8756 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
8757 		if ((neg_ttlm.downlink[i] &&
8758 		     (neg_ttlm.downlink[i] & ~sdata->vif.valid_links)) ||
8759 		    (neg_ttlm.uplink[i] &&
8760 		     (neg_ttlm.uplink[i] & ~sdata->vif.valid_links))) {
8761 			ttlm_res = NEG_TTLM_RES_REJECT;
8762 			goto out;
8763 		}
8764 	}
8765 
8766 	ttlm_res = drv_can_neg_ttlm(sdata->local, sdata, &neg_ttlm);
8767 
8768 	if (ttlm_res != NEG_TTLM_RES_ACCEPT)
8769 		goto out;
8770 
8771 	ieee80211_apply_neg_ttlm(sdata, neg_ttlm);
8772 out:
8773 	kfree(elems);
8774 	ieee80211_send_neg_ttlm_res(sdata, ttlm_res, dialog_token, &neg_ttlm);
8775 }
8776 
8777 static void ieee80211_process_neg_ttlm_res(struct ieee80211_sub_if_data *sdata,
8778 					   struct ieee80211_mgmt *mgmt,
8779 					   size_t len)
8780 {
8781 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
8782 	    mgmt->u.action.ttlm_res.dialog_token != sdata->u.mgd.dialog_token_alloc)
8783 		return;
8784 
8785 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
8786 				  &sdata->u.mgd.neg_ttlm_timeout_work);
8787 
8788 	/* MLD station sends a TID to link mapping request, mainly to handle
8789 	 * BTM (BSS transition management) request, in which case it needs to
8790 	 * restrict the active links set.
8791 	 * In this case it's not expected that the MLD AP will reject the
8792 	 * negotiated TTLM request.
8793 	 * This can be better implemented in the future, to handle request
8794 	 * rejections.
8795 	 */
8796 	if (le16_to_cpu(mgmt->u.action.ttlm_res.status_code) != WLAN_STATUS_SUCCESS)
8797 		__ieee80211_disconnect(sdata);
8798 }
8799 
8800 static void ieee80211_process_ttlm_teardown(struct ieee80211_sub_if_data *sdata)
8801 {
8802 	u16 new_dormant_links;
8803 
8804 	if (!sdata->vif.neg_ttlm.valid)
8805 		return;
8806 
8807 	memset(&sdata->vif.neg_ttlm, 0, sizeof(sdata->vif.neg_ttlm));
8808 	new_dormant_links =
8809 		sdata->vif.dormant_links & ~sdata->vif.suspended_links;
8810 	sdata->vif.suspended_links = 0;
8811 	ieee80211_vif_set_links(sdata, sdata->vif.valid_links,
8812 				new_dormant_links);
8813 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_TTLM |
8814 					       BSS_CHANGED_MLD_VALID_LINKS);
8815 }
8816 
8817 static void ieee80211_teardown_ttlm_work(struct wiphy *wiphy,
8818 					 struct wiphy_work *work)
8819 {
8820 	struct ieee80211_sub_if_data *sdata =
8821 		container_of(work, struct ieee80211_sub_if_data,
8822 			     u.mgd.teardown_ttlm_work);
8823 
8824 	ieee80211_process_ttlm_teardown(sdata);
8825 }
8826 
8827 void ieee80211_send_teardown_neg_ttlm(struct ieee80211_vif *vif)
8828 {
8829 	int frame_len = IEEE80211_MIN_ACTION_SIZE(ttlm_tear_down);
8830 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
8831 	struct ieee80211_local *local = sdata->local;
8832 	struct ieee80211_mgmt *mgmt;
8833 	struct sk_buff *skb;
8834 	struct ieee80211_tx_info *info;
8835 
8836 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
8837 	if (!skb)
8838 		return;
8839 
8840 	skb_reserve(skb, local->hw.extra_tx_headroom);
8841 	mgmt = skb_put_zero(skb, frame_len);
8842 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
8843 					  IEEE80211_STYPE_ACTION);
8844 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
8845 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
8846 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
8847 
8848 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
8849 	mgmt->u.action.action_code = WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN;
8850 
8851 	info = IEEE80211_SKB_CB(skb);
8852 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
8853 	info->status_data = IEEE80211_STATUS_TYPE_NEG_TTLM;
8854 	ieee80211_tx_skb(sdata, skb);
8855 }
8856 EXPORT_SYMBOL(ieee80211_send_teardown_neg_ttlm);
8857 
8858 static void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
8859 					struct sk_buff *skb)
8860 {
8861 	struct ieee80211_link_data *link = &sdata->deflink;
8862 	struct ieee80211_rx_status *rx_status;
8863 	struct ieee80211_hdr *hdr;
8864 	u16 fc;
8865 
8866 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8867 
8868 	rx_status = (struct ieee80211_rx_status *) skb->cb;
8869 	hdr = (struct ieee80211_hdr *) skb->data;
8870 	fc = le16_to_cpu(hdr->frame_control);
8871 
8872 	switch (fc & IEEE80211_FCTL_STYPE) {
8873 	case IEEE80211_STYPE_S1G_BEACON:
8874 		ieee80211_rx_mgmt_beacon(link, hdr, skb->len, rx_status);
8875 		break;
8876 	}
8877 }
8878 
8879 static void ieee80211_sta_timer(struct timer_list *t)
8880 {
8881 	struct ieee80211_sub_if_data *sdata =
8882 		timer_container_of(sdata, t, u.mgd.timer);
8883 
8884 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
8885 }
8886 
8887 void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
8888 				   u8 reason, bool tx)
8889 {
8890 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
8891 
8892 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
8893 			       tx, frame_buf);
8894 
8895 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
8896 				    reason, false);
8897 }
8898 
8899 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
8900 {
8901 	struct ieee80211_local *local = sdata->local;
8902 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8903 	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
8904 	u32 tx_flags = 0;
8905 	u16 trans = 1;
8906 	u16 status = 0;
8907 	struct ieee80211_prep_tx_info info = {
8908 		.subtype = IEEE80211_STYPE_AUTH,
8909 	};
8910 
8911 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8912 
8913 	if (WARN_ON_ONCE(!auth_data))
8914 		return -EINVAL;
8915 
8916 	if (auth_data->algorithm == WLAN_AUTH_EPPKE &&
8917 	    ieee80211_vif_is_mld(&sdata->vif) &&
8918 	    !cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_MULTI_LINK,
8919 				    auth_data->data, auth_data->data_len))
8920 		return -EINVAL;
8921 
8922 	auth_data->tries++;
8923 
8924 	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
8925 		sdata_info(sdata, "authentication with %pM timed out\n",
8926 			   auth_data->ap_addr);
8927 
8928 		/*
8929 		 * Most likely AP is not in the range so remove the
8930 		 * bss struct for that AP.
8931 		 */
8932 		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
8933 
8934 		return -ETIMEDOUT;
8935 	}
8936 
8937 	if (auth_data->algorithm == WLAN_AUTH_SAE ||
8938 	    auth_data->algorithm == WLAN_AUTH_EPPKE)
8939 		info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
8940 
8941 	info.link_id = auth_data->link_id;
8942 	drv_mgd_prepare_tx(local, sdata, &info);
8943 
8944 	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
8945 		   auth_data->ap_addr, auth_data->tries,
8946 		   IEEE80211_AUTH_MAX_TRIES);
8947 
8948 	auth_data->expected_transaction = 2;
8949 
8950 	if (auth_data->algorithm == WLAN_AUTH_SAE) {
8951 		trans = auth_data->trans;
8952 		status = auth_data->status;
8953 		auth_data->expected_transaction = trans;
8954 	} else if (auth_data->algorithm == WLAN_AUTH_EPPKE) {
8955 		trans = auth_data->trans;
8956 		status = auth_data->status;
8957 	} else if (auth_data->algorithm == WLAN_AUTH_IEEE8021X) {
8958 		trans = auth_data->trans;
8959 		status = auth_data->status;
8960 		auth_data->expected_transaction = trans + 1;
8961 	}
8962 
8963 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
8964 		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
8965 			   IEEE80211_TX_INTFL_MLME_CONN_TX;
8966 
8967 	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
8968 			    auth_data->data, auth_data->data_len,
8969 			    auth_data->ap_addr, auth_data->ap_addr,
8970 			    NULL, 0, 0, tx_flags);
8971 
8972 	if (tx_flags == 0) {
8973 		if (auth_data->algorithm == WLAN_AUTH_SAE)
8974 			auth_data->timeout = jiffies +
8975 				IEEE80211_AUTH_TIMEOUT_SAE;
8976 		else
8977 			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
8978 	} else {
8979 		auth_data->timeout =
8980 			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
8981 	}
8982 
8983 	auth_data->timeout_started = true;
8984 	run_again(sdata, auth_data->timeout);
8985 
8986 	return 0;
8987 }
8988 
8989 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
8990 {
8991 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
8992 	struct ieee80211_local *local = sdata->local;
8993 	int ret;
8994 
8995 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
8996 
8997 	assoc_data->tries++;
8998 	assoc_data->comeback = false;
8999 	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
9000 		sdata_info(sdata, "association with %pM timed out\n",
9001 			   assoc_data->ap_addr);
9002 
9003 		/*
9004 		 * Most likely AP is not in the range so remove the
9005 		 * bss struct for that AP.
9006 		 */
9007 		cfg80211_unlink_bss(local->hw.wiphy,
9008 				    assoc_data->link[assoc_data->assoc_link_id].bss);
9009 
9010 		return -ETIMEDOUT;
9011 	}
9012 
9013 	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
9014 		   assoc_data->ap_addr, assoc_data->tries,
9015 		   IEEE80211_ASSOC_MAX_TRIES);
9016 	ret = ieee80211_send_assoc(sdata);
9017 	if (ret)
9018 		return ret;
9019 
9020 	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
9021 		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
9022 		assoc_data->timeout_started = true;
9023 		run_again(sdata, assoc_data->timeout);
9024 	} else {
9025 		assoc_data->timeout =
9026 			round_jiffies_up(jiffies +
9027 					 IEEE80211_ASSOC_TIMEOUT_LONG);
9028 		assoc_data->timeout_started = true;
9029 		run_again(sdata, assoc_data->timeout);
9030 	}
9031 
9032 	return 0;
9033 }
9034 
9035 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
9036 				  __le16 fc, bool acked)
9037 {
9038 	struct ieee80211_local *local = sdata->local;
9039 
9040 	sdata->u.mgd.status_fc = fc;
9041 	sdata->u.mgd.status_acked = acked;
9042 	sdata->u.mgd.status_received = true;
9043 
9044 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
9045 }
9046 
9047 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
9048 {
9049 	struct ieee80211_local *local = sdata->local;
9050 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9051 
9052 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
9053 
9054 	if (ifmgd->status_received) {
9055 		__le16 fc = ifmgd->status_fc;
9056 		bool status_acked = ifmgd->status_acked;
9057 
9058 		ifmgd->status_received = false;
9059 		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
9060 			if (status_acked) {
9061 				if (ifmgd->auth_data->algorithm ==
9062 				    WLAN_AUTH_SAE)
9063 					ifmgd->auth_data->timeout =
9064 						jiffies +
9065 						IEEE80211_AUTH_TIMEOUT_SAE;
9066 				else
9067 					ifmgd->auth_data->timeout =
9068 						jiffies +
9069 						IEEE80211_AUTH_TIMEOUT_SHORT;
9070 				run_again(sdata, ifmgd->auth_data->timeout);
9071 			} else {
9072 				ifmgd->auth_data->timeout = jiffies - 1;
9073 			}
9074 			ifmgd->auth_data->timeout_started = true;
9075 		} else if (ifmgd->assoc_data &&
9076 			   !ifmgd->assoc_data->comeback &&
9077 			   (ieee80211_is_assoc_req(fc) ||
9078 			    ieee80211_is_reassoc_req(fc))) {
9079 			/*
9080 			 * Update association timeout based on the TX status
9081 			 * for the (Re)Association Request frame. Skip this if
9082 			 * we have already processed a (Re)Association Response
9083 			 * frame that indicated need for association comeback
9084 			 * at a specific time in the future. This could happen
9085 			 * if the TX status information is delayed enough for
9086 			 * the response to be received and processed first.
9087 			 */
9088 			if (status_acked) {
9089 				ifmgd->assoc_data->timeout =
9090 					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
9091 				run_again(sdata, ifmgd->assoc_data->timeout);
9092 			} else {
9093 				ifmgd->assoc_data->timeout = jiffies - 1;
9094 			}
9095 			ifmgd->assoc_data->timeout_started = true;
9096 		}
9097 	}
9098 
9099 	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
9100 	    time_after(jiffies, ifmgd->auth_data->timeout)) {
9101 		if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
9102 			/*
9103 			 * ok ... we waited for assoc or continuation but
9104 			 * userspace didn't do it, so kill the auth data
9105 			 */
9106 			ieee80211_destroy_auth_data(sdata, false);
9107 		} else if (ieee80211_auth(sdata)) {
9108 			u8 ap_addr[ETH_ALEN];
9109 			struct ieee80211_event event = {
9110 				.type = MLME_EVENT,
9111 				.u.mlme.data = AUTH_EVENT,
9112 				.u.mlme.status = MLME_TIMEOUT,
9113 			};
9114 
9115 			memcpy(ap_addr, ifmgd->auth_data->ap_addr, ETH_ALEN);
9116 
9117 			ieee80211_destroy_auth_data(sdata, false);
9118 
9119 			cfg80211_auth_timeout(sdata->dev, ap_addr);
9120 			drv_event_callback(sdata->local, sdata, &event);
9121 		}
9122 	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
9123 		run_again(sdata, ifmgd->auth_data->timeout);
9124 
9125 	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
9126 	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
9127 		if ((ifmgd->assoc_data->need_beacon &&
9128 		     !sdata->deflink.u.mgd.have_beacon) ||
9129 		    ieee80211_do_assoc(sdata)) {
9130 			struct ieee80211_event event = {
9131 				.type = MLME_EVENT,
9132 				.u.mlme.data = ASSOC_EVENT,
9133 				.u.mlme.status = MLME_TIMEOUT,
9134 			};
9135 
9136 			ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
9137 			drv_event_callback(sdata->local, sdata, &event);
9138 		}
9139 	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
9140 		run_again(sdata, ifmgd->assoc_data->timeout);
9141 
9142 	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
9143 	    ifmgd->associated) {
9144 		u8 *bssid = sdata->deflink.u.mgd.bssid;
9145 		int max_tries;
9146 
9147 		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
9148 			max_tries = max_nullfunc_tries;
9149 		else
9150 			max_tries = max_probe_tries;
9151 
9152 		/* ACK received for nullfunc probing frame */
9153 		if (!ifmgd->probe_send_count)
9154 			ieee80211_reset_ap_probe(sdata);
9155 		else if (ifmgd->nullfunc_failed) {
9156 			if (ifmgd->probe_send_count < max_tries) {
9157 				mlme_dbg(sdata,
9158 					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
9159 					 bssid, ifmgd->probe_send_count,
9160 					 max_tries);
9161 				ieee80211_mgd_probe_ap_send(sdata);
9162 			} else {
9163 				mlme_dbg(sdata,
9164 					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
9165 					 bssid);
9166 				ieee80211_sta_connection_lost(sdata,
9167 					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
9168 					false);
9169 			}
9170 		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
9171 			run_again(sdata, ifmgd->probe_timeout);
9172 		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
9173 			mlme_dbg(sdata,
9174 				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
9175 				 bssid, probe_wait_ms);
9176 			ieee80211_sta_connection_lost(sdata,
9177 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
9178 		} else if (ifmgd->probe_send_count < max_tries) {
9179 			mlme_dbg(sdata,
9180 				 "No probe response from AP %pM after %dms, try %d/%i\n",
9181 				 bssid, probe_wait_ms,
9182 				 ifmgd->probe_send_count, max_tries);
9183 			ieee80211_mgd_probe_ap_send(sdata);
9184 		} else {
9185 			/*
9186 			 * We actually lost the connection ... or did we?
9187 			 * Let's make sure!
9188 			 */
9189 			mlme_dbg(sdata,
9190 				 "No probe response from AP %pM after %dms, disconnecting.\n",
9191 				 bssid, probe_wait_ms);
9192 
9193 			ieee80211_sta_connection_lost(sdata,
9194 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
9195 		}
9196 	}
9197 }
9198 
9199 static bool
9200 ieee80211_is_csa_in_progress(struct ieee80211_sub_if_data *sdata)
9201 {
9202 	/*
9203 	 * In MLO, check the CSA flags 'active' and 'waiting_bcn' for all
9204 	 * the links.
9205 	 */
9206 	struct ieee80211_link_data *link;
9207 
9208 	guard(rcu)();
9209 
9210 	for_each_link_data_rcu(sdata, link) {
9211 		if (!(link->conf->csa_active &&
9212 		      !link->u.mgd.csa.waiting_bcn))
9213 			return false;
9214 	}
9215 
9216 	return true;
9217 }
9218 
9219 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
9220 {
9221 	struct ieee80211_sub_if_data *sdata =
9222 		timer_container_of(sdata, t, u.mgd.bcn_mon_timer);
9223 
9224 	if (ieee80211_is_csa_in_progress(sdata))
9225 		return;
9226 
9227 	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
9228 		return;
9229 
9230 	sdata->u.mgd.connection_loss = false;
9231 	wiphy_work_queue(sdata->local->hw.wiphy,
9232 			 &sdata->u.mgd.beacon_connection_loss_work);
9233 }
9234 
9235 static unsigned long
9236 ieee80211_latest_active_link_conn_timeout(struct ieee80211_sub_if_data *sdata)
9237 {
9238 	unsigned long latest_timeout = jiffies;
9239 	unsigned int link_id;
9240 	struct sta_info *sta;
9241 
9242 	guard(rcu)();
9243 
9244 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
9245 	if (!sta)
9246 		return 0;
9247 
9248 	for (link_id = 0; link_id < ARRAY_SIZE(sta->link);
9249 	     link_id++) {
9250 		struct link_sta_info *link_sta;
9251 		unsigned long timeout;
9252 
9253 		link_sta = rcu_dereference(sta->link[link_id]);
9254 		if (!link_sta)
9255 			continue;
9256 
9257 		timeout = link_sta->status_stats.last_ack;
9258 		if (time_before(timeout, link_sta->rx_stats.last_rx))
9259 			timeout = link_sta->rx_stats.last_rx;
9260 
9261 		timeout += IEEE80211_CONNECTION_IDLE_TIME;
9262 
9263 		/*
9264 		 * latest_timeout holds the timeout of the link
9265 		 * that will expire last among all links in an
9266 		 * non-AP MLD STA. This ensures that the connection
9267 		 * monitor timer is only reset if at least one link
9268 		 * is still active, and it is scheduled to fire at
9269 		 * the latest possible timeout.
9270 		 */
9271 		if (time_after(timeout, latest_timeout))
9272 			latest_timeout = timeout;
9273 	}
9274 
9275 	return latest_timeout;
9276 }
9277 
9278 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
9279 {
9280 	struct ieee80211_sub_if_data *sdata =
9281 		timer_container_of(sdata, t, u.mgd.conn_mon_timer);
9282 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9283 	struct ieee80211_local *local = sdata->local;
9284 	unsigned long latest_timeout;
9285 
9286 	if (ieee80211_is_csa_in_progress(sdata))
9287 		return;
9288 
9289 	latest_timeout = ieee80211_latest_active_link_conn_timeout(sdata);
9290 
9291 	/*
9292 	 * If latest timeout is after now, then update timer to fire at
9293 	 * the later date, but do not actually probe at this time.
9294 	 */
9295 	if (time_is_after_jiffies(latest_timeout)) {
9296 		mod_timer(&ifmgd->conn_mon_timer,
9297 			  round_jiffies_up(latest_timeout));
9298 		return;
9299 	}
9300 
9301 	wiphy_work_queue(local->hw.wiphy, &sdata->u.mgd.monitor_work);
9302 }
9303 
9304 static void ieee80211_sta_monitor_work(struct wiphy *wiphy,
9305 				       struct wiphy_work *work)
9306 {
9307 	struct ieee80211_sub_if_data *sdata =
9308 		container_of(work, struct ieee80211_sub_if_data,
9309 			     u.mgd.monitor_work);
9310 
9311 	ieee80211_mgd_probe_ap(sdata, false);
9312 }
9313 
9314 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
9315 {
9316 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
9317 		__ieee80211_stop_poll(sdata);
9318 
9319 		/* let's probe the connection once */
9320 		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
9321 			wiphy_work_queue(sdata->local->hw.wiphy,
9322 					 &sdata->u.mgd.monitor_work);
9323 	}
9324 }
9325 
9326 #ifdef CONFIG_PM
9327 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
9328 {
9329 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9330 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9331 
9332 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
9333 
9334 	if (ifmgd->auth_data || ifmgd->assoc_data) {
9335 		const u8 *ap_addr = ifmgd->auth_data ?
9336 				ifmgd->auth_data->ap_addr :
9337 				ifmgd->assoc_data->ap_addr;
9338 
9339 		/*
9340 		 * If we are trying to authenticate / associate while suspending,
9341 		 * cfg80211 won't know and won't actually abort those attempts,
9342 		 * thus we need to do that ourselves.
9343 		 */
9344 		ieee80211_send_deauth_disassoc(sdata, ap_addr, ap_addr,
9345 					       IEEE80211_STYPE_DEAUTH,
9346 					       WLAN_REASON_DEAUTH_LEAVING,
9347 					       false, frame_buf);
9348 		if (ifmgd->assoc_data)
9349 			ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
9350 		if (ifmgd->auth_data)
9351 			ieee80211_destroy_auth_data(sdata, false);
9352 		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
9353 				      IEEE80211_DEAUTH_FRAME_LEN,
9354 				      false);
9355 	}
9356 
9357 	/* This is a bit of a hack - we should find a better and more generic
9358 	 * solution to this. Normally when suspending, cfg80211 will in fact
9359 	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
9360 	 * auth (not so important) or assoc (this is the problem) process.
9361 	 *
9362 	 * As a consequence, it can happen that we are in the process of both
9363 	 * associating and suspending, and receive an association response
9364 	 * after cfg80211 has checked if it needs to disconnect, but before
9365 	 * we actually set the flag to drop incoming frames. This will then
9366 	 * cause the workqueue flush to process the association response in
9367 	 * the suspend, resulting in a successful association just before it
9368 	 * tries to remove the interface from the driver, which now though
9369 	 * has a channel context assigned ... this results in issues.
9370 	 *
9371 	 * To work around this (for now) simply deauth here again if we're
9372 	 * now connected.
9373 	 */
9374 	if (ifmgd->associated && !sdata->local->wowlan) {
9375 		u8 bssid[ETH_ALEN];
9376 		struct cfg80211_deauth_request req = {
9377 			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
9378 			.bssid = bssid,
9379 		};
9380 
9381 		memcpy(bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
9382 		ieee80211_mgd_deauth(sdata, &req);
9383 	}
9384 }
9385 #endif
9386 
9387 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
9388 {
9389 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9390 
9391 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
9392 
9393 	if (!ifmgd->associated)
9394 		return;
9395 
9396 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
9397 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
9398 		mlme_dbg(sdata, "driver requested disconnect after resume\n");
9399 		ieee80211_sta_connection_lost(sdata,
9400 					      WLAN_REASON_UNSPECIFIED,
9401 					      true);
9402 		return;
9403 	}
9404 
9405 	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_HW_RESTART) {
9406 		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_HW_RESTART;
9407 		mlme_dbg(sdata, "driver requested disconnect after hardware restart\n");
9408 		ieee80211_sta_connection_lost(sdata,
9409 					      WLAN_REASON_UNSPECIFIED,
9410 					      true);
9411 		return;
9412 	}
9413 }
9414 
9415 static void ieee80211_request_smps_mgd_work(struct wiphy *wiphy,
9416 					    struct wiphy_work *work)
9417 {
9418 	struct ieee80211_link_data *link =
9419 		container_of(work, struct ieee80211_link_data,
9420 			     u.mgd.request_smps_work);
9421 
9422 	__ieee80211_request_smps_mgd(link->sdata, link,
9423 				     link->u.mgd.driver_smps_mode);
9424 }
9425 
9426 static void ieee80211_ml_sta_reconf_timeout(struct wiphy *wiphy,
9427 					    struct wiphy_work *work)
9428 {
9429 	struct ieee80211_sub_if_data *sdata =
9430 		container_of(work, struct ieee80211_sub_if_data,
9431 			     u.mgd.reconf.wk.work);
9432 
9433 	if (!sdata->u.mgd.reconf.added_links &&
9434 	    !sdata->u.mgd.reconf.removed_links)
9435 		return;
9436 
9437 	sdata_info(sdata,
9438 		   "mlo: reconf: timeout: added=0x%x, removed=0x%x\n",
9439 		   sdata->u.mgd.reconf.added_links,
9440 		   sdata->u.mgd.reconf.removed_links);
9441 
9442 	__ieee80211_disconnect(sdata);
9443 }
9444 
9445 /* interface setup */
9446 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
9447 {
9448 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9449 
9450 	wiphy_work_init(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
9451 	wiphy_work_init(&ifmgd->beacon_connection_loss_work,
9452 			ieee80211_beacon_connection_loss_work);
9453 	wiphy_work_init(&ifmgd->csa_connection_drop_work,
9454 			ieee80211_csa_connection_drop_work);
9455 	wiphy_delayed_work_init(&ifmgd->tdls_peer_del_work,
9456 				ieee80211_tdls_peer_del_work);
9457 	wiphy_hrtimer_work_init(&ifmgd->ml_reconf_work,
9458 				ieee80211_ml_reconf_work);
9459 	wiphy_delayed_work_init(&ifmgd->reconf.wk,
9460 				ieee80211_ml_sta_reconf_timeout);
9461 	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
9462 	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
9463 	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
9464 	wiphy_delayed_work_init(&ifmgd->tx_tspec_wk,
9465 				ieee80211_sta_handle_tspec_ac_params_wk);
9466 	wiphy_hrtimer_work_init(&ifmgd->ttlm_work,
9467 				ieee80211_tid_to_link_map_work);
9468 	wiphy_delayed_work_init(&ifmgd->neg_ttlm_timeout_work,
9469 				ieee80211_neg_ttlm_timeout_work);
9470 	wiphy_work_init(&ifmgd->teardown_ttlm_work,
9471 			ieee80211_teardown_ttlm_work);
9472 	wiphy_hrtimer_work_init(&ifmgd->uhr_omp.status_work,
9473 				ieee80211_uhr_omp_req_status_wk);
9474 
9475 	ifmgd->flags = 0;
9476 	ifmgd->powersave = sdata->wdev.ps;
9477 	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
9478 	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
9479 	/* Setup TDLS data */
9480 	spin_lock_init(&ifmgd->teardown_lock);
9481 	ifmgd->teardown_skb = NULL;
9482 	ifmgd->orig_teardown_skb = NULL;
9483 	ifmgd->mcast_seq_last = IEEE80211_SN_MODULO;
9484 }
9485 
9486 static void ieee80211_recalc_smps_work(struct wiphy *wiphy,
9487 				       struct wiphy_work *work)
9488 {
9489 	struct ieee80211_link_data *link =
9490 		container_of(work, struct ieee80211_link_data,
9491 			     u.mgd.recalc_smps);
9492 
9493 	ieee80211_recalc_smps(link->sdata, link);
9494 }
9495 
9496 void ieee80211_mgd_setup_link(struct ieee80211_link_data *link)
9497 {
9498 	struct ieee80211_sub_if_data *sdata = link->sdata;
9499 	struct ieee80211_local *local = sdata->local;
9500 	unsigned int link_id = link->link_id;
9501 
9502 	link->u.mgd.p2p_noa_index = -1;
9503 	link->conf->bssid = link->u.mgd.bssid;
9504 	link->smps_mode = IEEE80211_SMPS_OFF;
9505 
9506 	wiphy_work_init(&link->u.mgd.request_smps_work,
9507 			ieee80211_request_smps_mgd_work);
9508 	wiphy_work_init(&link->u.mgd.recalc_smps,
9509 			ieee80211_recalc_smps_work);
9510 	if (local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
9511 		link->u.mgd.req_smps = IEEE80211_SMPS_AUTOMATIC;
9512 	else
9513 		link->u.mgd.req_smps = IEEE80211_SMPS_OFF;
9514 
9515 	wiphy_hrtimer_work_init(&link->u.mgd.csa.switch_work,
9516 				ieee80211_csa_switch_work);
9517 
9518 	ieee80211_clear_tpe(&link->conf->tpe);
9519 
9520 	if (sdata->u.mgd.assoc_data)
9521 		ether_addr_copy(link->conf->addr,
9522 				sdata->u.mgd.assoc_data->link[link_id].addr);
9523 	else if (sdata->u.mgd.reconf.add_links_data)
9524 		ether_addr_copy(link->conf->addr,
9525 				sdata->u.mgd.reconf.add_links_data->link[link_id].addr);
9526 	else if (!is_valid_ether_addr(link->conf->addr))
9527 		eth_random_addr(link->conf->addr);
9528 }
9529 
9530 /* scan finished notification */
9531 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
9532 {
9533 	struct ieee80211_sub_if_data *sdata;
9534 
9535 	/* Restart STA timers */
9536 	rcu_read_lock();
9537 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
9538 		if (ieee80211_sdata_running(sdata))
9539 			ieee80211_restart_sta_timer(sdata);
9540 	}
9541 	rcu_read_unlock();
9542 }
9543 
9544 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
9545 				     struct cfg80211_bss *cbss, s8 link_id,
9546 				     const u8 *ap_mld_addr, bool assoc,
9547 				     struct ieee80211_conn_settings *conn,
9548 				     bool override,
9549 				     unsigned long *userspace_selectors)
9550 {
9551 	struct ieee80211_local *local = sdata->local;
9552 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9553 	struct ieee80211_bss *bss = (void *)cbss->priv;
9554 	struct sta_info *new_sta = NULL;
9555 	struct ieee80211_link_data *link;
9556 	struct sta_info *have_sta = NULL;
9557 	bool mlo;
9558 	int err;
9559 	u16 new_links;
9560 
9561 	if (link_id >= 0) {
9562 		mlo = true;
9563 		if (WARN_ON(!ap_mld_addr))
9564 			return -EINVAL;
9565 		new_links = BIT(link_id);
9566 	} else {
9567 		if (WARN_ON(ap_mld_addr))
9568 			return -EINVAL;
9569 		ap_mld_addr = cbss->bssid;
9570 		new_links = 0;
9571 		link_id = 0;
9572 		mlo = false;
9573 	}
9574 
9575 	if (assoc)
9576 		have_sta = sta_info_get(sdata, ap_mld_addr);
9577 
9578 	if (mlo && !have_sta &&
9579 	    WARN_ON(sdata->vif.valid_links || sdata->vif.active_links))
9580 		return -EINVAL;
9581 
9582 	err = ieee80211_vif_set_links(sdata, new_links, 0);
9583 	if (err)
9584 		return err;
9585 
9586 	link = sdata_dereference(sdata->link[link_id], sdata);
9587 	if (WARN_ON(!link)) {
9588 		err = -ENOLINK;
9589 		goto out_err;
9590 	}
9591 
9592 	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data)) {
9593 		err = -EINVAL;
9594 		goto out_err;
9595 	}
9596 
9597 	/* If a reconfig is happening, bail out */
9598 	if (local->in_reconfig) {
9599 		err = -EBUSY;
9600 		goto out_err;
9601 	}
9602 
9603 	if (!have_sta) {
9604 		if (mlo)
9605 			new_sta = sta_info_alloc_with_link(sdata, ap_mld_addr,
9606 							   link_id, cbss->bssid,
9607 							   GFP_KERNEL);
9608 		else
9609 			new_sta = sta_info_alloc(sdata, ap_mld_addr, GFP_KERNEL);
9610 
9611 		if (!new_sta) {
9612 			err = -ENOMEM;
9613 			goto out_err;
9614 		}
9615 
9616 		if (ifmgd->auth_data &&
9617 		    (ifmgd->auth_data->algorithm == WLAN_AUTH_EPPKE ||
9618 		     ifmgd->auth_data->algorithm == WLAN_AUTH_IEEE8021X))
9619 			new_sta->sta.epp_peer = true;
9620 
9621 		new_sta->sta.mlo = mlo;
9622 	}
9623 
9624 	/*
9625 	 * Set up the information for the new channel before setting the
9626 	 * new channel. We can't - completely race-free - change the basic
9627 	 * rates bitmap and the channel (sband) that it refers to, but if
9628 	 * we set it up before we at least avoid calling into the driver's
9629 	 * bss_info_changed() method with invalid information (since we do
9630 	 * call that from changing the channel - only for IDLE and perhaps
9631 	 * some others, but ...).
9632 	 *
9633 	 * So to avoid that, just set up all the new information before the
9634 	 * channel, but tell the driver to apply it only afterwards, since
9635 	 * it might need the new channel for that.
9636 	 */
9637 	if (new_sta) {
9638 		const struct cfg80211_bss_ies *ies;
9639 		struct link_sta_info *link_sta;
9640 
9641 		rcu_read_lock();
9642 		link_sta = rcu_dereference(new_sta->link[link_id]);
9643 		if (WARN_ON(!link_sta)) {
9644 			rcu_read_unlock();
9645 			sta_info_free(local, new_sta);
9646 			err = -EINVAL;
9647 			goto out_err;
9648 		}
9649 
9650 		err = ieee80211_mgd_setup_link_sta(link, new_sta,
9651 						   link_sta, cbss);
9652 		if (err) {
9653 			rcu_read_unlock();
9654 			sta_info_free(local, new_sta);
9655 			goto out_err;
9656 		}
9657 
9658 		memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
9659 
9660 		/* set timing information */
9661 		link->conf->beacon_int = cbss->beacon_interval;
9662 		ies = rcu_dereference(cbss->beacon_ies);
9663 		if (ies) {
9664 			link->conf->sync_tsf = ies->tsf;
9665 			link->conf->sync_device_ts =
9666 				bss->device_ts_beacon;
9667 
9668 			ieee80211_get_dtim(ies,
9669 					   &link->conf->sync_dtim_count,
9670 					   NULL);
9671 		} else if (!ieee80211_hw_check(&sdata->local->hw,
9672 					       TIMING_BEACON_ONLY)) {
9673 			ies = rcu_dereference(cbss->proberesp_ies);
9674 			/* must be non-NULL since beacon IEs were NULL */
9675 			link->conf->sync_tsf = ies->tsf;
9676 			link->conf->sync_device_ts =
9677 				bss->device_ts_presp;
9678 			link->conf->sync_dtim_count = 0;
9679 		} else {
9680 			link->conf->sync_tsf = 0;
9681 			link->conf->sync_device_ts = 0;
9682 			link->conf->sync_dtim_count = 0;
9683 		}
9684 		rcu_read_unlock();
9685 	}
9686 
9687 	if (new_sta || override) {
9688 		struct link_sta_info *link_sta;
9689 		struct sta_info *ap;
9690 
9691 		/*
9692 		 * Only set this if we're also going to calculate the AP
9693 		 * settings etc., otherwise this was set before in a
9694 		 * previous call. Note override is set to %true in assoc
9695 		 * if the settings were changed.
9696 		 */
9697 		link->u.mgd.conn = *conn;
9698 
9699 		ap = new_sta ?: have_sta;
9700 		link_sta = sdata_dereference(ap->link[link->link_id], sdata);
9701 		if (!link_sta) {
9702 			err = -EINVAL;
9703 			if (new_sta)
9704 				sta_info_free(local, new_sta);
9705 			goto out_err;
9706 		}
9707 
9708 		err = ieee80211_prep_channel(sdata, link, link_sta,
9709 					     link->link_id, cbss,
9710 					     mlo, &link->u.mgd.conn,
9711 					     userspace_selectors);
9712 		if (err) {
9713 			if (new_sta)
9714 				sta_info_free(local, new_sta);
9715 			goto out_err;
9716 		}
9717 		/* pass out for use in assoc */
9718 		*conn = link->u.mgd.conn;
9719 	}
9720 
9721 	if (new_sta) {
9722 		/*
9723 		 * tell driver about BSSID, basic rates and timing
9724 		 * this was set up above, before setting the channel
9725 		 */
9726 		ieee80211_link_info_change_notify(sdata, link,
9727 						  BSS_CHANGED_BSSID |
9728 						  BSS_CHANGED_BASIC_RATES |
9729 						  BSS_CHANGED_BEACON_INT);
9730 
9731 		if (assoc)
9732 			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
9733 
9734 		err = sta_info_insert(new_sta);
9735 		new_sta = NULL;
9736 		if (err) {
9737 			sdata_info(sdata,
9738 				   "failed to insert STA entry for the AP (error %d)\n",
9739 				   err);
9740 			goto out_release_chan;
9741 		}
9742 	} else
9743 		WARN_ON_ONCE(!ether_addr_equal(link->u.mgd.bssid, cbss->bssid));
9744 
9745 	/* Cancel scan to ensure that nothing interferes with connection */
9746 	if (local->scanning)
9747 		ieee80211_scan_cancel(local);
9748 
9749 	return 0;
9750 
9751 out_release_chan:
9752 	ieee80211_link_release_channel(link);
9753 out_err:
9754 	if (mlo && have_sta)
9755 		WARN_ON(__sta_info_destroy(have_sta));
9756 	ieee80211_vif_set_links(sdata, 0, 0);
9757 	return err;
9758 }
9759 
9760 static bool ieee80211_mgd_csa_present(struct ieee80211_sub_if_data *sdata,
9761 				      const struct cfg80211_bss_ies *ies,
9762 				      u8 cur_channel, bool ignore_ecsa)
9763 {
9764 	const struct element *csa_elem, *ecsa_elem;
9765 	struct ieee80211_channel_sw_ie *csa = NULL;
9766 	struct ieee80211_ext_chansw_ie *ecsa = NULL;
9767 
9768 	if (!ies)
9769 		return false;
9770 
9771 	csa_elem = cfg80211_find_elem(WLAN_EID_CHANNEL_SWITCH,
9772 				      ies->data, ies->len);
9773 	if (csa_elem && csa_elem->datalen == sizeof(*csa))
9774 		csa = (void *)csa_elem->data;
9775 
9776 	ecsa_elem = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
9777 				       ies->data, ies->len);
9778 	if (ecsa_elem && ecsa_elem->datalen == sizeof(*ecsa))
9779 		ecsa = (void *)ecsa_elem->data;
9780 
9781 	if (csa && csa->count == 0)
9782 		csa = NULL;
9783 	if (csa && !csa->mode && csa->new_ch_num == cur_channel)
9784 		csa = NULL;
9785 
9786 	if (ecsa && ecsa->count == 0)
9787 		ecsa = NULL;
9788 	if (ecsa && !ecsa->mode && ecsa->new_ch_num == cur_channel)
9789 		ecsa = NULL;
9790 
9791 	if (ignore_ecsa && ecsa) {
9792 		sdata_info(sdata,
9793 			   "Ignoring ECSA in probe response - was considered stuck!\n");
9794 		return csa;
9795 	}
9796 
9797 	return csa || ecsa;
9798 }
9799 
9800 static bool ieee80211_mgd_csa_in_process(struct ieee80211_sub_if_data *sdata,
9801 					 struct cfg80211_bss *bss)
9802 {
9803 	u8 cur_channel;
9804 	bool ret;
9805 
9806 	cur_channel = ieee80211_frequency_to_channel(bss->channel->center_freq);
9807 
9808 	rcu_read_lock();
9809 	if (ieee80211_mgd_csa_present(sdata,
9810 				      rcu_dereference(bss->beacon_ies),
9811 				      cur_channel, false)) {
9812 		ret = true;
9813 		goto out;
9814 	}
9815 
9816 	if (ieee80211_mgd_csa_present(sdata,
9817 				      rcu_dereference(bss->proberesp_ies),
9818 				      cur_channel, bss->proberesp_ecsa_stuck)) {
9819 		ret = true;
9820 		goto out;
9821 	}
9822 
9823 	ret = false;
9824 out:
9825 	rcu_read_unlock();
9826 	return ret;
9827 }
9828 
9829 static void ieee80211_parse_cfg_selectors(unsigned long *userspace_selectors,
9830 					  const u8 *supported_selectors,
9831 					  u8 supported_selectors_len)
9832 {
9833 	if (supported_selectors) {
9834 		for (int i = 0; i < supported_selectors_len; i++) {
9835 			set_bit(supported_selectors[i],
9836 				userspace_selectors);
9837 		}
9838 	} else {
9839 		/* Assume SAE_H2E support for backward compatibility. */
9840 		set_bit(BSS_MEMBERSHIP_SELECTOR_SAE_H2E,
9841 			userspace_selectors);
9842 	}
9843 }
9844 
9845 /* config hooks */
9846 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
9847 		       struct cfg80211_auth_request *req)
9848 {
9849 	struct ieee80211_local *local = sdata->local;
9850 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9851 	struct ieee80211_mgd_auth_data *auth_data;
9852 	struct ieee80211_conn_settings conn;
9853 	struct ieee80211_link_data *link;
9854 	struct ieee80211_supported_band *sband;
9855 	struct ieee80211_bss *bss;
9856 	u16 auth_alg;
9857 	int err;
9858 	bool cont_auth, wmm_used;
9859 
9860 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
9861 
9862 	/* prepare auth data structure */
9863 
9864 	switch (req->auth_type) {
9865 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
9866 		auth_alg = WLAN_AUTH_OPEN;
9867 		break;
9868 	case NL80211_AUTHTYPE_SHARED_KEY:
9869 		if (fips_enabled)
9870 			return -EOPNOTSUPP;
9871 		auth_alg = WLAN_AUTH_SHARED_KEY;
9872 		break;
9873 	case NL80211_AUTHTYPE_FT:
9874 		auth_alg = WLAN_AUTH_FT;
9875 		break;
9876 	case NL80211_AUTHTYPE_NETWORK_EAP:
9877 		auth_alg = WLAN_AUTH_LEAP;
9878 		break;
9879 	case NL80211_AUTHTYPE_SAE:
9880 		auth_alg = WLAN_AUTH_SAE;
9881 		break;
9882 	case NL80211_AUTHTYPE_FILS_SK:
9883 		auth_alg = WLAN_AUTH_FILS_SK;
9884 		break;
9885 	case NL80211_AUTHTYPE_FILS_SK_PFS:
9886 		auth_alg = WLAN_AUTH_FILS_SK_PFS;
9887 		break;
9888 	case NL80211_AUTHTYPE_FILS_PK:
9889 		auth_alg = WLAN_AUTH_FILS_PK;
9890 		break;
9891 	case NL80211_AUTHTYPE_EPPKE:
9892 		auth_alg = WLAN_AUTH_EPPKE;
9893 		break;
9894 	case NL80211_AUTHTYPE_IEEE8021X:
9895 		auth_alg = WLAN_AUTH_IEEE8021X;
9896 		break;
9897 	default:
9898 		return -EOPNOTSUPP;
9899 	}
9900 
9901 	if (ifmgd->assoc_data)
9902 		return -EBUSY;
9903 
9904 	if (ieee80211_mgd_csa_in_process(sdata, req->bss)) {
9905 		sdata_info(sdata, "AP is in CSA process, reject auth\n");
9906 		return -EINVAL;
9907 	}
9908 
9909 	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
9910 			    req->ie_len, GFP_KERNEL);
9911 	if (!auth_data)
9912 		return -ENOMEM;
9913 
9914 	memcpy(auth_data->ap_addr,
9915 	       req->ap_mld_addr ?: req->bss->bssid,
9916 	       ETH_ALEN);
9917 	auth_data->bss = req->bss;
9918 	auth_data->link_id = req->link_id;
9919 
9920 	if (req->auth_data_len >= 4) {
9921 		if (req->auth_type == NL80211_AUTHTYPE_SAE ||
9922 		    req->auth_type == NL80211_AUTHTYPE_EPPKE ||
9923 		    req->auth_type == NL80211_AUTHTYPE_IEEE8021X) {
9924 			__le16 *pos = (__le16 *) req->auth_data;
9925 
9926 			auth_data->trans = le16_to_cpu(pos[0]);
9927 			auth_data->status = le16_to_cpu(pos[1]);
9928 		}
9929 
9930 		memcpy(auth_data->data, req->auth_data + 4,
9931 		       req->auth_data_len - 4);
9932 		auth_data->data_len += req->auth_data_len - 4;
9933 	}
9934 
9935 	/* Check if continuing authentication or trying to authenticate with the
9936 	 * same BSS that we were in the process of authenticating with and avoid
9937 	 * removal and re-addition of the STA entry in
9938 	 * ieee80211_prep_connection().
9939 	 */
9940 	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss &&
9941 		    ifmgd->auth_data->link_id == req->link_id;
9942 
9943 	if (req->ie && req->ie_len) {
9944 		memcpy(&auth_data->data[auth_data->data_len],
9945 		       req->ie, req->ie_len);
9946 		auth_data->data_len += req->ie_len;
9947 	}
9948 
9949 	if (req->key && req->key_len) {
9950 		auth_data->key_len = req->key_len;
9951 		auth_data->key_idx = req->key_idx;
9952 		memcpy(auth_data->key, req->key, req->key_len);
9953 	}
9954 
9955 	ieee80211_parse_cfg_selectors(auth_data->userspace_selectors,
9956 				      req->supported_selectors,
9957 				      req->supported_selectors_len);
9958 
9959 	auth_data->algorithm = auth_alg;
9960 
9961 	/* try to authenticate/probe */
9962 
9963 	if (ifmgd->auth_data) {
9964 		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
9965 			auth_data->peer_confirmed =
9966 				ifmgd->auth_data->peer_confirmed;
9967 		}
9968 		ieee80211_destroy_auth_data(sdata, cont_auth);
9969 	}
9970 
9971 	/* prep auth_data so we don't go into idle on disassoc */
9972 	ifmgd->auth_data = auth_data;
9973 
9974 	/* If this is continuation of an ongoing SAE authentication exchange
9975 	 * (i.e., request to send SAE Confirm) and the peer has already
9976 	 * confirmed, mark authentication completed since we are about to send
9977 	 * out SAE Confirm.
9978 	 */
9979 	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
9980 	    auth_data->peer_confirmed && auth_data->trans == 2)
9981 		ieee80211_mark_sta_auth(sdata);
9982 
9983 	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_EPPKE &&
9984 	    auth_data->trans == 3)
9985 		ieee80211_mark_sta_auth(sdata);
9986 
9987 	if (ifmgd->associated) {
9988 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9989 
9990 		sdata_info(sdata,
9991 			   "disconnect from AP %pM for new auth to %pM\n",
9992 			   sdata->vif.cfg.ap_addr, auth_data->ap_addr);
9993 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
9994 				       WLAN_REASON_UNSPECIFIED,
9995 				       false, frame_buf);
9996 
9997 		ieee80211_report_disconnect(sdata, frame_buf,
9998 					    sizeof(frame_buf), true,
9999 					    WLAN_REASON_UNSPECIFIED,
10000 					    false);
10001 	}
10002 
10003 	/* needed for transmitting the auth frame(s) properly */
10004 	memcpy(sdata->vif.cfg.ap_addr, auth_data->ap_addr, ETH_ALEN);
10005 
10006 	bss = (void *)req->bss->priv;
10007 	wmm_used = bss->wmm_used && (local->hw.queues >= IEEE80211_NUM_ACS);
10008 
10009 	sband = local->hw.wiphy->bands[req->bss->channel->band];
10010 
10011 	ieee80211_determine_our_sta_mode_auth(sdata, sband, req, wmm_used,
10012 					      &conn);
10013 
10014 	err = ieee80211_prep_connection(sdata, req->bss, req->link_id,
10015 					req->ap_mld_addr, cont_auth,
10016 					&conn, false,
10017 					auth_data->userspace_selectors);
10018 	if (err)
10019 		goto err_clear;
10020 
10021 	if (req->link_id >= 0)
10022 		link = sdata_dereference(sdata->link[req->link_id], sdata);
10023 	else
10024 		link = &sdata->deflink;
10025 
10026 	if (WARN_ON(!link)) {
10027 		err = -ENOLINK;
10028 		goto err_clear;
10029 	}
10030 
10031 	sdata_info(sdata, "authenticate with %pM (local address=%pM)\n",
10032 		   auth_data->ap_addr, link->conf->addr);
10033 
10034 	err = ieee80211_auth(sdata);
10035 	if (err) {
10036 		sta_info_destroy_addr(sdata, auth_data->ap_addr);
10037 		goto err_clear;
10038 	}
10039 
10040 	/* hold our own reference */
10041 	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
10042 	return 0;
10043 
10044  err_clear:
10045 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
10046 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
10047 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
10048 						  BSS_CHANGED_BSSID);
10049 		ieee80211_link_release_channel(&sdata->deflink);
10050 	}
10051 	ifmgd->auth_data = NULL;
10052 	kfree(auth_data);
10053 	return err;
10054 }
10055 
10056 static void
10057 ieee80211_setup_assoc_link(struct ieee80211_sub_if_data *sdata,
10058 			   struct ieee80211_mgd_assoc_data *assoc_data,
10059 			   struct cfg80211_assoc_request *req,
10060 			   struct ieee80211_conn_settings *conn,
10061 			   unsigned int link_id)
10062 {
10063 	struct ieee80211_local *local = sdata->local;
10064 	const struct cfg80211_bss_ies *bss_ies;
10065 	struct ieee80211_supported_band *sband;
10066 	struct ieee80211_link_data *link;
10067 	struct cfg80211_bss *cbss;
10068 	struct ieee80211_bss *bss;
10069 
10070 	cbss = assoc_data->link[link_id].bss;
10071 	if (WARN_ON(!cbss))
10072 		return;
10073 
10074 	bss = (void *)cbss->priv;
10075 
10076 	sband = local->hw.wiphy->bands[cbss->channel->band];
10077 	if (WARN_ON(!sband))
10078 		return;
10079 
10080 	link = sdata_dereference(sdata->link[link_id], sdata);
10081 	if (WARN_ON(!link))
10082 		return;
10083 
10084 	/* for MLO connections assume advertising all rates is OK */
10085 	if (!req->ap_mld_addr) {
10086 		assoc_data->supp_rates = bss->supp_rates;
10087 		assoc_data->supp_rates_len = bss->supp_rates_len;
10088 	}
10089 
10090 	/* copy and link elems for the STA profile */
10091 	if (req->links[link_id].elems_len) {
10092 		memcpy(assoc_data->ie_pos, req->links[link_id].elems,
10093 		       req->links[link_id].elems_len);
10094 		assoc_data->link[link_id].elems = assoc_data->ie_pos;
10095 		assoc_data->link[link_id].elems_len = req->links[link_id].elems_len;
10096 		assoc_data->ie_pos += req->links[link_id].elems_len;
10097 	}
10098 
10099 	link->u.mgd.beacon_crc_valid = false;
10100 	link->u.mgd.dtim_period = 0;
10101 	link->u.mgd.have_beacon = false;
10102 
10103 	/* override HT configuration only if the AP and we support it */
10104 	if (conn->mode >= IEEE80211_CONN_MODE_HT) {
10105 		struct ieee80211_sta_ht_cap sta_ht_cap;
10106 
10107 		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
10108 		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
10109 	}
10110 
10111 	rcu_read_lock();
10112 	bss_ies = rcu_dereference(cbss->beacon_ies);
10113 	if (bss_ies) {
10114 		u8 dtim_count = 0;
10115 
10116 		ieee80211_get_dtim(bss_ies, &dtim_count,
10117 				   &link->u.mgd.dtim_period);
10118 
10119 		sdata->deflink.u.mgd.have_beacon = true;
10120 
10121 		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
10122 			link->conf->sync_tsf = bss_ies->tsf;
10123 			link->conf->sync_device_ts = bss->device_ts_beacon;
10124 			link->conf->sync_dtim_count = dtim_count;
10125 		}
10126 	} else {
10127 		bss_ies = rcu_dereference(cbss->ies);
10128 	}
10129 
10130 	if (bss_ies) {
10131 		const struct element *elem;
10132 
10133 		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
10134 					      bss_ies->data, bss_ies->len);
10135 		if (elem && elem->datalen >= 3)
10136 			link->conf->profile_periodicity = elem->data[2];
10137 		else
10138 			link->conf->profile_periodicity = 0;
10139 
10140 		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
10141 					  bss_ies->data, bss_ies->len);
10142 		if (elem && elem->datalen >= 11 &&
10143 		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
10144 			link->conf->ema_ap = true;
10145 		else
10146 			link->conf->ema_ap = false;
10147 	}
10148 	rcu_read_unlock();
10149 
10150 	if (bss->corrupt_data) {
10151 		char *corrupt_type = "data";
10152 
10153 		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
10154 			if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
10155 				corrupt_type = "beacon and probe response";
10156 			else
10157 				corrupt_type = "beacon";
10158 		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) {
10159 			corrupt_type = "probe response";
10160 		}
10161 		sdata_info(sdata, "associating to AP %pM with corrupt %s\n",
10162 			   cbss->bssid, corrupt_type);
10163 	}
10164 
10165 	if (link->u.mgd.req_smps == IEEE80211_SMPS_AUTOMATIC) {
10166 		if (sdata->u.mgd.powersave)
10167 			link->smps_mode = IEEE80211_SMPS_DYNAMIC;
10168 		else
10169 			link->smps_mode = IEEE80211_SMPS_OFF;
10170 	} else {
10171 		link->smps_mode = link->u.mgd.req_smps;
10172 	}
10173 }
10174 
10175 static int
10176 ieee80211_mgd_get_ap_ht_vht_capa(struct ieee80211_sub_if_data *sdata,
10177 				 struct ieee80211_mgd_assoc_data *assoc_data,
10178 				 int link_id)
10179 {
10180 	struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
10181 	enum nl80211_band band = cbss->channel->band;
10182 	struct ieee80211_supported_band *sband;
10183 	const struct element *elem;
10184 	int err;
10185 
10186 	/* neither HT nor VHT elements used on 6 GHz */
10187 	if (band == NL80211_BAND_6GHZ)
10188 		return 0;
10189 
10190 	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_HT)
10191 		return 0;
10192 
10193 	rcu_read_lock();
10194 	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_OPERATION);
10195 	if (!elem || elem->datalen < sizeof(struct ieee80211_ht_operation)) {
10196 		mlme_link_id_dbg(sdata, link_id, "no HT operation on BSS %pM\n",
10197 				 cbss->bssid);
10198 		err = -EINVAL;
10199 		goto out_rcu;
10200 	}
10201 	assoc_data->link[link_id].ap_ht_param =
10202 		((struct ieee80211_ht_operation *)(elem->data))->ht_param;
10203 	rcu_read_unlock();
10204 
10205 	if (assoc_data->link[link_id].conn.mode < IEEE80211_CONN_MODE_VHT)
10206 		return 0;
10207 
10208 	/* some drivers want to support VHT on 2.4 GHz even */
10209 	sband = sdata->local->hw.wiphy->bands[band];
10210 	if (!sband->vht_cap.vht_supported)
10211 		return 0;
10212 
10213 	rcu_read_lock();
10214 	elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
10215 	/* but even then accept it not being present on the AP */
10216 	if (!elem && band == NL80211_BAND_2GHZ) {
10217 		err = 0;
10218 		goto out_rcu;
10219 	}
10220 	if (!elem || elem->datalen < sizeof(struct ieee80211_vht_cap)) {
10221 		mlme_link_id_dbg(sdata, link_id, "no VHT capa on BSS %pM\n",
10222 				 cbss->bssid);
10223 		err = -EINVAL;
10224 		goto out_rcu;
10225 	}
10226 	memcpy(&assoc_data->link[link_id].ap_vht_cap, elem->data,
10227 	       sizeof(struct ieee80211_vht_cap));
10228 	rcu_read_unlock();
10229 
10230 	return 0;
10231 out_rcu:
10232 	rcu_read_unlock();
10233 	return err;
10234 }
10235 
10236 static bool
10237 ieee80211_mgd_assoc_bss_has_mld_ext_capa_ops(struct cfg80211_assoc_request *req)
10238 {
10239 	const struct cfg80211_bss_ies *ies;
10240 	struct cfg80211_bss *bss;
10241 	const struct element *ml;
10242 
10243 	/* not an MLO connection if link_id < 0, so irrelevant */
10244 	if (req->link_id < 0)
10245 		return false;
10246 
10247 	bss = req->links[req->link_id].bss;
10248 
10249 	guard(rcu)();
10250 	ies = rcu_dereference(bss->ies);
10251 	for_each_element_extid(ml, WLAN_EID_EXT_EHT_MULTI_LINK,
10252 			       ies->data, ies->len) {
10253 		const struct ieee80211_multi_link_elem *mle;
10254 
10255 		if (!ieee80211_mle_type_ok(ml->data + 1,
10256 					   IEEE80211_ML_CONTROL_TYPE_BASIC,
10257 					   ml->datalen - 1))
10258 			continue;
10259 
10260 		mle = (void *)(ml->data + 1);
10261 		if (mle->control & cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP))
10262 			return true;
10263 	}
10264 
10265 	return false;
10266 
10267 }
10268 
10269 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
10270 			struct cfg80211_assoc_request *req)
10271 {
10272 	unsigned int assoc_link_id = req->link_id < 0 ? 0 : req->link_id;
10273 	struct ieee80211_local *local = sdata->local;
10274 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10275 	struct ieee80211_mgd_assoc_data *assoc_data;
10276 	const struct element *ssid_elem;
10277 	struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
10278 	const struct wiphy_iftype_ext_capab *ift_ext_capa;
10279 	struct ieee80211_link_data *link;
10280 	u16 driver_ext_mld_capa_ops = 0;
10281 	struct cfg80211_bss *cbss;
10282 	bool override, uapsd_supported;
10283 	bool match_auth;
10284 	int i, err;
10285 	size_t size = sizeof(*assoc_data) + req->ie_len;
10286 
10287 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
10288 		size += req->links[i].elems_len;
10289 
10290 	assoc_data = kzalloc(size, GFP_KERNEL);
10291 	if (!assoc_data)
10292 		return -ENOMEM;
10293 
10294 	cbss = req->link_id < 0 ? req->bss : req->links[req->link_id].bss;
10295 
10296 	if (ieee80211_mgd_csa_in_process(sdata, cbss)) {
10297 		sdata_info(sdata, "AP is in CSA process, reject assoc\n");
10298 		err = -EINVAL;
10299 		goto err_free;
10300 	}
10301 
10302 	rcu_read_lock();
10303 	ssid_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
10304 	if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
10305 		rcu_read_unlock();
10306 		err = -EINVAL;
10307 		goto err_free;
10308 	}
10309 
10310 	memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
10311 	assoc_data->ssid_len = ssid_elem->datalen;
10312 	rcu_read_unlock();
10313 
10314 	if (req->ap_mld_addr)
10315 		memcpy(assoc_data->ap_addr, req->ap_mld_addr, ETH_ALEN);
10316 	else
10317 		memcpy(assoc_data->ap_addr, cbss->bssid, ETH_ALEN);
10318 
10319 	ift_ext_capa = cfg80211_get_iftype_ext_capa(local->hw.wiphy,
10320 						    ieee80211_vif_type_p2p(&sdata->vif));
10321 	if (ift_ext_capa)
10322 		driver_ext_mld_capa_ops = ift_ext_capa->ext_mld_capa_and_ops;
10323 
10324 	/*
10325 	 * Many APs have broken parsing of the extended MLD capa/ops field,
10326 	 * dropping (re-)association request frames or replying with association
10327 	 * response with a failure status if it's present.
10328 	 * Set our value from the userspace request only in strict mode or if
10329 	 * the AP also had that field present.
10330 	 * For UHR we may want to advertise ML-PM (per driver_ext_mld_capa_ops)
10331 	 * but if the AP doesn't have it then it's pointless, and if it does
10332 	 * then it has to have the extended MLD capa/ops field.
10333 	 */
10334 	if (ieee80211_hw_check(&local->hw, STRICT) ||
10335 	    ieee80211_mgd_assoc_bss_has_mld_ext_capa_ops(req))
10336 		assoc_data->ext_mld_capa_ops =
10337 			cpu_to_le16(req->ext_mld_capa_ops |
10338 				    driver_ext_mld_capa_ops);
10339 
10340 	if (ifmgd->associated) {
10341 		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
10342 
10343 		sdata_info(sdata,
10344 			   "disconnect from AP %pM for new assoc to %pM\n",
10345 			   sdata->vif.cfg.ap_addr, assoc_data->ap_addr);
10346 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
10347 				       WLAN_REASON_UNSPECIFIED,
10348 				       false, frame_buf);
10349 
10350 		ieee80211_report_disconnect(sdata, frame_buf,
10351 					    sizeof(frame_buf), true,
10352 					    WLAN_REASON_UNSPECIFIED,
10353 					    false);
10354 	}
10355 
10356 	memset(sdata->u.mgd.userspace_selectors, 0,
10357 	       sizeof(sdata->u.mgd.userspace_selectors));
10358 	ieee80211_parse_cfg_selectors(sdata->u.mgd.userspace_selectors,
10359 				      req->supported_selectors,
10360 				      req->supported_selectors_len);
10361 
10362 	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
10363 	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
10364 	       sizeof(ifmgd->ht_capa_mask));
10365 
10366 	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
10367 	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
10368 	       sizeof(ifmgd->vht_capa_mask));
10369 
10370 	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
10371 	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
10372 	       sizeof(ifmgd->s1g_capa_mask));
10373 
10374 	/* keep some setup (AP STA, channel, ...) if matching */
10375 	match_auth = ifmgd->auth_data &&
10376 		     ether_addr_equal(ifmgd->auth_data->ap_addr,
10377 				      assoc_data->ap_addr) &&
10378 		     ifmgd->auth_data->link_id == req->link_id;
10379 
10380 	if (req->ap_mld_addr) {
10381 		uapsd_supported = true;
10382 
10383 		if (req->flags & (ASSOC_REQ_DISABLE_HT |
10384 				  ASSOC_REQ_DISABLE_VHT |
10385 				  ASSOC_REQ_DISABLE_HE |
10386 				  ASSOC_REQ_DISABLE_EHT)) {
10387 			err = -EINVAL;
10388 			goto err_free;
10389 		}
10390 
10391 		for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
10392 			struct ieee80211_supported_band *sband;
10393 			struct cfg80211_bss *link_cbss = req->links[i].bss;
10394 			struct ieee80211_bss *bss;
10395 
10396 			if (!link_cbss)
10397 				continue;
10398 
10399 			bss = (void *)link_cbss->priv;
10400 
10401 			if (!bss->wmm_used) {
10402 				err = -EINVAL;
10403 				req->links[i].error = err;
10404 				goto err_free;
10405 			}
10406 
10407 			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
10408 				err = -EINVAL;
10409 				req->links[i].error = err;
10410 				goto err_free;
10411 			}
10412 
10413 			link = sdata_dereference(sdata->link[i], sdata);
10414 			if (link)
10415 				ether_addr_copy(assoc_data->link[i].addr,
10416 						link->conf->addr);
10417 			else
10418 				eth_random_addr(assoc_data->link[i].addr);
10419 			sband = local->hw.wiphy->bands[link_cbss->channel->band];
10420 
10421 			if (match_auth && i == assoc_link_id && link)
10422 				assoc_data->link[i].conn = link->u.mgd.conn;
10423 			else
10424 				assoc_data->link[i].conn =
10425 					ieee80211_conn_settings_unlimited;
10426 			ieee80211_determine_our_sta_mode_assoc(sdata, sband,
10427 							       req, true, i,
10428 							       &assoc_data->link[i].conn);
10429 			assoc_data->link[i].bss = link_cbss;
10430 
10431 			if (!bss->uapsd_supported)
10432 				uapsd_supported = false;
10433 
10434 			if (assoc_data->link[i].conn.mode < IEEE80211_CONN_MODE_EHT) {
10435 				err = -EINVAL;
10436 				req->links[i].error = err;
10437 				goto err_free;
10438 			}
10439 
10440 			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata,
10441 							       assoc_data, i);
10442 			if (err) {
10443 				err = -EINVAL;
10444 				req->links[i].error = err;
10445 				goto err_free;
10446 			}
10447 		}
10448 
10449 		assoc_data->wmm = true;
10450 	} else {
10451 		struct ieee80211_supported_band *sband;
10452 		struct ieee80211_bss *bss = (void *)cbss->priv;
10453 
10454 		memcpy(assoc_data->link[0].addr, sdata->vif.addr, ETH_ALEN);
10455 		assoc_data->s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
10456 
10457 		assoc_data->wmm = bss->wmm_used &&
10458 				  (local->hw.queues >= IEEE80211_NUM_ACS);
10459 
10460 		if (cbss->channel->band == NL80211_BAND_6GHZ &&
10461 		    req->flags & (ASSOC_REQ_DISABLE_HT |
10462 				  ASSOC_REQ_DISABLE_VHT |
10463 				  ASSOC_REQ_DISABLE_HE)) {
10464 			err = -EINVAL;
10465 			goto err_free;
10466 		}
10467 
10468 		sband = local->hw.wiphy->bands[cbss->channel->band];
10469 
10470 		assoc_data->link[0].bss = cbss;
10471 
10472 		if (match_auth)
10473 			assoc_data->link[0].conn = sdata->deflink.u.mgd.conn;
10474 		else
10475 			assoc_data->link[0].conn =
10476 				ieee80211_conn_settings_unlimited;
10477 		ieee80211_determine_our_sta_mode_assoc(sdata, sband, req,
10478 						       assoc_data->wmm, 0,
10479 						       &assoc_data->link[0].conn);
10480 
10481 		uapsd_supported = bss->uapsd_supported;
10482 
10483 		err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, assoc_data, 0);
10484 		if (err)
10485 			goto err_free;
10486 	}
10487 
10488 	assoc_data->spp_amsdu = req->flags & ASSOC_REQ_SPP_AMSDU;
10489 
10490 	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
10491 		err = -EBUSY;
10492 		goto err_free;
10493 	}
10494 
10495 	if (ifmgd->assoc_data) {
10496 		err = -EBUSY;
10497 		goto err_free;
10498 	}
10499 
10500 	/* Cleanup is delayed if auth_data matches */
10501 	if (ifmgd->auth_data && !match_auth)
10502 		ieee80211_destroy_auth_data(sdata, false);
10503 
10504 	if (req->ie && req->ie_len) {
10505 		memcpy(assoc_data->ie, req->ie, req->ie_len);
10506 		assoc_data->ie_len = req->ie_len;
10507 		assoc_data->ie_pos = assoc_data->ie + assoc_data->ie_len;
10508 	} else {
10509 		assoc_data->ie_pos = assoc_data->ie;
10510 	}
10511 
10512 	if (req->fils_kek) {
10513 		/* should already be checked in cfg80211 - so warn */
10514 		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
10515 			err = -EINVAL;
10516 			goto err_free;
10517 		}
10518 		memcpy(assoc_data->fils_kek, req->fils_kek,
10519 		       req->fils_kek_len);
10520 		assoc_data->fils_kek_len = req->fils_kek_len;
10521 	}
10522 
10523 	if (req->fils_nonces)
10524 		memcpy(assoc_data->fils_nonces, req->fils_nonces,
10525 		       2 * FILS_NONCE_LEN);
10526 
10527 	/* default timeout */
10528 	assoc_data->timeout = jiffies;
10529 	assoc_data->timeout_started = true;
10530 
10531 	assoc_data->assoc_link_id = assoc_link_id;
10532 
10533 	if (req->ap_mld_addr) {
10534 		/* if there was no authentication, set up the link */
10535 		err = ieee80211_vif_set_links(sdata, BIT(assoc_link_id), 0);
10536 		if (err)
10537 			goto err_clear;
10538 	}
10539 
10540 	link = sdata_dereference(sdata->link[assoc_link_id], sdata);
10541 	if (WARN_ON(!link)) {
10542 		err = -EINVAL;
10543 		goto err_clear;
10544 	}
10545 
10546 	override = link->u.mgd.conn.mode !=
10547 			assoc_data->link[assoc_link_id].conn.mode ||
10548 		   link->u.mgd.conn.bw_limit !=
10549 			assoc_data->link[assoc_link_id].conn.bw_limit;
10550 	link->u.mgd.conn = assoc_data->link[assoc_link_id].conn;
10551 
10552 	ieee80211_setup_assoc_link(sdata, assoc_data, req, &link->u.mgd.conn,
10553 				   assoc_link_id);
10554 
10555 	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
10556 		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
10557 	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
10558 		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
10559 
10560 	if (assoc_data->wmm && uapsd_supported &&
10561 	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
10562 		assoc_data->uapsd = true;
10563 		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
10564 	} else {
10565 		assoc_data->uapsd = false;
10566 		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
10567 	}
10568 
10569 	if (req->prev_bssid)
10570 		memcpy(assoc_data->prev_ap_addr, req->prev_bssid, ETH_ALEN);
10571 
10572 	if (req->use_mfp) {
10573 		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
10574 		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
10575 	} else {
10576 		ifmgd->mfp = IEEE80211_MFP_DISABLED;
10577 		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
10578 	}
10579 
10580 	if (req->flags & ASSOC_REQ_USE_RRM)
10581 		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
10582 	else
10583 		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
10584 
10585 	if (req->crypto.control_port)
10586 		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
10587 	else
10588 		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
10589 
10590 	sdata->control_port_protocol = req->crypto.control_port_ethertype;
10591 	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
10592 	sdata->control_port_over_nl80211 =
10593 					req->crypto.control_port_over_nl80211;
10594 	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
10595 
10596 	/* kick off associate process */
10597 	ifmgd->assoc_data = assoc_data;
10598 
10599 	for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) {
10600 		if (!assoc_data->link[i].bss)
10601 			continue;
10602 		if (i == assoc_data->assoc_link_id)
10603 			continue;
10604 		/* only calculate the mode, hence link/link_sta == NULL */
10605 		err = ieee80211_prep_channel(sdata, NULL, NULL, i,
10606 					     assoc_data->link[i].bss, true,
10607 					     &assoc_data->link[i].conn,
10608 					     sdata->u.mgd.userspace_selectors);
10609 		if (err) {
10610 			req->links[i].error = err;
10611 			goto err_clear;
10612 		}
10613 	}
10614 
10615 	memcpy(vif_cfg->ssid, assoc_data->ssid, assoc_data->ssid_len);
10616 	vif_cfg->ssid_len = assoc_data->ssid_len;
10617 
10618 	/* needed for transmitting the assoc frames properly */
10619 	memcpy(sdata->vif.cfg.ap_addr, assoc_data->ap_addr, ETH_ALEN);
10620 
10621 	err = ieee80211_prep_connection(sdata, cbss, req->link_id,
10622 					req->ap_mld_addr, true,
10623 					&assoc_data->link[assoc_link_id].conn,
10624 					override,
10625 					sdata->u.mgd.userspace_selectors);
10626 	if (err)
10627 		goto err_clear;
10628 
10629 	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC)) {
10630 		const struct cfg80211_bss_ies *beacon_ies;
10631 
10632 		rcu_read_lock();
10633 		beacon_ies = rcu_dereference(req->bss->beacon_ies);
10634 		if (!beacon_ies) {
10635 			/*
10636 			 * Wait up to one beacon interval ...
10637 			 * should this be more if we miss one?
10638 			 */
10639 			sdata_info(sdata, "waiting for beacon from %pM\n",
10640 				   link->u.mgd.bssid);
10641 			assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
10642 			assoc_data->timeout_started = true;
10643 			assoc_data->need_beacon = true;
10644 		}
10645 		rcu_read_unlock();
10646 	}
10647 
10648 	run_again(sdata, assoc_data->timeout);
10649 
10650 	/* We are associating, clean up auth_data */
10651 	if (ifmgd->auth_data)
10652 		ieee80211_destroy_auth_data(sdata, true);
10653 
10654 	return 0;
10655  err_clear:
10656 	if (!ifmgd->auth_data) {
10657 		eth_zero_addr(sdata->deflink.u.mgd.bssid);
10658 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
10659 						  BSS_CHANGED_BSSID);
10660 	}
10661 	ifmgd->assoc_data = NULL;
10662  err_free:
10663 	kfree(assoc_data);
10664 	return err;
10665 }
10666 
10667 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
10668 			 struct cfg80211_deauth_request *req)
10669 {
10670 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10671 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
10672 	bool tx = !req->local_state_change;
10673 	struct ieee80211_prep_tx_info info = {
10674 		.subtype = IEEE80211_STYPE_DEAUTH,
10675 	};
10676 
10677 	if (ifmgd->auth_data &&
10678 	    ether_addr_equal(ifmgd->auth_data->ap_addr, req->bssid)) {
10679 		sdata_info(sdata,
10680 			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
10681 			   req->bssid, req->reason_code,
10682 			   ieee80211_get_reason_code_string(req->reason_code));
10683 
10684 		info.link_id = ifmgd->auth_data->link_id;
10685 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
10686 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
10687 					       IEEE80211_STYPE_DEAUTH,
10688 					       req->reason_code, tx,
10689 					       frame_buf);
10690 		ieee80211_destroy_auth_data(sdata, false);
10691 		ieee80211_report_disconnect(sdata, frame_buf,
10692 					    sizeof(frame_buf), true,
10693 					    req->reason_code, false);
10694 		drv_mgd_complete_tx(sdata->local, sdata, &info);
10695 		return 0;
10696 	}
10697 
10698 	if (ifmgd->assoc_data &&
10699 	    ether_addr_equal(ifmgd->assoc_data->ap_addr, req->bssid)) {
10700 		sdata_info(sdata,
10701 			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
10702 			   req->bssid, req->reason_code,
10703 			   ieee80211_get_reason_code_string(req->reason_code));
10704 
10705 		info.link_id = ifmgd->assoc_data->assoc_link_id;
10706 		drv_mgd_prepare_tx(sdata->local, sdata, &info);
10707 		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
10708 					       IEEE80211_STYPE_DEAUTH,
10709 					       req->reason_code, tx,
10710 					       frame_buf);
10711 		ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
10712 		ieee80211_report_disconnect(sdata, frame_buf,
10713 					    sizeof(frame_buf), true,
10714 					    req->reason_code, false);
10715 		drv_mgd_complete_tx(sdata->local, sdata, &info);
10716 		return 0;
10717 	}
10718 
10719 	if (ifmgd->associated &&
10720 	    ether_addr_equal(sdata->vif.cfg.ap_addr, req->bssid)) {
10721 		sdata_info(sdata,
10722 			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
10723 			   req->bssid, req->reason_code,
10724 			   ieee80211_get_reason_code_string(req->reason_code));
10725 
10726 		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
10727 				       req->reason_code, tx, frame_buf);
10728 		ieee80211_report_disconnect(sdata, frame_buf,
10729 					    sizeof(frame_buf), true,
10730 					    req->reason_code, false);
10731 		return 0;
10732 	}
10733 
10734 	return -ENOTCONN;
10735 }
10736 
10737 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
10738 			   struct cfg80211_disassoc_request *req)
10739 {
10740 	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
10741 
10742 	if (!sdata->u.mgd.associated ||
10743 	    memcmp(sdata->vif.cfg.ap_addr, req->ap_addr, ETH_ALEN))
10744 		return -ENOTCONN;
10745 
10746 	sdata_info(sdata,
10747 		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
10748 		   req->ap_addr, req->reason_code,
10749 		   ieee80211_get_reason_code_string(req->reason_code));
10750 
10751 	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
10752 			       req->reason_code, !req->local_state_change,
10753 			       frame_buf);
10754 
10755 	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
10756 				    req->reason_code, false);
10757 
10758 	return 0;
10759 }
10760 
10761 void ieee80211_mgd_stop_link(struct ieee80211_link_data *link)
10762 {
10763 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
10764 			  &link->u.mgd.request_smps_work);
10765 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
10766 			  &link->u.mgd.recalc_smps);
10767 	wiphy_hrtimer_work_cancel(link->sdata->local->hw.wiphy,
10768 				  &link->u.mgd.csa.switch_work);
10769 }
10770 
10771 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
10772 {
10773 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10774 
10775 	/*
10776 	 * Make sure some work items will not run after this,
10777 	 * they will not do anything but might not have been
10778 	 * cancelled when disconnecting.
10779 	 */
10780 	wiphy_work_cancel(sdata->local->hw.wiphy,
10781 			  &ifmgd->monitor_work);
10782 	wiphy_work_cancel(sdata->local->hw.wiphy,
10783 			  &ifmgd->beacon_connection_loss_work);
10784 	wiphy_work_cancel(sdata->local->hw.wiphy,
10785 			  &ifmgd->csa_connection_drop_work);
10786 	wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
10787 				  &ifmgd->tdls_peer_del_work);
10788 	wiphy_hrtimer_work_cancel(sdata->local->hw.wiphy,
10789 				  &ifmgd->uhr_omp.status_work);
10790 
10791 	if (ifmgd->assoc_data)
10792 		ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
10793 	if (ifmgd->auth_data)
10794 		ieee80211_destroy_auth_data(sdata, false);
10795 	spin_lock_bh(&ifmgd->teardown_lock);
10796 	if (ifmgd->teardown_skb) {
10797 		kfree_skb(ifmgd->teardown_skb);
10798 		ifmgd->teardown_skb = NULL;
10799 		ifmgd->orig_teardown_skb = NULL;
10800 	}
10801 	kfree(ifmgd->assoc_req_ies);
10802 	ifmgd->assoc_req_ies = NULL;
10803 	ifmgd->assoc_req_ies_len = 0;
10804 	spin_unlock_bh(&ifmgd->teardown_lock);
10805 	timer_delete_sync(&ifmgd->timer);
10806 }
10807 
10808 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
10809 			       enum nl80211_cqm_rssi_threshold_event rssi_event,
10810 			       s32 rssi_level,
10811 			       gfp_t gfp)
10812 {
10813 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10814 
10815 	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
10816 
10817 	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
10818 }
10819 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
10820 
10821 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
10822 {
10823 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10824 
10825 	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
10826 
10827 	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
10828 }
10829 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
10830 
10831 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
10832 					    int rssi_min_thold,
10833 					    int rssi_max_thold)
10834 {
10835 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
10836 
10837 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
10838 		return;
10839 
10840 	/*
10841 	 * Scale up threshold values before storing it, as the RSSI averaging
10842 	 * algorithm uses a scaled up value as well. Change this scaling
10843 	 * factor if the RSSI averaging algorithm changes.
10844 	 */
10845 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
10846 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
10847 }
10848 
10849 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
10850 				    int rssi_min_thold,
10851 				    int rssi_max_thold)
10852 {
10853 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10854 
10855 	WARN_ON(rssi_min_thold == rssi_max_thold ||
10856 		rssi_min_thold > rssi_max_thold);
10857 
10858 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
10859 				       rssi_max_thold);
10860 }
10861 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
10862 
10863 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
10864 {
10865 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
10866 
10867 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
10868 }
10869 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
10870 
10871 static void
10872 ieee80211_process_ml_reconf_resp(struct ieee80211_sub_if_data *sdata,
10873 				 struct ieee80211_mgmt *mgmt, size_t len)
10874 {
10875 	struct ieee80211_local *local = sdata->local;
10876 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
10877 	struct ieee80211_mgd_assoc_data *add_links_data =
10878 		ifmgd->reconf.add_links_data;
10879 	struct sta_info *sta;
10880 	struct cfg80211_mlo_reconf_done_data done_data = {};
10881 	u16 sta_changed_links = sdata->u.mgd.reconf.added_links |
10882 		                sdata->u.mgd.reconf.removed_links;
10883 	u16 link_mask, valid_links;
10884 	unsigned int link_id;
10885 	size_t orig_len = len;
10886 	u8 i, group_key_data_len;
10887 	u8 *pos;
10888 
10889 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
10890 	    len < IEEE80211_MIN_ACTION_SIZE(ml_reconf_resp) ||
10891 	    mgmt->u.action.ml_reconf_resp.dialog_token !=
10892 		sdata->u.mgd.reconf.dialog_token ||
10893 	    !sta_changed_links)
10894 		return;
10895 
10896 	pos = mgmt->u.action.ml_reconf_resp.variable;
10897 	len -= offsetofend(typeof(*mgmt), u.action.ml_reconf_resp);
10898 
10899 	if (len < mgmt->u.action.ml_reconf_resp.count *
10900 				sizeof(struct ieee80211_ml_reconf_status)) {
10901 		sdata_info(sdata,
10902 			   "mlo: reconf: unexpected len=%zu, count=%u\n",
10903 			   len, mgmt->u.action.ml_reconf_resp.count);
10904 		goto disconnect;
10905 	}
10906 
10907 	link_mask = sta_changed_links;
10908 	for (i = 0; i < mgmt->u.action.ml_reconf_resp.count; i++) {
10909 		struct ieee80211_ml_reconf_status *reconf_status = (void *)pos;
10910 		u16 status = le16_to_cpu(reconf_status->status);
10911 
10912 		link_id = u8_get_bits(reconf_status->info,
10913 				      IEEE80211_ML_RECONF_LINK_ID_MASK);
10914 
10915 		if (!(link_mask & BIT(link_id))) {
10916 			sdata_info(sdata,
10917 				   "mlo: reconf: unexpected link: %u, changed=0x%x\n",
10918 				   link_id, sta_changed_links);
10919 			goto disconnect;
10920 		}
10921 
10922 		/* clear the corresponding link, to detect the case that
10923 		 * the same link was included more than one time
10924 		 */
10925 		link_mask &= ~BIT(link_id);
10926 
10927 		/* Handle failure to remove links here. Failure to remove added
10928 		 * links will be done later in the flow.
10929 		 */
10930 		if (status != WLAN_STATUS_SUCCESS) {
10931 			sdata_info(sdata,
10932 				   "mlo: reconf: failed on link=%u, status=%u\n",
10933 				   link_id, status);
10934 
10935 			/* The AP MLD failed to remove a link that was already
10936 			 * removed locally. As this is not expected behavior,
10937 			 * disconnect
10938 			 */
10939 			if (sdata->u.mgd.reconf.removed_links & BIT(link_id))
10940 				goto disconnect;
10941 
10942 			/* The AP MLD failed to add a link. Remove it from the
10943 			 * added links.
10944 			 */
10945 			sdata->u.mgd.reconf.added_links &= ~BIT(link_id);
10946 		}
10947 
10948 		pos += sizeof(*reconf_status);
10949 		len -= sizeof(*reconf_status);
10950 	}
10951 
10952 	if (link_mask) {
10953 		sdata_info(sdata,
10954 			   "mlo: reconf: no response for links=0x%x\n",
10955 			   link_mask);
10956 		goto disconnect;
10957 	}
10958 
10959 	if (!sdata->u.mgd.reconf.added_links)
10960 		goto out;
10961 
10962 	if (len < 1 || len < 1 + *pos) {
10963 		sdata_info(sdata,
10964 			   "mlo: reconf: invalid group key data length");
10965 		goto disconnect;
10966 	}
10967 
10968 	/* The Group Key Data field must be present when links are added. This
10969 	 * field should be processed by userland.
10970 	 */
10971 	group_key_data_len = *pos++;
10972 
10973 	pos += group_key_data_len;
10974 	len -= group_key_data_len + 1;
10975 
10976 	/* Process the information for the added links */
10977 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
10978 	if (WARN_ON(!sta))
10979 		goto disconnect;
10980 
10981 	valid_links = sdata->vif.valid_links;
10982 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10983 		if (!add_links_data->link[link_id].bss ||
10984 		    !(sdata->u.mgd.reconf.added_links & BIT(link_id)))
10985 			continue;
10986 
10987 		valid_links |= BIT(link_id);
10988 		if (ieee80211_sta_allocate_link(sta, link_id))
10989 			goto disconnect;
10990 	}
10991 
10992 	ieee80211_vif_set_links(sdata, valid_links, sdata->vif.dormant_links);
10993 	link_mask = 0;
10994 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
10995 		struct cfg80211_bss *cbss = add_links_data->link[link_id].bss;
10996 		struct ieee80211_link_data *link;
10997 		struct link_sta_info *link_sta;
10998 		u64 changed = 0;
10999 
11000 		if (!cbss)
11001 			continue;
11002 
11003 		link = sdata_dereference(sdata->link[link_id], sdata);
11004 		if (WARN_ON(!link))
11005 			goto disconnect;
11006 
11007 		link_info(link,
11008 			  "mlo: reconf: local address %pM, AP link address %pM\n",
11009 			  add_links_data->link[link_id].addr,
11010 			  add_links_data->link[link_id].bss->bssid);
11011 
11012 		link_sta = rcu_dereference_protected(sta->link[link_id],
11013 						     lockdep_is_held(&local->hw.wiphy->mtx));
11014 		if (WARN_ON(!link_sta))
11015 			goto disconnect;
11016 
11017 		if (!link->u.mgd.have_beacon) {
11018 			const struct cfg80211_bss_ies *ies;
11019 
11020 			rcu_read_lock();
11021 			ies = rcu_dereference(cbss->beacon_ies);
11022 			if (ies)
11023 				link->u.mgd.have_beacon = true;
11024 			else
11025 				ies = rcu_dereference(cbss->ies);
11026 			ieee80211_get_dtim(ies,
11027 					   &link->conf->sync_dtim_count,
11028 					   &link->u.mgd.dtim_period);
11029 			link->conf->beacon_int = cbss->beacon_interval;
11030 			rcu_read_unlock();
11031 		}
11032 
11033 		link->conf->dtim_period = link->u.mgd.dtim_period ?: 1;
11034 
11035 		link->u.mgd.conn = add_links_data->link[link_id].conn;
11036 		if (ieee80211_prep_channel(sdata, link, link_sta, link_id, cbss,
11037 					   true, &link->u.mgd.conn,
11038 					   sdata->u.mgd.userspace_selectors)) {
11039 			link_info(link, "mlo: reconf: prep_channel failed\n");
11040 			goto disconnect;
11041 		}
11042 
11043 		if (ieee80211_mgd_setup_link_sta(link, sta, link_sta,
11044 						 add_links_data->link[link_id].bss))
11045 			goto disconnect;
11046 
11047 		if (!ieee80211_assoc_config_link(link, link_sta,
11048 						 add_links_data->link[link_id].bss,
11049 						 mgmt, pos, len,
11050 						 &changed))
11051 			goto disconnect;
11052 
11053 		/* The AP MLD indicated success for this link, but the station
11054 		 * profile status indicated otherwise. Since there is an
11055 		 * inconsistency in the ML reconfiguration response, disconnect
11056 		 */
11057 		if (add_links_data->link[link_id].status != WLAN_STATUS_SUCCESS)
11058 			goto disconnect;
11059 
11060 		if (ieee80211_sta_activate_link(sta, link_id))
11061 			goto disconnect;
11062 
11063 		changed |= ieee80211_link_set_associated(link, cbss);
11064 		ieee80211_link_info_change_notify(sdata, link, changed);
11065 
11066 		ieee80211_recalc_smps(sdata, link);
11067 		link_mask |= BIT(link_id);
11068 	}
11069 
11070 	sdata_info(sdata,
11071 		   "mlo: reconf: current valid_links=0x%x, added=0x%x\n",
11072 		   valid_links, link_mask);
11073 
11074 	/* links might have changed due to rejected ones, set them again */
11075 	ieee80211_vif_set_links(sdata, valid_links, sdata->vif.dormant_links);
11076 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS);
11077 
11078 	ieee80211_recalc_ps(local);
11079 	ieee80211_recalc_ps_vif(sdata);
11080 
11081 	ieee80211_send_uhr_omp_req_dbe(sdata, link_mask, true);
11082 
11083 	done_data.buf = (const u8 *)mgmt;
11084 	done_data.len = orig_len;
11085 	done_data.added_links = link_mask;
11086 
11087 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
11088 		done_data.links[link_id].bss = add_links_data->link[link_id].bss;
11089 		done_data.links[link_id].addr =
11090 			add_links_data->link[link_id].addr;
11091 	}
11092 
11093 	cfg80211_mlo_reconf_add_done(sdata->dev, &done_data);
11094 	kfree(sdata->u.mgd.reconf.add_links_data);
11095 	sdata->u.mgd.reconf.add_links_data = NULL;
11096 out:
11097 	ieee80211_ml_reconf_reset(sdata);
11098 	return;
11099 
11100 disconnect:
11101 	__ieee80211_disconnect(sdata);
11102 }
11103 
11104 static struct sk_buff *
11105 ieee80211_build_ml_reconf_req(struct ieee80211_sub_if_data *sdata,
11106 			      struct ieee80211_mgd_assoc_data *add_links_data,
11107 			      u16 removed_links, __le16 ext_mld_capa_ops)
11108 {
11109 	struct ieee80211_local *local = sdata->local;
11110 	struct ieee80211_mgmt *mgmt;
11111 	struct ieee80211_multi_link_elem *ml_elem;
11112 	struct ieee80211_mle_basic_common_info *common;
11113 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
11114 	struct sk_buff *skb;
11115 	size_t size;
11116 	unsigned int link_id;
11117 	__le16 eml_capa = 0, mld_capa_ops = 0;
11118 	struct ieee80211_tx_info *info;
11119 	u8 common_size, var_common_size;
11120 	u8 *ml_elem_len;
11121 	u16 capab = 0;
11122 
11123 	size = local->hw.extra_tx_headroom + sizeof(*mgmt);
11124 
11125 	/* Consider the maximal length of the reconfiguration ML element */
11126 	size += sizeof(struct ieee80211_multi_link_elem);
11127 
11128 	/* The Basic ML element and the Reconfiguration ML element have the same
11129 	 * fixed common information fields in the context of ML reconfiguration
11130 	 * action frame. The AP MLD MAC address must always be present
11131 	 */
11132 	common_size = sizeof(*common);
11133 
11134 	/* when adding links, the MLD capabilities must be present */
11135 	var_common_size = 0;
11136 	if (add_links_data) {
11137 		const struct wiphy_iftype_ext_capab *ift_ext_capa =
11138 			cfg80211_get_iftype_ext_capa(local->hw.wiphy,
11139 						     ieee80211_vif_type_p2p(&sdata->vif));
11140 
11141 		if (ift_ext_capa) {
11142 			eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities);
11143 			mld_capa_ops =
11144 				cpu_to_le16(ift_ext_capa->mld_capa_and_ops);
11145 		}
11146 
11147 		/* MLD capabilities and operation */
11148 		var_common_size += 2;
11149 
11150 		/* EML capabilities */
11151 		if (eml_capa & cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
11152 					    IEEE80211_EML_CAP_EMLMR_SUPPORT)))
11153 			var_common_size += 2;
11154 	}
11155 
11156 	if (ext_mld_capa_ops)
11157 		var_common_size += 2;
11158 
11159 	/* Add the common information length */
11160 	size += common_size + var_common_size;
11161 
11162 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
11163 		struct cfg80211_bss *cbss;
11164 		size_t elems_len;
11165 
11166 		if (removed_links & BIT(link_id)) {
11167 			size += sizeof(struct ieee80211_mle_per_sta_profile) +
11168 				ETH_ALEN;
11169 			continue;
11170 		}
11171 
11172 		if (!add_links_data || !add_links_data->link[link_id].bss)
11173 			continue;
11174 
11175 		elems_len = add_links_data->link[link_id].elems_len;
11176 		cbss = add_links_data->link[link_id].bss;
11177 
11178 		/* should be the same across all BSSes */
11179 		if (cbss->capability & WLAN_CAPABILITY_PRIVACY)
11180 			capab |= WLAN_CAPABILITY_PRIVACY;
11181 
11182 		size += 2 + sizeof(struct ieee80211_mle_per_sta_profile) +
11183 			ETH_ALEN;
11184 
11185 		/* WMM */
11186 		size += 9;
11187 		size += ieee80211_link_common_elems_size(sdata, iftype, cbss,
11188 							 elems_len);
11189 	}
11190 
11191 	skb = alloc_skb(size, GFP_KERNEL);
11192 	if (!skb)
11193 		return NULL;
11194 
11195 	skb_reserve(skb, local->hw.extra_tx_headroom);
11196 	mgmt = skb_put_zero(skb, IEEE80211_MIN_ACTION_SIZE(ml_reconf_req));
11197 
11198 	/* Add the MAC header */
11199 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
11200 					  IEEE80211_STYPE_ACTION);
11201 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
11202 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
11203 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
11204 
11205 	/* Add the action frame fixed fields */
11206 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
11207 	mgmt->u.action.action_code = WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_REQ;
11208 
11209 	/* allocate a dialog token and store it */
11210 	sdata->u.mgd.reconf.dialog_token = ++sdata->u.mgd.dialog_token_alloc;
11211 	mgmt->u.action.ml_reconf_req.dialog_token =
11212 		sdata->u.mgd.reconf.dialog_token;
11213 
11214 	/* Add the ML reconfiguration element and the common information  */
11215 	skb_put_u8(skb, WLAN_EID_EXTENSION);
11216 	ml_elem_len = skb_put(skb, 1);
11217 	skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
11218 	ml_elem = skb_put(skb, sizeof(*ml_elem));
11219 	ml_elem->control =
11220 		cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_RECONF |
11221 			    IEEE80211_MLC_RECONF_PRES_MLD_MAC_ADDR);
11222 	common = skb_put(skb, common_size);
11223 	common->len = common_size + var_common_size;
11224 	memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
11225 
11226 	if (add_links_data) {
11227 		if (eml_capa &
11228 		    cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP |
11229 				 IEEE80211_EML_CAP_EMLMR_SUPPORT))) {
11230 			ml_elem->control |=
11231 				cpu_to_le16(IEEE80211_MLC_RECONF_PRES_EML_CAPA);
11232 			skb_put_data(skb, &eml_capa, sizeof(eml_capa));
11233 		}
11234 
11235 		ml_elem->control |=
11236 			cpu_to_le16(IEEE80211_MLC_RECONF_PRES_MLD_CAPA_OP);
11237 
11238 		skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops));
11239 	}
11240 
11241 	if (ext_mld_capa_ops) {
11242 		ml_elem->control |=
11243 			cpu_to_le16(IEEE80211_MLC_RECONF_PRES_EXT_MLD_CAPA_OP);
11244 		skb_put_data(skb, &ext_mld_capa_ops, sizeof(ext_mld_capa_ops));
11245 	}
11246 
11247 	if (sdata->u.mgd.flags & IEEE80211_STA_ENABLE_RRM)
11248 		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
11249 
11250 	/* Add the per station profile */
11251 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
11252 		u8 *subelem_len = NULL;
11253 		u16 ctrl;
11254 		const u8 *addr;
11255 
11256 		/* Skip links that are not changing */
11257 		if (!(removed_links & BIT(link_id)) &&
11258 		    (!add_links_data || !add_links_data->link[link_id].bss))
11259 			continue;
11260 
11261 		ctrl = link_id |
11262 		       IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT;
11263 
11264 		if (removed_links & BIT(link_id)) {
11265 			struct ieee80211_bss_conf *conf =
11266 				sdata_dereference(sdata->vif.link_conf[link_id],
11267 						  sdata);
11268 			if (!conf)
11269 				continue;
11270 
11271 			addr = conf->addr;
11272 			ctrl |= u16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_DEL_LINK,
11273 						IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
11274 		} else {
11275 			addr = add_links_data->link[link_id].addr;
11276 			ctrl |= IEEE80211_MLE_STA_RECONF_CONTROL_COMPLETE_PROFILE |
11277 				u16_encode_bits(IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_ADD_LINK,
11278 						IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE);
11279 		}
11280 
11281 		skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
11282 		subelem_len = skb_put(skb, 1);
11283 
11284 		put_unaligned_le16(ctrl, skb_put(skb, sizeof(ctrl)));
11285 		skb_put_u8(skb, 1 + ETH_ALEN);
11286 		skb_put_data(skb, addr, ETH_ALEN);
11287 
11288 		if (!(removed_links & BIT(link_id))) {
11289 			u16 link_present_elems[PRESENT_ELEMS_MAX] = {};
11290 			size_t extra_used;
11291 			void *capab_pos;
11292 			u8 qos_info;
11293 
11294 			capab_pos = skb_put(skb, 2);
11295 
11296 			extra_used =
11297 				ieee80211_add_link_elems(sdata, skb, &capab, NULL,
11298 							 add_links_data->link[link_id].elems,
11299 							 add_links_data->link[link_id].elems_len,
11300 							 link_id, NULL,
11301 							 link_present_elems,
11302 							 add_links_data);
11303 
11304 			if (add_links_data->link[link_id].elems)
11305 				skb_put_data(skb,
11306 					     add_links_data->link[link_id].elems +
11307 					     extra_used,
11308 					     add_links_data->link[link_id].elems_len -
11309 					     extra_used);
11310 			if (sdata->u.mgd.flags & IEEE80211_STA_UAPSD_ENABLED) {
11311 				qos_info = sdata->u.mgd.uapsd_queues;
11312 				qos_info |= (sdata->u.mgd.uapsd_max_sp_len <<
11313 					     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
11314 			} else {
11315 				qos_info = 0;
11316 			}
11317 
11318 			ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
11319 			put_unaligned_le16(capab, capab_pos);
11320 		}
11321 
11322 		ieee80211_fragment_element(skb, subelem_len,
11323 					   IEEE80211_MLE_SUBELEM_FRAGMENT);
11324 	}
11325 
11326 	ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT);
11327 
11328 	info = IEEE80211_SKB_CB(skb);
11329 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
11330 
11331 	return skb;
11332 }
11333 
11334 int ieee80211_mgd_assoc_ml_reconf(struct ieee80211_sub_if_data *sdata,
11335 				  struct cfg80211_ml_reconf_req *req)
11336 {
11337 	const struct wiphy_iftype_ext_capab *ift_ext_capa;
11338 	struct ieee80211_local *local = sdata->local;
11339 	struct ieee80211_mgd_assoc_data *data = NULL;
11340 	struct sta_info *sta;
11341 	struct sk_buff *skb;
11342 	u16 added_links, new_valid_links;
11343 	u16 driver_ext_mld_capa_ops = 0;
11344 	int link_id, err;
11345 
11346 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
11347 	    !(sdata->vif.cfg.mld_capa_op &
11348 	      IEEE80211_MLD_CAP_OP_LINK_RECONF_SUPPORT))
11349 		return -EINVAL;
11350 
11351 	/* No support for concurrent ML reconfiguration operation */
11352 	if (sdata->u.mgd.reconf.added_links ||
11353 	    sdata->u.mgd.reconf.removed_links)
11354 		return -EBUSY;
11355 
11356 	added_links = 0;
11357 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
11358 		if (!req->add_links[link_id].bss)
11359 			continue;
11360 
11361 		added_links |= BIT(link_id);
11362 	}
11363 
11364 	sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
11365 	if (WARN_ON(!sta))
11366 		return -ENOLINK;
11367 
11368 	/* Adding links to the set of valid link is done only after a successful
11369 	 * ML reconfiguration frame exchange. Here prepare the data for the ML
11370 	 * reconfiguration frame construction and allocate the required
11371 	 * resources
11372 	 */
11373 	if (added_links) {
11374 		bool uapsd_supported;
11375 
11376 		data = kzalloc_obj(*data);
11377 		if (!data)
11378 			return -ENOMEM;
11379 
11380 		data->assoc_link_id = -1;
11381 		data->wmm = true;
11382 
11383 		uapsd_supported = true;
11384 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
11385 		     link_id++) {
11386 			struct ieee80211_supported_band *sband;
11387 			struct cfg80211_bss *link_cbss =
11388 				req->add_links[link_id].bss;
11389 			struct ieee80211_bss *bss;
11390 
11391 			if (!link_cbss)
11392 				continue;
11393 
11394 			bss = (void *)link_cbss->priv;
11395 
11396 			if (!bss->wmm_used) {
11397 				err = -EINVAL;
11398 				goto err_free;
11399 			}
11400 
11401 			if (link_cbss->channel->band == NL80211_BAND_S1GHZ) {
11402 				err = -EINVAL;
11403 				goto err_free;
11404 			}
11405 
11406 			eth_random_addr(data->link[link_id].addr);
11407 			data->link[link_id].conn =
11408 				ieee80211_conn_settings_unlimited;
11409 			sband =
11410 				local->hw.wiphy->bands[link_cbss->channel->band];
11411 
11412 			ieee80211_determine_our_sta_mode(sdata, sband,
11413 							 NULL, true, link_id,
11414 							 &data->link[link_id].conn);
11415 
11416 			data->link[link_id].bss = link_cbss;
11417 			data->link[link_id].elems =
11418 				(u8 *)req->add_links[link_id].elems;
11419 			data->link[link_id].elems_len =
11420 				req->add_links[link_id].elems_len;
11421 
11422 			if (!bss->uapsd_supported)
11423 				uapsd_supported = false;
11424 
11425 			if (data->link[link_id].conn.mode <
11426 			    IEEE80211_CONN_MODE_EHT) {
11427 				err = -EINVAL;
11428 				goto err_free;
11429 			}
11430 
11431 			err = ieee80211_mgd_get_ap_ht_vht_capa(sdata, data,
11432 							       link_id);
11433 			if (err) {
11434 				err = -EINVAL;
11435 				goto err_free;
11436 			}
11437 		}
11438 
11439 		/* Require U-APSD support if we enabled it */
11440 		if (sdata->u.mgd.flags & IEEE80211_STA_UAPSD_ENABLED &&
11441 		    !uapsd_supported) {
11442 			err = -EINVAL;
11443 			sdata_info(sdata, "U-APSD on but not available on (all) new links\n");
11444 			goto err_free;
11445 		}
11446 
11447 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
11448 		     link_id++) {
11449 			if (!data->link[link_id].bss)
11450 				continue;
11451 
11452 			/* only used to verify the mode, nothing is allocated */
11453 			err = ieee80211_prep_channel(sdata, NULL, NULL, link_id,
11454 						     data->link[link_id].bss,
11455 						     true,
11456 						     &data->link[link_id].conn,
11457 						     sdata->u.mgd.userspace_selectors);
11458 			if (err)
11459 				goto err_free;
11460 		}
11461 	}
11462 
11463 	/* link removal is done before the ML reconfiguration frame exchange so
11464 	 * that these links will not be used between their removal by the AP MLD
11465 	 * and before the station got the ML reconfiguration response. Based on
11466 	 * Section 35.3.6.4 in Draft P802.11be_D7.0 the AP MLD should accept the
11467 	 * link removal request.
11468 	 */
11469 	if (req->rem_links) {
11470 		u16 new_active_links =
11471 			sdata->vif.active_links & ~req->rem_links;
11472 
11473 		new_valid_links = sdata->vif.valid_links & ~req->rem_links;
11474 
11475 		/* Should not be left with no valid links to perform the
11476 		 * ML reconfiguration
11477 		 */
11478 		if (!new_valid_links ||
11479 		    !(new_valid_links & ~sdata->vif.dormant_links)) {
11480 			sdata_info(sdata, "mlo: reconf: no valid links\n");
11481 			err = -EINVAL;
11482 			goto err_free;
11483 		}
11484 
11485 		if (new_active_links != sdata->vif.active_links) {
11486 			if (!new_active_links)
11487 				new_active_links =
11488 					BIT(__ffs(new_valid_links &
11489 						  ~sdata->vif.dormant_links));
11490 
11491 			err = ieee80211_set_active_links(&sdata->vif,
11492 							 new_active_links);
11493 			if (err) {
11494 				sdata_info(sdata,
11495 					   "mlo: reconf: failed set active links\n");
11496 				goto err_free;
11497 			}
11498 		}
11499 	}
11500 
11501 	ift_ext_capa = cfg80211_get_iftype_ext_capa(local->hw.wiphy,
11502 						    ieee80211_vif_type_p2p(&sdata->vif));
11503 	if (ift_ext_capa)
11504 		driver_ext_mld_capa_ops = ift_ext_capa->ext_mld_capa_and_ops;
11505 
11506 	/* Build the SKB before the link removal as the construction of the
11507 	 * station info for removed links requires the local address.
11508 	 * Invalidate the removed links, so that the transmission of the ML
11509 	 * reconfiguration request frame would not be done using them, as the AP
11510 	 * is expected to send the ML reconfiguration response frame on the link
11511 	 * on which the request was received.
11512 	 */
11513 	skb = ieee80211_build_ml_reconf_req(sdata, data, req->rem_links,
11514 					    cpu_to_le16(req->ext_mld_capa_ops |
11515 							driver_ext_mld_capa_ops));
11516 	if (!skb) {
11517 		err = -ENOMEM;
11518 		goto err_free;
11519 	}
11520 
11521 	if (req->rem_links) {
11522 		u16 new_dormant_links =
11523 			sdata->vif.dormant_links & ~req->rem_links;
11524 
11525 		err = ieee80211_vif_set_links(sdata, new_valid_links,
11526 					      new_dormant_links);
11527 		if (err) {
11528 			sdata_info(sdata,
11529 				   "mlo: reconf: failed set valid links\n");
11530 			kfree_skb(skb);
11531 			goto err_free;
11532 		}
11533 
11534 		/* notify the driver and upper layers */
11535 		ieee80211_vif_cfg_change_notify(sdata,
11536 						BSS_CHANGED_MLD_VALID_LINKS);
11537 		cfg80211_links_removed(sdata->dev, req->rem_links);
11538 	}
11539 
11540 	sdata_info(sdata, "mlo: reconf: adding=0x%x, removed=0x%x\n",
11541 		   added_links, req->rem_links);
11542 
11543 	ieee80211_tx_skb(sdata, skb);
11544 
11545 	sdata->u.mgd.reconf.added_links = added_links;
11546 	sdata->u.mgd.reconf.add_links_data = data;
11547 	sdata->u.mgd.reconf.removed_links = req->rem_links;
11548 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
11549 				 &sdata->u.mgd.reconf.wk,
11550 				 IEEE80211_ASSOC_TIMEOUT_SHORT);
11551 	return 0;
11552 
11553  err_free:
11554 	kfree(data);
11555 	return err;
11556 }
11557 
11558 static bool ieee80211_mgd_epcs_supp(struct ieee80211_sub_if_data *sdata)
11559 {
11560 	unsigned long valid_links = sdata->vif.valid_links;
11561 	u8 link_id;
11562 
11563 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
11564 
11565 	if (!ieee80211_vif_is_mld(&sdata->vif))
11566 		return false;
11567 
11568 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
11569 		struct ieee80211_bss_conf *bss_conf =
11570 			sdata_dereference(sdata->vif.link_conf[link_id], sdata);
11571 
11572 		if (WARN_ON(!bss_conf) || !bss_conf->epcs_support)
11573 			return false;
11574 	}
11575 
11576 	return true;
11577 }
11578 
11579 int ieee80211_mgd_set_epcs(struct ieee80211_sub_if_data *sdata, bool enable)
11580 {
11581 	int frame_len = IEEE80211_MIN_ACTION_SIZE(epcs) + (enable ? 1 : 0);
11582 	struct ieee80211_local *local = sdata->local;
11583 	struct ieee80211_mgmt *mgmt;
11584 	struct sk_buff *skb;
11585 
11586 	if (!ieee80211_mgd_epcs_supp(sdata))
11587 		return -EINVAL;
11588 
11589 	if (sdata->u.mgd.epcs.enabled == enable &&
11590 	    !sdata->u.mgd.epcs.dialog_token)
11591 		return 0;
11592 
11593 	/* Do not allow enabling EPCS if the AP didn't respond yet.
11594 	 * However, allow disabling EPCS in such a case.
11595 	 */
11596 	if (sdata->u.mgd.epcs.dialog_token && enable)
11597 		return -EALREADY;
11598 
11599 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + frame_len);
11600 	if (!skb)
11601 		return -ENOBUFS;
11602 
11603 	skb_reserve(skb, local->hw.extra_tx_headroom);
11604 	mgmt = skb_put_zero(skb, frame_len);
11605 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
11606 					  IEEE80211_STYPE_ACTION);
11607 	memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
11608 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
11609 	memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
11610 
11611 	mgmt->u.action.category = WLAN_CATEGORY_PROTECTED_EHT;
11612 	if (enable) {
11613 		u8 *pos = mgmt->u.action.epcs.variable;
11614 
11615 		mgmt->u.action.action_code =
11616 			WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_REQ;
11617 
11618 		*pos = ++sdata->u.mgd.dialog_token_alloc;
11619 		sdata->u.mgd.epcs.dialog_token = *pos;
11620 	} else {
11621 		mgmt->u.action.action_code =
11622 			WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN;
11623 
11624 		ieee80211_epcs_teardown(sdata);
11625 		ieee80211_epcs_changed(sdata, false);
11626 	}
11627 
11628 	ieee80211_tx_skb(sdata, skb);
11629 	return 0;
11630 }
11631 
11632 static void ieee80211_ml_epcs(struct ieee80211_sub_if_data *sdata,
11633 			      struct ieee802_11_elems *elems)
11634 {
11635 	const struct element *sub;
11636 	size_t scratch_len = elems->ml_epcs_len;
11637 	u8 *scratch __free(kfree) = kzalloc(scratch_len, GFP_KERNEL);
11638 
11639 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
11640 
11641 	if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_epcs)
11642 		return;
11643 
11644 	if (WARN_ON(!scratch))
11645 		return;
11646 
11647 	/* Directly parse the sub elements as the common information doesn't
11648 	 * hold any useful information.
11649 	 */
11650 	for_each_mle_subelement(sub, (const u8 *)elems->ml_epcs,
11651 				elems->ml_epcs_len) {
11652 		struct ieee802_11_elems *link_elems __free(kfree) = NULL;
11653 		struct ieee80211_link_data *link;
11654 		u8 *pos = (void *)sub->data;
11655 		u16 control;
11656 		ssize_t len;
11657 		u8 link_id;
11658 
11659 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
11660 			continue;
11661 
11662 		if (sub->datalen < sizeof(control))
11663 			break;
11664 
11665 		control = get_unaligned_le16(pos);
11666 		link_id = control & IEEE80211_MLE_STA_EPCS_CONTROL_LINK_ID;
11667 
11668 		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
11669 			continue;
11670 
11671 		link = sdata_dereference(sdata->link[link_id], sdata);
11672 		if (!link)
11673 			continue;
11674 
11675 		len = cfg80211_defragment_element(sub, (u8 *)elems->ml_epcs,
11676 						  elems->ml_epcs_len,
11677 						  scratch, scratch_len,
11678 						  IEEE80211_MLE_SUBELEM_FRAGMENT);
11679 		if (len < (ssize_t)sizeof(control))
11680 			continue;
11681 
11682 		pos = scratch + sizeof(control);
11683 		len -= sizeof(control);
11684 
11685 		link_elems = ieee802_11_parse_elems(pos, len,
11686 						    IEEE80211_FTYPE_MGMT |
11687 						    IEEE80211_STYPE_ACTION,
11688 						    NULL);
11689 		if (!link_elems)
11690 			continue;
11691 
11692 		if (ieee80211_sta_wmm_params(sdata->local, link,
11693 					     link_elems->wmm_param,
11694 					     link_elems->wmm_param_len,
11695 					     link_elems->mu_edca_param_set))
11696 			ieee80211_link_info_change_notify(sdata, link,
11697 							  BSS_CHANGED_QOS);
11698 	}
11699 }
11700 
11701 static void ieee80211_process_epcs_ena_resp(struct ieee80211_sub_if_data *sdata,
11702 					    struct ieee80211_mgmt *mgmt,
11703 					    size_t len)
11704 {
11705 	struct ieee802_11_elems *elems __free(kfree) = NULL;
11706 	size_t ies_len;
11707 	u16 status_code;
11708 	u8 *pos, dialog_token;
11709 
11710 	if (!ieee80211_mgd_epcs_supp(sdata))
11711 		return;
11712 
11713 	/* Handle dialog token and status code */
11714 	pos = mgmt->u.action.epcs.variable;
11715 	dialog_token = *pos;
11716 	status_code = get_unaligned_le16(pos + 1);
11717 
11718 	/* An EPCS enable response with dialog token == 0 is an unsolicited
11719 	 * notification from the AP MLD. In such a case, EPCS should already be
11720 	 * enabled and status must be success
11721 	 */
11722 	if (!dialog_token &&
11723 	    (!sdata->u.mgd.epcs.enabled ||
11724 	     status_code != WLAN_STATUS_SUCCESS))
11725 		return;
11726 
11727 	if (sdata->u.mgd.epcs.dialog_token != dialog_token)
11728 		return;
11729 
11730 	sdata->u.mgd.epcs.dialog_token = 0;
11731 
11732 	if (status_code != WLAN_STATUS_SUCCESS)
11733 		return;
11734 
11735 	pos += IEEE80211_EPCS_ENA_RESP_BODY_LEN;
11736 	ies_len = len - IEEE80211_MIN_ACTION_SIZE(epcs) -
11737 		IEEE80211_EPCS_ENA_RESP_BODY_LEN;
11738 
11739 	elems = ieee802_11_parse_elems(pos, ies_len,
11740 				       IEEE80211_FTYPE_MGMT |
11741 				       IEEE80211_STYPE_ACTION,
11742 				       NULL);
11743 	if (!elems)
11744 		return;
11745 
11746 	ieee80211_ml_epcs(sdata, elems);
11747 	ieee80211_epcs_changed(sdata, true);
11748 }
11749 
11750 static void ieee80211_process_epcs_teardown(struct ieee80211_sub_if_data *sdata,
11751 					    struct ieee80211_mgmt *mgmt,
11752 					    size_t len)
11753 {
11754 	if (!ieee80211_vif_is_mld(&sdata->vif) ||
11755 	    !sdata->u.mgd.epcs.enabled)
11756 		return;
11757 
11758 	ieee80211_epcs_teardown(sdata);
11759 	ieee80211_epcs_changed(sdata, false);
11760 }
11761 
11762 void ieee80211_sta_rx_queued_frame(struct ieee80211_sub_if_data *sdata,
11763 				   struct sk_buff *skb)
11764 {
11765 	struct ieee80211_link_data *link = &sdata->deflink;
11766 	struct ieee80211_rx_status *rx_status;
11767 	struct ieee802_11_elems *elems;
11768 	struct ieee80211_mgmt *mgmt;
11769 	u16 fc;
11770 	int ies_len;
11771 
11772 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
11773 
11774 	mgmt = (struct ieee80211_mgmt *) skb->data;
11775 
11776 	if (ieee80211_is_ext(mgmt->frame_control)) {
11777 		ieee80211_sta_rx_queued_ext(sdata, skb);
11778 		return;
11779 	}
11780 
11781 	rx_status = (struct ieee80211_rx_status *) skb->cb;
11782 	fc = le16_to_cpu(mgmt->frame_control);
11783 
11784 	if (rx_status->link_valid) {
11785 		link = sdata_dereference(sdata->link[rx_status->link_id],
11786 					 sdata);
11787 		if (!link)
11788 			return;
11789 	}
11790 
11791 	switch (fc & IEEE80211_FCTL_STYPE) {
11792 	case IEEE80211_STYPE_BEACON:
11793 		ieee80211_rx_mgmt_beacon(link, (void *)mgmt,
11794 					 skb->len, rx_status);
11795 		break;
11796 	case IEEE80211_STYPE_PROBE_RESP:
11797 		ieee80211_rx_mgmt_probe_resp(link, skb);
11798 		break;
11799 	case IEEE80211_STYPE_AUTH:
11800 		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
11801 		break;
11802 	case IEEE80211_STYPE_DEAUTH:
11803 		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
11804 		break;
11805 	case IEEE80211_STYPE_DISASSOC:
11806 		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
11807 		break;
11808 	case IEEE80211_STYPE_ASSOC_RESP:
11809 	case IEEE80211_STYPE_REASSOC_RESP:
11810 		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
11811 		break;
11812 	case IEEE80211_STYPE_ACTION:
11813 		if (!sdata->u.mgd.associated ||
11814 		    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
11815 			break;
11816 
11817 		switch (mgmt->u.action.category) {
11818 		case WLAN_CATEGORY_SPECTRUM_MGMT:
11819 			ies_len = skb->len -
11820 				  offsetof(struct ieee80211_mgmt,
11821 					   u.action.chan_switch.variable);
11822 
11823 			if (ies_len < 0)
11824 				break;
11825 
11826 			/* CSA IE cannot be overridden, no need for BSSID */
11827 			elems = ieee802_11_parse_elems(mgmt->u.action.chan_switch.variable,
11828 						       ies_len,
11829 						       IEEE80211_FTYPE_MGMT |
11830 						       IEEE80211_STYPE_ACTION,
11831 						       NULL);
11832 
11833 			if (elems && !elems->parse_error) {
11834 				enum ieee80211_csa_source src =
11835 					IEEE80211_CSA_SOURCE_PROT_ACTION;
11836 
11837 				ieee80211_sta_process_chanswitch(link,
11838 								 rx_status->mactime,
11839 								 rx_status->device_timestamp,
11840 								 elems, elems,
11841 								 src);
11842 			}
11843 			kfree(elems);
11844 			break;
11845 		case WLAN_CATEGORY_PUBLIC:
11846 		case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
11847 			ies_len = skb->len -
11848 				  offsetof(struct ieee80211_mgmt,
11849 					   u.action.ext_chan_switch.variable);
11850 
11851 			if (ies_len < 0)
11852 				break;
11853 
11854 			/*
11855 			 * extended CSA IE can't be overridden, no need for
11856 			 * BSSID
11857 			 */
11858 			elems = ieee802_11_parse_elems(mgmt->u.action.ext_chan_switch.variable,
11859 						       ies_len,
11860 						       IEEE80211_FTYPE_MGMT |
11861 						       IEEE80211_STYPE_ACTION,
11862 						       NULL);
11863 
11864 			if (elems && !elems->parse_error) {
11865 				enum ieee80211_csa_source src;
11866 
11867 				if (mgmt->u.action.category ==
11868 						WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
11869 					src = IEEE80211_CSA_SOURCE_PROT_ACTION;
11870 				else
11871 					src = IEEE80211_CSA_SOURCE_UNPROT_ACTION;
11872 
11873 				/* for the handling code pretend it was an IE */
11874 				elems->ext_chansw_ie =
11875 					&mgmt->u.action.ext_chan_switch.data;
11876 
11877 				ieee80211_sta_process_chanswitch(link,
11878 								 rx_status->mactime,
11879 								 rx_status->device_timestamp,
11880 								 elems, elems,
11881 								 src);
11882 			}
11883 
11884 			kfree(elems);
11885 			break;
11886 		case WLAN_CATEGORY_PROTECTED_EHT:
11887 			switch (mgmt->u.action.action_code) {
11888 			case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
11889 				ieee80211_process_neg_ttlm_req(sdata, mgmt,
11890 							       skb->len);
11891 				break;
11892 			case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
11893 				ieee80211_process_neg_ttlm_res(sdata, mgmt,
11894 							       skb->len);
11895 				break;
11896 			case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN:
11897 				ieee80211_process_ttlm_teardown(sdata);
11898 				break;
11899 			case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
11900 				ieee80211_process_ml_reconf_resp(sdata, mgmt,
11901 								 skb->len);
11902 				break;
11903 			case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP:
11904 				ieee80211_process_epcs_ena_resp(sdata, mgmt,
11905 								skb->len);
11906 				break;
11907 			case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN:
11908 				ieee80211_process_epcs_teardown(sdata, mgmt,
11909 								skb->len);
11910 				break;
11911 			default:
11912 				break;
11913 			}
11914 			break;
11915 		case WLAN_CATEGORY_PROTECTED_UHR:
11916 			switch (mgmt->u.action.action_code) {
11917 			case IEEE80211_PROTECTED_UHR_ACTION_LINK_RECONFIG_NOTIFY:
11918 				ieee80211_process_uhr_link_reconf_notif(sdata,
11919 									mgmt,
11920 									skb->len);
11921 				break;
11922 			}
11923 			break;
11924 		}
11925 		break;
11926 	}
11927 }
11928