xref: /kernel/linux/linux-5.10/net/mac80211/mlme.c (revision 8c2ecf20)
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 - 2020 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 <asm/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
46static int max_nullfunc_tries = 2;
47module_param(max_nullfunc_tries, int, 0644);
48MODULE_PARM_DESC(max_nullfunc_tries,
49		 "Maximum nullfunc tx tries before disconnecting (reason 4).");
50
51static int max_probe_tries = 5;
52module_param(max_probe_tries, int, 0644);
53MODULE_PARM_DESC(max_probe_tries,
54		 "Maximum probe tries before disconnecting (reason 4).");
55
56/*
57 * Beacon loss timeout is calculated as N frames times the
58 * advertised beacon interval.  This may need to be somewhat
59 * higher than what hardware might detect to account for
60 * delays in the host processing frames. But since we also
61 * probe on beacon miss before declaring the connection lost
62 * default to what we want.
63 */
64static int beacon_loss_count = 7;
65module_param(beacon_loss_count, int, 0644);
66MODULE_PARM_DESC(beacon_loss_count,
67		 "Number of beacon intervals before we decide beacon was lost.");
68
69/*
70 * Time the connection can be idle before we probe
71 * it to see if we can still talk to the AP.
72 */
73#define IEEE80211_CONNECTION_IDLE_TIME	(30 * HZ)
74/*
75 * Time we wait for a probe response after sending
76 * a probe request because of beacon loss or for
77 * checking the connection still works.
78 */
79static int probe_wait_ms = 500;
80module_param(probe_wait_ms, int, 0644);
81MODULE_PARM_DESC(probe_wait_ms,
82		 "Maximum time(ms) to wait for probe response"
83		 " before disconnecting (reason 4).");
84
85/*
86 * How many Beacon frames need to have been used in average signal strength
87 * before starting to indicate signal change events.
88 */
89#define IEEE80211_SIGNAL_AVE_MIN_COUNT	4
90
91/*
92 * We can have multiple work items (and connection probing)
93 * scheduling this timer, but we need to take care to only
94 * reschedule it when it should fire _earlier_ than it was
95 * asked for before, or if it's not pending right now. This
96 * function ensures that. Note that it then is required to
97 * run this function for all timeouts after the first one
98 * has happened -- the work that runs from this timer will
99 * do that.
100 */
101static void run_again(struct ieee80211_sub_if_data *sdata,
102		      unsigned long timeout)
103{
104	sdata_assert_lock(sdata);
105
106	if (!timer_pending(&sdata->u.mgd.timer) ||
107	    time_before(timeout, sdata->u.mgd.timer.expires))
108		mod_timer(&sdata->u.mgd.timer, timeout);
109}
110
111void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
112{
113	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
114		return;
115
116	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
117		return;
118
119	mod_timer(&sdata->u.mgd.bcn_mon_timer,
120		  round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
121}
122
123void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
124{
125	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
126
127	if (unlikely(!ifmgd->associated))
128		return;
129
130	if (ifmgd->probe_send_count)
131		ifmgd->probe_send_count = 0;
132
133	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
134		return;
135
136	mod_timer(&ifmgd->conn_mon_timer,
137		  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
138}
139
140static int ecw2cw(int ecw)
141{
142	return (1 << ecw) - 1;
143}
144
145static u32
146ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
147			     struct ieee80211_supported_band *sband,
148			     struct ieee80211_channel *channel,
149			     u32 vht_cap_info,
150			     const struct ieee80211_ht_operation *ht_oper,
151			     const struct ieee80211_vht_operation *vht_oper,
152			     const struct ieee80211_he_operation *he_oper,
153			     const struct ieee80211_s1g_oper_ie *s1g_oper,
154			     struct cfg80211_chan_def *chandef, bool tracking)
155{
156	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157	struct cfg80211_chan_def vht_chandef;
158	struct ieee80211_sta_ht_cap sta_ht_cap;
159	u32 ht_cfreq, ret;
160
161	memset(chandef, 0, sizeof(struct cfg80211_chan_def));
162	chandef->chan = channel;
163	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
164	chandef->center_freq1 = channel->center_freq;
165	chandef->freq1_offset = channel->freq_offset;
166
167	if (channel->band == NL80211_BAND_6GHZ) {
168		if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, chandef))
169			ret = IEEE80211_STA_DISABLE_HT |
170			      IEEE80211_STA_DISABLE_VHT |
171			      IEEE80211_STA_DISABLE_HE;
172		else
173			ret = 0;
174		vht_chandef = *chandef;
175		goto out;
176	} else if (sband->band == NL80211_BAND_S1GHZ) {
177		if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) {
178			sdata_info(sdata,
179				   "Missing S1G Operation Element? Trying operating == primary\n");
180			chandef->width = ieee80211_s1g_channel_width(channel);
181		}
182
183		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_40MHZ |
184		      IEEE80211_STA_DISABLE_VHT |
185		      IEEE80211_STA_DISABLE_80P80MHZ |
186		      IEEE80211_STA_DISABLE_160MHZ;
187		goto out;
188	}
189
190	memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
191	ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
192
193	if (!ht_oper || !sta_ht_cap.ht_supported) {
194		ret = IEEE80211_STA_DISABLE_HT |
195		      IEEE80211_STA_DISABLE_VHT |
196		      IEEE80211_STA_DISABLE_HE;
197		goto out;
198	}
199
200	chandef->width = NL80211_CHAN_WIDTH_20;
201
202	ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
203						  channel->band);
204	/* check that channel matches the right operating channel */
205	if (!tracking && channel->center_freq != ht_cfreq) {
206		/*
207		 * It's possible that some APs are confused here;
208		 * Netgear WNDR3700 sometimes reports 4 higher than
209		 * the actual channel in association responses, but
210		 * since we look at probe response/beacon data here
211		 * it should be OK.
212		 */
213		sdata_info(sdata,
214			   "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
215			   channel->center_freq, ht_cfreq,
216			   ht_oper->primary_chan, channel->band);
217		ret = IEEE80211_STA_DISABLE_HT |
218		      IEEE80211_STA_DISABLE_VHT |
219		      IEEE80211_STA_DISABLE_HE;
220		goto out;
221	}
222
223	/* check 40 MHz support, if we have it */
224	if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
225		ieee80211_chandef_ht_oper(ht_oper, chandef);
226	} else {
227		/* 40 MHz (and 80 MHz) must be supported for VHT */
228		ret = IEEE80211_STA_DISABLE_VHT;
229		/* also mark 40 MHz disabled */
230		ret |= IEEE80211_STA_DISABLE_40MHZ;
231		goto out;
232	}
233
234	if (!vht_oper || !sband->vht_cap.vht_supported) {
235		ret = IEEE80211_STA_DISABLE_VHT;
236		goto out;
237	}
238
239	vht_chandef = *chandef;
240	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
241	    (le32_to_cpu(he_oper->he_oper_params) &
242	     IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
243		struct ieee80211_vht_operation he_oper_vht_cap;
244
245		/*
246		 * Set only first 3 bytes (other 2 aren't used in
247		 * ieee80211_chandef_vht_oper() anyway)
248		 */
249		memcpy(&he_oper_vht_cap, he_oper->optional, 3);
250		he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
251
252		if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
253						&he_oper_vht_cap, ht_oper,
254						&vht_chandef)) {
255			if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
256				sdata_info(sdata,
257					   "HE AP VHT information is invalid, disable HE\n");
258			ret = IEEE80211_STA_DISABLE_HE;
259			goto out;
260		}
261	} else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
262					       vht_cap_info,
263					       vht_oper, ht_oper,
264					       &vht_chandef)) {
265		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
266			sdata_info(sdata,
267				   "AP VHT information is invalid, disable VHT\n");
268		ret = IEEE80211_STA_DISABLE_VHT;
269		goto out;
270	}
271
272	if (!cfg80211_chandef_valid(&vht_chandef)) {
273		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
274			sdata_info(sdata,
275				   "AP VHT information is invalid, disable VHT\n");
276		ret = IEEE80211_STA_DISABLE_VHT;
277		goto out;
278	}
279
280	if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
281		ret = 0;
282		goto out;
283	}
284
285	if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
286		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
287			sdata_info(sdata,
288				   "AP VHT information doesn't match HT, disable VHT\n");
289		ret = IEEE80211_STA_DISABLE_VHT;
290		goto out;
291	}
292
293	*chandef = vht_chandef;
294
295	ret = 0;
296
297out:
298	/*
299	 * When tracking the current AP, don't do any further checks if the
300	 * new chandef is identical to the one we're currently using for the
301	 * connection. This keeps us from playing ping-pong with regulatory,
302	 * without it the following can happen (for example):
303	 *  - connect to an AP with 80 MHz, world regdom allows 80 MHz
304	 *  - AP advertises regdom US
305	 *  - CRDA loads regdom US with 80 MHz prohibited (old database)
306	 *  - the code below detects an unsupported channel, downgrades, and
307	 *    we disconnect from the AP in the caller
308	 *  - disconnect causes CRDA to reload world regdomain and the game
309	 *    starts anew.
310	 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
311	 *
312	 * It seems possible that there are still scenarios with CSA or real
313	 * bandwidth changes where a this could happen, but those cases are
314	 * less common and wouldn't completely prevent using the AP.
315	 */
316	if (tracking &&
317	    cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
318		return ret;
319
320	/* don't print the message below for VHT mismatch if VHT is disabled */
321	if (ret & IEEE80211_STA_DISABLE_VHT)
322		vht_chandef = *chandef;
323
324	/*
325	 * Ignore the DISABLED flag when we're already connected and only
326	 * tracking the APs beacon for bandwidth changes - otherwise we
327	 * might get disconnected here if we connect to an AP, update our
328	 * regulatory information based on the AP's country IE and the
329	 * information we have is wrong/outdated and disables the channel
330	 * that we're actually using for the connection to the AP.
331	 */
332	while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
333					tracking ? 0 :
334						   IEEE80211_CHAN_DISABLED)) {
335		if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
336			ret = IEEE80211_STA_DISABLE_HT |
337			      IEEE80211_STA_DISABLE_VHT |
338			      IEEE80211_STA_DISABLE_HE;
339			break;
340		}
341
342		ret |= ieee80211_chandef_downgrade(chandef);
343	}
344
345	if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
346						 IEEE80211_CHAN_NO_HE))
347		ret |= IEEE80211_STA_DISABLE_HE;
348
349	if (chandef->width != vht_chandef.width && !tracking)
350		sdata_info(sdata,
351			   "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
352
353	WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
354	return ret;
355}
356
357static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
358			       struct sta_info *sta,
359			       const struct ieee80211_ht_cap *ht_cap,
360			       const struct ieee80211_vht_cap *vht_cap,
361			       const struct ieee80211_ht_operation *ht_oper,
362			       const struct ieee80211_vht_operation *vht_oper,
363			       const struct ieee80211_he_operation *he_oper,
364			       const struct ieee80211_s1g_oper_ie *s1g_oper,
365			       const u8 *bssid, u32 *changed)
366{
367	struct ieee80211_local *local = sdata->local;
368	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
369	struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
370	struct ieee80211_supported_band *sband =
371		local->hw.wiphy->bands[chan->band];
372	struct cfg80211_chan_def chandef;
373	u16 ht_opmode;
374	u32 flags;
375	enum ieee80211_sta_rx_bandwidth new_sta_bw;
376	u32 vht_cap_info = 0;
377	int ret;
378
379	/* if HT was/is disabled, don't track any bandwidth changes */
380	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
381		return 0;
382
383	/* don't check VHT if we associated as non-VHT station */
384	if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
385		vht_oper = NULL;
386
387	/* don't check HE if we associated as non-HE station */
388	if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
389	    !ieee80211_get_he_sta_cap(sband))
390		he_oper = NULL;
391
392	if (WARN_ON_ONCE(!sta))
393		return -EINVAL;
394
395	/*
396	 * if bss configuration changed store the new one -
397	 * this may be applicable even if channel is identical
398	 */
399	ht_opmode = le16_to_cpu(ht_oper->operation_mode);
400	if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
401		*changed |= BSS_CHANGED_HT;
402		sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
403	}
404
405	if (vht_cap)
406		vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
407
408	/* calculate new channel (type) based on HT/VHT/HE operation IEs */
409	flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
410					     ht_oper, vht_oper, he_oper,
411					     s1g_oper, &chandef, true);
412
413	/*
414	 * Downgrade the new channel if we associated with restricted
415	 * capabilities. For example, if we associated as a 20 MHz STA
416	 * to a 40 MHz AP (due to regulatory, capabilities or config
417	 * reasons) then switching to a 40 MHz channel now won't do us
418	 * any good -- we couldn't use it with the AP.
419	 */
420	if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
421	    chandef.width == NL80211_CHAN_WIDTH_80P80)
422		flags |= ieee80211_chandef_downgrade(&chandef);
423	if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
424	    chandef.width == NL80211_CHAN_WIDTH_160)
425		flags |= ieee80211_chandef_downgrade(&chandef);
426	if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
427	    chandef.width > NL80211_CHAN_WIDTH_20)
428		flags |= ieee80211_chandef_downgrade(&chandef);
429
430	if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
431		return 0;
432
433	sdata_info(sdata,
434		   "AP %pM changed bandwidth, new config is %d.%03d MHz, "
435		   "width %d (%d.%03d/%d MHz)\n",
436		   ifmgd->bssid, chandef.chan->center_freq,
437		   chandef.chan->freq_offset, chandef.width,
438		   chandef.center_freq1, chandef.freq1_offset,
439		   chandef.center_freq2);
440
441	if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
442				      IEEE80211_STA_DISABLE_VHT |
443				      IEEE80211_STA_DISABLE_HE |
444				      IEEE80211_STA_DISABLE_40MHZ |
445				      IEEE80211_STA_DISABLE_80P80MHZ |
446				      IEEE80211_STA_DISABLE_160MHZ)) ||
447	    !cfg80211_chandef_valid(&chandef)) {
448		sdata_info(sdata,
449			   "AP %pM changed bandwidth in a way we can't support - disconnect\n",
450			   ifmgd->bssid);
451		return -EINVAL;
452	}
453
454	switch (chandef.width) {
455	case NL80211_CHAN_WIDTH_20_NOHT:
456	case NL80211_CHAN_WIDTH_20:
457		new_sta_bw = IEEE80211_STA_RX_BW_20;
458		break;
459	case NL80211_CHAN_WIDTH_40:
460		new_sta_bw = IEEE80211_STA_RX_BW_40;
461		break;
462	case NL80211_CHAN_WIDTH_80:
463		new_sta_bw = IEEE80211_STA_RX_BW_80;
464		break;
465	case NL80211_CHAN_WIDTH_80P80:
466	case NL80211_CHAN_WIDTH_160:
467		new_sta_bw = IEEE80211_STA_RX_BW_160;
468		break;
469	default:
470		return -EINVAL;
471	}
472
473	if (new_sta_bw > sta->cur_max_bandwidth)
474		new_sta_bw = sta->cur_max_bandwidth;
475
476	if (new_sta_bw < sta->sta.bandwidth) {
477		sta->sta.bandwidth = new_sta_bw;
478		rate_control_rate_update(local, sband, sta,
479					 IEEE80211_RC_BW_CHANGED);
480	}
481
482	ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
483	if (ret) {
484		sdata_info(sdata,
485			   "AP %pM changed bandwidth to incompatible one - disconnect\n",
486			   ifmgd->bssid);
487		return ret;
488	}
489
490	if (new_sta_bw > sta->sta.bandwidth) {
491		sta->sta.bandwidth = new_sta_bw;
492		rate_control_rate_update(local, sband, sta,
493					 IEEE80211_RC_BW_CHANGED);
494	}
495
496	return 0;
497}
498
499/* frame sending functions */
500
501static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
502				struct sk_buff *skb, u8 ap_ht_param,
503				struct ieee80211_supported_band *sband,
504				struct ieee80211_channel *channel,
505				enum ieee80211_smps_mode smps)
506{
507	u8 *pos;
508	u32 flags = channel->flags;
509	u16 cap;
510	struct ieee80211_sta_ht_cap ht_cap;
511
512	BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
513
514	memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
515	ieee80211_apply_htcap_overrides(sdata, &ht_cap);
516
517	/* determine capability flags */
518	cap = ht_cap.cap;
519
520	switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
521	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
522		if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
523			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
524			cap &= ~IEEE80211_HT_CAP_SGI_40;
525		}
526		break;
527	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
528		if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
529			cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
530			cap &= ~IEEE80211_HT_CAP_SGI_40;
531		}
532		break;
533	}
534
535	/*
536	 * If 40 MHz was disabled associate as though we weren't
537	 * capable of 40 MHz -- some broken APs will never fall
538	 * back to trying to transmit in 20 MHz.
539	 */
540	if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
541		cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
542		cap &= ~IEEE80211_HT_CAP_SGI_40;
543	}
544
545	/* set SM PS mode properly */
546	cap &= ~IEEE80211_HT_CAP_SM_PS;
547	switch (smps) {
548	case IEEE80211_SMPS_AUTOMATIC:
549	case IEEE80211_SMPS_NUM_MODES:
550		WARN_ON(1);
551		fallthrough;
552	case IEEE80211_SMPS_OFF:
553		cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
554			IEEE80211_HT_CAP_SM_PS_SHIFT;
555		break;
556	case IEEE80211_SMPS_STATIC:
557		cap |= WLAN_HT_CAP_SM_PS_STATIC <<
558			IEEE80211_HT_CAP_SM_PS_SHIFT;
559		break;
560	case IEEE80211_SMPS_DYNAMIC:
561		cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
562			IEEE80211_HT_CAP_SM_PS_SHIFT;
563		break;
564	}
565
566	/* reserve and fill IE */
567	pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
568	ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
569}
570
571/* This function determines vht capability flags for the association
572 * and builds the IE.
573 * Note - the function may set the owner of the MU-MIMO capability
574 */
575static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
576				 struct sk_buff *skb,
577				 struct ieee80211_supported_band *sband,
578				 struct ieee80211_vht_cap *ap_vht_cap)
579{
580	struct ieee80211_local *local = sdata->local;
581	u8 *pos;
582	u32 cap;
583	struct ieee80211_sta_vht_cap vht_cap;
584	u32 mask, ap_bf_sts, our_bf_sts;
585
586	BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
587
588	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
589	ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
590
591	/* determine capability flags */
592	cap = vht_cap.cap;
593
594	if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
595		u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
596
597		cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
598		if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
599		    bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
600			cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
601	}
602
603	if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
604		cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
605		cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
606	}
607
608	/*
609	 * Some APs apparently get confused if our capabilities are better
610	 * than theirs, so restrict what we advertise in the assoc request.
611	 */
612	if (!(ap_vht_cap->vht_cap_info &
613			cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
614		cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
615			 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
616	else if (!(ap_vht_cap->vht_cap_info &
617			cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
618		cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
619
620	/*
621	 * If some other vif is using the MU-MIMO capablity we cannot associate
622	 * using MU-MIMO - this will lead to contradictions in the group-id
623	 * mechanism.
624	 * Ownership is defined since association request, in order to avoid
625	 * simultaneous associations with MU-MIMO.
626	 */
627	if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
628		bool disable_mu_mimo = false;
629		struct ieee80211_sub_if_data *other;
630
631		list_for_each_entry_rcu(other, &local->interfaces, list) {
632			if (other->vif.mu_mimo_owner) {
633				disable_mu_mimo = true;
634				break;
635			}
636		}
637		if (disable_mu_mimo)
638			cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
639		else
640			sdata->vif.mu_mimo_owner = true;
641	}
642
643	mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
644
645	ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
646	our_bf_sts = cap & mask;
647
648	if (ap_bf_sts < our_bf_sts) {
649		cap &= ~mask;
650		cap |= ap_bf_sts;
651	}
652
653	/* reserve and fill IE */
654	pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
655	ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
656}
657
658/* This function determines HE capability flags for the association
659 * and builds the IE.
660 */
661static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
662				struct sk_buff *skb,
663				struct ieee80211_supported_band *sband)
664{
665	u8 *pos;
666	const struct ieee80211_sta_he_cap *he_cap = NULL;
667	struct ieee80211_chanctx_conf *chanctx_conf;
668	u8 he_cap_size;
669	bool reg_cap = false;
670
671	rcu_read_lock();
672	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
673	if (!WARN_ON_ONCE(!chanctx_conf))
674		reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
675						  &chanctx_conf->def,
676						  IEEE80211_CHAN_NO_HE);
677
678	rcu_read_unlock();
679
680	he_cap = ieee80211_get_he_sta_cap(sband);
681	if (!he_cap || !reg_cap)
682		return;
683
684	/*
685	 * TODO: the 1 added is because this temporarily is under the EXTENSION
686	 * IE. Get rid of it when it moves.
687	 */
688	he_cap_size =
689		2 + 1 + sizeof(he_cap->he_cap_elem) +
690		ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
691		ieee80211_he_ppe_size(he_cap->ppe_thres[0],
692				      he_cap->he_cap_elem.phy_cap_info);
693	pos = skb_put(skb, he_cap_size);
694	ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
695
696	ieee80211_ie_build_he_6ghz_cap(sdata, skb);
697}
698
699static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
700{
701	struct ieee80211_local *local = sdata->local;
702	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
703	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
704	struct sk_buff *skb;
705	struct ieee80211_mgmt *mgmt;
706	u8 *pos, qos_info, *ie_start;
707	size_t offset = 0, noffset;
708	int i, count, rates_len, supp_rates_len, shift;
709	u16 capab;
710	struct ieee80211_supported_band *sband;
711	struct ieee80211_chanctx_conf *chanctx_conf;
712	struct ieee80211_channel *chan;
713	u32 rates = 0;
714	__le16 listen_int;
715	struct element *ext_capa = NULL;
716
717	/* we know it's writable, cast away the const */
718	if (assoc_data->ie_len)
719		ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
720						      assoc_data->ie,
721						      assoc_data->ie_len);
722
723	sdata_assert_lock(sdata);
724
725	rcu_read_lock();
726	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
727	if (WARN_ON(!chanctx_conf)) {
728		rcu_read_unlock();
729		return;
730	}
731	chan = chanctx_conf->def.chan;
732	rcu_read_unlock();
733	sband = local->hw.wiphy->bands[chan->band];
734	shift = ieee80211_vif_get_shift(&sdata->vif);
735
736	if (assoc_data->supp_rates_len) {
737		/*
738		 * Get all rates supported by the device and the AP as
739		 * some APs don't like getting a superset of their rates
740		 * in the association request (e.g. D-Link DAP 1353 in
741		 * b-only mode)...
742		 */
743		rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
744						     assoc_data->supp_rates,
745						     assoc_data->supp_rates_len,
746						     &rates);
747	} else {
748		/*
749		 * In case AP not provide any supported rates information
750		 * before association, we send information element(s) with
751		 * all rates that we support.
752		 */
753		rates_len = 0;
754		for (i = 0; i < sband->n_bitrates; i++) {
755			rates |= BIT(i);
756			rates_len++;
757		}
758	}
759
760	skb = alloc_skb(local->hw.extra_tx_headroom +
761			sizeof(*mgmt) + /* bit too much but doesn't matter */
762			2 + assoc_data->ssid_len + /* SSID */
763			4 + rates_len + /* (extended) rates */
764			4 + /* power capability */
765			2 + 2 * sband->n_channels + /* supported channels */
766			2 + sizeof(struct ieee80211_ht_cap) + /* HT */
767			2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
768			2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
769				sizeof(struct ieee80211_he_mcs_nss_supp) +
770				IEEE80211_HE_PPE_THRES_MAX_LEN +
771			2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
772			assoc_data->ie_len + /* extra IEs */
773			(assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
774			9, /* WMM */
775			GFP_KERNEL);
776	if (!skb)
777		return;
778
779	skb_reserve(skb, local->hw.extra_tx_headroom);
780
781	capab = WLAN_CAPABILITY_ESS;
782
783	if (sband->band == NL80211_BAND_2GHZ) {
784		capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
785		capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
786	}
787
788	if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
789		capab |= WLAN_CAPABILITY_PRIVACY;
790
791	if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
792	    ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
793		capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
794
795	if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
796		capab |= WLAN_CAPABILITY_RADIO_MEASURE;
797
798	mgmt = skb_put_zero(skb, 24);
799	memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
800	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
801	memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
802
803	listen_int = cpu_to_le16(sband->band == NL80211_BAND_S1GHZ ?
804			ieee80211_encode_usf(local->hw.conf.listen_interval) :
805			local->hw.conf.listen_interval);
806	if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
807		skb_put(skb, 10);
808		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
809						  IEEE80211_STYPE_REASSOC_REQ);
810		mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
811		mgmt->u.reassoc_req.listen_interval = listen_int;
812		memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
813		       ETH_ALEN);
814	} else {
815		skb_put(skb, 4);
816		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
817						  IEEE80211_STYPE_ASSOC_REQ);
818		mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
819		mgmt->u.assoc_req.listen_interval = listen_int;
820	}
821
822	/* SSID */
823	pos = skb_put(skb, 2 + assoc_data->ssid_len);
824	ie_start = pos;
825	*pos++ = WLAN_EID_SSID;
826	*pos++ = assoc_data->ssid_len;
827	memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
828
829	if (sband->band == NL80211_BAND_S1GHZ)
830		goto skip_rates;
831
832	/* add all rates which were marked to be used above */
833	supp_rates_len = rates_len;
834	if (supp_rates_len > 8)
835		supp_rates_len = 8;
836
837	pos = skb_put(skb, supp_rates_len + 2);
838	*pos++ = WLAN_EID_SUPP_RATES;
839	*pos++ = supp_rates_len;
840
841	count = 0;
842	for (i = 0; i < sband->n_bitrates; i++) {
843		if (BIT(i) & rates) {
844			int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
845						5 * (1 << shift));
846			*pos++ = (u8) rate;
847			if (++count == 8)
848				break;
849		}
850	}
851
852	if (rates_len > count) {
853		pos = skb_put(skb, rates_len - count + 2);
854		*pos++ = WLAN_EID_EXT_SUPP_RATES;
855		*pos++ = rates_len - count;
856
857		for (i++; i < sband->n_bitrates; i++) {
858			if (BIT(i) & rates) {
859				int rate;
860				rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
861						    5 * (1 << shift));
862				*pos++ = (u8) rate;
863			}
864		}
865	}
866
867skip_rates:
868	if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
869	    capab & WLAN_CAPABILITY_RADIO_MEASURE) {
870		pos = skb_put(skb, 4);
871		*pos++ = WLAN_EID_PWR_CAPABILITY;
872		*pos++ = 2;
873		*pos++ = 0; /* min tx power */
874		 /* max tx power */
875		*pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
876	}
877
878	/*
879	 * Per spec, we shouldn't include the list of channels if we advertise
880	 * support for extended channel switching, but we've always done that;
881	 * (for now?) apply this restriction only on the (new) 6 GHz band.
882	 */
883	if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
884	    (sband->band != NL80211_BAND_6GHZ ||
885	     !ext_capa || ext_capa->datalen < 1 ||
886	     !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
887		/* TODO: get this in reg domain format */
888		pos = skb_put(skb, 2 * sband->n_channels + 2);
889		*pos++ = WLAN_EID_SUPPORTED_CHANNELS;
890		*pos++ = 2 * sband->n_channels;
891		for (i = 0; i < sband->n_channels; i++) {
892			*pos++ = ieee80211_frequency_to_channel(
893					sband->channels[i].center_freq);
894			*pos++ = 1; /* one channel in the subband*/
895		}
896	}
897
898	/* Set MBSSID support for HE AP if needed */
899	if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
900	    !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
901	    ext_capa && ext_capa->datalen >= 3)
902		ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
903
904	/* if present, add any custom IEs that go before HT */
905	if (assoc_data->ie_len) {
906		static const u8 before_ht[] = {
907			WLAN_EID_SSID,
908			WLAN_EID_SUPP_RATES,
909			WLAN_EID_EXT_SUPP_RATES,
910			WLAN_EID_PWR_CAPABILITY,
911			WLAN_EID_SUPPORTED_CHANNELS,
912			WLAN_EID_RSN,
913			WLAN_EID_QOS_CAPA,
914			WLAN_EID_RRM_ENABLED_CAPABILITIES,
915			WLAN_EID_MOBILITY_DOMAIN,
916			WLAN_EID_FAST_BSS_TRANSITION,	/* reassoc only */
917			WLAN_EID_RIC_DATA,		/* reassoc only */
918			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
919		};
920		static const u8 after_ric[] = {
921			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
922			WLAN_EID_HT_CAPABILITY,
923			WLAN_EID_BSS_COEX_2040,
924			/* luckily this is almost always there */
925			WLAN_EID_EXT_CAPABILITY,
926			WLAN_EID_QOS_TRAFFIC_CAPA,
927			WLAN_EID_TIM_BCAST_REQ,
928			WLAN_EID_INTERWORKING,
929			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
930			WLAN_EID_VHT_CAPABILITY,
931			WLAN_EID_OPMODE_NOTIF,
932		};
933
934		noffset = ieee80211_ie_split_ric(assoc_data->ie,
935						 assoc_data->ie_len,
936						 before_ht,
937						 ARRAY_SIZE(before_ht),
938						 after_ric,
939						 ARRAY_SIZE(after_ric),
940						 offset);
941		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
942		offset = noffset;
943	}
944
945	if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
946			 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
947		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
948
949	if (sband->band != NL80211_BAND_6GHZ &&
950	    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
951		ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
952				    sband, chan, sdata->smps_mode);
953
954	/* if present, add any custom IEs that go before VHT */
955	if (assoc_data->ie_len) {
956		static const u8 before_vht[] = {
957			/*
958			 * no need to list the ones split off before HT
959			 * or generated here
960			 */
961			WLAN_EID_BSS_COEX_2040,
962			WLAN_EID_EXT_CAPABILITY,
963			WLAN_EID_QOS_TRAFFIC_CAPA,
964			WLAN_EID_TIM_BCAST_REQ,
965			WLAN_EID_INTERWORKING,
966			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
967		};
968
969		/* RIC already taken above, so no need to handle here anymore */
970		noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
971					     before_vht, ARRAY_SIZE(before_vht),
972					     offset);
973		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
974		offset = noffset;
975	}
976
977	/* if present, add any custom IEs that go before HE */
978	if (assoc_data->ie_len) {
979		static const u8 before_he[] = {
980			/*
981			 * no need to list the ones split off before VHT
982			 * or generated here
983			 */
984			WLAN_EID_OPMODE_NOTIF,
985			WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
986			/* 11ai elements */
987			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
988			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
989			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
990			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
991			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
992			/* TODO: add 11ah/11aj/11ak elements */
993		};
994
995		/* RIC already taken above, so no need to handle here anymore */
996		noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
997					     before_he, ARRAY_SIZE(before_he),
998					     offset);
999		pos = skb_put(skb, noffset - offset);
1000		memcpy(pos, assoc_data->ie + offset, noffset - offset);
1001		offset = noffset;
1002	}
1003
1004	if (sband->band != NL80211_BAND_6GHZ &&
1005	    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
1006		ieee80211_add_vht_ie(sdata, skb, sband,
1007				     &assoc_data->ap_vht_cap);
1008
1009	/*
1010	 * If AP doesn't support HT, mark HE as disabled.
1011	 * If on the 5GHz band, make sure it supports VHT.
1012	 */
1013	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
1014	    (sband->band == NL80211_BAND_5GHZ &&
1015	     ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
1016		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
1017
1018	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1019		ieee80211_add_he_ie(sdata, skb, sband);
1020
1021	/* if present, add any custom non-vendor IEs that go after HE */
1022	if (assoc_data->ie_len) {
1023		noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1024						    assoc_data->ie_len,
1025						    offset);
1026		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1027		offset = noffset;
1028	}
1029
1030	if (assoc_data->wmm) {
1031		if (assoc_data->uapsd) {
1032			qos_info = ifmgd->uapsd_queues;
1033			qos_info |= (ifmgd->uapsd_max_sp_len <<
1034				     IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1035		} else {
1036			qos_info = 0;
1037		}
1038
1039		pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
1040	}
1041
1042	if (sband->band == NL80211_BAND_S1GHZ) {
1043		ieee80211_add_aid_request_ie(sdata, skb);
1044		ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
1045	}
1046
1047	/* add any remaining custom (i.e. vendor specific here) IEs */
1048	if (assoc_data->ie_len) {
1049		noffset = assoc_data->ie_len;
1050		skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1051	}
1052
1053	if (assoc_data->fils_kek_len &&
1054	    fils_encrypt_assoc_req(skb, assoc_data) < 0) {
1055		dev_kfree_skb(skb);
1056		return;
1057	}
1058
1059	pos = skb_tail_pointer(skb);
1060	kfree(ifmgd->assoc_req_ies);
1061	ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1062	ifmgd->assoc_req_ies_len = pos - ie_start;
1063
1064	drv_mgd_prepare_tx(local, sdata, 0);
1065
1066	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1067	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1068		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1069						IEEE80211_TX_INTFL_MLME_CONN_TX;
1070	ieee80211_tx_skb(sdata, skb);
1071}
1072
1073void ieee80211_send_pspoll(struct ieee80211_local *local,
1074			   struct ieee80211_sub_if_data *sdata)
1075{
1076	struct ieee80211_pspoll *pspoll;
1077	struct sk_buff *skb;
1078
1079	skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1080	if (!skb)
1081		return;
1082
1083	pspoll = (struct ieee80211_pspoll *) skb->data;
1084	pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1085
1086	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1087	ieee80211_tx_skb(sdata, skb);
1088}
1089
1090void ieee80211_send_nullfunc(struct ieee80211_local *local,
1091			     struct ieee80211_sub_if_data *sdata,
1092			     bool powersave)
1093{
1094	struct sk_buff *skb;
1095	struct ieee80211_hdr_3addr *nullfunc;
1096	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1097
1098	skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1099		!ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
1100	if (!skb)
1101		return;
1102
1103	nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1104	if (powersave)
1105		nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1106
1107	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1108					IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1109
1110	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1111		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1112
1113	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1114		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1115
1116	ieee80211_tx_skb(sdata, skb);
1117}
1118
1119void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1120				   struct ieee80211_sub_if_data *sdata)
1121{
1122	struct sk_buff *skb;
1123	struct ieee80211_hdr *nullfunc;
1124	__le16 fc;
1125
1126	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1127		return;
1128
1129	skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1130	if (!skb)
1131		return;
1132
1133	skb_reserve(skb, local->hw.extra_tx_headroom);
1134
1135	nullfunc = skb_put_zero(skb, 30);
1136	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1137			 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1138	nullfunc->frame_control = fc;
1139	memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1140	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1141	memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1142	memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1143
1144	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1145	ieee80211_tx_skb(sdata, skb);
1146}
1147
1148/* spectrum management related things */
1149static void ieee80211_chswitch_work(struct work_struct *work)
1150{
1151	struct ieee80211_sub_if_data *sdata =
1152		container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1153	struct ieee80211_local *local = sdata->local;
1154	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1155	int ret;
1156
1157	if (!ieee80211_sdata_running(sdata))
1158		return;
1159
1160	sdata_lock(sdata);
1161	mutex_lock(&local->mtx);
1162	mutex_lock(&local->chanctx_mtx);
1163
1164	if (!ifmgd->associated)
1165		goto out;
1166
1167	if (!sdata->vif.csa_active)
1168		goto out;
1169
1170	/*
1171	 * using reservation isn't immediate as it may be deferred until later
1172	 * with multi-vif. once reservation is complete it will re-schedule the
1173	 * work with no reserved_chanctx so verify chandef to check if it
1174	 * completed successfully
1175	 */
1176
1177	if (sdata->reserved_chanctx) {
1178		struct ieee80211_supported_band *sband = NULL;
1179		struct sta_info *mgd_sta = NULL;
1180		enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1181
1182		/*
1183		 * with multi-vif csa driver may call ieee80211_csa_finish()
1184		 * many times while waiting for other interfaces to use their
1185		 * reservations
1186		 */
1187		if (sdata->reserved_ready)
1188			goto out;
1189
1190		if (sdata->vif.bss_conf.chandef.width !=
1191		    sdata->csa_chandef.width) {
1192			/*
1193			 * For managed interface, we need to also update the AP
1194			 * station bandwidth and align the rate scale algorithm
1195			 * on the bandwidth change. Here we only consider the
1196			 * bandwidth of the new channel definition (as channel
1197			 * switch flow does not have the full HT/VHT/HE
1198			 * information), assuming that if additional changes are
1199			 * required they would be done as part of the processing
1200			 * of the next beacon from the AP.
1201			 */
1202			switch (sdata->csa_chandef.width) {
1203			case NL80211_CHAN_WIDTH_20_NOHT:
1204			case NL80211_CHAN_WIDTH_20:
1205			default:
1206				bw = IEEE80211_STA_RX_BW_20;
1207				break;
1208			case NL80211_CHAN_WIDTH_40:
1209				bw = IEEE80211_STA_RX_BW_40;
1210				break;
1211			case NL80211_CHAN_WIDTH_80:
1212				bw = IEEE80211_STA_RX_BW_80;
1213				break;
1214			case NL80211_CHAN_WIDTH_80P80:
1215			case NL80211_CHAN_WIDTH_160:
1216				bw = IEEE80211_STA_RX_BW_160;
1217				break;
1218			}
1219
1220			mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1221			sband =
1222				local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1223		}
1224
1225		if (sdata->vif.bss_conf.chandef.width >
1226		    sdata->csa_chandef.width) {
1227			mgd_sta->sta.bandwidth = bw;
1228			rate_control_rate_update(local, sband, mgd_sta,
1229						 IEEE80211_RC_BW_CHANGED);
1230		}
1231
1232		ret = ieee80211_vif_use_reserved_context(sdata);
1233		if (ret) {
1234			sdata_info(sdata,
1235				   "failed to use reserved channel context, disconnecting (err=%d)\n",
1236				   ret);
1237			ieee80211_queue_work(&sdata->local->hw,
1238					     &ifmgd->csa_connection_drop_work);
1239			goto out;
1240		}
1241
1242		if (sdata->vif.bss_conf.chandef.width <
1243		    sdata->csa_chandef.width) {
1244			mgd_sta->sta.bandwidth = bw;
1245			rate_control_rate_update(local, sband, mgd_sta,
1246						 IEEE80211_RC_BW_CHANGED);
1247		}
1248
1249		goto out;
1250	}
1251
1252	if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1253					&sdata->csa_chandef)) {
1254		sdata_info(sdata,
1255			   "failed to finalize channel switch, disconnecting\n");
1256		ieee80211_queue_work(&sdata->local->hw,
1257				     &ifmgd->csa_connection_drop_work);
1258		goto out;
1259	}
1260
1261	ifmgd->csa_waiting_bcn = true;
1262
1263	ieee80211_sta_reset_beacon_monitor(sdata);
1264	ieee80211_sta_reset_conn_monitor(sdata);
1265
1266out:
1267	mutex_unlock(&local->chanctx_mtx);
1268	mutex_unlock(&local->mtx);
1269	sdata_unlock(sdata);
1270}
1271
1272static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1273{
1274	struct ieee80211_local *local = sdata->local;
1275	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1276	int ret;
1277
1278	sdata_assert_lock(sdata);
1279
1280	WARN_ON(!sdata->vif.csa_active);
1281
1282	if (sdata->csa_block_tx) {
1283		ieee80211_wake_vif_queues(local, sdata,
1284					  IEEE80211_QUEUE_STOP_REASON_CSA);
1285		sdata->csa_block_tx = false;
1286	}
1287
1288	sdata->vif.csa_active = false;
1289	ifmgd->csa_waiting_bcn = false;
1290	/*
1291	 * If the CSA IE is still present on the beacon after the switch,
1292	 * we need to consider it as a new CSA (possibly to self).
1293	 */
1294	ifmgd->beacon_crc_valid = false;
1295
1296	ret = drv_post_channel_switch(sdata);
1297	if (ret) {
1298		sdata_info(sdata,
1299			   "driver post channel switch failed, disconnecting\n");
1300		ieee80211_queue_work(&local->hw,
1301				     &ifmgd->csa_connection_drop_work);
1302		return;
1303	}
1304
1305	cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1306}
1307
1308void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1309{
1310	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1311	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1312
1313	trace_api_chswitch_done(sdata, success);
1314	if (!success) {
1315		sdata_info(sdata,
1316			   "driver channel switch failed, disconnecting\n");
1317		ieee80211_queue_work(&sdata->local->hw,
1318				     &ifmgd->csa_connection_drop_work);
1319	} else {
1320		ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1321	}
1322}
1323EXPORT_SYMBOL(ieee80211_chswitch_done);
1324
1325static void ieee80211_chswitch_timer(struct timer_list *t)
1326{
1327	struct ieee80211_sub_if_data *sdata =
1328		from_timer(sdata, t, u.mgd.chswitch_timer);
1329
1330	ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1331}
1332
1333static void
1334ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1335{
1336	struct ieee80211_local *local = sdata->local;
1337
1338	if (!local->ops->abort_channel_switch)
1339		return;
1340
1341	mutex_lock(&local->mtx);
1342
1343	mutex_lock(&local->chanctx_mtx);
1344	ieee80211_vif_unreserve_chanctx(sdata);
1345	mutex_unlock(&local->chanctx_mtx);
1346
1347	if (sdata->csa_block_tx)
1348		ieee80211_wake_vif_queues(local, sdata,
1349					  IEEE80211_QUEUE_STOP_REASON_CSA);
1350
1351	sdata->csa_block_tx = false;
1352	sdata->vif.csa_active = false;
1353
1354	mutex_unlock(&local->mtx);
1355
1356	drv_abort_channel_switch(sdata);
1357}
1358
1359static void
1360ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1361				 u64 timestamp, u32 device_timestamp,
1362				 struct ieee802_11_elems *elems,
1363				 bool beacon)
1364{
1365	struct ieee80211_local *local = sdata->local;
1366	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1367	struct cfg80211_bss *cbss = ifmgd->associated;
1368	struct ieee80211_chanctx_conf *conf;
1369	struct ieee80211_chanctx *chanctx;
1370	enum nl80211_band current_band;
1371	struct ieee80211_csa_ie csa_ie;
1372	struct ieee80211_channel_switch ch_switch;
1373	struct ieee80211_bss *bss;
1374	int res;
1375
1376	sdata_assert_lock(sdata);
1377
1378	if (!cbss)
1379		return;
1380
1381	if (local->scanning)
1382		return;
1383
1384	current_band = cbss->channel->band;
1385	bss = (void *)cbss->priv;
1386	res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1387					   bss->vht_cap_info,
1388					   ifmgd->flags,
1389					   ifmgd->associated->bssid, &csa_ie);
1390
1391	if (!res) {
1392		ch_switch.timestamp = timestamp;
1393		ch_switch.device_timestamp = device_timestamp;
1394		ch_switch.block_tx = csa_ie.mode;
1395		ch_switch.chandef = csa_ie.chandef;
1396		ch_switch.count = csa_ie.count;
1397		ch_switch.delay = csa_ie.max_switch_time;
1398	}
1399
1400	if (res < 0) {
1401		ieee80211_queue_work(&local->hw,
1402				     &ifmgd->csa_connection_drop_work);
1403		return;
1404	}
1405
1406	if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1407		if (res)
1408			ieee80211_sta_abort_chanswitch(sdata);
1409		else
1410			drv_channel_switch_rx_beacon(sdata, &ch_switch);
1411		return;
1412	} else if (sdata->vif.csa_active || res) {
1413		/* disregard subsequent announcements if already processing */
1414		return;
1415	}
1416
1417	if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1418				     IEEE80211_CHAN_DISABLED)) {
1419		sdata_info(sdata,
1420			   "AP %pM switches to unsupported channel "
1421			   "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1422			   "disconnecting\n",
1423			   ifmgd->associated->bssid,
1424			   csa_ie.chandef.chan->center_freq,
1425			   csa_ie.chandef.chan->freq_offset,
1426			   csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1427			   csa_ie.chandef.freq1_offset,
1428			   csa_ie.chandef.center_freq2);
1429		ieee80211_queue_work(&local->hw,
1430				     &ifmgd->csa_connection_drop_work);
1431		return;
1432	}
1433
1434	if (cfg80211_chandef_identical(&csa_ie.chandef,
1435				       &sdata->vif.bss_conf.chandef) &&
1436	    (!csa_ie.mode || !beacon)) {
1437		if (ifmgd->csa_ignored_same_chan)
1438			return;
1439		sdata_info(sdata,
1440			   "AP %pM tries to chanswitch to same channel, ignore\n",
1441			   ifmgd->associated->bssid);
1442		ifmgd->csa_ignored_same_chan = true;
1443		return;
1444	}
1445
1446	/*
1447	 * Drop all TDLS peers - either we disconnect or move to a different
1448	 * channel from this point on. There's no telling what our peer will do.
1449	 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1450	 * have an incompatible wider chandef.
1451	 */
1452	ieee80211_teardown_tdls_peers(sdata);
1453
1454	mutex_lock(&local->mtx);
1455	mutex_lock(&local->chanctx_mtx);
1456	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1457					 lockdep_is_held(&local->chanctx_mtx));
1458	if (!conf) {
1459		sdata_info(sdata,
1460			   "no channel context assigned to vif?, disconnecting\n");
1461		goto drop_connection;
1462	}
1463
1464	chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1465
1466	if (local->use_chanctx &&
1467	    !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1468		sdata_info(sdata,
1469			   "driver doesn't support chan-switch with channel contexts\n");
1470		goto drop_connection;
1471	}
1472
1473	if (drv_pre_channel_switch(sdata, &ch_switch)) {
1474		sdata_info(sdata,
1475			   "preparing for channel switch failed, disconnecting\n");
1476		goto drop_connection;
1477	}
1478
1479	res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1480					    chanctx->mode, false);
1481	if (res) {
1482		sdata_info(sdata,
1483			   "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1484			   res);
1485		goto drop_connection;
1486	}
1487	mutex_unlock(&local->chanctx_mtx);
1488
1489	sdata->vif.csa_active = true;
1490	sdata->csa_chandef = csa_ie.chandef;
1491	sdata->csa_block_tx = csa_ie.mode;
1492	ifmgd->csa_ignored_same_chan = false;
1493
1494	if (sdata->csa_block_tx)
1495		ieee80211_stop_vif_queues(local, sdata,
1496					  IEEE80211_QUEUE_STOP_REASON_CSA);
1497	mutex_unlock(&local->mtx);
1498
1499	cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1500					  csa_ie.count);
1501
1502	if (local->ops->channel_switch) {
1503		/* use driver's channel switch callback */
1504		drv_channel_switch(local, sdata, &ch_switch);
1505		return;
1506	}
1507
1508	/* channel switch handled in software */
1509	if (csa_ie.count <= 1)
1510		ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1511	else
1512		mod_timer(&ifmgd->chswitch_timer,
1513			  TU_TO_EXP_TIME((csa_ie.count - 1) *
1514					 cbss->beacon_interval));
1515	return;
1516 drop_connection:
1517	/*
1518	 * This is just so that the disconnect flow will know that
1519	 * we were trying to switch channel and failed. In case the
1520	 * mode is 1 (we are not allowed to Tx), we will know not to
1521	 * send a deauthentication frame. Those two fields will be
1522	 * reset when the disconnection worker runs.
1523	 */
1524	sdata->vif.csa_active = true;
1525	sdata->csa_block_tx = csa_ie.mode;
1526
1527	ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1528	mutex_unlock(&local->chanctx_mtx);
1529	mutex_unlock(&local->mtx);
1530}
1531
1532static bool
1533ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1534				 struct ieee80211_channel *channel,
1535				 const u8 *country_ie, u8 country_ie_len,
1536				 const u8 *pwr_constr_elem,
1537				 int *chan_pwr, int *pwr_reduction)
1538{
1539	struct ieee80211_country_ie_triplet *triplet;
1540	int chan = ieee80211_frequency_to_channel(channel->center_freq);
1541	int i, chan_increment;
1542	bool have_chan_pwr = false;
1543
1544	/* Invalid IE */
1545	if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1546		return false;
1547
1548	triplet = (void *)(country_ie + 3);
1549	country_ie_len -= 3;
1550
1551	switch (channel->band) {
1552	default:
1553		WARN_ON_ONCE(1);
1554		fallthrough;
1555	case NL80211_BAND_2GHZ:
1556	case NL80211_BAND_60GHZ:
1557		chan_increment = 1;
1558		break;
1559	case NL80211_BAND_5GHZ:
1560	case NL80211_BAND_6GHZ:
1561		chan_increment = 4;
1562		break;
1563	}
1564
1565	/* find channel */
1566	while (country_ie_len >= 3) {
1567		u8 first_channel = triplet->chans.first_channel;
1568
1569		if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1570			goto next;
1571
1572		for (i = 0; i < triplet->chans.num_channels; i++) {
1573			if (first_channel + i * chan_increment == chan) {
1574				have_chan_pwr = true;
1575				*chan_pwr = triplet->chans.max_power;
1576				break;
1577			}
1578		}
1579		if (have_chan_pwr)
1580			break;
1581
1582 next:
1583		triplet++;
1584		country_ie_len -= 3;
1585	}
1586
1587	if (have_chan_pwr && pwr_constr_elem)
1588		*pwr_reduction = *pwr_constr_elem;
1589	else
1590		*pwr_reduction = 0;
1591
1592	return have_chan_pwr;
1593}
1594
1595static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1596				      struct ieee80211_channel *channel,
1597				      const u8 *cisco_dtpc_ie,
1598				      int *pwr_level)
1599{
1600	/* From practical testing, the first data byte of the DTPC element
1601	 * seems to contain the requested dBm level, and the CLI on Cisco
1602	 * APs clearly state the range is -127 to 127 dBm, which indicates
1603	 * a signed byte, although it seemingly never actually goes negative.
1604	 * The other byte seems to always be zero.
1605	 */
1606	*pwr_level = (__s8)cisco_dtpc_ie[4];
1607}
1608
1609static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1610				       struct ieee80211_channel *channel,
1611				       struct ieee80211_mgmt *mgmt,
1612				       const u8 *country_ie, u8 country_ie_len,
1613				       const u8 *pwr_constr_ie,
1614				       const u8 *cisco_dtpc_ie)
1615{
1616	bool has_80211h_pwr = false, has_cisco_pwr = false;
1617	int chan_pwr = 0, pwr_reduction_80211h = 0;
1618	int pwr_level_cisco, pwr_level_80211h;
1619	int new_ap_level;
1620	__le16 capab = mgmt->u.probe_resp.capab_info;
1621
1622	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
1623		return 0;	/* TODO */
1624
1625	if (country_ie &&
1626	    (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1627	     capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1628		has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1629			sdata, channel, country_ie, country_ie_len,
1630			pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1631		pwr_level_80211h =
1632			max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1633	}
1634
1635	if (cisco_dtpc_ie) {
1636		ieee80211_find_cisco_dtpc(
1637			sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1638		has_cisco_pwr = true;
1639	}
1640
1641	if (!has_80211h_pwr && !has_cisco_pwr)
1642		return 0;
1643
1644	/* If we have both 802.11h and Cisco DTPC, apply both limits
1645	 * by picking the smallest of the two power levels advertised.
1646	 */
1647	if (has_80211h_pwr &&
1648	    (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1649		new_ap_level = pwr_level_80211h;
1650
1651		if (sdata->ap_power_level == new_ap_level)
1652			return 0;
1653
1654		sdata_dbg(sdata,
1655			  "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1656			  pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1657			  sdata->u.mgd.bssid);
1658	} else {  /* has_cisco_pwr is always true here. */
1659		new_ap_level = pwr_level_cisco;
1660
1661		if (sdata->ap_power_level == new_ap_level)
1662			return 0;
1663
1664		sdata_dbg(sdata,
1665			  "Limiting TX power to %d dBm as advertised by %pM\n",
1666			  pwr_level_cisco, sdata->u.mgd.bssid);
1667	}
1668
1669	sdata->ap_power_level = new_ap_level;
1670	if (__ieee80211_recalc_txpower(sdata))
1671		return BSS_CHANGED_TXPOWER;
1672	return 0;
1673}
1674
1675/* powersave */
1676static void ieee80211_enable_ps(struct ieee80211_local *local,
1677				struct ieee80211_sub_if_data *sdata)
1678{
1679	struct ieee80211_conf *conf = &local->hw.conf;
1680
1681	/*
1682	 * If we are scanning right now then the parameters will
1683	 * take effect when scan finishes.
1684	 */
1685	if (local->scanning)
1686		return;
1687
1688	if (conf->dynamic_ps_timeout > 0 &&
1689	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1690		mod_timer(&local->dynamic_ps_timer, jiffies +
1691			  msecs_to_jiffies(conf->dynamic_ps_timeout));
1692	} else {
1693		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1694			ieee80211_send_nullfunc(local, sdata, true);
1695
1696		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1697		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1698			return;
1699
1700		conf->flags |= IEEE80211_CONF_PS;
1701		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1702	}
1703}
1704
1705static void ieee80211_change_ps(struct ieee80211_local *local)
1706{
1707	struct ieee80211_conf *conf = &local->hw.conf;
1708
1709	if (local->ps_sdata) {
1710		ieee80211_enable_ps(local, local->ps_sdata);
1711	} else if (conf->flags & IEEE80211_CONF_PS) {
1712		conf->flags &= ~IEEE80211_CONF_PS;
1713		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1714		del_timer_sync(&local->dynamic_ps_timer);
1715		cancel_work_sync(&local->dynamic_ps_enable_work);
1716	}
1717}
1718
1719static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1720{
1721	struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1722	struct sta_info *sta = NULL;
1723	bool authorized = false;
1724
1725	if (!mgd->powersave)
1726		return false;
1727
1728	if (mgd->broken_ap)
1729		return false;
1730
1731	if (!mgd->associated)
1732		return false;
1733
1734	if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1735		return false;
1736
1737	if (!mgd->have_beacon)
1738		return false;
1739
1740	rcu_read_lock();
1741	sta = sta_info_get(sdata, mgd->bssid);
1742	if (sta)
1743		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1744	rcu_read_unlock();
1745
1746	return authorized;
1747}
1748
1749/* need to hold RTNL or interface lock */
1750void ieee80211_recalc_ps(struct ieee80211_local *local)
1751{
1752	struct ieee80211_sub_if_data *sdata, *found = NULL;
1753	int count = 0;
1754	int timeout;
1755
1756	if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1757		local->ps_sdata = NULL;
1758		return;
1759	}
1760
1761	list_for_each_entry(sdata, &local->interfaces, list) {
1762		if (!ieee80211_sdata_running(sdata))
1763			continue;
1764		if (sdata->vif.type == NL80211_IFTYPE_AP) {
1765			/* If an AP vif is found, then disable PS
1766			 * by setting the count to zero thereby setting
1767			 * ps_sdata to NULL.
1768			 */
1769			count = 0;
1770			break;
1771		}
1772		if (sdata->vif.type != NL80211_IFTYPE_STATION)
1773			continue;
1774		found = sdata;
1775		count++;
1776	}
1777
1778	if (count == 1 && ieee80211_powersave_allowed(found)) {
1779		u8 dtimper = found->u.mgd.dtim_period;
1780
1781		timeout = local->dynamic_ps_forced_timeout;
1782		if (timeout < 0)
1783			timeout = 100;
1784		local->hw.conf.dynamic_ps_timeout = timeout;
1785
1786		/* If the TIM IE is invalid, pretend the value is 1 */
1787		if (!dtimper)
1788			dtimper = 1;
1789
1790		local->hw.conf.ps_dtim_period = dtimper;
1791		local->ps_sdata = found;
1792	} else {
1793		local->ps_sdata = NULL;
1794	}
1795
1796	ieee80211_change_ps(local);
1797}
1798
1799void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1800{
1801	bool ps_allowed = ieee80211_powersave_allowed(sdata);
1802
1803	if (sdata->vif.bss_conf.ps != ps_allowed) {
1804		sdata->vif.bss_conf.ps = ps_allowed;
1805		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1806	}
1807}
1808
1809void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1810{
1811	struct ieee80211_local *local =
1812		container_of(work, struct ieee80211_local,
1813			     dynamic_ps_disable_work);
1814
1815	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1816		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1817		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1818	}
1819
1820	ieee80211_wake_queues_by_reason(&local->hw,
1821					IEEE80211_MAX_QUEUE_MAP,
1822					IEEE80211_QUEUE_STOP_REASON_PS,
1823					false);
1824}
1825
1826void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1827{
1828	struct ieee80211_local *local =
1829		container_of(work, struct ieee80211_local,
1830			     dynamic_ps_enable_work);
1831	struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1832	struct ieee80211_if_managed *ifmgd;
1833	unsigned long flags;
1834	int q;
1835
1836	/* can only happen when PS was just disabled anyway */
1837	if (!sdata)
1838		return;
1839
1840	ifmgd = &sdata->u.mgd;
1841
1842	if (local->hw.conf.flags & IEEE80211_CONF_PS)
1843		return;
1844
1845	if (local->hw.conf.dynamic_ps_timeout > 0) {
1846		/* don't enter PS if TX frames are pending */
1847		if (drv_tx_frames_pending(local)) {
1848			mod_timer(&local->dynamic_ps_timer, jiffies +
1849				  msecs_to_jiffies(
1850				  local->hw.conf.dynamic_ps_timeout));
1851			return;
1852		}
1853
1854		/*
1855		 * transmission can be stopped by others which leads to
1856		 * dynamic_ps_timer expiry. Postpone the ps timer if it
1857		 * is not the actual idle state.
1858		 */
1859		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1860		for (q = 0; q < local->hw.queues; q++) {
1861			if (local->queue_stop_reasons[q]) {
1862				spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1863						       flags);
1864				mod_timer(&local->dynamic_ps_timer, jiffies +
1865					  msecs_to_jiffies(
1866					  local->hw.conf.dynamic_ps_timeout));
1867				return;
1868			}
1869		}
1870		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1871	}
1872
1873	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1874	    !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1875		if (drv_tx_frames_pending(local)) {
1876			mod_timer(&local->dynamic_ps_timer, jiffies +
1877				  msecs_to_jiffies(
1878				  local->hw.conf.dynamic_ps_timeout));
1879		} else {
1880			ieee80211_send_nullfunc(local, sdata, true);
1881			/* Flush to get the tx status of nullfunc frame */
1882			ieee80211_flush_queues(local, sdata, false);
1883		}
1884	}
1885
1886	if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1887	      ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1888	    (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1889		ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1890		local->hw.conf.flags |= IEEE80211_CONF_PS;
1891		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1892	}
1893}
1894
1895void ieee80211_dynamic_ps_timer(struct timer_list *t)
1896{
1897	struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1898
1899	ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1900}
1901
1902void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1903{
1904	struct delayed_work *delayed_work = to_delayed_work(work);
1905	struct ieee80211_sub_if_data *sdata =
1906		container_of(delayed_work, struct ieee80211_sub_if_data,
1907			     dfs_cac_timer_work);
1908	struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1909
1910	mutex_lock(&sdata->local->mtx);
1911	if (sdata->wdev.cac_started) {
1912		ieee80211_vif_release_channel(sdata);
1913		cfg80211_cac_event(sdata->dev, &chandef,
1914				   NL80211_RADAR_CAC_FINISHED,
1915				   GFP_KERNEL);
1916	}
1917	mutex_unlock(&sdata->local->mtx);
1918}
1919
1920static bool
1921__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1922{
1923	struct ieee80211_local *local = sdata->local;
1924	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1925	bool ret = false;
1926	int ac;
1927
1928	if (local->hw.queues < IEEE80211_NUM_ACS)
1929		return false;
1930
1931	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1932		struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1933		int non_acm_ac;
1934		unsigned long now = jiffies;
1935
1936		if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1937		    tx_tspec->admitted_time &&
1938		    time_after(now, tx_tspec->time_slice_start + HZ)) {
1939			tx_tspec->consumed_tx_time = 0;
1940			tx_tspec->time_slice_start = now;
1941
1942			if (tx_tspec->downgraded)
1943				tx_tspec->action =
1944					TX_TSPEC_ACTION_STOP_DOWNGRADE;
1945		}
1946
1947		switch (tx_tspec->action) {
1948		case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1949			/* take the original parameters */
1950			if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1951				sdata_err(sdata,
1952					  "failed to set TX queue parameters for queue %d\n",
1953					  ac);
1954			tx_tspec->action = TX_TSPEC_ACTION_NONE;
1955			tx_tspec->downgraded = false;
1956			ret = true;
1957			break;
1958		case TX_TSPEC_ACTION_DOWNGRADE:
1959			if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1960				tx_tspec->action = TX_TSPEC_ACTION_NONE;
1961				ret = true;
1962				break;
1963			}
1964			/* downgrade next lower non-ACM AC */
1965			for (non_acm_ac = ac + 1;
1966			     non_acm_ac < IEEE80211_NUM_ACS;
1967			     non_acm_ac++)
1968				if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1969					break;
1970			/* Usually the loop will result in using BK even if it
1971			 * requires admission control, but such a configuration
1972			 * makes no sense and we have to transmit somehow - the
1973			 * AC selection does the same thing.
1974			 * If we started out trying to downgrade from BK, then
1975			 * the extra condition here might be needed.
1976			 */
1977			if (non_acm_ac >= IEEE80211_NUM_ACS)
1978				non_acm_ac = IEEE80211_AC_BK;
1979			if (drv_conf_tx(local, sdata, ac,
1980					&sdata->tx_conf[non_acm_ac]))
1981				sdata_err(sdata,
1982					  "failed to set TX queue parameters for queue %d\n",
1983					  ac);
1984			tx_tspec->action = TX_TSPEC_ACTION_NONE;
1985			ret = true;
1986			schedule_delayed_work(&ifmgd->tx_tspec_wk,
1987				tx_tspec->time_slice_start + HZ - now + 1);
1988			break;
1989		case TX_TSPEC_ACTION_NONE:
1990			/* nothing now */
1991			break;
1992		}
1993	}
1994
1995	return ret;
1996}
1997
1998void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1999{
2000	if (__ieee80211_sta_handle_tspec_ac_params(sdata))
2001		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
2002}
2003
2004static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
2005{
2006	struct ieee80211_sub_if_data *sdata;
2007
2008	sdata = container_of(work, struct ieee80211_sub_if_data,
2009			     u.mgd.tx_tspec_wk.work);
2010	ieee80211_sta_handle_tspec_ac_params(sdata);
2011}
2012
2013/* MLME */
2014static bool
2015ieee80211_sta_wmm_params(struct ieee80211_local *local,
2016			 struct ieee80211_sub_if_data *sdata,
2017			 const u8 *wmm_param, size_t wmm_param_len,
2018			 const struct ieee80211_mu_edca_param_set *mu_edca)
2019{
2020	struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
2021	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2022	size_t left;
2023	int count, mu_edca_count, ac;
2024	const u8 *pos;
2025	u8 uapsd_queues = 0;
2026
2027	if (!local->ops->conf_tx)
2028		return false;
2029
2030	if (local->hw.queues < IEEE80211_NUM_ACS)
2031		return false;
2032
2033	if (!wmm_param)
2034		return false;
2035
2036	if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
2037		return false;
2038
2039	if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
2040		uapsd_queues = ifmgd->uapsd_queues;
2041
2042	count = wmm_param[6] & 0x0f;
2043	/* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2044	 * if mu_edca was preset before and now it disappeared tell
2045	 * the driver about it.
2046	 */
2047	mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2048	if (count == ifmgd->wmm_last_param_set &&
2049	    mu_edca_count == ifmgd->mu_edca_last_param_set)
2050		return false;
2051	ifmgd->wmm_last_param_set = count;
2052	ifmgd->mu_edca_last_param_set = mu_edca_count;
2053
2054	pos = wmm_param + 8;
2055	left = wmm_param_len - 8;
2056
2057	memset(&params, 0, sizeof(params));
2058
2059	sdata->wmm_acm = 0;
2060	for (; left >= 4; left -= 4, pos += 4) {
2061		int aci = (pos[0] >> 5) & 0x03;
2062		int acm = (pos[0] >> 4) & 0x01;
2063		bool uapsd = false;
2064
2065		switch (aci) {
2066		case 1: /* AC_BK */
2067			ac = IEEE80211_AC_BK;
2068			if (acm)
2069				sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
2070			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2071				uapsd = true;
2072			params[ac].mu_edca = !!mu_edca;
2073			if (mu_edca)
2074				params[ac].mu_edca_param_rec = mu_edca->ac_bk;
2075			break;
2076		case 2: /* AC_VI */
2077			ac = IEEE80211_AC_VI;
2078			if (acm)
2079				sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
2080			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2081				uapsd = true;
2082			params[ac].mu_edca = !!mu_edca;
2083			if (mu_edca)
2084				params[ac].mu_edca_param_rec = mu_edca->ac_vi;
2085			break;
2086		case 3: /* AC_VO */
2087			ac = IEEE80211_AC_VO;
2088			if (acm)
2089				sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
2090			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2091				uapsd = true;
2092			params[ac].mu_edca = !!mu_edca;
2093			if (mu_edca)
2094				params[ac].mu_edca_param_rec = mu_edca->ac_vo;
2095			break;
2096		case 0: /* AC_BE */
2097		default:
2098			ac = IEEE80211_AC_BE;
2099			if (acm)
2100				sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
2101			if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2102				uapsd = true;
2103			params[ac].mu_edca = !!mu_edca;
2104			if (mu_edca)
2105				params[ac].mu_edca_param_rec = mu_edca->ac_be;
2106			break;
2107		}
2108
2109		params[ac].aifs = pos[0] & 0x0f;
2110
2111		if (params[ac].aifs < 2) {
2112			sdata_info(sdata,
2113				   "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2114				   params[ac].aifs, aci);
2115			params[ac].aifs = 2;
2116		}
2117		params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2118		params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2119		params[ac].txop = get_unaligned_le16(pos + 2);
2120		params[ac].acm = acm;
2121		params[ac].uapsd = uapsd;
2122
2123		if (params[ac].cw_min == 0 ||
2124		    params[ac].cw_min > params[ac].cw_max) {
2125			sdata_info(sdata,
2126				   "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2127				   params[ac].cw_min, params[ac].cw_max, aci);
2128			return false;
2129		}
2130		ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
2131	}
2132
2133	/* WMM specification requires all 4 ACIs. */
2134	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2135		if (params[ac].cw_min == 0) {
2136			sdata_info(sdata,
2137				   "AP has invalid WMM params (missing AC %d), using defaults\n",
2138				   ac);
2139			return false;
2140		}
2141	}
2142
2143	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2144		mlme_dbg(sdata,
2145			 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2146			 ac, params[ac].acm,
2147			 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2148			 params[ac].txop, params[ac].uapsd,
2149			 ifmgd->tx_tspec[ac].downgraded);
2150		sdata->tx_conf[ac] = params[ac];
2151		if (!ifmgd->tx_tspec[ac].downgraded &&
2152		    drv_conf_tx(local, sdata, ac, &params[ac]))
2153			sdata_err(sdata,
2154				  "failed to set TX queue parameters for AC %d\n",
2155				  ac);
2156	}
2157
2158	/* enable WMM or activate new settings */
2159	sdata->vif.bss_conf.qos = true;
2160	return true;
2161}
2162
2163static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2164{
2165	lockdep_assert_held(&sdata->local->mtx);
2166
2167	sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
2168	ieee80211_run_deferred_scan(sdata->local);
2169}
2170
2171static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2172{
2173	mutex_lock(&sdata->local->mtx);
2174	__ieee80211_stop_poll(sdata);
2175	mutex_unlock(&sdata->local->mtx);
2176}
2177
2178static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2179					   u16 capab, bool erp_valid, u8 erp)
2180{
2181	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2182	struct ieee80211_supported_band *sband;
2183	u32 changed = 0;
2184	bool use_protection;
2185	bool use_short_preamble;
2186	bool use_short_slot;
2187
2188	sband = ieee80211_get_sband(sdata);
2189	if (!sband)
2190		return changed;
2191
2192	if (erp_valid) {
2193		use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2194		use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2195	} else {
2196		use_protection = false;
2197		use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2198	}
2199
2200	use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
2201	if (sband->band == NL80211_BAND_5GHZ ||
2202	    sband->band == NL80211_BAND_6GHZ)
2203		use_short_slot = true;
2204
2205	if (use_protection != bss_conf->use_cts_prot) {
2206		bss_conf->use_cts_prot = use_protection;
2207		changed |= BSS_CHANGED_ERP_CTS_PROT;
2208	}
2209
2210	if (use_short_preamble != bss_conf->use_short_preamble) {
2211		bss_conf->use_short_preamble = use_short_preamble;
2212		changed |= BSS_CHANGED_ERP_PREAMBLE;
2213	}
2214
2215	if (use_short_slot != bss_conf->use_short_slot) {
2216		bss_conf->use_short_slot = use_short_slot;
2217		changed |= BSS_CHANGED_ERP_SLOT;
2218	}
2219
2220	return changed;
2221}
2222
2223static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
2224				     struct cfg80211_bss *cbss,
2225				     u32 bss_info_changed)
2226{
2227	struct ieee80211_bss *bss = (void *)cbss->priv;
2228	struct ieee80211_local *local = sdata->local;
2229	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2230
2231	bss_info_changed |= BSS_CHANGED_ASSOC;
2232	bss_info_changed |= ieee80211_handle_bss_capability(sdata,
2233		bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2234
2235	sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2236		beacon_loss_count * bss_conf->beacon_int));
2237
2238	sdata->u.mgd.associated = cbss;
2239	memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2240
2241	ieee80211_check_rate_mask(sdata);
2242
2243	sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2244
2245	if (sdata->vif.p2p ||
2246	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2247		const struct cfg80211_bss_ies *ies;
2248
2249		rcu_read_lock();
2250		ies = rcu_dereference(cbss->ies);
2251		if (ies) {
2252			int ret;
2253
2254			ret = cfg80211_get_p2p_attr(
2255					ies->data, ies->len,
2256					IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2257					(u8 *) &bss_conf->p2p_noa_attr,
2258					sizeof(bss_conf->p2p_noa_attr));
2259			if (ret >= 2) {
2260				sdata->u.mgd.p2p_noa_index =
2261					bss_conf->p2p_noa_attr.index;
2262				bss_info_changed |= BSS_CHANGED_P2P_PS;
2263			}
2264		}
2265		rcu_read_unlock();
2266	}
2267
2268	/* just to be sure */
2269	ieee80211_stop_poll(sdata);
2270
2271	ieee80211_led_assoc(local, 1);
2272
2273	if (sdata->u.mgd.have_beacon) {
2274		/*
2275		 * If the AP is buggy we may get here with no DTIM period
2276		 * known, so assume it's 1 which is the only safe assumption
2277		 * in that case, although if the TIM IE is broken powersave
2278		 * probably just won't work at all.
2279		 */
2280		bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2281		bss_conf->beacon_rate = bss->beacon_rate;
2282		bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2283	} else {
2284		bss_conf->beacon_rate = NULL;
2285		bss_conf->dtim_period = 0;
2286	}
2287
2288	bss_conf->assoc = 1;
2289
2290	/* Tell the driver to monitor connection quality (if supported) */
2291	if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2292	    bss_conf->cqm_rssi_thold)
2293		bss_info_changed |= BSS_CHANGED_CQM;
2294
2295	/* Enable ARP filtering */
2296	if (bss_conf->arp_addr_cnt)
2297		bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2298
2299	ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2300
2301	mutex_lock(&local->iflist_mtx);
2302	ieee80211_recalc_ps(local);
2303	mutex_unlock(&local->iflist_mtx);
2304
2305	ieee80211_recalc_smps(sdata);
2306	ieee80211_recalc_ps_vif(sdata);
2307
2308	netif_carrier_on(sdata->dev);
2309}
2310
2311static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2312				   u16 stype, u16 reason, bool tx,
2313				   u8 *frame_buf)
2314{
2315	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2316	struct ieee80211_local *local = sdata->local;
2317	u32 changed = 0;
2318
2319	sdata_assert_lock(sdata);
2320
2321	if (WARN_ON_ONCE(tx && !frame_buf))
2322		return;
2323
2324	if (WARN_ON(!ifmgd->associated))
2325		return;
2326
2327	ieee80211_stop_poll(sdata);
2328
2329	ifmgd->associated = NULL;
2330	netif_carrier_off(sdata->dev);
2331
2332	/*
2333	 * if we want to get out of ps before disassoc (why?) we have
2334	 * to do it before sending disassoc, as otherwise the null-packet
2335	 * won't be valid.
2336	 */
2337	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2338		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2339		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2340	}
2341	local->ps_sdata = NULL;
2342
2343	/* disable per-vif ps */
2344	ieee80211_recalc_ps_vif(sdata);
2345
2346	/* make sure ongoing transmission finishes */
2347	synchronize_net();
2348
2349	/*
2350	 * drop any frame before deauth/disassoc, this can be data or
2351	 * management frame. Since we are disconnecting, we should not
2352	 * insist sending these frames which can take time and delay
2353	 * the disconnection and possible the roaming.
2354	 */
2355	if (tx)
2356		ieee80211_flush_queues(local, sdata, true);
2357
2358	/* deauthenticate/disassociate now */
2359	if (tx || frame_buf) {
2360		/*
2361		 * In multi channel scenarios guarantee that the virtual
2362		 * interface is granted immediate airtime to transmit the
2363		 * deauthentication frame by calling mgd_prepare_tx, if the
2364		 * driver requested so.
2365		 */
2366		if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2367		    !ifmgd->have_beacon)
2368			drv_mgd_prepare_tx(sdata->local, sdata, 0);
2369
2370		ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2371					       ifmgd->bssid, stype, reason,
2372					       tx, frame_buf);
2373	}
2374
2375	/* flush out frame - make sure the deauth was actually sent */
2376	if (tx)
2377		ieee80211_flush_queues(local, sdata, false);
2378
2379	/* clear bssid only after building the needed mgmt frames */
2380	eth_zero_addr(ifmgd->bssid);
2381
2382	/* remove AP and TDLS peers */
2383	sta_info_flush(sdata);
2384
2385	/* finally reset all BSS / config parameters */
2386	changed |= ieee80211_reset_erp_info(sdata);
2387
2388	ieee80211_led_assoc(local, 0);
2389	changed |= BSS_CHANGED_ASSOC;
2390	sdata->vif.bss_conf.assoc = false;
2391
2392	ifmgd->p2p_noa_index = -1;
2393	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2394	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2395
2396	/* on the next assoc, re-program HT/VHT parameters */
2397	memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2398	memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2399	memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2400	memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2401
2402	/* reset MU-MIMO ownership and group data */
2403	memset(sdata->vif.bss_conf.mu_group.membership, 0,
2404	       sizeof(sdata->vif.bss_conf.mu_group.membership));
2405	memset(sdata->vif.bss_conf.mu_group.position, 0,
2406	       sizeof(sdata->vif.bss_conf.mu_group.position));
2407	changed |= BSS_CHANGED_MU_GROUPS;
2408	sdata->vif.mu_mimo_owner = false;
2409
2410	sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2411
2412	del_timer_sync(&local->dynamic_ps_timer);
2413	cancel_work_sync(&local->dynamic_ps_enable_work);
2414
2415	/* Disable ARP filtering */
2416	if (sdata->vif.bss_conf.arp_addr_cnt)
2417		changed |= BSS_CHANGED_ARP_FILTER;
2418
2419	sdata->vif.bss_conf.qos = false;
2420	changed |= BSS_CHANGED_QOS;
2421
2422	/* The BSSID (not really interesting) and HT changed */
2423	changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2424	ieee80211_bss_info_change_notify(sdata, changed);
2425
2426	/* disassociated - set to defaults now */
2427	ieee80211_set_wmm_default(sdata, false, false);
2428
2429	del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2430	del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2431	del_timer_sync(&sdata->u.mgd.timer);
2432	del_timer_sync(&sdata->u.mgd.chswitch_timer);
2433
2434	sdata->vif.bss_conf.dtim_period = 0;
2435	sdata->vif.bss_conf.beacon_rate = NULL;
2436
2437	ifmgd->have_beacon = false;
2438
2439	ifmgd->flags = 0;
2440	mutex_lock(&local->mtx);
2441	ieee80211_vif_release_channel(sdata);
2442
2443	sdata->vif.csa_active = false;
2444	ifmgd->csa_waiting_bcn = false;
2445	ifmgd->csa_ignored_same_chan = false;
2446	if (sdata->csa_block_tx) {
2447		ieee80211_wake_vif_queues(local, sdata,
2448					  IEEE80211_QUEUE_STOP_REASON_CSA);
2449		sdata->csa_block_tx = false;
2450	}
2451	mutex_unlock(&local->mtx);
2452
2453	/* existing TX TSPEC sessions no longer exist */
2454	memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2455	cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2456
2457	sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2458}
2459
2460static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2461{
2462	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2463	struct ieee80211_local *local = sdata->local;
2464
2465	mutex_lock(&local->mtx);
2466	if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2467		goto out;
2468
2469	__ieee80211_stop_poll(sdata);
2470
2471	mutex_lock(&local->iflist_mtx);
2472	ieee80211_recalc_ps(local);
2473	mutex_unlock(&local->iflist_mtx);
2474
2475	if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2476		goto out;
2477
2478	/*
2479	 * We've received a probe response, but are not sure whether
2480	 * we have or will be receiving any beacons or data, so let's
2481	 * schedule the timers again, just in case.
2482	 */
2483	ieee80211_sta_reset_beacon_monitor(sdata);
2484
2485	mod_timer(&ifmgd->conn_mon_timer,
2486		  round_jiffies_up(jiffies +
2487				   IEEE80211_CONNECTION_IDLE_TIME));
2488out:
2489	mutex_unlock(&local->mtx);
2490}
2491
2492static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2493					   struct ieee80211_hdr *hdr,
2494					   u16 tx_time)
2495{
2496	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2497	u16 tid;
2498	int ac;
2499	struct ieee80211_sta_tx_tspec *tx_tspec;
2500	unsigned long now = jiffies;
2501
2502	if (!ieee80211_is_data_qos(hdr->frame_control))
2503		return;
2504
2505	tid = ieee80211_get_tid(hdr);
2506	ac = ieee80211_ac_from_tid(tid);
2507	tx_tspec = &ifmgd->tx_tspec[ac];
2508
2509	if (likely(!tx_tspec->admitted_time))
2510		return;
2511
2512	if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2513		tx_tspec->consumed_tx_time = 0;
2514		tx_tspec->time_slice_start = now;
2515
2516		if (tx_tspec->downgraded) {
2517			tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2518			schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2519		}
2520	}
2521
2522	if (tx_tspec->downgraded)
2523		return;
2524
2525	tx_tspec->consumed_tx_time += tx_time;
2526
2527	if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2528		tx_tspec->downgraded = true;
2529		tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2530		schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2531	}
2532}
2533
2534void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2535			     struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2536{
2537	ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2538
2539	if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
2540	    !sdata->u.mgd.probe_send_count)
2541		return;
2542
2543	if (ack)
2544		sdata->u.mgd.probe_send_count = 0;
2545	else
2546		sdata->u.mgd.nullfunc_failed = true;
2547	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2548}
2549
2550static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2551					  const u8 *src, const u8 *dst,
2552					  const u8 *ssid, size_t ssid_len,
2553					  struct ieee80211_channel *channel)
2554{
2555	struct sk_buff *skb;
2556
2557	skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2558					ssid, ssid_len, NULL, 0,
2559					IEEE80211_PROBE_FLAG_DIRECTED);
2560	if (skb)
2561		ieee80211_tx_skb(sdata, skb);
2562}
2563
2564static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2565{
2566	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2567	const u8 *ssid;
2568	u8 *dst = ifmgd->associated->bssid;
2569	u8 unicast_limit = max(1, max_probe_tries - 3);
2570	struct sta_info *sta;
2571
2572	/*
2573	 * Try sending broadcast probe requests for the last three
2574	 * probe requests after the first ones failed since some
2575	 * buggy APs only support broadcast probe requests.
2576	 */
2577	if (ifmgd->probe_send_count >= unicast_limit)
2578		dst = NULL;
2579
2580	/*
2581	 * When the hardware reports an accurate Tx ACK status, it's
2582	 * better to send a nullfunc frame instead of a probe request,
2583	 * as it will kick us off the AP quickly if we aren't associated
2584	 * anymore. The timeout will be reset if the frame is ACKed by
2585	 * the AP.
2586	 */
2587	ifmgd->probe_send_count++;
2588
2589	if (dst) {
2590		mutex_lock(&sdata->local->sta_mtx);
2591		sta = sta_info_get(sdata, dst);
2592		if (!WARN_ON(!sta))
2593			ieee80211_check_fast_rx(sta);
2594		mutex_unlock(&sdata->local->sta_mtx);
2595	}
2596
2597	if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2598		ifmgd->nullfunc_failed = false;
2599		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2600			ifmgd->probe_send_count--;
2601		else
2602			ieee80211_send_nullfunc(sdata->local, sdata, false);
2603	} else {
2604		int ssid_len;
2605
2606		rcu_read_lock();
2607		ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2608		if (WARN_ON_ONCE(ssid == NULL))
2609			ssid_len = 0;
2610		else
2611			ssid_len = ssid[1];
2612
2613		ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2614					      ssid + 2, ssid_len,
2615					      ifmgd->associated->channel);
2616		rcu_read_unlock();
2617	}
2618
2619	ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2620	run_again(sdata, ifmgd->probe_timeout);
2621}
2622
2623static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2624				   bool beacon)
2625{
2626	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2627	bool already = false;
2628
2629	if (!ieee80211_sdata_running(sdata))
2630		return;
2631
2632	sdata_lock(sdata);
2633
2634	if (!ifmgd->associated)
2635		goto out;
2636
2637	mutex_lock(&sdata->local->mtx);
2638
2639	if (sdata->local->tmp_channel || sdata->local->scanning) {
2640		mutex_unlock(&sdata->local->mtx);
2641		goto out;
2642	}
2643
2644	if (beacon) {
2645		mlme_dbg_ratelimited(sdata,
2646				     "detected beacon loss from AP (missed %d beacons) - probing\n",
2647				     beacon_loss_count);
2648
2649		ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2650	}
2651
2652	/*
2653	 * The driver/our work has already reported this event or the
2654	 * connection monitoring has kicked in and we have already sent
2655	 * a probe request. Or maybe the AP died and the driver keeps
2656	 * reporting until we disassociate...
2657	 *
2658	 * In either case we have to ignore the current call to this
2659	 * function (except for setting the correct probe reason bit)
2660	 * because otherwise we would reset the timer every time and
2661	 * never check whether we received a probe response!
2662	 */
2663	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2664		already = true;
2665
2666	ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2667
2668	mutex_unlock(&sdata->local->mtx);
2669
2670	if (already)
2671		goto out;
2672
2673	mutex_lock(&sdata->local->iflist_mtx);
2674	ieee80211_recalc_ps(sdata->local);
2675	mutex_unlock(&sdata->local->iflist_mtx);
2676
2677	ifmgd->probe_send_count = 0;
2678	ieee80211_mgd_probe_ap_send(sdata);
2679 out:
2680	sdata_unlock(sdata);
2681}
2682
2683struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2684					  struct ieee80211_vif *vif)
2685{
2686	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2687	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2688	struct cfg80211_bss *cbss;
2689	struct sk_buff *skb;
2690	const u8 *ssid;
2691	int ssid_len;
2692
2693	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2694		return NULL;
2695
2696	sdata_assert_lock(sdata);
2697
2698	if (ifmgd->associated)
2699		cbss = ifmgd->associated;
2700	else if (ifmgd->auth_data)
2701		cbss = ifmgd->auth_data->bss;
2702	else if (ifmgd->assoc_data)
2703		cbss = ifmgd->assoc_data->bss;
2704	else
2705		return NULL;
2706
2707	rcu_read_lock();
2708	ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2709	if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2710		      "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
2711		ssid_len = 0;
2712	else
2713		ssid_len = ssid[1];
2714
2715	skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2716					(u32) -1, cbss->channel,
2717					ssid + 2, ssid_len,
2718					NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2719	rcu_read_unlock();
2720
2721	return skb;
2722}
2723EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2724
2725static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2726					const u8 *buf, size_t len, bool tx,
2727					u16 reason)
2728{
2729	struct ieee80211_event event = {
2730		.type = MLME_EVENT,
2731		.u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2732		.u.mlme.reason = reason,
2733	};
2734
2735	if (tx)
2736		cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2737	else
2738		cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2739
2740	drv_event_callback(sdata->local, sdata, &event);
2741}
2742
2743static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2744{
2745	struct ieee80211_local *local = sdata->local;
2746	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2747	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2748	bool tx;
2749
2750	sdata_lock(sdata);
2751	if (!ifmgd->associated) {
2752		sdata_unlock(sdata);
2753		return;
2754	}
2755
2756	tx = !sdata->csa_block_tx;
2757
2758	/* AP is probably out of range (or not reachable for another reason) so
2759	 * remove the bss struct for that AP.
2760	 */
2761	cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2762
2763	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2764			       WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2765			       tx, frame_buf);
2766	mutex_lock(&local->mtx);
2767	sdata->vif.csa_active = false;
2768	ifmgd->csa_waiting_bcn = false;
2769	if (sdata->csa_block_tx) {
2770		ieee80211_wake_vif_queues(local, sdata,
2771					  IEEE80211_QUEUE_STOP_REASON_CSA);
2772		sdata->csa_block_tx = false;
2773	}
2774	mutex_unlock(&local->mtx);
2775
2776	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
2777				    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2778
2779	sdata_unlock(sdata);
2780}
2781
2782static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2783{
2784	struct ieee80211_sub_if_data *sdata =
2785		container_of(work, struct ieee80211_sub_if_data,
2786			     u.mgd.beacon_connection_loss_work);
2787	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2788
2789	if (ifmgd->associated)
2790		ifmgd->beacon_loss_count++;
2791
2792	if (ifmgd->connection_loss) {
2793		sdata_info(sdata, "Connection to AP %pM lost\n",
2794			   ifmgd->bssid);
2795		__ieee80211_disconnect(sdata);
2796	} else {
2797		ieee80211_mgd_probe_ap(sdata, true);
2798	}
2799}
2800
2801static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2802{
2803	struct ieee80211_sub_if_data *sdata =
2804		container_of(work, struct ieee80211_sub_if_data,
2805			     u.mgd.csa_connection_drop_work);
2806
2807	__ieee80211_disconnect(sdata);
2808}
2809
2810void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2811{
2812	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2813	struct ieee80211_hw *hw = &sdata->local->hw;
2814
2815	trace_api_beacon_loss(sdata);
2816
2817	sdata->u.mgd.connection_loss = false;
2818	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2819}
2820EXPORT_SYMBOL(ieee80211_beacon_loss);
2821
2822void ieee80211_connection_loss(struct ieee80211_vif *vif)
2823{
2824	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2825	struct ieee80211_hw *hw = &sdata->local->hw;
2826
2827	trace_api_connection_loss(sdata);
2828
2829	sdata->u.mgd.connection_loss = true;
2830	ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2831}
2832EXPORT_SYMBOL(ieee80211_connection_loss);
2833
2834
2835static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2836					bool assoc)
2837{
2838	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2839
2840	sdata_assert_lock(sdata);
2841
2842	if (!assoc) {
2843		/*
2844		 * we are not authenticated yet, the only timer that could be
2845		 * running is the timeout for the authentication response which
2846		 * which is not relevant anymore.
2847		 */
2848		del_timer_sync(&sdata->u.mgd.timer);
2849		sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2850
2851		eth_zero_addr(sdata->u.mgd.bssid);
2852		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2853		sdata->u.mgd.flags = 0;
2854		mutex_lock(&sdata->local->mtx);
2855		ieee80211_vif_release_channel(sdata);
2856		mutex_unlock(&sdata->local->mtx);
2857	}
2858
2859	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2860	kfree(auth_data);
2861	sdata->u.mgd.auth_data = NULL;
2862}
2863
2864static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2865					 bool assoc, bool abandon)
2866{
2867	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2868
2869	sdata_assert_lock(sdata);
2870
2871	if (!assoc) {
2872		/*
2873		 * we are not associated yet, the only timer that could be
2874		 * running is the timeout for the association response which
2875		 * which is not relevant anymore.
2876		 */
2877		del_timer_sync(&sdata->u.mgd.timer);
2878		sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2879
2880		eth_zero_addr(sdata->u.mgd.bssid);
2881		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2882		sdata->u.mgd.flags = 0;
2883		sdata->vif.mu_mimo_owner = false;
2884
2885		mutex_lock(&sdata->local->mtx);
2886		ieee80211_vif_release_channel(sdata);
2887		mutex_unlock(&sdata->local->mtx);
2888
2889		if (abandon)
2890			cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2891	}
2892
2893	kfree(assoc_data);
2894	sdata->u.mgd.assoc_data = NULL;
2895}
2896
2897static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2898				     struct ieee80211_mgmt *mgmt, size_t len)
2899{
2900	struct ieee80211_local *local = sdata->local;
2901	struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2902	const struct element *challenge;
2903	u8 *pos;
2904	u32 tx_flags = 0;
2905
2906	pos = mgmt->u.auth.variable;
2907	challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
2908				       len - (pos - (u8 *)mgmt));
2909	if (!challenge)
2910		return;
2911	auth_data->expected_transaction = 4;
2912	drv_mgd_prepare_tx(sdata->local, sdata, 0);
2913	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2914		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2915			   IEEE80211_TX_INTFL_MLME_CONN_TX;
2916	ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2917			    (void *)challenge,
2918			    challenge->datalen + sizeof(*challenge),
2919			    auth_data->bss->bssid, auth_data->bss->bssid,
2920			    auth_data->key, auth_data->key_len,
2921			    auth_data->key_idx, tx_flags);
2922}
2923
2924static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2925				    const u8 *bssid)
2926{
2927	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2928	struct sta_info *sta;
2929	bool result = true;
2930
2931	sdata_info(sdata, "authenticated\n");
2932	ifmgd->auth_data->done = true;
2933	ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2934	ifmgd->auth_data->timeout_started = true;
2935	run_again(sdata, ifmgd->auth_data->timeout);
2936
2937	/* move station state to auth */
2938	mutex_lock(&sdata->local->sta_mtx);
2939	sta = sta_info_get(sdata, bssid);
2940	if (!sta) {
2941		WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2942		result = false;
2943		goto out;
2944	}
2945	if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2946		sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2947		result = false;
2948		goto out;
2949	}
2950
2951out:
2952	mutex_unlock(&sdata->local->sta_mtx);
2953	return result;
2954}
2955
2956static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2957				   struct ieee80211_mgmt *mgmt, size_t len)
2958{
2959	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2960	u8 bssid[ETH_ALEN];
2961	u16 auth_alg, auth_transaction, status_code;
2962	struct ieee80211_event event = {
2963		.type = MLME_EVENT,
2964		.u.mlme.data = AUTH_EVENT,
2965	};
2966
2967	sdata_assert_lock(sdata);
2968
2969	if (len < 24 + 6)
2970		return;
2971
2972	if (!ifmgd->auth_data || ifmgd->auth_data->done)
2973		return;
2974
2975	memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2976
2977	if (!ether_addr_equal(bssid, mgmt->bssid))
2978		return;
2979
2980	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2981	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2982	status_code = le16_to_cpu(mgmt->u.auth.status_code);
2983
2984	if (auth_alg != ifmgd->auth_data->algorithm ||
2985	    (auth_alg != WLAN_AUTH_SAE &&
2986	     auth_transaction != ifmgd->auth_data->expected_transaction) ||
2987	    (auth_alg == WLAN_AUTH_SAE &&
2988	     (auth_transaction < ifmgd->auth_data->expected_transaction ||
2989	      auth_transaction > 2))) {
2990		sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2991			   mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2992			   auth_transaction,
2993			   ifmgd->auth_data->expected_transaction);
2994		return;
2995	}
2996
2997	if (status_code != WLAN_STATUS_SUCCESS) {
2998		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2999
3000		if (auth_alg == WLAN_AUTH_SAE &&
3001		    (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
3002		     (auth_transaction == 1 &&
3003		      (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
3004		       status_code == WLAN_STATUS_SAE_PK)))) {
3005			/* waiting for userspace now */
3006			ifmgd->auth_data->waiting = true;
3007			ifmgd->auth_data->timeout =
3008				jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
3009			ifmgd->auth_data->timeout_started = true;
3010			run_again(sdata, ifmgd->auth_data->timeout);
3011			return;
3012		}
3013
3014		sdata_info(sdata, "%pM denied authentication (status %d)\n",
3015			   mgmt->sa, status_code);
3016		ieee80211_destroy_auth_data(sdata, false);
3017		event.u.mlme.status = MLME_DENIED;
3018		event.u.mlme.reason = status_code;
3019		drv_event_callback(sdata->local, sdata, &event);
3020		return;
3021	}
3022
3023	switch (ifmgd->auth_data->algorithm) {
3024	case WLAN_AUTH_OPEN:
3025	case WLAN_AUTH_LEAP:
3026	case WLAN_AUTH_FT:
3027	case WLAN_AUTH_SAE:
3028	case WLAN_AUTH_FILS_SK:
3029	case WLAN_AUTH_FILS_SK_PFS:
3030	case WLAN_AUTH_FILS_PK:
3031		break;
3032	case WLAN_AUTH_SHARED_KEY:
3033		if (ifmgd->auth_data->expected_transaction != 4) {
3034			ieee80211_auth_challenge(sdata, mgmt, len);
3035			/* need another frame */
3036			return;
3037		}
3038		break;
3039	default:
3040		WARN_ONCE(1, "invalid auth alg %d",
3041			  ifmgd->auth_data->algorithm);
3042		return;
3043	}
3044
3045	event.u.mlme.status = MLME_SUCCESS;
3046	drv_event_callback(sdata->local, sdata, &event);
3047	if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3048	    (auth_transaction == 2 &&
3049	     ifmgd->auth_data->expected_transaction == 2)) {
3050		if (!ieee80211_mark_sta_auth(sdata, bssid))
3051			return; /* ignore frame -- wait for timeout */
3052	} else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3053		   auth_transaction == 2) {
3054		sdata_info(sdata, "SAE peer confirmed\n");
3055		ifmgd->auth_data->peer_confirmed = true;
3056	}
3057
3058	cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3059}
3060
3061#define case_WLAN(type) \
3062	case WLAN_REASON_##type: return #type
3063
3064const char *ieee80211_get_reason_code_string(u16 reason_code)
3065{
3066	switch (reason_code) {
3067	case_WLAN(UNSPECIFIED);
3068	case_WLAN(PREV_AUTH_NOT_VALID);
3069	case_WLAN(DEAUTH_LEAVING);
3070	case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3071	case_WLAN(DISASSOC_AP_BUSY);
3072	case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3073	case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3074	case_WLAN(DISASSOC_STA_HAS_LEFT);
3075	case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3076	case_WLAN(DISASSOC_BAD_POWER);
3077	case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3078	case_WLAN(INVALID_IE);
3079	case_WLAN(MIC_FAILURE);
3080	case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3081	case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3082	case_WLAN(IE_DIFFERENT);
3083	case_WLAN(INVALID_GROUP_CIPHER);
3084	case_WLAN(INVALID_PAIRWISE_CIPHER);
3085	case_WLAN(INVALID_AKMP);
3086	case_WLAN(UNSUPP_RSN_VERSION);
3087	case_WLAN(INVALID_RSN_IE_CAP);
3088	case_WLAN(IEEE8021X_FAILED);
3089	case_WLAN(CIPHER_SUITE_REJECTED);
3090	case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3091	case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3092	case_WLAN(DISASSOC_LOW_ACK);
3093	case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3094	case_WLAN(QSTA_LEAVE_QBSS);
3095	case_WLAN(QSTA_NOT_USE);
3096	case_WLAN(QSTA_REQUIRE_SETUP);
3097	case_WLAN(QSTA_TIMEOUT);
3098	case_WLAN(QSTA_CIPHER_NOT_SUPP);
3099	case_WLAN(MESH_PEER_CANCELED);
3100	case_WLAN(MESH_MAX_PEERS);
3101	case_WLAN(MESH_CONFIG);
3102	case_WLAN(MESH_CLOSE);
3103	case_WLAN(MESH_MAX_RETRIES);
3104	case_WLAN(MESH_CONFIRM_TIMEOUT);
3105	case_WLAN(MESH_INVALID_GTK);
3106	case_WLAN(MESH_INCONSISTENT_PARAM);
3107	case_WLAN(MESH_INVALID_SECURITY);
3108	case_WLAN(MESH_PATH_ERROR);
3109	case_WLAN(MESH_PATH_NOFORWARD);
3110	case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3111	case_WLAN(MAC_EXISTS_IN_MBSS);
3112	case_WLAN(MESH_CHAN_REGULATORY);
3113	case_WLAN(MESH_CHAN);
3114	default: return "<unknown>";
3115	}
3116}
3117
3118static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3119				     struct ieee80211_mgmt *mgmt, size_t len)
3120{
3121	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3122	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3123
3124	sdata_assert_lock(sdata);
3125
3126	if (len < 24 + 2)
3127		return;
3128
3129	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3130		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3131		return;
3132	}
3133
3134	if (ifmgd->associated &&
3135	    ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3136		const u8 *bssid = ifmgd->associated->bssid;
3137
3138		sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3139			   bssid, reason_code,
3140			   ieee80211_get_reason_code_string(reason_code));
3141
3142		ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3143
3144		ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3145					    reason_code);
3146		return;
3147	}
3148
3149	if (ifmgd->assoc_data &&
3150	    ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3151		const u8 *bssid = ifmgd->assoc_data->bss->bssid;
3152
3153		sdata_info(sdata,
3154			   "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3155			   bssid, reason_code,
3156			   ieee80211_get_reason_code_string(reason_code));
3157
3158		ieee80211_destroy_assoc_data(sdata, false, true);
3159
3160		cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3161		return;
3162	}
3163}
3164
3165
3166static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3167				       struct ieee80211_mgmt *mgmt, size_t len)
3168{
3169	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3170	u16 reason_code;
3171
3172	sdata_assert_lock(sdata);
3173
3174	if (len < 24 + 2)
3175		return;
3176
3177	if (!ifmgd->associated ||
3178	    !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3179		return;
3180
3181	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3182
3183	if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3184		ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3185		return;
3186	}
3187
3188	sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3189		   mgmt->sa, reason_code,
3190		   ieee80211_get_reason_code_string(reason_code));
3191
3192	ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3193
3194	ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
3195}
3196
3197static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3198				u8 *supp_rates, unsigned int supp_rates_len,
3199				u32 *rates, u32 *basic_rates,
3200				bool *have_higher_than_11mbit,
3201				int *min_rate, int *min_rate_index,
3202				int shift)
3203{
3204	int i, j;
3205
3206	for (i = 0; i < supp_rates_len; i++) {
3207		int rate = supp_rates[i] & 0x7f;
3208		bool is_basic = !!(supp_rates[i] & 0x80);
3209
3210		if ((rate * 5 * (1 << shift)) > 110)
3211			*have_higher_than_11mbit = true;
3212
3213		/*
3214		 * Skip HT, VHT and HE BSS membership selectors since they're
3215		 * not rates.
3216		 *
3217		 * Note: Even though the membership selector and the basic
3218		 *	 rate flag share the same bit, they are not exactly
3219		 *	 the same.
3220		 */
3221		if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3222		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3223		    supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY))
3224			continue;
3225
3226		for (j = 0; j < sband->n_bitrates; j++) {
3227			struct ieee80211_rate *br;
3228			int brate;
3229
3230			br = &sband->bitrates[j];
3231
3232			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3233			if (brate == rate) {
3234				*rates |= BIT(j);
3235				if (is_basic)
3236					*basic_rates |= BIT(j);
3237				if ((rate * 5) < *min_rate) {
3238					*min_rate = rate * 5;
3239					*min_rate_index = j;
3240				}
3241				break;
3242			}
3243		}
3244	}
3245}
3246
3247static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3248					const struct ieee802_11_elems *elems)
3249{
3250	if (elems->ext_capab_len < 10)
3251		return false;
3252
3253	if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3254		return false;
3255
3256	return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3257		IEEE80211_HE_MAC_CAP0_TWT_RES;
3258}
3259
3260static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3261				    struct sta_info *sta,
3262				    struct ieee802_11_elems *elems)
3263{
3264	bool twt = ieee80211_twt_req_supported(sta, elems);
3265
3266	if (sdata->vif.bss_conf.twt_requester != twt) {
3267		sdata->vif.bss_conf.twt_requester = twt;
3268		return BSS_CHANGED_TWT;
3269	}
3270	return 0;
3271}
3272
3273static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3274				    struct cfg80211_bss *cbss,
3275				    struct ieee80211_mgmt *mgmt, size_t len,
3276				    struct ieee802_11_elems *elems)
3277{
3278	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3279	struct ieee80211_local *local = sdata->local;
3280	struct ieee80211_supported_band *sband;
3281	struct sta_info *sta;
3282	u16 capab_info, aid;
3283	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3284	const struct cfg80211_bss_ies *bss_ies = NULL;
3285	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3286	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
3287	bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
3288	u32 changed = 0;
3289	u8 *pos;
3290	int err;
3291	bool ret;
3292
3293	/* AssocResp and ReassocResp have identical structure */
3294
3295	pos = mgmt->u.assoc_resp.variable;
3296	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3297	if (is_s1g) {
3298		pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3299		aid = 0; /* TODO */
3300	}
3301	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3302	ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, elems,
3303			       mgmt->bssid, NULL);
3304
3305	if (elems->aid_resp)
3306		aid = le16_to_cpu(elems->aid_resp->aid);
3307
3308	/*
3309	 * The 5 MSB of the AID field are reserved
3310	 * (802.11-2016 9.4.1.8 AID field)
3311	 */
3312	aid &= 0x7ff;
3313
3314	ifmgd->broken_ap = false;
3315
3316	if (aid == 0 || aid > IEEE80211_MAX_AID) {
3317		sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3318			   aid);
3319		aid = 0;
3320		ifmgd->broken_ap = true;
3321	}
3322
3323	if (!is_s1g && !elems->supp_rates) {
3324		sdata_info(sdata, "no SuppRates element in AssocResp\n");
3325		return false;
3326	}
3327
3328	sdata->vif.bss_conf.aid = aid;
3329	ifmgd->tdls_chan_switch_prohibited =
3330		elems->ext_capab && elems->ext_capab_len >= 5 &&
3331		(elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3332
3333	/*
3334	 * Some APs are erroneously not including some information in their
3335	 * (re)association response frames. Try to recover by using the data
3336	 * from the beacon or probe response. This seems to afflict mobile
3337	 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3338	 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3339	 */
3340	if (!is_6ghz &&
3341	    ((assoc_data->wmm && !elems->wmm_param) ||
3342	     (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3343	      (!elems->ht_cap_elem || !elems->ht_operation)) ||
3344	     (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3345	      (!elems->vht_cap_elem || !elems->vht_operation)))) {
3346		const struct cfg80211_bss_ies *ies;
3347		struct ieee802_11_elems bss_elems;
3348
3349		rcu_read_lock();
3350		ies = rcu_dereference(cbss->ies);
3351		if (ies)
3352			bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3353					  GFP_ATOMIC);
3354		rcu_read_unlock();
3355		if (!bss_ies)
3356			return false;
3357
3358		ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3359				       false, &bss_elems,
3360				       mgmt->bssid,
3361				       assoc_data->bss->bssid);
3362		if (assoc_data->wmm &&
3363		    !elems->wmm_param && bss_elems.wmm_param) {
3364			elems->wmm_param = bss_elems.wmm_param;
3365			sdata_info(sdata,
3366				   "AP bug: WMM param missing from AssocResp\n");
3367		}
3368
3369		/*
3370		 * Also check if we requested HT/VHT, otherwise the AP doesn't
3371		 * have to include the IEs in the (re)association response.
3372		 */
3373		if (!elems->ht_cap_elem && bss_elems.ht_cap_elem &&
3374		    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3375			elems->ht_cap_elem = bss_elems.ht_cap_elem;
3376			sdata_info(sdata,
3377				   "AP bug: HT capability missing from AssocResp\n");
3378		}
3379		if (!elems->ht_operation && bss_elems.ht_operation &&
3380		    !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3381			elems->ht_operation = bss_elems.ht_operation;
3382			sdata_info(sdata,
3383				   "AP bug: HT operation missing from AssocResp\n");
3384		}
3385		if (!elems->vht_cap_elem && bss_elems.vht_cap_elem &&
3386		    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3387			elems->vht_cap_elem = bss_elems.vht_cap_elem;
3388			sdata_info(sdata,
3389				   "AP bug: VHT capa missing from AssocResp\n");
3390		}
3391		if (!elems->vht_operation && bss_elems.vht_operation &&
3392		    !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3393			elems->vht_operation = bss_elems.vht_operation;
3394			sdata_info(sdata,
3395				   "AP bug: VHT operation missing from AssocResp\n");
3396		}
3397		kfree(bss_elems.nontx_profile);
3398	}
3399
3400	/*
3401	 * We previously checked these in the beacon/probe response, so
3402	 * they should be present here. This is just a safety net.
3403	 */
3404	if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3405	    (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
3406		sdata_info(sdata,
3407			   "HT AP is missing WMM params or HT capability/operation\n");
3408		ret = false;
3409		goto out;
3410	}
3411
3412	if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3413	    (!elems->vht_cap_elem || !elems->vht_operation)) {
3414		sdata_info(sdata,
3415			   "VHT AP is missing VHT capability/operation\n");
3416		ret = false;
3417		goto out;
3418	}
3419
3420	if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3421	    !elems->he_6ghz_capa) {
3422		sdata_info(sdata,
3423			   "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3424		ret = false;
3425		goto out;
3426	}
3427
3428	mutex_lock(&sdata->local->sta_mtx);
3429	/*
3430	 * station info was already allocated and inserted before
3431	 * the association and should be available to us
3432	 */
3433	sta = sta_info_get(sdata, cbss->bssid);
3434	if (WARN_ON(!sta)) {
3435		mutex_unlock(&sdata->local->sta_mtx);
3436		ret = false;
3437		goto out;
3438	}
3439
3440	sband = ieee80211_get_sband(sdata);
3441	if (!sband) {
3442		mutex_unlock(&sdata->local->sta_mtx);
3443		ret = false;
3444		goto out;
3445	}
3446
3447	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3448	    (!elems->he_cap || !elems->he_operation)) {
3449		mutex_unlock(&sdata->local->sta_mtx);
3450		sdata_info(sdata,
3451			   "HE AP is missing HE capability/operation\n");
3452		ret = false;
3453		goto out;
3454	}
3455
3456	/* Set up internal HT/VHT capabilities */
3457	if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3458		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3459						  elems->ht_cap_elem, sta);
3460
3461	if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3462		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3463						    elems->vht_cap_elem, sta);
3464
3465	if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3466	    elems->he_cap) {
3467		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3468						  elems->he_cap,
3469						  elems->he_cap_len,
3470						  elems->he_6ghz_capa,
3471						  sta);
3472
3473		bss_conf->he_support = sta->sta.he_cap.has_he;
3474		if (elems->rsnx && elems->rsnx_len &&
3475		    (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3476		    wiphy_ext_feature_isset(local->hw.wiphy,
3477					    NL80211_EXT_FEATURE_PROTECTED_TWT))
3478			bss_conf->twt_protected = true;
3479		else
3480			bss_conf->twt_protected = false;
3481
3482		changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
3483	} else {
3484		bss_conf->he_support = false;
3485		bss_conf->twt_requester = false;
3486		bss_conf->twt_protected = false;
3487	}
3488
3489	if (bss_conf->he_support) {
3490		bss_conf->he_bss_color.color =
3491			le32_get_bits(elems->he_operation->he_oper_params,
3492				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3493		bss_conf->he_bss_color.partial =
3494			le32_get_bits(elems->he_operation->he_oper_params,
3495				      IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
3496		bss_conf->he_bss_color.enabled =
3497			!le32_get_bits(elems->he_operation->he_oper_params,
3498				       IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3499
3500		if (bss_conf->he_bss_color.enabled)
3501			changed |= BSS_CHANGED_HE_BSS_COLOR;
3502
3503		bss_conf->htc_trig_based_pkt_ext =
3504			le32_get_bits(elems->he_operation->he_oper_params,
3505			      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
3506		bss_conf->frame_time_rts_th =
3507			le32_get_bits(elems->he_operation->he_oper_params,
3508			      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3509
3510		bss_conf->multi_sta_back_32bit =
3511			sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3512			IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3513
3514		bss_conf->ack_enabled =
3515			sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3516			IEEE80211_HE_MAC_CAP2_ACK_EN;
3517
3518		bss_conf->uora_exists = !!elems->uora_element;
3519		if (elems->uora_element)
3520			bss_conf->uora_ocw_range = elems->uora_element[0];
3521
3522		ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3523		ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
3524		/* TODO: OPEN: what happens if BSS color disable is set? */
3525	}
3526
3527	if (cbss->transmitted_bss) {
3528		bss_conf->nontransmitted = true;
3529		ether_addr_copy(bss_conf->transmitter_bssid,
3530				cbss->transmitted_bss->bssid);
3531		bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3532		bss_conf->bssid_index = cbss->bssid_index;
3533	} else {
3534		bss_conf->nontransmitted = false;
3535		memset(bss_conf->transmitter_bssid, 0,
3536		       sizeof(bss_conf->transmitter_bssid));
3537		bss_conf->bssid_indicator = 0;
3538		bss_conf->bssid_index = 0;
3539	}
3540
3541	/*
3542	 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3543	 * in their association response, so ignore that data for our own
3544	 * configuration. If it changed since the last beacon, we'll get the
3545	 * next beacon and update then.
3546	 */
3547
3548	/*
3549	 * If an operating mode notification IE is present, override the
3550	 * NSS calculation (that would be done in rate_control_rate_init())
3551	 * and use the # of streams from that element.
3552	 */
3553	if (elems->opmode_notif &&
3554	    !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3555		u8 nss;
3556
3557		nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3558		nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3559		nss += 1;
3560		sta->sta.rx_nss = nss;
3561	}
3562
3563	rate_control_rate_init(sta);
3564
3565	if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3566		set_sta_flag(sta, WLAN_STA_MFP);
3567		sta->sta.mfp = true;
3568	} else {
3569		sta->sta.mfp = false;
3570	}
3571
3572	sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
3573		       local->hw.queues >= IEEE80211_NUM_ACS;
3574
3575	err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3576	if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3577		err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3578	if (err) {
3579		sdata_info(sdata,
3580			   "failed to move station %pM to desired state\n",
3581			   sta->sta.addr);
3582		WARN_ON(__sta_info_destroy(sta));
3583		mutex_unlock(&sdata->local->sta_mtx);
3584		ret = false;
3585		goto out;
3586	}
3587
3588	if (sdata->wdev.use_4addr)
3589		drv_sta_set_4addr(local, sdata, &sta->sta, true);
3590
3591	mutex_unlock(&sdata->local->sta_mtx);
3592
3593	/*
3594	 * Always handle WMM once after association regardless
3595	 * of the first value the AP uses. Setting -1 here has
3596	 * that effect because the AP values is an unsigned
3597	 * 4-bit value.
3598	 */
3599	ifmgd->wmm_last_param_set = -1;
3600	ifmgd->mu_edca_last_param_set = -1;
3601
3602	if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3603		ieee80211_set_wmm_default(sdata, false, false);
3604	} else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3605					     elems->wmm_param_len,
3606					     elems->mu_edca_param_set)) {
3607		/* still enable QoS since we might have HT/VHT */
3608		ieee80211_set_wmm_default(sdata, false, true);
3609		/* set the disable-WMM flag in this case to disable
3610		 * tracking WMM parameter changes in the beacon if
3611		 * the parameters weren't actually valid. Doing so
3612		 * avoids changing parameters very strangely when
3613		 * the AP is going back and forth between valid and
3614		 * invalid parameters.
3615		 */
3616		ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3617	}
3618	changed |= BSS_CHANGED_QOS;
3619
3620	if (elems->max_idle_period_ie) {
3621		bss_conf->max_idle_period =
3622			le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
3623		bss_conf->protected_keep_alive =
3624			!!(elems->max_idle_period_ie->idle_options &
3625			   WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3626		changed |= BSS_CHANGED_KEEP_ALIVE;
3627	} else {
3628		bss_conf->max_idle_period = 0;
3629		bss_conf->protected_keep_alive = false;
3630	}
3631
3632	/* set assoc capability (AID was already set earlier),
3633	 * ieee80211_set_associated() will tell the driver */
3634	bss_conf->assoc_capability = capab_info;
3635	ieee80211_set_associated(sdata, cbss, changed);
3636
3637	/*
3638	 * If we're using 4-addr mode, let the AP know that we're
3639	 * doing so, so that it can create the STA VLAN on its side
3640	 */
3641	if (ifmgd->use_4addr)
3642		ieee80211_send_4addr_nullfunc(local, sdata);
3643
3644	/*
3645	 * Start timer to probe the connection to the AP now.
3646	 * Also start the timer that will detect beacon loss.
3647	 */
3648	ieee80211_sta_reset_beacon_monitor(sdata);
3649	ieee80211_sta_reset_conn_monitor(sdata);
3650
3651	ret = true;
3652 out:
3653	kfree(bss_ies);
3654	return ret;
3655}
3656
3657static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3658					 struct ieee80211_mgmt *mgmt,
3659					 size_t len)
3660{
3661	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3662	struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3663	u16 capab_info, status_code, aid;
3664	struct ieee802_11_elems elems;
3665	int ac, uapsd_queues = -1;
3666	u8 *pos;
3667	bool reassoc;
3668	struct cfg80211_bss *cbss;
3669	struct ieee80211_event event = {
3670		.type = MLME_EVENT,
3671		.u.mlme.data = ASSOC_EVENT,
3672	};
3673
3674	sdata_assert_lock(sdata);
3675
3676	if (!assoc_data)
3677		return;
3678
3679	if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
3680		return;
3681
3682	cbss = assoc_data->bss;
3683
3684	/*
3685	 * AssocResp and ReassocResp have identical structure, so process both
3686	 * of them in this function.
3687	 */
3688
3689	if (len < 24 + 6)
3690		return;
3691
3692	reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
3693	capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3694	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3695	pos = mgmt->u.assoc_resp.variable;
3696	aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3697	if (cbss->channel->band == NL80211_BAND_S1GHZ) {
3698		pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3699		aid = 0; /* TODO */
3700	}
3701
3702	sdata_info(sdata,
3703		   "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3704		   reassoc ? "Rea" : "A", mgmt->sa,
3705		   capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
3706
3707	if (assoc_data->fils_kek_len &&
3708	    fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3709		return;
3710
3711	ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3712			       mgmt->bssid, NULL);
3713
3714	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
3715	    elems.timeout_int &&
3716	    elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
3717		u32 tu, ms;
3718		tu = le32_to_cpu(elems.timeout_int->value);
3719		ms = tu * 1024 / 1000;
3720		sdata_info(sdata,
3721			   "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3722			   mgmt->sa, tu, ms);
3723		assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
3724		assoc_data->timeout_started = true;
3725		if (ms > IEEE80211_ASSOC_TIMEOUT)
3726			run_again(sdata, assoc_data->timeout);
3727		return;
3728	}
3729
3730	if (status_code != WLAN_STATUS_SUCCESS) {
3731		sdata_info(sdata, "%pM denied association (code=%d)\n",
3732			   mgmt->sa, status_code);
3733		ieee80211_destroy_assoc_data(sdata, false, false);
3734		event.u.mlme.status = MLME_DENIED;
3735		event.u.mlme.reason = status_code;
3736		drv_event_callback(sdata->local, sdata, &event);
3737	} else {
3738		if (!ieee80211_assoc_success(sdata, cbss, mgmt, len, &elems)) {
3739			/* oops -- internal error -- send timeout for now */
3740			ieee80211_destroy_assoc_data(sdata, false, false);
3741			cfg80211_assoc_timeout(sdata->dev, cbss);
3742			return;
3743		}
3744		event.u.mlme.status = MLME_SUCCESS;
3745		drv_event_callback(sdata->local, sdata, &event);
3746		sdata_info(sdata, "associated\n");
3747
3748		/*
3749		 * destroy assoc_data afterwards, as otherwise an idle
3750		 * recalc after assoc_data is NULL but before associated
3751		 * is set can cause the interface to go idle
3752		 */
3753		ieee80211_destroy_assoc_data(sdata, true, false);
3754
3755		/* get uapsd queues configuration */
3756		uapsd_queues = 0;
3757		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3758			if (sdata->tx_conf[ac].uapsd)
3759				uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
3760	}
3761
3762	cfg80211_rx_assoc_resp(sdata->dev, cbss, (u8 *)mgmt, len, uapsd_queues,
3763			       ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
3764}
3765
3766static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
3767				  struct ieee80211_mgmt *mgmt, size_t len,
3768				  struct ieee80211_rx_status *rx_status)
3769{
3770	struct ieee80211_local *local = sdata->local;
3771	struct ieee80211_bss *bss;
3772	struct ieee80211_channel *channel;
3773
3774	sdata_assert_lock(sdata);
3775
3776	channel = ieee80211_get_channel_khz(local->hw.wiphy,
3777					ieee80211_rx_status_to_khz(rx_status));
3778	if (!channel)
3779		return;
3780
3781	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
3782	if (bss) {
3783		sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
3784		ieee80211_rx_bss_put(local, bss);
3785	}
3786}
3787
3788
3789static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
3790					 struct sk_buff *skb)
3791{
3792	struct ieee80211_mgmt *mgmt = (void *)skb->data;
3793	struct ieee80211_if_managed *ifmgd;
3794	struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3795	struct ieee80211_channel *channel;
3796	size_t baselen, len = skb->len;
3797
3798	ifmgd = &sdata->u.mgd;
3799
3800	sdata_assert_lock(sdata);
3801
3802	/*
3803	 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3804	 * "If a 6 GHz AP receives a Probe Request frame  and responds with
3805	 * a Probe Response frame [..], the Address 1 field of the Probe
3806	 * Response frame shall be set to the broadcast address [..]"
3807	 * So, on 6GHz band we should also accept broadcast responses.
3808	 */
3809	channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3810					rx_status->freq);
3811	if (!channel)
3812		return;
3813
3814	if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3815	    (channel->band != NL80211_BAND_6GHZ ||
3816	     !is_broadcast_ether_addr(mgmt->da)))
3817		return; /* ignore ProbeResp to foreign address */
3818
3819	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3820	if (baselen > len)
3821		return;
3822
3823	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3824
3825	if (ifmgd->associated &&
3826	    ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3827		ieee80211_reset_ap_probe(sdata);
3828}
3829
3830/*
3831 * This is the canonical list of information elements we care about,
3832 * the filter code also gives us all changes to the Microsoft OUI
3833 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3834 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3835 * changes to requested client power.
3836 *
3837 * We implement beacon filtering in software since that means we can
3838 * avoid processing the frame here and in cfg80211, and userspace
3839 * will not be able to tell whether the hardware supports it or not.
3840 *
3841 * XXX: This list needs to be dynamic -- userspace needs to be able to
3842 *	add items it requires. It also needs to be able to tell us to
3843 *	look out for other vendor IEs.
3844 */
3845static const u64 care_about_ies =
3846	(1ULL << WLAN_EID_COUNTRY) |
3847	(1ULL << WLAN_EID_ERP_INFO) |
3848	(1ULL << WLAN_EID_CHANNEL_SWITCH) |
3849	(1ULL << WLAN_EID_PWR_CONSTRAINT) |
3850	(1ULL << WLAN_EID_HT_CAPABILITY) |
3851	(1ULL << WLAN_EID_HT_OPERATION) |
3852	(1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
3853
3854static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3855					struct ieee80211_if_managed *ifmgd,
3856					struct ieee80211_bss_conf *bss_conf,
3857					struct ieee80211_local *local,
3858					struct ieee80211_rx_status *rx_status)
3859{
3860	/* Track average RSSI from the Beacon frames of the current AP */
3861
3862	if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3863		ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3864		ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
3865		ifmgd->last_cqm_event_signal = 0;
3866		ifmgd->count_beacon_signal = 1;
3867		ifmgd->last_ave_beacon_signal = 0;
3868	} else {
3869		ifmgd->count_beacon_signal++;
3870	}
3871
3872	ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3873
3874	if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3875	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3876		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3877		int last_sig = ifmgd->last_ave_beacon_signal;
3878		struct ieee80211_event event = {
3879			.type = RSSI_EVENT,
3880		};
3881
3882		/*
3883		 * if signal crosses either of the boundaries, invoke callback
3884		 * with appropriate parameters
3885		 */
3886		if (sig > ifmgd->rssi_max_thold &&
3887		    (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3888			ifmgd->last_ave_beacon_signal = sig;
3889			event.u.rssi.data = RSSI_EVENT_HIGH;
3890			drv_event_callback(local, sdata, &event);
3891		} else if (sig < ifmgd->rssi_min_thold &&
3892			   (last_sig >= ifmgd->rssi_max_thold ||
3893			   last_sig == 0)) {
3894			ifmgd->last_ave_beacon_signal = sig;
3895			event.u.rssi.data = RSSI_EVENT_LOW;
3896			drv_event_callback(local, sdata, &event);
3897		}
3898	}
3899
3900	if (bss_conf->cqm_rssi_thold &&
3901	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3902	    !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3903		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3904		int last_event = ifmgd->last_cqm_event_signal;
3905		int thold = bss_conf->cqm_rssi_thold;
3906		int hyst = bss_conf->cqm_rssi_hyst;
3907
3908		if (sig < thold &&
3909		    (last_event == 0 || sig < last_event - hyst)) {
3910			ifmgd->last_cqm_event_signal = sig;
3911			ieee80211_cqm_rssi_notify(
3912				&sdata->vif,
3913				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3914				sig, GFP_KERNEL);
3915		} else if (sig > thold &&
3916			   (last_event == 0 || sig > last_event + hyst)) {
3917			ifmgd->last_cqm_event_signal = sig;
3918			ieee80211_cqm_rssi_notify(
3919				&sdata->vif,
3920				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3921				sig, GFP_KERNEL);
3922		}
3923	}
3924
3925	if (bss_conf->cqm_rssi_low &&
3926	    ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3927		int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3928		int last_event = ifmgd->last_cqm_event_signal;
3929		int low = bss_conf->cqm_rssi_low;
3930		int high = bss_conf->cqm_rssi_high;
3931
3932		if (sig < low &&
3933		    (last_event == 0 || last_event >= low)) {
3934			ifmgd->last_cqm_event_signal = sig;
3935			ieee80211_cqm_rssi_notify(
3936				&sdata->vif,
3937				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3938				sig, GFP_KERNEL);
3939		} else if (sig > high &&
3940			   (last_event == 0 || last_event <= high)) {
3941			ifmgd->last_cqm_event_signal = sig;
3942			ieee80211_cqm_rssi_notify(
3943				&sdata->vif,
3944				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3945				sig, GFP_KERNEL);
3946		}
3947	}
3948}
3949
3950static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3951				    struct cfg80211_bss *bss)
3952{
3953	if (ether_addr_equal(tx_bssid, bss->bssid))
3954		return true;
3955	if (!bss->transmitted_bss)
3956		return false;
3957	return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3958}
3959
3960static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3961				     struct ieee80211_hdr *hdr, size_t len,
3962				     struct ieee80211_rx_status *rx_status)
3963{
3964	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3965	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3966	struct ieee80211_mgmt *mgmt = (void *) hdr;
3967	size_t baselen;
3968	struct ieee802_11_elems elems;
3969	struct ieee80211_local *local = sdata->local;
3970	struct ieee80211_chanctx_conf *chanctx_conf;
3971	struct ieee80211_channel *chan;
3972	struct sta_info *sta;
3973	u32 changed = 0;
3974	bool erp_valid;
3975	u8 erp_value = 0;
3976	u32 ncrc = 0;
3977	u8 *bssid, *variable = mgmt->u.beacon.variable;
3978	u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3979
3980	sdata_assert_lock(sdata);
3981
3982	/* Process beacon from the current BSS */
3983	bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
3984	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
3985		struct ieee80211_ext *ext = (void *) mgmt;
3986
3987		if (ieee80211_is_s1g_short_beacon(ext->frame_control))
3988			variable = ext->u.s1g_short_beacon.variable;
3989		else
3990			variable = ext->u.s1g_beacon.variable;
3991	}
3992
3993	baselen = (u8 *) variable - (u8 *) mgmt;
3994	if (baselen > len)
3995		return;
3996
3997	rcu_read_lock();
3998	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3999	if (!chanctx_conf) {
4000		rcu_read_unlock();
4001		return;
4002	}
4003
4004	if (ieee80211_rx_status_to_khz(rx_status) !=
4005	    ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
4006		rcu_read_unlock();
4007		return;
4008	}
4009	chan = chanctx_conf->def.chan;
4010	rcu_read_unlock();
4011
4012	if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
4013	    ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->bss)) {
4014		ieee802_11_parse_elems(variable,
4015				       len - baselen, false, &elems,
4016				       bssid,
4017				       ifmgd->assoc_data->bss->bssid);
4018
4019		ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4020
4021		if (elems.dtim_period)
4022			ifmgd->dtim_period = elems.dtim_period;
4023		ifmgd->have_beacon = true;
4024		ifmgd->assoc_data->need_beacon = false;
4025		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
4026			sdata->vif.bss_conf.sync_tsf =
4027				le64_to_cpu(mgmt->u.beacon.timestamp);
4028			sdata->vif.bss_conf.sync_device_ts =
4029				rx_status->device_timestamp;
4030			sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
4031		}
4032
4033		if (elems.mbssid_config_ie)
4034			bss_conf->profile_periodicity =
4035				elems.mbssid_config_ie->profile_periodicity;
4036		else
4037			bss_conf->profile_periodicity = 0;
4038
4039		if (elems.ext_capab_len >= 11 &&
4040		    (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
4041			bss_conf->ema_ap = true;
4042		else
4043			bss_conf->ema_ap = false;
4044
4045		/* continue assoc process */
4046		ifmgd->assoc_data->timeout = jiffies;
4047		ifmgd->assoc_data->timeout_started = true;
4048		run_again(sdata, ifmgd->assoc_data->timeout);
4049		kfree(elems.nontx_profile);
4050		return;
4051	}
4052
4053	if (!ifmgd->associated ||
4054	    !ieee80211_rx_our_beacon(bssid,  ifmgd->associated))
4055		return;
4056	bssid = ifmgd->associated->bssid;
4057
4058	if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4059		ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
4060					    local, rx_status);
4061
4062	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
4063		mlme_dbg_ratelimited(sdata,
4064				     "cancelling AP probe due to a received beacon\n");
4065		ieee80211_reset_ap_probe(sdata);
4066	}
4067
4068	/*
4069	 * Push the beacon loss detection into the future since
4070	 * we are processing a beacon from the AP just now.
4071	 */
4072	ieee80211_sta_reset_beacon_monitor(sdata);
4073
4074	/* TODO: CRC urrently not calculated on S1G Beacon Compatibility
4075	 * element (which carries the beacon interval). Don't forget to add a
4076	 * bit to care_about_ies[] above if mac80211 is interested in a
4077	 * changing S1G element.
4078	 */
4079	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4080		ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4081	ncrc = ieee802_11_parse_elems_crc(variable,
4082					  len - baselen, false, &elems,
4083					  care_about_ies, ncrc,
4084					  mgmt->bssid, bssid);
4085
4086	if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
4087	    ieee80211_check_tim(elems.tim, elems.tim_len, bss_conf->aid)) {
4088		if (local->hw.conf.dynamic_ps_timeout > 0) {
4089			if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4090				local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4091				ieee80211_hw_config(local,
4092						    IEEE80211_CONF_CHANGE_PS);
4093			}
4094			ieee80211_send_nullfunc(local, sdata, false);
4095		} else if (!local->pspolling && sdata->u.mgd.powersave) {
4096			local->pspolling = true;
4097
4098			/*
4099			 * Here is assumed that the driver will be
4100			 * able to send ps-poll frame and receive a
4101			 * response even though power save mode is
4102			 * enabled, but some drivers might require
4103			 * to disable power save here. This needs
4104			 * to be investigated.
4105			 */
4106			ieee80211_send_pspoll(local, sdata);
4107		}
4108	}
4109
4110	if (sdata->vif.p2p ||
4111	    sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
4112		struct ieee80211_p2p_noa_attr noa = {};
4113		int ret;
4114
4115		ret = cfg80211_get_p2p_attr(variable,
4116					    len - baselen,
4117					    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
4118					    (u8 *) &noa, sizeof(noa));
4119		if (ret >= 2) {
4120			if (sdata->u.mgd.p2p_noa_index != noa.index) {
4121				/* valid noa_attr and index changed */
4122				sdata->u.mgd.p2p_noa_index = noa.index;
4123				memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4124				changed |= BSS_CHANGED_P2P_PS;
4125				/*
4126				 * make sure we update all information, the CRC
4127				 * mechanism doesn't look at P2P attributes.
4128				 */
4129				ifmgd->beacon_crc_valid = false;
4130			}
4131		} else if (sdata->u.mgd.p2p_noa_index != -1) {
4132			/* noa_attr not found and we had valid noa_attr before */
4133			sdata->u.mgd.p2p_noa_index = -1;
4134			memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
4135			changed |= BSS_CHANGED_P2P_PS;
4136			ifmgd->beacon_crc_valid = false;
4137		}
4138	}
4139
4140	if (ifmgd->csa_waiting_bcn)
4141		ieee80211_chswitch_post_beacon(sdata);
4142
4143	/*
4144	 * Update beacon timing and dtim count on every beacon appearance. This
4145	 * will allow the driver to use the most updated values. Do it before
4146	 * comparing this one with last received beacon.
4147	 * IMPORTANT: These parameters would possibly be out of sync by the time
4148	 * the driver will use them. The synchronized view is currently
4149	 * guaranteed only in certain callbacks.
4150	 */
4151	if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
4152	    !ieee80211_is_s1g_beacon(hdr->frame_control)) {
4153		sdata->vif.bss_conf.sync_tsf =
4154			le64_to_cpu(mgmt->u.beacon.timestamp);
4155		sdata->vif.bss_conf.sync_device_ts =
4156			rx_status->device_timestamp;
4157		sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
4158	}
4159
4160	if ((ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) ||
4161	    ieee80211_is_s1g_short_beacon(mgmt->frame_control))
4162		return;
4163	ifmgd->beacon_crc = ncrc;
4164	ifmgd->beacon_crc_valid = true;
4165
4166	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4167
4168	ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
4169					 rx_status->device_timestamp,
4170					 &elems, true);
4171
4172	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4173	    ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
4174				     elems.wmm_param_len,
4175				     elems.mu_edca_param_set))
4176		changed |= BSS_CHANGED_QOS;
4177
4178	/*
4179	 * If we haven't had a beacon before, tell the driver about the
4180	 * DTIM period (and beacon timing if desired) now.
4181	 */
4182	if (!ifmgd->have_beacon) {
4183		/* a few bogus AP send dtim_period = 0 or no TIM IE */
4184		bss_conf->dtim_period = elems.dtim_period ?: 1;
4185
4186		changed |= BSS_CHANGED_BEACON_INFO;
4187		ifmgd->have_beacon = true;
4188
4189		mutex_lock(&local->iflist_mtx);
4190		ieee80211_recalc_ps(local);
4191		mutex_unlock(&local->iflist_mtx);
4192
4193		ieee80211_recalc_ps_vif(sdata);
4194	}
4195
4196	if (elems.erp_info) {
4197		erp_valid = true;
4198		erp_value = elems.erp_info[0];
4199	} else {
4200		erp_valid = false;
4201	}
4202
4203	if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4204		changed |= ieee80211_handle_bss_capability(sdata,
4205				le16_to_cpu(mgmt->u.beacon.capab_info),
4206				erp_valid, erp_value);
4207
4208	mutex_lock(&local->sta_mtx);
4209	sta = sta_info_get(sdata, bssid);
4210
4211	changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4212
4213	if (ieee80211_config_bw(sdata, sta, elems.ht_cap_elem,
4214				elems.vht_cap_elem, elems.ht_operation,
4215				elems.vht_operation, elems.he_operation,
4216				elems.s1g_oper, bssid, &changed)) {
4217		mutex_unlock(&local->sta_mtx);
4218		sdata_info(sdata,
4219			   "failed to follow AP %pM bandwidth change, disconnect\n",
4220			   bssid);
4221		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4222				       WLAN_REASON_DEAUTH_LEAVING,
4223				       true, deauth_buf);
4224		ieee80211_report_disconnect(sdata, deauth_buf,
4225					    sizeof(deauth_buf), true,
4226					    WLAN_REASON_DEAUTH_LEAVING);
4227		goto free;
4228	}
4229
4230	if (sta && elems.opmode_notif)
4231		ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
4232					    rx_status->band);
4233	mutex_unlock(&local->sta_mtx);
4234
4235	changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4236					       elems.country_elem,
4237					       elems.country_elem_len,
4238					       elems.pwr_constr_elem,
4239					       elems.cisco_dtpc_elem);
4240
4241	ieee80211_bss_info_change_notify(sdata, changed);
4242free:
4243	kfree(elems.nontx_profile);
4244}
4245
4246void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
4247				 struct sk_buff *skb)
4248{
4249	struct ieee80211_rx_status *rx_status;
4250	struct ieee80211_hdr *hdr;
4251	u16 fc;
4252
4253	rx_status = (struct ieee80211_rx_status *) skb->cb;
4254	hdr = (struct ieee80211_hdr *) skb->data;
4255	fc = le16_to_cpu(hdr->frame_control);
4256
4257	sdata_lock(sdata);
4258	switch (fc & IEEE80211_FCTL_STYPE) {
4259	case IEEE80211_STYPE_S1G_BEACON:
4260		ieee80211_rx_mgmt_beacon(sdata, hdr, skb->len, rx_status);
4261		break;
4262	}
4263	sdata_unlock(sdata);
4264}
4265
4266void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4267				  struct sk_buff *skb)
4268{
4269	struct ieee80211_rx_status *rx_status;
4270	struct ieee80211_mgmt *mgmt;
4271	u16 fc;
4272	struct ieee802_11_elems elems;
4273	int ies_len;
4274
4275	rx_status = (struct ieee80211_rx_status *) skb->cb;
4276	mgmt = (struct ieee80211_mgmt *) skb->data;
4277	fc = le16_to_cpu(mgmt->frame_control);
4278
4279	sdata_lock(sdata);
4280
4281	switch (fc & IEEE80211_FCTL_STYPE) {
4282	case IEEE80211_STYPE_BEACON:
4283		ieee80211_rx_mgmt_beacon(sdata, (void *)mgmt,
4284					 skb->len, rx_status);
4285		break;
4286	case IEEE80211_STYPE_PROBE_RESP:
4287		ieee80211_rx_mgmt_probe_resp(sdata, skb);
4288		break;
4289	case IEEE80211_STYPE_AUTH:
4290		ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
4291		break;
4292	case IEEE80211_STYPE_DEAUTH:
4293		ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
4294		break;
4295	case IEEE80211_STYPE_DISASSOC:
4296		ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
4297		break;
4298	case IEEE80211_STYPE_ASSOC_RESP:
4299	case IEEE80211_STYPE_REASSOC_RESP:
4300		ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
4301		break;
4302	case IEEE80211_STYPE_ACTION:
4303		if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
4304			ies_len = skb->len -
4305				  offsetof(struct ieee80211_mgmt,
4306					   u.action.u.chan_switch.variable);
4307
4308			if (ies_len < 0)
4309				break;
4310
4311			/* CSA IE cannot be overridden, no need for BSSID */
4312			ieee802_11_parse_elems(
4313				mgmt->u.action.u.chan_switch.variable,
4314				ies_len, true, &elems, mgmt->bssid, NULL);
4315
4316			if (elems.parse_error)
4317				break;
4318
4319			ieee80211_sta_process_chanswitch(sdata,
4320						 rx_status->mactime,
4321						 rx_status->device_timestamp,
4322						 &elems, false);
4323		} else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4324			ies_len = skb->len -
4325				  offsetof(struct ieee80211_mgmt,
4326					   u.action.u.ext_chan_switch.variable);
4327
4328			if (ies_len < 0)
4329				break;
4330
4331			/*
4332			 * extended CSA IE can't be overridden, no need for
4333			 * BSSID
4334			 */
4335			ieee802_11_parse_elems(
4336				mgmt->u.action.u.ext_chan_switch.variable,
4337				ies_len, true, &elems, mgmt->bssid, NULL);
4338
4339			if (elems.parse_error)
4340				break;
4341
4342			/* for the handling code pretend this was also an IE */
4343			elems.ext_chansw_ie =
4344				&mgmt->u.action.u.ext_chan_switch.data;
4345
4346			ieee80211_sta_process_chanswitch(sdata,
4347						 rx_status->mactime,
4348						 rx_status->device_timestamp,
4349						 &elems, false);
4350		}
4351		break;
4352	}
4353	sdata_unlock(sdata);
4354}
4355
4356static void ieee80211_sta_timer(struct timer_list *t)
4357{
4358	struct ieee80211_sub_if_data *sdata =
4359		from_timer(sdata, t, u.mgd.timer);
4360
4361	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
4362}
4363
4364static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4365					  u8 *bssid, u8 reason, bool tx)
4366{
4367	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4368
4369	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
4370			       tx, frame_buf);
4371
4372	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4373				    reason);
4374}
4375
4376static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
4377{
4378	struct ieee80211_local *local = sdata->local;
4379	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4380	struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
4381	u32 tx_flags = 0;
4382	u16 trans = 1;
4383	u16 status = 0;
4384	u16 prepare_tx_duration = 0;
4385
4386	sdata_assert_lock(sdata);
4387
4388	if (WARN_ON_ONCE(!auth_data))
4389		return -EINVAL;
4390
4391	auth_data->tries++;
4392
4393	if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
4394		sdata_info(sdata, "authentication with %pM timed out\n",
4395			   auth_data->bss->bssid);
4396
4397		/*
4398		 * Most likely AP is not in the range so remove the
4399		 * bss struct for that AP.
4400		 */
4401		cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4402
4403		return -ETIMEDOUT;
4404	}
4405
4406	if (auth_data->algorithm == WLAN_AUTH_SAE)
4407		prepare_tx_duration =
4408			jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4409
4410	drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
4411
4412	sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4413		   auth_data->bss->bssid, auth_data->tries,
4414		   IEEE80211_AUTH_MAX_TRIES);
4415
4416	auth_data->expected_transaction = 2;
4417
4418	if (auth_data->algorithm == WLAN_AUTH_SAE) {
4419		trans = auth_data->sae_trans;
4420		status = auth_data->sae_status;
4421		auth_data->expected_transaction = trans;
4422	}
4423
4424	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4425		tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4426			   IEEE80211_TX_INTFL_MLME_CONN_TX;
4427
4428	ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4429			    auth_data->data, auth_data->data_len,
4430			    auth_data->bss->bssid,
4431			    auth_data->bss->bssid, NULL, 0, 0,
4432			    tx_flags);
4433
4434	if (tx_flags == 0) {
4435		if (auth_data->algorithm == WLAN_AUTH_SAE)
4436			auth_data->timeout = jiffies +
4437				IEEE80211_AUTH_TIMEOUT_SAE;
4438		else
4439			auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
4440	} else {
4441		auth_data->timeout =
4442			round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
4443	}
4444
4445	auth_data->timeout_started = true;
4446	run_again(sdata, auth_data->timeout);
4447
4448	return 0;
4449}
4450
4451static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4452{
4453	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4454	struct ieee80211_local *local = sdata->local;
4455
4456	sdata_assert_lock(sdata);
4457
4458	assoc_data->tries++;
4459	if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
4460		sdata_info(sdata, "association with %pM timed out\n",
4461			   assoc_data->bss->bssid);
4462
4463		/*
4464		 * Most likely AP is not in the range so remove the
4465		 * bss struct for that AP.
4466		 */
4467		cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4468
4469		return -ETIMEDOUT;
4470	}
4471
4472	sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4473		   assoc_data->bss->bssid, assoc_data->tries,
4474		   IEEE80211_ASSOC_MAX_TRIES);
4475	ieee80211_send_assoc(sdata);
4476
4477	if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4478		assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
4479		assoc_data->timeout_started = true;
4480		run_again(sdata, assoc_data->timeout);
4481	} else {
4482		assoc_data->timeout =
4483			round_jiffies_up(jiffies +
4484					 IEEE80211_ASSOC_TIMEOUT_LONG);
4485		assoc_data->timeout_started = true;
4486		run_again(sdata, assoc_data->timeout);
4487	}
4488
4489	return 0;
4490}
4491
4492void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4493				  __le16 fc, bool acked)
4494{
4495	struct ieee80211_local *local = sdata->local;
4496
4497	sdata->u.mgd.status_fc = fc;
4498	sdata->u.mgd.status_acked = acked;
4499	sdata->u.mgd.status_received = true;
4500
4501	ieee80211_queue_work(&local->hw, &sdata->work);
4502}
4503
4504void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
4505{
4506	struct ieee80211_local *local = sdata->local;
4507	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4508
4509	sdata_lock(sdata);
4510
4511	if (ifmgd->status_received) {
4512		__le16 fc = ifmgd->status_fc;
4513		bool status_acked = ifmgd->status_acked;
4514
4515		ifmgd->status_received = false;
4516		if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
4517			if (status_acked) {
4518				if (ifmgd->auth_data->algorithm ==
4519				    WLAN_AUTH_SAE)
4520					ifmgd->auth_data->timeout =
4521						jiffies +
4522						IEEE80211_AUTH_TIMEOUT_SAE;
4523				else
4524					ifmgd->auth_data->timeout =
4525						jiffies +
4526						IEEE80211_AUTH_TIMEOUT_SHORT;
4527				run_again(sdata, ifmgd->auth_data->timeout);
4528			} else {
4529				ifmgd->auth_data->timeout = jiffies - 1;
4530			}
4531			ifmgd->auth_data->timeout_started = true;
4532		} else if (ifmgd->assoc_data &&
4533			   (ieee80211_is_assoc_req(fc) ||
4534			    ieee80211_is_reassoc_req(fc))) {
4535			if (status_acked) {
4536				ifmgd->assoc_data->timeout =
4537					jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
4538				run_again(sdata, ifmgd->assoc_data->timeout);
4539			} else {
4540				ifmgd->assoc_data->timeout = jiffies - 1;
4541			}
4542			ifmgd->assoc_data->timeout_started = true;
4543		}
4544	}
4545
4546	if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
4547	    time_after(jiffies, ifmgd->auth_data->timeout)) {
4548		if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
4549			/*
4550			 * ok ... we waited for assoc or continuation but
4551			 * userspace didn't do it, so kill the auth data
4552			 */
4553			ieee80211_destroy_auth_data(sdata, false);
4554		} else if (ieee80211_auth(sdata)) {
4555			u8 bssid[ETH_ALEN];
4556			struct ieee80211_event event = {
4557				.type = MLME_EVENT,
4558				.u.mlme.data = AUTH_EVENT,
4559				.u.mlme.status = MLME_TIMEOUT,
4560			};
4561
4562			memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4563
4564			ieee80211_destroy_auth_data(sdata, false);
4565
4566			cfg80211_auth_timeout(sdata->dev, bssid);
4567			drv_event_callback(sdata->local, sdata, &event);
4568		}
4569	} else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
4570		run_again(sdata, ifmgd->auth_data->timeout);
4571
4572	if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
4573	    time_after(jiffies, ifmgd->assoc_data->timeout)) {
4574		if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
4575		    ieee80211_do_assoc(sdata)) {
4576			struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4577			struct ieee80211_event event = {
4578				.type = MLME_EVENT,
4579				.u.mlme.data = ASSOC_EVENT,
4580				.u.mlme.status = MLME_TIMEOUT,
4581			};
4582
4583			ieee80211_destroy_assoc_data(sdata, false, false);
4584			cfg80211_assoc_timeout(sdata->dev, bss);
4585			drv_event_callback(sdata->local, sdata, &event);
4586		}
4587	} else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
4588		run_again(sdata, ifmgd->assoc_data->timeout);
4589
4590	if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
4591	    ifmgd->associated) {
4592		u8 bssid[ETH_ALEN];
4593		int max_tries;
4594
4595		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4596
4597		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4598			max_tries = max_nullfunc_tries;
4599		else
4600			max_tries = max_probe_tries;
4601
4602		/* ACK received for nullfunc probing frame */
4603		if (!ifmgd->probe_send_count)
4604			ieee80211_reset_ap_probe(sdata);
4605		else if (ifmgd->nullfunc_failed) {
4606			if (ifmgd->probe_send_count < max_tries) {
4607				mlme_dbg(sdata,
4608					 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4609					 bssid, ifmgd->probe_send_count,
4610					 max_tries);
4611				ieee80211_mgd_probe_ap_send(sdata);
4612			} else {
4613				mlme_dbg(sdata,
4614					 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4615					 bssid);
4616				ieee80211_sta_connection_lost(sdata, bssid,
4617					WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4618					false);
4619			}
4620		} else if (time_is_after_jiffies(ifmgd->probe_timeout))
4621			run_again(sdata, ifmgd->probe_timeout);
4622		else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4623			mlme_dbg(sdata,
4624				 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4625				 bssid, probe_wait_ms);
4626			ieee80211_sta_connection_lost(sdata, bssid,
4627				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4628		} else if (ifmgd->probe_send_count < max_tries) {
4629			mlme_dbg(sdata,
4630				 "No probe response from AP %pM after %dms, try %d/%i\n",
4631				 bssid, probe_wait_ms,
4632				 ifmgd->probe_send_count, max_tries);
4633			ieee80211_mgd_probe_ap_send(sdata);
4634		} else {
4635			/*
4636			 * We actually lost the connection ... or did we?
4637			 * Let's make sure!
4638			 */
4639			mlme_dbg(sdata,
4640				 "No probe response from AP %pM after %dms, disconnecting.\n",
4641				 bssid, probe_wait_ms);
4642
4643			ieee80211_sta_connection_lost(sdata, bssid,
4644				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4645		}
4646	}
4647
4648	sdata_unlock(sdata);
4649}
4650
4651static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
4652{
4653	struct ieee80211_sub_if_data *sdata =
4654		from_timer(sdata, t, u.mgd.bcn_mon_timer);
4655	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4656
4657	if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4658		return;
4659
4660	if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
4661		return;
4662
4663	sdata->u.mgd.connection_loss = false;
4664	ieee80211_queue_work(&sdata->local->hw,
4665			     &sdata->u.mgd.beacon_connection_loss_work);
4666}
4667
4668static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
4669{
4670	struct ieee80211_sub_if_data *sdata =
4671		from_timer(sdata, t, u.mgd.conn_mon_timer);
4672	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4673	struct ieee80211_local *local = sdata->local;
4674	struct sta_info *sta;
4675	unsigned long timeout;
4676
4677	if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4678		return;
4679
4680	sta = sta_info_get(sdata, ifmgd->bssid);
4681	if (!sta)
4682		return;
4683
4684	timeout = sta->status_stats.last_ack;
4685	if (time_before(sta->status_stats.last_ack, sta->rx_stats.last_rx))
4686		timeout = sta->rx_stats.last_rx;
4687	timeout += IEEE80211_CONNECTION_IDLE_TIME;
4688
4689	/* If timeout is after now, then update timer to fire at
4690	 * the later date, but do not actually probe at this time.
4691	 */
4692	if (time_is_after_jiffies(timeout)) {
4693		mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
4694		return;
4695	}
4696
4697	ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
4698}
4699
4700static void ieee80211_sta_monitor_work(struct work_struct *work)
4701{
4702	struct ieee80211_sub_if_data *sdata =
4703		container_of(work, struct ieee80211_sub_if_data,
4704			     u.mgd.monitor_work);
4705
4706	ieee80211_mgd_probe_ap(sdata, false);
4707}
4708
4709static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4710{
4711	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
4712		__ieee80211_stop_poll(sdata);
4713
4714		/* let's probe the connection once */
4715		if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4716			ieee80211_queue_work(&sdata->local->hw,
4717					     &sdata->u.mgd.monitor_work);
4718	}
4719}
4720
4721#ifdef CONFIG_PM
4722void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4723{
4724	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4725	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4726
4727	sdata_lock(sdata);
4728
4729	if (ifmgd->auth_data || ifmgd->assoc_data) {
4730		const u8 *bssid = ifmgd->auth_data ?
4731				ifmgd->auth_data->bss->bssid :
4732				ifmgd->assoc_data->bss->bssid;
4733
4734		/*
4735		 * If we are trying to authenticate / associate while suspending,
4736		 * cfg80211 won't know and won't actually abort those attempts,
4737		 * thus we need to do that ourselves.
4738		 */
4739		ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
4740					       IEEE80211_STYPE_DEAUTH,
4741					       WLAN_REASON_DEAUTH_LEAVING,
4742					       false, frame_buf);
4743		if (ifmgd->assoc_data)
4744			ieee80211_destroy_assoc_data(sdata, false, true);
4745		if (ifmgd->auth_data)
4746			ieee80211_destroy_auth_data(sdata, false);
4747		cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4748				      IEEE80211_DEAUTH_FRAME_LEN);
4749	}
4750
4751	/* This is a bit of a hack - we should find a better and more generic
4752	 * solution to this. Normally when suspending, cfg80211 will in fact
4753	 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4754	 * auth (not so important) or assoc (this is the problem) process.
4755	 *
4756	 * As a consequence, it can happen that we are in the process of both
4757	 * associating and suspending, and receive an association response
4758	 * after cfg80211 has checked if it needs to disconnect, but before
4759	 * we actually set the flag to drop incoming frames. This will then
4760	 * cause the workqueue flush to process the association response in
4761	 * the suspend, resulting in a successful association just before it
4762	 * tries to remove the interface from the driver, which now though
4763	 * has a channel context assigned ... this results in issues.
4764	 *
4765	 * To work around this (for now) simply deauth here again if we're
4766	 * now connected.
4767	 */
4768	if (ifmgd->associated && !sdata->local->wowlan) {
4769		u8 bssid[ETH_ALEN];
4770		struct cfg80211_deauth_request req = {
4771			.reason_code = WLAN_REASON_DEAUTH_LEAVING,
4772			.bssid = bssid,
4773		};
4774
4775		memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4776		ieee80211_mgd_deauth(sdata, &req);
4777	}
4778
4779	sdata_unlock(sdata);
4780}
4781
4782void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4783{
4784	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4785
4786	sdata_lock(sdata);
4787	if (!ifmgd->associated) {
4788		sdata_unlock(sdata);
4789		return;
4790	}
4791
4792	if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4793		sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4794		mlme_dbg(sdata, "driver requested disconnect after resume\n");
4795		ieee80211_sta_connection_lost(sdata,
4796					      ifmgd->associated->bssid,
4797					      WLAN_REASON_UNSPECIFIED,
4798					      true);
4799		sdata_unlock(sdata);
4800		return;
4801	}
4802	sdata_unlock(sdata);
4803}
4804#endif
4805
4806/* interface setup */
4807void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4808{
4809	struct ieee80211_if_managed *ifmgd;
4810
4811	ifmgd = &sdata->u.mgd;
4812	INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
4813	INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
4814	INIT_WORK(&ifmgd->beacon_connection_loss_work,
4815		  ieee80211_beacon_connection_loss_work);
4816	INIT_WORK(&ifmgd->csa_connection_drop_work,
4817		  ieee80211_csa_connection_drop_work);
4818	INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
4819	INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4820			  ieee80211_tdls_peer_del_work);
4821	timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4822	timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4823	timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4824	timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
4825	INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4826			  ieee80211_sta_handle_tspec_ac_params_wk);
4827
4828	ifmgd->flags = 0;
4829	ifmgd->powersave = sdata->wdev.ps;
4830	ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4831	ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
4832	ifmgd->p2p_noa_index = -1;
4833
4834	if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
4835		ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4836	else
4837		ifmgd->req_smps = IEEE80211_SMPS_OFF;
4838
4839	/* Setup TDLS data */
4840	spin_lock_init(&ifmgd->teardown_lock);
4841	ifmgd->teardown_skb = NULL;
4842	ifmgd->orig_teardown_skb = NULL;
4843}
4844
4845/* scan finished notification */
4846void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4847{
4848	struct ieee80211_sub_if_data *sdata;
4849
4850	/* Restart STA timers */
4851	rcu_read_lock();
4852	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4853		if (ieee80211_sdata_running(sdata))
4854			ieee80211_restart_sta_timer(sdata);
4855	}
4856	rcu_read_unlock();
4857}
4858
4859static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4860				     struct cfg80211_bss *cbss)
4861{
4862	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4863	const u8 *ht_cap_ie, *vht_cap_ie;
4864	const struct ieee80211_ht_cap *ht_cap;
4865	const struct ieee80211_vht_cap *vht_cap;
4866	u8 chains = 1;
4867
4868	if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4869		return chains;
4870
4871	ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4872	if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4873		ht_cap = (void *)(ht_cap_ie + 2);
4874		chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4875		/*
4876		 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4877		 *	 "Tx Unequal Modulation Supported" fields.
4878		 */
4879	}
4880
4881	if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4882		return chains;
4883
4884	vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4885	if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4886		u8 nss;
4887		u16 tx_mcs_map;
4888
4889		vht_cap = (void *)(vht_cap_ie + 2);
4890		tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4891		for (nss = 8; nss > 0; nss--) {
4892			if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4893					IEEE80211_VHT_MCS_NOT_SUPPORTED)
4894				break;
4895		}
4896		/* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4897		chains = max(chains, nss);
4898	}
4899
4900	return chains;
4901}
4902
4903static bool
4904ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4905				    const struct ieee80211_he_operation *he_op)
4906{
4907	const struct ieee80211_sta_he_cap *sta_he_cap =
4908		ieee80211_get_he_sta_cap(sband);
4909	u16 ap_min_req_set;
4910	int i;
4911
4912	if (!sta_he_cap || !he_op)
4913		return false;
4914
4915	ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4916
4917	/* Need to go over for 80MHz, 160MHz and for 80+80 */
4918	for (i = 0; i < 3; i++) {
4919		const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4920			&sta_he_cap->he_mcs_nss_supp;
4921		u16 sta_mcs_map_rx =
4922			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4923		u16 sta_mcs_map_tx =
4924			le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4925		u8 nss;
4926		bool verified = true;
4927
4928		/*
4929		 * For each band there is a maximum of 8 spatial streams
4930		 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4931		 * of 2 bits per NSS (1-8), with the values defined in enum
4932		 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4933		 * capabilities aren't less than the AP's minimum requirements
4934		 * for this HE BSS per SS.
4935		 * It is enough to find one such band that meets the reqs.
4936		 */
4937		for (nss = 8; nss > 0; nss--) {
4938			u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4939			u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4940			u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4941
4942			if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4943				continue;
4944
4945			/*
4946			 * Make sure the HE AP doesn't require MCSs that aren't
4947			 * supported by the client
4948			 */
4949			if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4950			    sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4951			    (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4952				verified = false;
4953				break;
4954			}
4955		}
4956
4957		if (verified)
4958			return true;
4959	}
4960
4961	/* If here, STA doesn't meet AP's HE min requirements */
4962	return false;
4963}
4964
4965static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4966				  struct cfg80211_bss *cbss)
4967{
4968	struct ieee80211_local *local = sdata->local;
4969	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4970	const struct ieee80211_ht_cap *ht_cap = NULL;
4971	const struct ieee80211_ht_operation *ht_oper = NULL;
4972	const struct ieee80211_vht_operation *vht_oper = NULL;
4973	const struct ieee80211_he_operation *he_oper = NULL;
4974	const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
4975	struct ieee80211_supported_band *sband;
4976	struct cfg80211_chan_def chandef;
4977	bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
4978	bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
4979	struct ieee80211_bss *bss = (void *)cbss->priv;
4980	int ret;
4981	u32 i;
4982	bool have_80mhz;
4983
4984	sband = local->hw.wiphy->bands[cbss->channel->band];
4985
4986	ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4987			  IEEE80211_STA_DISABLE_80P80MHZ |
4988			  IEEE80211_STA_DISABLE_160MHZ);
4989
4990	/* disable HT/VHT/HE if we don't support them */
4991	if (!sband->ht_cap.ht_supported && !is_6ghz) {
4992		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4993		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4994		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4995	}
4996
4997	if (!sband->vht_cap.vht_supported && is_5ghz) {
4998		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4999		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5000	}
5001
5002	if (!ieee80211_get_he_sta_cap(sband))
5003		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5004
5005	rcu_read_lock();
5006
5007	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
5008		const u8 *ht_oper_ie, *ht_cap_ie;
5009
5010		ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
5011		if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
5012			ht_oper = (void *)(ht_oper_ie + 2);
5013
5014		ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
5015		if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
5016			ht_cap = (void *)(ht_cap_ie + 2);
5017
5018		if (!ht_cap) {
5019			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5020			ht_oper = NULL;
5021		}
5022	}
5023
5024	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
5025		const u8 *vht_oper_ie, *vht_cap;
5026
5027		vht_oper_ie = ieee80211_bss_get_ie(cbss,
5028						   WLAN_EID_VHT_OPERATION);
5029		if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
5030			vht_oper = (void *)(vht_oper_ie + 2);
5031		if (vht_oper && !ht_oper) {
5032			vht_oper = NULL;
5033			sdata_info(sdata,
5034				   "AP advertised VHT without HT, disabling HT/VHT/HE\n");
5035			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5036			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5037			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5038		}
5039
5040		vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
5041		if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
5042			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5043			vht_oper = NULL;
5044		}
5045	}
5046
5047	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
5048		const struct cfg80211_bss_ies *ies;
5049		const u8 *he_oper_ie;
5050
5051		ies = rcu_dereference(cbss->ies);
5052		he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
5053						  ies->data, ies->len);
5054		if (he_oper_ie &&
5055		    he_oper_ie[1] >= ieee80211_he_oper_size(&he_oper_ie[3]))
5056			he_oper = (void *)(he_oper_ie + 3);
5057		else
5058			he_oper = NULL;
5059
5060		if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
5061			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5062	}
5063
5064	/* Allow VHT if at least one channel on the sband supports 80 MHz */
5065	have_80mhz = false;
5066	for (i = 0; i < sband->n_channels; i++) {
5067		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5068						IEEE80211_CHAN_NO_80MHZ))
5069			continue;
5070
5071		have_80mhz = true;
5072		break;
5073	}
5074
5075	if (!have_80mhz)
5076		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5077
5078	if (sband->band == NL80211_BAND_S1GHZ) {
5079		const u8 *s1g_oper_ie;
5080
5081		s1g_oper_ie = ieee80211_bss_get_ie(cbss,
5082						   WLAN_EID_S1G_OPERATION);
5083		if (s1g_oper_ie && s1g_oper_ie[1] >= sizeof(*s1g_oper))
5084			s1g_oper = (void *)(s1g_oper_ie + 2);
5085		else
5086			sdata_info(sdata,
5087				   "AP missing S1G operation element?\n");
5088	}
5089
5090	ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
5091						     cbss->channel,
5092						     bss->vht_cap_info,
5093						     ht_oper, vht_oper, he_oper,
5094						     s1g_oper,
5095						     &chandef, false);
5096
5097	sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
5098				      local->rx_chains);
5099
5100	rcu_read_unlock();
5101
5102	if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
5103		sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
5104		return -EINVAL;
5105	}
5106
5107	/* will change later if needed */
5108	sdata->smps_mode = IEEE80211_SMPS_OFF;
5109
5110	mutex_lock(&local->mtx);
5111	/*
5112	 * If this fails (possibly due to channel context sharing
5113	 * on incompatible channels, e.g. 80+80 and 160 sharing the
5114	 * same control channel) try to use a smaller bandwidth.
5115	 */
5116	ret = ieee80211_vif_use_channel(sdata, &chandef,
5117					IEEE80211_CHANCTX_SHARED);
5118
5119	/* don't downgrade for 5 and 10 MHz channels, though. */
5120	if (chandef.width == NL80211_CHAN_WIDTH_5 ||
5121	    chandef.width == NL80211_CHAN_WIDTH_10)
5122		goto out;
5123
5124	while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
5125		ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
5126		ret = ieee80211_vif_use_channel(sdata, &chandef,
5127						IEEE80211_CHANCTX_SHARED);
5128	}
5129 out:
5130	mutex_unlock(&local->mtx);
5131	return ret;
5132}
5133
5134static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5135			       u8 *dtim_count, u8 *dtim_period)
5136{
5137	const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5138	const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5139					 ies->len);
5140	const struct ieee80211_tim_ie *tim = NULL;
5141	const struct ieee80211_bssid_index *idx;
5142	bool valid = tim_ie && tim_ie[1] >= 2;
5143
5144	if (valid)
5145		tim = (void *)(tim_ie + 2);
5146
5147	if (dtim_count)
5148		*dtim_count = valid ? tim->dtim_count : 0;
5149
5150	if (dtim_period)
5151		*dtim_period = valid ? tim->dtim_period : 0;
5152
5153	/* Check if value is overridden by non-transmitted profile */
5154	if (!idx_ie || idx_ie[1] < 3)
5155		return valid;
5156
5157	idx = (void *)(idx_ie + 2);
5158
5159	if (dtim_count)
5160		*dtim_count = idx->dtim_count;
5161
5162	if (dtim_period)
5163		*dtim_period = idx->dtim_period;
5164
5165	return true;
5166}
5167
5168static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
5169				     struct cfg80211_bss *cbss, bool assoc,
5170				     bool override)
5171{
5172	struct ieee80211_local *local = sdata->local;
5173	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5174	struct ieee80211_bss *bss = (void *)cbss->priv;
5175	struct sta_info *new_sta = NULL;
5176	struct ieee80211_supported_band *sband;
5177	bool have_sta = false;
5178	int err;
5179
5180	sband = local->hw.wiphy->bands[cbss->channel->band];
5181
5182	if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5183		return -EINVAL;
5184
5185	/* If a reconfig is happening, bail out */
5186	if (local->in_reconfig)
5187		return -EBUSY;
5188
5189	if (assoc) {
5190		rcu_read_lock();
5191		have_sta = sta_info_get(sdata, cbss->bssid);
5192		rcu_read_unlock();
5193	}
5194
5195	if (!have_sta) {
5196		new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5197		if (!new_sta)
5198			return -ENOMEM;
5199	}
5200
5201	/*
5202	 * Set up the information for the new channel before setting the
5203	 * new channel. We can't - completely race-free - change the basic
5204	 * rates bitmap and the channel (sband) that it refers to, but if
5205	 * we set it up before we at least avoid calling into the driver's
5206	 * bss_info_changed() method with invalid information (since we do
5207	 * call that from changing the channel - only for IDLE and perhaps
5208	 * some others, but ...).
5209	 *
5210	 * So to avoid that, just set up all the new information before the
5211	 * channel, but tell the driver to apply it only afterwards, since
5212	 * it might need the new channel for that.
5213	 */
5214	if (new_sta) {
5215		u32 rates = 0, basic_rates = 0;
5216		bool have_higher_than_11mbit = false;
5217		int min_rate = INT_MAX, min_rate_index = -1;
5218		const struct cfg80211_bss_ies *ies;
5219		int shift = ieee80211_vif_get_shift(&sdata->vif);
5220
5221		/* TODO: S1G Basic Rate Set is expressed elsewhere */
5222		if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5223			ieee80211_s1g_sta_rate_init(new_sta);
5224			goto skip_rates;
5225		}
5226
5227		ieee80211_get_rates(sband, bss->supp_rates,
5228				    bss->supp_rates_len,
5229				    &rates, &basic_rates,
5230				    &have_higher_than_11mbit,
5231				    &min_rate, &min_rate_index,
5232				    shift);
5233
5234		/*
5235		 * This used to be a workaround for basic rates missing
5236		 * in the association response frame. Now that we no
5237		 * longer use the basic rates from there, it probably
5238		 * doesn't happen any more, but keep the workaround so
5239		 * in case some *other* APs are buggy in different ways
5240		 * we can connect -- with a warning.
5241		 * Allow this workaround only in case the AP provided at least
5242		 * one rate.
5243		 */
5244		if (min_rate_index < 0) {
5245			sdata_info(sdata,
5246				   "No legacy rates in association response\n");
5247
5248			sta_info_free(local, new_sta);
5249			return -EINVAL;
5250		} else if (!basic_rates) {
5251			sdata_info(sdata,
5252				   "No basic rates, using min rate instead\n");
5253			basic_rates = BIT(min_rate_index);
5254		}
5255
5256		if (rates)
5257			new_sta->sta.supp_rates[cbss->channel->band] = rates;
5258		else
5259			sdata_info(sdata,
5260				   "No rates found, keeping mandatory only\n");
5261
5262		sdata->vif.bss_conf.basic_rates = basic_rates;
5263
5264		/* cf. IEEE 802.11 9.2.12 */
5265		if (cbss->channel->band == NL80211_BAND_2GHZ &&
5266		    have_higher_than_11mbit)
5267			sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5268		else
5269			sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5270
5271skip_rates:
5272		memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5273
5274		/* set timing information */
5275		sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
5276		rcu_read_lock();
5277		ies = rcu_dereference(cbss->beacon_ies);
5278		if (ies) {
5279			sdata->vif.bss_conf.sync_tsf = ies->tsf;
5280			sdata->vif.bss_conf.sync_device_ts =
5281				bss->device_ts_beacon;
5282
5283			ieee80211_get_dtim(ies,
5284					   &sdata->vif.bss_conf.sync_dtim_count,
5285					   NULL);
5286		} else if (!ieee80211_hw_check(&sdata->local->hw,
5287					       TIMING_BEACON_ONLY)) {
5288			ies = rcu_dereference(cbss->proberesp_ies);
5289			/* must be non-NULL since beacon IEs were NULL */
5290			sdata->vif.bss_conf.sync_tsf = ies->tsf;
5291			sdata->vif.bss_conf.sync_device_ts =
5292				bss->device_ts_presp;
5293			sdata->vif.bss_conf.sync_dtim_count = 0;
5294		} else {
5295			sdata->vif.bss_conf.sync_tsf = 0;
5296			sdata->vif.bss_conf.sync_device_ts = 0;
5297			sdata->vif.bss_conf.sync_dtim_count = 0;
5298		}
5299		rcu_read_unlock();
5300	}
5301
5302	if (new_sta || override) {
5303		err = ieee80211_prep_channel(sdata, cbss);
5304		if (err) {
5305			if (new_sta)
5306				sta_info_free(local, new_sta);
5307			return -EINVAL;
5308		}
5309	}
5310
5311	if (new_sta) {
5312		/*
5313		 * tell driver about BSSID, basic rates and timing
5314		 * this was set up above, before setting the channel
5315		 */
5316		ieee80211_bss_info_change_notify(sdata,
5317			BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5318			BSS_CHANGED_BEACON_INT);
5319
5320		if (assoc)
5321			sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
5322
5323		err = sta_info_insert(new_sta);
5324		new_sta = NULL;
5325		if (err) {
5326			sdata_info(sdata,
5327				   "failed to insert STA entry for the AP (error %d)\n",
5328				   err);
5329			return err;
5330		}
5331	} else
5332		WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
5333
5334	/* Cancel scan to ensure that nothing interferes with connection */
5335	if (local->scanning)
5336		ieee80211_scan_cancel(local);
5337
5338	return 0;
5339}
5340
5341/* config hooks */
5342int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5343		       struct cfg80211_auth_request *req)
5344{
5345	struct ieee80211_local *local = sdata->local;
5346	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5347	struct ieee80211_mgd_auth_data *auth_data;
5348	u16 auth_alg;
5349	int err;
5350	bool cont_auth;
5351
5352	/* prepare auth data structure */
5353
5354	switch (req->auth_type) {
5355	case NL80211_AUTHTYPE_OPEN_SYSTEM:
5356		auth_alg = WLAN_AUTH_OPEN;
5357		break;
5358	case NL80211_AUTHTYPE_SHARED_KEY:
5359		if (fips_enabled)
5360			return -EOPNOTSUPP;
5361		auth_alg = WLAN_AUTH_SHARED_KEY;
5362		break;
5363	case NL80211_AUTHTYPE_FT:
5364		auth_alg = WLAN_AUTH_FT;
5365		break;
5366	case NL80211_AUTHTYPE_NETWORK_EAP:
5367		auth_alg = WLAN_AUTH_LEAP;
5368		break;
5369	case NL80211_AUTHTYPE_SAE:
5370		auth_alg = WLAN_AUTH_SAE;
5371		break;
5372	case NL80211_AUTHTYPE_FILS_SK:
5373		auth_alg = WLAN_AUTH_FILS_SK;
5374		break;
5375	case NL80211_AUTHTYPE_FILS_SK_PFS:
5376		auth_alg = WLAN_AUTH_FILS_SK_PFS;
5377		break;
5378	case NL80211_AUTHTYPE_FILS_PK:
5379		auth_alg = WLAN_AUTH_FILS_PK;
5380		break;
5381	default:
5382		return -EOPNOTSUPP;
5383	}
5384
5385	if (ifmgd->assoc_data)
5386		return -EBUSY;
5387
5388	auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
5389			    req->ie_len, GFP_KERNEL);
5390	if (!auth_data)
5391		return -ENOMEM;
5392
5393	auth_data->bss = req->bss;
5394
5395	if (req->auth_data_len >= 4) {
5396		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5397			__le16 *pos = (__le16 *) req->auth_data;
5398
5399			auth_data->sae_trans = le16_to_cpu(pos[0]);
5400			auth_data->sae_status = le16_to_cpu(pos[1]);
5401		}
5402		memcpy(auth_data->data, req->auth_data + 4,
5403		       req->auth_data_len - 4);
5404		auth_data->data_len += req->auth_data_len - 4;
5405	}
5406
5407	/* Check if continuing authentication or trying to authenticate with the
5408	 * same BSS that we were in the process of authenticating with and avoid
5409	 * removal and re-addition of the STA entry in
5410	 * ieee80211_prep_connection().
5411	 */
5412	cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5413
5414	if (req->ie && req->ie_len) {
5415		memcpy(&auth_data->data[auth_data->data_len],
5416		       req->ie, req->ie_len);
5417		auth_data->data_len += req->ie_len;
5418	}
5419
5420	if (req->key && req->key_len) {
5421		auth_data->key_len = req->key_len;
5422		auth_data->key_idx = req->key_idx;
5423		memcpy(auth_data->key, req->key, req->key_len);
5424	}
5425
5426	auth_data->algorithm = auth_alg;
5427
5428	/* try to authenticate/probe */
5429
5430	if (ifmgd->auth_data) {
5431		if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5432			auth_data->peer_confirmed =
5433				ifmgd->auth_data->peer_confirmed;
5434		}
5435		ieee80211_destroy_auth_data(sdata, cont_auth);
5436	}
5437
5438	/* prep auth_data so we don't go into idle on disassoc */
5439	ifmgd->auth_data = auth_data;
5440
5441	/* If this is continuation of an ongoing SAE authentication exchange
5442	 * (i.e., request to send SAE Confirm) and the peer has already
5443	 * confirmed, mark authentication completed since we are about to send
5444	 * out SAE Confirm.
5445	 */
5446	if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5447	    auth_data->peer_confirmed && auth_data->sae_trans == 2)
5448		ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5449
5450	if (ifmgd->associated) {
5451		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5452
5453		sdata_info(sdata,
5454			   "disconnect from AP %pM for new auth to %pM\n",
5455			   ifmgd->associated->bssid, req->bss->bssid);
5456		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5457				       WLAN_REASON_UNSPECIFIED,
5458				       false, frame_buf);
5459
5460		ieee80211_report_disconnect(sdata, frame_buf,
5461					    sizeof(frame_buf), true,
5462					    WLAN_REASON_UNSPECIFIED);
5463	}
5464
5465	sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
5466
5467	err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
5468	if (err)
5469		goto err_clear;
5470
5471	err = ieee80211_auth(sdata);
5472	if (err) {
5473		sta_info_destroy_addr(sdata, req->bss->bssid);
5474		goto err_clear;
5475	}
5476
5477	/* hold our own reference */
5478	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
5479	return 0;
5480
5481 err_clear:
5482	eth_zero_addr(ifmgd->bssid);
5483	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5484	ifmgd->auth_data = NULL;
5485	mutex_lock(&sdata->local->mtx);
5486	ieee80211_vif_release_channel(sdata);
5487	mutex_unlock(&sdata->local->mtx);
5488	kfree(auth_data);
5489	return err;
5490}
5491
5492int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5493			struct cfg80211_assoc_request *req)
5494{
5495	bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
5496	bool is_5ghz = req->bss->channel->band == NL80211_BAND_5GHZ;
5497	struct ieee80211_local *local = sdata->local;
5498	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5499	struct ieee80211_bss *bss = (void *)req->bss->priv;
5500	struct ieee80211_mgd_assoc_data *assoc_data;
5501	const struct cfg80211_bss_ies *beacon_ies;
5502	struct ieee80211_supported_band *sband;
5503	const u8 *ssidie, *ht_ie, *vht_ie;
5504	int i, err;
5505	bool override = false;
5506
5507	assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5508	if (!assoc_data)
5509		return -ENOMEM;
5510
5511	rcu_read_lock();
5512	ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
5513	if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
5514		rcu_read_unlock();
5515		kfree(assoc_data);
5516		return -EINVAL;
5517	}
5518	memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5519	assoc_data->ssid_len = ssidie[1];
5520	rcu_read_unlock();
5521
5522	if (ifmgd->associated) {
5523		u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5524
5525		sdata_info(sdata,
5526			   "disconnect from AP %pM for new assoc to %pM\n",
5527			   ifmgd->associated->bssid, req->bss->bssid);
5528		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5529				       WLAN_REASON_UNSPECIFIED,
5530				       false, frame_buf);
5531
5532		ieee80211_report_disconnect(sdata, frame_buf,
5533					    sizeof(frame_buf), true,
5534					    WLAN_REASON_UNSPECIFIED);
5535	}
5536
5537	if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5538		err = -EBUSY;
5539		goto err_free;
5540	}
5541
5542	if (ifmgd->assoc_data) {
5543		err = -EBUSY;
5544		goto err_free;
5545	}
5546
5547	if (ifmgd->auth_data) {
5548		bool match;
5549
5550		/* keep sta info, bssid if matching */
5551		match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
5552		ieee80211_destroy_auth_data(sdata, match);
5553	}
5554
5555	/* prepare assoc data */
5556
5557	ifmgd->beacon_crc_valid = false;
5558
5559	assoc_data->wmm = bss->wmm_used &&
5560			  (local->hw.queues >= IEEE80211_NUM_ACS);
5561
5562	/*
5563	 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5564	 * We still associate in non-HT mode (11a/b/g) if any one of these
5565	 * ciphers is configured as pairwise.
5566	 * We can set this to true for non-11n hardware, that'll be checked
5567	 * separately along with the peer capabilities.
5568	 */
5569	for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5570		if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5571		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5572		    req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5573			ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5574			ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5575			ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5576			netdev_info(sdata->dev,
5577				    "disabling HT/VHT/HE due to WEP/TKIP use\n");
5578		}
5579	}
5580
5581	sband = local->hw.wiphy->bands[req->bss->channel->band];
5582
5583	/* also disable HT/VHT/HE if the AP doesn't use WMM */
5584	if (!bss->wmm_used) {
5585		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5586		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5587		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5588		netdev_info(sdata->dev,
5589			    "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
5590	}
5591
5592	memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5593	memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5594	       sizeof(ifmgd->ht_capa_mask));
5595
5596	memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5597	memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5598	       sizeof(ifmgd->vht_capa_mask));
5599
5600	memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
5601	memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
5602	       sizeof(ifmgd->s1g_capa_mask));
5603
5604	if (req->ie && req->ie_len) {
5605		memcpy(assoc_data->ie, req->ie, req->ie_len);
5606		assoc_data->ie_len = req->ie_len;
5607	}
5608
5609	if (req->fils_kek) {
5610		/* should already be checked in cfg80211 - so warn */
5611		if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5612			err = -EINVAL;
5613			goto err_free;
5614		}
5615		memcpy(assoc_data->fils_kek, req->fils_kek,
5616		       req->fils_kek_len);
5617		assoc_data->fils_kek_len = req->fils_kek_len;
5618	}
5619
5620	if (req->fils_nonces)
5621		memcpy(assoc_data->fils_nonces, req->fils_nonces,
5622		       2 * FILS_NONCE_LEN);
5623
5624	assoc_data->bss = req->bss;
5625
5626	if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5627		if (ifmgd->powersave)
5628			sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
5629		else
5630			sdata->smps_mode = IEEE80211_SMPS_OFF;
5631	} else
5632		sdata->smps_mode = ifmgd->req_smps;
5633
5634	assoc_data->capability = req->bss->capability;
5635	assoc_data->supp_rates = bss->supp_rates;
5636	assoc_data->supp_rates_len = bss->supp_rates_len;
5637
5638	rcu_read_lock();
5639	ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5640	if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5641		assoc_data->ap_ht_param =
5642			((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
5643	else if (!is_6ghz)
5644		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5645	vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5646	if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5647		memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5648		       sizeof(struct ieee80211_vht_cap));
5649	else if (is_5ghz)
5650		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5651				IEEE80211_STA_DISABLE_HE;
5652	rcu_read_unlock();
5653
5654	if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
5655		 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
5656	     "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5657		sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5658
5659	if (bss->wmm_used && bss->uapsd_supported &&
5660	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
5661		assoc_data->uapsd = true;
5662		ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5663	} else {
5664		assoc_data->uapsd = false;
5665		ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5666	}
5667
5668	if (req->prev_bssid)
5669		memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
5670
5671	if (req->use_mfp) {
5672		ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5673		ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5674	} else {
5675		ifmgd->mfp = IEEE80211_MFP_DISABLED;
5676		ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5677	}
5678
5679	if (req->flags & ASSOC_REQ_USE_RRM)
5680		ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5681	else
5682		ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5683
5684	if (req->crypto.control_port)
5685		ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5686	else
5687		ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5688
5689	sdata->control_port_protocol = req->crypto.control_port_ethertype;
5690	sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
5691	sdata->control_port_over_nl80211 =
5692					req->crypto.control_port_over_nl80211;
5693	sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
5694	sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5695							sdata->vif.type);
5696
5697	/* kick off associate process */
5698
5699	ifmgd->assoc_data = assoc_data;
5700	ifmgd->dtim_period = 0;
5701	ifmgd->have_beacon = false;
5702
5703	/* override HT/VHT configuration only if the AP and we support it */
5704	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5705		struct ieee80211_sta_ht_cap sta_ht_cap;
5706
5707		if (req->flags & ASSOC_REQ_DISABLE_HT)
5708			override = true;
5709
5710		memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5711		ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5712
5713		/* check for 40 MHz disable override */
5714		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5715		    sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5716		    !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5717			override = true;
5718
5719		if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5720		    req->flags & ASSOC_REQ_DISABLE_VHT)
5721			override = true;
5722	}
5723
5724	if (req->flags & ASSOC_REQ_DISABLE_HT) {
5725		ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5726		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5727		ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5728	}
5729
5730	if (req->flags & ASSOC_REQ_DISABLE_VHT)
5731		ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5732
5733	err = ieee80211_prep_connection(sdata, req->bss, true, override);
5734	if (err)
5735		goto err_clear;
5736
5737	rcu_read_lock();
5738	beacon_ies = rcu_dereference(req->bss->beacon_ies);
5739
5740	if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
5741	    !beacon_ies) {
5742		/*
5743		 * Wait up to one beacon interval ...
5744		 * should this be more if we miss one?
5745		 */
5746		sdata_info(sdata, "waiting for beacon from %pM\n",
5747			   ifmgd->bssid);
5748		assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
5749		assoc_data->timeout_started = true;
5750		assoc_data->need_beacon = true;
5751	} else if (beacon_ies) {
5752		const struct element *elem;
5753		u8 dtim_count = 0;
5754
5755		ieee80211_get_dtim(beacon_ies, &dtim_count,
5756				   &ifmgd->dtim_period);
5757
5758		ifmgd->have_beacon = true;
5759		assoc_data->timeout = jiffies;
5760		assoc_data->timeout_started = true;
5761
5762		if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
5763			sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5764			sdata->vif.bss_conf.sync_device_ts =
5765				bss->device_ts_beacon;
5766			sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5767		}
5768
5769		elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5770					      beacon_ies->data, beacon_ies->len);
5771		if (elem && elem->datalen >= 3)
5772			sdata->vif.bss_conf.profile_periodicity = elem->data[2];
5773		else
5774			sdata->vif.bss_conf.profile_periodicity = 0;
5775
5776		elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5777					  beacon_ies->data, beacon_ies->len);
5778		if (elem && elem->datalen >= 11 &&
5779		    (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
5780			sdata->vif.bss_conf.ema_ap = true;
5781		else
5782			sdata->vif.bss_conf.ema_ap = false;
5783	} else {
5784		assoc_data->timeout = jiffies;
5785		assoc_data->timeout_started = true;
5786	}
5787	rcu_read_unlock();
5788
5789	run_again(sdata, assoc_data->timeout);
5790
5791	if (bss->corrupt_data) {
5792		char *corrupt_type = "data";
5793		if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5794			if (bss->corrupt_data &
5795					IEEE80211_BSS_CORRUPT_PROBE_RESP)
5796				corrupt_type = "beacon and probe response";
5797			else
5798				corrupt_type = "beacon";
5799		} else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5800			corrupt_type = "probe response";
5801		sdata_info(sdata, "associating with AP with corrupt %s\n",
5802			   corrupt_type);
5803	}
5804
5805	return 0;
5806 err_clear:
5807	eth_zero_addr(ifmgd->bssid);
5808	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5809	ifmgd->assoc_data = NULL;
5810 err_free:
5811	kfree(assoc_data);
5812	return err;
5813}
5814
5815int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
5816			 struct cfg80211_deauth_request *req)
5817{
5818	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5819	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5820	bool tx = !req->local_state_change;
5821
5822	if (ifmgd->auth_data &&
5823	    ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5824		sdata_info(sdata,
5825			   "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5826			   req->bssid, req->reason_code,
5827			   ieee80211_get_reason_code_string(req->reason_code));
5828
5829		drv_mgd_prepare_tx(sdata->local, sdata, 0);
5830		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
5831					       IEEE80211_STYPE_DEAUTH,
5832					       req->reason_code, tx,
5833					       frame_buf);
5834		ieee80211_destroy_auth_data(sdata, false);
5835		ieee80211_report_disconnect(sdata, frame_buf,
5836					    sizeof(frame_buf), true,
5837					    req->reason_code);
5838
5839		return 0;
5840	}
5841
5842	if (ifmgd->assoc_data &&
5843	    ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5844		sdata_info(sdata,
5845			   "aborting association with %pM by local choice (Reason: %u=%s)\n",
5846			   req->bssid, req->reason_code,
5847			   ieee80211_get_reason_code_string(req->reason_code));
5848
5849		drv_mgd_prepare_tx(sdata->local, sdata, 0);
5850		ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
5851					       IEEE80211_STYPE_DEAUTH,
5852					       req->reason_code, tx,
5853					       frame_buf);
5854		ieee80211_destroy_assoc_data(sdata, false, true);
5855		ieee80211_report_disconnect(sdata, frame_buf,
5856					    sizeof(frame_buf), true,
5857					    req->reason_code);
5858		return 0;
5859	}
5860
5861	if (ifmgd->associated &&
5862	    ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
5863		sdata_info(sdata,
5864			   "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5865			   req->bssid, req->reason_code,
5866			   ieee80211_get_reason_code_string(req->reason_code));
5867
5868		ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5869				       req->reason_code, tx, frame_buf);
5870		ieee80211_report_disconnect(sdata, frame_buf,
5871					    sizeof(frame_buf), true,
5872					    req->reason_code);
5873		return 0;
5874	}
5875
5876	return -ENOTCONN;
5877}
5878
5879int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
5880			   struct cfg80211_disassoc_request *req)
5881{
5882	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5883	u8 bssid[ETH_ALEN];
5884	u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5885
5886	/*
5887	 * cfg80211 should catch this ... but it's racy since
5888	 * we can receive a disassoc frame, process it, hand it
5889	 * to cfg80211 while that's in a locked section already
5890	 * trying to tell us that the user wants to disconnect.
5891	 */
5892	if (ifmgd->associated != req->bss)
5893		return -ENOLINK;
5894
5895	sdata_info(sdata,
5896		   "disassociating from %pM by local choice (Reason: %u=%s)\n",
5897		   req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
5898
5899	memcpy(bssid, req->bss->bssid, ETH_ALEN);
5900	ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5901			       req->reason_code, !req->local_state_change,
5902			       frame_buf);
5903
5904	ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5905				    req->reason_code);
5906
5907	return 0;
5908}
5909
5910void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
5911{
5912	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5913
5914	/*
5915	 * Make sure some work items will not run after this,
5916	 * they will not do anything but might not have been
5917	 * cancelled when disconnecting.
5918	 */
5919	cancel_work_sync(&ifmgd->monitor_work);
5920	cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5921	cancel_work_sync(&ifmgd->request_smps_work);
5922	cancel_work_sync(&ifmgd->csa_connection_drop_work);
5923	cancel_work_sync(&ifmgd->chswitch_work);
5924	cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
5925
5926	sdata_lock(sdata);
5927	if (ifmgd->assoc_data) {
5928		struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
5929		ieee80211_destroy_assoc_data(sdata, false, false);
5930		cfg80211_assoc_timeout(sdata->dev, bss);
5931	}
5932	if (ifmgd->auth_data)
5933		ieee80211_destroy_auth_data(sdata, false);
5934	spin_lock_bh(&ifmgd->teardown_lock);
5935	if (ifmgd->teardown_skb) {
5936		kfree_skb(ifmgd->teardown_skb);
5937		ifmgd->teardown_skb = NULL;
5938		ifmgd->orig_teardown_skb = NULL;
5939	}
5940	kfree(ifmgd->assoc_req_ies);
5941	ifmgd->assoc_req_ies = NULL;
5942	ifmgd->assoc_req_ies_len = 0;
5943	spin_unlock_bh(&ifmgd->teardown_lock);
5944	del_timer_sync(&ifmgd->timer);
5945	sdata_unlock(sdata);
5946}
5947
5948void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5949			       enum nl80211_cqm_rssi_threshold_event rssi_event,
5950			       s32 rssi_level,
5951			       gfp_t gfp)
5952{
5953	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5954
5955	trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
5956
5957	cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
5958}
5959EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
5960
5961void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5962{
5963	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5964
5965	trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5966
5967	cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5968}
5969EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
5970