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