blob: 1eeabdf1005241a5be57b681e1f6bb1b9bccc3e8 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jiri Bencf0706e82007-05-05 11:45:53 -07002/*
3 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02004 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07005 * 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>
Johannes Bergd98ad832014-09-03 15:24:57 +03009 * Copyright 2013-2014 Intel Mobile Communications GmbH
Avraham Sterne38a0172017-04-26 10:58:47 +030010 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Johannes Berg9bd6a83e2021-06-18 13:41:53 +030011 * Copyright (C) 2018 - 2021 Intel Corporation
Jiri Bencf0706e82007-05-05 11:45:53 -070012 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Ard Biesheuvel5fdb3732019-06-12 18:19:54 +020015#include <linux/fips.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070016#include <linux/if_ether.h>
17#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070019#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040020#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010021#include <linux/rtnetlink.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b39c2008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jouni Malinen39404fe2016-10-27 00:42:05 +030032#include "fils_aead.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070033
Johannes Berg1672c0e32013-01-29 15:02:27 +010034#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020035#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010036#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
Ilan Peer407879b2018-04-20 13:49:20 +030037#define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010038#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
40#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020041#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010042#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
43#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010044
Ben Greear180205b2011-02-04 15:30:24 -080045static int max_nullfunc_tries = 2;
46module_param(max_nullfunc_tries, int, 0644);
47MODULE_PARM_DESC(max_nullfunc_tries,
48 "Maximum nullfunc tx tries before disconnecting (reason 4).");
49
50static int max_probe_tries = 5;
51module_param(max_probe_tries, int, 0644);
52MODULE_PARM_DESC(max_probe_tries,
53 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020054
55/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010056 * Beacon loss timeout is calculated as N frames times the
57 * advertised beacon interval. This may need to be somewhat
58 * higher than what hardware might detect to account for
59 * delays in the host processing frames. But since we also
60 * probe on beacon miss before declaring the connection lost
61 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020062 */
Ben Greear59c1ec22013-03-19 14:19:56 -070063static int beacon_loss_count = 7;
64module_param(beacon_loss_count, int, 0644);
65MODULE_PARM_DESC(beacon_loss_count,
66 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010067
Johannes Bergb291ba12009-07-10 15:29:03 +020068/*
69 * Time the connection can be idle before we probe
70 * it to see if we can still talk to the AP.
71 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030072#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020073/*
74 * Time we wait for a probe response after sending
75 * a probe request because of beacon loss or for
76 * checking the connection still works.
77 */
Ben Greear180205b2011-02-04 15:30:24 -080078static int probe_wait_ms = 500;
79module_param(probe_wait_ms, int, 0644);
80MODULE_PARM_DESC(probe_wait_ms,
81 "Maximum time(ms) to wait for probe response"
82 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070083
Jouni Malinen17e4ec12010-03-29 23:28:30 -070084/*
Jouni Malinen391a2002010-08-27 22:22:00 +030085 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg77fdaa12009-07-07 03:45:17 +020090/*
Johannes Bergca386f32009-07-10 02:39:48 +020091 * We can have multiple work items (and connection probing)
92 * scheduling this timer, but we need to take care to only
93 * reschedule it when it should fire _earlier_ than it was
94 * asked for before, or if it's not pending right now. This
95 * function ensures that. Note that it then is required to
96 * run this function for all timeouts after the first one
97 * has happened -- the work that runs from this timer will
98 * do that.
99 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200100static void run_again(struct ieee80211_sub_if_data *sdata,
101 unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200102{
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200103 sdata_assert_lock(sdata);
Johannes Bergca386f32009-07-10 02:39:48 +0200104
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200105 if (!timer_pending(&sdata->u.mgd.timer) ||
106 time_before(timeout, sdata->u.mgd.timer.expires))
107 mod_timer(&sdata->u.mgd.timer, timeout);
Johannes Bergca386f32009-07-10 02:39:48 +0200108}
109
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400110void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200111{
Johannes Bergc1288b12012-01-19 09:29:57 +0100112 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200113 return;
114
Johannes Berg30686bf2015-06-02 21:39:54 +0200115 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Johannes Berg7eeff742012-07-18 10:27:27 +0200116 return;
117
Johannes Bergb291ba12009-07-10 15:29:03 +0200118 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100119 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200120}
121
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400122void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
123{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
125
Johannes Bergb100e5d2016-03-17 15:41:37 +0200126 if (unlikely(!ifmgd->associated))
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100127 return;
128
Johannes Bergb100e5d2016-03-17 15:41:37 +0200129 if (ifmgd->probe_send_count)
130 ifmgd->probe_send_count = 0;
Eliad Peller448cd2e2014-02-11 12:30:18 +0200131
Johannes Berg30686bf2015-06-02 21:39:54 +0200132 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400133 return;
134
Johannes Bergb100e5d2016-03-17 15:41:37 +0200135 mod_timer(&ifmgd->conn_mon_timer,
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400136 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
137}
138
Johannes Berg5484e232008-09-08 17:44:27 +0200139static int ecw2cw(int ecw)
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200140{
Johannes Berg5484e232008-09-08 17:44:27 +0200141 return (1 << ecw) - 1;
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200142}
143
Johannes Berg6565ec92013-02-08 14:52:32 +0100144static u32
145ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
146 struct ieee80211_supported_band *sband,
147 struct ieee80211_channel *channel,
Johannes Berg2a333a02020-05-28 21:34:35 +0200148 u32 vht_cap_info,
Johannes Berg6565ec92013-02-08 14:52:32 +0100149 const struct ieee80211_ht_operation *ht_oper,
150 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300151 const struct ieee80211_he_operation *he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700152 const struct ieee80211_s1g_oper_ie *s1g_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200153 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100154{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100156 struct cfg80211_chan_def vht_chandef;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200157 struct ieee80211_sta_ht_cap sta_ht_cap;
Johannes Berg6565ec92013-02-08 14:52:32 +0100158 u32 ht_cfreq, ret;
159
Alexei Avshalom Lazar2a380752019-08-18 17:35:17 +0300160 memset(chandef, 0, sizeof(struct cfg80211_chan_def));
Johannes Berg6565ec92013-02-08 14:52:32 +0100161 chandef->chan = channel;
162 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
163 chandef->center_freq1 = channel->center_freq;
Thomas Pedersenb60119602020-04-01 18:18:04 -0700164 chandef->freq1_offset = channel->freq_offset;
Johannes Berg6565ec92013-02-08 14:52:32 +0100165
Johannes Berg57fa5e82020-05-28 21:34:36 +0200166 if (channel->band == NL80211_BAND_6GHZ) {
Johannes Berg636ccda2021-11-30 13:16:59 +0200167 if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, chandef)) {
168 mlme_dbg(sdata,
169 "bad 6 GHz operation, disabling HT/VHT/HE\n");
Johannes Berg57fa5e82020-05-28 21:34:36 +0200170 ret = IEEE80211_STA_DISABLE_HT |
171 IEEE80211_STA_DISABLE_VHT |
172 IEEE80211_STA_DISABLE_HE;
Johannes Berg636ccda2021-11-30 13:16:59 +0200173 } else {
Johannes Berg523f3ec2020-06-03 11:15:03 +0200174 ret = 0;
Johannes Berg636ccda2021-11-30 13:16:59 +0200175 }
Johannes Berg57fa5e82020-05-28 21:34:36 +0200176 vht_chandef = *chandef;
177 goto out;
Thomas Pedersen75f87ea2020-10-01 10:47:48 -0700178 } else if (sband->band == NL80211_BAND_S1GHZ) {
179 if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) {
180 sdata_info(sdata,
181 "Missing S1G Operation Element? Trying operating == primary\n");
182 chandef->width = ieee80211_s1g_channel_width(channel);
183 }
Johannes Berg57fa5e82020-05-28 21:34:36 +0200184
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700185 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_40MHZ |
186 IEEE80211_STA_DISABLE_VHT |
187 IEEE80211_STA_DISABLE_80P80MHZ |
188 IEEE80211_STA_DISABLE_160MHZ;
189 goto out;
190 }
191
Thomas Pedersen75f87ea2020-10-01 10:47:48 -0700192 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
193 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
194
Johannes Bergb1b1ae22017-10-13 15:26:01 +0300195 if (!ht_oper || !sta_ht_cap.ht_supported) {
Johannes Berg636ccda2021-11-30 13:16:59 +0200196 mlme_dbg(sdata, "HT operation missing / HT not supported\n");
Johannes Berg75e296e2020-01-31 13:12:39 +0200197 ret = IEEE80211_STA_DISABLE_HT |
198 IEEE80211_STA_DISABLE_VHT |
199 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100200 goto out;
201 }
202
203 chandef->width = NL80211_CHAN_WIDTH_20;
204
205 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
206 channel->band);
207 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200208 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100209 /*
210 * It's possible that some APs are confused here;
211 * Netgear WNDR3700 sometimes reports 4 higher than
212 * the actual channel in association responses, but
213 * since we look at probe response/beacon data here
214 * it should be OK.
215 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200216 sdata_info(sdata,
217 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
218 channel->center_freq, ht_cfreq,
219 ht_oper->primary_chan, channel->band);
Johannes Berg75e296e2020-01-31 13:12:39 +0200220 ret = IEEE80211_STA_DISABLE_HT |
221 IEEE80211_STA_DISABLE_VHT |
222 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100223 goto out;
224 }
225
226 /* check 40 MHz support, if we have it */
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200227 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100228 ieee80211_chandef_ht_oper(ht_oper, chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100229 } else {
Johannes Berg636ccda2021-11-30 13:16:59 +0200230 mlme_dbg(sdata, "40 MHz not supported\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100231 /* 40 MHz (and 80 MHz) must be supported for VHT */
232 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100233 /* also mark 40 MHz disabled */
234 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100235 goto out;
236 }
237
238 if (!vht_oper || !sband->vht_cap.vht_supported) {
Johannes Berg636ccda2021-11-30 13:16:59 +0200239 mlme_dbg(sdata, "VHT operation missing / VHT not supported\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100240 ret = IEEE80211_STA_DISABLE_VHT;
241 goto out;
242 }
243
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100244 vht_chandef = *chandef;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300245 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
246 (le32_to_cpu(he_oper->he_oper_params) &
247 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
248 struct ieee80211_vht_operation he_oper_vht_cap;
249
250 /*
251 * Set only first 3 bytes (other 2 aren't used in
252 * ieee80211_chandef_vht_oper() anyway)
253 */
254 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
255 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
256
Johannes Berg2a333a02020-05-28 21:34:35 +0200257 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
Johannes Berg7eb26df2018-08-31 11:31:18 +0300258 &he_oper_vht_cap, ht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300259 &vht_chandef)) {
260 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
261 sdata_info(sdata,
Johannes Berg636ccda2021-11-30 13:16:59 +0200262 "HE AP VHT information is invalid, disabling HE\n");
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300263 ret = IEEE80211_STA_DISABLE_HE;
264 goto out;
265 }
Johannes Berg2a333a02020-05-28 21:34:35 +0200266 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
267 vht_cap_info,
268 vht_oper, ht_oper,
269 &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200270 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100271 sdata_info(sdata,
Johannes Berg636ccda2021-11-30 13:16:59 +0200272 "AP VHT information is invalid, disabling VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100273 ret = IEEE80211_STA_DISABLE_VHT;
274 goto out;
275 }
276
277 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200278 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100279 sdata_info(sdata,
Johannes Berg636ccda2021-11-30 13:16:59 +0200280 "AP VHT information is invalid, disabling VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100281 ret = IEEE80211_STA_DISABLE_VHT;
282 goto out;
283 }
284
285 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
286 ret = 0;
287 goto out;
288 }
289
290 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200291 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100292 sdata_info(sdata,
Johannes Berg636ccda2021-11-30 13:16:59 +0200293 "AP VHT information doesn't match HT, disabling VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100294 ret = IEEE80211_STA_DISABLE_VHT;
295 goto out;
296 }
297
298 *chandef = vht_chandef;
299
300 ret = 0;
301
302out:
Johannes Berg963a18522014-02-21 20:34:34 +0100303 /*
304 * When tracking the current AP, don't do any further checks if the
305 * new chandef is identical to the one we're currently using for the
306 * connection. This keeps us from playing ping-pong with regulatory,
307 * without it the following can happen (for example):
308 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
309 * - AP advertises regdom US
310 * - CRDA loads regdom US with 80 MHz prohibited (old database)
311 * - the code below detects an unsupported channel, downgrades, and
312 * we disconnect from the AP in the caller
313 * - disconnect causes CRDA to reload world regdomain and the game
314 * starts anew.
315 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
316 *
317 * It seems possible that there are still scenarios with CSA or real
318 * bandwidth changes where a this could happen, but those cases are
319 * less common and wouldn't completely prevent using the AP.
320 */
321 if (tracking &&
322 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
323 return ret;
324
Johannes Berg586e01e2013-02-14 12:13:53 +0100325 /* don't print the message below for VHT mismatch if VHT is disabled */
326 if (ret & IEEE80211_STA_DISABLE_VHT)
327 vht_chandef = *chandef;
328
Johannes Bergddfe49b2013-07-31 20:52:03 +0200329 /*
330 * Ignore the DISABLED flag when we're already connected and only
331 * tracking the APs beacon for bandwidth changes - otherwise we
332 * might get disconnected here if we connect to an AP, update our
333 * regulatory information based on the AP's country IE and the
334 * information we have is wrong/outdated and disables the channel
335 * that we're actually using for the connection to the AP.
336 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100337 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200338 tracking ? 0 :
339 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100340 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
341 ret = IEEE80211_STA_DISABLE_HT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200342 IEEE80211_STA_DISABLE_VHT |
343 IEEE80211_STA_DISABLE_HE;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700344 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100345 }
346
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200347 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100348 }
349
Daniel Gabay8cadb202020-01-31 13:12:52 +0200350 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
351 IEEE80211_CHAN_NO_HE))
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200352 ret |= IEEE80211_STA_DISABLE_HE;
353
Johannes Berg5cdaed12013-07-31 11:23:06 +0200354 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100355 sdata_info(sdata,
356 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
357
358 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
359 return ret;
360}
361
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100362static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
363 struct sta_info *sta,
Eliad Peller53b954e2014-07-24 11:20:05 +0300364 const struct ieee80211_ht_cap *ht_cap,
Johannes Berg2a333a02020-05-28 21:34:35 +0200365 const struct ieee80211_vht_cap *vht_cap,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100366 const struct ieee80211_ht_operation *ht_oper,
367 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300368 const struct ieee80211_he_operation *he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700369 const struct ieee80211_s1g_oper_ie *s1g_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100370 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200371{
372 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100373 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300374 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
375 struct ieee80211_supported_band *sband =
376 local->hw.wiphy->bands[chan->band];
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100377 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200378 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100379 u32 flags;
Johannes Berg2a333a02020-05-28 21:34:35 +0200380 u32 vht_cap_info = 0;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100381 int ret;
382
383 /* if HT was/is disabled, don't track any bandwidth changes */
384 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
385 return 0;
386
387 /* don't check VHT if we associated as non-VHT station */
388 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
389 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200390
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300391 /* don't check HE if we associated as non-HE station */
392 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
Johannes Bergbac2fd32021-06-18 13:41:50 +0300393 !ieee80211_get_he_iftype_cap(sband,
394 ieee80211_vif_type_p2p(&sdata->vif)))
395
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300396 he_oper = NULL;
397
Johannes Berg11289582013-02-07 17:36:12 +0100398 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100399 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100400
Avri Altman017b45b2013-11-18 19:06:48 +0200401 /*
402 * if bss configuration changed store the new one -
403 * this may be applicable even if channel is identical
404 */
405 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
406 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
407 *changed |= BSS_CHANGED_HT;
408 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
409 }
410
Johannes Berg2a333a02020-05-28 21:34:35 +0200411 if (vht_cap)
412 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
413
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300414 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
Johannes Berg2a333a02020-05-28 21:34:35 +0200415 flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300416 ht_oper, vht_oper, he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700417 s1g_oper, &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100418
419 /*
420 * Downgrade the new channel if we associated with restricted
421 * capabilities. For example, if we associated as a 20 MHz STA
422 * to a 40 MHz AP (due to regulatory, capabilities or config
423 * reasons) then switching to a 40 MHz channel now won't do us
424 * any good -- we couldn't use it with the AP.
425 */
426 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
427 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200428 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100429 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
430 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200431 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100432 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
433 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200434 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100435
436 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
437 return 0;
438
439 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -0700440 "AP %pM changed bandwidth, new config is %d.%03d MHz, "
441 "width %d (%d.%03d/%d MHz)\n",
442 ifmgd->bssid, chandef.chan->center_freq,
443 chandef.chan->freq_offset, chandef.width,
444 chandef.center_freq1, chandef.freq1_offset,
445 chandef.center_freq2);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100446
447 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
448 IEEE80211_STA_DISABLE_VHT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200449 IEEE80211_STA_DISABLE_HE |
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100450 IEEE80211_STA_DISABLE_40MHZ |
451 IEEE80211_STA_DISABLE_80P80MHZ |
452 IEEE80211_STA_DISABLE_160MHZ)) ||
453 !cfg80211_chandef_valid(&chandef)) {
454 sdata_info(sdata,
Johannes Berg6516ee22021-06-18 13:41:47 +0300455 "AP %pM changed caps/bw in a way we can't support (0x%x/0x%x) - disconnect\n",
456 ifmgd->bssid, flags, ifmgd->flags);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100457 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200458 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200459
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100460 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
Mordechay Goodsteind6c37502021-06-18 13:41:30 +0300461
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100462 if (ret) {
463 sdata_info(sdata,
464 "AP %pM changed bandwidth to incompatible one - disconnect\n",
465 ifmgd->bssid);
466 return ret;
467 }
468
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100469 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200470}
471
Johannes Berg9c6bd792008-09-11 00:01:52 +0200472/* frame sending functions */
473
Johannes Berg66e67e42012-01-20 13:55:27 +0100474static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200475 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100476 struct ieee80211_supported_band *sband,
477 struct ieee80211_channel *channel,
478 enum ieee80211_smps_mode smps)
479{
Johannes Berg66e67e42012-01-20 13:55:27 +0100480 u8 *pos;
481 u32 flags = channel->flags;
482 u16 cap;
483 struct ieee80211_sta_ht_cap ht_cap;
484
485 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
486
Johannes Berg66e67e42012-01-20 13:55:27 +0100487 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
488 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
489
Johannes Berg66e67e42012-01-20 13:55:27 +0100490 /* determine capability flags */
491 cap = ht_cap.cap;
492
Johannes Berg9dde6422012-05-16 23:43:19 +0200493 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100494 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
495 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
496 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
497 cap &= ~IEEE80211_HT_CAP_SGI_40;
498 }
499 break;
500 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
501 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
502 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
503 cap &= ~IEEE80211_HT_CAP_SGI_40;
504 }
505 break;
506 }
507
Johannes Berg24398e32012-03-28 10:58:36 +0200508 /*
509 * If 40 MHz was disabled associate as though we weren't
510 * capable of 40 MHz -- some broken APs will never fall
511 * back to trying to transmit in 20 MHz.
512 */
513 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
514 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
515 cap &= ~IEEE80211_HT_CAP_SGI_40;
516 }
517
Johannes Berg66e67e42012-01-20 13:55:27 +0100518 /* set SM PS mode properly */
519 cap &= ~IEEE80211_HT_CAP_SM_PS;
520 switch (smps) {
521 case IEEE80211_SMPS_AUTOMATIC:
522 case IEEE80211_SMPS_NUM_MODES:
523 WARN_ON(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -0500524 fallthrough;
Johannes Berg66e67e42012-01-20 13:55:27 +0100525 case IEEE80211_SMPS_OFF:
526 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
527 IEEE80211_HT_CAP_SM_PS_SHIFT;
528 break;
529 case IEEE80211_SMPS_STATIC:
530 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
531 IEEE80211_HT_CAP_SM_PS_SHIFT;
532 break;
533 case IEEE80211_SMPS_DYNAMIC:
534 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
535 IEEE80211_HT_CAP_SM_PS_SHIFT;
536 break;
537 }
538
539 /* reserve and fill IE */
540 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
541 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
542}
543
Sara Sharon322cd402015-07-08 15:41:43 +0300544/* This function determines vht capability flags for the association
545 * and builds the IE.
546 * Note - the function may set the owner of the MU-MIMO capability
547 */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000548static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
549 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100550 struct ieee80211_supported_band *sband,
551 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000552{
Sara Sharon322cd402015-07-08 15:41:43 +0300553 struct ieee80211_local *local = sdata->local;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000554 u8 *pos;
555 u32 cap;
556 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200557 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000558
559 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
560
561 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100562 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000563
564 /* determine capability flags */
565 cap = vht_cap.cap;
566
Johannes Bergf2d9d272012-11-22 14:11:39 +0100567 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
Johannes Berg575f0532014-11-24 16:51:33 +0100568 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
569
570 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
571 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
572 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
573 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100574 }
575
576 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
577 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
Johannes Berg575f0532014-11-24 16:51:33 +0100578 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100579 }
580
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100581 /*
582 * Some APs apparently get confused if our capabilities are better
583 * than theirs, so restrict what we advertise in the assoc request.
584 */
585 if (!(ap_vht_cap->vht_cap_info &
586 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
Sara Sharon322cd402015-07-08 15:41:43 +0300587 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
588 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
589 else if (!(ap_vht_cap->vht_cap_info &
590 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
591 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
592
593 /*
Zheng Yongjunab4040d2021-06-07 23:00:47 +0800594 * If some other vif is using the MU-MIMO capability we cannot associate
Sara Sharon322cd402015-07-08 15:41:43 +0300595 * using MU-MIMO - this will lead to contradictions in the group-id
596 * mechanism.
597 * Ownership is defined since association request, in order to avoid
598 * simultaneous associations with MU-MIMO.
599 */
600 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
601 bool disable_mu_mimo = false;
602 struct ieee80211_sub_if_data *other;
603
604 list_for_each_entry_rcu(other, &local->interfaces, list) {
Sara Sharonb5a33d52016-02-16 12:48:18 +0200605 if (other->vif.mu_mimo_owner) {
Sara Sharon322cd402015-07-08 15:41:43 +0300606 disable_mu_mimo = true;
607 break;
608 }
609 }
610 if (disable_mu_mimo)
611 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
612 else
Sara Sharonb5a33d52016-02-16 12:48:18 +0200613 sdata->vif.mu_mimo_owner = true;
Sara Sharon322cd402015-07-08 15:41:43 +0300614 }
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100615
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200616 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
617
618 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
619 our_bf_sts = cap & mask;
620
621 if (ap_bf_sts < our_bf_sts) {
622 cap &= ~mask;
623 cap |= ap_bf_sts;
624 }
625
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000626 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000627 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000628 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
629}
630
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300631/* This function determines HE capability flags for the association
632 * and builds the IE.
633 */
634static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
635 struct sk_buff *skb,
636 struct ieee80211_supported_band *sband)
637{
638 u8 *pos;
639 const struct ieee80211_sta_he_cap *he_cap = NULL;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200640 struct ieee80211_chanctx_conf *chanctx_conf;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300641 u8 he_cap_size;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200642 bool reg_cap = false;
643
644 rcu_read_lock();
645 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
646 if (!WARN_ON_ONCE(!chanctx_conf))
647 reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
648 &chanctx_conf->def,
649 IEEE80211_CHAN_NO_HE);
650
651 rcu_read_unlock();
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300652
Johannes Bergbac2fd32021-06-18 13:41:50 +0300653 he_cap = ieee80211_get_he_iftype_cap(sband,
654 ieee80211_vif_type_p2p(&sdata->vif));
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200655 if (!he_cap || !reg_cap)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300656 return;
657
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300658 he_cap_size =
659 2 + 1 + sizeof(he_cap->he_cap_elem) +
660 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
661 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
662 he_cap->he_cap_elem.phy_cap_info);
663 pos = skb_put(skb, he_cap_size);
664 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200665
666 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300667}
668
Johannes Berg66e67e42012-01-20 13:55:27 +0100669static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
670{
671 struct ieee80211_local *local = sdata->local;
672 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
673 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
674 struct sk_buff *skb;
675 struct ieee80211_mgmt *mgmt;
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200676 u8 *pos, qos_info, *ie_start;
Johannes Berg66e67e42012-01-20 13:55:27 +0100677 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200678 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100679 u16 capab;
680 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200681 struct ieee80211_chanctx_conf *chanctx_conf;
682 struct ieee80211_channel *chan;
Johannes Berg44f6d422017-06-10 13:52:44 +0300683 u32 rates = 0;
Thomas Pedersen05d10952020-09-21 19:28:10 -0700684 __le16 listen_int;
Johannes Berg2ff69b02020-01-31 13:31:11 +0200685 struct element *ext_capa = NULL;
Johannes Berg9bd6a83e2021-06-18 13:41:53 +0300686 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
687 const struct ieee80211_sband_iftype_data *iftd;
Johannes Berg15fae342021-06-18 13:41:55 +0300688 struct ieee80211_prep_tx_info info = {};
Johannes Berg2ff69b02020-01-31 13:31:11 +0200689
690 /* we know it's writable, cast away the const */
691 if (assoc_data->ie_len)
692 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
693 assoc_data->ie,
694 assoc_data->ie_len);
Johannes Berg66e67e42012-01-20 13:55:27 +0100695
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200696 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100697
Johannes Berg55de9082012-07-26 17:24:39 +0200698 rcu_read_lock();
699 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
700 if (WARN_ON(!chanctx_conf)) {
701 rcu_read_unlock();
702 return;
703 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100704 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200705 rcu_read_unlock();
706 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200707 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100708
709 if (assoc_data->supp_rates_len) {
710 /*
711 * Get all rates supported by the device and the AP as
712 * some APs don't like getting a superset of their rates
713 * in the association request (e.g. D-Link DAP 1353 in
714 * b-only mode)...
715 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200716 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
717 assoc_data->supp_rates,
718 assoc_data->supp_rates_len,
719 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100720 } else {
721 /*
722 * In case AP not provide any supported rates information
723 * before association, we send information element(s) with
724 * all rates that we support.
725 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200726 rates_len = 0;
727 for (i = 0; i < sband->n_bitrates; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200728 rates |= BIT(i);
729 rates_len++;
730 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100731 }
732
Johannes Berg9bd6a83e2021-06-18 13:41:53 +0300733 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
734
Johannes Berg66e67e42012-01-20 13:55:27 +0100735 skb = alloc_skb(local->hw.extra_tx_headroom +
736 sizeof(*mgmt) + /* bit too much but doesn't matter */
737 2 + assoc_data->ssid_len + /* SSID */
738 4 + rates_len + /* (extended) rates */
739 4 + /* power capability */
740 2 + 2 * sband->n_channels + /* supported channels */
741 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000742 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300743 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
744 sizeof(struct ieee80211_he_mcs_nss_supp) +
745 IEEE80211_HE_PPE_THRES_MAX_LEN +
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200746 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100747 assoc_data->ie_len + /* extra IEs */
Jouni Malinen39404fe2016-10-27 00:42:05 +0300748 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
Johannes Berg9bd6a83e2021-06-18 13:41:53 +0300749 9 + /* WMM */
750 (iftd ? iftd->vendor_elems.len : 0),
Johannes Berg66e67e42012-01-20 13:55:27 +0100751 GFP_KERNEL);
752 if (!skb)
753 return;
754
755 skb_reserve(skb, local->hw.extra_tx_headroom);
756
757 capab = WLAN_CAPABILITY_ESS;
758
Johannes Berg57fbcce2016-04-12 15:56:15 +0200759 if (sband->band == NL80211_BAND_2GHZ) {
Johannes Bergea1b2b452015-06-02 20:15:49 +0200760 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
761 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Johannes Berg66e67e42012-01-20 13:55:27 +0100762 }
763
764 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
765 capab |= WLAN_CAPABILITY_PRIVACY;
766
767 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200768 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
Johannes Berg66e67e42012-01-20 13:55:27 +0100769 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
770
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300771 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
772 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
773
Johannes Bergb080db52017-06-16 14:29:19 +0200774 mgmt = skb_put_zero(skb, 24);
Johannes Berg66e67e42012-01-20 13:55:27 +0100775 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
776 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
777 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
778
Thomas Pedersen05d10952020-09-21 19:28:10 -0700779 listen_int = cpu_to_le16(sband->band == NL80211_BAND_S1GHZ ?
780 ieee80211_encode_usf(local->hw.conf.listen_interval) :
781 local->hw.conf.listen_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +0100782 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
783 skb_put(skb, 10);
784 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
785 IEEE80211_STYPE_REASSOC_REQ);
786 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
Thomas Pedersen05d10952020-09-21 19:28:10 -0700787 mgmt->u.reassoc_req.listen_interval = listen_int;
Johannes Berg66e67e42012-01-20 13:55:27 +0100788 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
789 ETH_ALEN);
Johannes Berg15fae342021-06-18 13:41:55 +0300790 info.subtype = IEEE80211_STYPE_REASSOC_REQ;
Johannes Berg66e67e42012-01-20 13:55:27 +0100791 } else {
792 skb_put(skb, 4);
793 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
794 IEEE80211_STYPE_ASSOC_REQ);
795 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Thomas Pedersen05d10952020-09-21 19:28:10 -0700796 mgmt->u.assoc_req.listen_interval = listen_int;
Johannes Berg15fae342021-06-18 13:41:55 +0300797 info.subtype = IEEE80211_STYPE_ASSOC_REQ;
Johannes Berg66e67e42012-01-20 13:55:27 +0100798 }
799
800 /* SSID */
801 pos = skb_put(skb, 2 + assoc_data->ssid_len);
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200802 ie_start = pos;
Johannes Berg66e67e42012-01-20 13:55:27 +0100803 *pos++ = WLAN_EID_SSID;
804 *pos++ = assoc_data->ssid_len;
805 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
806
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700807 if (sband->band == NL80211_BAND_S1GHZ)
808 goto skip_rates;
809
Johannes Berg66e67e42012-01-20 13:55:27 +0100810 /* add all rates which were marked to be used above */
811 supp_rates_len = rates_len;
812 if (supp_rates_len > 8)
813 supp_rates_len = 8;
814
815 pos = skb_put(skb, supp_rates_len + 2);
816 *pos++ = WLAN_EID_SUPP_RATES;
817 *pos++ = supp_rates_len;
818
819 count = 0;
820 for (i = 0; i < sband->n_bitrates; i++) {
821 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200822 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
823 5 * (1 << shift));
824 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100825 if (++count == 8)
826 break;
827 }
828 }
829
830 if (rates_len > count) {
831 pos = skb_put(skb, rates_len - count + 2);
832 *pos++ = WLAN_EID_EXT_SUPP_RATES;
833 *pos++ = rates_len - count;
834
835 for (i++; i < sband->n_bitrates; i++) {
836 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200837 int rate;
838 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
839 5 * (1 << shift));
840 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100841 }
842 }
843 }
844
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700845skip_rates:
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300846 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
847 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100848 pos = skb_put(skb, 4);
849 *pos++ = WLAN_EID_PWR_CAPABILITY;
850 *pos++ = 2;
851 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200852 /* max tx power */
853 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300854 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100855
Johannes Berg2ff69b02020-01-31 13:31:11 +0200856 /*
857 * Per spec, we shouldn't include the list of channels if we advertise
858 * support for extended channel switching, but we've always done that;
859 * (for now?) apply this restriction only on the (new) 6 GHz band.
860 */
861 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
862 (sband->band != NL80211_BAND_6GHZ ||
863 !ext_capa || ext_capa->datalen < 1 ||
864 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100865 /* TODO: get this in reg domain format */
866 pos = skb_put(skb, 2 * sband->n_channels + 2);
867 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
868 *pos++ = 2 * sband->n_channels;
869 for (i = 0; i < sband->n_channels; i++) {
870 *pos++ = ieee80211_frequency_to_channel(
871 sband->channels[i].center_freq);
872 *pos++ = 1; /* one channel in the subband*/
873 }
874 }
875
Sara Sharoncaf56332019-01-16 23:03:25 +0200876 /* Set MBSSID support for HE AP if needed */
877 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
Johannes Berg2ff69b02020-01-31 13:31:11 +0200878 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
879 ext_capa && ext_capa->datalen >= 3)
880 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
Sara Sharoncaf56332019-01-16 23:03:25 +0200881
Johannes Berg66e67e42012-01-20 13:55:27 +0100882 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200883 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100884 static const u8 before_ht[] = {
885 WLAN_EID_SSID,
886 WLAN_EID_SUPP_RATES,
887 WLAN_EID_EXT_SUPP_RATES,
888 WLAN_EID_PWR_CAPABILITY,
889 WLAN_EID_SUPPORTED_CHANNELS,
890 WLAN_EID_RSN,
891 WLAN_EID_QOS_CAPA,
892 WLAN_EID_RRM_ENABLED_CAPABILITIES,
893 WLAN_EID_MOBILITY_DOMAIN,
Johannes Berg8ed28742014-10-27 12:03:19 +0100894 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
895 WLAN_EID_RIC_DATA, /* reassoc only */
Johannes Berg66e67e42012-01-20 13:55:27 +0100896 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
897 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100898 static const u8 after_ric[] = {
899 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
900 WLAN_EID_HT_CAPABILITY,
901 WLAN_EID_BSS_COEX_2040,
Johannes Berga7f26d82017-08-05 11:44:32 +0300902 /* luckily this is almost always there */
Johannes Berg8ed28742014-10-27 12:03:19 +0100903 WLAN_EID_EXT_CAPABILITY,
904 WLAN_EID_QOS_TRAFFIC_CAPA,
905 WLAN_EID_TIM_BCAST_REQ,
906 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300907 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg8ed28742014-10-27 12:03:19 +0100908 WLAN_EID_VHT_CAPABILITY,
909 WLAN_EID_OPMODE_NOTIF,
910 };
911
912 noffset = ieee80211_ie_split_ric(assoc_data->ie,
913 assoc_data->ie_len,
914 before_ht,
915 ARRAY_SIZE(before_ht),
916 after_ric,
917 ARRAY_SIZE(after_ric),
918 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800919 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100920 offset = noffset;
921 }
922
Johannes Bergf2d9d272012-11-22 14:11:39 +0100923 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
924 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
925 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
926
Johannes Berg57fa5e82020-05-28 21:34:36 +0200927 if (sband->band != NL80211_BAND_6GHZ &&
928 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200929 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200930 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100931
Johannes Berg3de38022014-02-04 09:54:07 +0100932 /* if present, add any custom IEs that go before VHT */
933 if (assoc_data->ie_len) {
934 static const u8 before_vht[] = {
Johannes Berga7f26d82017-08-05 11:44:32 +0300935 /*
936 * no need to list the ones split off before HT
937 * or generated here
938 */
Johannes Berg3de38022014-02-04 09:54:07 +0100939 WLAN_EID_BSS_COEX_2040,
940 WLAN_EID_EXT_CAPABILITY,
941 WLAN_EID_QOS_TRAFFIC_CAPA,
942 WLAN_EID_TIM_BCAST_REQ,
943 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300944 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg3de38022014-02-04 09:54:07 +0100945 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100946
947 /* RIC already taken above, so no need to handle here anymore */
Johannes Berg3de38022014-02-04 09:54:07 +0100948 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
949 before_vht, ARRAY_SIZE(before_vht),
950 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800951 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg3de38022014-02-04 09:54:07 +0100952 offset = noffset;
953 }
954
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300955 /* if present, add any custom IEs that go before HE */
956 if (assoc_data->ie_len) {
957 static const u8 before_he[] = {
958 /*
959 * no need to list the ones split off before VHT
960 * or generated here
961 */
962 WLAN_EID_OPMODE_NOTIF,
963 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
964 /* 11ai elements */
965 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
966 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
967 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
968 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
969 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
970 /* TODO: add 11ah/11aj/11ak elements */
971 };
972
973 /* RIC already taken above, so no need to handle here anymore */
974 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
975 before_he, ARRAY_SIZE(before_he),
976 offset);
977 pos = skb_put(skb, noffset - offset);
978 memcpy(pos, assoc_data->ie + offset, noffset - offset);
979 offset = noffset;
980 }
981
Johannes Berg57fa5e82020-05-28 21:34:36 +0200982 if (sband->band != NL80211_BAND_6GHZ &&
983 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100984 ieee80211_add_vht_ie(sdata, skb, sband,
985 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000986
Shaul Triebitzdc7eb0f2018-12-15 11:03:20 +0200987 /*
988 * If AP doesn't support HT, mark HE as disabled.
989 * If on the 5GHz band, make sure it supports VHT.
990 */
991 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
992 (sband->band == NL80211_BAND_5GHZ &&
993 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
994 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
995
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300996 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
997 ieee80211_add_he_ie(sdata, skb, sband);
998
999 /* if present, add any custom non-vendor IEs that go after HE */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001000 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001001 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1002 assoc_data->ie_len,
1003 offset);
yuan linyub952f4d2017-06-18 22:52:04 +08001004 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001005 offset = noffset;
1006 }
1007
Johannes Berg76f03032012-03-08 15:02:05 +01001008 if (assoc_data->wmm) {
1009 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001010 qos_info = ifmgd->uapsd_queues;
1011 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +01001012 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1013 } else {
1014 qos_info = 0;
1015 }
1016
Arik Nemtsov40b861a2014-07-17 17:14:23 +03001017 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
Johannes Berg66e67e42012-01-20 13:55:27 +01001018 }
1019
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07001020 if (sband->band == NL80211_BAND_S1GHZ) {
1021 ieee80211_add_aid_request_ie(sdata, skb);
Thomas Pedersen7957c6c2020-09-21 19:28:05 -07001022 ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07001023 }
Thomas Pedersen7957c6c2020-09-21 19:28:05 -07001024
Johannes Berg9bd6a83e2021-06-18 13:41:53 +03001025 if (iftd && iftd->vendor_elems.data && iftd->vendor_elems.len)
1026 skb_put_data(skb, iftd->vendor_elems.data, iftd->vendor_elems.len);
1027
Johannes Berg66e67e42012-01-20 13:55:27 +01001028 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001029 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001030 noffset = assoc_data->ie_len;
yuan linyub952f4d2017-06-18 22:52:04 +08001031 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001032 }
1033
Jouni Malinen39404fe2016-10-27 00:42:05 +03001034 if (assoc_data->fils_kek_len &&
1035 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
1036 dev_kfree_skb(skb);
1037 return;
1038 }
1039
Jouni Malinen4d9ec732019-02-15 02:14:33 +02001040 pos = skb_tail_pointer(skb);
1041 kfree(ifmgd->assoc_req_ies);
1042 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1043 ifmgd->assoc_req_ies_len = pos - ie_start;
1044
Johannes Berg15fae342021-06-18 13:41:55 +03001045 drv_mgd_prepare_tx(local, sdata, &info);
Johannes Berga1845fc2012-06-27 13:18:36 +02001046
Johannes Berg66e67e42012-01-20 13:55:27 +01001047 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg30686bf2015-06-02 21:39:54 +02001048 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01001049 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1050 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +01001051 ieee80211_tx_skb(sdata, skb);
1052}
1053
Kalle Valo572e0012009-02-10 17:09:31 +02001054void ieee80211_send_pspoll(struct ieee80211_local *local,
1055 struct ieee80211_sub_if_data *sdata)
1056{
Kalle Valo572e0012009-02-10 17:09:31 +02001057 struct ieee80211_pspoll *pspoll;
1058 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +02001059
Kalle Valod8cd1892010-01-05 20:16:26 +02001060 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1061 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +02001062 return;
Kalle Valo572e0012009-02-10 17:09:31 +02001063
Kalle Valod8cd1892010-01-05 20:16:26 +02001064 pspoll = (struct ieee80211_pspoll *) skb->data;
1065 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +02001066
Johannes Berg62ae67b2009-11-18 18:42:05 +01001067 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1068 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +02001069}
1070
Johannes Berg965beda2009-04-16 13:17:24 +02001071void ieee80211_send_nullfunc(struct ieee80211_local *local,
1072 struct ieee80211_sub_if_data *sdata,
Johannes Berg076cdcb2015-09-24 16:14:55 +02001073 bool powersave)
Johannes Berg965beda2009-04-16 13:17:24 +02001074{
1075 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +02001076 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301077 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +02001078
Ben Caradoc-Davies7c181f42018-03-19 12:57:44 +13001079 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1080 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
Kalle Valod8cd1892010-01-05 20:16:26 +02001081 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +02001082 return;
1083
Kalle Valod8cd1892010-01-05 20:16:26 +02001084 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +02001085 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +02001086 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +02001087
Seth Forshee6c17b772013-02-11 11:21:07 -06001088 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1089 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +02001090
Johannes Berg30686bf2015-06-02 21:39:54 +02001091 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Pontus Fuchsff40b422013-06-04 12:44:52 +02001092 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1093
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001094 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301095 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1096
Johannes Berg62ae67b2009-11-18 18:42:05 +01001097 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +02001098}
1099
Felix Fietkaua5d3cbd2021-07-02 07:01:11 +02001100void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1101 struct ieee80211_sub_if_data *sdata)
Felix Fietkaud5242152010-01-08 18:06:26 +01001102{
1103 struct sk_buff *skb;
1104 struct ieee80211_hdr *nullfunc;
1105 __le16 fc;
1106
1107 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1108 return;
1109
1110 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -07001111 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +01001112 return;
Joe Perchesd15b8452011-08-29 14:17:31 -07001113
Felix Fietkaud5242152010-01-08 18:06:26 +01001114 skb_reserve(skb, local->hw.extra_tx_headroom);
1115
Johannes Bergb080db52017-06-16 14:29:19 +02001116 nullfunc = skb_put_zero(skb, 30);
Felix Fietkaud5242152010-01-08 18:06:26 +01001117 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1118 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1119 nullfunc->frame_control = fc;
1120 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1121 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1122 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1123 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1124
1125 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1126 ieee80211_tx_skb(sdata, skb);
1127}
1128
Johannes Bergcc32abd2009-05-15 11:52:31 +02001129/* spectrum management related things */
1130static void ieee80211_chswitch_work(struct work_struct *work)
1131{
1132 struct ieee80211_sub_if_data *sdata =
1133 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +01001134 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001135 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001136 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001137
Johannes Berg9607e6b662009-12-23 13:15:31 +01001138 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +02001139 return;
1140
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001141 sdata_lock(sdata);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001142 mutex_lock(&local->mtx);
1143 mutex_lock(&local->chanctx_mtx);
1144
Johannes Berg77fdaa12009-07-07 03:45:17 +02001145 if (!ifmgd->associated)
1146 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001147
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001148 if (!sdata->vif.csa_active)
1149 goto out;
1150
1151 /*
1152 * using reservation isn't immediate as it may be deferred until later
1153 * with multi-vif. once reservation is complete it will re-schedule the
1154 * work with no reserved_chanctx so verify chandef to check if it
1155 * completed successfully
1156 */
1157
1158 if (sdata->reserved_chanctx) {
1159 /*
1160 * with multi-vif csa driver may call ieee80211_csa_finish()
1161 * many times while waiting for other interfaces to use their
1162 * reservations
1163 */
1164 if (sdata->reserved_ready)
1165 goto out;
1166
1167 ret = ieee80211_vif_use_reserved_context(sdata);
1168 if (ret) {
1169 sdata_info(sdata,
1170 "failed to use reserved channel context, disconnecting (err=%d)\n",
1171 ret);
1172 ieee80211_queue_work(&sdata->local->hw,
1173 &ifmgd->csa_connection_drop_work);
1174 goto out;
1175 }
1176
1177 goto out;
1178 }
1179
1180 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1181 &sdata->csa_chandef)) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001182 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001183 "failed to finalize channel switch, disconnecting\n");
Arik Nemtsov7578d572013-09-01 17:15:51 +03001184 ieee80211_queue_work(&sdata->local->hw,
1185 &ifmgd->csa_connection_drop_work);
1186 goto out;
1187 }
Karl Beldan675a0b02013-03-25 16:26:57 +01001188
Luciano Coelho0c21e632014-10-08 09:48:39 +03001189 ifmgd->csa_waiting_bcn = true;
Luciano Coelhof1d65582014-10-08 09:48:38 +03001190
Michal Kaziore5593f52014-04-09 15:45:36 +02001191 ieee80211_sta_reset_beacon_monitor(sdata);
1192 ieee80211_sta_reset_conn_monitor(sdata);
1193
Michal Kazior59af6922014-04-09 15:10:59 +02001194out:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001195 mutex_unlock(&local->chanctx_mtx);
1196 mutex_unlock(&local->mtx);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001197 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001198}
1199
Luciano Coelho0c21e632014-10-08 09:48:39 +03001200static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1201{
1202 struct ieee80211_local *local = sdata->local;
1203 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1204 int ret;
1205
1206 sdata_assert_lock(sdata);
1207
1208 WARN_ON(!sdata->vif.csa_active);
1209
1210 if (sdata->csa_block_tx) {
1211 ieee80211_wake_vif_queues(local, sdata,
1212 IEEE80211_QUEUE_STOP_REASON_CSA);
1213 sdata->csa_block_tx = false;
1214 }
1215
1216 sdata->vif.csa_active = false;
1217 ifmgd->csa_waiting_bcn = false;
Emmanuel Grumbachd6843d12021-04-08 14:31:25 +02001218 /*
1219 * If the CSA IE is still present on the beacon after the switch,
1220 * we need to consider it as a new CSA (possibly to self).
1221 */
1222 ifmgd->beacon_crc_valid = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03001223
1224 ret = drv_post_channel_switch(sdata);
1225 if (ret) {
1226 sdata_info(sdata,
1227 "driver post channel switch failed, disconnecting\n");
1228 ieee80211_queue_work(&local->hw,
1229 &ifmgd->csa_connection_drop_work);
1230 return;
1231 }
Luciano Coelho688b1ec2014-12-16 16:01:39 +02001232
1233 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
Luciano Coelho0c21e632014-10-08 09:48:39 +03001234}
1235
Johannes Berg5ce6e432010-05-11 16:20:57 +02001236void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1237{
Johannes Berg55de9082012-07-26 17:24:39 +02001238 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1239 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001240
1241 trace_api_chswitch_done(sdata, success);
1242 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001243 sdata_info(sdata,
1244 "driver channel switch failed, disconnecting\n");
1245 ieee80211_queue_work(&sdata->local->hw,
1246 &ifmgd->csa_connection_drop_work);
1247 } else {
1248 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001249 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001250}
1251EXPORT_SYMBOL(ieee80211_chswitch_done);
1252
Kees Cook34f11cd2017-10-16 16:35:49 -07001253static void ieee80211_chswitch_timer(struct timer_list *t)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001254{
1255 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07001256 from_timer(sdata, t, u.mgd.chswitch_timer);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001257
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001258 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001259}
1260
Johannes Berg37799e52013-03-26 14:02:26 +01001261static void
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001262ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1263{
1264 struct ieee80211_local *local = sdata->local;
1265
1266 if (!local->ops->abort_channel_switch)
1267 return;
1268
1269 mutex_lock(&local->mtx);
1270
1271 mutex_lock(&local->chanctx_mtx);
1272 ieee80211_vif_unreserve_chanctx(sdata);
1273 mutex_unlock(&local->chanctx_mtx);
1274
1275 if (sdata->csa_block_tx)
1276 ieee80211_wake_vif_queues(local, sdata,
1277 IEEE80211_QUEUE_STOP_REASON_CSA);
1278
1279 sdata->csa_block_tx = false;
1280 sdata->vif.csa_active = false;
1281
1282 mutex_unlock(&local->mtx);
1283
1284 drv_abort_channel_switch(sdata);
1285}
1286
1287static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001288ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03001289 u64 timestamp, u32 device_timestamp,
1290 struct ieee802_11_elems *elems,
Johannes Berg04a161f2013-05-03 09:35:35 +02001291 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001292{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001293 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001294 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001295 struct cfg80211_bss *cbss = ifmgd->associated;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001296 struct ieee80211_chanctx_conf *conf;
Johannes Berg55de9082012-07-26 17:24:39 +02001297 struct ieee80211_chanctx *chanctx;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001298 enum nl80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001299 struct ieee80211_csa_ie csa_ie;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001300 struct ieee80211_channel_switch ch_switch;
Johannes Berg2a333a02020-05-28 21:34:35 +02001301 struct ieee80211_bss *bss;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001302 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001303
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001304 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001305
Johannes Berg37799e52013-03-26 14:02:26 +01001306 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001307 return;
1308
Johannes Bergb4f286a12013-03-26 14:13:58 +01001309 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001310 return;
1311
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001312 current_band = cbss->channel->band;
Johannes Berg2a333a02020-05-28 21:34:35 +02001313 bss = (void *)cbss->priv;
Luciano Coelho84469a42014-10-28 13:33:04 +02001314 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
Johannes Berg2a333a02020-05-28 21:34:35 +02001315 bss->vht_cap_info,
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001316 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001317 ifmgd->associated->bssid, &csa_ie);
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001318
1319 if (!res) {
1320 ch_switch.timestamp = timestamp;
1321 ch_switch.device_timestamp = device_timestamp;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001322 ch_switch.block_tx = csa_ie.mode;
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001323 ch_switch.chandef = csa_ie.chandef;
1324 ch_switch.count = csa_ie.count;
1325 ch_switch.delay = csa_ie.max_switch_time;
1326 }
1327
Emmanuel Grumbach253907a2021-04-09 12:40:16 +03001328 if (res < 0)
1329 goto lock_and_drop_connection;
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001330
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001331 if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1332 if (res)
1333 ieee80211_sta_abort_chanswitch(sdata);
1334 else
1335 drv_channel_switch_rx_beacon(sdata, &ch_switch);
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001336 return;
1337 } else if (sdata->vif.csa_active || res) {
1338 /* disregard subsequent announcements if already processing */
1339 return;
1340 }
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001341
Johannes Berg36609442020-11-29 17:30:54 +02001342 if (sdata->vif.bss_conf.chandef.chan->band !=
1343 csa_ie.chandef.chan->band) {
1344 sdata_info(sdata,
1345 "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1346 ifmgd->associated->bssid,
1347 csa_ie.chandef.chan->center_freq,
1348 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1349 csa_ie.chandef.center_freq2);
1350 goto lock_and_drop_connection;
1351 }
1352
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001353 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001354 IEEE80211_CHAN_DISABLED)) {
1355 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001356 "AP %pM switches to unsupported channel "
1357 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1358 "disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001359 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001360 csa_ie.chandef.chan->center_freq,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001361 csa_ie.chandef.chan->freq_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001362 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001363 csa_ie.chandef.freq1_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001364 csa_ie.chandef.center_freq2);
Johannes Berg36609442020-11-29 17:30:54 +02001365 goto lock_and_drop_connection;
Johannes Berg85220d72013-03-25 18:29:27 +01001366 }
1367
Johannes Bergf84eaa12015-03-12 08:53:26 +02001368 if (cfg80211_chandef_identical(&csa_ie.chandef,
Sara Sharon97928752019-02-06 13:17:16 +02001369 &sdata->vif.bss_conf.chandef) &&
1370 (!csa_ie.mode || !beacon)) {
Johannes Bergf84eaa12015-03-12 08:53:26 +02001371 if (ifmgd->csa_ignored_same_chan)
1372 return;
1373 sdata_info(sdata,
1374 "AP %pM tries to chanswitch to same channel, ignore\n",
1375 ifmgd->associated->bssid);
1376 ifmgd->csa_ignored_same_chan = true;
1377 return;
1378 }
1379
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001380 /*
1381 * Drop all TDLS peers - either we disconnect or move to a different
1382 * channel from this point on. There's no telling what our peer will do.
1383 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1384 * have an incompatible wider chandef.
1385 */
1386 ieee80211_teardown_tdls_peers(sdata);
1387
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001388 mutex_lock(&local->mtx);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001389 mutex_lock(&local->chanctx_mtx);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001390 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1391 lockdep_is_held(&local->chanctx_mtx));
1392 if (!conf) {
1393 sdata_info(sdata,
1394 "no channel context assigned to vif?, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001395 goto drop_connection;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001396 }
1397
1398 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1399
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001400 if (local->use_chanctx &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001401 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001402 sdata_info(sdata,
1403 "driver doesn't support chan-switch with channel contexts\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001404 goto drop_connection;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001405 }
1406
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001407 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1408 sdata_info(sdata,
1409 "preparing for channel switch failed, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001410 goto drop_connection;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001411 }
1412
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001413 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1414 chanctx->mode, false);
1415 if (res) {
Johannes Berg55de9082012-07-26 17:24:39 +02001416 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001417 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1418 res);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001419 goto drop_connection;
Johannes Berg55de9082012-07-26 17:24:39 +02001420 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001421 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001422
Michal Kazior59af6922014-04-09 15:10:59 +02001423 sdata->vif.csa_active = true;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001424 sdata->csa_chandef = csa_ie.chandef;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001425 sdata->csa_block_tx = csa_ie.mode;
Johannes Bergf84eaa12015-03-12 08:53:26 +02001426 ifmgd->csa_ignored_same_chan = false;
Emmanuel Grumbach189a1642020-12-06 14:54:49 +02001427 ifmgd->beacon_crc_valid = false;
Michal Kazior59af6922014-04-09 15:10:59 +02001428
1429 if (sdata->csa_block_tx)
Luciano Coelhoa46992b2014-06-13 16:30:07 +03001430 ieee80211_stop_vif_queues(local, sdata,
1431 IEEE80211_QUEUE_STOP_REASON_CSA);
Michal Kazior59af6922014-04-09 15:10:59 +02001432 mutex_unlock(&local->mtx);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001433
Luciano Coelho2f457292014-11-07 14:31:36 +02001434 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
Johannes Berg669b8412020-11-29 17:30:55 +02001435 csa_ie.count, csa_ie.mode);
Luciano Coelho2f457292014-11-07 14:31:36 +02001436
Johannes Bergb4f286a12013-03-26 14:13:58 +01001437 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001438 /* use driver's channel switch callback */
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001439 drv_channel_switch(local, sdata, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001440 return;
1441 }
1442
1443 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001444 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001445 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001446 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001447 mod_timer(&ifmgd->chswitch_timer,
Luciano Coelhoff1e4172014-10-28 13:33:05 +02001448 TU_TO_EXP_TIME((csa_ie.count - 1) *
1449 cbss->beacon_interval));
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001450 return;
Johannes Berg36609442020-11-29 17:30:54 +02001451 lock_and_drop_connection:
1452 mutex_lock(&local->mtx);
1453 mutex_lock(&local->chanctx_mtx);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001454 drop_connection:
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001455 /*
1456 * This is just so that the disconnect flow will know that
1457 * we were trying to switch channel and failed. In case the
1458 * mode is 1 (we are not allowed to Tx), we will know not to
1459 * send a deauthentication frame. Those two fields will be
1460 * reset when the disconnection worker runs.
1461 */
1462 sdata->vif.csa_active = true;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001463 sdata->csa_block_tx = csa_ie.mode;
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001464
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001465 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1466 mutex_unlock(&local->chanctx_mtx);
1467 mutex_unlock(&local->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001468}
1469
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001470static bool
1471ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1472 struct ieee80211_channel *channel,
1473 const u8 *country_ie, u8 country_ie_len,
1474 const u8 *pwr_constr_elem,
1475 int *chan_pwr, int *pwr_reduction)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001476{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001477 struct ieee80211_country_ie_triplet *triplet;
1478 int chan = ieee80211_frequency_to_channel(channel->center_freq);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001479 int i, chan_increment;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001480 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001481
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001482 /* Invalid IE */
1483 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001484 return false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001485
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001486 triplet = (void *)(country_ie + 3);
1487 country_ie_len -= 3;
1488
1489 switch (channel->band) {
1490 default:
1491 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001492 fallthrough;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001493 case NL80211_BAND_2GHZ:
1494 case NL80211_BAND_60GHZ:
Srinivasan Raju63fa0422021-10-18 11:00:54 +01001495 case NL80211_BAND_LC:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001496 chan_increment = 1;
1497 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001498 case NL80211_BAND_5GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001499 chan_increment = 4;
1500 break;
Johannes Berg2dedfe12020-12-06 14:54:47 +02001501 case NL80211_BAND_6GHZ:
1502 /*
1503 * In the 6 GHz band, the "maximum transmit power level"
1504 * field in the triplets is reserved, and thus will be
1505 * zero and we shouldn't use it to control TX power.
1506 * The actual TX power will be given in the transmit
1507 * power envelope element instead.
1508 */
1509 return false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001510 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001511
1512 /* find channel */
1513 while (country_ie_len >= 3) {
1514 u8 first_channel = triplet->chans.first_channel;
1515
1516 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1517 goto next;
1518
1519 for (i = 0; i < triplet->chans.num_channels; i++) {
1520 if (first_channel + i * chan_increment == chan) {
1521 have_chan_pwr = true;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001522 *chan_pwr = triplet->chans.max_power;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001523 break;
1524 }
1525 }
1526 if (have_chan_pwr)
1527 break;
1528
1529 next:
1530 triplet++;
1531 country_ie_len -= 3;
1532 }
1533
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001534 if (have_chan_pwr && pwr_constr_elem)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001535 *pwr_reduction = *pwr_constr_elem;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001536 else
1537 *pwr_reduction = 0;
1538
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001539 return have_chan_pwr;
1540}
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001541
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001542static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1543 struct ieee80211_channel *channel,
1544 const u8 *cisco_dtpc_ie,
1545 int *pwr_level)
1546{
1547 /* From practical testing, the first data byte of the DTPC element
1548 * seems to contain the requested dBm level, and the CLI on Cisco
1549 * APs clearly state the range is -127 to 127 dBm, which indicates
1550 * a signed byte, although it seemingly never actually goes negative.
1551 * The other byte seems to always be zero.
1552 */
1553 *pwr_level = (__s8)cisco_dtpc_ie[4];
1554}
1555
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001556static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1557 struct ieee80211_channel *channel,
1558 struct ieee80211_mgmt *mgmt,
1559 const u8 *country_ie, u8 country_ie_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001560 const u8 *pwr_constr_ie,
1561 const u8 *cisco_dtpc_ie)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001562{
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001563 bool has_80211h_pwr = false, has_cisco_pwr = false;
1564 int chan_pwr = 0, pwr_reduction_80211h = 0;
1565 int pwr_level_cisco, pwr_level_80211h;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001566 int new_ap_level;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001567 __le16 capab = mgmt->u.probe_resp.capab_info;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001568
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001569 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
1570 return 0; /* TODO */
1571
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001572 if (country_ie &&
1573 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1574 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001575 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1576 sdata, channel, country_ie, country_ie_len,
1577 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001578 pwr_level_80211h =
1579 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001580 }
1581
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001582 if (cisco_dtpc_ie) {
1583 ieee80211_find_cisco_dtpc(
1584 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1585 has_cisco_pwr = true;
1586 }
1587
1588 if (!has_80211h_pwr && !has_cisco_pwr)
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001589 return 0;
1590
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001591 /* If we have both 802.11h and Cisco DTPC, apply both limits
1592 * by picking the smallest of the two power levels advertised.
1593 */
1594 if (has_80211h_pwr &&
1595 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
Johannes Berga87da0c2015-12-08 16:04:37 +02001596 new_ap_level = pwr_level_80211h;
1597
1598 if (sdata->ap_power_level == new_ap_level)
1599 return 0;
1600
John Linvillecef2fc12015-03-31 10:49:14 -04001601 sdata_dbg(sdata,
1602 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1603 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1604 sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001605 } else { /* has_cisco_pwr is always true here. */
Johannes Berga87da0c2015-12-08 16:04:37 +02001606 new_ap_level = pwr_level_cisco;
1607
1608 if (sdata->ap_power_level == new_ap_level)
1609 return 0;
1610
John Linvillecef2fc12015-03-31 10:49:14 -04001611 sdata_dbg(sdata,
1612 "Limiting TX power to %d dBm as advertised by %pM\n",
1613 pwr_level_cisco, sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001614 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001615
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001616 sdata->ap_power_level = new_ap_level;
1617 if (__ieee80211_recalc_txpower(sdata))
1618 return BSS_CHANGED_TXPOWER;
1619 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001620}
1621
Johannes Berg965beda2009-04-16 13:17:24 +02001622/* powersave */
1623static void ieee80211_enable_ps(struct ieee80211_local *local,
1624 struct ieee80211_sub_if_data *sdata)
1625{
1626 struct ieee80211_conf *conf = &local->hw.conf;
1627
Johannes Bergd5edaed2009-04-22 23:02:51 +02001628 /*
1629 * If we are scanning right now then the parameters will
1630 * take effect when scan finishes.
1631 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001632 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001633 return;
1634
Johannes Berg965beda2009-04-16 13:17:24 +02001635 if (conf->dynamic_ps_timeout > 0 &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001636 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001637 mod_timer(&local->dynamic_ps_timer, jiffies +
1638 msecs_to_jiffies(conf->dynamic_ps_timeout));
1639 } else {
Johannes Berg30686bf2015-06-02 21:39:54 +02001640 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
Johannes Berg076cdcb2015-09-24 16:14:55 +02001641 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301642
Johannes Berg30686bf2015-06-02 21:39:54 +02001643 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1644 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001645 return;
1646
1647 conf->flags |= IEEE80211_CONF_PS;
1648 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001649 }
1650}
1651
1652static void ieee80211_change_ps(struct ieee80211_local *local)
1653{
1654 struct ieee80211_conf *conf = &local->hw.conf;
1655
1656 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001657 ieee80211_enable_ps(local, local->ps_sdata);
1658 } else if (conf->flags & IEEE80211_CONF_PS) {
1659 conf->flags &= ~IEEE80211_CONF_PS;
1660 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1661 del_timer_sync(&local->dynamic_ps_timer);
1662 cancel_work_sync(&local->dynamic_ps_enable_work);
1663 }
1664}
1665
Jason Young808118c2011-03-10 16:43:19 -08001666static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1667{
1668 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1669 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001670 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001671
1672 if (!mgd->powersave)
1673 return false;
1674
Johannes Berg05cb9102011-10-28 11:59:47 +02001675 if (mgd->broken_ap)
1676 return false;
1677
Jason Young808118c2011-03-10 16:43:19 -08001678 if (!mgd->associated)
1679 return false;
1680
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001681 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001682 return false;
1683
Alexander Bondar989c6502013-05-16 17:34:17 +03001684 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001685 return false;
1686
Jason Young808118c2011-03-10 16:43:19 -08001687 rcu_read_lock();
1688 sta = sta_info_get(sdata, mgd->bssid);
1689 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001690 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001691 rcu_read_unlock();
1692
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001693 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001694}
1695
Johannes Berg965beda2009-04-16 13:17:24 +02001696/* need to hold RTNL or interface lock */
Johannes Berg4a733ef2015-10-14 18:02:43 +02001697void ieee80211_recalc_ps(struct ieee80211_local *local)
Johannes Berg965beda2009-04-16 13:17:24 +02001698{
1699 struct ieee80211_sub_if_data *sdata, *found = NULL;
1700 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001701 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001702
Johannes Berg30686bf2015-06-02 21:39:54 +02001703 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001704 local->ps_sdata = NULL;
1705 return;
1706 }
1707
1708 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001709 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001710 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301711 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1712 /* If an AP vif is found, then disable PS
1713 * by setting the count to zero thereby setting
1714 * ps_sdata to NULL.
1715 */
1716 count = 0;
1717 break;
1718 }
Johannes Berg965beda2009-04-16 13:17:24 +02001719 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1720 continue;
1721 found = sdata;
1722 count++;
1723 }
1724
Jason Young808118c2011-03-10 16:43:19 -08001725 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg4a733ef2015-10-14 18:02:43 +02001726 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg10f644a2009-04-16 13:17:25 +02001727
Juuso Oikarinenff616382010-06-09 09:51:52 +03001728 timeout = local->dynamic_ps_forced_timeout;
Johannes Berg4a733ef2015-10-14 18:02:43 +02001729 if (timeout < 0)
1730 timeout = 100;
Johannes Berg09b85562013-02-06 23:07:41 +01001731 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001732
Johannes Berg4a733ef2015-10-14 18:02:43 +02001733 /* If the TIM IE is invalid, pretend the value is 1 */
1734 if (!dtimper)
1735 dtimper = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001736
Johannes Berg4a733ef2015-10-14 18:02:43 +02001737 local->hw.conf.ps_dtim_period = dtimper;
1738 local->ps_sdata = found;
Johannes Berg10f644a2009-04-16 13:17:25 +02001739 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001740 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001741 }
Johannes Berg965beda2009-04-16 13:17:24 +02001742
1743 ieee80211_change_ps(local);
1744}
1745
Eliad Pellerab095872012-07-27 12:33:22 +03001746void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1747{
1748 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1749
1750 if (sdata->vif.bss_conf.ps != ps_allowed) {
1751 sdata->vif.bss_conf.ps = ps_allowed;
1752 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1753 }
1754}
1755
Johannes Berg965beda2009-04-16 13:17:24 +02001756void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1757{
1758 struct ieee80211_local *local =
1759 container_of(work, struct ieee80211_local,
1760 dynamic_ps_disable_work);
1761
1762 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1763 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1764 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1765 }
1766
1767 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001768 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +03001769 IEEE80211_QUEUE_STOP_REASON_PS,
1770 false);
Johannes Berg965beda2009-04-16 13:17:24 +02001771}
1772
1773void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1774{
1775 struct ieee80211_local *local =
1776 container_of(work, struct ieee80211_local,
1777 dynamic_ps_enable_work);
1778 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001779 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301780 unsigned long flags;
1781 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001782
1783 /* can only happen when PS was just disabled anyway */
1784 if (!sdata)
1785 return;
1786
Christian Lamparter5e340692011-06-30 21:08:43 +02001787 ifmgd = &sdata->u.mgd;
1788
Johannes Berg965beda2009-04-16 13:17:24 +02001789 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1790 return;
1791
Johannes Berg09b85562013-02-06 23:07:41 +01001792 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001793 /* don't enter PS if TX frames are pending */
1794 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301795 mod_timer(&local->dynamic_ps_timer, jiffies +
1796 msecs_to_jiffies(
1797 local->hw.conf.dynamic_ps_timeout));
1798 return;
1799 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001800
1801 /*
1802 * transmission can be stopped by others which leads to
1803 * dynamic_ps_timer expiry. Postpone the ps timer if it
1804 * is not the actual idle state.
1805 */
1806 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1807 for (q = 0; q < local->hw.queues; q++) {
1808 if (local->queue_stop_reasons[q]) {
1809 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1810 flags);
1811 mod_timer(&local->dynamic_ps_timer, jiffies +
1812 msecs_to_jiffies(
1813 local->hw.conf.dynamic_ps_timeout));
1814 return;
1815 }
1816 }
1817 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301818 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301819
Johannes Berg30686bf2015-06-02 21:39:54 +02001820 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301821 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001822 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301823 mod_timer(&local->dynamic_ps_timer, jiffies +
1824 msecs_to_jiffies(
1825 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001826 } else {
Johannes Berg076cdcb2015-09-24 16:14:55 +02001827 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301828 /* Flush to get the tx status of nullfunc frame */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001829 ieee80211_flush_queues(local, sdata, false);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301830 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301831 }
1832
Johannes Berg30686bf2015-06-02 21:39:54 +02001833 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1834 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301835 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1836 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1837 local->hw.conf.flags |= IEEE80211_CONF_PS;
1838 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1839 }
Johannes Berg965beda2009-04-16 13:17:24 +02001840}
1841
Kees Cook34f11cd2017-10-16 16:35:49 -07001842void ieee80211_dynamic_ps_timer(struct timer_list *t)
Johannes Berg965beda2009-04-16 13:17:24 +02001843{
Kees Cook34f11cd2017-10-16 16:35:49 -07001844 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
Johannes Berg965beda2009-04-16 13:17:24 +02001845
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001846 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001847}
1848
Simon Wunderlich164eb022013-02-08 18:16:20 +01001849void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1850{
Geliang Tanga85a7e22016-01-01 23:48:52 +08001851 struct delayed_work *delayed_work = to_delayed_work(work);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001852 struct ieee80211_sub_if_data *sdata =
1853 container_of(delayed_work, struct ieee80211_sub_if_data,
1854 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001855 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001856
Johannes Berg34a37402013-12-18 09:43:33 +01001857 mutex_lock(&sdata->local->mtx);
1858 if (sdata->wdev.cac_started) {
1859 ieee80211_vif_release_channel(sdata);
1860 cfg80211_cac_event(sdata->dev, &chandef,
1861 NL80211_RADAR_CAC_FINISHED,
1862 GFP_KERNEL);
1863 }
1864 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001865}
1866
Johannes Berg02219b32014-10-07 10:38:50 +03001867static bool
1868__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1869{
1870 struct ieee80211_local *local = sdata->local;
1871 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
John Linvillecc72f6e2015-01-06 14:39:33 -05001872 bool ret = false;
Johannes Berg02219b32014-10-07 10:38:50 +03001873 int ac;
1874
1875 if (local->hw.queues < IEEE80211_NUM_ACS)
1876 return false;
1877
1878 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1879 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1880 int non_acm_ac;
1881 unsigned long now = jiffies;
1882
1883 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1884 tx_tspec->admitted_time &&
1885 time_after(now, tx_tspec->time_slice_start + HZ)) {
1886 tx_tspec->consumed_tx_time = 0;
1887 tx_tspec->time_slice_start = now;
1888
1889 if (tx_tspec->downgraded)
1890 tx_tspec->action =
1891 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1892 }
1893
1894 switch (tx_tspec->action) {
1895 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1896 /* take the original parameters */
1897 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1898 sdata_err(sdata,
1899 "failed to set TX queue parameters for queue %d\n",
1900 ac);
1901 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1902 tx_tspec->downgraded = false;
1903 ret = true;
1904 break;
1905 case TX_TSPEC_ACTION_DOWNGRADE:
1906 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1907 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1908 ret = true;
1909 break;
1910 }
1911 /* downgrade next lower non-ACM AC */
1912 for (non_acm_ac = ac + 1;
1913 non_acm_ac < IEEE80211_NUM_ACS;
1914 non_acm_ac++)
1915 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1916 break;
Johannes Berg93db1d92016-09-14 09:23:51 +02001917 /* Usually the loop will result in using BK even if it
1918 * requires admission control, but such a configuration
1919 * makes no sense and we have to transmit somehow - the
1920 * AC selection does the same thing.
1921 * If we started out trying to downgrade from BK, then
1922 * the extra condition here might be needed.
Johannes Berg02219b32014-10-07 10:38:50 +03001923 */
Johannes Berg93db1d92016-09-14 09:23:51 +02001924 if (non_acm_ac >= IEEE80211_NUM_ACS)
1925 non_acm_ac = IEEE80211_AC_BK;
Johannes Berg02219b32014-10-07 10:38:50 +03001926 if (drv_conf_tx(local, sdata, ac,
1927 &sdata->tx_conf[non_acm_ac]))
1928 sdata_err(sdata,
1929 "failed to set TX queue parameters for queue %d\n",
1930 ac);
1931 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1932 ret = true;
1933 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1934 tx_tspec->time_slice_start + HZ - now + 1);
1935 break;
1936 case TX_TSPEC_ACTION_NONE:
1937 /* nothing now */
1938 break;
1939 }
1940 }
1941
1942 return ret;
1943}
1944
1945void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1946{
1947 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1948 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1949}
1950
1951static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1952{
1953 struct ieee80211_sub_if_data *sdata;
1954
1955 sdata = container_of(work, struct ieee80211_sub_if_data,
1956 u.mgd.tx_tspec_wk.work);
1957 ieee80211_sta_handle_tspec_ac_params(sdata);
1958}
1959
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001960/* MLME */
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001961static bool
1962ieee80211_sta_wmm_params(struct ieee80211_local *local,
1963 struct ieee80211_sub_if_data *sdata,
1964 const u8 *wmm_param, size_t wmm_param_len,
1965 const struct ieee80211_mu_edca_param_set *mu_edca)
Jiri Bencf0706e82007-05-05 11:45:53 -07001966{
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001967 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
Johannes Berg4ced3f72010-07-19 16:39:04 +02001968 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001969 size_t left;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001970 int count, mu_edca_count, ac;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001971 const u8 *pos;
1972 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001973
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001974 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001975 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001976
Johannes Berg32c50572012-03-28 11:04:29 +02001977 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001978 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001979
1980 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001981 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001982
Jiri Bencf0706e82007-05-05 11:45:53 -07001983 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001984 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001985
1986 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001987 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001988
Jiri Bencf0706e82007-05-05 11:45:53 -07001989 count = wmm_param[6] & 0x0f;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001990 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
1991 * if mu_edca was preset before and now it disappeared tell
1992 * the driver about it.
1993 */
1994 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
1995 if (count == ifmgd->wmm_last_param_set &&
1996 mu_edca_count == ifmgd->mu_edca_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001997 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001998 ifmgd->wmm_last_param_set = count;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001999 ifmgd->mu_edca_last_param_set = mu_edca_count;
Jiri Bencf0706e82007-05-05 11:45:53 -07002000
2001 pos = wmm_param + 8;
2002 left = wmm_param_len - 8;
2003
2004 memset(&params, 0, sizeof(params));
2005
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002006 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002007 for (; left >= 4; left -= 4, pos += 4) {
2008 int aci = (pos[0] >> 5) & 0x03;
2009 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02002010 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002011
2012 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002013 case 1: /* AC_BK */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002014 ac = IEEE80211_AC_BK;
Johannes Berg988c0f72008-04-17 19:21:22 +02002015 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002016 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02002017 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2018 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002019 params[ac].mu_edca = !!mu_edca;
2020 if (mu_edca)
2021 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
Jiri Bencf0706e82007-05-05 11:45:53 -07002022 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002023 case 2: /* AC_VI */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002024 ac = IEEE80211_AC_VI;
Johannes Berg988c0f72008-04-17 19:21:22 +02002025 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002026 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02002027 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2028 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002029 params[ac].mu_edca = !!mu_edca;
2030 if (mu_edca)
2031 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
Jiri Bencf0706e82007-05-05 11:45:53 -07002032 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002033 case 3: /* AC_VO */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002034 ac = IEEE80211_AC_VO;
Johannes Berg988c0f72008-04-17 19:21:22 +02002035 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002036 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02002037 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2038 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002039 params[ac].mu_edca = !!mu_edca;
2040 if (mu_edca)
2041 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
Jiri Bencf0706e82007-05-05 11:45:53 -07002042 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002043 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07002044 default:
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002045 ac = IEEE80211_AC_BE;
Johannes Berg988c0f72008-04-17 19:21:22 +02002046 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002047 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02002048 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2049 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002050 params[ac].mu_edca = !!mu_edca;
2051 if (mu_edca)
2052 params[ac].mu_edca_param_rec = mu_edca->ac_be;
Jiri Bencf0706e82007-05-05 11:45:53 -07002053 break;
2054 }
2055
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002056 params[ac].aifs = pos[0] & 0x0f;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002057
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002058 if (params[ac].aifs < 2) {
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002059 sdata_info(sdata,
2060 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002061 params[ac].aifs, aci);
2062 params[ac].aifs = 2;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002063 }
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002064 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2065 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2066 params[ac].txop = get_unaligned_le16(pos + 2);
2067 params[ac].acm = acm;
2068 params[ac].uapsd = uapsd;
Kalle Valoab133152010-01-12 10:42:31 +02002069
Ilan Peer911a2642018-04-03 11:35:22 +03002070 if (params[ac].cw_min == 0 ||
Emmanuel Grumbachc470bdc2018-03-26 16:21:04 +03002071 params[ac].cw_min > params[ac].cw_max) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002072 sdata_info(sdata,
2073 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2074 params[ac].cw_min, params[ac].cw_max, aci);
2075 return false;
2076 }
Haim Dreyfusse552af02018-03-28 13:24:10 +03002077 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002078 }
2079
Brian Norris05aaa5c2019-07-26 15:47:58 -07002080 /* WMM specification requires all 4 ACIs. */
2081 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2082 if (params[ac].cw_min == 0) {
2083 sdata_info(sdata,
2084 "AP has invalid WMM params (missing AC %d), using defaults\n",
2085 ac);
2086 return false;
2087 }
2088 }
2089
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002090 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002091 mlme_dbg(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002092 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2093 ac, params[ac].acm,
2094 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2095 params[ac].txop, params[ac].uapsd,
2096 ifmgd->tx_tspec[ac].downgraded);
2097 sdata->tx_conf[ac] = params[ac];
2098 if (!ifmgd->tx_tspec[ac].downgraded &&
2099 drv_conf_tx(local, sdata, ac, &params[ac]))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002100 sdata_err(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002101 "failed to set TX queue parameters for AC %d\n",
2102 ac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002103 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002104
2105 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02002106 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02002107 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002108}
2109
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002110static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2111{
2112 lockdep_assert_held(&sdata->local->mtx);
2113
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002114 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002115 ieee80211_run_deferred_scan(sdata->local);
2116}
2117
2118static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2119{
2120 mutex_lock(&sdata->local->mtx);
2121 __ieee80211_stop_poll(sdata);
2122 mutex_unlock(&sdata->local->mtx);
2123}
2124
Johannes Berg7a5158e2008-10-08 10:59:33 +02002125static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2126 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02002127{
Johannes Bergbda39332008-10-11 01:51:51 +02002128 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302129 struct ieee80211_supported_band *sband;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002130 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002131 bool use_protection;
2132 bool use_short_preamble;
2133 bool use_short_slot;
2134
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302135 sband = ieee80211_get_sband(sdata);
2136 if (!sband)
2137 return changed;
2138
Johannes Berg7a5158e2008-10-08 10:59:33 +02002139 if (erp_valid) {
2140 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2141 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2142 } else {
2143 use_protection = false;
2144 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2145 }
2146
2147 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Tova Mussai07c12d62020-05-28 21:34:46 +02002148 if (sband->band == NL80211_BAND_5GHZ ||
2149 sband->band == NL80211_BAND_6GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01002150 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02002151
Johannes Berg471b3ef2007-12-28 14:32:58 +01002152 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01002153 bss_conf->use_cts_prot = use_protection;
2154 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02002155 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02002156
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002157 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002158 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002159 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02002160 }
Daniel Draked9430a32007-07-27 15:43:24 +02002161
Johannes Berg7a5158e2008-10-08 10:59:33 +02002162 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02002163 bss_conf->use_short_slot = use_short_slot;
2164 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04002165 }
2166
2167 return changed;
2168}
2169
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002170static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002171 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02002172 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07002173{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002174 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002175 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002176 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002177
Johannes Bergae5eb022008-10-14 16:58:37 +02002178 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002179 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03002180 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002181
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002182 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07002183 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002184
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002185 sdata->u.mgd.associated = cbss;
2186 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01002187
Johannes Berge8e4f522017-03-08 11:12:10 +01002188 ieee80211_check_rate_mask(sdata);
2189
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002190 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2191
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01002192 if (sdata->vif.p2p ||
2193 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002194 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002195
Johannes Berg9caf0362012-11-29 01:25:20 +01002196 rcu_read_lock();
2197 ies = rcu_dereference(cbss->ies);
2198 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002199 int ret;
2200
2201 ret = cfg80211_get_p2p_attr(
2202 ies->data, ies->len,
2203 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002204 (u8 *) &bss_conf->p2p_noa_attr,
2205 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01002206 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002207 sdata->u.mgd.p2p_noa_index =
2208 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01002209 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01002210 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01002211 }
Johannes Berg9caf0362012-11-29 01:25:20 +01002212 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01002213 }
2214
Johannes Bergb291ba12009-07-10 15:29:03 +02002215 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002216 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002217
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002218 ieee80211_led_assoc(local, 1);
2219
Alexander Bondar989c6502013-05-16 17:34:17 +03002220 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02002221 /*
2222 * If the AP is buggy we may get here with no DTIM period
2223 * known, so assume it's 1 which is the only safe assumption
2224 * in that case, although if the TIM IE is broken powersave
2225 * probably just won't work at all.
2226 */
2227 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03002228 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03002229 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02002230 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03002231 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02002232 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02002233 }
Johannes Berge5b900d2010-07-29 16:08:55 +02002234
Juuso Oikarinen68542962010-06-09 13:43:26 +03002235 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02002236
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002237 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01002238 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03002239 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002240 bss_info_changed |= BSS_CHANGED_CQM;
2241
Juuso Oikarinen68542962010-06-09 13:43:26 +03002242 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002243 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002244 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002245
Johannes Bergae5eb022008-10-14 16:58:37 +02002246 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03002247
Johannes Berg056508d2009-07-30 21:43:55 +02002248 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002249 ieee80211_recalc_ps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02002250 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02002251
Johannes Berg04ecd252012-09-11 14:34:12 +02002252 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03002253 ieee80211_recalc_ps_vif(sdata);
2254
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002255 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002256}
2257
Jouni Malinene69e95d2010-03-29 23:29:31 -07002258static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01002259 u16 stype, u16 reason, bool tx,
2260 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03002261{
Johannes Berg46900292009-02-15 12:44:28 +01002262 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002263 struct ieee80211_local *local = sdata->local;
Wen Gong63214f02021-09-24 06:00:52 -04002264 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg3cc52402012-03-09 13:12:35 +01002265 u32 changed = 0;
Johannes Berg15fae342021-06-18 13:41:55 +03002266 struct ieee80211_prep_tx_info info = {
2267 .subtype = stype,
2268 };
Tomas Winkleraa458d12008-09-09 00:32:12 +03002269
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002270 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002271
Johannes Berg37ad3882012-02-24 13:50:54 +01002272 if (WARN_ON_ONCE(tx && !frame_buf))
2273 return;
2274
Johannes Bergb291ba12009-07-10 15:29:03 +02002275 if (WARN_ON(!ifmgd->associated))
2276 return;
2277
David Spinadel79543d82012-06-12 09:59:45 +03002278 ieee80211_stop_poll(sdata);
2279
Johannes Berg77fdaa12009-07-07 03:45:17 +02002280 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002281 netif_carrier_off(sdata->dev);
2282
Eliad Peller88bc40e2012-07-12 17:35:33 +03002283 /*
2284 * if we want to get out of ps before disassoc (why?) we have
2285 * to do it before sending disassoc, as otherwise the null-packet
2286 * won't be valid.
2287 */
2288 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2289 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2290 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2291 }
2292 local->ps_sdata = NULL;
2293
Eliad Pellerab095872012-07-27 12:33:22 +03002294 /* disable per-vif ps */
2295 ieee80211_recalc_ps_vif(sdata);
2296
Emmanuel Grumbach14f2ae82015-01-22 23:30:19 +02002297 /* make sure ongoing transmission finishes */
2298 synchronize_net();
2299
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002300 /*
2301 * drop any frame before deauth/disassoc, this can be data or
2302 * management frame. Since we are disconnecting, we should not
2303 * insist sending these frames which can take time and delay
2304 * the disconnection and possible the roaming.
2305 */
Eliad Pellerf8239812012-06-27 14:18:22 +03002306 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002307 ieee80211_flush_queues(local, sdata, true);
Eliad Pellerf8239812012-06-27 14:18:22 +03002308
Johannes Berg37ad3882012-02-24 13:50:54 +01002309 /* deauthenticate/disassociate now */
Ilan Peer94ba9272018-02-19 14:48:41 +02002310 if (tx || frame_buf) {
Ilan Peer94ba9272018-02-19 14:48:41 +02002311 /*
2312 * In multi channel scenarios guarantee that the virtual
2313 * interface is granted immediate airtime to transmit the
2314 * deauthentication frame by calling mgd_prepare_tx, if the
2315 * driver requested so.
2316 */
2317 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
Johannes Berg15fae342021-06-18 13:41:55 +03002318 !ifmgd->have_beacon) {
2319 drv_mgd_prepare_tx(sdata->local, sdata, &info);
2320 }
Ilan Peer94ba9272018-02-19 14:48:41 +02002321
Johannes Berg4b08d1b2019-08-30 14:24:51 +03002322 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2323 ifmgd->bssid, stype, reason,
2324 tx, frame_buf);
Ilan Peer94ba9272018-02-19 14:48:41 +02002325 }
Johannes Berg37ad3882012-02-24 13:50:54 +01002326
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002327 /* flush out frame - make sure the deauth was actually sent */
Johannes Berg37ad3882012-02-24 13:50:54 +01002328 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002329 ieee80211_flush_queues(local, sdata, false);
Johannes Berg37ad3882012-02-24 13:50:54 +01002330
Johannes Berg15fae342021-06-18 13:41:55 +03002331 drv_mgd_complete_tx(sdata->local, sdata, &info);
2332
Eliad Peller88a9e312012-06-01 11:14:03 +03002333 /* clear bssid only after building the needed mgmt frames */
Joe Perchesc84a67a2015-03-02 19:54:57 -08002334 eth_zero_addr(ifmgd->bssid);
Eliad Peller88a9e312012-06-01 11:14:03 +03002335
Wen Gongb0140fd2020-12-07 11:36:34 +08002336 sdata->vif.bss_conf.ssid_len = 0;
2337
Johannes Berg37ad3882012-02-24 13:50:54 +01002338 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01002339 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01002340
2341 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002342 changed |= ieee80211_reset_erp_info(sdata);
2343
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002344 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02002345 changed |= BSS_CHANGED_ASSOC;
2346 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002347
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002348 ifmgd->p2p_noa_index = -1;
2349 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2350 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002351
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002352 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08002353 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2354 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002355 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2356 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002357
2358 /* reset MU-MIMO ownership and group data */
2359 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2360 sizeof(sdata->vif.bss_conf.mu_group.membership));
2361 memset(sdata->vif.bss_conf.mu_group.position, 0,
2362 sizeof(sdata->vif.bss_conf.mu_group.position));
2363 changed |= BSS_CHANGED_MU_GROUPS;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002364 sdata->vif.mu_mimo_owner = false;
Johannes Berg413ad502009-05-08 21:21:06 +02002365
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002366 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302367
Kalle Valo520eb822008-12-18 23:35:27 +02002368 del_timer_sync(&local->dynamic_ps_timer);
2369 cancel_work_sync(&local->dynamic_ps_enable_work);
2370
Juuso Oikarinen68542962010-06-09 13:43:26 +03002371 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002372 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002373 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002374
Johannes Berg3abead52012-03-02 15:56:59 +01002375 sdata->vif.bss_conf.qos = false;
2376 changed |= BSS_CHANGED_QOS;
2377
Johannes Berg0aaffa92010-05-05 15:28:27 +02002378 /* The BSSID (not really interesting) and HT changed */
2379 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02002380 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02002381
Johannes Berg3abead52012-03-02 15:56:59 +01002382 /* disassociated - set to defaults now */
Johannes Bergcec66282015-10-22 17:46:04 +02002383 ieee80211_set_wmm_default(sdata, false, false);
Johannes Berg3abead52012-03-02 15:56:59 +01002384
Johannes Bergb9dcf712010-08-27 12:35:54 +02002385 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2386 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2387 del_timer_sync(&sdata->u.mgd.timer);
2388 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02002389
Johannes Berg826262c2012-12-10 16:38:14 +02002390 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03002391 sdata->vif.bss_conf.beacon_rate = NULL;
2392
Alexander Bondar989c6502013-05-16 17:34:17 +03002393 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02002394
Johannes Berg028e8da02012-11-26 11:57:41 +01002395 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002396 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01002397 ieee80211_vif_release_channel(sdata);
Michal Kazior59af6922014-04-09 15:10:59 +02002398
2399 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002400 ifmgd->csa_waiting_bcn = false;
Johannes Bergf84eaa12015-03-12 08:53:26 +02002401 ifmgd->csa_ignored_same_chan = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002402 if (sdata->csa_block_tx) {
2403 ieee80211_wake_vif_queues(local, sdata,
2404 IEEE80211_QUEUE_STOP_REASON_CSA);
2405 sdata->csa_block_tx = false;
2406 }
Johannes Berg34a37402013-12-18 09:43:33 +01002407 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002408
Johannes Berg02219b32014-10-07 10:38:50 +03002409 /* existing TX TSPEC sessions no longer exist */
2410 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2411 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2412
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002413 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Wen Gong63214f02021-09-24 06:00:52 -04002414
2415 bss_conf->pwr_reduction = 0;
2416 bss_conf->tx_pwr_env_num = 0;
2417 memset(bss_conf->tx_pwr_env, 0, sizeof(bss_conf->tx_pwr_env));
Tomas Winkleraa458d12008-09-09 00:32:12 +03002418}
Jiri Bencf0706e82007-05-05 11:45:53 -07002419
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002420static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2421{
2422 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002423 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002424
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002425 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002426 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2427 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002428
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002429 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002430
2431 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002432 ieee80211_recalc_ps(local);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002433 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002434
Johannes Berg30686bf2015-06-02 21:39:54 +02002435 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002436 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002437
2438 /*
2439 * We've received a probe response, but are not sure whether
2440 * we have or will be receiving any beacons or data, so let's
2441 * schedule the timers again, just in case.
2442 */
2443 ieee80211_sta_reset_beacon_monitor(sdata);
2444
2445 mod_timer(&ifmgd->conn_mon_timer,
2446 round_jiffies_up(jiffies +
2447 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002448out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002449 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002450}
2451
Johannes Berg02219b32014-10-07 10:38:50 +03002452static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2453 struct ieee80211_hdr *hdr,
2454 u16 tx_time)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002455{
Johannes Berg02219b32014-10-07 10:38:50 +03002456 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5e568c2021-11-22 12:47:40 +01002457 u16 tid;
2458 int ac;
2459 struct ieee80211_sta_tx_tspec *tx_tspec;
Johannes Berg02219b32014-10-07 10:38:50 +03002460 unsigned long now = jiffies;
2461
Johannes Bergd5e568c2021-11-22 12:47:40 +01002462 if (!ieee80211_is_data_qos(hdr->frame_control))
2463 return;
2464
2465 tid = ieee80211_get_tid(hdr);
2466 ac = ieee80211_ac_from_tid(tid);
2467 tx_tspec = &ifmgd->tx_tspec[ac];
2468
Johannes Berg02219b32014-10-07 10:38:50 +03002469 if (likely(!tx_tspec->admitted_time))
2470 return;
2471
2472 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2473 tx_tspec->consumed_tx_time = 0;
2474 tx_tspec->time_slice_start = now;
2475
2476 if (tx_tspec->downgraded) {
2477 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2478 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2479 }
2480 }
2481
2482 if (tx_tspec->downgraded)
2483 return;
2484
2485 tx_tspec->consumed_tx_time += tx_time;
2486
2487 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2488 tx_tspec->downgraded = true;
2489 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2490 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2491 }
2492}
2493
2494void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2495 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2496{
2497 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2498
Felix Fietkau9abf4e42020-09-08 14:36:56 +02002499 if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
2500 !sdata->u.mgd.probe_send_count)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002501 return;
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002502
Felix Fietkaue3f25902020-09-27 12:56:05 +02002503 if (ack)
2504 sdata->u.mgd.probe_send_count = 0;
2505 else
Felix Fietkau9abf4e42020-09-08 14:36:56 +02002506 sdata->u.mgd.nullfunc_failed = true;
2507 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002508}
2509
Johannes Berg45ad6832018-05-28 15:47:39 +02002510static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2511 const u8 *src, const u8 *dst,
2512 const u8 *ssid, size_t ssid_len,
2513 struct ieee80211_channel *channel)
2514{
2515 struct sk_buff *skb;
2516
2517 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2518 ssid, ssid_len, NULL, 0,
2519 IEEE80211_PROBE_FLAG_DIRECTED);
2520 if (skb)
2521 ieee80211_tx_skb(sdata, skb);
2522}
2523
Maxim Levitskya43abf22009-07-31 18:54:12 +03002524static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2525{
2526 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergf2622132021-09-30 13:11:31 +02002527 const struct element *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002528 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002529 u8 unicast_limit = max(1, max_probe_tries - 3);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002530 struct sta_info *sta;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002531
2532 /*
2533 * Try sending broadcast probe requests for the last three
2534 * probe requests after the first ones failed since some
2535 * buggy APs only support broadcast probe requests.
2536 */
2537 if (ifmgd->probe_send_count >= unicast_limit)
2538 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002539
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002540 /*
2541 * When the hardware reports an accurate Tx ACK status, it's
2542 * better to send a nullfunc frame instead of a probe request,
2543 * as it will kick us off the AP quickly if we aren't associated
2544 * anymore. The timeout will be reset if the frame is ACKed by
2545 * the AP.
2546 */
Soumik Das992e68b2012-05-20 15:31:13 +05302547 ifmgd->probe_send_count++;
2548
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002549 if (dst) {
2550 mutex_lock(&sdata->local->sta_mtx);
2551 sta = sta_info_get(sdata, dst);
2552 if (!WARN_ON(!sta))
2553 ieee80211_check_fast_rx(sta);
2554 mutex_unlock(&sdata->local->sta_mtx);
2555 }
2556
Johannes Berg30686bf2015-06-02 21:39:54 +02002557 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002558 ifmgd->nullfunc_failed = false;
Ping-Ke Shih28329432021-06-23 21:48:26 +08002559 ieee80211_send_nullfunc(sdata->local, sdata, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002560 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002561 int ssid_len;
2562
Johannes Berg9caf0362012-11-29 01:25:20 +01002563 rcu_read_lock();
Johannes Bergf2622132021-09-30 13:11:31 +02002564 ssid = ieee80211_bss_get_elem(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002565 if (WARN_ON_ONCE(ssid == NULL))
2566 ssid_len = 0;
2567 else
Johannes Bergf2622132021-09-30 13:11:31 +02002568 ssid_len = ssid->datalen;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002569
Johannes Berg45ad6832018-05-28 15:47:39 +02002570 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
Johannes Bergf2622132021-09-30 13:11:31 +02002571 ssid->data, ssid_len,
Johannes Berg45ad6832018-05-28 15:47:39 +02002572 ifmgd->associated->channel);
Johannes Berg9caf0362012-11-29 01:25:20 +01002573 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002574 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002575
Ben Greear180205b2011-02-04 15:30:24 -08002576 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002577 run_again(sdata, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002578}
2579
Johannes Bergb291ba12009-07-10 15:29:03 +02002580static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2581 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002582{
Kalle Valo04de8382009-03-22 21:57:35 +02002583 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002584 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002585
Johannes Berg9607e6b662009-12-23 13:15:31 +01002586 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002587 return;
2588
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002589 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002590
2591 if (!ifmgd->associated)
2592 goto out;
2593
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002594 mutex_lock(&sdata->local->mtx);
2595
2596 if (sdata->local->tmp_channel || sdata->local->scanning) {
2597 mutex_unlock(&sdata->local->mtx);
2598 goto out;
2599 }
2600
Loic Poulainb33fb282021-10-21 10:45:27 +02002601 if (sdata->local->suspending) {
2602 /* reschedule after resume */
2603 mutex_unlock(&sdata->local->mtx);
2604 ieee80211_reset_ap_probe(sdata);
2605 goto out;
2606 }
2607
Ben Greeara13fbe52013-03-25 11:19:35 -07002608 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002609 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002610 "detected beacon loss from AP (missed %d beacons) - probing\n",
2611 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002612
Johannes Berg98f03342014-11-26 12:42:02 +01002613 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
Ben Greeara13fbe52013-03-25 11:19:35 -07002614 }
Kalle Valo04de8382009-03-22 21:57:35 +02002615
Johannes Bergb291ba12009-07-10 15:29:03 +02002616 /*
2617 * The driver/our work has already reported this event or the
2618 * connection monitoring has kicked in and we have already sent
2619 * a probe request. Or maybe the AP died and the driver keeps
2620 * reporting until we disassociate...
2621 *
2622 * In either case we have to ignore the current call to this
2623 * function (except for setting the correct probe reason bit)
2624 * because otherwise we would reset the timer every time and
2625 * never check whether we received a probe response!
2626 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002627 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02002628 already = true;
2629
Eliad Peller12b5f342013-11-18 19:06:46 +02002630 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2631
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002632 mutex_unlock(&sdata->local->mtx);
2633
Johannes Bergb291ba12009-07-10 15:29:03 +02002634 if (already)
2635 goto out;
2636
Johannes Berg4e751842009-06-10 15:16:52 +02002637 mutex_lock(&sdata->local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002638 ieee80211_recalc_ps(sdata->local);
Johannes Berg4e751842009-06-10 15:16:52 +02002639 mutex_unlock(&sdata->local->iflist_mtx);
2640
Maxim Levitskya43abf22009-07-31 18:54:12 +03002641 ifmgd->probe_send_count = 0;
2642 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002643 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002644 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002645}
2646
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002647struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2648 struct ieee80211_vif *vif)
2649{
2650 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2651 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002652 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002653 struct sk_buff *skb;
Johannes Bergf2622132021-09-30 13:11:31 +02002654 const struct element *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002655 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002656
2657 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2658 return NULL;
2659
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002660 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002661
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002662 if (ifmgd->associated)
2663 cbss = ifmgd->associated;
2664 else if (ifmgd->auth_data)
2665 cbss = ifmgd->auth_data->bss;
2666 else if (ifmgd->assoc_data)
2667 cbss = ifmgd->assoc_data->bss;
2668 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002669 return NULL;
2670
Johannes Berg9caf0362012-11-29 01:25:20 +01002671 rcu_read_lock();
Johannes Bergf2622132021-09-30 13:11:31 +02002672 ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
2673 if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
2674 "invalid SSID element (len=%d)",
2675 ssid ? ssid->datalen : -1))
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002676 ssid_len = 0;
2677 else
Johannes Bergf2622132021-09-30 13:11:31 +02002678 ssid_len = ssid->datalen;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002679
Johannes Berga344d672014-06-12 22:24:31 +02002680 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002681 (u32) -1, cbss->channel,
Johannes Bergf2622132021-09-30 13:11:31 +02002682 ssid->data, ssid_len,
Johannes Berg00387f32018-05-28 15:47:38 +02002683 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
Johannes Berg9caf0362012-11-29 01:25:20 +01002684 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002685
2686 return skb;
2687}
2688EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2689
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002690static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2691 const u8 *buf, size_t len, bool tx,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002692 u16 reason, bool reconnect)
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002693{
2694 struct ieee80211_event event = {
2695 .type = MLME_EVENT,
2696 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2697 .u.mlme.reason = reason,
2698 };
2699
2700 if (tx)
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002701 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002702 else
2703 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2704
2705 drv_event_callback(sdata->local, sdata, &event);
2706}
2707
Johannes Bergeef9e542013-01-29 13:09:34 +01002708static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002709{
Michal Kazior59af6922014-04-09 15:10:59 +02002710 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002711 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002712 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002713 bool tx;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002714
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002715 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002716 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002717 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002718 return;
2719 }
2720
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002721 tx = !sdata->csa_block_tx;
2722
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002723 if (!ifmgd->driver_disconnect) {
2724 /*
2725 * AP is probably out of range (or not reachable for another
2726 * reason) so remove the bss struct for that AP.
2727 */
2728 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2729 }
David Spinadel20eb7ea2016-05-03 16:05:02 +03002730
Johannes Berg37ad3882012-02-24 13:50:54 +01002731 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002732 ifmgd->driver_disconnect ?
2733 WLAN_REASON_DEAUTH_LEAVING :
2734 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002735 tx, frame_buf);
Michal Kazior59af6922014-04-09 15:10:59 +02002736 mutex_lock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03002737 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002738 ifmgd->csa_waiting_bcn = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002739 if (sdata->csa_block_tx) {
2740 ieee80211_wake_vif_queues(local, sdata,
2741 IEEE80211_QUEUE_STOP_REASON_CSA);
2742 sdata->csa_block_tx = false;
2743 }
Michal Kazior59af6922014-04-09 15:10:59 +02002744 mutex_unlock(&local->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002745
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002746 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002747 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2748 ifmgd->reconnect);
2749 ifmgd->reconnect = false;
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002750
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002751 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002752}
2753
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002754static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002755{
2756 struct ieee80211_sub_if_data *sdata =
2757 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002758 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002759 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Paul Stewarta85e1d52011-12-09 11:01:49 -08002760
Johannes Berg976bd9e2015-10-16 17:18:11 +02002761 if (ifmgd->associated)
2762 ifmgd->beacon_loss_count++;
Johannes Bergb291ba12009-07-10 15:29:03 +02002763
Johannes Berg682bd382013-01-29 13:13:50 +01002764 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002765 sdata_info(sdata, "Connection to AP %pM lost\n",
2766 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002767 __ieee80211_disconnect(sdata);
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002768 ifmgd->connection_loss = false;
2769 } else if (ifmgd->driver_disconnect) {
2770 sdata_info(sdata,
2771 "Driver requested disconnection from AP %pM\n",
2772 ifmgd->bssid);
2773 __ieee80211_disconnect(sdata);
2774 ifmgd->driver_disconnect = false;
Johannes Berg882a7c62012-08-01 22:32:45 +02002775 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002776 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002777 }
2778}
2779
2780static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2781{
2782 struct ieee80211_sub_if_data *sdata =
2783 container_of(work, struct ieee80211_sub_if_data,
2784 u.mgd.csa_connection_drop_work);
2785
Johannes Bergeef9e542013-01-29 13:09:34 +01002786 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002787}
2788
Kalle Valo04de8382009-03-22 21:57:35 +02002789void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2790{
2791 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002792 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002793
Johannes Bergb5878a22010-04-07 16:48:40 +02002794 trace_api_beacon_loss(sdata);
2795
Johannes Berg682bd382013-01-29 13:13:50 +01002796 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002797 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002798}
2799EXPORT_SYMBOL(ieee80211_beacon_loss);
2800
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002801void ieee80211_connection_loss(struct ieee80211_vif *vif)
2802{
2803 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2804 struct ieee80211_hw *hw = &sdata->local->hw;
2805
Johannes Bergb5878a22010-04-07 16:48:40 +02002806 trace_api_connection_loss(sdata);
2807
Johannes Berg682bd382013-01-29 13:13:50 +01002808 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002809 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2810}
2811EXPORT_SYMBOL(ieee80211_connection_loss);
2812
Johannes Berg3f8a39f2020-12-06 14:54:43 +02002813void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
2814{
2815 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2816 struct ieee80211_hw *hw = &sdata->local->hw;
2817
2818 trace_api_disconnect(sdata, reconnect);
2819
2820 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2821 return;
2822
2823 sdata->u.mgd.driver_disconnect = true;
2824 sdata->u.mgd.reconnect = reconnect;
2825 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2826}
2827EXPORT_SYMBOL(ieee80211_disconnect);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002828
Johannes Berg66e67e42012-01-20 13:55:27 +01002829static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2830 bool assoc)
2831{
2832 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2833
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002834 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002835
Johannes Berg66e67e42012-01-20 13:55:27 +01002836 if (!assoc) {
Emmanuel Grumbachc1e140b2015-01-19 16:53:06 +02002837 /*
2838 * we are not authenticated yet, the only timer that could be
2839 * running is the timeout for the authentication response which
2840 * which is not relevant anymore.
2841 */
2842 del_timer_sync(&sdata->u.mgd.timer);
Johannes Berg66e67e42012-01-20 13:55:27 +01002843 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2844
Joe Perchesc84a67a2015-03-02 19:54:57 -08002845 eth_zero_addr(sdata->u.mgd.bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002846 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002847 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002848 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002849 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002850 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002851 }
2852
Johannes Berg5b112d32013-02-01 01:49:58 +01002853 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002854 kfree(auth_data);
2855 sdata->u.mgd.auth_data = NULL;
2856}
2857
Johannes Bergc9c99f82015-06-01 14:10:09 +02002858static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
Johannes Berge6f462d2016-12-08 17:22:09 +01002859 bool assoc, bool abandon)
Johannes Bergc9c99f82015-06-01 14:10:09 +02002860{
2861 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2862
2863 sdata_assert_lock(sdata);
2864
2865 if (!assoc) {
2866 /*
2867 * we are not associated yet, the only timer that could be
2868 * running is the timeout for the association response which
2869 * which is not relevant anymore.
2870 */
2871 del_timer_sync(&sdata->u.mgd.timer);
2872 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2873
2874 eth_zero_addr(sdata->u.mgd.bssid);
2875 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2876 sdata->u.mgd.flags = 0;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002877 sdata->vif.mu_mimo_owner = false;
2878
Johannes Bergc9c99f82015-06-01 14:10:09 +02002879 mutex_lock(&sdata->local->mtx);
2880 ieee80211_vif_release_channel(sdata);
2881 mutex_unlock(&sdata->local->mtx);
Johannes Berge6f462d2016-12-08 17:22:09 +01002882
2883 if (abandon)
2884 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
Johannes Bergc9c99f82015-06-01 14:10:09 +02002885 }
2886
2887 kfree(assoc_data);
2888 sdata->u.mgd.assoc_data = NULL;
2889}
2890
Johannes Berg66e67e42012-01-20 13:55:27 +01002891static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2892 struct ieee80211_mgmt *mgmt, size_t len)
2893{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002894 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002895 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
Johannes Berg49a765d2021-09-20 15:40:09 +02002896 const struct element *challenge;
Johannes Berg66e67e42012-01-20 13:55:27 +01002897 u8 *pos;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002898 u32 tx_flags = 0;
Johannes Berg15fae342021-06-18 13:41:55 +03002899 struct ieee80211_prep_tx_info info = {
2900 .subtype = IEEE80211_STYPE_AUTH,
2901 };
Johannes Berg66e67e42012-01-20 13:55:27 +01002902
2903 pos = mgmt->u.auth.variable;
Johannes Berg49a765d2021-09-20 15:40:09 +02002904 challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
2905 len - (pos - (u8 *)mgmt));
2906 if (!challenge)
Johannes Berg66e67e42012-01-20 13:55:27 +01002907 return;
2908 auth_data->expected_transaction = 4;
Johannes Berg15fae342021-06-18 13:41:55 +03002909 drv_mgd_prepare_tx(sdata->local, sdata, &info);
Johannes Berg30686bf2015-06-02 21:39:54 +02002910 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01002911 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2912 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002913 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg49a765d2021-09-20 15:40:09 +02002914 (void *)challenge,
2915 challenge->datalen + sizeof(*challenge),
Johannes Berg66e67e42012-01-20 13:55:27 +01002916 auth_data->bss->bssid, auth_data->bss->bssid,
2917 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002918 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002919}
2920
Jouni Malinenfc107a92018-10-11 00:21:19 +03002921static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2922 const u8 *bssid)
2923{
Jouni Malinenefb543e2018-10-11 00:21:21 +03002924 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002925 struct sta_info *sta;
Wei Yongjun33483a62018-10-16 02:35:30 +00002926 bool result = true;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002927
Jouni Malinenefb543e2018-10-11 00:21:21 +03002928 sdata_info(sdata, "authenticated\n");
2929 ifmgd->auth_data->done = true;
2930 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2931 ifmgd->auth_data->timeout_started = true;
2932 run_again(sdata, ifmgd->auth_data->timeout);
2933
Jouni Malinenfc107a92018-10-11 00:21:19 +03002934 /* move station state to auth */
2935 mutex_lock(&sdata->local->sta_mtx);
2936 sta = sta_info_get(sdata, bssid);
2937 if (!sta) {
2938 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002939 result = false;
2940 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002941 }
2942 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2943 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002944 result = false;
2945 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002946 }
Jouni Malinenfc107a92018-10-11 00:21:19 +03002947
Wei Yongjun33483a62018-10-16 02:35:30 +00002948out:
2949 mutex_unlock(&sdata->local->sta_mtx);
2950 return result;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002951}
2952
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002953static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2954 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002955{
2956 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2957 u8 bssid[ETH_ALEN];
2958 u16 auth_alg, auth_transaction, status_code;
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002959 struct ieee80211_event event = {
2960 .type = MLME_EVENT,
2961 .u.mlme.data = AUTH_EVENT,
2962 };
Johannes Berg15fae342021-06-18 13:41:55 +03002963 struct ieee80211_prep_tx_info info = {
2964 .subtype = IEEE80211_STYPE_AUTH,
2965 };
Johannes Berg66e67e42012-01-20 13:55:27 +01002966
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002967 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002968
2969 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002970 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002971
2972 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002973 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002974
2975 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2976
Joe Perchesb203ca32012-05-08 18:56:52 +00002977 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002978 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002979
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 ||
Jouni Malinenefb543e2018-10-11 00:21:21 +03002985 (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))) {
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002990 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);
Johannes Berg15fae342021-06-18 13:41:55 +03002994 goto notify_driver;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002995 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002996
2997 if (status_code != WLAN_STATUS_SUCCESS) {
Andrei Otcheretianskia4055e72020-03-26 15:09:34 +02002998 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2999
3000 if (auth_alg == WLAN_AUTH_SAE &&
Jouni Malinen4e56cde2020-07-31 21:38:30 +03003001 (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))))
Johannes Berg15fae342021-06-18 13:41:55 +03003005 goto notify_driver;
Andrei Otcheretianskia4055e72020-03-26 15:09:34 +02003006
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003007 sdata_info(sdata, "%pM denied authentication (status %d)\n",
3008 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03003009 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003010 event.u.mlme.status = MLME_DENIED;
3011 event.u.mlme.reason = status_code;
3012 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg15fae342021-06-18 13:41:55 +03003013 goto notify_driver;
Johannes Berg66e67e42012-01-20 13:55:27 +01003014 }
3015
3016 switch (ifmgd->auth_data->algorithm) {
3017 case WLAN_AUTH_OPEN:
3018 case WLAN_AUTH_LEAP:
3019 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003020 case WLAN_AUTH_SAE:
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03003021 case WLAN_AUTH_FILS_SK:
3022 case WLAN_AUTH_FILS_SK_PFS:
3023 case WLAN_AUTH_FILS_PK:
Johannes Berg66e67e42012-01-20 13:55:27 +01003024 break;
3025 case WLAN_AUTH_SHARED_KEY:
3026 if (ifmgd->auth_data->expected_transaction != 4) {
3027 ieee80211_auth_challenge(sdata, mgmt, len);
3028 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003029 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003030 }
3031 break;
3032 default:
3033 WARN_ONCE(1, "invalid auth alg %d",
3034 ifmgd->auth_data->algorithm);
Johannes Berg15fae342021-06-18 13:41:55 +03003035 goto notify_driver;
Johannes Berg66e67e42012-01-20 13:55:27 +01003036 }
3037
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003038 event.u.mlme.status = MLME_SUCCESS;
Johannes Berg15fae342021-06-18 13:41:55 +03003039 info.success = 1;
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003040 drv_event_callback(sdata->local, sdata, &event);
Jouni Malinenefb543e2018-10-11 00:21:21 +03003041 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3042 (auth_transaction == 2 &&
3043 ifmgd->auth_data->expected_transaction == 2)) {
3044 if (!ieee80211_mark_sta_auth(sdata, bssid))
Andrei Otcheretianski0daa63e2020-02-21 10:47:20 +01003045 return; /* ignore frame -- wait for timeout */
Jouni Malinenefb543e2018-10-11 00:21:21 +03003046 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3047 auth_transaction == 2) {
3048 sdata_info(sdata, "SAE peer confirmed\n");
3049 ifmgd->auth_data->peer_confirmed = true;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003050 }
3051
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003052 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg15fae342021-06-18 13:41:55 +03003053notify_driver:
3054 drv_mgd_complete_tx(sdata->local, sdata, &info);
Johannes Berg66e67e42012-01-20 13:55:27 +01003055}
3056
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003057#define case_WLAN(type) \
3058 case WLAN_REASON_##type: return #type
3059
Yu Wang79c92ca2019-05-10 17:04:52 +08003060const char *ieee80211_get_reason_code_string(u16 reason_code)
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003061{
3062 switch (reason_code) {
3063 case_WLAN(UNSPECIFIED);
3064 case_WLAN(PREV_AUTH_NOT_VALID);
3065 case_WLAN(DEAUTH_LEAVING);
3066 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3067 case_WLAN(DISASSOC_AP_BUSY);
3068 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3069 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3070 case_WLAN(DISASSOC_STA_HAS_LEFT);
3071 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3072 case_WLAN(DISASSOC_BAD_POWER);
3073 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3074 case_WLAN(INVALID_IE);
3075 case_WLAN(MIC_FAILURE);
3076 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3077 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3078 case_WLAN(IE_DIFFERENT);
3079 case_WLAN(INVALID_GROUP_CIPHER);
3080 case_WLAN(INVALID_PAIRWISE_CIPHER);
3081 case_WLAN(INVALID_AKMP);
3082 case_WLAN(UNSUPP_RSN_VERSION);
3083 case_WLAN(INVALID_RSN_IE_CAP);
3084 case_WLAN(IEEE8021X_FAILED);
3085 case_WLAN(CIPHER_SUITE_REJECTED);
3086 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3087 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3088 case_WLAN(DISASSOC_LOW_ACK);
3089 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3090 case_WLAN(QSTA_LEAVE_QBSS);
3091 case_WLAN(QSTA_NOT_USE);
3092 case_WLAN(QSTA_REQUIRE_SETUP);
3093 case_WLAN(QSTA_TIMEOUT);
3094 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3095 case_WLAN(MESH_PEER_CANCELED);
3096 case_WLAN(MESH_MAX_PEERS);
3097 case_WLAN(MESH_CONFIG);
3098 case_WLAN(MESH_CLOSE);
3099 case_WLAN(MESH_MAX_RETRIES);
3100 case_WLAN(MESH_CONFIRM_TIMEOUT);
3101 case_WLAN(MESH_INVALID_GTK);
3102 case_WLAN(MESH_INCONSISTENT_PARAM);
3103 case_WLAN(MESH_INVALID_SECURITY);
3104 case_WLAN(MESH_PATH_ERROR);
3105 case_WLAN(MESH_PATH_NOFORWARD);
3106 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3107 case_WLAN(MAC_EXISTS_IN_MBSS);
3108 case_WLAN(MESH_CHAN_REGULATORY);
3109 case_WLAN(MESH_CHAN);
3110 default: return "<unknown>";
3111 }
3112}
Johannes Berg66e67e42012-01-20 13:55:27 +01003113
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003114static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3115 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003116{
Johannes Berg46900292009-02-15 12:44:28 +01003117 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003118 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003119
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003120 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003121
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003122 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003123 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003124
Yu Wang79c92ca2019-05-10 17:04:52 +08003125 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3126 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3127 return;
3128 }
3129
Johannes Bergc9c99f82015-06-01 14:10:09 +02003130 if (ifmgd->associated &&
3131 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3132 const u8 *bssid = ifmgd->associated->bssid;
3133
3134 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3135 bssid, reason_code,
3136 ieee80211_get_reason_code_string(reason_code));
3137
3138 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3139
3140 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02003141 reason_code, false);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003142 return;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003143 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003144
Johannes Bergc9c99f82015-06-01 14:10:09 +02003145 if (ifmgd->assoc_data &&
3146 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3147 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07003148
Johannes Bergc9c99f82015-06-01 14:10:09 +02003149 sdata_info(sdata,
3150 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3151 bssid, reason_code,
3152 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003153
Johannes Berge6f462d2016-12-08 17:22:09 +01003154 ieee80211_destroy_assoc_data(sdata, false, true);
Jiri Bencf0706e82007-05-05 11:45:53 -07003155
Johannes Bergc9c99f82015-06-01 14:10:09 +02003156 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3157 return;
3158 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003159}
3160
3161
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003162static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3163 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003164{
Johannes Berg46900292009-02-15 12:44:28 +01003165 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003166 u16 reason_code;
3167
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003168 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003169
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003170 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003171 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003172
Johannes Berg66e67e42012-01-20 13:55:27 +01003173 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00003174 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003175 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003176
3177 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3178
Yu Wang79c92ca2019-05-10 17:04:52 +08003179 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3180 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3181 return;
3182 }
3183
Arkadiusz Miskiewicz68506e92017-02-15 14:21:27 +01003184 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3185 mgmt->sa, reason_code,
3186 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003187
Johannes Berg37ad3882012-02-24 13:50:54 +01003188 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3189
Johannes Berg3f8a39f2020-12-06 14:54:43 +02003190 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
3191 false);
Jiri Bencf0706e82007-05-05 11:45:53 -07003192}
3193
Christian Lamparterc74d0842011-10-15 00:14:49 +02003194static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3195 u8 *supp_rates, unsigned int supp_rates_len,
3196 u32 *rates, u32 *basic_rates,
3197 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003198 int *min_rate, int *min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03003199 int shift)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003200{
3201 int i, j;
3202
3203 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003204 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003205 bool is_basic = !!(supp_rates[i] & 0x80);
3206
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003207 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003208 *have_higher_than_11mbit = true;
3209
3210 /*
Ilan Peer3598ae82020-11-29 17:30:47 +02003211 * Skip HT, VHT, HE and SAE H2E only BSS membership selectors
3212 * since they're not rates.
Christian Lamparterc74d0842011-10-15 00:14:49 +02003213 *
Johannes Berga6289d32017-03-06 22:59:04 +01003214 * Note: Even though the membership selector and the basic
Christian Lamparterc74d0842011-10-15 00:14:49 +02003215 * rate flag share the same bit, they are not exactly
3216 * the same.
3217 */
Johannes Berga6289d32017-03-06 22:59:04 +01003218 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
Ilan Peer4826e722020-03-26 15:09:36 +02003219 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
Ilan Peer3598ae82020-11-29 17:30:47 +02003220 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY) ||
3221 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E))
Christian Lamparterc74d0842011-10-15 00:14:49 +02003222 continue;
3223
3224 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003225 struct ieee80211_rate *br;
3226 int brate;
3227
3228 br = &sband->bitrates[j];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003229
3230 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3231 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02003232 *rates |= BIT(j);
3233 if (is_basic)
3234 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003235 if ((rate * 5) < *min_rate) {
3236 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003237 *min_rate_index = j;
3238 }
3239 break;
3240 }
3241 }
3242 }
3243}
Jiri Bencf0706e82007-05-05 11:45:53 -07003244
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003245static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3246 const struct ieee802_11_elems *elems)
3247{
3248 if (elems->ext_capab_len < 10)
3249 return false;
3250
3251 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3252 return false;
3253
3254 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3255 IEEE80211_HE_MAC_CAP0_TWT_RES;
3256}
3257
John Crispinc9d32452019-05-28 13:49:47 +02003258static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3259 struct sta_info *sta,
3260 struct ieee802_11_elems *elems)
3261{
3262 bool twt = ieee80211_twt_req_supported(sta, elems);
3263
3264 if (sdata->vif.bss_conf.twt_requester != twt) {
3265 sdata->vif.bss_conf.twt_requester = twt;
3266 return BSS_CHANGED_TWT;
3267 }
3268 return 0;
3269}
3270
Johannes Bergbac2fd32021-06-18 13:41:50 +03003271static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
3272 struct ieee80211_bss_conf *bss_conf,
Shaul Triebitzd8b26152021-06-18 13:41:35 +03003273 struct ieee80211_supported_band *sband,
3274 struct sta_info *sta)
3275{
3276 const struct ieee80211_sta_he_cap *own_he_cap =
Johannes Bergbac2fd32021-06-18 13:41:50 +03003277 ieee80211_get_he_iftype_cap(sband,
3278 ieee80211_vif_type_p2p(&sdata->vif));
Shaul Triebitzd8b26152021-06-18 13:41:35 +03003279
3280 return bss_conf->he_support &&
3281 (sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3282 IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
3283 own_he_cap &&
3284 (own_he_cap->he_cap_elem.mac_cap_info[2] &
3285 IEEE80211_HE_MAC_CAP2_BCAST_TWT);
3286}
3287
Johannes Berg66e67e42012-01-20 13:55:27 +01003288static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3289 struct cfg80211_bss *cbss,
Johannes Bergf61d7882019-10-28 12:52:42 +01003290 struct ieee80211_mgmt *mgmt, size_t len,
3291 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07003292{
Johannes Berg46900292009-02-15 12:44:28 +01003293 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01003294 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01003295 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003296 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003297 u16 capab_info, aid;
Johannes Bergbda39332008-10-11 01:51:51 +02003298 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02003299 const struct cfg80211_bss_ies *bss_ies = NULL;
3300 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Berg57fa5e82020-05-28 21:34:36 +02003301 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003302 bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
Johannes Bergae5eb022008-10-14 16:58:37 +02003303 u32 changed = 0;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003304 u8 *pos;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003305 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02003306 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003307
Johannes Bergaf6b6372009-12-23 13:15:35 +01003308 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07003309
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003310 pos = mgmt->u.assoc_resp.variable;
Jiri Bencf0706e82007-05-05 11:45:53 -07003311 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003312 if (is_s1g) {
3313 pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3314 aid = 0; /* TODO */
3315 }
Johannes Bergaf6b6372009-12-23 13:15:35 +01003316 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Johannes Berg5d248282021-09-20 15:40:10 +02003317 elems = ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false,
3318 mgmt->bssid, assoc_data->bss->bssid);
3319
3320 if (!elems)
3321 return false;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003322
3323 if (elems->aid_resp)
3324 aid = le16_to_cpu(elems->aid_resp->aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07003325
Johannes Bergc7c477b2017-12-01 13:50:51 +02003326 /*
3327 * The 5 MSB of the AID field are reserved
3328 * (802.11-2016 9.4.1.8 AID field)
3329 */
3330 aid &= 0x7ff;
Johannes Berg1dd84aa2007-10-10 12:03:41 +02003331
Johannes Berg05cb9102011-10-28 11:59:47 +02003332 ifmgd->broken_ap = false;
3333
3334 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003335 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3336 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02003337 aid = 0;
3338 ifmgd->broken_ap = true;
3339 }
3340
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003341 if (!is_s1g && !elems->supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003342 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Berg5d248282021-09-20 15:40:10 +02003343 ret = false;
3344 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07003345 }
3346
Johannes Berg1db364c2020-04-17 12:38:04 +02003347 sdata->vif.bss_conf.aid = aid;
Arik Nemtsov9041c1f2014-11-09 18:50:15 +02003348 ifmgd->tdls_chan_switch_prohibited =
Johannes Bergf61d7882019-10-28 12:52:42 +01003349 elems->ext_capab && elems->ext_capab_len >= 5 &&
3350 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
Jiri Bencf0706e82007-05-05 11:45:53 -07003351
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003352 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02003353 * Some APs are erroneously not including some information in their
3354 * (re)association response frames. Try to recover by using the data
3355 * from the beacon or probe response. This seems to afflict mobile
3356 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3357 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3358 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003359 if (!is_6ghz &&
3360 ((assoc_data->wmm && !elems->wmm_param) ||
3361 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3362 (!elems->ht_cap_elem || !elems->ht_operation)) ||
3363 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3364 (!elems->vht_cap_elem || !elems->vht_operation)))) {
Johannes Berg35d865a2013-05-28 10:54:03 +02003365 const struct cfg80211_bss_ies *ies;
Johannes Berg5d248282021-09-20 15:40:10 +02003366 struct ieee802_11_elems *bss_elems;
Johannes Berg35d865a2013-05-28 10:54:03 +02003367
3368 rcu_read_lock();
3369 ies = rcu_dereference(cbss->ies);
3370 if (ies)
3371 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3372 GFP_ATOMIC);
3373 rcu_read_unlock();
Johannes Berg8223ac12021-10-01 21:11:08 +02003374 if (!bss_ies) {
3375 ret = false;
3376 goto out;
3377 }
Johannes Berg35d865a2013-05-28 10:54:03 +02003378
Johannes Berg5d248282021-09-20 15:40:10 +02003379 bss_elems = ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3380 false, mgmt->bssid,
3381 assoc_data->bss->bssid);
3382 if (!bss_elems) {
3383 ret = false;
3384 goto out;
3385 }
3386
Johannes Berg35d865a2013-05-28 10:54:03 +02003387 if (assoc_data->wmm &&
Johannes Berg5d248282021-09-20 15:40:10 +02003388 !elems->wmm_param && bss_elems->wmm_param) {
3389 elems->wmm_param = bss_elems->wmm_param;
Johannes Berg35d865a2013-05-28 10:54:03 +02003390 sdata_info(sdata,
3391 "AP bug: WMM param missing from AssocResp\n");
3392 }
3393
3394 /*
3395 * Also check if we requested HT/VHT, otherwise the AP doesn't
3396 * have to include the IEs in the (re)association response.
3397 */
Johannes Berg5d248282021-09-20 15:40:10 +02003398 if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003399 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Berg5d248282021-09-20 15:40:10 +02003400 elems->ht_cap_elem = bss_elems->ht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003401 sdata_info(sdata,
3402 "AP bug: HT capability missing from AssocResp\n");
3403 }
Johannes Berg5d248282021-09-20 15:40:10 +02003404 if (!elems->ht_operation && bss_elems->ht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003405 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Berg5d248282021-09-20 15:40:10 +02003406 elems->ht_operation = bss_elems->ht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003407 sdata_info(sdata,
3408 "AP bug: HT operation missing from AssocResp\n");
3409 }
Johannes Berg5d248282021-09-20 15:40:10 +02003410 if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003411 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Berg5d248282021-09-20 15:40:10 +02003412 elems->vht_cap_elem = bss_elems->vht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003413 sdata_info(sdata,
3414 "AP bug: VHT capa missing from AssocResp\n");
3415 }
Johannes Berg5d248282021-09-20 15:40:10 +02003416 if (!elems->vht_operation && bss_elems->vht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003417 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Berg5d248282021-09-20 15:40:10 +02003418 elems->vht_operation = bss_elems->vht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003419 sdata_info(sdata,
3420 "AP bug: VHT operation missing from AssocResp\n");
3421 }
Johannes Berg5d248282021-09-20 15:40:10 +02003422
3423 kfree(bss_elems);
Johannes Berg35d865a2013-05-28 10:54:03 +02003424 }
3425
3426 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003427 * We previously checked these in the beacon/probe response, so
3428 * they should be present here. This is just a safety net.
3429 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003430 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003431 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003432 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003433 "HT AP is missing WMM params or HT capability/operation\n");
3434 ret = false;
3435 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003436 }
3437
Johannes Berg57fa5e82020-05-28 21:34:36 +02003438 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003439 (!elems->vht_cap_elem || !elems->vht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003440 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003441 "VHT AP is missing VHT capability/operation\n");
3442 ret = false;
3443 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003444 }
3445
Johannes Berg57fa5e82020-05-28 21:34:36 +02003446 if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3447 !elems->he_6ghz_capa) {
3448 sdata_info(sdata,
3449 "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3450 ret = false;
3451 goto out;
3452 }
3453
Guy Eilam2a33bee2011-08-17 15:18:15 +03003454 mutex_lock(&sdata->local->sta_mtx);
3455 /*
3456 * station info was already allocated and inserted before
3457 * the association and should be available to us
3458 */
Johannes Berg7852e362012-01-20 13:55:24 +01003459 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003460 if (WARN_ON(!sta)) {
3461 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003462 ret = false;
3463 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07003464 }
3465
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05303466 sband = ieee80211_get_sband(sdata);
3467 if (!sband) {
3468 mutex_unlock(&sdata->local->sta_mtx);
3469 ret = false;
3470 goto out;
3471 }
Johannes Berg8318d782008-01-24 19:38:38 +01003472
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003473 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003474 (!elems->he_cap || !elems->he_operation)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003475 mutex_unlock(&sdata->local->sta_mtx);
3476 sdata_info(sdata,
3477 "HE AP is missing HE capability/operation\n");
3478 ret = false;
3479 goto out;
3480 }
3481
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003482 /* Set up internal HT/VHT capabilities */
Johannes Bergf61d7882019-10-28 12:52:42 +01003483 if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08003484 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003485 elems->ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02003486
Johannes Bergf61d7882019-10-28 12:52:42 +01003487 if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Mahesh Palivela818255e2012-10-10 11:33:04 +00003488 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003489 elems->vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00003490
Johannes Bergf61d7882019-10-28 12:52:42 +01003491 if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3492 elems->he_cap) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003493 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003494 elems->he_cap,
3495 elems->he_cap_len,
Johannes Berg1bb9a8a2020-05-28 21:34:38 +02003496 elems->he_6ghz_capa,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003497 sta);
3498
3499 bss_conf->he_support = sta->sta.he_cap.has_he;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003500 if (elems->rsnx && elems->rsnx_len &&
3501 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3502 wiphy_ext_feature_isset(local->hw.wiphy,
3503 NL80211_EXT_FEATURE_PROTECTED_TWT))
3504 bss_conf->twt_protected = true;
3505 else
3506 bss_conf->twt_protected = false;
3507
Johannes Bergf61d7882019-10-28 12:52:42 +01003508 changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003509 } else {
3510 bss_conf->he_support = false;
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003511 bss_conf->twt_requester = false;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003512 bss_conf->twt_protected = false;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003513 }
3514
Shaul Triebitzd8b26152021-06-18 13:41:35 +03003515 bss_conf->twt_broadcast =
Johannes Bergbac2fd32021-06-18 13:41:50 +03003516 ieee80211_twt_bcast_support(sdata, bss_conf, sband, sta);
Shaul Triebitzd8b26152021-06-18 13:41:35 +03003517
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003518 if (bss_conf->he_support) {
John Crispindd56e902019-12-17 15:19:19 +01003519 bss_conf->he_bss_color.color =
Johannes Bergf61d7882019-10-28 12:52:42 +01003520 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003521 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
John Crispindd56e902019-12-17 15:19:19 +01003522 bss_conf->he_bss_color.partial =
3523 le32_get_bits(elems->he_operation->he_oper_params,
3524 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
Johannes Berg75e6b592020-07-30 13:00:52 +02003525 bss_conf->he_bss_color.enabled =
3526 !le32_get_bits(elems->he_operation->he_oper_params,
3527 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
P Praneesh322cd272020-07-09 08:16:21 +05303528
Johannes Berg75e6b592020-07-30 13:00:52 +02003529 if (bss_conf->he_bss_color.enabled)
P Praneesh322cd272020-07-09 08:16:21 +05303530 changed |= BSS_CHANGED_HE_BSS_COLOR;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003531
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003532 bss_conf->htc_trig_based_pkt_ext =
Johannes Bergf61d7882019-10-28 12:52:42 +01003533 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003534 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003535 bss_conf->frame_time_rts_th =
Johannes Bergf61d7882019-10-28 12:52:42 +01003536 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003537 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003538
Johannes Bergf61d7882019-10-28 12:52:42 +01003539 bss_conf->uora_exists = !!elems->uora_element;
3540 if (elems->uora_element)
3541 bss_conf->uora_ocw_range = elems->uora_element[0];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003542
Johannes Bergf61d7882019-10-28 12:52:42 +01003543 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3544 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003545 /* TODO: OPEN: what happens if BSS color disable is set? */
3546 }
3547
Sara Sharon78ac51f2019-01-16 18:22:56 +02003548 if (cbss->transmitted_bss) {
3549 bss_conf->nontransmitted = true;
3550 ether_addr_copy(bss_conf->transmitter_bssid,
3551 cbss->transmitted_bss->bssid);
3552 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3553 bss_conf->bssid_index = cbss->bssid_index;
3554 }
3555
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003556 /*
3557 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3558 * in their association response, so ignore that data for our own
3559 * configuration. If it changed since the last beacon, we'll get the
3560 * next beacon and update then.
3561 */
Johannes Berg11289582013-02-07 17:36:12 +01003562
Johannes Bergbee7f5862013-02-07 22:24:55 +01003563 /*
3564 * If an operating mode notification IE is present, override the
3565 * NSS calculation (that would be done in rate_control_rate_init())
3566 * and use the # of streams from that element.
3567 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003568 if (elems->opmode_notif &&
3569 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
Johannes Bergbee7f5862013-02-07 22:24:55 +01003570 u8 nss;
3571
Johannes Bergf61d7882019-10-28 12:52:42 +01003572 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
Johannes Bergbee7f5862013-02-07 22:24:55 +01003573 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3574 nss += 1;
3575 sta->sta.rx_nss = nss;
3576 }
3577
Johannes Berg4b7679a2008-09-18 18:14:18 +02003578 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003579
Tamizh chelvam93f04902015-10-07 10:40:04 +05303580 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02003581 set_sta_flag(sta, WLAN_STA_MFP);
Tamizh chelvam93f04902015-10-07 10:40:04 +05303582 sta->sta.mfp = true;
3583 } else {
3584 sta->sta.mfp = false;
3585 }
Jouni Malinen5394af42009-01-08 13:31:59 +02003586
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003587 sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
3588 local->hw.queues >= IEEE80211_NUM_ACS;
Johannes Bergddf4ac52008-10-22 11:41:38 +02003589
Johannes Berg3e4d40f2013-02-07 17:19:08 +01003590 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01003591 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3592 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3593 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003594 sdata_info(sdata,
3595 "failed to move station %pM to desired state\n",
3596 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01003597 WARN_ON(__sta_info_destroy(sta));
3598 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003599 ret = false;
3600 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01003601 }
3602
Felix Fietkau1ff4e8f2020-09-08 14:37:01 +02003603 if (sdata->wdev.use_4addr)
3604 drv_sta_set_4addr(local, sdata, &sta->sta, true);
3605
Johannes Berg7852e362012-01-20 13:55:24 +01003606 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02003607
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003608 /*
3609 * Always handle WMM once after association regardless
3610 * of the first value the AP uses. Setting -1 here has
3611 * that effect because the AP values is an unsigned
3612 * 4-bit value.
3613 */
3614 ifmgd->wmm_last_param_set = -1;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02003615 ifmgd->mu_edca_last_param_set = -1;
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003616
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003617 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Bergcec66282015-10-22 17:46:04 +02003618 ieee80211_set_wmm_default(sdata, false, false);
Johannes Bergf61d7882019-10-28 12:52:42 +01003619 } else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3620 elems->wmm_param_len,
3621 elems->mu_edca_param_set)) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003622 /* still enable QoS since we might have HT/VHT */
3623 ieee80211_set_wmm_default(sdata, false, true);
3624 /* set the disable-WMM flag in this case to disable
3625 * tracking WMM parameter changes in the beacon if
3626 * the parameters weren't actually valid. Doing so
3627 * avoids changing parameters very strangely when
3628 * the AP is going back and forth between valid and
3629 * invalid parameters.
3630 */
3631 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3632 }
Johannes Berg3abead52012-03-02 15:56:59 +01003633 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07003634
Johannes Bergf61d7882019-10-28 12:52:42 +01003635 if (elems->max_idle_period_ie) {
Avraham Sterne38a0172017-04-26 10:58:47 +03003636 bss_conf->max_idle_period =
Johannes Bergf61d7882019-10-28 12:52:42 +01003637 le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
Avraham Sterne38a0172017-04-26 10:58:47 +03003638 bss_conf->protected_keep_alive =
Johannes Bergf61d7882019-10-28 12:52:42 +01003639 !!(elems->max_idle_period_ie->idle_options &
Avraham Sterne38a0172017-04-26 10:58:47 +03003640 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3641 changed |= BSS_CHANGED_KEEP_ALIVE;
3642 } else {
3643 bss_conf->max_idle_period = 0;
3644 bss_conf->protected_keep_alive = false;
3645 }
3646
Johannes Berg1db364c2020-04-17 12:38:04 +02003647 /* set assoc capability (AID was already set earlier),
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003648 * ieee80211_set_associated() will tell the driver */
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003649 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003650 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003651
Kalle Valo15b7b062009-03-22 21:57:14 +02003652 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01003653 * If we're using 4-addr mode, let the AP know that we're
3654 * doing so, so that it can create the STA VLAN on its side
3655 */
3656 if (ifmgd->use_4addr)
3657 ieee80211_send_4addr_nullfunc(local, sdata);
3658
3659 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02003660 * Start timer to probe the connection to the AP now.
3661 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02003662 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003663 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau9abf4e42020-09-08 14:36:56 +02003664 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02003665
Johannes Berg35d865a2013-05-28 10:54:03 +02003666 ret = true;
3667 out:
Johannes Berg5d248282021-09-20 15:40:10 +02003668 kfree(elems);
Johannes Berg35d865a2013-05-28 10:54:03 +02003669 kfree(bss_ies);
3670 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003671}
3672
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003673static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3674 struct ieee80211_mgmt *mgmt,
3675 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01003676{
3677 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3678 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3679 u16 capab_info, status_code, aid;
Johannes Berg5d248282021-09-20 15:40:10 +02003680 struct ieee802_11_elems *elems;
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003681 int ac, uapsd_queues = -1;
Johannes Berg66e67e42012-01-20 13:55:27 +01003682 u8 *pos;
3683 bool reassoc;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003684 struct cfg80211_bss *cbss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003685 struct ieee80211_event event = {
3686 .type = MLME_EVENT,
3687 .u.mlme.data = ASSOC_EVENT,
3688 };
Johannes Berg15fae342021-06-18 13:41:55 +03003689 struct ieee80211_prep_tx_info info = {};
Jiri Bencf0706e82007-05-05 11:45:53 -07003690
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003691 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003692
3693 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003694 return;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003695
Joe Perchesb203ca32012-05-08 18:56:52 +00003696 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003697 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003698
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003699 cbss = assoc_data->bss;
3700
Johannes Berg66e67e42012-01-20 13:55:27 +01003701 /*
3702 * AssocResp and ReassocResp have identical structure, so process both
3703 * of them in this function.
3704 */
3705
3706 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003707 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003708
Simon Dinkin7a7d3e42017-08-31 13:09:36 +03003709 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
Johannes Berg66e67e42012-01-20 13:55:27 +01003710 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3711 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003712 pos = mgmt->u.assoc_resp.variable;
Johannes Berg66e67e42012-01-20 13:55:27 +01003713 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003714 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
3715 pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3716 aid = 0; /* TODO */
3717 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003718
Johannes Berg15fae342021-06-18 13:41:55 +03003719 /*
3720 * Note: this may not be perfect, AP might misbehave - if
3721 * anyone needs to rely on perfect complete notification
3722 * with the exact right subtype, then we need to track what
3723 * we actually transmitted.
3724 */
3725 info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
3726 IEEE80211_STYPE_ASSOC_REQ;
3727
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003728 sdata_info(sdata,
3729 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3730 reassoc ? "Rea" : "A", mgmt->sa,
3731 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01003732
Jouni Malinen39404fe2016-10-27 00:42:05 +03003733 if (assoc_data->fils_kek_len &&
3734 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3735 return;
3736
Johannes Berg5d248282021-09-20 15:40:10 +02003737 elems = ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false,
3738 mgmt->bssid, assoc_data->bss->bssid);
3739 if (!elems)
3740 goto notify_driver;
Johannes Berg66e67e42012-01-20 13:55:27 +01003741
3742 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg5d248282021-09-20 15:40:10 +02003743 elems->timeout_int &&
3744 elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003745 u32 tu, ms;
Ilan Peer852a07c2021-11-29 15:32:35 +02003746
3747 cfg80211_assoc_comeback(sdata->dev, assoc_data->bss,
3748 le32_to_cpu(elems->timeout_int->value));
3749
Johannes Berg5d248282021-09-20 15:40:10 +02003750 tu = le32_to_cpu(elems->timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01003751 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003752 sdata_info(sdata,
3753 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3754 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01003755 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01003756 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003757 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003758 run_again(sdata, assoc_data->timeout);
Johannes Berg15fae342021-06-18 13:41:55 +03003759 goto notify_driver;
Johannes Berg66e67e42012-01-20 13:55:27 +01003760 }
3761
Johannes Berg66e67e42012-01-20 13:55:27 +01003762 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003763 sdata_info(sdata, "%pM denied association (code=%d)\n",
3764 mgmt->sa, status_code);
Johannes Berge6f462d2016-12-08 17:22:09 +01003765 ieee80211_destroy_assoc_data(sdata, false, false);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003766 event.u.mlme.status = MLME_DENIED;
3767 event.u.mlme.reason = status_code;
3768 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01003769 } else {
Johannes Berg5d248282021-09-20 15:40:10 +02003770 if (!ieee80211_assoc_success(sdata, cbss, mgmt, len, elems)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003771 /* oops -- internal error -- send timeout for now */
Johannes Berge6f462d2016-12-08 17:22:09 +01003772 ieee80211_destroy_assoc_data(sdata, false, false);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003773 cfg80211_assoc_timeout(sdata->dev, cbss);
Johannes Berg15fae342021-06-18 13:41:55 +03003774 goto notify_driver;
Johannes Berg66e67e42012-01-20 13:55:27 +01003775 }
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003776 event.u.mlme.status = MLME_SUCCESS;
3777 drv_event_callback(sdata->local, sdata, &event);
John W. Linville635d9992012-07-09 16:34:34 -04003778 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01003779
3780 /*
3781 * destroy assoc_data afterwards, as otherwise an idle
3782 * recalc after assoc_data is NULL but before associated
3783 * is set can cause the interface to go idle
3784 */
Johannes Berge6f462d2016-12-08 17:22:09 +01003785 ieee80211_destroy_assoc_data(sdata, true, false);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003786
3787 /* get uapsd queues configuration */
3788 uapsd_queues = 0;
3789 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3790 if (sdata->tx_conf[ac].uapsd)
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03003791 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
Johannes Berg15fae342021-06-18 13:41:55 +03003792
3793 info.success = 1;
Johannes Berg66e67e42012-01-20 13:55:27 +01003794 }
3795
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003796 cfg80211_rx_assoc_resp(sdata->dev, cbss, (u8 *)mgmt, len, uapsd_queues,
Jouni Malinen4d9ec732019-02-15 02:14:33 +02003797 ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
Johannes Berg15fae342021-06-18 13:41:55 +03003798notify_driver:
3799 drv_mgd_complete_tx(sdata->local, sdata, &info);
Johannes Berg5d248282021-09-20 15:40:10 +02003800 kfree(elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01003801}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003802
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003803static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003804 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003805 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003806{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003807 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02003808 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01003809 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01003810
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003811 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01003812
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003813 channel = ieee80211_get_channel_khz(local->hw.wiphy,
3814 ieee80211_rx_status_to_khz(rx_status));
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02003815 if (!channel)
Johannes Berg5bda6172008-09-08 11:05:10 +02003816 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003817
Sara Sharon4abb52a2019-01-16 12:14:41 +02003818 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03003819 if (bss) {
Alexander Bondar817cee72013-05-19 14:23:57 +03003820 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
Johannes Bergd2722f82014-03-04 11:43:28 +01003821 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03003822 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003823}
3824
3825
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003826static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01003827 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003828{
Johannes Bergaf6b6372009-12-23 13:15:35 +01003829 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02003830 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003831 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003832 struct ieee80211_channel *channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003833 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03003834
Kalle Valo15b7b062009-03-22 21:57:14 +02003835 ifmgd = &sdata->u.mgd;
3836
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003837 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003838
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003839 /*
3840 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3841 * "If a 6 GHz AP receives a Probe Request frame and responds with
3842 * a Probe Response frame [..], the Address 1 field of the Probe
3843 * Response frame shall be set to the broadcast address [..]"
3844 * So, on 6GHz band we should also accept broadcast responses.
3845 */
3846 channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3847 rx_status->freq);
3848 if (!channel)
3849 return;
3850
3851 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3852 (channel->band != NL80211_BAND_6GHZ ||
3853 !is_broadcast_ether_addr(mgmt->da)))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03003854 return; /* ignore ProbeResp to foreign address */
3855
Ester Kummerae6a44e2008-06-27 18:54:48 +03003856 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3857 if (baselen > len)
3858 return;
3859
Sara Sharon4abb52a2019-01-16 12:14:41 +02003860 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003861
Johannes Berg77fdaa12009-07-07 03:45:17 +02003862 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00003863 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003864 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003865}
3866
Johannes Bergd91f36d2009-04-16 13:17:26 +02003867/*
3868 * This is the canonical list of information elements we care about,
3869 * the filter code also gives us all changes to the Microsoft OUI
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07003870 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3871 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3872 * changes to requested client power.
Johannes Bergd91f36d2009-04-16 13:17:26 +02003873 *
3874 * We implement beacon filtering in software since that means we can
3875 * avoid processing the frame here and in cfg80211, and userspace
3876 * will not be able to tell whether the hardware supports it or not.
3877 *
3878 * XXX: This list needs to be dynamic -- userspace needs to be able to
3879 * add items it requires. It also needs to be able to tell us to
3880 * look out for other vendor IEs.
3881 */
3882static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02003883 (1ULL << WLAN_EID_COUNTRY) |
3884 (1ULL << WLAN_EID_ERP_INFO) |
3885 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3886 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3887 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg70a3fd62015-03-12 08:53:29 +02003888 (1ULL << WLAN_EID_HT_OPERATION) |
3889 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
Johannes Bergd91f36d2009-04-16 13:17:26 +02003890
Tosoni1ad22fb2018-03-14 16:58:34 +00003891static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3892 struct ieee80211_if_managed *ifmgd,
3893 struct ieee80211_bss_conf *bss_conf,
3894 struct ieee80211_local *local,
3895 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003896{
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003897 /* Track average RSSI from the Beacon frames of the current AP */
Tosoni1ad22fb2018-03-14 16:58:34 +00003898
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003899 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3900 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Johannes Berg338c17a2015-08-28 10:52:54 +02003901 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003902 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003903 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003904 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003905 } else {
Jouni Malinen391a2002010-08-27 22:22:00 +03003906 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003907 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003908
Johannes Berg338c17a2015-08-28 10:52:54 +02003909 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3910
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003911 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3912 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003913 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003914 int last_sig = ifmgd->last_ave_beacon_signal;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003915 struct ieee80211_event event = {
3916 .type = RSSI_EVENT,
3917 };
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003918
3919 /*
3920 * if signal crosses either of the boundaries, invoke callback
3921 * with appropriate parameters
3922 */
3923 if (sig > ifmgd->rssi_max_thold &&
3924 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3925 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003926 event.u.rssi.data = RSSI_EVENT_HIGH;
3927 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003928 } else if (sig < ifmgd->rssi_min_thold &&
3929 (last_sig >= ifmgd->rssi_max_thold ||
3930 last_sig == 0)) {
3931 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003932 event.u.rssi.data = RSSI_EVENT_LOW;
3933 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003934 }
3935 }
3936
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003937 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003938 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003939 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003940 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003941 int last_event = ifmgd->last_cqm_event_signal;
3942 int thold = bss_conf->cqm_rssi_thold;
3943 int hyst = bss_conf->cqm_rssi_hyst;
Johannes Berg338c17a2015-08-28 10:52:54 +02003944
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003945 if (sig < thold &&
3946 (last_event == 0 || sig < last_event - hyst)) {
3947 ifmgd->last_cqm_event_signal = sig;
3948 ieee80211_cqm_rssi_notify(
3949 &sdata->vif,
3950 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003951 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003952 } else if (sig > thold &&
3953 (last_event == 0 || sig > last_event + hyst)) {
3954 ifmgd->last_cqm_event_signal = sig;
3955 ieee80211_cqm_rssi_notify(
3956 &sdata->vif,
3957 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003958 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003959 }
3960 }
3961
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003962 if (bss_conf->cqm_rssi_low &&
3963 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3964 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3965 int last_event = ifmgd->last_cqm_event_signal;
3966 int low = bss_conf->cqm_rssi_low;
3967 int high = bss_conf->cqm_rssi_high;
3968
3969 if (sig < low &&
3970 (last_event == 0 || last_event >= low)) {
3971 ifmgd->last_cqm_event_signal = sig;
3972 ieee80211_cqm_rssi_notify(
3973 &sdata->vif,
3974 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3975 sig, GFP_KERNEL);
3976 } else if (sig > high &&
3977 (last_event == 0 || last_event <= high)) {
3978 ifmgd->last_cqm_event_signal = sig;
3979 ieee80211_cqm_rssi_notify(
3980 &sdata->vif,
3981 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3982 sig, GFP_KERNEL);
3983 }
3984 }
Tosoni1ad22fb2018-03-14 16:58:34 +00003985}
3986
Sara Sharon78ac51f2019-01-16 18:22:56 +02003987static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3988 struct cfg80211_bss *bss)
3989{
3990 if (ether_addr_equal(tx_bssid, bss->bssid))
3991 return true;
3992 if (!bss->transmitted_bss)
3993 return false;
3994 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3995}
3996
Tosoni1ad22fb2018-03-14 16:58:34 +00003997static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003998 struct ieee80211_hdr *hdr, size_t len,
Tosoni1ad22fb2018-03-14 16:58:34 +00003999 struct ieee80211_rx_status *rx_status)
4000{
4001 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4002 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004003 struct ieee80211_mgmt *mgmt = (void *) hdr;
Tosoni1ad22fb2018-03-14 16:58:34 +00004004 size_t baselen;
Johannes Berg5d248282021-09-20 15:40:10 +02004005 struct ieee802_11_elems *elems;
Tosoni1ad22fb2018-03-14 16:58:34 +00004006 struct ieee80211_local *local = sdata->local;
4007 struct ieee80211_chanctx_conf *chanctx_conf;
4008 struct ieee80211_channel *chan;
4009 struct sta_info *sta;
4010 u32 changed = 0;
4011 bool erp_valid;
4012 u8 erp_value = 0;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004013 u32 ncrc = 0;
4014 u8 *bssid, *variable = mgmt->u.beacon.variable;
Tosoni1ad22fb2018-03-14 16:58:34 +00004015 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
4016
4017 sdata_assert_lock(sdata);
4018
4019 /* Process beacon from the current BSS */
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004020 bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
4021 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
4022 struct ieee80211_ext *ext = (void *) mgmt;
4023
4024 if (ieee80211_is_s1g_short_beacon(ext->frame_control))
4025 variable = ext->u.s1g_short_beacon.variable;
4026 else
4027 variable = ext->u.s1g_beacon.variable;
4028 }
4029
4030 baselen = (u8 *) variable - (u8 *) mgmt;
Tosoni1ad22fb2018-03-14 16:58:34 +00004031 if (baselen > len)
4032 return;
4033
4034 rcu_read_lock();
4035 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4036 if (!chanctx_conf) {
4037 rcu_read_unlock();
4038 return;
4039 }
4040
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07004041 if (ieee80211_rx_status_to_khz(rx_status) !=
4042 ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00004043 rcu_read_unlock();
4044 return;
4045 }
4046 chan = chanctx_conf->def.chan;
4047 rcu_read_unlock();
4048
4049 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004050 ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->bss)) {
Johannes Berg5d248282021-09-20 15:40:10 +02004051 elems = ieee802_11_parse_elems(variable, len - baselen, false,
4052 bssid,
4053 ifmgd->assoc_data->bss->bssid);
4054 if (!elems)
4055 return;
Tosoni1ad22fb2018-03-14 16:58:34 +00004056
Sara Sharon4abb52a2019-01-16 12:14:41 +02004057 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Sara Sharon78ac51f2019-01-16 18:22:56 +02004058
Johannes Berg5d248282021-09-20 15:40:10 +02004059 if (elems->dtim_period)
4060 ifmgd->dtim_period = elems->dtim_period;
Tosoni1ad22fb2018-03-14 16:58:34 +00004061 ifmgd->have_beacon = true;
4062 ifmgd->assoc_data->need_beacon = false;
4063 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
4064 sdata->vif.bss_conf.sync_tsf =
4065 le64_to_cpu(mgmt->u.beacon.timestamp);
4066 sdata->vif.bss_conf.sync_device_ts =
4067 rx_status->device_timestamp;
Johannes Berg5d248282021-09-20 15:40:10 +02004068 sdata->vif.bss_conf.sync_dtim_count = elems->dtim_count;
Tosoni1ad22fb2018-03-14 16:58:34 +00004069 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02004070
Johannes Berg5d248282021-09-20 15:40:10 +02004071 if (elems->mbssid_config_ie)
Sara Sharon78ac51f2019-01-16 18:22:56 +02004072 bss_conf->profile_periodicity =
Johannes Berg5d248282021-09-20 15:40:10 +02004073 elems->mbssid_config_ie->profile_periodicity;
Johannes Bergbbc6f032021-06-18 13:41:49 +03004074 else
4075 bss_conf->profile_periodicity = 0;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004076
Johannes Berg5d248282021-09-20 15:40:10 +02004077 if (elems->ext_capab_len >= 11 &&
4078 (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
Sara Sharon78ac51f2019-01-16 18:22:56 +02004079 bss_conf->ema_ap = true;
Johannes Bergbbc6f032021-06-18 13:41:49 +03004080 else
4081 bss_conf->ema_ap = false;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004082
Tosoni1ad22fb2018-03-14 16:58:34 +00004083 /* continue assoc process */
4084 ifmgd->assoc_data->timeout = jiffies;
4085 ifmgd->assoc_data->timeout_started = true;
4086 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg5d248282021-09-20 15:40:10 +02004087 kfree(elems);
Tosoni1ad22fb2018-03-14 16:58:34 +00004088 return;
4089 }
4090
4091 if (!ifmgd->associated ||
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004092 !ieee80211_rx_our_beacon(bssid, ifmgd->associated))
Tosoni1ad22fb2018-03-14 16:58:34 +00004093 return;
4094 bssid = ifmgd->associated->bssid;
4095
4096 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4097 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
4098 local, rx_status);
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01004099
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004100 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004101 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01004102 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004103 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03004104 }
4105
Johannes Bergb291ba12009-07-10 15:29:03 +02004106 /*
4107 * Push the beacon loss detection into the future since
4108 * we are processing a beacon from the AP just now.
4109 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04004110 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02004111
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004112 /* TODO: CRC urrently not calculated on S1G Beacon Compatibility
4113 * element (which carries the beacon interval). Don't forget to add a
4114 * bit to care_about_ies[] above if mac80211 is interested in a
4115 * changing S1G element.
4116 */
4117 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4118 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
Johannes Berg5d248282021-09-20 15:40:10 +02004119 elems = ieee802_11_parse_elems_crc(variable, len - baselen,
4120 false, care_about_ies, ncrc,
4121 mgmt->bssid, bssid);
4122 if (!elems)
4123 return;
4124 ncrc = elems->crc;
Johannes Bergd91f36d2009-04-16 13:17:26 +02004125
Johannes Berg90d13e82015-09-24 16:13:07 +02004126 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Johannes Berg5d248282021-09-20 15:40:10 +02004127 ieee80211_check_tim(elems->tim, elems->tim_len, bss_conf->aid)) {
Johannes Berg90d13e82015-09-24 16:13:07 +02004128 if (local->hw.conf.dynamic_ps_timeout > 0) {
4129 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4130 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4131 ieee80211_hw_config(local,
4132 IEEE80211_CONF_CHANGE_PS);
Kalle Valo572e0012009-02-10 17:09:31 +02004133 }
Johannes Berg076cdcb2015-09-24 16:14:55 +02004134 ieee80211_send_nullfunc(local, sdata, false);
Johannes Berg90d13e82015-09-24 16:13:07 +02004135 } else if (!local->pspolling && sdata->u.mgd.powersave) {
4136 local->pspolling = true;
4137
4138 /*
4139 * Here is assumed that the driver will be
4140 * able to send ps-poll frame and receive a
4141 * response even though power save mode is
4142 * enabled, but some drivers might require
4143 * to disable power save here. This needs
4144 * to be investigated.
4145 */
4146 ieee80211_send_pspoll(local, sdata);
Vivek Natarajana97b77b2008-12-23 18:39:02 -08004147 }
4148 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02004149
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01004150 if (sdata->vif.p2p ||
4151 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004152 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01004153 int ret;
4154
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004155 ret = cfg80211_get_p2p_attr(variable,
Johannes Berg488dd7b2012-10-29 20:08:01 +01004156 len - baselen,
4157 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01004158 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004159 if (ret >= 2) {
4160 if (sdata->u.mgd.p2p_noa_index != noa.index) {
4161 /* valid noa_attr and index changed */
4162 sdata->u.mgd.p2p_noa_index = noa.index;
4163 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4164 changed |= BSS_CHANGED_P2P_PS;
4165 /*
4166 * make sure we update all information, the CRC
4167 * mechanism doesn't look at P2P attributes.
4168 */
4169 ifmgd->beacon_crc_valid = false;
4170 }
4171 } else if (sdata->u.mgd.p2p_noa_index != -1) {
4172 /* noa_attr not found and we had valid noa_attr before */
4173 sdata->u.mgd.p2p_noa_index = -1;
4174 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01004175 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01004176 ifmgd->beacon_crc_valid = false;
4177 }
4178 }
4179
Luciano Coelho0c21e632014-10-08 09:48:39 +03004180 if (ifmgd->csa_waiting_bcn)
4181 ieee80211_chswitch_post_beacon(sdata);
4182
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004183 /*
4184 * Update beacon timing and dtim count on every beacon appearance. This
4185 * will allow the driver to use the most updated values. Do it before
4186 * comparing this one with last received beacon.
4187 * IMPORTANT: These parameters would possibly be out of sync by the time
4188 * the driver will use them. The synchronized view is currently
4189 * guaranteed only in certain callbacks.
4190 */
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004191 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
4192 !ieee80211_is_s1g_beacon(hdr->frame_control)) {
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004193 sdata->vif.bss_conf.sync_tsf =
4194 le64_to_cpu(mgmt->u.beacon.timestamp);
4195 sdata->vif.bss_conf.sync_device_ts =
4196 rx_status->device_timestamp;
Johannes Berg5d248282021-09-20 15:40:10 +02004197 sdata->vif.bss_conf.sync_dtim_count = elems->dtim_count;
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004198 }
4199
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004200 if ((ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) ||
4201 ieee80211_is_s1g_short_beacon(mgmt->frame_control))
Johannes Berg5d248282021-09-20 15:40:10 +02004202 goto free;
Jouni Malinen30196672009-05-19 17:01:43 +03004203 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004204 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03004205
Sara Sharon4abb52a2019-01-16 12:14:41 +02004206 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Johannes Berg7d257452012-07-06 17:37:43 +02004207
Johannes Bergd70b7612013-08-23 15:48:48 +02004208 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004209 rx_status->device_timestamp,
Johannes Berg5d248282021-09-20 15:40:10 +02004210 elems, true);
Johannes Bergd70b7612013-08-23 15:48:48 +02004211
Johannes Berg095d81c2013-10-15 12:25:07 +02004212 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
Johannes Berg5d248282021-09-20 15:40:10 +02004213 ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
4214 elems->wmm_param_len,
4215 elems->mu_edca_param_set))
Johannes Berg7d257452012-07-06 17:37:43 +02004216 changed |= BSS_CHANGED_QOS;
4217
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004218 /*
4219 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01004220 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004221 */
Alexander Bondar989c6502013-05-16 17:34:17 +03004222 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004223 /* a few bogus AP send dtim_period = 0 or no TIM IE */
Johannes Berg5d248282021-09-20 15:40:10 +02004224 bss_conf->dtim_period = elems->dtim_period ?: 1;
Johannes Bergef429da2013-02-05 17:48:40 +01004225
Alexander Bondar989c6502013-05-16 17:34:17 +03004226 changed |= BSS_CHANGED_BEACON_INFO;
4227 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03004228
4229 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02004230 ieee80211_recalc_ps(local);
Alexander Bondar482a9c72013-06-03 17:29:33 +03004231 mutex_unlock(&local->iflist_mtx);
4232
Alexander Bondarce857882013-05-06 17:17:04 +03004233 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004234 }
4235
Johannes Berg5d248282021-09-20 15:40:10 +02004236 if (elems->erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02004237 erp_valid = true;
Johannes Berg5d248282021-09-20 15:40:10 +02004238 erp_value = elems->erp_info[0];
Johannes Berg7a5158e2008-10-08 10:59:33 +02004239 } else {
4240 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04004241 }
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004242
4243 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4244 changed |= ieee80211_handle_bss_capability(sdata,
4245 le16_to_cpu(mgmt->u.beacon.capab_info),
4246 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07004247
Johannes Berg11289582013-02-07 17:36:12 +01004248 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01004249 sta = sta_info_get(sdata, bssid);
4250
Johannes Berg5d248282021-09-20 15:40:10 +02004251 changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
John Crispinc9d32452019-05-28 13:49:47 +02004252
Johannes Berg5d248282021-09-20 15:40:10 +02004253 if (ieee80211_config_bw(sdata, sta, elems->ht_cap_elem,
4254 elems->vht_cap_elem, elems->ht_operation,
4255 elems->vht_operation, elems->he_operation,
4256 elems->s1g_oper, bssid, &changed)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004257 mutex_unlock(&local->sta_mtx);
Johannes Berg89f774e2016-01-25 15:46:36 +02004258 sdata_info(sdata,
4259 "failed to follow AP %pM bandwidth change, disconnect\n",
4260 bssid);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004261 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4262 WLAN_REASON_DEAUTH_LEAVING,
4263 true, deauth_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004264 ieee80211_report_disconnect(sdata, deauth_buf,
4265 sizeof(deauth_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02004266 WLAN_REASON_DEAUTH_LEAVING,
4267 false);
Johannes Berg5d248282021-09-20 15:40:10 +02004268 goto free;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004269 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01004270
Johannes Berg5d248282021-09-20 15:40:10 +02004271 if (sta && elems->opmode_notif)
4272 ieee80211_vht_handle_opmode(sdata, sta, *elems->opmode_notif,
Eyal Shapiracf1e05c2015-12-08 16:04:36 +02004273 rx_status->band);
Johannes Berg11289582013-02-07 17:36:12 +01004274 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02004275
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07004276 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
Johannes Berg5d248282021-09-20 15:40:10 +02004277 elems->country_elem,
4278 elems->country_elem_len,
4279 elems->pwr_constr_elem,
4280 elems->cisco_dtpc_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08004281
Johannes Berg471b3ef2007-12-28 14:32:58 +01004282 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg5d248282021-09-20 15:40:10 +02004283free:
4284 kfree(elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07004285}
4286
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004287void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
4288 struct sk_buff *skb)
4289{
4290 struct ieee80211_rx_status *rx_status;
4291 struct ieee80211_hdr *hdr;
4292 u16 fc;
4293
4294 rx_status = (struct ieee80211_rx_status *) skb->cb;
4295 hdr = (struct ieee80211_hdr *) skb->data;
4296 fc = le16_to_cpu(hdr->frame_control);
4297
4298 sdata_lock(sdata);
4299 switch (fc & IEEE80211_FCTL_STYPE) {
4300 case IEEE80211_STYPE_S1G_BEACON:
4301 ieee80211_rx_mgmt_beacon(sdata, hdr, skb->len, rx_status);
4302 break;
4303 }
4304 sdata_unlock(sdata);
4305}
4306
Johannes Berg1fa57d02010-06-10 10:21:32 +02004307void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4308 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07004309{
4310 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07004311 struct ieee80211_mgmt *mgmt;
4312 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004313 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07004314
Jiri Bencf0706e82007-05-05 11:45:53 -07004315 rx_status = (struct ieee80211_rx_status *) skb->cb;
4316 mgmt = (struct ieee80211_mgmt *) skb->data;
4317 fc = le16_to_cpu(mgmt->frame_control);
4318
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004319 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004320
Johannes Berg66e67e42012-01-20 13:55:27 +01004321 switch (fc & IEEE80211_FCTL_STYPE) {
4322 case IEEE80211_STYPE_BEACON:
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004323 ieee80211_rx_mgmt_beacon(sdata, (void *)mgmt,
4324 skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01004325 break;
4326 case IEEE80211_STYPE_PROBE_RESP:
4327 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4328 break;
4329 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004330 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004331 break;
4332 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004333 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004334 break;
4335 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004336 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004337 break;
4338 case IEEE80211_STYPE_ASSOC_RESP:
4339 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004340 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004341 break;
4342 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01004343 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg5d248282021-09-20 15:40:10 +02004344 struct ieee802_11_elems *elems;
4345
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004346 ies_len = skb->len -
4347 offsetof(struct ieee80211_mgmt,
4348 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01004349
4350 if (ies_len < 0)
4351 break;
4352
Sara Sharon4abb52a2019-01-16 12:14:41 +02004353 /* CSA IE cannot be overridden, no need for BSSID */
Johannes Berg5d248282021-09-20 15:40:10 +02004354 elems = ieee802_11_parse_elems(
4355 mgmt->u.action.u.chan_switch.variable,
4356 ies_len, true, mgmt->bssid, NULL);
Johannes Berg37799e52013-03-26 14:02:26 +01004357
Johannes Berg8223ac12021-10-01 21:11:08 +02004358 if (elems && !elems->parse_error)
4359 ieee80211_sta_process_chanswitch(sdata,
4360 rx_status->mactime,
4361 rx_status->device_timestamp,
4362 elems, false);
Johannes Berg5d248282021-09-20 15:40:10 +02004363 kfree(elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004364 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
Johannes Berg5d248282021-09-20 15:40:10 +02004365 struct ieee802_11_elems *elems;
4366
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004367 ies_len = skb->len -
4368 offsetof(struct ieee80211_mgmt,
4369 u.action.u.ext_chan_switch.variable);
4370
4371 if (ies_len < 0)
4372 break;
4373
Sara Sharon4abb52a2019-01-16 12:14:41 +02004374 /*
4375 * extended CSA IE can't be overridden, no need for
4376 * BSSID
4377 */
Johannes Berg5d248282021-09-20 15:40:10 +02004378 elems = ieee802_11_parse_elems(
4379 mgmt->u.action.u.ext_chan_switch.variable,
4380 ies_len, true, mgmt->bssid, NULL);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004381
Johannes Berg8223ac12021-10-01 21:11:08 +02004382 if (elems && !elems->parse_error) {
4383 /* for the handling code pretend it was an IE */
4384 elems->ext_chansw_ie =
4385 &mgmt->u.action.u.ext_chan_switch.data;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004386
Johannes Berg8223ac12021-10-01 21:11:08 +02004387 ieee80211_sta_process_chanswitch(sdata,
4388 rx_status->mactime,
4389 rx_status->device_timestamp,
4390 elems, false);
4391 }
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004392
Johannes Berg5d248282021-09-20 15:40:10 +02004393 kfree(elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004394 }
Johannes Berg37799e52013-03-26 14:02:26 +01004395 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004396 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004397 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07004398}
4399
Kees Cook34f11cd2017-10-16 16:35:49 -07004400static void ieee80211_sta_timer(struct timer_list *t)
Jiri Bencf0706e82007-05-05 11:45:53 -07004401{
4402 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004403 from_timer(sdata, t, u.mgd.timer);
Jiri Bencf0706e82007-05-05 11:45:53 -07004404
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01004405 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07004406}
4407
Naftali Goldstein7dd231e2021-04-09 12:40:14 +03004408void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4409 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004410{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004411 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004412
Johannes Berg37ad3882012-02-24 13:50:54 +01004413 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01004414 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01004415
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004416 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02004417 reason, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004418}
4419
Johannes Berg46cad4b2015-08-15 22:39:54 +03004420static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
Johannes Berg66e67e42012-01-20 13:55:27 +01004421{
4422 struct ieee80211_local *local = sdata->local;
4423 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4424 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004425 u32 tx_flags = 0;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004426 u16 trans = 1;
4427 u16 status = 0;
Johannes Berg15fae342021-06-18 13:41:55 +03004428 struct ieee80211_prep_tx_info info = {
4429 .subtype = IEEE80211_STYPE_AUTH,
4430 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004431
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004432 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004433
4434 if (WARN_ON_ONCE(!auth_data))
4435 return -EINVAL;
4436
Johannes Berg66e67e42012-01-20 13:55:27 +01004437 auth_data->tries++;
4438
4439 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004440 sdata_info(sdata, "authentication with %pM timed out\n",
4441 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004442
4443 /*
4444 * Most likely AP is not in the range so remove the
4445 * bss struct for that AP.
4446 */
4447 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4448
4449 return -ETIMEDOUT;
4450 }
4451
Ilan Peerd4e36e52018-04-20 13:49:25 +03004452 if (auth_data->algorithm == WLAN_AUTH_SAE)
Johannes Berg15fae342021-06-18 13:41:55 +03004453 info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
Ilan Peerd4e36e52018-04-20 13:49:25 +03004454
Johannes Berg15fae342021-06-18 13:41:55 +03004455 drv_mgd_prepare_tx(local, sdata, &info);
Johannes Berga1845fc2012-06-27 13:18:36 +02004456
Johannes Berg46cad4b2015-08-15 22:39:54 +03004457 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4458 auth_data->bss->bssid, auth_data->tries,
4459 IEEE80211_AUTH_MAX_TRIES);
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004460
Johannes Berg46cad4b2015-08-15 22:39:54 +03004461 auth_data->expected_transaction = 2;
Johannes Berg66e67e42012-01-20 13:55:27 +01004462
Johannes Berg46cad4b2015-08-15 22:39:54 +03004463 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4464 trans = auth_data->sae_trans;
4465 status = auth_data->sae_status;
4466 auth_data->expected_transaction = trans;
Johannes Berg66e67e42012-01-20 13:55:27 +01004467 }
4468
Johannes Berg46cad4b2015-08-15 22:39:54 +03004469 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4470 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4471 IEEE80211_TX_INTFL_MLME_CONN_TX;
4472
4473 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4474 auth_data->data, auth_data->data_len,
4475 auth_data->bss->bssid,
4476 auth_data->bss->bssid, NULL, 0, 0,
4477 tx_flags);
4478
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02004479 if (tx_flags == 0) {
Ilan Peer407879b2018-04-20 13:49:20 +03004480 if (auth_data->algorithm == WLAN_AUTH_SAE)
4481 auth_data->timeout = jiffies +
4482 IEEE80211_AUTH_TIMEOUT_SAE;
4483 else
4484 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004485 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004486 auth_data->timeout =
4487 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004488 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004489
Ilan Peer407879b2018-04-20 13:49:20 +03004490 auth_data->timeout_started = true;
4491 run_again(sdata, auth_data->timeout);
4492
Johannes Berg66e67e42012-01-20 13:55:27 +01004493 return 0;
4494}
4495
4496static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4497{
4498 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4499 struct ieee80211_local *local = sdata->local;
4500
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004501 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004502
Johannes Berg66e67e42012-01-20 13:55:27 +01004503 assoc_data->tries++;
4504 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004505 sdata_info(sdata, "association with %pM timed out\n",
4506 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004507
4508 /*
4509 * Most likely AP is not in the range so remove the
4510 * bss struct for that AP.
4511 */
4512 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4513
4514 return -ETIMEDOUT;
4515 }
4516
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004517 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4518 assoc_data->bss->bssid, assoc_data->tries,
4519 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01004520 ieee80211_send_assoc(sdata);
4521
Johannes Berg30686bf2015-06-02 21:39:54 +02004522 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004523 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004524 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004525 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01004526 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004527 assoc_data->timeout =
4528 round_jiffies_up(jiffies +
4529 IEEE80211_ASSOC_TIMEOUT_LONG);
4530 assoc_data->timeout_started = true;
4531 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004532 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004533
4534 return 0;
4535}
4536
Johannes Berg1672c0e32013-01-29 15:02:27 +01004537void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4538 __le16 fc, bool acked)
4539{
4540 struct ieee80211_local *local = sdata->local;
4541
4542 sdata->u.mgd.status_fc = fc;
4543 sdata->u.mgd.status_acked = acked;
4544 sdata->u.mgd.status_received = true;
4545
4546 ieee80211_queue_work(&local->hw, &sdata->work);
4547}
4548
Johannes Berg1fa57d02010-06-10 10:21:32 +02004549void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004550{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004551 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02004552 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004553
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004554 sdata_lock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004555
Johannes Berg1672c0e32013-01-29 15:02:27 +01004556 if (ifmgd->status_received) {
4557 __le16 fc = ifmgd->status_fc;
4558 bool status_acked = ifmgd->status_acked;
4559
4560 ifmgd->status_received = false;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004561 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004562 if (status_acked) {
Ilan Peer407879b2018-04-20 13:49:20 +03004563 if (ifmgd->auth_data->algorithm ==
4564 WLAN_AUTH_SAE)
4565 ifmgd->auth_data->timeout =
4566 jiffies +
4567 IEEE80211_AUTH_TIMEOUT_SAE;
4568 else
4569 ifmgd->auth_data->timeout =
4570 jiffies +
4571 IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004572 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004573 } else {
4574 ifmgd->auth_data->timeout = jiffies - 1;
4575 }
Johannes Berg89afe612013-02-13 15:39:57 +01004576 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004577 } else if (ifmgd->assoc_data &&
4578 (ieee80211_is_assoc_req(fc) ||
4579 ieee80211_is_reassoc_req(fc))) {
4580 if (status_acked) {
4581 ifmgd->assoc_data->timeout =
4582 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004583 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004584 } else {
4585 ifmgd->assoc_data->timeout = jiffies - 1;
4586 }
Johannes Berg89afe612013-02-13 15:39:57 +01004587 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004588 }
4589 }
4590
Johannes Berg89afe612013-02-13 15:39:57 +01004591 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004592 time_after(jiffies, ifmgd->auth_data->timeout)) {
4593 if (ifmgd->auth_data->done) {
4594 /*
4595 * ok ... we waited for assoc but userspace didn't,
4596 * so let's just kill the auth data
4597 */
4598 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg46cad4b2015-08-15 22:39:54 +03004599 } else if (ieee80211_auth(sdata)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004600 u8 bssid[ETH_ALEN];
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004601 struct ieee80211_event event = {
4602 .type = MLME_EVENT,
4603 .u.mlme.data = AUTH_EVENT,
4604 .u.mlme.status = MLME_TIMEOUT,
4605 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004606
4607 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4608
4609 ieee80211_destroy_auth_data(sdata, false);
4610
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004611 cfg80211_auth_timeout(sdata->dev, bssid);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004612 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004613 }
Johannes Berg89afe612013-02-13 15:39:57 +01004614 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004615 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004616
Johannes Berg89afe612013-02-13 15:39:57 +01004617 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004618 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03004619 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01004620 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02004621 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004622 struct ieee80211_event event = {
4623 .type = MLME_EVENT,
4624 .u.mlme.data = ASSOC_EVENT,
4625 .u.mlme.status = MLME_TIMEOUT,
4626 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004627
Johannes Berge6f462d2016-12-08 17:22:09 +01004628 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004629 cfg80211_assoc_timeout(sdata->dev, bss);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004630 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004631 }
Johannes Berg89afe612013-02-13 15:39:57 +01004632 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004633 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004634
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004635 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02004636 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03004637 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004638 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03004639
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004640 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004641
Johannes Berg30686bf2015-06-02 21:39:54 +02004642 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Ben Greear180205b2011-02-04 15:30:24 -08004643 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004644 else
Ben Greear180205b2011-02-04 15:30:24 -08004645 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004646
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004647 /* ACK received for nullfunc probing frame */
4648 if (!ifmgd->probe_send_count)
4649 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004650 else if (ifmgd->nullfunc_failed) {
4651 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004652 mlme_dbg(sdata,
4653 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4654 bssid, ifmgd->probe_send_count,
4655 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004656 ieee80211_mgd_probe_ap_send(sdata);
4657 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004658 mlme_dbg(sdata,
4659 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4660 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02004661 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004662 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4663 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004664 }
4665 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004666 run_again(sdata, ifmgd->probe_timeout);
Johannes Berg30686bf2015-06-02 21:39:54 +02004667 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004668 mlme_dbg(sdata,
4669 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4670 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02004671 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004672 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004673 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004674 mlme_dbg(sdata,
4675 "No probe response from AP %pM after %dms, try %d/%i\n",
4676 bssid, probe_wait_ms,
4677 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03004678 ieee80211_mgd_probe_ap_send(sdata);
4679 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02004680 /*
4681 * We actually lost the connection ... or did we?
4682 * Let's make sure!
4683 */
Johannes Berg89f774e2016-01-25 15:46:36 +02004684 mlme_dbg(sdata,
4685 "No probe response from AP %pM after %dms, disconnecting.\n",
4686 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004687
Johannes Berg95acac62011-07-12 12:30:59 +02004688 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004689 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02004690 }
4691 }
4692
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004693 sdata_unlock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004694}
Johannes Berg0a51b272008-09-08 17:44:25 +02004695
Kees Cook34f11cd2017-10-16 16:35:49 -07004696static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004697{
4698 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004699 from_timer(sdata, t, u.mgd.bcn_mon_timer);
Luciano Coelho0c21e632014-10-08 09:48:39 +03004700 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004701
Luciano Coelho0c21e632014-10-08 09:48:39 +03004702 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004703 return;
4704
Loic Poulain0b911112020-06-18 11:17:42 +02004705 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
4706 return;
4707
Johannes Berg682bd382013-01-29 13:13:50 +01004708 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004709 ieee80211_queue_work(&sdata->local->hw,
4710 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004711}
4712
Kees Cook34f11cd2017-10-16 16:35:49 -07004713static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004714{
4715 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004716 from_timer(sdata, t, u.mgd.conn_mon_timer);
Johannes Bergb291ba12009-07-10 15:29:03 +02004717 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4718 struct ieee80211_local *local = sdata->local;
Felix Fietkau9abf4e42020-09-08 14:36:56 +02004719 struct sta_info *sta;
4720 unsigned long timeout;
Johannes Bergb291ba12009-07-10 15:29:03 +02004721
Luciano Coelho0c21e632014-10-08 09:48:39 +03004722 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004723 return;
4724
Felix Fietkau9abf4e42020-09-08 14:36:56 +02004725 sta = sta_info_get(sdata, ifmgd->bssid);
4726 if (!sta)
4727 return;
4728
4729 timeout = sta->status_stats.last_ack;
4730 if (time_before(sta->status_stats.last_ack, sta->rx_stats.last_rx))
4731 timeout = sta->rx_stats.last_rx;
4732 timeout += IEEE80211_CONNECTION_IDLE_TIME;
4733
Ben Greear7d73cd92021-03-30 16:07:49 -07004734 /* If timeout is after now, then update timer to fire at
4735 * the later date, but do not actually probe at this time.
4736 */
4737 if (time_is_after_jiffies(timeout)) {
Felix Fietkau9abf4e42020-09-08 14:36:56 +02004738 mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
4739 return;
4740 }
4741
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04004742 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004743}
4744
4745static void ieee80211_sta_monitor_work(struct work_struct *work)
4746{
4747 struct ieee80211_sub_if_data *sdata =
4748 container_of(work, struct ieee80211_sub_if_data,
4749 u.mgd.monitor_work);
4750
4751 ieee80211_mgd_probe_ap(sdata, false);
4752}
4753
Johannes Berga1678f82008-09-11 00:01:47 +02004754static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4755{
Kalle Vaload935682009-04-19 08:47:19 +03004756 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02004757 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03004758
Johannes Bergb291ba12009-07-10 15:29:03 +02004759 /* let's probe the connection once */
Johannes Berg30686bf2015-06-02 21:39:54 +02004760 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Eliad Peller494f1fe2012-02-19 15:26:09 +02004761 ieee80211_queue_work(&sdata->local->hw,
4762 &sdata->u.mgd.monitor_work);
Kalle Vaload935682009-04-19 08:47:19 +03004763 }
Johannes Berga1678f82008-09-11 00:01:47 +02004764}
4765
Johannes Bergb8360ab2013-04-29 14:57:44 +02004766#ifdef CONFIG_PM
Johannes Berg1a1cb742014-03-19 09:55:55 +01004767void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4768{
4769 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4770 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4771
4772 sdata_lock(sdata);
4773
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004774 if (ifmgd->auth_data || ifmgd->assoc_data) {
4775 const u8 *bssid = ifmgd->auth_data ?
4776 ifmgd->auth_data->bss->bssid :
4777 ifmgd->assoc_data->bss->bssid;
4778
Johannes Berg1a1cb742014-03-19 09:55:55 +01004779 /*
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004780 * If we are trying to authenticate / associate while suspending,
4781 * cfg80211 won't know and won't actually abort those attempts,
4782 * thus we need to do that ourselves.
Johannes Berg1a1cb742014-03-19 09:55:55 +01004783 */
Johannes Berg4b08d1b2019-08-30 14:24:51 +03004784 ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
Johannes Berg1a1cb742014-03-19 09:55:55 +01004785 IEEE80211_STYPE_DEAUTH,
4786 WLAN_REASON_DEAUTH_LEAVING,
4787 false, frame_buf);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004788 if (ifmgd->assoc_data)
Johannes Berge6f462d2016-12-08 17:22:09 +01004789 ieee80211_destroy_assoc_data(sdata, false, true);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004790 if (ifmgd->auth_data)
4791 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg1a1cb742014-03-19 09:55:55 +01004792 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
Johannes Berg3bb02142020-12-06 14:54:42 +02004793 IEEE80211_DEAUTH_FRAME_LEN,
4794 false);
Johannes Berg1a1cb742014-03-19 09:55:55 +01004795 }
4796
Johannes Bergbe72afe2015-03-01 09:10:03 +02004797 /* This is a bit of a hack - we should find a better and more generic
4798 * solution to this. Normally when suspending, cfg80211 will in fact
4799 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4800 * auth (not so important) or assoc (this is the problem) process.
4801 *
4802 * As a consequence, it can happen that we are in the process of both
4803 * associating and suspending, and receive an association response
4804 * after cfg80211 has checked if it needs to disconnect, but before
4805 * we actually set the flag to drop incoming frames. This will then
4806 * cause the workqueue flush to process the association response in
4807 * the suspend, resulting in a successful association just before it
4808 * tries to remove the interface from the driver, which now though
4809 * has a channel context assigned ... this results in issues.
4810 *
4811 * To work around this (for now) simply deauth here again if we're
4812 * now connected.
4813 */
4814 if (ifmgd->associated && !sdata->local->wowlan) {
4815 u8 bssid[ETH_ALEN];
4816 struct cfg80211_deauth_request req = {
4817 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4818 .bssid = bssid,
4819 };
4820
4821 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4822 ieee80211_mgd_deauth(sdata, &req);
4823 }
4824
Johannes Berg1a1cb742014-03-19 09:55:55 +01004825 sdata_unlock(sdata);
4826}
4827
Johannes Bergb8360ab2013-04-29 14:57:44 +02004828void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4829{
4830 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4831
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004832 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004833 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004834 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004835 return;
4836 }
4837
4838 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4839 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4840 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4841 ieee80211_sta_connection_lost(sdata,
4842 ifmgd->associated->bssid,
4843 WLAN_REASON_UNSPECIFIED,
4844 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004845 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004846 return;
4847 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004848 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004849}
4850#endif
4851
Johannes Berg9c6bd792008-09-11 00:01:52 +02004852/* interface setup */
4853void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4854{
Johannes Berg46900292009-02-15 12:44:28 +01004855 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004856
Johannes Berg46900292009-02-15 12:44:28 +01004857 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004858 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01004859 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004860 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4861 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02004862 INIT_WORK(&ifmgd->csa_connection_drop_work,
4863 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03004864 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03004865 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4866 ieee80211_tdls_peer_del_work);
Kees Cook34f11cd2017-10-16 16:35:49 -07004867 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4868 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4869 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4870 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
Johannes Berg02219b32014-10-07 10:38:50 +03004871 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4872 ieee80211_sta_handle_tspec_ac_params_wk);
Johannes Berg9c6bd792008-09-11 00:01:52 +02004873
Johannes Bergab1faea2009-07-01 21:41:17 +02004874 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05304875 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02004876 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4877 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004878 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02004879
Eliad Peller0d8614b2014-09-10 14:07:36 +03004880 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
Johannes Berg0f782312009-12-01 13:37:02 +01004881 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4882 else
4883 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02004884
4885 /* Setup TDLS data */
4886 spin_lock_init(&ifmgd->teardown_lock);
4887 ifmgd->teardown_skb = NULL;
4888 ifmgd->orig_teardown_skb = NULL;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004889}
4890
4891/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02004892void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4893{
Johannes Berg31ee67a2012-07-06 21:18:24 +02004894 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02004895
4896 /* Restart STA timers */
4897 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07004898 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4899 if (ieee80211_sdata_running(sdata))
4900 ieee80211_restart_sta_timer(sdata);
4901 }
Johannes Berga1678f82008-09-11 00:01:47 +02004902 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02004903}
Johannes Berg10f644a2009-04-16 13:17:25 +02004904
Johannes Bergf2d9d272012-11-22 14:11:39 +01004905static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4906 struct cfg80211_bss *cbss)
4907{
4908 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb3c19062021-12-20 11:36:10 +01004909 const struct element *ht_cap_elem, *vht_cap_elem;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004910 const struct ieee80211_ht_cap *ht_cap;
4911 const struct ieee80211_vht_cap *vht_cap;
4912 u8 chains = 1;
4913
4914 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4915 return chains;
4916
Johannes Bergb3c19062021-12-20 11:36:10 +01004917 ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
4918 if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
4919 ht_cap = (void *)ht_cap_elem->data;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004920 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4921 /*
4922 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4923 * "Tx Unequal Modulation Supported" fields.
4924 */
4925 }
4926
4927 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4928 return chains;
4929
Johannes Bergb3c19062021-12-20 11:36:10 +01004930 vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
4931 if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01004932 u8 nss;
4933 u16 tx_mcs_map;
4934
Johannes Bergb3c19062021-12-20 11:36:10 +01004935 vht_cap = (void *)vht_cap_elem->data;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004936 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4937 for (nss = 8; nss > 0; nss--) {
4938 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4939 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4940 break;
4941 }
4942 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4943 chains = max(chains, nss);
4944 }
4945
4946 return chains;
4947}
4948
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004949static bool
Johannes Bergbac2fd32021-06-18 13:41:50 +03004950ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata,
4951 struct ieee80211_supported_band *sband,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004952 const struct ieee80211_he_operation *he_op)
4953{
4954 const struct ieee80211_sta_he_cap *sta_he_cap =
Johannes Bergbac2fd32021-06-18 13:41:50 +03004955 ieee80211_get_he_iftype_cap(sband,
4956 ieee80211_vif_type_p2p(&sdata->vif));
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004957 u16 ap_min_req_set;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004958 int i;
4959
4960 if (!sta_he_cap || !he_op)
4961 return false;
4962
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004963 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4964
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004965 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4966 for (i = 0; i < 3; i++) {
4967 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4968 &sta_he_cap->he_mcs_nss_supp;
4969 u16 sta_mcs_map_rx =
4970 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4971 u16 sta_mcs_map_tx =
4972 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4973 u8 nss;
4974 bool verified = true;
4975
4976 /*
4977 * For each band there is a maximum of 8 spatial streams
4978 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4979 * of 2 bits per NSS (1-8), with the values defined in enum
4980 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4981 * capabilities aren't less than the AP's minimum requirements
4982 * for this HE BSS per SS.
4983 * It is enough to find one such band that meets the reqs.
4984 */
4985 for (nss = 8; nss > 0; nss--) {
4986 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4987 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4988 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4989
4990 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4991 continue;
4992
4993 /*
4994 * Make sure the HE AP doesn't require MCSs that aren't
4995 * supported by the client
4996 */
4997 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4998 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4999 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
5000 verified = false;
5001 break;
5002 }
5003 }
5004
5005 if (verified)
5006 return true;
5007 }
5008
5009 /* If here, STA doesn't meet AP's HE min requirements */
5010 return false;
5011}
5012
Johannes Bergb17166a2012-07-27 11:41:27 +02005013static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
5014 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01005015{
5016 struct ieee80211_local *local = sdata->local;
5017 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Peller53b954e2014-07-24 11:20:05 +03005018 const struct ieee80211_ht_cap *ht_cap = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02005019 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01005020 const struct ieee80211_vht_operation *vht_oper = NULL;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005021 const struct ieee80211_he_operation *he_oper = NULL;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005022 const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02005023 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01005024 struct cfg80211_chan_def chandef;
Johannes Berg57fa5e82020-05-28 21:34:36 +02005025 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Wen Gong780a8c92020-09-11 10:29:02 +00005026 bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
Johannes Berg2a333a02020-05-28 21:34:35 +02005027 struct ieee80211_bss *bss = (void *)cbss->priv;
Wen Gong37123c32021-09-24 06:00:51 -04005028 struct ieee802_11_elems *elems;
5029 const struct cfg80211_bss_ies *ies;
Johannes Bergf2d9d272012-11-22 14:11:39 +01005030 int ret;
Arik Nemtsov52a45f32015-08-15 22:39:53 +03005031 u32 i;
5032 bool have_80mhz;
Johannes Berga1cf7752012-03-08 15:02:07 +01005033
Wen Gong37123c32021-09-24 06:00:51 -04005034 rcu_read_lock();
5035
5036 ies = rcu_dereference(cbss->ies);
5037 elems = ieee802_11_parse_elems(ies->data, ies->len, false,
5038 NULL, NULL);
5039 if (!elems) {
5040 rcu_read_unlock();
5041 return -ENOMEM;
5042 }
5043
Johannes Berg24398e32012-03-28 10:58:36 +02005044 sband = local->hw.wiphy->bands[cbss->channel->band];
5045
Johannes Bergf2d9d272012-11-22 14:11:39 +01005046 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
5047 IEEE80211_STA_DISABLE_80P80MHZ |
5048 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02005049
Johannes Berg75e296e2020-01-31 13:12:39 +02005050 /* disable HT/VHT/HE if we don't support them */
Johannes Berg57fa5e82020-05-28 21:34:36 +02005051 if (!sband->ht_cap.ht_supported && !is_6ghz) {
Johannes Berg636ccda2021-11-30 13:16:59 +02005052 mlme_dbg(sdata, "HT not supported, disabling HT/VHT/HE\n");
Johannes Berg75e296e2020-01-31 13:12:39 +02005053 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5054 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5055 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5056 }
5057
Wen Gong780a8c92020-09-11 10:29:02 +00005058 if (!sband->vht_cap.vht_supported && is_5ghz) {
Johannes Berg636ccda2021-11-30 13:16:59 +02005059 mlme_dbg(sdata, "VHT not supported, disabling VHT/HE\n");
Johannes Berg75e296e2020-01-31 13:12:39 +02005060 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg57fa5e82020-05-28 21:34:36 +02005061 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5062 }
Johannes Berg75e296e2020-01-31 13:12:39 +02005063
Johannes Bergbac2fd32021-06-18 13:41:50 +03005064 if (!ieee80211_get_he_iftype_cap(sband,
Johannes Berg636ccda2021-11-30 13:16:59 +02005065 ieee80211_vif_type_p2p(&sdata->vif))) {
5066 mlme_dbg(sdata, "HE not supported, disabling it\n");
Johannes Berg75e296e2020-01-31 13:12:39 +02005067 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg636ccda2021-11-30 13:16:59 +02005068 }
Johannes Berg75e296e2020-01-31 13:12:39 +02005069
Johannes Berg57fa5e82020-05-28 21:34:36 +02005070 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
Wen Gong37123c32021-09-24 06:00:51 -04005071 ht_oper = elems->ht_operation;
5072 ht_cap = elems->ht_cap_elem;
Eliad Peller53b954e2014-07-24 11:20:05 +03005073
5074 if (!ht_cap) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01005075 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5076 ht_oper = NULL;
5077 }
Johannes Berg24398e32012-03-28 10:58:36 +02005078 }
5079
Johannes Berg57fa5e82020-05-28 21:34:36 +02005080 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
Wen Gong37123c32021-09-24 06:00:51 -04005081 vht_oper = elems->vht_operation;
Johannes Bergf2d9d272012-11-22 14:11:39 +01005082 if (vht_oper && !ht_oper) {
5083 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005084 sdata_info(sdata,
Johannes Berg75e296e2020-01-31 13:12:39 +02005085 "AP advertised VHT without HT, disabling HT/VHT/HE\n");
Johannes Bergcb145022013-02-07 20:41:50 +01005086 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5087 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005088 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg24398e32012-03-28 10:58:36 +02005089 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01005090
Wen Gong37123c32021-09-24 06:00:51 -04005091 if (!elems->vht_cap_elem) {
Johannes Berg636ccda2021-11-30 13:16:59 +02005092 sdata_info(sdata,
5093 "bad VHT capabilities, disabling VHT\n");
Johannes Berg08e6effa2013-02-07 23:33:32 +01005094 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5095 vht_oper = NULL;
5096 }
Johannes Berg24398e32012-03-28 10:58:36 +02005097 }
5098
Shaul Triebitz002245e2018-12-15 11:03:19 +02005099 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Wen Gong37123c32021-09-24 06:00:51 -04005100 he_oper = elems->he_operation;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005101
Wen Gong63214f02021-09-24 06:00:52 -04005102 if (is_6ghz) {
5103 struct ieee80211_bss_conf *bss_conf;
5104 u8 i, j = 0;
5105
5106 bss_conf = &sdata->vif.bss_conf;
5107
5108 if (elems->pwr_constr_elem)
5109 bss_conf->pwr_reduction = *elems->pwr_constr_elem;
5110
5111 BUILD_BUG_ON(ARRAY_SIZE(bss_conf->tx_pwr_env) !=
5112 ARRAY_SIZE(elems->tx_pwr_env));
5113
5114 for (i = 0; i < elems->tx_pwr_env_num; i++) {
5115 if (elems->tx_pwr_env_len[i] >
5116 sizeof(bss_conf->tx_pwr_env[j]))
5117 continue;
5118
5119 bss_conf->tx_pwr_env_num++;
5120 memcpy(&bss_conf->tx_pwr_env[j], elems->tx_pwr_env[i],
5121 elems->tx_pwr_env_len[i]);
5122 j++;
5123 }
5124 }
5125
Johannes Bergbac2fd32021-06-18 13:41:50 +03005126 if (!ieee80211_verify_sta_he_mcs_support(sdata, sband, he_oper))
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005127 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5128 }
5129
Arik Nemtsov52a45f32015-08-15 22:39:53 +03005130 /* Allow VHT if at least one channel on the sband supports 80 MHz */
5131 have_80mhz = false;
5132 for (i = 0; i < sband->n_channels; i++) {
5133 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5134 IEEE80211_CHAN_NO_80MHZ))
5135 continue;
5136
5137 have_80mhz = true;
5138 break;
5139 }
5140
Johannes Berg636ccda2021-11-30 13:16:59 +02005141 if (!have_80mhz) {
5142 sdata_info(sdata, "80 MHz not supported, disabling VHT\n");
Arik Nemtsov52a45f32015-08-15 22:39:53 +03005143 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg636ccda2021-11-30 13:16:59 +02005144 }
Arik Nemtsov52a45f32015-08-15 22:39:53 +03005145
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005146 if (sband->band == NL80211_BAND_S1GHZ) {
Wen Gong37123c32021-09-24 06:00:51 -04005147 s1g_oper = elems->s1g_oper;
5148 if (!s1g_oper)
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005149 sdata_info(sdata,
5150 "AP missing S1G operation element?\n");
5151 }
5152
Johannes Bergf2d9d272012-11-22 14:11:39 +01005153 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
5154 cbss->channel,
Johannes Berg2a333a02020-05-28 21:34:35 +02005155 bss->vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005156 ht_oper, vht_oper, he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005157 s1g_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02005158 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02005159
Johannes Bergf2d9d272012-11-22 14:11:39 +01005160 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
5161 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02005162
Johannes Berg9caf0362012-11-29 01:25:20 +01005163 rcu_read_unlock();
Wen Gong37123c32021-09-24 06:00:51 -04005164 /* the element data was RCU protected so no longer valid anyway */
5165 kfree(elems);
5166 elems = NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +01005167
Johannes Berg57fa5e82020-05-28 21:34:36 +02005168 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
5169 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
5170 return -EINVAL;
5171 }
5172
Johannes Berg04ecd252012-09-11 14:34:12 +02005173 /* will change later if needed */
5174 sdata->smps_mode = IEEE80211_SMPS_OFF;
5175
Johannes Berg34a37402013-12-18 09:43:33 +01005176 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01005177 /*
5178 * If this fails (possibly due to channel context sharing
5179 * on incompatible channels, e.g. 80+80 and 160 sharing the
5180 * same control channel) try to use a smaller bandwidth.
5181 */
5182 ret = ieee80211_vif_use_channel(sdata, &chandef,
5183 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02005184
5185 /* don't downgrade for 5 and 10 MHz channels, though. */
5186 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
5187 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01005188 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02005189
Johannes Bergd601cd82013-02-07 20:54:51 +01005190 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02005191 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01005192 ret = ieee80211_vif_use_channel(sdata, &chandef,
5193 IEEE80211_CHANCTX_SHARED);
5194 }
Johannes Berg34a37402013-12-18 09:43:33 +01005195 out:
5196 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01005197 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02005198}
5199
Sara Sharon78ac51f2019-01-16 18:22:56 +02005200static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5201 u8 *dtim_count, u8 *dtim_period)
5202{
5203 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5204 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5205 ies->len);
5206 const struct ieee80211_tim_ie *tim = NULL;
5207 const struct ieee80211_bssid_index *idx;
5208 bool valid = tim_ie && tim_ie[1] >= 2;
5209
5210 if (valid)
5211 tim = (void *)(tim_ie + 2);
5212
5213 if (dtim_count)
5214 *dtim_count = valid ? tim->dtim_count : 0;
5215
5216 if (dtim_period)
5217 *dtim_period = valid ? tim->dtim_period : 0;
5218
5219 /* Check if value is overridden by non-transmitted profile */
5220 if (!idx_ie || idx_ie[1] < 3)
5221 return valid;
5222
5223 idx = (void *)(idx_ie + 2);
5224
5225 if (dtim_count)
5226 *dtim_count = idx->dtim_count;
5227
5228 if (dtim_period)
5229 *dtim_period = idx->dtim_period;
5230
5231 return true;
5232}
5233
Johannes Bergb17166a2012-07-27 11:41:27 +02005234static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005235 struct cfg80211_bss *cbss, bool assoc,
5236 bool override)
Johannes Bergb17166a2012-07-27 11:41:27 +02005237{
5238 struct ieee80211_local *local = sdata->local;
5239 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5240 struct ieee80211_bss *bss = (void *)cbss->priv;
5241 struct sta_info *new_sta = NULL;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005242 struct ieee80211_supported_band *sband;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005243 bool have_sta = false;
Johannes Bergb17166a2012-07-27 11:41:27 +02005244 int err;
5245
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005246 sband = local->hw.wiphy->bands[cbss->channel->band];
5247
Johannes Bergb17166a2012-07-27 11:41:27 +02005248 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5249 return -EINVAL;
5250
Luca Coelhof8860ce2017-05-02 17:56:21 +03005251 /* If a reconfig is happening, bail out */
5252 if (local->in_reconfig)
5253 return -EBUSY;
5254
Johannes Bergb17166a2012-07-27 11:41:27 +02005255 if (assoc) {
5256 rcu_read_lock();
5257 have_sta = sta_info_get(sdata, cbss->bssid);
5258 rcu_read_unlock();
5259 }
5260
5261 if (!have_sta) {
5262 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5263 if (!new_sta)
5264 return -ENOMEM;
5265 }
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005266
Johannes Bergc87905b2017-06-10 13:52:43 +03005267 /*
5268 * Set up the information for the new channel before setting the
5269 * new channel. We can't - completely race-free - change the basic
5270 * rates bitmap and the channel (sband) that it refers to, but if
5271 * we set it up before we at least avoid calling into the driver's
5272 * bss_info_changed() method with invalid information (since we do
5273 * call that from changing the channel - only for IDLE and perhaps
5274 * some others, but ...).
5275 *
5276 * So to avoid that, just set up all the new information before the
5277 * channel, but tell the driver to apply it only afterwards, since
5278 * it might need the new channel for that.
5279 */
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005280 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005281 u32 rates = 0, basic_rates = 0;
Tom Rix68a18ad2021-12-23 08:28:48 -08005282 bool have_higher_than_11mbit = false;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005283 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005284 const struct cfg80211_bss_ies *ies;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005285 int shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005286
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005287 /* TODO: S1G Basic Rate Set is expressed elsewhere */
Thomas Pedersen12bf8fa2020-10-05 09:45:22 -07005288 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5289 ieee80211_s1g_sta_rate_init(new_sta);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005290 goto skip_rates;
Thomas Pedersen12bf8fa2020-10-05 09:45:22 -07005291 }
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005292
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005293 ieee80211_get_rates(sband, bss->supp_rates,
5294 bss->supp_rates_len,
5295 &rates, &basic_rates,
5296 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02005297 &min_rate, &min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03005298 shift);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005299
5300 /*
5301 * This used to be a workaround for basic rates missing
5302 * in the association response frame. Now that we no
5303 * longer use the basic rates from there, it probably
5304 * doesn't happen any more, but keep the workaround so
5305 * in case some *other* APs are buggy in different ways
5306 * we can connect -- with a warning.
Ilan Peer302ff8b2020-03-26 15:09:39 +02005307 * Allow this workaround only in case the AP provided at least
5308 * one rate.
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005309 */
Ilan Peer302ff8b2020-03-26 15:09:39 +02005310 if (min_rate_index < 0) {
5311 sdata_info(sdata,
5312 "No legacy rates in association response\n");
5313
5314 sta_info_free(local, new_sta);
5315 return -EINVAL;
5316 } else if (!basic_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005317 sdata_info(sdata,
5318 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005319 basic_rates = BIT(min_rate_index);
5320 }
5321
Johannes Bergbd718fc2019-05-29 15:25:35 +03005322 if (rates)
5323 new_sta->sta.supp_rates[cbss->channel->band] = rates;
5324 else
5325 sdata_info(sdata,
5326 "No rates found, keeping mandatory only\n");
5327
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005328 sdata->vif.bss_conf.basic_rates = basic_rates;
5329
5330 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg57fbcce2016-04-12 15:56:15 +02005331 if (cbss->channel->band == NL80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005332 have_higher_than_11mbit)
5333 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5334 else
5335 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5336
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005337skip_rates:
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005338 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5339
Johannes Berg8c358bc2012-05-22 22:13:05 +02005340 /* set timing information */
5341 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005342 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01005343 ies = rcu_dereference(cbss->beacon_ies);
5344 if (ies) {
Johannes Bergef429da2013-02-05 17:48:40 +01005345 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5346 sdata->vif.bss_conf.sync_device_ts =
5347 bss->device_ts_beacon;
Sara Sharon78ac51f2019-01-16 18:22:56 +02005348
5349 ieee80211_get_dtim(ies,
5350 &sdata->vif.bss_conf.sync_dtim_count,
5351 NULL);
Johannes Berg30686bf2015-06-02 21:39:54 +02005352 } else if (!ieee80211_hw_check(&sdata->local->hw,
5353 TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005354 ies = rcu_dereference(cbss->proberesp_ies);
5355 /* must be non-NULL since beacon IEs were NULL */
5356 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5357 sdata->vif.bss_conf.sync_device_ts =
5358 bss->device_ts_presp;
5359 sdata->vif.bss_conf.sync_dtim_count = 0;
5360 } else {
5361 sdata->vif.bss_conf.sync_tsf = 0;
5362 sdata->vif.bss_conf.sync_device_ts = 0;
5363 sdata->vif.bss_conf.sync_dtim_count = 0;
5364 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01005365 rcu_read_unlock();
Johannes Bergc87905b2017-06-10 13:52:43 +03005366 }
Johannes Berg8c358bc2012-05-22 22:13:05 +02005367
Johannes Bergc87905b2017-06-10 13:52:43 +03005368 if (new_sta || override) {
5369 err = ieee80211_prep_channel(sdata, cbss);
5370 if (err) {
5371 if (new_sta)
5372 sta_info_free(local, new_sta);
5373 return -EINVAL;
5374 }
5375 }
5376
5377 if (new_sta) {
5378 /*
5379 * tell driver about BSSID, basic rates and timing
5380 * this was set up above, before setting the channel
5381 */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005382 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02005383 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5384 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01005385
5386 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005387 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01005388
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005389 err = sta_info_insert(new_sta);
5390 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01005391 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005392 sdata_info(sdata,
5393 "failed to insert STA entry for the AP (error %d)\n",
5394 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01005395 return err;
5396 }
5397 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00005398 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01005399
David Spinadel7d830a12015-03-17 19:58:38 +02005400 /* Cancel scan to ensure that nothing interferes with connection */
5401 if (local->scanning)
5402 ieee80211_scan_cancel(local);
5403
Johannes Berga1cf7752012-03-08 15:02:07 +01005404 return 0;
5405}
5406
Johannes Berg77fdaa12009-07-07 03:45:17 +02005407/* config hooks */
5408int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5409 struct cfg80211_auth_request *req)
5410{
Johannes Berg66e67e42012-01-20 13:55:27 +01005411 struct ieee80211_local *local = sdata->local;
5412 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5413 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005414 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01005415 int err;
Jouni Malinenefb543e2018-10-11 00:21:21 +03005416 bool cont_auth;
Johannes Berg66e67e42012-01-20 13:55:27 +01005417
5418 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02005419
5420 switch (req->auth_type) {
5421 case NL80211_AUTHTYPE_OPEN_SYSTEM:
5422 auth_alg = WLAN_AUTH_OPEN;
5423 break;
5424 case NL80211_AUTHTYPE_SHARED_KEY:
Ard Biesheuvel5fdb3732019-06-12 18:19:54 +02005425 if (fips_enabled)
Johannes Berg9dca9c42010-07-21 10:09:25 +02005426 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005427 auth_alg = WLAN_AUTH_SHARED_KEY;
5428 break;
5429 case NL80211_AUTHTYPE_FT:
5430 auth_alg = WLAN_AUTH_FT;
5431 break;
5432 case NL80211_AUTHTYPE_NETWORK_EAP:
5433 auth_alg = WLAN_AUTH_LEAP;
5434 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005435 case NL80211_AUTHTYPE_SAE:
5436 auth_alg = WLAN_AUTH_SAE;
5437 break;
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03005438 case NL80211_AUTHTYPE_FILS_SK:
5439 auth_alg = WLAN_AUTH_FILS_SK;
5440 break;
5441 case NL80211_AUTHTYPE_FILS_SK_PFS:
5442 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5443 break;
5444 case NL80211_AUTHTYPE_FILS_PK:
5445 auth_alg = WLAN_AUTH_FILS_PK;
5446 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005447 default:
5448 return -EOPNOTSUPP;
5449 }
5450
Jouni Malinenefb543e2018-10-11 00:21:21 +03005451 if (ifmgd->assoc_data)
Jouni Malinen8d7432a2018-10-11 00:21:20 +03005452 return -EBUSY;
5453
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005454 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005455 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01005456 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005457 return -ENOMEM;
5458
Johannes Berg66e67e42012-01-20 13:55:27 +01005459 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005460
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005461 if (req->auth_data_len >= 4) {
Jouni Malinen6ec63612016-10-27 00:41:59 +03005462 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5463 __le16 *pos = (__le16 *) req->auth_data;
5464
5465 auth_data->sae_trans = le16_to_cpu(pos[0]);
5466 auth_data->sae_status = le16_to_cpu(pos[1]);
5467 }
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005468 memcpy(auth_data->data, req->auth_data + 4,
5469 req->auth_data_len - 4);
5470 auth_data->data_len += req->auth_data_len - 4;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005471 }
5472
Jouni Malinenefb543e2018-10-11 00:21:21 +03005473 /* Check if continuing authentication or trying to authenticate with the
5474 * same BSS that we were in the process of authenticating with and avoid
5475 * removal and re-addition of the STA entry in
5476 * ieee80211_prep_connection().
5477 */
5478 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5479
Johannes Berg77fdaa12009-07-07 03:45:17 +02005480 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005481 memcpy(&auth_data->data[auth_data->data_len],
5482 req->ie, req->ie_len);
5483 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005484 }
5485
Johannes Bergfffd0932009-07-08 14:22:54 +02005486 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005487 auth_data->key_len = req->key_len;
5488 auth_data->key_idx = req->key_idx;
5489 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02005490 }
5491
Johannes Berg66e67e42012-01-20 13:55:27 +01005492 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005493
Johannes Berg66e67e42012-01-20 13:55:27 +01005494 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01005495
Jouni Malinenefb543e2018-10-11 00:21:21 +03005496 if (ifmgd->auth_data) {
5497 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5498 auth_data->peer_confirmed =
5499 ifmgd->auth_data->peer_confirmed;
5500 }
5501 ieee80211_destroy_auth_data(sdata, cont_auth);
5502 }
Guy Eilam2a33bee2011-08-17 15:18:15 +03005503
Johannes Berg66e67e42012-01-20 13:55:27 +01005504 /* prep auth_data so we don't go into idle on disassoc */
5505 ifmgd->auth_data = auth_data;
5506
Jouni Malinenefb543e2018-10-11 00:21:21 +03005507 /* If this is continuation of an ongoing SAE authentication exchange
5508 * (i.e., request to send SAE Confirm) and the peer has already
5509 * confirmed, mark authentication completed since we are about to send
5510 * out SAE Confirm.
5511 */
5512 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5513 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5514 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5515
Johannes Berg7b119dc2013-04-10 21:38:36 +02005516 if (ifmgd->associated) {
5517 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5518
Johannes Berg89f774e2016-01-25 15:46:36 +02005519 sdata_info(sdata,
5520 "disconnect from AP %pM for new auth to %pM\n",
5521 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005522 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5523 WLAN_REASON_UNSPECIFIED,
5524 false, frame_buf);
5525
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005526 ieee80211_report_disconnect(sdata, frame_buf,
5527 sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02005528 WLAN_REASON_UNSPECIFIED,
5529 false);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005530 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005531
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005532 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005533
Jouni Malinenefb543e2018-10-11 00:21:21 +03005534 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Johannes Berga1cf7752012-03-08 15:02:07 +01005535 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01005536 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005537
Johannes Berg46cad4b2015-08-15 22:39:54 +03005538 err = ieee80211_auth(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01005539 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005540 sta_info_destroy_addr(sdata, req->bss->bssid);
5541 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005542 }
5543
Johannes Berg66e67e42012-01-20 13:55:27 +01005544 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01005545 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005546 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02005547
Johannes Berg66e67e42012-01-20 13:55:27 +01005548 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005549 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005550 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005551 ifmgd->auth_data = NULL;
Michal Kaziord01f8582015-06-03 08:36:13 +02005552 mutex_lock(&sdata->local->mtx);
5553 ieee80211_vif_release_channel(sdata);
5554 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01005555 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005556 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005557}
5558
Johannes Berg77fdaa12009-07-07 03:45:17 +02005559int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5560 struct cfg80211_assoc_request *req)
5561{
Johannes Berg57fa5e82020-05-28 21:34:36 +02005562 bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
Johannes Bergc2f46812020-10-13 14:01:57 +02005563 bool is_5ghz = req->bss->channel->band == NL80211_BAND_5GHZ;
Johannes Berg66e67e42012-01-20 13:55:27 +01005564 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005565 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01005566 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01005567 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005568 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005569 struct ieee80211_supported_band *sband;
Wen Gongb0140fd2020-12-07 11:36:34 +08005570 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergf2622132021-09-30 13:11:31 +02005571 const struct element *ssid_elem, *ht_elem, *vht_elem;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005572 int i, err;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005573 bool override = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005574
Johannes Berg66e67e42012-01-20 13:55:27 +01005575 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5576 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01005577 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005578
Johannes Berg9caf0362012-11-29 01:25:20 +01005579 rcu_read_lock();
Johannes Bergf2622132021-09-30 13:11:31 +02005580 ssid_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_SSID);
5581 if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
Johannes Berg9caf0362012-11-29 01:25:20 +01005582 rcu_read_unlock();
5583 kfree(assoc_data);
5584 return -EINVAL;
5585 }
Johannes Bergf2622132021-09-30 13:11:31 +02005586 memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
5587 assoc_data->ssid_len = ssid_elem->datalen;
Wen Gongb0140fd2020-12-07 11:36:34 +08005588 memcpy(bss_conf->ssid, assoc_data->ssid, assoc_data->ssid_len);
5589 bss_conf->ssid_len = assoc_data->ssid_len;
Johannes Berg9caf0362012-11-29 01:25:20 +01005590 rcu_read_unlock();
5591
Johannes Berg7b119dc2013-04-10 21:38:36 +02005592 if (ifmgd->associated) {
5593 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5594
Johannes Berg89f774e2016-01-25 15:46:36 +02005595 sdata_info(sdata,
5596 "disconnect from AP %pM for new assoc to %pM\n",
5597 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005598 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5599 WLAN_REASON_UNSPECIFIED,
5600 false, frame_buf);
5601
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005602 ieee80211_report_disconnect(sdata, frame_buf,
5603 sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02005604 WLAN_REASON_UNSPECIFIED,
5605 false);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005606 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005607
5608 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5609 err = -EBUSY;
5610 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005611 }
5612
Johannes Berg66e67e42012-01-20 13:55:27 +01005613 if (ifmgd->assoc_data) {
5614 err = -EBUSY;
5615 goto err_free;
5616 }
5617
5618 if (ifmgd->auth_data) {
5619 bool match;
5620
5621 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00005622 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005623 ieee80211_destroy_auth_data(sdata, match);
5624 }
5625
5626 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02005627
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03005628 ifmgd->beacon_crc_valid = false;
5629
Johannes Berg095d81c2013-10-15 12:25:07 +02005630 assoc_data->wmm = bss->wmm_used &&
5631 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg095d81c2013-10-15 12:25:07 +02005632
Johannes Bergde5036a2012-03-08 15:02:03 +01005633 /*
5634 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5635 * We still associate in non-HT mode (11a/b/g) if any one of these
5636 * ciphers is configured as pairwise.
5637 * We can set this to true for non-11n hardware, that'll be checked
5638 * separately along with the peer capabilities.
5639 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02005640 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02005641 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5642 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02005643 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01005644 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005645 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005646 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg1c4cb922012-05-30 15:57:00 +02005647 netdev_info(sdata->dev,
Johannes Berg54626322019-08-30 14:24:46 +03005648 "disabling HT/VHT/HE due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02005649 }
5650 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005651
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005652 sband = local->hw.wiphy->bands[req->bss->channel->band];
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005653
Johannes Berg75e296e2020-01-31 13:12:39 +02005654 /* also disable HT/VHT/HE if the AP doesn't use WMM */
5655 if (!bss->wmm_used) {
5656 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005657 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005658 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5659 netdev_info(sdata->dev,
5660 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005661 }
5662
Ben Greearef96a8422011-11-18 11:32:00 -08005663 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5664 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5665 sizeof(ifmgd->ht_capa_mask));
5666
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01005667 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5668 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5669 sizeof(ifmgd->vht_capa_mask));
5670
Thomas Pedersen7957c6c2020-09-21 19:28:05 -07005671 memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
5672 memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
5673 sizeof(ifmgd->s1g_capa_mask));
5674
Johannes Berg77fdaa12009-07-07 03:45:17 +02005675 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005676 memcpy(assoc_data->ie, req->ie, req->ie_len);
5677 assoc_data->ie_len = req->ie_len;
5678 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005679
Jouni Malinen39404fe2016-10-27 00:42:05 +03005680 if (req->fils_kek) {
5681 /* should already be checked in cfg80211 - so warn */
5682 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5683 err = -EINVAL;
5684 goto err_free;
5685 }
5686 memcpy(assoc_data->fils_kek, req->fils_kek,
5687 req->fils_kek_len);
5688 assoc_data->fils_kek_len = req->fils_kek_len;
5689 }
5690
5691 if (req->fils_nonces)
5692 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5693 2 * FILS_NONCE_LEN);
5694
Johannes Berg66e67e42012-01-20 13:55:27 +01005695 assoc_data->bss = req->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01005696 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01005697 assoc_data->supp_rates = bss->supp_rates;
5698 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02005699
Johannes Berg9caf0362012-11-29 01:25:20 +01005700 rcu_read_lock();
Johannes Bergf2622132021-09-30 13:11:31 +02005701 ht_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_HT_OPERATION);
5702 if (ht_elem && ht_elem->datalen >= sizeof(struct ieee80211_ht_operation))
Johannes Berg9dde6422012-05-16 23:43:19 +02005703 assoc_data->ap_ht_param =
Johannes Bergf2622132021-09-30 13:11:31 +02005704 ((struct ieee80211_ht_operation *)(ht_elem->data))->ht_param;
Johannes Berg57fa5e82020-05-28 21:34:36 +02005705 else if (!is_6ghz)
Johannes Berga8243b72012-11-22 14:32:09 +01005706 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergf2622132021-09-30 13:11:31 +02005707 vht_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_VHT_CAPABILITY);
Johannes Berg636ccda2021-11-30 13:16:59 +02005708 if (vht_elem && vht_elem->datalen >= sizeof(struct ieee80211_vht_cap)) {
Johannes Bergf2622132021-09-30 13:11:31 +02005709 memcpy(&assoc_data->ap_vht_cap, vht_elem->data,
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005710 sizeof(struct ieee80211_vht_cap));
Johannes Berg636ccda2021-11-30 13:16:59 +02005711 } else if (is_5ghz) {
5712 sdata_info(sdata, "VHT capa missing/short, disabling VHT/HE\n");
Johannes Berg57fa5e82020-05-28 21:34:36 +02005713 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5714 IEEE80211_STA_DISABLE_HE;
Johannes Berg636ccda2021-11-30 13:16:59 +02005715 }
Johannes Berg9caf0362012-11-29 01:25:20 +01005716 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01005717
Johannes Berg848955c2014-11-11 12:48:42 +01005718 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02005719 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
Johannes Berg848955c2014-11-11 12:48:42 +01005720 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5721 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5722
Kalle Valoab133152010-01-12 10:42:31 +02005723 if (bss->wmm_used && bss->uapsd_supported &&
Johannes Berg848955c2014-11-11 12:48:42 +01005724 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01005725 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02005726 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5727 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01005728 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02005729 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5730 }
5731
Johannes Berg77fdaa12009-07-07 03:45:17 +02005732 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01005733 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005734
5735 if (req->use_mfp) {
5736 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5737 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5738 } else {
5739 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5740 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5741 }
5742
Assaf Krausscd2f5dd2014-09-03 15:25:02 +03005743 if (req->flags & ASSOC_REQ_USE_RRM)
5744 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5745 else
5746 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5747
Johannes Berg77fdaa12009-07-07 03:45:17 +02005748 if (req->crypto.control_port)
5749 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5750 else
5751 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5752
Johannes Berga621fa42010-08-27 14:26:54 +03005753 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5754 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Denis Kenzior018f6fb2018-03-26 12:52:51 -05005755 sdata->control_port_over_nl80211 =
5756 req->crypto.control_port_over_nl80211;
Markus Theil7f3f96c2020-03-12 10:10:54 +01005757 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02005758 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5759 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03005760
Johannes Berg66e67e42012-01-20 13:55:27 +01005761 /* kick off associate process */
5762
5763 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02005764 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03005765 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01005766
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005767 /* override HT/VHT configuration only if the AP and we support it */
5768 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5769 struct ieee80211_sta_ht_cap sta_ht_cap;
5770
5771 if (req->flags & ASSOC_REQ_DISABLE_HT)
5772 override = true;
5773
5774 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5775 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5776
5777 /* check for 40 MHz disable override */
5778 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5779 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5780 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5781 override = true;
5782
5783 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5784 req->flags & ASSOC_REQ_DISABLE_VHT)
5785 override = true;
5786 }
5787
5788 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berg636ccda2021-11-30 13:16:59 +02005789 mlme_dbg(sdata, "HT disabled by flag, disabling HT/VHT/HE\n");
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005790 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5791 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005792 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005793 }
5794
Johannes Berg636ccda2021-11-30 13:16:59 +02005795 if (req->flags & ASSOC_REQ_DISABLE_VHT) {
5796 mlme_dbg(sdata, "VHT disabled by flag, disabling VHT\n");
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005797 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg636ccda2021-11-30 13:16:59 +02005798 }
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005799
Johannes Berg636ccda2021-11-30 13:16:59 +02005800 if (req->flags & ASSOC_REQ_DISABLE_HE) {
5801 mlme_dbg(sdata, "HE disabled by flag, disabling VHT\n");
Ben Greearb6db0f82021-02-04 06:46:10 -08005802 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg636ccda2021-11-30 13:16:59 +02005803 }
Ben Greearb6db0f82021-02-04 06:46:10 -08005804
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005805 err = ieee80211_prep_connection(sdata, req->bss, true, override);
Johannes Berga1cf7752012-03-08 15:02:07 +01005806 if (err)
5807 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01005808
Shaul Triebitz79ea0a52021-06-18 13:41:34 +03005809 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5810 if (ifmgd->powersave)
5811 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
5812 else
5813 sdata->smps_mode = IEEE80211_SMPS_OFF;
5814 } else {
5815 sdata->smps_mode = ifmgd->req_smps;
5816 }
5817
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005818 rcu_read_lock();
5819 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02005820
Johannes Berg30686bf2015-06-02 21:39:54 +02005821 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005822 !beacon_ies) {
5823 /*
5824 * Wait up to one beacon interval ...
5825 * should this be more if we miss one?
5826 */
5827 sdata_info(sdata, "waiting for beacon from %pM\n",
5828 ifmgd->bssid);
5829 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01005830 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005831 assoc_data->need_beacon = true;
5832 } else if (beacon_ies) {
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005833 const struct element *elem;
Johannes Bergef429da2013-02-05 17:48:40 +01005834 u8 dtim_count = 0;
5835
Sara Sharon78ac51f2019-01-16 18:22:56 +02005836 ieee80211_get_dtim(beacon_ies, &dtim_count,
5837 &ifmgd->dtim_period);
5838
Alexander Bondar989c6502013-05-16 17:34:17 +03005839 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005840 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005841 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01005842
Johannes Berg30686bf2015-06-02 21:39:54 +02005843 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005844 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5845 sdata->vif.bss_conf.sync_device_ts =
5846 bss->device_ts_beacon;
5847 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5848 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02005849
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005850 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5851 beacon_ies->data, beacon_ies->len);
5852 if (elem && elem->datalen >= 3)
5853 sdata->vif.bss_conf.profile_periodicity = elem->data[2];
Johannes Bergbbc6f032021-06-18 13:41:49 +03005854 else
5855 sdata->vif.bss_conf.profile_periodicity = 0;
Sara Sharon78ac51f2019-01-16 18:22:56 +02005856
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005857 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5858 beacon_ies->data, beacon_ies->len);
5859 if (elem && elem->datalen >= 11 &&
5860 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
Sara Sharon78ac51f2019-01-16 18:22:56 +02005861 sdata->vif.bss_conf.ema_ap = true;
Johannes Bergbbc6f032021-06-18 13:41:49 +03005862 else
5863 sdata->vif.bss_conf.ema_ap = false;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005864 } else {
5865 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005866 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005867 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005868 rcu_read_unlock();
5869
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005870 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01005871
Paul Stewartfcff4f12012-02-23 17:59:53 -08005872 if (bss->corrupt_data) {
5873 char *corrupt_type = "data";
5874 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5875 if (bss->corrupt_data &
5876 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5877 corrupt_type = "beacon and probe response";
5878 else
5879 corrupt_type = "beacon";
5880 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5881 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005882 sdata_info(sdata, "associating with AP with corrupt %s\n",
5883 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08005884 }
5885
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005886 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01005887 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005888 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005889 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005890 ifmgd->assoc_data = NULL;
5891 err_free:
5892 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005893 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005894}
5895
5896int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005897 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005898{
5899 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005900 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005901 bool tx = !req->local_state_change;
Johannes Berg15fae342021-06-18 13:41:55 +03005902 struct ieee80211_prep_tx_info info = {
5903 .subtype = IEEE80211_STYPE_DEAUTH,
5904 };
Johannes Berg77fdaa12009-07-07 03:45:17 +02005905
Johannes Bergc9c3a062014-03-19 09:11:19 +01005906 if (ifmgd->auth_data &&
5907 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5908 sdata_info(sdata,
5909 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5910 req->bssid, req->reason_code,
5911 ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005912
Johannes Berg15fae342021-06-18 13:41:55 +03005913 drv_mgd_prepare_tx(sdata->local, sdata, &info);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005914 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Johannes Berg37ad3882012-02-24 13:50:54 +01005915 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005916 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01005917 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01005918 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005919 ieee80211_report_disconnect(sdata, frame_buf,
5920 sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02005921 req->reason_code, false);
Johannes Berg15fae342021-06-18 13:41:55 +03005922 drv_mgd_complete_tx(sdata->local, sdata, &info);
Johannes Bergc9c3a062014-03-19 09:11:19 +01005923 return 0;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03005924 }
5925
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005926 if (ifmgd->assoc_data &&
5927 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5928 sdata_info(sdata,
5929 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5930 req->bssid, req->reason_code,
5931 ieee80211_get_reason_code_string(req->reason_code));
5932
Johannes Berg15fae342021-06-18 13:41:55 +03005933 drv_mgd_prepare_tx(sdata->local, sdata, &info);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005934 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005935 IEEE80211_STYPE_DEAUTH,
5936 req->reason_code, tx,
5937 frame_buf);
Johannes Berge6f462d2016-12-08 17:22:09 +01005938 ieee80211_destroy_assoc_data(sdata, false, true);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005939 ieee80211_report_disconnect(sdata, frame_buf,
5940 sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02005941 req->reason_code, false);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005942 return 0;
5943 }
5944
Johannes Berg86552012012-10-29 09:46:31 +01005945 if (ifmgd->associated &&
5946 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Bergc9c3a062014-03-19 09:11:19 +01005947 sdata_info(sdata,
5948 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5949 req->bssid, req->reason_code,
5950 ieee80211_get_reason_code_string(req->reason_code));
5951
Johannes Berg86552012012-10-29 09:46:31 +01005952 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5953 req->reason_code, tx, frame_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005954 ieee80211_report_disconnect(sdata, frame_buf,
5955 sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02005956 req->reason_code, false);
Johannes Berg15fae342021-06-18 13:41:55 +03005957 drv_mgd_complete_tx(sdata->local, sdata, &info);
Johannes Bergc9c3a062014-03-19 09:11:19 +01005958 return 0;
5959 }
Johannes Berg86552012012-10-29 09:46:31 +01005960
Johannes Bergc9c3a062014-03-19 09:11:19 +01005961 return -ENOTCONN;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005962}
5963
5964int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005965 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005966{
5967 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07005968 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005969 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02005970
Johannes Berg8d8b2612009-07-25 11:58:36 +02005971 /*
5972 * cfg80211 should catch this ... but it's racy since
5973 * we can receive a disassoc frame, process it, hand it
5974 * to cfg80211 while that's in a locked section already
5975 * trying to tell us that the user wants to disconnect.
5976 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005977 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005978 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005979
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005980 sdata_info(sdata,
Calvin Owensdfa1ad22014-02-11 12:36:24 -06005981 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5982 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005983
Jouni Malinene69e95d2010-03-29 23:29:31 -07005984 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01005985 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5986 req->reason_code, !req->local_state_change,
5987 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005988
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005989 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
Johannes Berg3f8a39f2020-12-06 14:54:43 +02005990 req->reason_code, false);
Johannes Bergbc83b682009-11-29 12:19:06 +01005991
Johannes Berg77fdaa12009-07-07 03:45:17 +02005992 return 0;
5993}
Jouni Malinen026331c2010-02-15 12:53:10 +02005994
Eliad Pellerafa762f2012-04-23 14:45:15 +03005995void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005996{
5997 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5998
Ben Greear49921852013-02-20 09:41:09 -08005999 /*
6000 * Make sure some work items will not run after this,
6001 * they will not do anything but might not have been
6002 * cancelled when disconnecting.
6003 */
6004 cancel_work_sync(&ifmgd->monitor_work);
6005 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
6006 cancel_work_sync(&ifmgd->request_smps_work);
6007 cancel_work_sync(&ifmgd->csa_connection_drop_work);
6008 cancel_work_sync(&ifmgd->chswitch_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03006009 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
Ben Greear49921852013-02-20 09:41:09 -08006010
Johannes Berg8d61ffa2013-05-10 12:32:47 +02006011 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02006012 if (ifmgd->assoc_data) {
6013 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berge6f462d2016-12-08 17:22:09 +01006014 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02006015 cfg80211_assoc_timeout(sdata->dev, bss);
6016 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01006017 if (ifmgd->auth_data)
6018 ieee80211_destroy_auth_data(sdata, false);
Liad Kaufman1277b4a2014-11-09 18:50:08 +02006019 spin_lock_bh(&ifmgd->teardown_lock);
6020 if (ifmgd->teardown_skb) {
6021 kfree_skb(ifmgd->teardown_skb);
6022 ifmgd->teardown_skb = NULL;
6023 ifmgd->orig_teardown_skb = NULL;
6024 }
Jouni Malinen4d9ec732019-02-15 02:14:33 +02006025 kfree(ifmgd->assoc_req_ies);
6026 ifmgd->assoc_req_ies = NULL;
6027 ifmgd->assoc_req_ies_len = 0;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02006028 spin_unlock_bh(&ifmgd->teardown_lock);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01006029 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02006030 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01006031}
6032
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02006033void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
6034 enum nl80211_cqm_rssi_threshold_event rssi_event,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01006035 s32 rssi_level,
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02006036 gfp_t gfp)
6037{
6038 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6039
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01006040 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
Johannes Bergb5878a22010-04-07 16:48:40 +02006041
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +01006042 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02006043}
6044EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Johannes Berg98f03342014-11-26 12:42:02 +01006045
6046void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
6047{
6048 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6049
6050 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
6051
6052 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
6053}
6054EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);