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