blob: 8a92a62dc54d95c0e53a49ee4a14874b36d196d8 [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
Sara Sharon2bf973f2020-01-31 13:12:51 +020011 * Copyright (C) 2018 - 2020 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,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200152 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100153{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200154 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100155 struct cfg80211_chan_def vht_chandef;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200156 struct ieee80211_sta_ht_cap sta_ht_cap;
Johannes Berg6565ec92013-02-08 14:52:32 +0100157 u32 ht_cfreq, ret;
158
Alexei Avshalom Lazar2a380752019-08-18 17:35:17 +0300159 memset(chandef, 0, sizeof(struct cfg80211_chan_def));
Johannes Berg6565ec92013-02-08 14:52:32 +0100160 chandef->chan = channel;
161 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
162 chandef->center_freq1 = channel->center_freq;
Thomas Pedersenb60119602020-04-01 18:18:04 -0700163 chandef->freq1_offset = channel->freq_offset;
Johannes Berg6565ec92013-02-08 14:52:32 +0100164
Johannes Berg57fa5e82020-05-28 21:34:36 +0200165 if (channel->band == NL80211_BAND_6GHZ) {
166 if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, chandef))
167 ret = IEEE80211_STA_DISABLE_HT |
168 IEEE80211_STA_DISABLE_VHT |
169 IEEE80211_STA_DISABLE_HE;
Johannes Berg523f3ec2020-06-03 11:15:03 +0200170 else
171 ret = 0;
Johannes Berg57fa5e82020-05-28 21:34:36 +0200172 vht_chandef = *chandef;
173 goto out;
174 }
175
176 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
177 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
178
Johannes Bergb1b1ae22017-10-13 15:26:01 +0300179 if (!ht_oper || !sta_ht_cap.ht_supported) {
Johannes Berg75e296e2020-01-31 13:12:39 +0200180 ret = IEEE80211_STA_DISABLE_HT |
181 IEEE80211_STA_DISABLE_VHT |
182 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100183 goto out;
184 }
185
186 chandef->width = NL80211_CHAN_WIDTH_20;
187
188 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
189 channel->band);
190 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200191 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100192 /*
193 * It's possible that some APs are confused here;
194 * Netgear WNDR3700 sometimes reports 4 higher than
195 * the actual channel in association responses, but
196 * since we look at probe response/beacon data here
197 * it should be OK.
198 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200199 sdata_info(sdata,
200 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
201 channel->center_freq, ht_cfreq,
202 ht_oper->primary_chan, channel->band);
Johannes Berg75e296e2020-01-31 13:12:39 +0200203 ret = IEEE80211_STA_DISABLE_HT |
204 IEEE80211_STA_DISABLE_VHT |
205 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100206 goto out;
207 }
208
209 /* check 40 MHz support, if we have it */
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200210 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100211 ieee80211_chandef_ht_oper(ht_oper, chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100212 } else {
213 /* 40 MHz (and 80 MHz) must be supported for VHT */
214 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100215 /* also mark 40 MHz disabled */
216 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100217 goto out;
218 }
219
220 if (!vht_oper || !sband->vht_cap.vht_supported) {
221 ret = IEEE80211_STA_DISABLE_VHT;
222 goto out;
223 }
224
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100225 vht_chandef = *chandef;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300226 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
227 (le32_to_cpu(he_oper->he_oper_params) &
228 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
229 struct ieee80211_vht_operation he_oper_vht_cap;
230
231 /*
232 * Set only first 3 bytes (other 2 aren't used in
233 * ieee80211_chandef_vht_oper() anyway)
234 */
235 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
236 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
237
Johannes Berg2a333a02020-05-28 21:34:35 +0200238 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
Johannes Berg7eb26df2018-08-31 11:31:18 +0300239 &he_oper_vht_cap, ht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300240 &vht_chandef)) {
241 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
242 sdata_info(sdata,
243 "HE AP VHT information is invalid, disable HE\n");
244 ret = IEEE80211_STA_DISABLE_HE;
245 goto out;
246 }
Johannes Berg2a333a02020-05-28 21:34:35 +0200247 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
248 vht_cap_info,
249 vht_oper, ht_oper,
250 &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200251 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100252 sdata_info(sdata,
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100253 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100254 ret = IEEE80211_STA_DISABLE_VHT;
255 goto out;
256 }
257
258 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200259 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100260 sdata_info(sdata,
261 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100262 ret = IEEE80211_STA_DISABLE_VHT;
263 goto out;
264 }
265
266 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
267 ret = 0;
268 goto out;
269 }
270
271 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200272 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100273 sdata_info(sdata,
274 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100275 ret = IEEE80211_STA_DISABLE_VHT;
276 goto out;
277 }
278
279 *chandef = vht_chandef;
280
281 ret = 0;
282
283out:
Johannes Berg963a18522014-02-21 20:34:34 +0100284 /*
285 * When tracking the current AP, don't do any further checks if the
286 * new chandef is identical to the one we're currently using for the
287 * connection. This keeps us from playing ping-pong with regulatory,
288 * without it the following can happen (for example):
289 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
290 * - AP advertises regdom US
291 * - CRDA loads regdom US with 80 MHz prohibited (old database)
292 * - the code below detects an unsupported channel, downgrades, and
293 * we disconnect from the AP in the caller
294 * - disconnect causes CRDA to reload world regdomain and the game
295 * starts anew.
296 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
297 *
298 * It seems possible that there are still scenarios with CSA or real
299 * bandwidth changes where a this could happen, but those cases are
300 * less common and wouldn't completely prevent using the AP.
301 */
302 if (tracking &&
303 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
304 return ret;
305
Johannes Berg586e01e2013-02-14 12:13:53 +0100306 /* don't print the message below for VHT mismatch if VHT is disabled */
307 if (ret & IEEE80211_STA_DISABLE_VHT)
308 vht_chandef = *chandef;
309
Johannes Bergddfe49b2013-07-31 20:52:03 +0200310 /*
311 * Ignore the DISABLED flag when we're already connected and only
312 * tracking the APs beacon for bandwidth changes - otherwise we
313 * might get disconnected here if we connect to an AP, update our
314 * regulatory information based on the AP's country IE and the
315 * information we have is wrong/outdated and disables the channel
316 * that we're actually using for the connection to the AP.
317 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100318 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200319 tracking ? 0 :
320 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100321 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
322 ret = IEEE80211_STA_DISABLE_HT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200323 IEEE80211_STA_DISABLE_VHT |
324 IEEE80211_STA_DISABLE_HE;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700325 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100326 }
327
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200328 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100329 }
330
Daniel Gabay8cadb202020-01-31 13:12:52 +0200331 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
332 IEEE80211_CHAN_NO_HE))
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200333 ret |= IEEE80211_STA_DISABLE_HE;
334
Johannes Berg5cdaed12013-07-31 11:23:06 +0200335 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100336 sdata_info(sdata,
337 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
338
339 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
340 return ret;
341}
342
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100343static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
344 struct sta_info *sta,
Eliad Peller53b954e2014-07-24 11:20:05 +0300345 const struct ieee80211_ht_cap *ht_cap,
Johannes Berg2a333a02020-05-28 21:34:35 +0200346 const struct ieee80211_vht_cap *vht_cap,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100347 const struct ieee80211_ht_operation *ht_oper,
348 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300349 const struct ieee80211_he_operation *he_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100350 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200351{
352 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100353 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300354 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
355 struct ieee80211_supported_band *sband =
356 local->hw.wiphy->bands[chan->band];
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100357 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200358 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100359 u32 flags;
360 enum ieee80211_sta_rx_bandwidth new_sta_bw;
Johannes Berg2a333a02020-05-28 21:34:35 +0200361 u32 vht_cap_info = 0;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100362 int ret;
363
364 /* if HT was/is disabled, don't track any bandwidth changes */
365 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
366 return 0;
367
368 /* don't check VHT if we associated as non-VHT station */
369 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
370 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200371
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300372 /* don't check HE if we associated as non-HE station */
373 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
374 !ieee80211_get_he_sta_cap(sband))
375 he_oper = NULL;
376
Johannes Berg11289582013-02-07 17:36:12 +0100377 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100378 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100379
Avri Altman017b45b2013-11-18 19:06:48 +0200380 /*
381 * if bss configuration changed store the new one -
382 * this may be applicable even if channel is identical
383 */
384 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
385 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
386 *changed |= BSS_CHANGED_HT;
387 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
388 }
389
Johannes Berg2a333a02020-05-28 21:34:35 +0200390 if (vht_cap)
391 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
392
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300393 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
Johannes Berg2a333a02020-05-28 21:34:35 +0200394 flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300395 ht_oper, vht_oper, he_oper,
Eliad Peller53b954e2014-07-24 11:20:05 +0300396 &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100397
398 /*
399 * Downgrade the new channel if we associated with restricted
400 * capabilities. For example, if we associated as a 20 MHz STA
401 * to a 40 MHz AP (due to regulatory, capabilities or config
402 * reasons) then switching to a 40 MHz channel now won't do us
403 * any good -- we couldn't use it with the AP.
404 */
405 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
406 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200407 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100408 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
409 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200410 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100411 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
412 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200413 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100414
415 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
416 return 0;
417
418 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -0700419 "AP %pM changed bandwidth, new config is %d.%03d MHz, "
420 "width %d (%d.%03d/%d MHz)\n",
421 ifmgd->bssid, chandef.chan->center_freq,
422 chandef.chan->freq_offset, chandef.width,
423 chandef.center_freq1, chandef.freq1_offset,
424 chandef.center_freq2);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100425
426 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
427 IEEE80211_STA_DISABLE_VHT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200428 IEEE80211_STA_DISABLE_HE |
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100429 IEEE80211_STA_DISABLE_40MHZ |
430 IEEE80211_STA_DISABLE_80P80MHZ |
431 IEEE80211_STA_DISABLE_160MHZ)) ||
432 !cfg80211_chandef_valid(&chandef)) {
433 sdata_info(sdata,
434 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
435 ifmgd->bssid);
436 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200437 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200438
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100439 switch (chandef.width) {
440 case NL80211_CHAN_WIDTH_20_NOHT:
441 case NL80211_CHAN_WIDTH_20:
442 new_sta_bw = IEEE80211_STA_RX_BW_20;
443 break;
444 case NL80211_CHAN_WIDTH_40:
445 new_sta_bw = IEEE80211_STA_RX_BW_40;
446 break;
447 case NL80211_CHAN_WIDTH_80:
448 new_sta_bw = IEEE80211_STA_RX_BW_80;
449 break;
450 case NL80211_CHAN_WIDTH_80P80:
451 case NL80211_CHAN_WIDTH_160:
452 new_sta_bw = IEEE80211_STA_RX_BW_160;
453 break;
454 default:
455 return -EINVAL;
456 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200457
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100458 if (new_sta_bw > sta->cur_max_bandwidth)
459 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200460
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100461 if (new_sta_bw < sta->sta.bandwidth) {
462 sta->sta.bandwidth = new_sta_bw;
463 rate_control_rate_update(local, sband, sta,
464 IEEE80211_RC_BW_CHANGED);
465 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530466
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100467 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
468 if (ret) {
469 sdata_info(sdata,
470 "AP %pM changed bandwidth to incompatible one - disconnect\n",
471 ifmgd->bssid);
472 return ret;
473 }
474
475 if (new_sta_bw > sta->sta.bandwidth) {
476 sta->sta.bandwidth = new_sta_bw;
477 rate_control_rate_update(local, sband, sta,
478 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200479 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200480
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100481 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200482}
483
Johannes Berg9c6bd792008-09-11 00:01:52 +0200484/* frame sending functions */
485
Johannes Berg66e67e42012-01-20 13:55:27 +0100486static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200487 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100488 struct ieee80211_supported_band *sband,
489 struct ieee80211_channel *channel,
490 enum ieee80211_smps_mode smps)
491{
Johannes Berg66e67e42012-01-20 13:55:27 +0100492 u8 *pos;
493 u32 flags = channel->flags;
494 u16 cap;
495 struct ieee80211_sta_ht_cap ht_cap;
496
497 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
498
Johannes Berg66e67e42012-01-20 13:55:27 +0100499 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
500 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
501
Johannes Berg66e67e42012-01-20 13:55:27 +0100502 /* determine capability flags */
503 cap = ht_cap.cap;
504
Johannes Berg9dde6422012-05-16 23:43:19 +0200505 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100506 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
507 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
508 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
509 cap &= ~IEEE80211_HT_CAP_SGI_40;
510 }
511 break;
512 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
513 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
514 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
515 cap &= ~IEEE80211_HT_CAP_SGI_40;
516 }
517 break;
518 }
519
Johannes Berg24398e32012-03-28 10:58:36 +0200520 /*
521 * If 40 MHz was disabled associate as though we weren't
522 * capable of 40 MHz -- some broken APs will never fall
523 * back to trying to transmit in 20 MHz.
524 */
525 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
526 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
527 cap &= ~IEEE80211_HT_CAP_SGI_40;
528 }
529
Johannes Berg66e67e42012-01-20 13:55:27 +0100530 /* set SM PS mode properly */
531 cap &= ~IEEE80211_HT_CAP_SM_PS;
532 switch (smps) {
533 case IEEE80211_SMPS_AUTOMATIC:
534 case IEEE80211_SMPS_NUM_MODES:
535 WARN_ON(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -0500536 fallthrough;
Johannes Berg66e67e42012-01-20 13:55:27 +0100537 case IEEE80211_SMPS_OFF:
538 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
539 IEEE80211_HT_CAP_SM_PS_SHIFT;
540 break;
541 case IEEE80211_SMPS_STATIC:
542 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
543 IEEE80211_HT_CAP_SM_PS_SHIFT;
544 break;
545 case IEEE80211_SMPS_DYNAMIC:
546 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
547 IEEE80211_HT_CAP_SM_PS_SHIFT;
548 break;
549 }
550
551 /* reserve and fill IE */
552 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
553 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
554}
555
Sara Sharon322cd402015-07-08 15:41:43 +0300556/* This function determines vht capability flags for the association
557 * and builds the IE.
558 * Note - the function may set the owner of the MU-MIMO capability
559 */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000560static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
561 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100562 struct ieee80211_supported_band *sband,
563 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000564{
Sara Sharon322cd402015-07-08 15:41:43 +0300565 struct ieee80211_local *local = sdata->local;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000566 u8 *pos;
567 u32 cap;
568 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200569 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000570
571 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
572
573 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100574 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000575
576 /* determine capability flags */
577 cap = vht_cap.cap;
578
Johannes Bergf2d9d272012-11-22 14:11:39 +0100579 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
Johannes Berg575f0532014-11-24 16:51:33 +0100580 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
581
582 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
583 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
584 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
585 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100586 }
587
588 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
589 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
Johannes Berg575f0532014-11-24 16:51:33 +0100590 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100591 }
592
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100593 /*
594 * Some APs apparently get confused if our capabilities are better
595 * than theirs, so restrict what we advertise in the assoc request.
596 */
597 if (!(ap_vht_cap->vht_cap_info &
598 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
Sara Sharon322cd402015-07-08 15:41:43 +0300599 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
600 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
601 else if (!(ap_vht_cap->vht_cap_info &
602 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
603 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
604
605 /*
606 * If some other vif is using the MU-MIMO capablity we cannot associate
607 * using MU-MIMO - this will lead to contradictions in the group-id
608 * mechanism.
609 * Ownership is defined since association request, in order to avoid
610 * simultaneous associations with MU-MIMO.
611 */
612 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
613 bool disable_mu_mimo = false;
614 struct ieee80211_sub_if_data *other;
615
616 list_for_each_entry_rcu(other, &local->interfaces, list) {
Sara Sharonb5a33d52016-02-16 12:48:18 +0200617 if (other->vif.mu_mimo_owner) {
Sara Sharon322cd402015-07-08 15:41:43 +0300618 disable_mu_mimo = true;
619 break;
620 }
621 }
622 if (disable_mu_mimo)
623 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
624 else
Sara Sharonb5a33d52016-02-16 12:48:18 +0200625 sdata->vif.mu_mimo_owner = true;
Sara Sharon322cd402015-07-08 15:41:43 +0300626 }
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100627
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200628 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
629
630 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
631 our_bf_sts = cap & mask;
632
633 if (ap_bf_sts < our_bf_sts) {
634 cap &= ~mask;
635 cap |= ap_bf_sts;
636 }
637
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000638 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000639 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000640 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
641}
642
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300643/* This function determines HE capability flags for the association
644 * and builds the IE.
645 */
646static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
647 struct sk_buff *skb,
648 struct ieee80211_supported_band *sband)
649{
650 u8 *pos;
651 const struct ieee80211_sta_he_cap *he_cap = NULL;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200652 struct ieee80211_chanctx_conf *chanctx_conf;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300653 u8 he_cap_size;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200654 bool reg_cap = false;
655
656 rcu_read_lock();
657 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
658 if (!WARN_ON_ONCE(!chanctx_conf))
659 reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
660 &chanctx_conf->def,
661 IEEE80211_CHAN_NO_HE);
662
663 rcu_read_unlock();
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300664
665 he_cap = ieee80211_get_he_sta_cap(sband);
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200666 if (!he_cap || !reg_cap)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300667 return;
668
669 /*
670 * TODO: the 1 added is because this temporarily is under the EXTENSION
671 * IE. Get rid of it when it moves.
672 */
673 he_cap_size =
674 2 + 1 + sizeof(he_cap->he_cap_elem) +
675 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
676 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
677 he_cap->he_cap_elem.phy_cap_info);
678 pos = skb_put(skb, he_cap_size);
679 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200680
681 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300682}
683
Johannes Berg66e67e42012-01-20 13:55:27 +0100684static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
685{
686 struct ieee80211_local *local = sdata->local;
687 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
688 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
689 struct sk_buff *skb;
690 struct ieee80211_mgmt *mgmt;
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200691 u8 *pos, qos_info, *ie_start;
Johannes Berg66e67e42012-01-20 13:55:27 +0100692 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200693 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100694 u16 capab;
695 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200696 struct ieee80211_chanctx_conf *chanctx_conf;
697 struct ieee80211_channel *chan;
Johannes Berg44f6d422017-06-10 13:52:44 +0300698 u32 rates = 0;
Johannes Berg2ff69b02020-01-31 13:31:11 +0200699 struct element *ext_capa = NULL;
700
701 /* we know it's writable, cast away the const */
702 if (assoc_data->ie_len)
703 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
704 assoc_data->ie,
705 assoc_data->ie_len);
Johannes Berg66e67e42012-01-20 13:55:27 +0100706
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200707 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100708
Johannes Berg55de9082012-07-26 17:24:39 +0200709 rcu_read_lock();
710 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
711 if (WARN_ON(!chanctx_conf)) {
712 rcu_read_unlock();
713 return;
714 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100715 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200716 rcu_read_unlock();
717 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200718 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100719
720 if (assoc_data->supp_rates_len) {
721 /*
722 * Get all rates supported by the device and the AP as
723 * some APs don't like getting a superset of their rates
724 * in the association request (e.g. D-Link DAP 1353 in
725 * b-only mode)...
726 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200727 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
728 assoc_data->supp_rates,
729 assoc_data->supp_rates_len,
730 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100731 } else {
732 /*
733 * In case AP not provide any supported rates information
734 * before association, we send information element(s) with
735 * all rates that we support.
736 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200737 rates_len = 0;
738 for (i = 0; i < sband->n_bitrates; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200739 rates |= BIT(i);
740 rates_len++;
741 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100742 }
743
744 skb = alloc_skb(local->hw.extra_tx_headroom +
745 sizeof(*mgmt) + /* bit too much but doesn't matter */
746 2 + assoc_data->ssid_len + /* SSID */
747 4 + rates_len + /* (extended) rates */
748 4 + /* power capability */
749 2 + 2 * sband->n_channels + /* supported channels */
750 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000751 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300752 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
753 sizeof(struct ieee80211_he_mcs_nss_supp) +
754 IEEE80211_HE_PPE_THRES_MAX_LEN +
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200755 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100756 assoc_data->ie_len + /* extra IEs */
Jouni Malinen39404fe2016-10-27 00:42:05 +0300757 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100758 9, /* WMM */
759 GFP_KERNEL);
760 if (!skb)
761 return;
762
763 skb_reserve(skb, local->hw.extra_tx_headroom);
764
765 capab = WLAN_CAPABILITY_ESS;
766
Johannes Berg57fbcce2016-04-12 15:56:15 +0200767 if (sband->band == NL80211_BAND_2GHZ) {
Johannes Bergea1b2b452015-06-02 20:15:49 +0200768 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
769 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Johannes Berg66e67e42012-01-20 13:55:27 +0100770 }
771
772 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
773 capab |= WLAN_CAPABILITY_PRIVACY;
774
775 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200776 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
Johannes Berg66e67e42012-01-20 13:55:27 +0100777 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
778
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300779 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
780 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
781
Johannes Bergb080db52017-06-16 14:29:19 +0200782 mgmt = skb_put_zero(skb, 24);
Johannes Berg66e67e42012-01-20 13:55:27 +0100783 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
784 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
785 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
786
787 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
788 skb_put(skb, 10);
789 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
790 IEEE80211_STYPE_REASSOC_REQ);
791 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
792 mgmt->u.reassoc_req.listen_interval =
793 cpu_to_le16(local->hw.conf.listen_interval);
794 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
795 ETH_ALEN);
796 } else {
797 skb_put(skb, 4);
798 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
799 IEEE80211_STYPE_ASSOC_REQ);
800 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
801 mgmt->u.assoc_req.listen_interval =
802 cpu_to_le16(local->hw.conf.listen_interval);
803 }
804
805 /* SSID */
806 pos = skb_put(skb, 2 + assoc_data->ssid_len);
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200807 ie_start = pos;
Johannes Berg66e67e42012-01-20 13:55:27 +0100808 *pos++ = WLAN_EID_SSID;
809 *pos++ = assoc_data->ssid_len;
810 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
811
812 /* add all rates which were marked to be used above */
813 supp_rates_len = rates_len;
814 if (supp_rates_len > 8)
815 supp_rates_len = 8;
816
817 pos = skb_put(skb, supp_rates_len + 2);
818 *pos++ = WLAN_EID_SUPP_RATES;
819 *pos++ = supp_rates_len;
820
821 count = 0;
822 for (i = 0; i < sband->n_bitrates; i++) {
823 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200824 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
825 5 * (1 << shift));
826 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100827 if (++count == 8)
828 break;
829 }
830 }
831
832 if (rates_len > count) {
833 pos = skb_put(skb, rates_len - count + 2);
834 *pos++ = WLAN_EID_EXT_SUPP_RATES;
835 *pos++ = rates_len - count;
836
837 for (i++; i < sband->n_bitrates; i++) {
838 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200839 int rate;
840 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
841 5 * (1 << shift));
842 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100843 }
844 }
845 }
846
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300847 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
848 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100849 pos = skb_put(skb, 4);
850 *pos++ = WLAN_EID_PWR_CAPABILITY;
851 *pos++ = 2;
852 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200853 /* max tx power */
854 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300855 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100856
Johannes Berg2ff69b02020-01-31 13:31:11 +0200857 /*
858 * Per spec, we shouldn't include the list of channels if we advertise
859 * support for extended channel switching, but we've always done that;
860 * (for now?) apply this restriction only on the (new) 6 GHz band.
861 */
862 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
863 (sband->band != NL80211_BAND_6GHZ ||
864 !ext_capa || ext_capa->datalen < 1 ||
865 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100866 /* TODO: get this in reg domain format */
867 pos = skb_put(skb, 2 * sband->n_channels + 2);
868 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
869 *pos++ = 2 * sband->n_channels;
870 for (i = 0; i < sband->n_channels; i++) {
871 *pos++ = ieee80211_frequency_to_channel(
872 sband->channels[i].center_freq);
873 *pos++ = 1; /* one channel in the subband*/
874 }
875 }
876
Sara Sharoncaf56332019-01-16 23:03:25 +0200877 /* Set MBSSID support for HE AP if needed */
878 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
Johannes Berg2ff69b02020-01-31 13:31:11 +0200879 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
880 ext_capa && ext_capa->datalen >= 3)
881 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
Sara Sharoncaf56332019-01-16 23:03:25 +0200882
Johannes Berg66e67e42012-01-20 13:55:27 +0100883 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200884 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100885 static const u8 before_ht[] = {
886 WLAN_EID_SSID,
887 WLAN_EID_SUPP_RATES,
888 WLAN_EID_EXT_SUPP_RATES,
889 WLAN_EID_PWR_CAPABILITY,
890 WLAN_EID_SUPPORTED_CHANNELS,
891 WLAN_EID_RSN,
892 WLAN_EID_QOS_CAPA,
893 WLAN_EID_RRM_ENABLED_CAPABILITIES,
894 WLAN_EID_MOBILITY_DOMAIN,
Johannes Berg8ed28742014-10-27 12:03:19 +0100895 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
896 WLAN_EID_RIC_DATA, /* reassoc only */
Johannes Berg66e67e42012-01-20 13:55:27 +0100897 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
898 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100899 static const u8 after_ric[] = {
900 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
901 WLAN_EID_HT_CAPABILITY,
902 WLAN_EID_BSS_COEX_2040,
Johannes Berga7f26d82017-08-05 11:44:32 +0300903 /* luckily this is almost always there */
Johannes Berg8ed28742014-10-27 12:03:19 +0100904 WLAN_EID_EXT_CAPABILITY,
905 WLAN_EID_QOS_TRAFFIC_CAPA,
906 WLAN_EID_TIM_BCAST_REQ,
907 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300908 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg8ed28742014-10-27 12:03:19 +0100909 WLAN_EID_VHT_CAPABILITY,
910 WLAN_EID_OPMODE_NOTIF,
911 };
912
913 noffset = ieee80211_ie_split_ric(assoc_data->ie,
914 assoc_data->ie_len,
915 before_ht,
916 ARRAY_SIZE(before_ht),
917 after_ric,
918 ARRAY_SIZE(after_ric),
919 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800920 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100921 offset = noffset;
922 }
923
Johannes Bergf2d9d272012-11-22 14:11:39 +0100924 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
925 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
926 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
927
Johannes Berg57fa5e82020-05-28 21:34:36 +0200928 if (sband->band != NL80211_BAND_6GHZ &&
929 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200930 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200931 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100932
Johannes Berg3de38022014-02-04 09:54:07 +0100933 /* if present, add any custom IEs that go before VHT */
934 if (assoc_data->ie_len) {
935 static const u8 before_vht[] = {
Johannes Berga7f26d82017-08-05 11:44:32 +0300936 /*
937 * no need to list the ones split off before HT
938 * or generated here
939 */
Johannes Berg3de38022014-02-04 09:54:07 +0100940 WLAN_EID_BSS_COEX_2040,
941 WLAN_EID_EXT_CAPABILITY,
942 WLAN_EID_QOS_TRAFFIC_CAPA,
943 WLAN_EID_TIM_BCAST_REQ,
944 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300945 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg3de38022014-02-04 09:54:07 +0100946 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100947
948 /* RIC already taken above, so no need to handle here anymore */
Johannes Berg3de38022014-02-04 09:54:07 +0100949 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
950 before_vht, ARRAY_SIZE(before_vht),
951 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800952 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg3de38022014-02-04 09:54:07 +0100953 offset = noffset;
954 }
955
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300956 /* if present, add any custom IEs that go before HE */
957 if (assoc_data->ie_len) {
958 static const u8 before_he[] = {
959 /*
960 * no need to list the ones split off before VHT
961 * or generated here
962 */
963 WLAN_EID_OPMODE_NOTIF,
964 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
965 /* 11ai elements */
966 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
967 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
968 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
969 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
970 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
971 /* TODO: add 11ah/11aj/11ak elements */
972 };
973
974 /* RIC already taken above, so no need to handle here anymore */
975 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
976 before_he, ARRAY_SIZE(before_he),
977 offset);
978 pos = skb_put(skb, noffset - offset);
979 memcpy(pos, assoc_data->ie + offset, noffset - offset);
980 offset = noffset;
981 }
982
Johannes Berg57fa5e82020-05-28 21:34:36 +0200983 if (sband->band != NL80211_BAND_6GHZ &&
984 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100985 ieee80211_add_vht_ie(sdata, skb, sband,
986 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000987
Shaul Triebitzdc7eb0f2018-12-15 11:03:20 +0200988 /*
989 * If AP doesn't support HT, mark HE as disabled.
990 * If on the 5GHz band, make sure it supports VHT.
991 */
992 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
993 (sband->band == NL80211_BAND_5GHZ &&
994 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
995 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
996
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300997 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
998 ieee80211_add_he_ie(sdata, skb, sband);
999
1000 /* if present, add any custom non-vendor IEs that go after HE */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001001 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001002 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1003 assoc_data->ie_len,
1004 offset);
yuan linyub952f4d2017-06-18 22:52:04 +08001005 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001006 offset = noffset;
1007 }
1008
Johannes Berg76f03032012-03-08 15:02:05 +01001009 if (assoc_data->wmm) {
1010 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001011 qos_info = ifmgd->uapsd_queues;
1012 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +01001013 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1014 } else {
1015 qos_info = 0;
1016 }
1017
Arik Nemtsov40b861a2014-07-17 17:14:23 +03001018 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
Johannes Berg66e67e42012-01-20 13:55:27 +01001019 }
1020
1021 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001022 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001023 noffset = assoc_data->ie_len;
yuan linyub952f4d2017-06-18 22:52:04 +08001024 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001025 }
1026
Jouni Malinen39404fe2016-10-27 00:42:05 +03001027 if (assoc_data->fils_kek_len &&
1028 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
1029 dev_kfree_skb(skb);
1030 return;
1031 }
1032
Jouni Malinen4d9ec732019-02-15 02:14:33 +02001033 pos = skb_tail_pointer(skb);
1034 kfree(ifmgd->assoc_req_ies);
1035 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1036 ifmgd->assoc_req_ies_len = pos - ie_start;
1037
Ilan Peerd4e36e52018-04-20 13:49:25 +03001038 drv_mgd_prepare_tx(local, sdata, 0);
Johannes Berga1845fc2012-06-27 13:18:36 +02001039
Johannes Berg66e67e42012-01-20 13:55:27 +01001040 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg30686bf2015-06-02 21:39:54 +02001041 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01001042 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1043 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +01001044 ieee80211_tx_skb(sdata, skb);
1045}
1046
Kalle Valo572e0012009-02-10 17:09:31 +02001047void ieee80211_send_pspoll(struct ieee80211_local *local,
1048 struct ieee80211_sub_if_data *sdata)
1049{
Kalle Valo572e0012009-02-10 17:09:31 +02001050 struct ieee80211_pspoll *pspoll;
1051 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +02001052
Kalle Valod8cd1892010-01-05 20:16:26 +02001053 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1054 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +02001055 return;
Kalle Valo572e0012009-02-10 17:09:31 +02001056
Kalle Valod8cd1892010-01-05 20:16:26 +02001057 pspoll = (struct ieee80211_pspoll *) skb->data;
1058 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +02001059
Johannes Berg62ae67b2009-11-18 18:42:05 +01001060 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1061 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +02001062}
1063
Johannes Berg965beda2009-04-16 13:17:24 +02001064void ieee80211_send_nullfunc(struct ieee80211_local *local,
1065 struct ieee80211_sub_if_data *sdata,
Johannes Berg076cdcb2015-09-24 16:14:55 +02001066 bool powersave)
Johannes Berg965beda2009-04-16 13:17:24 +02001067{
1068 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +02001069 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301070 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +02001071
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001072 /* Don't send NDPs when STA is connected HE */
1073 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1074 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1075 return;
1076
Ben Caradoc-Davies7c181f42018-03-19 12:57:44 +13001077 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1078 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
Kalle Valod8cd1892010-01-05 20:16:26 +02001079 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +02001080 return;
1081
Kalle Valod8cd1892010-01-05 20:16:26 +02001082 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +02001083 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +02001084 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +02001085
Seth Forshee6c17b772013-02-11 11:21:07 -06001086 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1087 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +02001088
Johannes Berg30686bf2015-06-02 21:39:54 +02001089 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Pontus Fuchsff40b422013-06-04 12:44:52 +02001090 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1091
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001092 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301093 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1094
Johannes Berg62ae67b2009-11-18 18:42:05 +01001095 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +02001096}
1097
Felix Fietkaud5242152010-01-08 18:06:26 +01001098static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1099 struct ieee80211_sub_if_data *sdata)
1100{
1101 struct sk_buff *skb;
1102 struct ieee80211_hdr *nullfunc;
1103 __le16 fc;
1104
1105 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1106 return;
1107
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001108 /* Don't send NDPs when connected HE */
1109 if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1110 return;
1111
Felix Fietkaud5242152010-01-08 18:06:26 +01001112 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -07001113 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +01001114 return;
Joe Perchesd15b8452011-08-29 14:17:31 -07001115
Felix Fietkaud5242152010-01-08 18:06:26 +01001116 skb_reserve(skb, local->hw.extra_tx_headroom);
1117
Johannes Bergb080db52017-06-16 14:29:19 +02001118 nullfunc = skb_put_zero(skb, 30);
Felix Fietkaud5242152010-01-08 18:06:26 +01001119 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1120 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1121 nullfunc->frame_control = fc;
1122 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1123 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1124 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1125 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1126
1127 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1128 ieee80211_tx_skb(sdata, skb);
1129}
1130
Johannes Bergcc32abd2009-05-15 11:52:31 +02001131/* spectrum management related things */
1132static void ieee80211_chswitch_work(struct work_struct *work)
1133{
1134 struct ieee80211_sub_if_data *sdata =
1135 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +01001136 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001137 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001138 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001139
Johannes Berg9607e6b662009-12-23 13:15:31 +01001140 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +02001141 return;
1142
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001143 sdata_lock(sdata);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001144 mutex_lock(&local->mtx);
1145 mutex_lock(&local->chanctx_mtx);
1146
Johannes Berg77fdaa12009-07-07 03:45:17 +02001147 if (!ifmgd->associated)
1148 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001149
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001150 if (!sdata->vif.csa_active)
1151 goto out;
1152
1153 /*
1154 * using reservation isn't immediate as it may be deferred until later
1155 * with multi-vif. once reservation is complete it will re-schedule the
1156 * work with no reserved_chanctx so verify chandef to check if it
1157 * completed successfully
1158 */
1159
1160 if (sdata->reserved_chanctx) {
Ilan Peer0007e942018-08-31 11:31:10 +03001161 struct ieee80211_supported_band *sband = NULL;
1162 struct sta_info *mgd_sta = NULL;
1163 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1164
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001165 /*
1166 * with multi-vif csa driver may call ieee80211_csa_finish()
1167 * many times while waiting for other interfaces to use their
1168 * reservations
1169 */
1170 if (sdata->reserved_ready)
1171 goto out;
1172
Ilan Peer0007e942018-08-31 11:31:10 +03001173 if (sdata->vif.bss_conf.chandef.width !=
1174 sdata->csa_chandef.width) {
1175 /*
1176 * For managed interface, we need to also update the AP
1177 * station bandwidth and align the rate scale algorithm
1178 * on the bandwidth change. Here we only consider the
1179 * bandwidth of the new channel definition (as channel
1180 * switch flow does not have the full HT/VHT/HE
1181 * information), assuming that if additional changes are
1182 * required they would be done as part of the processing
1183 * of the next beacon from the AP.
1184 */
1185 switch (sdata->csa_chandef.width) {
1186 case NL80211_CHAN_WIDTH_20_NOHT:
1187 case NL80211_CHAN_WIDTH_20:
1188 default:
1189 bw = IEEE80211_STA_RX_BW_20;
1190 break;
1191 case NL80211_CHAN_WIDTH_40:
1192 bw = IEEE80211_STA_RX_BW_40;
1193 break;
1194 case NL80211_CHAN_WIDTH_80:
1195 bw = IEEE80211_STA_RX_BW_80;
1196 break;
1197 case NL80211_CHAN_WIDTH_80P80:
1198 case NL80211_CHAN_WIDTH_160:
1199 bw = IEEE80211_STA_RX_BW_160;
1200 break;
1201 }
1202
1203 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1204 sband =
1205 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1206 }
1207
1208 if (sdata->vif.bss_conf.chandef.width >
1209 sdata->csa_chandef.width) {
1210 mgd_sta->sta.bandwidth = bw;
1211 rate_control_rate_update(local, sband, mgd_sta,
1212 IEEE80211_RC_BW_CHANGED);
1213 }
1214
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001215 ret = ieee80211_vif_use_reserved_context(sdata);
1216 if (ret) {
1217 sdata_info(sdata,
1218 "failed to use reserved channel context, disconnecting (err=%d)\n",
1219 ret);
1220 ieee80211_queue_work(&sdata->local->hw,
1221 &ifmgd->csa_connection_drop_work);
1222 goto out;
1223 }
1224
Ilan Peer0007e942018-08-31 11:31:10 +03001225 if (sdata->vif.bss_conf.chandef.width <
1226 sdata->csa_chandef.width) {
1227 mgd_sta->sta.bandwidth = bw;
1228 rate_control_rate_update(local, sband, mgd_sta,
1229 IEEE80211_RC_BW_CHANGED);
1230 }
1231
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001232 goto out;
1233 }
1234
1235 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1236 &sdata->csa_chandef)) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001237 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001238 "failed to finalize channel switch, disconnecting\n");
Arik Nemtsov7578d572013-09-01 17:15:51 +03001239 ieee80211_queue_work(&sdata->local->hw,
1240 &ifmgd->csa_connection_drop_work);
1241 goto out;
1242 }
Karl Beldan675a0b02013-03-25 16:26:57 +01001243
Luciano Coelho0c21e632014-10-08 09:48:39 +03001244 ifmgd->csa_waiting_bcn = true;
Luciano Coelhof1d65582014-10-08 09:48:38 +03001245
Michal Kaziore5593f52014-04-09 15:45:36 +02001246 ieee80211_sta_reset_beacon_monitor(sdata);
1247 ieee80211_sta_reset_conn_monitor(sdata);
1248
Michal Kazior59af6922014-04-09 15:10:59 +02001249out:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001250 mutex_unlock(&local->chanctx_mtx);
1251 mutex_unlock(&local->mtx);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001252 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001253}
1254
Luciano Coelho0c21e632014-10-08 09:48:39 +03001255static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1256{
1257 struct ieee80211_local *local = sdata->local;
1258 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1259 int ret;
1260
1261 sdata_assert_lock(sdata);
1262
1263 WARN_ON(!sdata->vif.csa_active);
1264
1265 if (sdata->csa_block_tx) {
1266 ieee80211_wake_vif_queues(local, sdata,
1267 IEEE80211_QUEUE_STOP_REASON_CSA);
1268 sdata->csa_block_tx = false;
1269 }
1270
1271 sdata->vif.csa_active = false;
1272 ifmgd->csa_waiting_bcn = false;
1273
1274 ret = drv_post_channel_switch(sdata);
1275 if (ret) {
1276 sdata_info(sdata,
1277 "driver post channel switch failed, disconnecting\n");
1278 ieee80211_queue_work(&local->hw,
1279 &ifmgd->csa_connection_drop_work);
1280 return;
1281 }
Luciano Coelho688b1ec2014-12-16 16:01:39 +02001282
1283 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
Luciano Coelho0c21e632014-10-08 09:48:39 +03001284}
1285
Johannes Berg5ce6e432010-05-11 16:20:57 +02001286void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1287{
Johannes Berg55de9082012-07-26 17:24:39 +02001288 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1289 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001290
1291 trace_api_chswitch_done(sdata, success);
1292 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001293 sdata_info(sdata,
1294 "driver channel switch failed, disconnecting\n");
1295 ieee80211_queue_work(&sdata->local->hw,
1296 &ifmgd->csa_connection_drop_work);
1297 } else {
1298 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001299 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001300}
1301EXPORT_SYMBOL(ieee80211_chswitch_done);
1302
Kees Cook34f11cd2017-10-16 16:35:49 -07001303static void ieee80211_chswitch_timer(struct timer_list *t)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001304{
1305 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07001306 from_timer(sdata, t, u.mgd.chswitch_timer);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001307
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001308 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001309}
1310
Johannes Berg37799e52013-03-26 14:02:26 +01001311static void
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001312ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1313{
1314 struct ieee80211_local *local = sdata->local;
1315
1316 if (!local->ops->abort_channel_switch)
1317 return;
1318
1319 mutex_lock(&local->mtx);
1320
1321 mutex_lock(&local->chanctx_mtx);
1322 ieee80211_vif_unreserve_chanctx(sdata);
1323 mutex_unlock(&local->chanctx_mtx);
1324
1325 if (sdata->csa_block_tx)
1326 ieee80211_wake_vif_queues(local, sdata,
1327 IEEE80211_QUEUE_STOP_REASON_CSA);
1328
1329 sdata->csa_block_tx = false;
1330 sdata->vif.csa_active = false;
1331
1332 mutex_unlock(&local->mtx);
1333
1334 drv_abort_channel_switch(sdata);
1335}
1336
1337static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001338ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03001339 u64 timestamp, u32 device_timestamp,
1340 struct ieee802_11_elems *elems,
Johannes Berg04a161f2013-05-03 09:35:35 +02001341 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001342{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001343 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001344 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001345 struct cfg80211_bss *cbss = ifmgd->associated;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001346 struct ieee80211_chanctx_conf *conf;
Johannes Berg55de9082012-07-26 17:24:39 +02001347 struct ieee80211_chanctx *chanctx;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001348 enum nl80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001349 struct ieee80211_csa_ie csa_ie;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001350 struct ieee80211_channel_switch ch_switch;
Johannes Berg2a333a02020-05-28 21:34:35 +02001351 struct ieee80211_bss *bss;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001352 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001353
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001354 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001355
Johannes Berg37799e52013-03-26 14:02:26 +01001356 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001357 return;
1358
Johannes Bergb4f286a12013-03-26 14:13:58 +01001359 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001360 return;
1361
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001362 current_band = cbss->channel->band;
Johannes Berg2a333a02020-05-28 21:34:35 +02001363 bss = (void *)cbss->priv;
Luciano Coelho84469a42014-10-28 13:33:04 +02001364 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
Johannes Berg2a333a02020-05-28 21:34:35 +02001365 bss->vht_cap_info,
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001366 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001367 ifmgd->associated->bssid, &csa_ie);
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001368
1369 if (!res) {
1370 ch_switch.timestamp = timestamp;
1371 ch_switch.device_timestamp = device_timestamp;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001372 ch_switch.block_tx = csa_ie.mode;
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001373 ch_switch.chandef = csa_ie.chandef;
1374 ch_switch.count = csa_ie.count;
1375 ch_switch.delay = csa_ie.max_switch_time;
1376 }
1377
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001378 if (res < 0) {
Johannes Bergb4f286a12013-03-26 14:13:58 +01001379 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001380 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001381 return;
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001382 }
1383
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001384 if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1385 if (res)
1386 ieee80211_sta_abort_chanswitch(sdata);
1387 else
1388 drv_channel_switch_rx_beacon(sdata, &ch_switch);
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001389 return;
1390 } else if (sdata->vif.csa_active || res) {
1391 /* disregard subsequent announcements if already processing */
1392 return;
1393 }
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001394
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001395 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001396 IEEE80211_CHAN_DISABLED)) {
1397 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001398 "AP %pM switches to unsupported channel "
1399 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1400 "disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001401 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001402 csa_ie.chandef.chan->center_freq,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001403 csa_ie.chandef.chan->freq_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001404 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001405 csa_ie.chandef.freq1_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001406 csa_ie.chandef.center_freq2);
Johannes Berg85220d72013-03-25 18:29:27 +01001407 ieee80211_queue_work(&local->hw,
1408 &ifmgd->csa_connection_drop_work);
1409 return;
1410 }
1411
Johannes Bergf84eaa12015-03-12 08:53:26 +02001412 if (cfg80211_chandef_identical(&csa_ie.chandef,
Sara Sharon97928752019-02-06 13:17:16 +02001413 &sdata->vif.bss_conf.chandef) &&
1414 (!csa_ie.mode || !beacon)) {
Johannes Bergf84eaa12015-03-12 08:53:26 +02001415 if (ifmgd->csa_ignored_same_chan)
1416 return;
1417 sdata_info(sdata,
1418 "AP %pM tries to chanswitch to same channel, ignore\n",
1419 ifmgd->associated->bssid);
1420 ifmgd->csa_ignored_same_chan = true;
1421 return;
1422 }
1423
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001424 /*
1425 * Drop all TDLS peers - either we disconnect or move to a different
1426 * channel from this point on. There's no telling what our peer will do.
1427 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1428 * have an incompatible wider chandef.
1429 */
1430 ieee80211_teardown_tdls_peers(sdata);
1431
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001432 mutex_lock(&local->mtx);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001433 mutex_lock(&local->chanctx_mtx);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001434 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1435 lockdep_is_held(&local->chanctx_mtx));
1436 if (!conf) {
1437 sdata_info(sdata,
1438 "no channel context assigned to vif?, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001439 goto drop_connection;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001440 }
1441
1442 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1443
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001444 if (local->use_chanctx &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001445 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001446 sdata_info(sdata,
1447 "driver doesn't support chan-switch with channel contexts\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001448 goto drop_connection;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001449 }
1450
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001451 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1452 sdata_info(sdata,
1453 "preparing for channel switch failed, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001454 goto drop_connection;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001455 }
1456
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001457 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1458 chanctx->mode, false);
1459 if (res) {
Johannes Berg55de9082012-07-26 17:24:39 +02001460 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001461 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1462 res);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001463 goto drop_connection;
Johannes Berg55de9082012-07-26 17:24:39 +02001464 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001465 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001466
Michal Kazior59af6922014-04-09 15:10:59 +02001467 sdata->vif.csa_active = true;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001468 sdata->csa_chandef = csa_ie.chandef;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001469 sdata->csa_block_tx = csa_ie.mode;
Johannes Bergf84eaa12015-03-12 08:53:26 +02001470 ifmgd->csa_ignored_same_chan = false;
Michal Kazior59af6922014-04-09 15:10:59 +02001471
1472 if (sdata->csa_block_tx)
Luciano Coelhoa46992b2014-06-13 16:30:07 +03001473 ieee80211_stop_vif_queues(local, sdata,
1474 IEEE80211_QUEUE_STOP_REASON_CSA);
Michal Kazior59af6922014-04-09 15:10:59 +02001475 mutex_unlock(&local->mtx);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001476
Luciano Coelho2f457292014-11-07 14:31:36 +02001477 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1478 csa_ie.count);
1479
Johannes Bergb4f286a12013-03-26 14:13:58 +01001480 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001481 /* use driver's channel switch callback */
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001482 drv_channel_switch(local, sdata, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001483 return;
1484 }
1485
1486 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001487 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001488 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001489 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001490 mod_timer(&ifmgd->chswitch_timer,
Luciano Coelhoff1e4172014-10-28 13:33:05 +02001491 TU_TO_EXP_TIME((csa_ie.count - 1) *
1492 cbss->beacon_interval));
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001493 return;
1494 drop_connection:
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001495 /*
1496 * This is just so that the disconnect flow will know that
1497 * we were trying to switch channel and failed. In case the
1498 * mode is 1 (we are not allowed to Tx), we will know not to
1499 * send a deauthentication frame. Those two fields will be
1500 * reset when the disconnection worker runs.
1501 */
1502 sdata->vif.csa_active = true;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001503 sdata->csa_block_tx = csa_ie.mode;
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001504
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001505 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1506 mutex_unlock(&local->chanctx_mtx);
1507 mutex_unlock(&local->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001508}
1509
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001510static bool
1511ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1512 struct ieee80211_channel *channel,
1513 const u8 *country_ie, u8 country_ie_len,
1514 const u8 *pwr_constr_elem,
1515 int *chan_pwr, int *pwr_reduction)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001516{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001517 struct ieee80211_country_ie_triplet *triplet;
1518 int chan = ieee80211_frequency_to_channel(channel->center_freq);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001519 int i, chan_increment;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001520 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001521
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001522 /* Invalid IE */
1523 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001524 return false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001525
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001526 triplet = (void *)(country_ie + 3);
1527 country_ie_len -= 3;
1528
1529 switch (channel->band) {
1530 default:
1531 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001532 fallthrough;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001533 case NL80211_BAND_2GHZ:
1534 case NL80211_BAND_60GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001535 chan_increment = 1;
1536 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001537 case NL80211_BAND_5GHZ:
Ilan Peer6fcb56c2020-05-28 21:34:45 +02001538 case NL80211_BAND_6GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001539 chan_increment = 4;
1540 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001541 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001542
1543 /* find channel */
1544 while (country_ie_len >= 3) {
1545 u8 first_channel = triplet->chans.first_channel;
1546
1547 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1548 goto next;
1549
1550 for (i = 0; i < triplet->chans.num_channels; i++) {
1551 if (first_channel + i * chan_increment == chan) {
1552 have_chan_pwr = true;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001553 *chan_pwr = triplet->chans.max_power;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001554 break;
1555 }
1556 }
1557 if (have_chan_pwr)
1558 break;
1559
1560 next:
1561 triplet++;
1562 country_ie_len -= 3;
1563 }
1564
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001565 if (have_chan_pwr && pwr_constr_elem)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001566 *pwr_reduction = *pwr_constr_elem;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001567 else
1568 *pwr_reduction = 0;
1569
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001570 return have_chan_pwr;
1571}
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001572
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001573static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1574 struct ieee80211_channel *channel,
1575 const u8 *cisco_dtpc_ie,
1576 int *pwr_level)
1577{
1578 /* From practical testing, the first data byte of the DTPC element
1579 * seems to contain the requested dBm level, and the CLI on Cisco
1580 * APs clearly state the range is -127 to 127 dBm, which indicates
1581 * a signed byte, although it seemingly never actually goes negative.
1582 * The other byte seems to always be zero.
1583 */
1584 *pwr_level = (__s8)cisco_dtpc_ie[4];
1585}
1586
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001587static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1588 struct ieee80211_channel *channel,
1589 struct ieee80211_mgmt *mgmt,
1590 const u8 *country_ie, u8 country_ie_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001591 const u8 *pwr_constr_ie,
1592 const u8 *cisco_dtpc_ie)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001593{
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001594 bool has_80211h_pwr = false, has_cisco_pwr = false;
1595 int chan_pwr = 0, pwr_reduction_80211h = 0;
1596 int pwr_level_cisco, pwr_level_80211h;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001597 int new_ap_level;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001598 __le16 capab = mgmt->u.probe_resp.capab_info;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001599
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001600 if (country_ie &&
1601 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1602 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001603 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1604 sdata, channel, country_ie, country_ie_len,
1605 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001606 pwr_level_80211h =
1607 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001608 }
1609
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001610 if (cisco_dtpc_ie) {
1611 ieee80211_find_cisco_dtpc(
1612 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1613 has_cisco_pwr = true;
1614 }
1615
1616 if (!has_80211h_pwr && !has_cisco_pwr)
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001617 return 0;
1618
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001619 /* If we have both 802.11h and Cisco DTPC, apply both limits
1620 * by picking the smallest of the two power levels advertised.
1621 */
1622 if (has_80211h_pwr &&
1623 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
Johannes Berga87da0c2015-12-08 16:04:37 +02001624 new_ap_level = pwr_level_80211h;
1625
1626 if (sdata->ap_power_level == new_ap_level)
1627 return 0;
1628
John Linvillecef2fc12015-03-31 10:49:14 -04001629 sdata_dbg(sdata,
1630 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1631 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1632 sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001633 } else { /* has_cisco_pwr is always true here. */
Johannes Berga87da0c2015-12-08 16:04:37 +02001634 new_ap_level = pwr_level_cisco;
1635
1636 if (sdata->ap_power_level == new_ap_level)
1637 return 0;
1638
John Linvillecef2fc12015-03-31 10:49:14 -04001639 sdata_dbg(sdata,
1640 "Limiting TX power to %d dBm as advertised by %pM\n",
1641 pwr_level_cisco, sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001642 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001643
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001644 sdata->ap_power_level = new_ap_level;
1645 if (__ieee80211_recalc_txpower(sdata))
1646 return BSS_CHANGED_TXPOWER;
1647 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001648}
1649
Johannes Berg965beda2009-04-16 13:17:24 +02001650/* powersave */
1651static void ieee80211_enable_ps(struct ieee80211_local *local,
1652 struct ieee80211_sub_if_data *sdata)
1653{
1654 struct ieee80211_conf *conf = &local->hw.conf;
1655
Johannes Bergd5edaed2009-04-22 23:02:51 +02001656 /*
1657 * If we are scanning right now then the parameters will
1658 * take effect when scan finishes.
1659 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001660 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001661 return;
1662
Johannes Berg965beda2009-04-16 13:17:24 +02001663 if (conf->dynamic_ps_timeout > 0 &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001664 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001665 mod_timer(&local->dynamic_ps_timer, jiffies +
1666 msecs_to_jiffies(conf->dynamic_ps_timeout));
1667 } else {
Johannes Berg30686bf2015-06-02 21:39:54 +02001668 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
Johannes Berg076cdcb2015-09-24 16:14:55 +02001669 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301670
Johannes Berg30686bf2015-06-02 21:39:54 +02001671 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1672 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001673 return;
1674
1675 conf->flags |= IEEE80211_CONF_PS;
1676 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001677 }
1678}
1679
1680static void ieee80211_change_ps(struct ieee80211_local *local)
1681{
1682 struct ieee80211_conf *conf = &local->hw.conf;
1683
1684 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001685 ieee80211_enable_ps(local, local->ps_sdata);
1686 } else if (conf->flags & IEEE80211_CONF_PS) {
1687 conf->flags &= ~IEEE80211_CONF_PS;
1688 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1689 del_timer_sync(&local->dynamic_ps_timer);
1690 cancel_work_sync(&local->dynamic_ps_enable_work);
1691 }
1692}
1693
Jason Young808118c2011-03-10 16:43:19 -08001694static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1695{
1696 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1697 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001698 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001699
1700 if (!mgd->powersave)
1701 return false;
1702
Johannes Berg05cb9102011-10-28 11:59:47 +02001703 if (mgd->broken_ap)
1704 return false;
1705
Jason Young808118c2011-03-10 16:43:19 -08001706 if (!mgd->associated)
1707 return false;
1708
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001709 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001710 return false;
1711
Alexander Bondar989c6502013-05-16 17:34:17 +03001712 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001713 return false;
1714
Jason Young808118c2011-03-10 16:43:19 -08001715 rcu_read_lock();
1716 sta = sta_info_get(sdata, mgd->bssid);
1717 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001718 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001719 rcu_read_unlock();
1720
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001721 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001722}
1723
Johannes Berg965beda2009-04-16 13:17:24 +02001724/* need to hold RTNL or interface lock */
Johannes Berg4a733ef2015-10-14 18:02:43 +02001725void ieee80211_recalc_ps(struct ieee80211_local *local)
Johannes Berg965beda2009-04-16 13:17:24 +02001726{
1727 struct ieee80211_sub_if_data *sdata, *found = NULL;
1728 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001729 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001730
Johannes Berg30686bf2015-06-02 21:39:54 +02001731 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001732 local->ps_sdata = NULL;
1733 return;
1734 }
1735
1736 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001737 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001738 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301739 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1740 /* If an AP vif is found, then disable PS
1741 * by setting the count to zero thereby setting
1742 * ps_sdata to NULL.
1743 */
1744 count = 0;
1745 break;
1746 }
Johannes Berg965beda2009-04-16 13:17:24 +02001747 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1748 continue;
1749 found = sdata;
1750 count++;
1751 }
1752
Jason Young808118c2011-03-10 16:43:19 -08001753 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg4a733ef2015-10-14 18:02:43 +02001754 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg10f644a2009-04-16 13:17:25 +02001755
Juuso Oikarinenff616382010-06-09 09:51:52 +03001756 timeout = local->dynamic_ps_forced_timeout;
Johannes Berg4a733ef2015-10-14 18:02:43 +02001757 if (timeout < 0)
1758 timeout = 100;
Johannes Berg09b85562013-02-06 23:07:41 +01001759 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001760
Johannes Berg4a733ef2015-10-14 18:02:43 +02001761 /* If the TIM IE is invalid, pretend the value is 1 */
1762 if (!dtimper)
1763 dtimper = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001764
Johannes Berg4a733ef2015-10-14 18:02:43 +02001765 local->hw.conf.ps_dtim_period = dtimper;
1766 local->ps_sdata = found;
Johannes Berg10f644a2009-04-16 13:17:25 +02001767 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001768 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001769 }
Johannes Berg965beda2009-04-16 13:17:24 +02001770
1771 ieee80211_change_ps(local);
1772}
1773
Eliad Pellerab095872012-07-27 12:33:22 +03001774void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1775{
1776 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1777
1778 if (sdata->vif.bss_conf.ps != ps_allowed) {
1779 sdata->vif.bss_conf.ps = ps_allowed;
1780 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1781 }
1782}
1783
Johannes Berg965beda2009-04-16 13:17:24 +02001784void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1785{
1786 struct ieee80211_local *local =
1787 container_of(work, struct ieee80211_local,
1788 dynamic_ps_disable_work);
1789
1790 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1791 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1792 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1793 }
1794
1795 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001796 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +03001797 IEEE80211_QUEUE_STOP_REASON_PS,
1798 false);
Johannes Berg965beda2009-04-16 13:17:24 +02001799}
1800
1801void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1802{
1803 struct ieee80211_local *local =
1804 container_of(work, struct ieee80211_local,
1805 dynamic_ps_enable_work);
1806 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001807 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301808 unsigned long flags;
1809 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001810
1811 /* can only happen when PS was just disabled anyway */
1812 if (!sdata)
1813 return;
1814
Christian Lamparter5e340692011-06-30 21:08:43 +02001815 ifmgd = &sdata->u.mgd;
1816
Johannes Berg965beda2009-04-16 13:17:24 +02001817 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1818 return;
1819
Johannes Berg09b85562013-02-06 23:07:41 +01001820 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001821 /* don't enter PS if TX frames are pending */
1822 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301823 mod_timer(&local->dynamic_ps_timer, jiffies +
1824 msecs_to_jiffies(
1825 local->hw.conf.dynamic_ps_timeout));
1826 return;
1827 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001828
1829 /*
1830 * transmission can be stopped by others which leads to
1831 * dynamic_ps_timer expiry. Postpone the ps timer if it
1832 * is not the actual idle state.
1833 */
1834 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1835 for (q = 0; q < local->hw.queues; q++) {
1836 if (local->queue_stop_reasons[q]) {
1837 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1838 flags);
1839 mod_timer(&local->dynamic_ps_timer, jiffies +
1840 msecs_to_jiffies(
1841 local->hw.conf.dynamic_ps_timeout));
1842 return;
1843 }
1844 }
1845 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301846 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301847
Johannes Berg30686bf2015-06-02 21:39:54 +02001848 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301849 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga15983832013-03-26 22:02:42 +01001850 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301851 mod_timer(&local->dynamic_ps_timer, jiffies +
1852 msecs_to_jiffies(
1853 local->hw.conf.dynamic_ps_timeout));
Johannes Berga15983832013-03-26 22:02:42 +01001854 } else {
Johannes Berg076cdcb2015-09-24 16:14:55 +02001855 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301856 /* Flush to get the tx status of nullfunc frame */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001857 ieee80211_flush_queues(local, sdata, false);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301858 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301859 }
1860
Johannes Berg30686bf2015-06-02 21:39:54 +02001861 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1862 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301863 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1864 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1865 local->hw.conf.flags |= IEEE80211_CONF_PS;
1866 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1867 }
Johannes Berg965beda2009-04-16 13:17:24 +02001868}
1869
Kees Cook34f11cd2017-10-16 16:35:49 -07001870void ieee80211_dynamic_ps_timer(struct timer_list *t)
Johannes Berg965beda2009-04-16 13:17:24 +02001871{
Kees Cook34f11cd2017-10-16 16:35:49 -07001872 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
Johannes Berg965beda2009-04-16 13:17:24 +02001873
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001874 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001875}
1876
Simon Wunderlich164eb022013-02-08 18:16:20 +01001877void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1878{
Geliang Tanga85a7e22016-01-01 23:48:52 +08001879 struct delayed_work *delayed_work = to_delayed_work(work);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001880 struct ieee80211_sub_if_data *sdata =
1881 container_of(delayed_work, struct ieee80211_sub_if_data,
1882 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001883 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001884
Johannes Berg34a37402013-12-18 09:43:33 +01001885 mutex_lock(&sdata->local->mtx);
1886 if (sdata->wdev.cac_started) {
1887 ieee80211_vif_release_channel(sdata);
1888 cfg80211_cac_event(sdata->dev, &chandef,
1889 NL80211_RADAR_CAC_FINISHED,
1890 GFP_KERNEL);
1891 }
1892 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001893}
1894
Johannes Berg02219b32014-10-07 10:38:50 +03001895static bool
1896__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1897{
1898 struct ieee80211_local *local = sdata->local;
1899 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
John Linvillecc72f6e2015-01-06 14:39:33 -05001900 bool ret = false;
Johannes Berg02219b32014-10-07 10:38:50 +03001901 int ac;
1902
1903 if (local->hw.queues < IEEE80211_NUM_ACS)
1904 return false;
1905
1906 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1907 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1908 int non_acm_ac;
1909 unsigned long now = jiffies;
1910
1911 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1912 tx_tspec->admitted_time &&
1913 time_after(now, tx_tspec->time_slice_start + HZ)) {
1914 tx_tspec->consumed_tx_time = 0;
1915 tx_tspec->time_slice_start = now;
1916
1917 if (tx_tspec->downgraded)
1918 tx_tspec->action =
1919 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1920 }
1921
1922 switch (tx_tspec->action) {
1923 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1924 /* take the original parameters */
1925 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1926 sdata_err(sdata,
1927 "failed to set TX queue parameters for queue %d\n",
1928 ac);
1929 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1930 tx_tspec->downgraded = false;
1931 ret = true;
1932 break;
1933 case TX_TSPEC_ACTION_DOWNGRADE:
1934 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1935 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1936 ret = true;
1937 break;
1938 }
1939 /* downgrade next lower non-ACM AC */
1940 for (non_acm_ac = ac + 1;
1941 non_acm_ac < IEEE80211_NUM_ACS;
1942 non_acm_ac++)
1943 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1944 break;
Johannes Berg93db1d92016-09-14 09:23:51 +02001945 /* Usually the loop will result in using BK even if it
1946 * requires admission control, but such a configuration
1947 * makes no sense and we have to transmit somehow - the
1948 * AC selection does the same thing.
1949 * If we started out trying to downgrade from BK, then
1950 * the extra condition here might be needed.
Johannes Berg02219b32014-10-07 10:38:50 +03001951 */
Johannes Berg93db1d92016-09-14 09:23:51 +02001952 if (non_acm_ac >= IEEE80211_NUM_ACS)
1953 non_acm_ac = IEEE80211_AC_BK;
Johannes Berg02219b32014-10-07 10:38:50 +03001954 if (drv_conf_tx(local, sdata, ac,
1955 &sdata->tx_conf[non_acm_ac]))
1956 sdata_err(sdata,
1957 "failed to set TX queue parameters for queue %d\n",
1958 ac);
1959 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1960 ret = true;
1961 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1962 tx_tspec->time_slice_start + HZ - now + 1);
1963 break;
1964 case TX_TSPEC_ACTION_NONE:
1965 /* nothing now */
1966 break;
1967 }
1968 }
1969
1970 return ret;
1971}
1972
1973void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1974{
1975 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1976 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1977}
1978
1979static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1980{
1981 struct ieee80211_sub_if_data *sdata;
1982
1983 sdata = container_of(work, struct ieee80211_sub_if_data,
1984 u.mgd.tx_tspec_wk.work);
1985 ieee80211_sta_handle_tspec_ac_params(sdata);
1986}
1987
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001988/* MLME */
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001989static bool
1990ieee80211_sta_wmm_params(struct ieee80211_local *local,
1991 struct ieee80211_sub_if_data *sdata,
1992 const u8 *wmm_param, size_t wmm_param_len,
1993 const struct ieee80211_mu_edca_param_set *mu_edca)
Jiri Bencf0706e82007-05-05 11:45:53 -07001994{
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001995 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
Johannes Berg4ced3f72010-07-19 16:39:04 +02001996 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001997 size_t left;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001998 int count, mu_edca_count, ac;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001999 const u8 *pos;
2000 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002001
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002002 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02002003 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002004
Johannes Berg32c50572012-03-28 11:04:29 +02002005 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02002006 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02002007
2008 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02002009 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02002010
Jiri Bencf0706e82007-05-05 11:45:53 -07002011 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02002012 return false;
Kalle Valoab133152010-01-12 10:42:31 +02002013
2014 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02002015 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02002016
Jiri Bencf0706e82007-05-05 11:45:53 -07002017 count = wmm_param[6] & 0x0f;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002018 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2019 * if mu_edca was preset before and now it disappeared tell
2020 * the driver about it.
2021 */
2022 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2023 if (count == ifmgd->wmm_last_param_set &&
2024 mu_edca_count == ifmgd->mu_edca_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02002025 return false;
Johannes Berg46900292009-02-15 12:44:28 +01002026 ifmgd->wmm_last_param_set = count;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002027 ifmgd->mu_edca_last_param_set = mu_edca_count;
Jiri Bencf0706e82007-05-05 11:45:53 -07002028
2029 pos = wmm_param + 8;
2030 left = wmm_param_len - 8;
2031
2032 memset(&params, 0, sizeof(params));
2033
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002034 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002035 for (; left >= 4; left -= 4, pos += 4) {
2036 int aci = (pos[0] >> 5) & 0x03;
2037 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02002038 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002039
2040 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002041 case 1: /* AC_BK */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002042 ac = IEEE80211_AC_BK;
Johannes Berg988c0f72008-04-17 19:21:22 +02002043 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002044 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02002045 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2046 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002047 params[ac].mu_edca = !!mu_edca;
2048 if (mu_edca)
2049 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
Jiri Bencf0706e82007-05-05 11:45:53 -07002050 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002051 case 2: /* AC_VI */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002052 ac = IEEE80211_AC_VI;
Johannes Berg988c0f72008-04-17 19:21:22 +02002053 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002054 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02002055 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2056 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002057 params[ac].mu_edca = !!mu_edca;
2058 if (mu_edca)
2059 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
Jiri Bencf0706e82007-05-05 11:45:53 -07002060 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002061 case 3: /* AC_VO */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002062 ac = IEEE80211_AC_VO;
Johannes Berg988c0f72008-04-17 19:21:22 +02002063 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002064 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02002065 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2066 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002067 params[ac].mu_edca = !!mu_edca;
2068 if (mu_edca)
2069 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
Jiri Bencf0706e82007-05-05 11:45:53 -07002070 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002071 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07002072 default:
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002073 ac = IEEE80211_AC_BE;
Johannes Berg988c0f72008-04-17 19:21:22 +02002074 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002075 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02002076 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2077 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002078 params[ac].mu_edca = !!mu_edca;
2079 if (mu_edca)
2080 params[ac].mu_edca_param_rec = mu_edca->ac_be;
Jiri Bencf0706e82007-05-05 11:45:53 -07002081 break;
2082 }
2083
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002084 params[ac].aifs = pos[0] & 0x0f;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002085
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002086 if (params[ac].aifs < 2) {
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002087 sdata_info(sdata,
2088 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002089 params[ac].aifs, aci);
2090 params[ac].aifs = 2;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002091 }
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002092 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2093 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2094 params[ac].txop = get_unaligned_le16(pos + 2);
2095 params[ac].acm = acm;
2096 params[ac].uapsd = uapsd;
Kalle Valoab133152010-01-12 10:42:31 +02002097
Ilan Peer911a2642018-04-03 11:35:22 +03002098 if (params[ac].cw_min == 0 ||
Emmanuel Grumbachc470bdc2018-03-26 16:21:04 +03002099 params[ac].cw_min > params[ac].cw_max) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002100 sdata_info(sdata,
2101 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2102 params[ac].cw_min, params[ac].cw_max, aci);
2103 return false;
2104 }
Haim Dreyfusse552af02018-03-28 13:24:10 +03002105 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002106 }
2107
Brian Norris05aaa5c2019-07-26 15:47:58 -07002108 /* WMM specification requires all 4 ACIs. */
2109 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2110 if (params[ac].cw_min == 0) {
2111 sdata_info(sdata,
2112 "AP has invalid WMM params (missing AC %d), using defaults\n",
2113 ac);
2114 return false;
2115 }
2116 }
2117
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002118 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002119 mlme_dbg(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002120 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2121 ac, params[ac].acm,
2122 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2123 params[ac].txop, params[ac].uapsd,
2124 ifmgd->tx_tspec[ac].downgraded);
2125 sdata->tx_conf[ac] = params[ac];
2126 if (!ifmgd->tx_tspec[ac].downgraded &&
2127 drv_conf_tx(local, sdata, ac, &params[ac]))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002128 sdata_err(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002129 "failed to set TX queue parameters for AC %d\n",
2130 ac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002131 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002132
2133 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02002134 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02002135 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002136}
2137
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002138static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2139{
2140 lockdep_assert_held(&sdata->local->mtx);
2141
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002142 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002143 ieee80211_run_deferred_scan(sdata->local);
2144}
2145
2146static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2147{
2148 mutex_lock(&sdata->local->mtx);
2149 __ieee80211_stop_poll(sdata);
2150 mutex_unlock(&sdata->local->mtx);
2151}
2152
Johannes Berg7a5158e2008-10-08 10:59:33 +02002153static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2154 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02002155{
Johannes Bergbda39332008-10-11 01:51:51 +02002156 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302157 struct ieee80211_supported_band *sband;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002158 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002159 bool use_protection;
2160 bool use_short_preamble;
2161 bool use_short_slot;
2162
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302163 sband = ieee80211_get_sband(sdata);
2164 if (!sband)
2165 return changed;
2166
Johannes Berg7a5158e2008-10-08 10:59:33 +02002167 if (erp_valid) {
2168 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2169 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2170 } else {
2171 use_protection = false;
2172 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2173 }
2174
2175 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Tova Mussai07c12d62020-05-28 21:34:46 +02002176 if (sband->band == NL80211_BAND_5GHZ ||
2177 sband->band == NL80211_BAND_6GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01002178 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02002179
Johannes Berg471b3ef2007-12-28 14:32:58 +01002180 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01002181 bss_conf->use_cts_prot = use_protection;
2182 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02002183 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02002184
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002185 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002186 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002187 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02002188 }
Daniel Draked9430a32007-07-27 15:43:24 +02002189
Johannes Berg7a5158e2008-10-08 10:59:33 +02002190 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02002191 bss_conf->use_short_slot = use_short_slot;
2192 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04002193 }
2194
2195 return changed;
2196}
2197
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002198static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002199 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02002200 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07002201{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002202 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002203 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002204 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002205
Johannes Bergae5eb022008-10-14 16:58:37 +02002206 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002207 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03002208 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002209
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002210 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07002211 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002212
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002213 sdata->u.mgd.associated = cbss;
2214 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01002215
Johannes Berge8e4f522017-03-08 11:12:10 +01002216 ieee80211_check_rate_mask(sdata);
2217
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002218 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2219
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01002220 if (sdata->vif.p2p ||
2221 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002222 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002223
Johannes Berg9caf0362012-11-29 01:25:20 +01002224 rcu_read_lock();
2225 ies = rcu_dereference(cbss->ies);
2226 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002227 int ret;
2228
2229 ret = cfg80211_get_p2p_attr(
2230 ies->data, ies->len,
2231 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002232 (u8 *) &bss_conf->p2p_noa_attr,
2233 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01002234 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002235 sdata->u.mgd.p2p_noa_index =
2236 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01002237 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01002238 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01002239 }
Johannes Berg9caf0362012-11-29 01:25:20 +01002240 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01002241 }
2242
Johannes Bergb291ba12009-07-10 15:29:03 +02002243 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002244 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002245
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002246 ieee80211_led_assoc(local, 1);
2247
Alexander Bondar989c6502013-05-16 17:34:17 +03002248 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02002249 /*
2250 * If the AP is buggy we may get here with no DTIM period
2251 * known, so assume it's 1 which is the only safe assumption
2252 * in that case, although if the TIM IE is broken powersave
2253 * probably just won't work at all.
2254 */
2255 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03002256 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03002257 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02002258 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03002259 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02002260 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02002261 }
Johannes Berge5b900d2010-07-29 16:08:55 +02002262
Juuso Oikarinen68542962010-06-09 13:43:26 +03002263 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02002264
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002265 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01002266 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03002267 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002268 bss_info_changed |= BSS_CHANGED_CQM;
2269
Juuso Oikarinen68542962010-06-09 13:43:26 +03002270 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002271 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002272 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002273
Johannes Bergae5eb022008-10-14 16:58:37 +02002274 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03002275
Johannes Berg056508d2009-07-30 21:43:55 +02002276 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002277 ieee80211_recalc_ps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02002278 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02002279
Johannes Berg04ecd252012-09-11 14:34:12 +02002280 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03002281 ieee80211_recalc_ps_vif(sdata);
2282
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002283 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002284}
2285
Jouni Malinene69e95d2010-03-29 23:29:31 -07002286static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01002287 u16 stype, u16 reason, bool tx,
2288 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03002289{
Johannes Berg46900292009-02-15 12:44:28 +01002290 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002291 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01002292 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002293
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002294 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002295
Johannes Berg37ad3882012-02-24 13:50:54 +01002296 if (WARN_ON_ONCE(tx && !frame_buf))
2297 return;
2298
Johannes Bergb291ba12009-07-10 15:29:03 +02002299 if (WARN_ON(!ifmgd->associated))
2300 return;
2301
David Spinadel79543d82012-06-12 09:59:45 +03002302 ieee80211_stop_poll(sdata);
2303
Johannes Berg77fdaa12009-07-07 03:45:17 +02002304 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002305 netif_carrier_off(sdata->dev);
2306
Eliad Peller88bc40e2012-07-12 17:35:33 +03002307 /*
2308 * if we want to get out of ps before disassoc (why?) we have
2309 * to do it before sending disassoc, as otherwise the null-packet
2310 * won't be valid.
2311 */
2312 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2313 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2314 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2315 }
2316 local->ps_sdata = NULL;
2317
Eliad Pellerab095872012-07-27 12:33:22 +03002318 /* disable per-vif ps */
2319 ieee80211_recalc_ps_vif(sdata);
2320
Emmanuel Grumbach14f2ae82015-01-22 23:30:19 +02002321 /* make sure ongoing transmission finishes */
2322 synchronize_net();
2323
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002324 /*
2325 * drop any frame before deauth/disassoc, this can be data or
2326 * management frame. Since we are disconnecting, we should not
2327 * insist sending these frames which can take time and delay
2328 * the disconnection and possible the roaming.
2329 */
Eliad Pellerf8239812012-06-27 14:18:22 +03002330 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002331 ieee80211_flush_queues(local, sdata, true);
Eliad Pellerf8239812012-06-27 14:18:22 +03002332
Johannes Berg37ad3882012-02-24 13:50:54 +01002333 /* deauthenticate/disassociate now */
Ilan Peer94ba9272018-02-19 14:48:41 +02002334 if (tx || frame_buf) {
Ilan Peer94ba9272018-02-19 14:48:41 +02002335 /*
2336 * In multi channel scenarios guarantee that the virtual
2337 * interface is granted immediate airtime to transmit the
2338 * deauthentication frame by calling mgd_prepare_tx, if the
2339 * driver requested so.
2340 */
2341 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2342 !ifmgd->have_beacon)
Ilan Peerd4e36e52018-04-20 13:49:25 +03002343 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Ilan Peer94ba9272018-02-19 14:48:41 +02002344
Johannes Berg4b08d1b2019-08-30 14:24:51 +03002345 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2346 ifmgd->bssid, stype, reason,
2347 tx, frame_buf);
Ilan Peer94ba9272018-02-19 14:48:41 +02002348 }
Johannes Berg37ad3882012-02-24 13:50:54 +01002349
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002350 /* flush out frame - make sure the deauth was actually sent */
Johannes Berg37ad3882012-02-24 13:50:54 +01002351 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002352 ieee80211_flush_queues(local, sdata, false);
Johannes Berg37ad3882012-02-24 13:50:54 +01002353
Eliad Peller88a9e312012-06-01 11:14:03 +03002354 /* clear bssid only after building the needed mgmt frames */
Joe Perchesc84a67a2015-03-02 19:54:57 -08002355 eth_zero_addr(ifmgd->bssid);
Eliad Peller88a9e312012-06-01 11:14:03 +03002356
Johannes Berg37ad3882012-02-24 13:50:54 +01002357 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01002358 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01002359
2360 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002361 changed |= ieee80211_reset_erp_info(sdata);
2362
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002363 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02002364 changed |= BSS_CHANGED_ASSOC;
2365 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002366
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002367 ifmgd->p2p_noa_index = -1;
2368 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2369 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002370
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002371 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08002372 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2373 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002374 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2375 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002376
2377 /* reset MU-MIMO ownership and group data */
2378 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2379 sizeof(sdata->vif.bss_conf.mu_group.membership));
2380 memset(sdata->vif.bss_conf.mu_group.position, 0,
2381 sizeof(sdata->vif.bss_conf.mu_group.position));
2382 changed |= BSS_CHANGED_MU_GROUPS;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002383 sdata->vif.mu_mimo_owner = false;
Johannes Berg413ad502009-05-08 21:21:06 +02002384
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002385 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302386
Kalle Valo520eb822008-12-18 23:35:27 +02002387 del_timer_sync(&local->dynamic_ps_timer);
2388 cancel_work_sync(&local->dynamic_ps_enable_work);
2389
Juuso Oikarinen68542962010-06-09 13:43:26 +03002390 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002391 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002392 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002393
Johannes Berg3abead52012-03-02 15:56:59 +01002394 sdata->vif.bss_conf.qos = false;
2395 changed |= BSS_CHANGED_QOS;
2396
Johannes Berg0aaffa92010-05-05 15:28:27 +02002397 /* The BSSID (not really interesting) and HT changed */
2398 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02002399 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02002400
Johannes Berg3abead52012-03-02 15:56:59 +01002401 /* disassociated - set to defaults now */
Johannes Bergcec66282015-10-22 17:46:04 +02002402 ieee80211_set_wmm_default(sdata, false, false);
Johannes Berg3abead52012-03-02 15:56:59 +01002403
Johannes Bergb9dcf712010-08-27 12:35:54 +02002404 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2405 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2406 del_timer_sync(&sdata->u.mgd.timer);
2407 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02002408
Johannes Berg826262c2012-12-10 16:38:14 +02002409 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03002410 sdata->vif.bss_conf.beacon_rate = NULL;
2411
Alexander Bondar989c6502013-05-16 17:34:17 +03002412 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02002413
Johannes Berg028e8da02012-11-26 11:57:41 +01002414 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002415 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01002416 ieee80211_vif_release_channel(sdata);
Michal Kazior59af6922014-04-09 15:10:59 +02002417
2418 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002419 ifmgd->csa_waiting_bcn = false;
Johannes Bergf84eaa12015-03-12 08:53:26 +02002420 ifmgd->csa_ignored_same_chan = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002421 if (sdata->csa_block_tx) {
2422 ieee80211_wake_vif_queues(local, sdata,
2423 IEEE80211_QUEUE_STOP_REASON_CSA);
2424 sdata->csa_block_tx = false;
2425 }
Johannes Berg34a37402013-12-18 09:43:33 +01002426 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002427
Johannes Berg02219b32014-10-07 10:38:50 +03002428 /* existing TX TSPEC sessions no longer exist */
2429 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2430 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2431
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002432 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002433}
Jiri Bencf0706e82007-05-05 11:45:53 -07002434
Kalle Valo3cf335d52009-03-22 21:57:06 +02002435void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2436 struct ieee80211_hdr *hdr)
2437{
2438 /*
2439 * We can postpone the mgd.timer whenever receiving unicast frames
2440 * from AP because we know that the connection is working both ways
2441 * at that time. But multicast frames (and hence also beacons) must
2442 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02002443 * data idle periods for sending the periodic probe request to the
2444 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02002445 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002446 if (is_multicast_ether_addr(hdr->addr1))
2447 return;
2448
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04002449 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02002450}
Jiri Bencf0706e82007-05-05 11:45:53 -07002451
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002452static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2453{
2454 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002455 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002456
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002457 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002458 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2459 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002460
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002461 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002462
2463 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002464 ieee80211_recalc_ps(local);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002465 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002466
Johannes Berg30686bf2015-06-02 21:39:54 +02002467 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002468 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002469
2470 /*
2471 * We've received a probe response, but are not sure whether
2472 * we have or will be receiving any beacons or data, so let's
2473 * schedule the timers again, just in case.
2474 */
2475 ieee80211_sta_reset_beacon_monitor(sdata);
2476
2477 mod_timer(&ifmgd->conn_mon_timer,
2478 round_jiffies_up(jiffies +
2479 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002480out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002481 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002482}
2483
Johannes Berg02219b32014-10-07 10:38:50 +03002484static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2485 struct ieee80211_hdr *hdr,
2486 u16 tx_time)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002487{
Johannes Berg02219b32014-10-07 10:38:50 +03002488 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02002489 u16 tid = ieee80211_get_tid(hdr);
Johannes Berg02219b32014-10-07 10:38:50 +03002490 int ac = ieee80211_ac_from_tid(tid);
2491 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2492 unsigned long now = jiffies;
2493
2494 if (likely(!tx_tspec->admitted_time))
2495 return;
2496
2497 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2498 tx_tspec->consumed_tx_time = 0;
2499 tx_tspec->time_slice_start = now;
2500
2501 if (tx_tspec->downgraded) {
2502 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2503 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2504 }
2505 }
2506
2507 if (tx_tspec->downgraded)
2508 return;
2509
2510 tx_tspec->consumed_tx_time += tx_time;
2511
2512 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2513 tx_tspec->downgraded = true;
2514 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2515 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2516 }
2517}
2518
2519void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2520 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2521{
2522 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2523
Felix Fietkau75706d02010-12-02 21:01:07 +01002524 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002525 return;
2526
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002527 if (ieee80211_is_any_nullfunc(hdr->frame_control) &&
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002528 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002529 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002530 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002531 else
2532 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002533 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002534 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002535 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002536
2537 if (ack)
2538 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002539}
2540
Johannes Berg45ad6832018-05-28 15:47:39 +02002541static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2542 const u8 *src, const u8 *dst,
2543 const u8 *ssid, size_t ssid_len,
2544 struct ieee80211_channel *channel)
2545{
2546 struct sk_buff *skb;
2547
2548 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2549 ssid, ssid_len, NULL, 0,
2550 IEEE80211_PROBE_FLAG_DIRECTED);
2551 if (skb)
2552 ieee80211_tx_skb(sdata, skb);
2553}
2554
Maxim Levitskya43abf22009-07-31 18:54:12 +03002555static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2556{
2557 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2558 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002559 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002560 u8 unicast_limit = max(1, max_probe_tries - 3);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002561 struct sta_info *sta;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002562
2563 /*
2564 * Try sending broadcast probe requests for the last three
2565 * probe requests after the first ones failed since some
2566 * buggy APs only support broadcast probe requests.
2567 */
2568 if (ifmgd->probe_send_count >= unicast_limit)
2569 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002570
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002571 /*
2572 * When the hardware reports an accurate Tx ACK status, it's
2573 * better to send a nullfunc frame instead of a probe request,
2574 * as it will kick us off the AP quickly if we aren't associated
2575 * anymore. The timeout will be reset if the frame is ACKed by
2576 * the AP.
2577 */
Soumik Das992e68b2012-05-20 15:31:13 +05302578 ifmgd->probe_send_count++;
2579
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002580 if (dst) {
2581 mutex_lock(&sdata->local->sta_mtx);
2582 sta = sta_info_get(sdata, dst);
2583 if (!WARN_ON(!sta))
2584 ieee80211_check_fast_rx(sta);
2585 mutex_unlock(&sdata->local->sta_mtx);
2586 }
2587
Johannes Berg30686bf2015-06-02 21:39:54 +02002588 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002589 ifmgd->nullfunc_failed = false;
Shay Barf39b07f2019-07-03 16:18:48 +03002590 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2591 ifmgd->probe_send_count--;
2592 else
2593 ieee80211_send_nullfunc(sdata->local, sdata, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002594 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002595 int ssid_len;
2596
Johannes Berg9caf0362012-11-29 01:25:20 +01002597 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002598 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002599 if (WARN_ON_ONCE(ssid == NULL))
2600 ssid_len = 0;
2601 else
2602 ssid_len = ssid[1];
2603
Johannes Berg45ad6832018-05-28 15:47:39 +02002604 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2605 ssid + 2, ssid_len,
2606 ifmgd->associated->channel);
Johannes Berg9caf0362012-11-29 01:25:20 +01002607 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002608 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002609
Ben Greear180205b2011-02-04 15:30:24 -08002610 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002611 run_again(sdata, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002612}
2613
Johannes Bergb291ba12009-07-10 15:29:03 +02002614static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2615 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002616{
Kalle Valo04de8382009-03-22 21:57:35 +02002617 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002618 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002619
Johannes Berg9607e6b662009-12-23 13:15:31 +01002620 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002621 return;
2622
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002623 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002624
2625 if (!ifmgd->associated)
2626 goto out;
2627
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002628 mutex_lock(&sdata->local->mtx);
2629
2630 if (sdata->local->tmp_channel || sdata->local->scanning) {
2631 mutex_unlock(&sdata->local->mtx);
2632 goto out;
2633 }
2634
Ben Greeara13fbe52013-03-25 11:19:35 -07002635 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002636 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002637 "detected beacon loss from AP (missed %d beacons) - probing\n",
2638 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002639
Johannes Berg98f03342014-11-26 12:42:02 +01002640 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
Ben Greeara13fbe52013-03-25 11:19:35 -07002641 }
Kalle Valo04de8382009-03-22 21:57:35 +02002642
Johannes Bergb291ba12009-07-10 15:29:03 +02002643 /*
2644 * The driver/our work has already reported this event or the
2645 * connection monitoring has kicked in and we have already sent
2646 * a probe request. Or maybe the AP died and the driver keeps
2647 * reporting until we disassociate...
2648 *
2649 * In either case we have to ignore the current call to this
2650 * function (except for setting the correct probe reason bit)
2651 * because otherwise we would reset the timer every time and
2652 * never check whether we received a probe response!
2653 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002654 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02002655 already = true;
2656
Eliad Peller12b5f342013-11-18 19:06:46 +02002657 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2658
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002659 mutex_unlock(&sdata->local->mtx);
2660
Johannes Bergb291ba12009-07-10 15:29:03 +02002661 if (already)
2662 goto out;
2663
Johannes Berg4e751842009-06-10 15:16:52 +02002664 mutex_lock(&sdata->local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002665 ieee80211_recalc_ps(sdata->local);
Johannes Berg4e751842009-06-10 15:16:52 +02002666 mutex_unlock(&sdata->local->iflist_mtx);
2667
Maxim Levitskya43abf22009-07-31 18:54:12 +03002668 ifmgd->probe_send_count = 0;
2669 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002670 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002671 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002672}
2673
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002674struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2675 struct ieee80211_vif *vif)
2676{
2677 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2678 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002679 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002680 struct sk_buff *skb;
2681 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002682 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002683
2684 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2685 return NULL;
2686
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002687 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002688
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002689 if (ifmgd->associated)
2690 cbss = ifmgd->associated;
2691 else if (ifmgd->auth_data)
2692 cbss = ifmgd->auth_data->bss;
2693 else if (ifmgd->assoc_data)
2694 cbss = ifmgd->assoc_data->bss;
2695 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002696 return NULL;
2697
Johannes Berg9caf0362012-11-29 01:25:20 +01002698 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002699 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Will Deacon41525612019-10-04 10:51:31 +01002700 if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2701 "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002702 ssid_len = 0;
2703 else
2704 ssid_len = ssid[1];
2705
Johannes Berga344d672014-06-12 22:24:31 +02002706 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002707 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002708 ssid + 2, ssid_len,
Johannes Berg00387f32018-05-28 15:47:38 +02002709 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
Johannes Berg9caf0362012-11-29 01:25:20 +01002710 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002711
2712 return skb;
2713}
2714EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2715
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002716static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2717 const u8 *buf, size_t len, bool tx,
2718 u16 reason)
2719{
2720 struct ieee80211_event event = {
2721 .type = MLME_EVENT,
2722 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2723 .u.mlme.reason = reason,
2724 };
2725
2726 if (tx)
2727 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2728 else
2729 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2730
2731 drv_event_callback(sdata->local, sdata, &event);
2732}
2733
Johannes Bergeef9e542013-01-29 13:09:34 +01002734static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002735{
Michal Kazior59af6922014-04-09 15:10:59 +02002736 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002737 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002738 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002739 bool tx;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002740
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002741 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002742 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002743 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002744 return;
2745 }
2746
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002747 tx = !sdata->csa_block_tx;
2748
David Spinadel20eb7ea2016-05-03 16:05:02 +03002749 /* AP is probably out of range (or not reachable for another reason) so
2750 * remove the bss struct for that AP.
2751 */
2752 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2753
Johannes Berg37ad3882012-02-24 13:50:54 +01002754 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2755 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002756 tx, frame_buf);
Michal Kazior59af6922014-04-09 15:10:59 +02002757 mutex_lock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03002758 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002759 ifmgd->csa_waiting_bcn = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002760 if (sdata->csa_block_tx) {
2761 ieee80211_wake_vif_queues(local, sdata,
2762 IEEE80211_QUEUE_STOP_REASON_CSA);
2763 sdata->csa_block_tx = false;
2764 }
Michal Kazior59af6922014-04-09 15:10:59 +02002765 mutex_unlock(&local->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002766
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002767 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002768 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2769
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002770 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002771}
2772
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002773static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002774{
2775 struct ieee80211_sub_if_data *sdata =
2776 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002777 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002778 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Paul Stewarta85e1d52011-12-09 11:01:49 -08002779
Johannes Berg976bd9e2015-10-16 17:18:11 +02002780 if (ifmgd->associated)
2781 ifmgd->beacon_loss_count++;
Johannes Bergb291ba12009-07-10 15:29:03 +02002782
Johannes Berg682bd382013-01-29 13:13:50 +01002783 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002784 sdata_info(sdata, "Connection to AP %pM lost\n",
2785 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002786 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002787 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002788 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002789 }
2790}
2791
2792static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2793{
2794 struct ieee80211_sub_if_data *sdata =
2795 container_of(work, struct ieee80211_sub_if_data,
2796 u.mgd.csa_connection_drop_work);
2797
Johannes Bergeef9e542013-01-29 13:09:34 +01002798 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002799}
2800
Kalle Valo04de8382009-03-22 21:57:35 +02002801void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2802{
2803 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002804 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002805
Johannes Bergb5878a22010-04-07 16:48:40 +02002806 trace_api_beacon_loss(sdata);
2807
Johannes Berg682bd382013-01-29 13:13:50 +01002808 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002809 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002810}
2811EXPORT_SYMBOL(ieee80211_beacon_loss);
2812
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002813void ieee80211_connection_loss(struct ieee80211_vif *vif)
2814{
2815 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2816 struct ieee80211_hw *hw = &sdata->local->hw;
2817
Johannes Bergb5878a22010-04-07 16:48:40 +02002818 trace_api_connection_loss(sdata);
2819
Johannes Berg682bd382013-01-29 13:13:50 +01002820 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002821 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2822}
2823EXPORT_SYMBOL(ieee80211_connection_loss);
2824
2825
Johannes Berg66e67e42012-01-20 13:55:27 +01002826static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2827 bool assoc)
2828{
2829 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2830
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002831 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002832
Johannes Berg66e67e42012-01-20 13:55:27 +01002833 if (!assoc) {
Emmanuel Grumbachc1e140b2015-01-19 16:53:06 +02002834 /*
2835 * we are not authenticated yet, the only timer that could be
2836 * running is the timeout for the authentication response which
2837 * which is not relevant anymore.
2838 */
2839 del_timer_sync(&sdata->u.mgd.timer);
Johannes Berg66e67e42012-01-20 13:55:27 +01002840 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2841
Joe Perchesc84a67a2015-03-02 19:54:57 -08002842 eth_zero_addr(sdata->u.mgd.bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002843 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002844 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002845 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002846 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002847 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002848 }
2849
Johannes Berg5b112d32013-02-01 01:49:58 +01002850 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002851 kfree(auth_data);
2852 sdata->u.mgd.auth_data = NULL;
2853}
2854
Johannes Bergc9c99f82015-06-01 14:10:09 +02002855static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
Johannes Berge6f462d2016-12-08 17:22:09 +01002856 bool assoc, bool abandon)
Johannes Bergc9c99f82015-06-01 14:10:09 +02002857{
2858 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2859
2860 sdata_assert_lock(sdata);
2861
2862 if (!assoc) {
2863 /*
2864 * we are not associated yet, the only timer that could be
2865 * running is the timeout for the association response which
2866 * which is not relevant anymore.
2867 */
2868 del_timer_sync(&sdata->u.mgd.timer);
2869 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2870
2871 eth_zero_addr(sdata->u.mgd.bssid);
2872 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2873 sdata->u.mgd.flags = 0;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002874 sdata->vif.mu_mimo_owner = false;
2875
Johannes Bergc9c99f82015-06-01 14:10:09 +02002876 mutex_lock(&sdata->local->mtx);
2877 ieee80211_vif_release_channel(sdata);
2878 mutex_unlock(&sdata->local->mtx);
Johannes Berge6f462d2016-12-08 17:22:09 +01002879
2880 if (abandon)
2881 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
Johannes Bergc9c99f82015-06-01 14:10:09 +02002882 }
2883
2884 kfree(assoc_data);
2885 sdata->u.mgd.assoc_data = NULL;
2886}
2887
Johannes Berg66e67e42012-01-20 13:55:27 +01002888static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2889 struct ieee80211_mgmt *mgmt, size_t len)
2890{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002891 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002892 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2893 u8 *pos;
2894 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002895 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002896
2897 pos = mgmt->u.auth.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02002898 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2899 mgmt->bssid, auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002900 if (!elems.challenge)
2901 return;
2902 auth_data->expected_transaction = 4;
Ilan Peerd4e36e52018-04-20 13:49:25 +03002903 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg30686bf2015-06-02 21:39:54 +02002904 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01002905 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2906 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002907 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002908 elems.challenge - 2, elems.challenge_len + 2,
2909 auth_data->bss->bssid, auth_data->bss->bssid,
2910 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002911 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002912}
2913
Jouni Malinenfc107a92018-10-11 00:21:19 +03002914static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2915 const u8 *bssid)
2916{
Jouni Malinenefb543e2018-10-11 00:21:21 +03002917 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002918 struct sta_info *sta;
Wei Yongjun33483a62018-10-16 02:35:30 +00002919 bool result = true;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002920
Jouni Malinenefb543e2018-10-11 00:21:21 +03002921 sdata_info(sdata, "authenticated\n");
2922 ifmgd->auth_data->done = true;
2923 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2924 ifmgd->auth_data->timeout_started = true;
2925 run_again(sdata, ifmgd->auth_data->timeout);
2926
Jouni Malinenfc107a92018-10-11 00:21:19 +03002927 /* move station state to auth */
2928 mutex_lock(&sdata->local->sta_mtx);
2929 sta = sta_info_get(sdata, bssid);
2930 if (!sta) {
2931 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002932 result = false;
2933 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002934 }
2935 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2936 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002937 result = false;
2938 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002939 }
Jouni Malinenfc107a92018-10-11 00:21:19 +03002940
Wei Yongjun33483a62018-10-16 02:35:30 +00002941out:
2942 mutex_unlock(&sdata->local->sta_mtx);
2943 return result;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002944}
2945
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002946static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2947 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002948{
2949 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2950 u8 bssid[ETH_ALEN];
2951 u16 auth_alg, auth_transaction, status_code;
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002952 struct ieee80211_event event = {
2953 .type = MLME_EVENT,
2954 .u.mlme.data = AUTH_EVENT,
2955 };
Johannes Berg66e67e42012-01-20 13:55:27 +01002956
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002957 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002958
2959 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002960 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002961
2962 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002963 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002964
2965 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2966
Joe Perchesb203ca32012-05-08 18:56:52 +00002967 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002968 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002969
2970 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2971 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2972 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2973
2974 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinenefb543e2018-10-11 00:21:21 +03002975 (auth_alg != WLAN_AUTH_SAE &&
2976 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2977 (auth_alg == WLAN_AUTH_SAE &&
2978 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2979 auth_transaction > 2))) {
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002980 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2981 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2982 auth_transaction,
2983 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002984 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002985 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002986
2987 if (status_code != WLAN_STATUS_SUCCESS) {
Andrei Otcheretianskia4055e72020-03-26 15:09:34 +02002988 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2989
2990 if (auth_alg == WLAN_AUTH_SAE &&
2991 status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED)
2992 return;
2993
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002994 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2995 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002996 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002997 event.u.mlme.status = MLME_DENIED;
2998 event.u.mlme.reason = status_code;
2999 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003000 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003001 }
3002
3003 switch (ifmgd->auth_data->algorithm) {
3004 case WLAN_AUTH_OPEN:
3005 case WLAN_AUTH_LEAP:
3006 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003007 case WLAN_AUTH_SAE:
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03003008 case WLAN_AUTH_FILS_SK:
3009 case WLAN_AUTH_FILS_SK_PFS:
3010 case WLAN_AUTH_FILS_PK:
Johannes Berg66e67e42012-01-20 13:55:27 +01003011 break;
3012 case WLAN_AUTH_SHARED_KEY:
3013 if (ifmgd->auth_data->expected_transaction != 4) {
3014 ieee80211_auth_challenge(sdata, mgmt, len);
3015 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003016 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003017 }
3018 break;
3019 default:
3020 WARN_ONCE(1, "invalid auth alg %d",
3021 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003022 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003023 }
3024
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003025 event.u.mlme.status = MLME_SUCCESS;
3026 drv_event_callback(sdata->local, sdata, &event);
Jouni Malinenefb543e2018-10-11 00:21:21 +03003027 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3028 (auth_transaction == 2 &&
3029 ifmgd->auth_data->expected_transaction == 2)) {
3030 if (!ieee80211_mark_sta_auth(sdata, bssid))
Andrei Otcheretianski0daa63e2020-02-21 10:47:20 +01003031 return; /* ignore frame -- wait for timeout */
Jouni Malinenefb543e2018-10-11 00:21:21 +03003032 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3033 auth_transaction == 2) {
3034 sdata_info(sdata, "SAE peer confirmed\n");
3035 ifmgd->auth_data->peer_confirmed = true;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003036 }
3037
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003038 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003039}
3040
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003041#define case_WLAN(type) \
3042 case WLAN_REASON_##type: return #type
3043
Yu Wang79c92ca2019-05-10 17:04:52 +08003044const char *ieee80211_get_reason_code_string(u16 reason_code)
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003045{
3046 switch (reason_code) {
3047 case_WLAN(UNSPECIFIED);
3048 case_WLAN(PREV_AUTH_NOT_VALID);
3049 case_WLAN(DEAUTH_LEAVING);
3050 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3051 case_WLAN(DISASSOC_AP_BUSY);
3052 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3053 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3054 case_WLAN(DISASSOC_STA_HAS_LEFT);
3055 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3056 case_WLAN(DISASSOC_BAD_POWER);
3057 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3058 case_WLAN(INVALID_IE);
3059 case_WLAN(MIC_FAILURE);
3060 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3061 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3062 case_WLAN(IE_DIFFERENT);
3063 case_WLAN(INVALID_GROUP_CIPHER);
3064 case_WLAN(INVALID_PAIRWISE_CIPHER);
3065 case_WLAN(INVALID_AKMP);
3066 case_WLAN(UNSUPP_RSN_VERSION);
3067 case_WLAN(INVALID_RSN_IE_CAP);
3068 case_WLAN(IEEE8021X_FAILED);
3069 case_WLAN(CIPHER_SUITE_REJECTED);
3070 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3071 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3072 case_WLAN(DISASSOC_LOW_ACK);
3073 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3074 case_WLAN(QSTA_LEAVE_QBSS);
3075 case_WLAN(QSTA_NOT_USE);
3076 case_WLAN(QSTA_REQUIRE_SETUP);
3077 case_WLAN(QSTA_TIMEOUT);
3078 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3079 case_WLAN(MESH_PEER_CANCELED);
3080 case_WLAN(MESH_MAX_PEERS);
3081 case_WLAN(MESH_CONFIG);
3082 case_WLAN(MESH_CLOSE);
3083 case_WLAN(MESH_MAX_RETRIES);
3084 case_WLAN(MESH_CONFIRM_TIMEOUT);
3085 case_WLAN(MESH_INVALID_GTK);
3086 case_WLAN(MESH_INCONSISTENT_PARAM);
3087 case_WLAN(MESH_INVALID_SECURITY);
3088 case_WLAN(MESH_PATH_ERROR);
3089 case_WLAN(MESH_PATH_NOFORWARD);
3090 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3091 case_WLAN(MAC_EXISTS_IN_MBSS);
3092 case_WLAN(MESH_CHAN_REGULATORY);
3093 case_WLAN(MESH_CHAN);
3094 default: return "<unknown>";
3095 }
3096}
Johannes Berg66e67e42012-01-20 13:55:27 +01003097
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003098static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3099 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003100{
Johannes Berg46900292009-02-15 12:44:28 +01003101 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003102 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003103
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003104 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003105
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003106 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003107 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003108
Yu Wang79c92ca2019-05-10 17:04:52 +08003109 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3110 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3111 return;
3112 }
3113
Johannes Bergc9c99f82015-06-01 14:10:09 +02003114 if (ifmgd->associated &&
3115 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3116 const u8 *bssid = ifmgd->associated->bssid;
3117
3118 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3119 bssid, reason_code,
3120 ieee80211_get_reason_code_string(reason_code));
3121
3122 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3123
3124 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3125 reason_code);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003126 return;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003127 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003128
Johannes Bergc9c99f82015-06-01 14:10:09 +02003129 if (ifmgd->assoc_data &&
3130 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3131 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07003132
Johannes Bergc9c99f82015-06-01 14:10:09 +02003133 sdata_info(sdata,
3134 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3135 bssid, reason_code,
3136 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003137
Johannes Berge6f462d2016-12-08 17:22:09 +01003138 ieee80211_destroy_assoc_data(sdata, false, true);
Jiri Bencf0706e82007-05-05 11:45:53 -07003139
Johannes Bergc9c99f82015-06-01 14:10:09 +02003140 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3141 return;
3142 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003143}
3144
3145
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003146static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3147 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003148{
Johannes Berg46900292009-02-15 12:44:28 +01003149 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003150 u16 reason_code;
3151
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003152 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003153
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003154 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003155 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003156
Johannes Berg66e67e42012-01-20 13:55:27 +01003157 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00003158 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003159 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003160
3161 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3162
Yu Wang79c92ca2019-05-10 17:04:52 +08003163 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3164 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3165 return;
3166 }
3167
Arkadiusz Miskiewicz68506e92017-02-15 14:21:27 +01003168 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3169 mgmt->sa, reason_code,
3170 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003171
Johannes Berg37ad3882012-02-24 13:50:54 +01003172 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3173
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02003174 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003175}
3176
Christian Lamparterc74d0842011-10-15 00:14:49 +02003177static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3178 u8 *supp_rates, unsigned int supp_rates_len,
3179 u32 *rates, u32 *basic_rates,
3180 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003181 int *min_rate, int *min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03003182 int shift)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003183{
3184 int i, j;
3185
3186 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003187 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003188 bool is_basic = !!(supp_rates[i] & 0x80);
3189
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003190 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003191 *have_higher_than_11mbit = true;
3192
3193 /*
Ilan Peer4826e722020-03-26 15:09:36 +02003194 * Skip HT, VHT and HE BSS membership selectors since they're
3195 * not rates.
Christian Lamparterc74d0842011-10-15 00:14:49 +02003196 *
Johannes Berga6289d32017-03-06 22:59:04 +01003197 * Note: Even though the membership selector and the basic
Christian Lamparterc74d0842011-10-15 00:14:49 +02003198 * rate flag share the same bit, they are not exactly
3199 * the same.
3200 */
Johannes Berga6289d32017-03-06 22:59:04 +01003201 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
Ilan Peer4826e722020-03-26 15:09:36 +02003202 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3203 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY))
Christian Lamparterc74d0842011-10-15 00:14:49 +02003204 continue;
3205
3206 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003207 struct ieee80211_rate *br;
3208 int brate;
3209
3210 br = &sband->bitrates[j];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003211
3212 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3213 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02003214 *rates |= BIT(j);
3215 if (is_basic)
3216 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003217 if ((rate * 5) < *min_rate) {
3218 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003219 *min_rate_index = j;
3220 }
3221 break;
3222 }
3223 }
3224 }
3225}
Jiri Bencf0706e82007-05-05 11:45:53 -07003226
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003227static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3228 const struct ieee802_11_elems *elems)
3229{
3230 if (elems->ext_capab_len < 10)
3231 return false;
3232
3233 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3234 return false;
3235
3236 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3237 IEEE80211_HE_MAC_CAP0_TWT_RES;
3238}
3239
John Crispinc9d32452019-05-28 13:49:47 +02003240static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3241 struct sta_info *sta,
3242 struct ieee802_11_elems *elems)
3243{
3244 bool twt = ieee80211_twt_req_supported(sta, elems);
3245
3246 if (sdata->vif.bss_conf.twt_requester != twt) {
3247 sdata->vif.bss_conf.twt_requester = twt;
3248 return BSS_CHANGED_TWT;
3249 }
3250 return 0;
3251}
3252
Johannes Berg66e67e42012-01-20 13:55:27 +01003253static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3254 struct cfg80211_bss *cbss,
Johannes Bergf61d7882019-10-28 12:52:42 +01003255 struct ieee80211_mgmt *mgmt, size_t len,
3256 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07003257{
Johannes Berg46900292009-02-15 12:44:28 +01003258 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01003259 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01003260 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003261 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003262 u16 capab_info, aid;
Johannes Bergbda39332008-10-11 01:51:51 +02003263 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02003264 const struct cfg80211_bss_ies *bss_ies = NULL;
3265 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Berg57fa5e82020-05-28 21:34:36 +02003266 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Johannes Bergae5eb022008-10-14 16:58:37 +02003267 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003268 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02003269 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003270
Johannes Bergaf6b6372009-12-23 13:15:35 +01003271 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07003272
Jiri Bencf0706e82007-05-05 11:45:53 -07003273 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003274 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07003275
Johannes Bergc7c477b2017-12-01 13:50:51 +02003276 /*
3277 * The 5 MSB of the AID field are reserved
3278 * (802.11-2016 9.4.1.8 AID field)
3279 */
3280 aid &= 0x7ff;
Johannes Berg1dd84aa2007-10-10 12:03:41 +02003281
Johannes Berg05cb9102011-10-28 11:59:47 +02003282 ifmgd->broken_ap = false;
3283
3284 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003285 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3286 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02003287 aid = 0;
3288 ifmgd->broken_ap = true;
3289 }
3290
Johannes Bergf61d7882019-10-28 12:52:42 +01003291 if (!elems->supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003292 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01003293 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07003294 }
3295
Johannes Berg1db364c2020-04-17 12:38:04 +02003296 sdata->vif.bss_conf.aid = aid;
Arik Nemtsov9041c1f2014-11-09 18:50:15 +02003297 ifmgd->tdls_chan_switch_prohibited =
Johannes Bergf61d7882019-10-28 12:52:42 +01003298 elems->ext_capab && elems->ext_capab_len >= 5 &&
3299 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
Jiri Bencf0706e82007-05-05 11:45:53 -07003300
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003301 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02003302 * Some APs are erroneously not including some information in their
3303 * (re)association response frames. Try to recover by using the data
3304 * from the beacon or probe response. This seems to afflict mobile
3305 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3306 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3307 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003308 if (!is_6ghz &&
3309 ((assoc_data->wmm && !elems->wmm_param) ||
3310 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3311 (!elems->ht_cap_elem || !elems->ht_operation)) ||
3312 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3313 (!elems->vht_cap_elem || !elems->vht_operation)))) {
Johannes Berg35d865a2013-05-28 10:54:03 +02003314 const struct cfg80211_bss_ies *ies;
3315 struct ieee802_11_elems bss_elems;
3316
3317 rcu_read_lock();
3318 ies = rcu_dereference(cbss->ies);
3319 if (ies)
3320 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3321 GFP_ATOMIC);
3322 rcu_read_unlock();
3323 if (!bss_ies)
3324 return false;
3325
3326 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003327 false, &bss_elems,
3328 mgmt->bssid,
3329 assoc_data->bss->bssid);
Johannes Berg35d865a2013-05-28 10:54:03 +02003330 if (assoc_data->wmm &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003331 !elems->wmm_param && bss_elems.wmm_param) {
3332 elems->wmm_param = bss_elems.wmm_param;
Johannes Berg35d865a2013-05-28 10:54:03 +02003333 sdata_info(sdata,
3334 "AP bug: WMM param missing from AssocResp\n");
3335 }
3336
3337 /*
3338 * Also check if we requested HT/VHT, otherwise the AP doesn't
3339 * have to include the IEs in the (re)association response.
3340 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003341 if (!elems->ht_cap_elem && bss_elems.ht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003342 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003343 elems->ht_cap_elem = bss_elems.ht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003344 sdata_info(sdata,
3345 "AP bug: HT capability missing from AssocResp\n");
3346 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003347 if (!elems->ht_operation && bss_elems.ht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003348 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003349 elems->ht_operation = bss_elems.ht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003350 sdata_info(sdata,
3351 "AP bug: HT operation missing from AssocResp\n");
3352 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003353 if (!elems->vht_cap_elem && bss_elems.vht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003354 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003355 elems->vht_cap_elem = bss_elems.vht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003356 sdata_info(sdata,
3357 "AP bug: VHT capa missing from AssocResp\n");
3358 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003359 if (!elems->vht_operation && bss_elems.vht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003360 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003361 elems->vht_operation = bss_elems.vht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003362 sdata_info(sdata,
3363 "AP bug: VHT operation missing from AssocResp\n");
3364 }
3365 }
3366
3367 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003368 * We previously checked these in the beacon/probe response, so
3369 * they should be present here. This is just a safety net.
3370 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003371 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003372 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003373 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003374 "HT AP is missing WMM params or HT capability/operation\n");
3375 ret = false;
3376 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003377 }
3378
Johannes Berg57fa5e82020-05-28 21:34:36 +02003379 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003380 (!elems->vht_cap_elem || !elems->vht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003381 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003382 "VHT AP is missing VHT capability/operation\n");
3383 ret = false;
3384 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003385 }
3386
Johannes Berg57fa5e82020-05-28 21:34:36 +02003387 if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3388 !elems->he_6ghz_capa) {
3389 sdata_info(sdata,
3390 "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3391 ret = false;
3392 goto out;
3393 }
3394
Guy Eilam2a33bee2011-08-17 15:18:15 +03003395 mutex_lock(&sdata->local->sta_mtx);
3396 /*
3397 * station info was already allocated and inserted before
3398 * the association and should be available to us
3399 */
Johannes Berg7852e362012-01-20 13:55:24 +01003400 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003401 if (WARN_ON(!sta)) {
3402 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003403 ret = false;
3404 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07003405 }
3406
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05303407 sband = ieee80211_get_sband(sdata);
3408 if (!sband) {
3409 mutex_unlock(&sdata->local->sta_mtx);
3410 ret = false;
3411 goto out;
3412 }
Johannes Berg8318d782008-01-24 19:38:38 +01003413
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003414 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003415 (!elems->he_cap || !elems->he_operation)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003416 mutex_unlock(&sdata->local->sta_mtx);
3417 sdata_info(sdata,
3418 "HE AP is missing HE capability/operation\n");
3419 ret = false;
3420 goto out;
3421 }
3422
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003423 /* Set up internal HT/VHT capabilities */
Johannes Bergf61d7882019-10-28 12:52:42 +01003424 if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08003425 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003426 elems->ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02003427
Johannes Bergf61d7882019-10-28 12:52:42 +01003428 if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Mahesh Palivela818255e2012-10-10 11:33:04 +00003429 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003430 elems->vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00003431
Johannes Bergf61d7882019-10-28 12:52:42 +01003432 if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3433 elems->he_cap) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003434 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003435 elems->he_cap,
3436 elems->he_cap_len,
Johannes Berg1bb9a8a2020-05-28 21:34:38 +02003437 elems->he_6ghz_capa,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003438 sta);
3439
3440 bss_conf->he_support = sta->sta.he_cap.has_he;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003441 if (elems->rsnx && elems->rsnx_len &&
3442 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3443 wiphy_ext_feature_isset(local->hw.wiphy,
3444 NL80211_EXT_FEATURE_PROTECTED_TWT))
3445 bss_conf->twt_protected = true;
3446 else
3447 bss_conf->twt_protected = false;
3448
Johannes Bergf61d7882019-10-28 12:52:42 +01003449 changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003450 } else {
3451 bss_conf->he_support = false;
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003452 bss_conf->twt_requester = false;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003453 bss_conf->twt_protected = false;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003454 }
3455
3456 if (bss_conf->he_support) {
John Crispindd56e902019-12-17 15:19:19 +01003457 bss_conf->he_bss_color.color =
Johannes Bergf61d7882019-10-28 12:52:42 +01003458 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003459 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
John Crispindd56e902019-12-17 15:19:19 +01003460 bss_conf->he_bss_color.partial =
3461 le32_get_bits(elems->he_operation->he_oper_params,
3462 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
3463 bss_conf->he_bss_color.disabled =
3464 le32_get_bits(elems->he_operation->he_oper_params,
3465 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
P Praneesh322cd272020-07-09 08:16:21 +05303466
3467 if (!bss_conf->he_bss_color.disabled)
3468 changed |= BSS_CHANGED_HE_BSS_COLOR;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003469
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003470 bss_conf->htc_trig_based_pkt_ext =
Johannes Bergf61d7882019-10-28 12:52:42 +01003471 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003472 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003473 bss_conf->frame_time_rts_th =
Johannes Bergf61d7882019-10-28 12:52:42 +01003474 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003475 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003476
3477 bss_conf->multi_sta_back_32bit =
3478 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3479 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3480
3481 bss_conf->ack_enabled =
3482 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3483 IEEE80211_HE_MAC_CAP2_ACK_EN;
3484
Johannes Bergf61d7882019-10-28 12:52:42 +01003485 bss_conf->uora_exists = !!elems->uora_element;
3486 if (elems->uora_element)
3487 bss_conf->uora_ocw_range = elems->uora_element[0];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003488
Johannes Bergf61d7882019-10-28 12:52:42 +01003489 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3490 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003491 /* TODO: OPEN: what happens if BSS color disable is set? */
3492 }
3493
Sara Sharon78ac51f2019-01-16 18:22:56 +02003494 if (cbss->transmitted_bss) {
3495 bss_conf->nontransmitted = true;
3496 ether_addr_copy(bss_conf->transmitter_bssid,
3497 cbss->transmitted_bss->bssid);
3498 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3499 bss_conf->bssid_index = cbss->bssid_index;
3500 }
3501
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003502 /*
3503 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3504 * in their association response, so ignore that data for our own
3505 * configuration. If it changed since the last beacon, we'll get the
3506 * next beacon and update then.
3507 */
Johannes Berg11289582013-02-07 17:36:12 +01003508
Johannes Bergbee7f5862013-02-07 22:24:55 +01003509 /*
3510 * If an operating mode notification IE is present, override the
3511 * NSS calculation (that would be done in rate_control_rate_init())
3512 * and use the # of streams from that element.
3513 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003514 if (elems->opmode_notif &&
3515 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
Johannes Bergbee7f5862013-02-07 22:24:55 +01003516 u8 nss;
3517
Johannes Bergf61d7882019-10-28 12:52:42 +01003518 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
Johannes Bergbee7f5862013-02-07 22:24:55 +01003519 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3520 nss += 1;
3521 sta->sta.rx_nss = nss;
3522 }
3523
Johannes Berg4b7679a2008-09-18 18:14:18 +02003524 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003525
Tamizh chelvam93f04902015-10-07 10:40:04 +05303526 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02003527 set_sta_flag(sta, WLAN_STA_MFP);
Tamizh chelvam93f04902015-10-07 10:40:04 +05303528 sta->sta.mfp = true;
3529 } else {
3530 sta->sta.mfp = false;
3531 }
Jouni Malinen5394af42009-01-08 13:31:59 +02003532
Johannes Bergf61d7882019-10-28 12:52:42 +01003533 sta->sta.wme = elems->wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
Johannes Bergddf4ac52008-10-22 11:41:38 +02003534
Johannes Berg3e4d40f2013-02-07 17:19:08 +01003535 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01003536 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3537 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3538 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003539 sdata_info(sdata,
3540 "failed to move station %pM to desired state\n",
3541 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01003542 WARN_ON(__sta_info_destroy(sta));
3543 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003544 ret = false;
3545 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01003546 }
3547
Johannes Berg7852e362012-01-20 13:55:24 +01003548 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02003549
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003550 /*
3551 * Always handle WMM once after association regardless
3552 * of the first value the AP uses. Setting -1 here has
3553 * that effect because the AP values is an unsigned
3554 * 4-bit value.
3555 */
3556 ifmgd->wmm_last_param_set = -1;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02003557 ifmgd->mu_edca_last_param_set = -1;
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003558
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003559 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Bergcec66282015-10-22 17:46:04 +02003560 ieee80211_set_wmm_default(sdata, false, false);
Johannes Bergf61d7882019-10-28 12:52:42 +01003561 } else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3562 elems->wmm_param_len,
3563 elems->mu_edca_param_set)) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003564 /* still enable QoS since we might have HT/VHT */
3565 ieee80211_set_wmm_default(sdata, false, true);
3566 /* set the disable-WMM flag in this case to disable
3567 * tracking WMM parameter changes in the beacon if
3568 * the parameters weren't actually valid. Doing so
3569 * avoids changing parameters very strangely when
3570 * the AP is going back and forth between valid and
3571 * invalid parameters.
3572 */
3573 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3574 }
Johannes Berg3abead52012-03-02 15:56:59 +01003575 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07003576
Johannes Bergf61d7882019-10-28 12:52:42 +01003577 if (elems->max_idle_period_ie) {
Avraham Sterne38a0172017-04-26 10:58:47 +03003578 bss_conf->max_idle_period =
Johannes Bergf61d7882019-10-28 12:52:42 +01003579 le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
Avraham Sterne38a0172017-04-26 10:58:47 +03003580 bss_conf->protected_keep_alive =
Johannes Bergf61d7882019-10-28 12:52:42 +01003581 !!(elems->max_idle_period_ie->idle_options &
Avraham Sterne38a0172017-04-26 10:58:47 +03003582 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3583 changed |= BSS_CHANGED_KEEP_ALIVE;
3584 } else {
3585 bss_conf->max_idle_period = 0;
3586 bss_conf->protected_keep_alive = false;
3587 }
3588
Johannes Berg1db364c2020-04-17 12:38:04 +02003589 /* set assoc capability (AID was already set earlier),
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003590 * ieee80211_set_associated() will tell the driver */
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003591 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003592 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003593
Kalle Valo15b7b062009-03-22 21:57:14 +02003594 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01003595 * If we're using 4-addr mode, let the AP know that we're
3596 * doing so, so that it can create the STA VLAN on its side
3597 */
3598 if (ifmgd->use_4addr)
3599 ieee80211_send_4addr_nullfunc(local, sdata);
3600
3601 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02003602 * Start timer to probe the connection to the AP now.
3603 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02003604 */
Johannes Bergb291ba12009-07-10 15:29:03 +02003605 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003606 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02003607
Johannes Berg35d865a2013-05-28 10:54:03 +02003608 ret = true;
3609 out:
3610 kfree(bss_ies);
3611 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003612}
3613
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003614static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3615 struct ieee80211_mgmt *mgmt,
3616 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01003617{
3618 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3619 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3620 u16 capab_info, status_code, aid;
3621 struct ieee802_11_elems elems;
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003622 int ac, uapsd_queues = -1;
Johannes Berg66e67e42012-01-20 13:55:27 +01003623 u8 *pos;
3624 bool reassoc;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003625 struct cfg80211_bss *bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003626 struct ieee80211_event event = {
3627 .type = MLME_EVENT,
3628 .u.mlme.data = ASSOC_EVENT,
3629 };
Jiri Bencf0706e82007-05-05 11:45:53 -07003630
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003631 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003632
3633 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003634 return;
Joe Perchesb203ca32012-05-08 18:56:52 +00003635 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003636 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003637
3638 /*
3639 * AssocResp and ReassocResp have identical structure, so process both
3640 * of them in this function.
3641 */
3642
3643 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003644 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003645
Simon Dinkin7a7d3e42017-08-31 13:09:36 +03003646 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
Johannes Berg66e67e42012-01-20 13:55:27 +01003647 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3648 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3649 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3650
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003651 sdata_info(sdata,
3652 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3653 reassoc ? "Rea" : "A", mgmt->sa,
3654 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01003655
Jouni Malinen39404fe2016-10-27 00:42:05 +03003656 if (assoc_data->fils_kek_len &&
3657 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3658 return;
3659
Johannes Berg66e67e42012-01-20 13:55:27 +01003660 pos = mgmt->u.assoc_resp.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02003661 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3662 mgmt->bssid, assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003663
3664 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01003665 elems.timeout_int &&
3666 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003667 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01003668 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01003669 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003670 sdata_info(sdata,
3671 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3672 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01003673 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01003674 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003675 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003676 run_again(sdata, assoc_data->timeout);
3677 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003678 }
3679
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003680 bss = assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01003681
3682 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003683 sdata_info(sdata, "%pM denied association (code=%d)\n",
3684 mgmt->sa, status_code);
Johannes Berge6f462d2016-12-08 17:22:09 +01003685 ieee80211_destroy_assoc_data(sdata, false, false);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003686 event.u.mlme.status = MLME_DENIED;
3687 event.u.mlme.reason = status_code;
3688 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01003689 } else {
Johannes Bergf61d7882019-10-28 12:52:42 +01003690 if (!ieee80211_assoc_success(sdata, bss, mgmt, len, &elems)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003691 /* oops -- internal error -- send timeout for now */
Johannes Berge6f462d2016-12-08 17:22:09 +01003692 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02003693 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003694 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003695 }
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003696 event.u.mlme.status = MLME_SUCCESS;
3697 drv_event_callback(sdata->local, sdata, &event);
John W. Linville635d9992012-07-09 16:34:34 -04003698 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01003699
3700 /*
3701 * destroy assoc_data afterwards, as otherwise an idle
3702 * recalc after assoc_data is NULL but before associated
3703 * is set can cause the interface to go idle
3704 */
Johannes Berge6f462d2016-12-08 17:22:09 +01003705 ieee80211_destroy_assoc_data(sdata, true, false);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003706
3707 /* get uapsd queues configuration */
3708 uapsd_queues = 0;
3709 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3710 if (sdata->tx_conf[ac].uapsd)
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03003711 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
Johannes Berg66e67e42012-01-20 13:55:27 +01003712 }
3713
Jouni Malinen4d9ec732019-02-15 02:14:33 +02003714 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues,
3715 ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003716}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003717
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003718static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003719 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003720 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003721{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003722 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02003723 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01003724 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01003725
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003726 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01003727
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003728 channel = ieee80211_get_channel_khz(local->hw.wiphy,
3729 ieee80211_rx_status_to_khz(rx_status));
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02003730 if (!channel)
Johannes Berg5bda6172008-09-08 11:05:10 +02003731 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003732
Sara Sharon4abb52a2019-01-16 12:14:41 +02003733 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03003734 if (bss) {
Alexander Bondar817cee72013-05-19 14:23:57 +03003735 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
Johannes Bergd2722f82014-03-04 11:43:28 +01003736 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03003737 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003738}
3739
3740
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003741static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01003742 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003743{
Johannes Bergaf6b6372009-12-23 13:15:35 +01003744 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02003745 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003746 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003747 struct ieee80211_channel *channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003748 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03003749
Kalle Valo15b7b062009-03-22 21:57:14 +02003750 ifmgd = &sdata->u.mgd;
3751
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003752 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003753
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003754 /*
3755 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3756 * "If a 6 GHz AP receives a Probe Request frame and responds with
3757 * a Probe Response frame [..], the Address 1 field of the Probe
3758 * Response frame shall be set to the broadcast address [..]"
3759 * So, on 6GHz band we should also accept broadcast responses.
3760 */
3761 channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3762 rx_status->freq);
3763 if (!channel)
3764 return;
3765
3766 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3767 (channel->band != NL80211_BAND_6GHZ ||
3768 !is_broadcast_ether_addr(mgmt->da)))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03003769 return; /* ignore ProbeResp to foreign address */
3770
Ester Kummerae6a44e2008-06-27 18:54:48 +03003771 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3772 if (baselen > len)
3773 return;
3774
Sara Sharon4abb52a2019-01-16 12:14:41 +02003775 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003776
Johannes Berg77fdaa12009-07-07 03:45:17 +02003777 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00003778 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003779 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003780}
3781
Johannes Bergd91f36d2009-04-16 13:17:26 +02003782/*
3783 * This is the canonical list of information elements we care about,
3784 * the filter code also gives us all changes to the Microsoft OUI
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07003785 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3786 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3787 * changes to requested client power.
Johannes Bergd91f36d2009-04-16 13:17:26 +02003788 *
3789 * We implement beacon filtering in software since that means we can
3790 * avoid processing the frame here and in cfg80211, and userspace
3791 * will not be able to tell whether the hardware supports it or not.
3792 *
3793 * XXX: This list needs to be dynamic -- userspace needs to be able to
3794 * add items it requires. It also needs to be able to tell us to
3795 * look out for other vendor IEs.
3796 */
3797static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02003798 (1ULL << WLAN_EID_COUNTRY) |
3799 (1ULL << WLAN_EID_ERP_INFO) |
3800 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3801 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3802 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg70a3fd62015-03-12 08:53:29 +02003803 (1ULL << WLAN_EID_HT_OPERATION) |
3804 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
Johannes Bergd91f36d2009-04-16 13:17:26 +02003805
Tosoni1ad22fb2018-03-14 16:58:34 +00003806static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3807 struct ieee80211_if_managed *ifmgd,
3808 struct ieee80211_bss_conf *bss_conf,
3809 struct ieee80211_local *local,
3810 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003811{
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003812 /* Track average RSSI from the Beacon frames of the current AP */
Tosoni1ad22fb2018-03-14 16:58:34 +00003813
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003814 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3815 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Johannes Berg338c17a2015-08-28 10:52:54 +02003816 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003817 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003818 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003819 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003820 } else {
Jouni Malinen391a2002010-08-27 22:22:00 +03003821 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003822 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003823
Johannes Berg338c17a2015-08-28 10:52:54 +02003824 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3825
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003826 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3827 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003828 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003829 int last_sig = ifmgd->last_ave_beacon_signal;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003830 struct ieee80211_event event = {
3831 .type = RSSI_EVENT,
3832 };
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003833
3834 /*
3835 * if signal crosses either of the boundaries, invoke callback
3836 * with appropriate parameters
3837 */
3838 if (sig > ifmgd->rssi_max_thold &&
3839 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3840 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003841 event.u.rssi.data = RSSI_EVENT_HIGH;
3842 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003843 } else if (sig < ifmgd->rssi_min_thold &&
3844 (last_sig >= ifmgd->rssi_max_thold ||
3845 last_sig == 0)) {
3846 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003847 event.u.rssi.data = RSSI_EVENT_LOW;
3848 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003849 }
3850 }
3851
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003852 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003853 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003854 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003855 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003856 int last_event = ifmgd->last_cqm_event_signal;
3857 int thold = bss_conf->cqm_rssi_thold;
3858 int hyst = bss_conf->cqm_rssi_hyst;
Johannes Berg338c17a2015-08-28 10:52:54 +02003859
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003860 if (sig < thold &&
3861 (last_event == 0 || sig < last_event - hyst)) {
3862 ifmgd->last_cqm_event_signal = sig;
3863 ieee80211_cqm_rssi_notify(
3864 &sdata->vif,
3865 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003866 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003867 } else if (sig > thold &&
3868 (last_event == 0 || sig > last_event + hyst)) {
3869 ifmgd->last_cqm_event_signal = sig;
3870 ieee80211_cqm_rssi_notify(
3871 &sdata->vif,
3872 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003873 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003874 }
3875 }
3876
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003877 if (bss_conf->cqm_rssi_low &&
3878 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3879 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3880 int last_event = ifmgd->last_cqm_event_signal;
3881 int low = bss_conf->cqm_rssi_low;
3882 int high = bss_conf->cqm_rssi_high;
3883
3884 if (sig < low &&
3885 (last_event == 0 || last_event >= low)) {
3886 ifmgd->last_cqm_event_signal = sig;
3887 ieee80211_cqm_rssi_notify(
3888 &sdata->vif,
3889 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3890 sig, GFP_KERNEL);
3891 } else if (sig > high &&
3892 (last_event == 0 || last_event <= high)) {
3893 ifmgd->last_cqm_event_signal = sig;
3894 ieee80211_cqm_rssi_notify(
3895 &sdata->vif,
3896 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3897 sig, GFP_KERNEL);
3898 }
3899 }
Tosoni1ad22fb2018-03-14 16:58:34 +00003900}
3901
Sara Sharon78ac51f2019-01-16 18:22:56 +02003902static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3903 struct cfg80211_bss *bss)
3904{
3905 if (ether_addr_equal(tx_bssid, bss->bssid))
3906 return true;
3907 if (!bss->transmitted_bss)
3908 return false;
3909 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3910}
3911
Tosoni1ad22fb2018-03-14 16:58:34 +00003912static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3913 struct ieee80211_mgmt *mgmt, size_t len,
3914 struct ieee80211_rx_status *rx_status)
3915{
3916 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3917 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3918 size_t baselen;
3919 struct ieee802_11_elems elems;
3920 struct ieee80211_local *local = sdata->local;
3921 struct ieee80211_chanctx_conf *chanctx_conf;
3922 struct ieee80211_channel *chan;
3923 struct sta_info *sta;
3924 u32 changed = 0;
3925 bool erp_valid;
3926 u8 erp_value = 0;
3927 u32 ncrc;
3928 u8 *bssid;
3929 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3930
3931 sdata_assert_lock(sdata);
3932
3933 /* Process beacon from the current BSS */
3934 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3935 if (baselen > len)
3936 return;
3937
3938 rcu_read_lock();
3939 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3940 if (!chanctx_conf) {
3941 rcu_read_unlock();
3942 return;
3943 }
3944
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003945 if (ieee80211_rx_status_to_khz(rx_status) !=
3946 ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00003947 rcu_read_unlock();
3948 return;
3949 }
3950 chan = chanctx_conf->def.chan;
3951 rcu_read_unlock();
3952
3953 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Sara Sharon78ac51f2019-01-16 18:22:56 +02003954 ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->assoc_data->bss)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00003955 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003956 len - baselen, false, &elems,
3957 mgmt->bssid,
3958 ifmgd->assoc_data->bss->bssid);
Tosoni1ad22fb2018-03-14 16:58:34 +00003959
Sara Sharon4abb52a2019-01-16 12:14:41 +02003960 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Sara Sharon78ac51f2019-01-16 18:22:56 +02003961
3962 if (elems.dtim_period)
3963 ifmgd->dtim_period = elems.dtim_period;
Tosoni1ad22fb2018-03-14 16:58:34 +00003964 ifmgd->have_beacon = true;
3965 ifmgd->assoc_data->need_beacon = false;
3966 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3967 sdata->vif.bss_conf.sync_tsf =
3968 le64_to_cpu(mgmt->u.beacon.timestamp);
3969 sdata->vif.bss_conf.sync_device_ts =
3970 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02003971 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Tosoni1ad22fb2018-03-14 16:58:34 +00003972 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02003973
3974 if (elems.mbssid_config_ie)
3975 bss_conf->profile_periodicity =
3976 elems.mbssid_config_ie->profile_periodicity;
3977
3978 if (elems.ext_capab_len >= 11 &&
3979 (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
3980 bss_conf->ema_ap = true;
3981
Tosoni1ad22fb2018-03-14 16:58:34 +00003982 /* continue assoc process */
3983 ifmgd->assoc_data->timeout = jiffies;
3984 ifmgd->assoc_data->timeout_started = true;
3985 run_again(sdata, ifmgd->assoc_data->timeout);
3986 return;
3987 }
3988
3989 if (!ifmgd->associated ||
Sara Sharon78ac51f2019-01-16 18:22:56 +02003990 !ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->associated))
Tosoni1ad22fb2018-03-14 16:58:34 +00003991 return;
3992 bssid = ifmgd->associated->bssid;
3993
3994 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3995 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
3996 local, rx_status);
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003997
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003998 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003999 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01004000 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004001 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03004002 }
4003
Johannes Bergb291ba12009-07-10 15:29:03 +02004004 /*
4005 * Push the beacon loss detection into the future since
4006 * we are processing a beacon from the AP just now.
4007 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04004008 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02004009
Johannes Bergd91f36d2009-04-16 13:17:26 +02004010 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4011 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01004012 len - baselen, false, &elems,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004013 care_about_ies, ncrc,
4014 mgmt->bssid, bssid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02004015
Johannes Berg90d13e82015-09-24 16:13:07 +02004016 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Johannes Berg1db364c2020-04-17 12:38:04 +02004017 ieee80211_check_tim(elems.tim, elems.tim_len, bss_conf->aid)) {
Johannes Berg90d13e82015-09-24 16:13:07 +02004018 if (local->hw.conf.dynamic_ps_timeout > 0) {
4019 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4020 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4021 ieee80211_hw_config(local,
4022 IEEE80211_CONF_CHANGE_PS);
Kalle Valo572e0012009-02-10 17:09:31 +02004023 }
Johannes Berg076cdcb2015-09-24 16:14:55 +02004024 ieee80211_send_nullfunc(local, sdata, false);
Johannes Berg90d13e82015-09-24 16:13:07 +02004025 } else if (!local->pspolling && sdata->u.mgd.powersave) {
4026 local->pspolling = true;
4027
4028 /*
4029 * Here is assumed that the driver will be
4030 * able to send ps-poll frame and receive a
4031 * response even though power save mode is
4032 * enabled, but some drivers might require
4033 * to disable power save here. This needs
4034 * to be investigated.
4035 */
4036 ieee80211_send_pspoll(local, sdata);
Vivek Natarajana97b77b2008-12-23 18:39:02 -08004037 }
4038 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02004039
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01004040 if (sdata->vif.p2p ||
4041 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004042 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01004043 int ret;
4044
4045 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
4046 len - baselen,
4047 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01004048 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004049 if (ret >= 2) {
4050 if (sdata->u.mgd.p2p_noa_index != noa.index) {
4051 /* valid noa_attr and index changed */
4052 sdata->u.mgd.p2p_noa_index = noa.index;
4053 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4054 changed |= BSS_CHANGED_P2P_PS;
4055 /*
4056 * make sure we update all information, the CRC
4057 * mechanism doesn't look at P2P attributes.
4058 */
4059 ifmgd->beacon_crc_valid = false;
4060 }
4061 } else if (sdata->u.mgd.p2p_noa_index != -1) {
4062 /* noa_attr not found and we had valid noa_attr before */
4063 sdata->u.mgd.p2p_noa_index = -1;
4064 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01004065 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01004066 ifmgd->beacon_crc_valid = false;
4067 }
4068 }
4069
Luciano Coelho0c21e632014-10-08 09:48:39 +03004070 if (ifmgd->csa_waiting_bcn)
4071 ieee80211_chswitch_post_beacon(sdata);
4072
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004073 /*
4074 * Update beacon timing and dtim count on every beacon appearance. This
4075 * will allow the driver to use the most updated values. Do it before
4076 * comparing this one with last received beacon.
4077 * IMPORTANT: These parameters would possibly be out of sync by the time
4078 * the driver will use them. The synchronized view is currently
4079 * guaranteed only in certain callbacks.
4080 */
Johannes Berg30686bf2015-06-02 21:39:54 +02004081 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004082 sdata->vif.bss_conf.sync_tsf =
4083 le64_to_cpu(mgmt->u.beacon.timestamp);
4084 sdata->vif.bss_conf.sync_device_ts =
4085 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004086 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004087 }
4088
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004089 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004090 return;
Jouni Malinen30196672009-05-19 17:01:43 +03004091 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004092 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03004093
Sara Sharon4abb52a2019-01-16 12:14:41 +02004094 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Johannes Berg7d257452012-07-06 17:37:43 +02004095
Johannes Bergd70b7612013-08-23 15:48:48 +02004096 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004097 rx_status->device_timestamp,
Johannes Bergd70b7612013-08-23 15:48:48 +02004098 &elems, true);
4099
Johannes Berg095d81c2013-10-15 12:25:07 +02004100 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4101 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004102 elems.wmm_param_len,
4103 elems.mu_edca_param_set))
Johannes Berg7d257452012-07-06 17:37:43 +02004104 changed |= BSS_CHANGED_QOS;
4105
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004106 /*
4107 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01004108 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004109 */
Alexander Bondar989c6502013-05-16 17:34:17 +03004110 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004111 /* a few bogus AP send dtim_period = 0 or no TIM IE */
Sara Sharon78ac51f2019-01-16 18:22:56 +02004112 bss_conf->dtim_period = elems.dtim_period ?: 1;
Johannes Bergef429da2013-02-05 17:48:40 +01004113
Alexander Bondar989c6502013-05-16 17:34:17 +03004114 changed |= BSS_CHANGED_BEACON_INFO;
4115 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03004116
4117 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02004118 ieee80211_recalc_ps(local);
Alexander Bondar482a9c72013-06-03 17:29:33 +03004119 mutex_unlock(&local->iflist_mtx);
4120
Alexander Bondarce857882013-05-06 17:17:04 +03004121 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004122 }
4123
Johannes Berg1946bed2013-03-27 14:31:53 +01004124 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02004125 erp_valid = true;
4126 erp_value = elems.erp_info[0];
4127 } else {
4128 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04004129 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02004130 changed |= ieee80211_handle_bss_capability(sdata,
4131 le16_to_cpu(mgmt->u.beacon.capab_info),
4132 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07004133
Johannes Berg11289582013-02-07 17:36:12 +01004134 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01004135 sta = sta_info_get(sdata, bssid);
4136
John Crispinc9d32452019-05-28 13:49:47 +02004137 changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4138
Johannes Berg2a333a02020-05-28 21:34:35 +02004139 if (ieee80211_config_bw(sdata, sta, elems.ht_cap_elem,
4140 elems.vht_cap_elem, elems.ht_operation,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004141 elems.vht_operation, elems.he_operation,
4142 bssid, &changed)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004143 mutex_unlock(&local->sta_mtx);
Johannes Berg89f774e2016-01-25 15:46:36 +02004144 sdata_info(sdata,
4145 "failed to follow AP %pM bandwidth change, disconnect\n",
4146 bssid);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004147 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4148 WLAN_REASON_DEAUTH_LEAVING,
4149 true, deauth_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004150 ieee80211_report_disconnect(sdata, deauth_buf,
4151 sizeof(deauth_buf), true,
4152 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004153 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004154 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01004155
4156 if (sta && elems.opmode_notif)
4157 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
Eyal Shapiracf1e05c2015-12-08 16:04:36 +02004158 rx_status->band);
Johannes Berg11289582013-02-07 17:36:12 +01004159 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02004160
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07004161 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4162 elems.country_elem,
4163 elems.country_elem_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07004164 elems.pwr_constr_elem,
4165 elems.cisco_dtpc_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08004166
Johannes Berg471b3ef2007-12-28 14:32:58 +01004167 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07004168}
4169
Johannes Berg1fa57d02010-06-10 10:21:32 +02004170void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4171 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07004172{
4173 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07004174 struct ieee80211_mgmt *mgmt;
4175 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004176 struct ieee802_11_elems elems;
4177 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07004178
Jiri Bencf0706e82007-05-05 11:45:53 -07004179 rx_status = (struct ieee80211_rx_status *) skb->cb;
4180 mgmt = (struct ieee80211_mgmt *) skb->data;
4181 fc = le16_to_cpu(mgmt->frame_control);
4182
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004183 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004184
Johannes Berg66e67e42012-01-20 13:55:27 +01004185 switch (fc & IEEE80211_FCTL_STYPE) {
4186 case IEEE80211_STYPE_BEACON:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004187 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01004188 break;
4189 case IEEE80211_STYPE_PROBE_RESP:
4190 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4191 break;
4192 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004193 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004194 break;
4195 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004196 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004197 break;
4198 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004199 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004200 break;
4201 case IEEE80211_STYPE_ASSOC_RESP:
4202 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004203 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004204 break;
4205 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01004206 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004207 ies_len = skb->len -
4208 offsetof(struct ieee80211_mgmt,
4209 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01004210
4211 if (ies_len < 0)
4212 break;
4213
Sara Sharon4abb52a2019-01-16 12:14:41 +02004214 /* CSA IE cannot be overridden, no need for BSSID */
Johannes Berg37799e52013-03-26 14:02:26 +01004215 ieee802_11_parse_elems(
4216 mgmt->u.action.u.chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004217 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg37799e52013-03-26 14:02:26 +01004218
4219 if (elems.parse_error)
4220 break;
4221
Johannes Berg66e67e42012-01-20 13:55:27 +01004222 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004223 rx_status->mactime,
4224 rx_status->device_timestamp,
4225 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004226 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4227 ies_len = skb->len -
4228 offsetof(struct ieee80211_mgmt,
4229 u.action.u.ext_chan_switch.variable);
4230
4231 if (ies_len < 0)
4232 break;
4233
Sara Sharon4abb52a2019-01-16 12:14:41 +02004234 /*
4235 * extended CSA IE can't be overridden, no need for
4236 * BSSID
4237 */
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004238 ieee802_11_parse_elems(
4239 mgmt->u.action.u.ext_chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004240 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004241
4242 if (elems.parse_error)
4243 break;
4244
4245 /* for the handling code pretend this was also an IE */
4246 elems.ext_chansw_ie =
4247 &mgmt->u.action.u.ext_chan_switch.data;
4248
4249 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004250 rx_status->mactime,
4251 rx_status->device_timestamp,
4252 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004253 }
Johannes Berg37799e52013-03-26 14:02:26 +01004254 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004255 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004256 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07004257}
4258
Kees Cook34f11cd2017-10-16 16:35:49 -07004259static void ieee80211_sta_timer(struct timer_list *t)
Jiri Bencf0706e82007-05-05 11:45:53 -07004260{
4261 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004262 from_timer(sdata, t, u.mgd.timer);
Jiri Bencf0706e82007-05-05 11:45:53 -07004263
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01004264 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07004265}
4266
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004267static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01004268 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004269{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004270 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004271
Johannes Berg37ad3882012-02-24 13:50:54 +01004272 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01004273 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01004274
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004275 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4276 reason);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004277}
4278
Johannes Berg46cad4b2015-08-15 22:39:54 +03004279static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
Johannes Berg66e67e42012-01-20 13:55:27 +01004280{
4281 struct ieee80211_local *local = sdata->local;
4282 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4283 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004284 u32 tx_flags = 0;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004285 u16 trans = 1;
4286 u16 status = 0;
Ilan Peerd4e36e52018-04-20 13:49:25 +03004287 u16 prepare_tx_duration = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004288
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004289 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004290
4291 if (WARN_ON_ONCE(!auth_data))
4292 return -EINVAL;
4293
Johannes Berg66e67e42012-01-20 13:55:27 +01004294 auth_data->tries++;
4295
4296 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004297 sdata_info(sdata, "authentication with %pM timed out\n",
4298 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004299
4300 /*
4301 * Most likely AP is not in the range so remove the
4302 * bss struct for that AP.
4303 */
4304 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4305
4306 return -ETIMEDOUT;
4307 }
4308
Ilan Peerd4e36e52018-04-20 13:49:25 +03004309 if (auth_data->algorithm == WLAN_AUTH_SAE)
4310 prepare_tx_duration =
4311 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4312
4313 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
Johannes Berga1845fc2012-06-27 13:18:36 +02004314
Johannes Berg46cad4b2015-08-15 22:39:54 +03004315 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4316 auth_data->bss->bssid, auth_data->tries,
4317 IEEE80211_AUTH_MAX_TRIES);
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004318
Johannes Berg46cad4b2015-08-15 22:39:54 +03004319 auth_data->expected_transaction = 2;
Johannes Berg66e67e42012-01-20 13:55:27 +01004320
Johannes Berg46cad4b2015-08-15 22:39:54 +03004321 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4322 trans = auth_data->sae_trans;
4323 status = auth_data->sae_status;
4324 auth_data->expected_transaction = trans;
Johannes Berg66e67e42012-01-20 13:55:27 +01004325 }
4326
Johannes Berg46cad4b2015-08-15 22:39:54 +03004327 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4328 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4329 IEEE80211_TX_INTFL_MLME_CONN_TX;
4330
4331 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4332 auth_data->data, auth_data->data_len,
4333 auth_data->bss->bssid,
4334 auth_data->bss->bssid, NULL, 0, 0,
4335 tx_flags);
4336
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02004337 if (tx_flags == 0) {
Ilan Peer407879b2018-04-20 13:49:20 +03004338 if (auth_data->algorithm == WLAN_AUTH_SAE)
4339 auth_data->timeout = jiffies +
4340 IEEE80211_AUTH_TIMEOUT_SAE;
4341 else
4342 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004343 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004344 auth_data->timeout =
4345 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004346 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004347
Ilan Peer407879b2018-04-20 13:49:20 +03004348 auth_data->timeout_started = true;
4349 run_again(sdata, auth_data->timeout);
4350
Johannes Berg66e67e42012-01-20 13:55:27 +01004351 return 0;
4352}
4353
4354static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4355{
4356 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4357 struct ieee80211_local *local = sdata->local;
4358
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004359 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004360
Johannes Berg66e67e42012-01-20 13:55:27 +01004361 assoc_data->tries++;
4362 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004363 sdata_info(sdata, "association with %pM timed out\n",
4364 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004365
4366 /*
4367 * Most likely AP is not in the range so remove the
4368 * bss struct for that AP.
4369 */
4370 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4371
4372 return -ETIMEDOUT;
4373 }
4374
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004375 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4376 assoc_data->bss->bssid, assoc_data->tries,
4377 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01004378 ieee80211_send_assoc(sdata);
4379
Johannes Berg30686bf2015-06-02 21:39:54 +02004380 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004381 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004382 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004383 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01004384 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004385 assoc_data->timeout =
4386 round_jiffies_up(jiffies +
4387 IEEE80211_ASSOC_TIMEOUT_LONG);
4388 assoc_data->timeout_started = true;
4389 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004390 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004391
4392 return 0;
4393}
4394
Johannes Berg1672c0e32013-01-29 15:02:27 +01004395void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4396 __le16 fc, bool acked)
4397{
4398 struct ieee80211_local *local = sdata->local;
4399
4400 sdata->u.mgd.status_fc = fc;
4401 sdata->u.mgd.status_acked = acked;
4402 sdata->u.mgd.status_received = true;
4403
4404 ieee80211_queue_work(&local->hw, &sdata->work);
4405}
4406
Johannes Berg1fa57d02010-06-10 10:21:32 +02004407void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004408{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004409 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02004410 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004411
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004412 sdata_lock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004413
Johannes Berg1672c0e32013-01-29 15:02:27 +01004414 if (ifmgd->status_received) {
4415 __le16 fc = ifmgd->status_fc;
4416 bool status_acked = ifmgd->status_acked;
4417
4418 ifmgd->status_received = false;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004419 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004420 if (status_acked) {
Ilan Peer407879b2018-04-20 13:49:20 +03004421 if (ifmgd->auth_data->algorithm ==
4422 WLAN_AUTH_SAE)
4423 ifmgd->auth_data->timeout =
4424 jiffies +
4425 IEEE80211_AUTH_TIMEOUT_SAE;
4426 else
4427 ifmgd->auth_data->timeout =
4428 jiffies +
4429 IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004430 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004431 } else {
4432 ifmgd->auth_data->timeout = jiffies - 1;
4433 }
Johannes Berg89afe612013-02-13 15:39:57 +01004434 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004435 } else if (ifmgd->assoc_data &&
4436 (ieee80211_is_assoc_req(fc) ||
4437 ieee80211_is_reassoc_req(fc))) {
4438 if (status_acked) {
4439 ifmgd->assoc_data->timeout =
4440 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004441 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004442 } else {
4443 ifmgd->assoc_data->timeout = jiffies - 1;
4444 }
Johannes Berg89afe612013-02-13 15:39:57 +01004445 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004446 }
4447 }
4448
Johannes Berg89afe612013-02-13 15:39:57 +01004449 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004450 time_after(jiffies, ifmgd->auth_data->timeout)) {
4451 if (ifmgd->auth_data->done) {
4452 /*
4453 * ok ... we waited for assoc but userspace didn't,
4454 * so let's just kill the auth data
4455 */
4456 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg46cad4b2015-08-15 22:39:54 +03004457 } else if (ieee80211_auth(sdata)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004458 u8 bssid[ETH_ALEN];
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004459 struct ieee80211_event event = {
4460 .type = MLME_EVENT,
4461 .u.mlme.data = AUTH_EVENT,
4462 .u.mlme.status = MLME_TIMEOUT,
4463 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004464
4465 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4466
4467 ieee80211_destroy_auth_data(sdata, false);
4468
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004469 cfg80211_auth_timeout(sdata->dev, bssid);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004470 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004471 }
Johannes Berg89afe612013-02-13 15:39:57 +01004472 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004473 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004474
Johannes Berg89afe612013-02-13 15:39:57 +01004475 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004476 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03004477 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01004478 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02004479 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004480 struct ieee80211_event event = {
4481 .type = MLME_EVENT,
4482 .u.mlme.data = ASSOC_EVENT,
4483 .u.mlme.status = MLME_TIMEOUT,
4484 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004485
Johannes Berge6f462d2016-12-08 17:22:09 +01004486 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004487 cfg80211_assoc_timeout(sdata->dev, bss);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004488 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004489 }
Johannes Berg89afe612013-02-13 15:39:57 +01004490 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004491 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004492
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004493 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02004494 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03004495 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004496 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03004497
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004498 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004499
Johannes Berg30686bf2015-06-02 21:39:54 +02004500 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Ben Greear180205b2011-02-04 15:30:24 -08004501 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004502 else
Ben Greear180205b2011-02-04 15:30:24 -08004503 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004504
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004505 /* ACK received for nullfunc probing frame */
4506 if (!ifmgd->probe_send_count)
4507 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004508 else if (ifmgd->nullfunc_failed) {
4509 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004510 mlme_dbg(sdata,
4511 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4512 bssid, ifmgd->probe_send_count,
4513 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004514 ieee80211_mgd_probe_ap_send(sdata);
4515 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004516 mlme_dbg(sdata,
4517 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4518 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02004519 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004520 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4521 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004522 }
4523 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004524 run_again(sdata, ifmgd->probe_timeout);
Johannes Berg30686bf2015-06-02 21:39:54 +02004525 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004526 mlme_dbg(sdata,
4527 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4528 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02004529 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004530 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004531 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004532 mlme_dbg(sdata,
4533 "No probe response from AP %pM after %dms, try %d/%i\n",
4534 bssid, probe_wait_ms,
4535 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03004536 ieee80211_mgd_probe_ap_send(sdata);
4537 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02004538 /*
4539 * We actually lost the connection ... or did we?
4540 * Let's make sure!
4541 */
Johannes Berg89f774e2016-01-25 15:46:36 +02004542 mlme_dbg(sdata,
4543 "No probe response from AP %pM after %dms, disconnecting.\n",
4544 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004545
Johannes Berg95acac62011-07-12 12:30:59 +02004546 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004547 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02004548 }
4549 }
4550
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004551 sdata_unlock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004552}
Johannes Berg0a51b272008-09-08 17:44:25 +02004553
Kees Cook34f11cd2017-10-16 16:35:49 -07004554static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004555{
4556 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004557 from_timer(sdata, t, u.mgd.bcn_mon_timer);
Luciano Coelho0c21e632014-10-08 09:48:39 +03004558 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004559
Luciano Coelho0c21e632014-10-08 09:48:39 +03004560 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004561 return;
4562
Johannes Berg682bd382013-01-29 13:13:50 +01004563 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004564 ieee80211_queue_work(&sdata->local->hw,
4565 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004566}
4567
Kees Cook34f11cd2017-10-16 16:35:49 -07004568static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004569{
4570 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004571 from_timer(sdata, t, u.mgd.conn_mon_timer);
Johannes Bergb291ba12009-07-10 15:29:03 +02004572 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4573 struct ieee80211_local *local = sdata->local;
4574
Luciano Coelho0c21e632014-10-08 09:48:39 +03004575 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004576 return;
4577
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04004578 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004579}
4580
4581static void ieee80211_sta_monitor_work(struct work_struct *work)
4582{
4583 struct ieee80211_sub_if_data *sdata =
4584 container_of(work, struct ieee80211_sub_if_data,
4585 u.mgd.monitor_work);
4586
4587 ieee80211_mgd_probe_ap(sdata, false);
4588}
4589
Johannes Berga1678f82008-09-11 00:01:47 +02004590static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4591{
Kalle Vaload935682009-04-19 08:47:19 +03004592 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02004593 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03004594
Johannes Bergb291ba12009-07-10 15:29:03 +02004595 /* let's probe the connection once */
Johannes Berg30686bf2015-06-02 21:39:54 +02004596 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Eliad Peller494f1fe2012-02-19 15:26:09 +02004597 ieee80211_queue_work(&sdata->local->hw,
4598 &sdata->u.mgd.monitor_work);
Kalle Vaload935682009-04-19 08:47:19 +03004599 }
Johannes Berga1678f82008-09-11 00:01:47 +02004600}
4601
Johannes Bergb8360ab2013-04-29 14:57:44 +02004602#ifdef CONFIG_PM
Johannes Berg1a1cb742014-03-19 09:55:55 +01004603void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4604{
4605 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4606 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4607
4608 sdata_lock(sdata);
4609
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004610 if (ifmgd->auth_data || ifmgd->assoc_data) {
4611 const u8 *bssid = ifmgd->auth_data ?
4612 ifmgd->auth_data->bss->bssid :
4613 ifmgd->assoc_data->bss->bssid;
4614
Johannes Berg1a1cb742014-03-19 09:55:55 +01004615 /*
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004616 * If we are trying to authenticate / associate while suspending,
4617 * cfg80211 won't know and won't actually abort those attempts,
4618 * thus we need to do that ourselves.
Johannes Berg1a1cb742014-03-19 09:55:55 +01004619 */
Johannes Berg4b08d1b2019-08-30 14:24:51 +03004620 ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
Johannes Berg1a1cb742014-03-19 09:55:55 +01004621 IEEE80211_STYPE_DEAUTH,
4622 WLAN_REASON_DEAUTH_LEAVING,
4623 false, frame_buf);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004624 if (ifmgd->assoc_data)
Johannes Berge6f462d2016-12-08 17:22:09 +01004625 ieee80211_destroy_assoc_data(sdata, false, true);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004626 if (ifmgd->auth_data)
4627 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg1a1cb742014-03-19 09:55:55 +01004628 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4629 IEEE80211_DEAUTH_FRAME_LEN);
4630 }
4631
Johannes Bergbe72afe2015-03-01 09:10:03 +02004632 /* This is a bit of a hack - we should find a better and more generic
4633 * solution to this. Normally when suspending, cfg80211 will in fact
4634 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4635 * auth (not so important) or assoc (this is the problem) process.
4636 *
4637 * As a consequence, it can happen that we are in the process of both
4638 * associating and suspending, and receive an association response
4639 * after cfg80211 has checked if it needs to disconnect, but before
4640 * we actually set the flag to drop incoming frames. This will then
4641 * cause the workqueue flush to process the association response in
4642 * the suspend, resulting in a successful association just before it
4643 * tries to remove the interface from the driver, which now though
4644 * has a channel context assigned ... this results in issues.
4645 *
4646 * To work around this (for now) simply deauth here again if we're
4647 * now connected.
4648 */
4649 if (ifmgd->associated && !sdata->local->wowlan) {
4650 u8 bssid[ETH_ALEN];
4651 struct cfg80211_deauth_request req = {
4652 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4653 .bssid = bssid,
4654 };
4655
4656 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4657 ieee80211_mgd_deauth(sdata, &req);
4658 }
4659
Johannes Berg1a1cb742014-03-19 09:55:55 +01004660 sdata_unlock(sdata);
4661}
4662
Johannes Bergb8360ab2013-04-29 14:57:44 +02004663void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4664{
4665 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4666
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004667 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004668 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004669 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004670 return;
4671 }
4672
4673 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4674 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4675 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4676 ieee80211_sta_connection_lost(sdata,
4677 ifmgd->associated->bssid,
4678 WLAN_REASON_UNSPECIFIED,
4679 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004680 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004681 return;
4682 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004683 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004684}
4685#endif
4686
Johannes Berg9c6bd792008-09-11 00:01:52 +02004687/* interface setup */
4688void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4689{
Johannes Berg46900292009-02-15 12:44:28 +01004690 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004691
Johannes Berg46900292009-02-15 12:44:28 +01004692 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004693 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01004694 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004695 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4696 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02004697 INIT_WORK(&ifmgd->csa_connection_drop_work,
4698 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03004699 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03004700 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4701 ieee80211_tdls_peer_del_work);
Kees Cook34f11cd2017-10-16 16:35:49 -07004702 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4703 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4704 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4705 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
Johannes Berg02219b32014-10-07 10:38:50 +03004706 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4707 ieee80211_sta_handle_tspec_ac_params_wk);
Johannes Berg9c6bd792008-09-11 00:01:52 +02004708
Johannes Bergab1faea2009-07-01 21:41:17 +02004709 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05304710 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02004711 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4712 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004713 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02004714
Eliad Peller0d8614b2014-09-10 14:07:36 +03004715 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
Johannes Berg0f782312009-12-01 13:37:02 +01004716 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4717 else
4718 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02004719
4720 /* Setup TDLS data */
4721 spin_lock_init(&ifmgd->teardown_lock);
4722 ifmgd->teardown_skb = NULL;
4723 ifmgd->orig_teardown_skb = NULL;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004724}
4725
4726/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02004727void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4728{
Johannes Berg31ee67a2012-07-06 21:18:24 +02004729 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02004730
4731 /* Restart STA timers */
4732 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07004733 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4734 if (ieee80211_sdata_running(sdata))
4735 ieee80211_restart_sta_timer(sdata);
4736 }
Johannes Berga1678f82008-09-11 00:01:47 +02004737 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02004738}
Johannes Berg10f644a2009-04-16 13:17:25 +02004739
Johannes Bergf2d9d272012-11-22 14:11:39 +01004740static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4741 struct cfg80211_bss *cbss)
4742{
4743 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4744 const u8 *ht_cap_ie, *vht_cap_ie;
4745 const struct ieee80211_ht_cap *ht_cap;
4746 const struct ieee80211_vht_cap *vht_cap;
4747 u8 chains = 1;
4748
4749 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4750 return chains;
4751
Johannes Berg9caf0362012-11-29 01:25:20 +01004752 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004753 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4754 ht_cap = (void *)(ht_cap_ie + 2);
4755 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4756 /*
4757 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4758 * "Tx Unequal Modulation Supported" fields.
4759 */
4760 }
4761
4762 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4763 return chains;
4764
Johannes Berg9caf0362012-11-29 01:25:20 +01004765 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004766 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4767 u8 nss;
4768 u16 tx_mcs_map;
4769
4770 vht_cap = (void *)(vht_cap_ie + 2);
4771 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4772 for (nss = 8; nss > 0; nss--) {
4773 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4774 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4775 break;
4776 }
4777 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4778 chains = max(chains, nss);
4779 }
4780
4781 return chains;
4782}
4783
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004784static bool
4785ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4786 const struct ieee80211_he_operation *he_op)
4787{
4788 const struct ieee80211_sta_he_cap *sta_he_cap =
4789 ieee80211_get_he_sta_cap(sband);
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004790 u16 ap_min_req_set;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004791 int i;
4792
4793 if (!sta_he_cap || !he_op)
4794 return false;
4795
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004796 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4797
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004798 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4799 for (i = 0; i < 3; i++) {
4800 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4801 &sta_he_cap->he_mcs_nss_supp;
4802 u16 sta_mcs_map_rx =
4803 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4804 u16 sta_mcs_map_tx =
4805 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4806 u8 nss;
4807 bool verified = true;
4808
4809 /*
4810 * For each band there is a maximum of 8 spatial streams
4811 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4812 * of 2 bits per NSS (1-8), with the values defined in enum
4813 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4814 * capabilities aren't less than the AP's minimum requirements
4815 * for this HE BSS per SS.
4816 * It is enough to find one such band that meets the reqs.
4817 */
4818 for (nss = 8; nss > 0; nss--) {
4819 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4820 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4821 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4822
4823 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4824 continue;
4825
4826 /*
4827 * Make sure the HE AP doesn't require MCSs that aren't
4828 * supported by the client
4829 */
4830 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4831 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4832 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4833 verified = false;
4834 break;
4835 }
4836 }
4837
4838 if (verified)
4839 return true;
4840 }
4841
4842 /* If here, STA doesn't meet AP's HE min requirements */
4843 return false;
4844}
4845
Johannes Bergb17166a2012-07-27 11:41:27 +02004846static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4847 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01004848{
4849 struct ieee80211_local *local = sdata->local;
4850 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Peller53b954e2014-07-24 11:20:05 +03004851 const struct ieee80211_ht_cap *ht_cap = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004852 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004853 const struct ieee80211_vht_operation *vht_oper = NULL;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004854 const struct ieee80211_he_operation *he_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004855 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01004856 struct cfg80211_chan_def chandef;
Johannes Berg57fa5e82020-05-28 21:34:36 +02004857 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Johannes Berg2a333a02020-05-28 21:34:35 +02004858 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004859 int ret;
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004860 u32 i;
4861 bool have_80mhz;
Johannes Berga1cf7752012-03-08 15:02:07 +01004862
Johannes Berg24398e32012-03-28 10:58:36 +02004863 sband = local->hw.wiphy->bands[cbss->channel->band];
4864
Johannes Bergf2d9d272012-11-22 14:11:39 +01004865 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4866 IEEE80211_STA_DISABLE_80P80MHZ |
4867 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02004868
Johannes Berg75e296e2020-01-31 13:12:39 +02004869 /* disable HT/VHT/HE if we don't support them */
Johannes Berg57fa5e82020-05-28 21:34:36 +02004870 if (!sband->ht_cap.ht_supported && !is_6ghz) {
Johannes Berg75e296e2020-01-31 13:12:39 +02004871 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4872 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4873 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4874 }
4875
Johannes Berg57fa5e82020-05-28 21:34:36 +02004876 if (!sband->vht_cap.vht_supported && !is_6ghz) {
Johannes Berg75e296e2020-01-31 13:12:39 +02004877 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg57fa5e82020-05-28 21:34:36 +02004878 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4879 }
Johannes Berg75e296e2020-01-31 13:12:39 +02004880
4881 if (!ieee80211_get_he_sta_cap(sband))
4882 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4883
Johannes Berg9caf0362012-11-29 01:25:20 +01004884 rcu_read_lock();
4885
Johannes Berg57fa5e82020-05-28 21:34:36 +02004886 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
Eliad Peller53b954e2014-07-24 11:20:05 +03004887 const u8 *ht_oper_ie, *ht_cap_ie;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004888
Johannes Berg9caf0362012-11-29 01:25:20 +01004889 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02004890 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4891 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6effa2013-02-07 23:33:32 +01004892
Eliad Peller53b954e2014-07-24 11:20:05 +03004893 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4894 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4895 ht_cap = (void *)(ht_cap_ie + 2);
4896
4897 if (!ht_cap) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004898 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4899 ht_oper = NULL;
4900 }
Johannes Berg24398e32012-03-28 10:58:36 +02004901 }
4902
Johannes Berg57fa5e82020-05-28 21:34:36 +02004903 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004904 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004905
Johannes Berg9caf0362012-11-29 01:25:20 +01004906 vht_oper_ie = ieee80211_bss_get_ie(cbss,
4907 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004908 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
4909 vht_oper = (void *)(vht_oper_ie + 2);
4910 if (vht_oper && !ht_oper) {
4911 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004912 sdata_info(sdata,
Johannes Berg75e296e2020-01-31 13:12:39 +02004913 "AP advertised VHT without HT, disabling HT/VHT/HE\n");
Johannes Bergcb145022013-02-07 20:41:50 +01004914 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4915 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02004916 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg24398e32012-03-28 10:58:36 +02004917 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01004918
4919 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4920 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
4921 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4922 vht_oper = NULL;
4923 }
Johannes Berg24398e32012-03-28 10:58:36 +02004924 }
4925
Shaul Triebitz002245e2018-12-15 11:03:19 +02004926 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004927 const struct cfg80211_bss_ies *ies;
4928 const u8 *he_oper_ie;
4929
4930 ies = rcu_dereference(cbss->ies);
4931 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
4932 ies->data, ies->len);
4933 if (he_oper_ie &&
4934 he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
4935 he_oper = (void *)(he_oper_ie + 3);
4936 else
4937 he_oper = NULL;
4938
Johannes Bergf0c04072018-06-29 09:51:39 +02004939 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004940 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4941 }
4942
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004943 /* Allow VHT if at least one channel on the sband supports 80 MHz */
4944 have_80mhz = false;
4945 for (i = 0; i < sband->n_channels; i++) {
4946 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4947 IEEE80211_CHAN_NO_80MHZ))
4948 continue;
4949
4950 have_80mhz = true;
4951 break;
4952 }
4953
4954 if (!have_80mhz)
4955 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4956
Johannes Bergf2d9d272012-11-22 14:11:39 +01004957 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
4958 cbss->channel,
Johannes Berg2a333a02020-05-28 21:34:35 +02004959 bss->vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004960 ht_oper, vht_oper, he_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02004961 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02004962
Johannes Bergf2d9d272012-11-22 14:11:39 +01004963 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
4964 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02004965
Johannes Berg9caf0362012-11-29 01:25:20 +01004966 rcu_read_unlock();
4967
Johannes Berg57fa5e82020-05-28 21:34:36 +02004968 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
4969 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
4970 return -EINVAL;
4971 }
4972
Johannes Berg04ecd252012-09-11 14:34:12 +02004973 /* will change later if needed */
4974 sdata->smps_mode = IEEE80211_SMPS_OFF;
4975
Johannes Berg34a37402013-12-18 09:43:33 +01004976 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004977 /*
4978 * If this fails (possibly due to channel context sharing
4979 * on incompatible channels, e.g. 80+80 and 160 sharing the
4980 * same control channel) try to use a smaller bandwidth.
4981 */
4982 ret = ieee80211_vif_use_channel(sdata, &chandef,
4983 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02004984
4985 /* don't downgrade for 5 and 10 MHz channels, though. */
4986 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4987 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01004988 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02004989
Johannes Bergd601cd82013-02-07 20:54:51 +01004990 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02004991 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01004992 ret = ieee80211_vif_use_channel(sdata, &chandef,
4993 IEEE80211_CHANCTX_SHARED);
4994 }
Johannes Berg34a37402013-12-18 09:43:33 +01004995 out:
4996 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004997 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02004998}
4999
Sara Sharon78ac51f2019-01-16 18:22:56 +02005000static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5001 u8 *dtim_count, u8 *dtim_period)
5002{
5003 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5004 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5005 ies->len);
5006 const struct ieee80211_tim_ie *tim = NULL;
5007 const struct ieee80211_bssid_index *idx;
5008 bool valid = tim_ie && tim_ie[1] >= 2;
5009
5010 if (valid)
5011 tim = (void *)(tim_ie + 2);
5012
5013 if (dtim_count)
5014 *dtim_count = valid ? tim->dtim_count : 0;
5015
5016 if (dtim_period)
5017 *dtim_period = valid ? tim->dtim_period : 0;
5018
5019 /* Check if value is overridden by non-transmitted profile */
5020 if (!idx_ie || idx_ie[1] < 3)
5021 return valid;
5022
5023 idx = (void *)(idx_ie + 2);
5024
5025 if (dtim_count)
5026 *dtim_count = idx->dtim_count;
5027
5028 if (dtim_period)
5029 *dtim_period = idx->dtim_period;
5030
5031 return true;
5032}
5033
Johannes Bergb17166a2012-07-27 11:41:27 +02005034static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005035 struct cfg80211_bss *cbss, bool assoc,
5036 bool override)
Johannes Bergb17166a2012-07-27 11:41:27 +02005037{
5038 struct ieee80211_local *local = sdata->local;
5039 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5040 struct ieee80211_bss *bss = (void *)cbss->priv;
5041 struct sta_info *new_sta = NULL;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005042 struct ieee80211_supported_band *sband;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005043 bool have_sta = false;
Johannes Bergb17166a2012-07-27 11:41:27 +02005044 int err;
5045
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005046 sband = local->hw.wiphy->bands[cbss->channel->band];
5047
Johannes Bergb17166a2012-07-27 11:41:27 +02005048 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5049 return -EINVAL;
5050
Luca Coelhof8860ce2017-05-02 17:56:21 +03005051 /* If a reconfig is happening, bail out */
5052 if (local->in_reconfig)
5053 return -EBUSY;
5054
Johannes Bergb17166a2012-07-27 11:41:27 +02005055 if (assoc) {
5056 rcu_read_lock();
5057 have_sta = sta_info_get(sdata, cbss->bssid);
5058 rcu_read_unlock();
5059 }
5060
5061 if (!have_sta) {
5062 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5063 if (!new_sta)
5064 return -ENOMEM;
5065 }
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005066
Johannes Bergc87905b2017-06-10 13:52:43 +03005067 /*
5068 * Set up the information for the new channel before setting the
5069 * new channel. We can't - completely race-free - change the basic
5070 * rates bitmap and the channel (sband) that it refers to, but if
5071 * we set it up before we at least avoid calling into the driver's
5072 * bss_info_changed() method with invalid information (since we do
5073 * call that from changing the channel - only for IDLE and perhaps
5074 * some others, but ...).
5075 *
5076 * So to avoid that, just set up all the new information before the
5077 * channel, but tell the driver to apply it only afterwards, since
5078 * it might need the new channel for that.
5079 */
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005080 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005081 u32 rates = 0, basic_rates = 0;
5082 bool have_higher_than_11mbit;
5083 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005084 const struct cfg80211_bss_ies *ies;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005085 int shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005086
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005087 ieee80211_get_rates(sband, bss->supp_rates,
5088 bss->supp_rates_len,
5089 &rates, &basic_rates,
5090 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02005091 &min_rate, &min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03005092 shift);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005093
5094 /*
5095 * This used to be a workaround for basic rates missing
5096 * in the association response frame. Now that we no
5097 * longer use the basic rates from there, it probably
5098 * doesn't happen any more, but keep the workaround so
5099 * in case some *other* APs are buggy in different ways
5100 * we can connect -- with a warning.
Ilan Peer302ff8b2020-03-26 15:09:39 +02005101 * Allow this workaround only in case the AP provided at least
5102 * one rate.
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005103 */
Ilan Peer302ff8b2020-03-26 15:09:39 +02005104 if (min_rate_index < 0) {
5105 sdata_info(sdata,
5106 "No legacy rates in association response\n");
5107
5108 sta_info_free(local, new_sta);
5109 return -EINVAL;
5110 } else if (!basic_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005111 sdata_info(sdata,
5112 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005113 basic_rates = BIT(min_rate_index);
5114 }
5115
Johannes Bergbd718fc2019-05-29 15:25:35 +03005116 if (rates)
5117 new_sta->sta.supp_rates[cbss->channel->band] = rates;
5118 else
5119 sdata_info(sdata,
5120 "No rates found, keeping mandatory only\n");
5121
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005122 sdata->vif.bss_conf.basic_rates = basic_rates;
5123
5124 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg57fbcce2016-04-12 15:56:15 +02005125 if (cbss->channel->band == NL80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005126 have_higher_than_11mbit)
5127 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5128 else
5129 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5130
5131 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5132
Johannes Berg8c358bc2012-05-22 22:13:05 +02005133 /* set timing information */
5134 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005135 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01005136 ies = rcu_dereference(cbss->beacon_ies);
5137 if (ies) {
Johannes Bergef429da2013-02-05 17:48:40 +01005138 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5139 sdata->vif.bss_conf.sync_device_ts =
5140 bss->device_ts_beacon;
Sara Sharon78ac51f2019-01-16 18:22:56 +02005141
5142 ieee80211_get_dtim(ies,
5143 &sdata->vif.bss_conf.sync_dtim_count,
5144 NULL);
Johannes Berg30686bf2015-06-02 21:39:54 +02005145 } else if (!ieee80211_hw_check(&sdata->local->hw,
5146 TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005147 ies = rcu_dereference(cbss->proberesp_ies);
5148 /* must be non-NULL since beacon IEs were NULL */
5149 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5150 sdata->vif.bss_conf.sync_device_ts =
5151 bss->device_ts_presp;
5152 sdata->vif.bss_conf.sync_dtim_count = 0;
5153 } else {
5154 sdata->vif.bss_conf.sync_tsf = 0;
5155 sdata->vif.bss_conf.sync_device_ts = 0;
5156 sdata->vif.bss_conf.sync_dtim_count = 0;
5157 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01005158 rcu_read_unlock();
Johannes Bergc87905b2017-06-10 13:52:43 +03005159 }
Johannes Berg8c358bc2012-05-22 22:13:05 +02005160
Johannes Bergc87905b2017-06-10 13:52:43 +03005161 if (new_sta || override) {
5162 err = ieee80211_prep_channel(sdata, cbss);
5163 if (err) {
5164 if (new_sta)
5165 sta_info_free(local, new_sta);
5166 return -EINVAL;
5167 }
5168 }
5169
5170 if (new_sta) {
5171 /*
5172 * tell driver about BSSID, basic rates and timing
5173 * this was set up above, before setting the channel
5174 */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005175 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02005176 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5177 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01005178
5179 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005180 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01005181
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005182 err = sta_info_insert(new_sta);
5183 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01005184 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005185 sdata_info(sdata,
5186 "failed to insert STA entry for the AP (error %d)\n",
5187 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01005188 return err;
5189 }
5190 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00005191 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01005192
David Spinadel7d830a12015-03-17 19:58:38 +02005193 /* Cancel scan to ensure that nothing interferes with connection */
5194 if (local->scanning)
5195 ieee80211_scan_cancel(local);
5196
Johannes Berga1cf7752012-03-08 15:02:07 +01005197 return 0;
5198}
5199
Johannes Berg77fdaa12009-07-07 03:45:17 +02005200/* config hooks */
5201int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5202 struct cfg80211_auth_request *req)
5203{
Johannes Berg66e67e42012-01-20 13:55:27 +01005204 struct ieee80211_local *local = sdata->local;
5205 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5206 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005207 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01005208 int err;
Jouni Malinenefb543e2018-10-11 00:21:21 +03005209 bool cont_auth;
Johannes Berg66e67e42012-01-20 13:55:27 +01005210
5211 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02005212
5213 switch (req->auth_type) {
5214 case NL80211_AUTHTYPE_OPEN_SYSTEM:
5215 auth_alg = WLAN_AUTH_OPEN;
5216 break;
5217 case NL80211_AUTHTYPE_SHARED_KEY:
Ard Biesheuvel5fdb3732019-06-12 18:19:54 +02005218 if (fips_enabled)
Johannes Berg9dca9c42010-07-21 10:09:25 +02005219 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005220 auth_alg = WLAN_AUTH_SHARED_KEY;
5221 break;
5222 case NL80211_AUTHTYPE_FT:
5223 auth_alg = WLAN_AUTH_FT;
5224 break;
5225 case NL80211_AUTHTYPE_NETWORK_EAP:
5226 auth_alg = WLAN_AUTH_LEAP;
5227 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005228 case NL80211_AUTHTYPE_SAE:
5229 auth_alg = WLAN_AUTH_SAE;
5230 break;
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03005231 case NL80211_AUTHTYPE_FILS_SK:
5232 auth_alg = WLAN_AUTH_FILS_SK;
5233 break;
5234 case NL80211_AUTHTYPE_FILS_SK_PFS:
5235 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5236 break;
5237 case NL80211_AUTHTYPE_FILS_PK:
5238 auth_alg = WLAN_AUTH_FILS_PK;
5239 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005240 default:
5241 return -EOPNOTSUPP;
5242 }
5243
Jouni Malinenefb543e2018-10-11 00:21:21 +03005244 if (ifmgd->assoc_data)
Jouni Malinen8d7432a2018-10-11 00:21:20 +03005245 return -EBUSY;
5246
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005247 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005248 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01005249 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005250 return -ENOMEM;
5251
Johannes Berg66e67e42012-01-20 13:55:27 +01005252 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005253
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005254 if (req->auth_data_len >= 4) {
Jouni Malinen6ec63612016-10-27 00:41:59 +03005255 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5256 __le16 *pos = (__le16 *) req->auth_data;
5257
5258 auth_data->sae_trans = le16_to_cpu(pos[0]);
5259 auth_data->sae_status = le16_to_cpu(pos[1]);
5260 }
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005261 memcpy(auth_data->data, req->auth_data + 4,
5262 req->auth_data_len - 4);
5263 auth_data->data_len += req->auth_data_len - 4;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005264 }
5265
Jouni Malinenefb543e2018-10-11 00:21:21 +03005266 /* Check if continuing authentication or trying to authenticate with the
5267 * same BSS that we were in the process of authenticating with and avoid
5268 * removal and re-addition of the STA entry in
5269 * ieee80211_prep_connection().
5270 */
5271 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5272
Johannes Berg77fdaa12009-07-07 03:45:17 +02005273 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005274 memcpy(&auth_data->data[auth_data->data_len],
5275 req->ie, req->ie_len);
5276 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005277 }
5278
Johannes Bergfffd0932009-07-08 14:22:54 +02005279 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005280 auth_data->key_len = req->key_len;
5281 auth_data->key_idx = req->key_idx;
5282 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02005283 }
5284
Johannes Berg66e67e42012-01-20 13:55:27 +01005285 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005286
Johannes Berg66e67e42012-01-20 13:55:27 +01005287 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01005288
Jouni Malinenefb543e2018-10-11 00:21:21 +03005289 if (ifmgd->auth_data) {
5290 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5291 auth_data->peer_confirmed =
5292 ifmgd->auth_data->peer_confirmed;
5293 }
5294 ieee80211_destroy_auth_data(sdata, cont_auth);
5295 }
Guy Eilam2a33bee2011-08-17 15:18:15 +03005296
Johannes Berg66e67e42012-01-20 13:55:27 +01005297 /* prep auth_data so we don't go into idle on disassoc */
5298 ifmgd->auth_data = auth_data;
5299
Jouni Malinenefb543e2018-10-11 00:21:21 +03005300 /* If this is continuation of an ongoing SAE authentication exchange
5301 * (i.e., request to send SAE Confirm) and the peer has already
5302 * confirmed, mark authentication completed since we are about to send
5303 * out SAE Confirm.
5304 */
5305 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5306 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5307 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5308
Johannes Berg7b119dc2013-04-10 21:38:36 +02005309 if (ifmgd->associated) {
5310 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5311
Johannes Berg89f774e2016-01-25 15:46:36 +02005312 sdata_info(sdata,
5313 "disconnect from AP %pM for new auth to %pM\n",
5314 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005315 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5316 WLAN_REASON_UNSPECIFIED,
5317 false, frame_buf);
5318
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005319 ieee80211_report_disconnect(sdata, frame_buf,
5320 sizeof(frame_buf), true,
5321 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005322 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005323
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005324 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005325
Jouni Malinenefb543e2018-10-11 00:21:21 +03005326 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Johannes Berga1cf7752012-03-08 15:02:07 +01005327 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01005328 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005329
Johannes Berg46cad4b2015-08-15 22:39:54 +03005330 err = ieee80211_auth(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01005331 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005332 sta_info_destroy_addr(sdata, req->bss->bssid);
5333 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005334 }
5335
Johannes Berg66e67e42012-01-20 13:55:27 +01005336 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01005337 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005338 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02005339
Johannes Berg66e67e42012-01-20 13:55:27 +01005340 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005341 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005342 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005343 ifmgd->auth_data = NULL;
Michal Kaziord01f8582015-06-03 08:36:13 +02005344 mutex_lock(&sdata->local->mtx);
5345 ieee80211_vif_release_channel(sdata);
5346 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01005347 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005348 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005349}
5350
Johannes Berg77fdaa12009-07-07 03:45:17 +02005351int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5352 struct cfg80211_assoc_request *req)
5353{
Johannes Berg57fa5e82020-05-28 21:34:36 +02005354 bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
Johannes Berg66e67e42012-01-20 13:55:27 +01005355 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005356 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01005357 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01005358 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005359 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005360 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005361 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005362 int i, err;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005363 bool override = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005364
Johannes Berg66e67e42012-01-20 13:55:27 +01005365 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5366 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01005367 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005368
Johannes Berg9caf0362012-11-29 01:25:20 +01005369 rcu_read_lock();
5370 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Will Deacon41525612019-10-04 10:51:31 +01005371 if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
Johannes Berg9caf0362012-11-29 01:25:20 +01005372 rcu_read_unlock();
5373 kfree(assoc_data);
5374 return -EINVAL;
5375 }
5376 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5377 assoc_data->ssid_len = ssidie[1];
5378 rcu_read_unlock();
5379
Johannes Berg7b119dc2013-04-10 21:38:36 +02005380 if (ifmgd->associated) {
5381 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5382
Johannes Berg89f774e2016-01-25 15:46:36 +02005383 sdata_info(sdata,
5384 "disconnect from AP %pM for new assoc to %pM\n",
5385 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005386 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5387 WLAN_REASON_UNSPECIFIED,
5388 false, frame_buf);
5389
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005390 ieee80211_report_disconnect(sdata, frame_buf,
5391 sizeof(frame_buf), true,
5392 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005393 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005394
5395 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5396 err = -EBUSY;
5397 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005398 }
5399
Johannes Berg66e67e42012-01-20 13:55:27 +01005400 if (ifmgd->assoc_data) {
5401 err = -EBUSY;
5402 goto err_free;
5403 }
5404
5405 if (ifmgd->auth_data) {
5406 bool match;
5407
5408 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00005409 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005410 ieee80211_destroy_auth_data(sdata, match);
5411 }
5412
5413 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02005414
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03005415 ifmgd->beacon_crc_valid = false;
5416
Johannes Berg095d81c2013-10-15 12:25:07 +02005417 assoc_data->wmm = bss->wmm_used &&
5418 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg095d81c2013-10-15 12:25:07 +02005419
Johannes Bergde5036a2012-03-08 15:02:03 +01005420 /*
5421 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5422 * We still associate in non-HT mode (11a/b/g) if any one of these
5423 * ciphers is configured as pairwise.
5424 * We can set this to true for non-11n hardware, that'll be checked
5425 * separately along with the peer capabilities.
5426 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02005427 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02005428 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5429 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02005430 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01005431 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005432 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005433 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg1c4cb922012-05-30 15:57:00 +02005434 netdev_info(sdata->dev,
Johannes Berg54626322019-08-30 14:24:46 +03005435 "disabling HT/VHT/HE due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02005436 }
5437 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005438
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005439 sband = local->hw.wiphy->bands[req->bss->channel->band];
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005440
Johannes Berg75e296e2020-01-31 13:12:39 +02005441 /* also disable HT/VHT/HE if the AP doesn't use WMM */
5442 if (!bss->wmm_used) {
5443 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005444 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005445 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5446 netdev_info(sdata->dev,
5447 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005448 }
5449
Ben Greearef96a8422011-11-18 11:32:00 -08005450 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5451 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5452 sizeof(ifmgd->ht_capa_mask));
5453
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01005454 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5455 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5456 sizeof(ifmgd->vht_capa_mask));
5457
Johannes Berg77fdaa12009-07-07 03:45:17 +02005458 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005459 memcpy(assoc_data->ie, req->ie, req->ie_len);
5460 assoc_data->ie_len = req->ie_len;
5461 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005462
Jouni Malinen39404fe2016-10-27 00:42:05 +03005463 if (req->fils_kek) {
5464 /* should already be checked in cfg80211 - so warn */
5465 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5466 err = -EINVAL;
5467 goto err_free;
5468 }
5469 memcpy(assoc_data->fils_kek, req->fils_kek,
5470 req->fils_kek_len);
5471 assoc_data->fils_kek_len = req->fils_kek_len;
5472 }
5473
5474 if (req->fils_nonces)
5475 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5476 2 * FILS_NONCE_LEN);
5477
Johannes Berg66e67e42012-01-20 13:55:27 +01005478 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01005479
Johannes Bergaf6b6372009-12-23 13:15:35 +01005480 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5481 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02005482 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005483 else
Johannes Berg04ecd252012-09-11 14:34:12 +02005484 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005485 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02005486 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005487
Johannes Berg66e67e42012-01-20 13:55:27 +01005488 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01005489 assoc_data->supp_rates = bss->supp_rates;
5490 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02005491
Johannes Berg9caf0362012-11-29 01:25:20 +01005492 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02005493 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5494 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5495 assoc_data->ap_ht_param =
5496 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
Johannes Berg57fa5e82020-05-28 21:34:36 +02005497 else if (!is_6ghz)
Johannes Berga8243b72012-11-22 14:32:09 +01005498 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005499 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5500 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5501 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5502 sizeof(struct ieee80211_vht_cap));
Johannes Berg57fa5e82020-05-28 21:34:36 +02005503 else if (!is_6ghz)
5504 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5505 IEEE80211_STA_DISABLE_HE;
Johannes Berg9caf0362012-11-29 01:25:20 +01005506 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01005507
Johannes Berg848955c2014-11-11 12:48:42 +01005508 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02005509 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
Johannes Berg848955c2014-11-11 12:48:42 +01005510 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5511 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5512
Kalle Valoab133152010-01-12 10:42:31 +02005513 if (bss->wmm_used && bss->uapsd_supported &&
Johannes Berg848955c2014-11-11 12:48:42 +01005514 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01005515 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02005516 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5517 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01005518 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02005519 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5520 }
5521
Johannes Berg77fdaa12009-07-07 03:45:17 +02005522 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01005523 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005524
5525 if (req->use_mfp) {
5526 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5527 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5528 } else {
5529 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5530 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5531 }
5532
Assaf Krausscd2f5dd2014-09-03 15:25:02 +03005533 if (req->flags & ASSOC_REQ_USE_RRM)
5534 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5535 else
5536 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5537
Johannes Berg77fdaa12009-07-07 03:45:17 +02005538 if (req->crypto.control_port)
5539 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5540 else
5541 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5542
Johannes Berga621fa42010-08-27 14:26:54 +03005543 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5544 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Denis Kenzior018f6fb2018-03-26 12:52:51 -05005545 sdata->control_port_over_nl80211 =
5546 req->crypto.control_port_over_nl80211;
Markus Theil7f3f96c2020-03-12 10:10:54 +01005547 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02005548 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5549 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03005550
Johannes Berg66e67e42012-01-20 13:55:27 +01005551 /* kick off associate process */
5552
5553 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02005554 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03005555 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01005556
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005557 /* override HT/VHT configuration only if the AP and we support it */
5558 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5559 struct ieee80211_sta_ht_cap sta_ht_cap;
5560
5561 if (req->flags & ASSOC_REQ_DISABLE_HT)
5562 override = true;
5563
5564 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5565 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5566
5567 /* check for 40 MHz disable override */
5568 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5569 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5570 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5571 override = true;
5572
5573 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5574 req->flags & ASSOC_REQ_DISABLE_VHT)
5575 override = true;
5576 }
5577
5578 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5579 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5580 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005581 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005582 }
5583
5584 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5585 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5586
5587 err = ieee80211_prep_connection(sdata, req->bss, true, override);
Johannes Berga1cf7752012-03-08 15:02:07 +01005588 if (err)
5589 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01005590
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005591 rcu_read_lock();
5592 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02005593
Johannes Berg30686bf2015-06-02 21:39:54 +02005594 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005595 !beacon_ies) {
5596 /*
5597 * Wait up to one beacon interval ...
5598 * should this be more if we miss one?
5599 */
5600 sdata_info(sdata, "waiting for beacon from %pM\n",
5601 ifmgd->bssid);
5602 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01005603 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005604 assoc_data->need_beacon = true;
5605 } else if (beacon_ies) {
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005606 const struct element *elem;
Johannes Bergef429da2013-02-05 17:48:40 +01005607 u8 dtim_count = 0;
5608
Sara Sharon78ac51f2019-01-16 18:22:56 +02005609 ieee80211_get_dtim(beacon_ies, &dtim_count,
5610 &ifmgd->dtim_period);
5611
Alexander Bondar989c6502013-05-16 17:34:17 +03005612 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005613 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005614 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01005615
Johannes Berg30686bf2015-06-02 21:39:54 +02005616 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005617 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5618 sdata->vif.bss_conf.sync_device_ts =
5619 bss->device_ts_beacon;
5620 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5621 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02005622
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005623 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5624 beacon_ies->data, beacon_ies->len);
5625 if (elem && elem->datalen >= 3)
5626 sdata->vif.bss_conf.profile_periodicity = elem->data[2];
Sara Sharon78ac51f2019-01-16 18:22:56 +02005627
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005628 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5629 beacon_ies->data, beacon_ies->len);
5630 if (elem && elem->datalen >= 11 &&
5631 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
Sara Sharon78ac51f2019-01-16 18:22:56 +02005632 sdata->vif.bss_conf.ema_ap = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005633 } else {
5634 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005635 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005636 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005637 rcu_read_unlock();
5638
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005639 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01005640
Paul Stewartfcff4f12012-02-23 17:59:53 -08005641 if (bss->corrupt_data) {
5642 char *corrupt_type = "data";
5643 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5644 if (bss->corrupt_data &
5645 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5646 corrupt_type = "beacon and probe response";
5647 else
5648 corrupt_type = "beacon";
5649 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5650 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005651 sdata_info(sdata, "associating with AP with corrupt %s\n",
5652 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08005653 }
5654
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005655 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01005656 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005657 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005658 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005659 ifmgd->assoc_data = NULL;
5660 err_free:
5661 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005662 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005663}
5664
5665int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005666 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005667{
5668 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005669 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005670 bool tx = !req->local_state_change;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005671
Johannes Bergc9c3a062014-03-19 09:11:19 +01005672 if (ifmgd->auth_data &&
5673 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5674 sdata_info(sdata,
5675 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5676 req->bssid, req->reason_code,
5677 ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005678
Ilan Peerd4e36e52018-04-20 13:49:25 +03005679 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005680 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Johannes Berg37ad3882012-02-24 13:50:54 +01005681 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005682 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01005683 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01005684 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005685 ieee80211_report_disconnect(sdata, frame_buf,
5686 sizeof(frame_buf), true,
5687 req->reason_code);
Johannes Berg86552012012-10-29 09:46:31 +01005688
Johannes Bergc9c3a062014-03-19 09:11:19 +01005689 return 0;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03005690 }
5691
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005692 if (ifmgd->assoc_data &&
5693 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5694 sdata_info(sdata,
5695 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5696 req->bssid, req->reason_code,
5697 ieee80211_get_reason_code_string(req->reason_code));
5698
Ilan Peerd4e36e52018-04-20 13:49:25 +03005699 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005700 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005701 IEEE80211_STYPE_DEAUTH,
5702 req->reason_code, tx,
5703 frame_buf);
Johannes Berge6f462d2016-12-08 17:22:09 +01005704 ieee80211_destroy_assoc_data(sdata, false, true);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005705 ieee80211_report_disconnect(sdata, frame_buf,
5706 sizeof(frame_buf), true,
5707 req->reason_code);
5708 return 0;
5709 }
5710
Johannes Berg86552012012-10-29 09:46:31 +01005711 if (ifmgd->associated &&
5712 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Bergc9c3a062014-03-19 09:11:19 +01005713 sdata_info(sdata,
5714 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5715 req->bssid, req->reason_code,
5716 ieee80211_get_reason_code_string(req->reason_code));
5717
Johannes Berg86552012012-10-29 09:46:31 +01005718 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5719 req->reason_code, tx, frame_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005720 ieee80211_report_disconnect(sdata, frame_buf,
5721 sizeof(frame_buf), true,
5722 req->reason_code);
Johannes Bergc9c3a062014-03-19 09:11:19 +01005723 return 0;
5724 }
Johannes Berg86552012012-10-29 09:46:31 +01005725
Johannes Bergc9c3a062014-03-19 09:11:19 +01005726 return -ENOTCONN;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005727}
5728
5729int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005730 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005731{
5732 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07005733 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005734 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02005735
Johannes Berg8d8b2612009-07-25 11:58:36 +02005736 /*
5737 * cfg80211 should catch this ... but it's racy since
5738 * we can receive a disassoc frame, process it, hand it
5739 * to cfg80211 while that's in a locked section already
5740 * trying to tell us that the user wants to disconnect.
5741 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005742 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005743 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005744
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005745 sdata_info(sdata,
Calvin Owensdfa1ad22014-02-11 12:36:24 -06005746 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5747 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005748
Jouni Malinene69e95d2010-03-29 23:29:31 -07005749 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01005750 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5751 req->reason_code, !req->local_state_change,
5752 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005753
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005754 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5755 req->reason_code);
Johannes Bergbc83b682009-11-29 12:19:06 +01005756
Johannes Berg77fdaa12009-07-07 03:45:17 +02005757 return 0;
5758}
Jouni Malinen026331c2010-02-15 12:53:10 +02005759
Eliad Pellerafa762f2012-04-23 14:45:15 +03005760void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005761{
5762 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5763
Ben Greear49921852013-02-20 09:41:09 -08005764 /*
5765 * Make sure some work items will not run after this,
5766 * they will not do anything but might not have been
5767 * cancelled when disconnecting.
5768 */
5769 cancel_work_sync(&ifmgd->monitor_work);
5770 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5771 cancel_work_sync(&ifmgd->request_smps_work);
5772 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5773 cancel_work_sync(&ifmgd->chswitch_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03005774 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
Ben Greear49921852013-02-20 09:41:09 -08005775
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005776 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02005777 if (ifmgd->assoc_data) {
5778 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berge6f462d2016-12-08 17:22:09 +01005779 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02005780 cfg80211_assoc_timeout(sdata->dev, bss);
5781 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005782 if (ifmgd->auth_data)
5783 ieee80211_destroy_auth_data(sdata, false);
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005784 spin_lock_bh(&ifmgd->teardown_lock);
5785 if (ifmgd->teardown_skb) {
5786 kfree_skb(ifmgd->teardown_skb);
5787 ifmgd->teardown_skb = NULL;
5788 ifmgd->orig_teardown_skb = NULL;
5789 }
Jouni Malinen4d9ec732019-02-15 02:14:33 +02005790 kfree(ifmgd->assoc_req_ies);
5791 ifmgd->assoc_req_ies = NULL;
5792 ifmgd->assoc_req_ies_len = 0;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005793 spin_unlock_bh(&ifmgd->teardown_lock);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005794 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005795 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005796}
5797
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005798void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5799 enum nl80211_cqm_rssi_threshold_event rssi_event,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005800 s32 rssi_level,
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005801 gfp_t gfp)
5802{
5803 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5804
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005805 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
Johannes Bergb5878a22010-04-07 16:48:40 +02005806
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +01005807 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005808}
5809EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Johannes Berg98f03342014-11-26 12:42:02 +01005810
5811void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5812{
5813 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5814
5815 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5816
5817 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5818}
5819EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);