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