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