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