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