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