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