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