blob: 8a37089e86bb1ad5d66f4350916cbdfe6bd30647 [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;
170 vht_chandef = *chandef;
171 goto out;
172 }
173
174 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
175 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
176
Johannes Bergb1b1ae22017-10-13 15:26:01 +0300177 if (!ht_oper || !sta_ht_cap.ht_supported) {
Johannes Berg75e296e2020-01-31 13:12:39 +0200178 ret = IEEE80211_STA_DISABLE_HT |
179 IEEE80211_STA_DISABLE_VHT |
180 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100181 goto out;
182 }
183
184 chandef->width = NL80211_CHAN_WIDTH_20;
185
186 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
187 channel->band);
188 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200189 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100190 /*
191 * It's possible that some APs are confused here;
192 * Netgear WNDR3700 sometimes reports 4 higher than
193 * the actual channel in association responses, but
194 * since we look at probe response/beacon data here
195 * it should be OK.
196 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200197 sdata_info(sdata,
198 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
199 channel->center_freq, ht_cfreq,
200 ht_oper->primary_chan, channel->band);
Johannes Berg75e296e2020-01-31 13:12:39 +0200201 ret = IEEE80211_STA_DISABLE_HT |
202 IEEE80211_STA_DISABLE_VHT |
203 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100204 goto out;
205 }
206
207 /* check 40 MHz support, if we have it */
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200208 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100209 ieee80211_chandef_ht_oper(ht_oper, chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100210 } else {
211 /* 40 MHz (and 80 MHz) must be supported for VHT */
212 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100213 /* also mark 40 MHz disabled */
214 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100215 goto out;
216 }
217
218 if (!vht_oper || !sband->vht_cap.vht_supported) {
219 ret = IEEE80211_STA_DISABLE_VHT;
220 goto out;
221 }
222
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100223 vht_chandef = *chandef;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300224 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
225 (le32_to_cpu(he_oper->he_oper_params) &
226 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
227 struct ieee80211_vht_operation he_oper_vht_cap;
228
229 /*
230 * Set only first 3 bytes (other 2 aren't used in
231 * ieee80211_chandef_vht_oper() anyway)
232 */
233 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
234 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
235
Johannes Berg2a333a02020-05-28 21:34:35 +0200236 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
Johannes Berg7eb26df2018-08-31 11:31:18 +0300237 &he_oper_vht_cap, ht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300238 &vht_chandef)) {
239 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
240 sdata_info(sdata,
241 "HE AP VHT information is invalid, disable HE\n");
242 ret = IEEE80211_STA_DISABLE_HE;
243 goto out;
244 }
Johannes Berg2a333a02020-05-28 21:34:35 +0200245 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
246 vht_cap_info,
247 vht_oper, ht_oper,
248 &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200249 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100250 sdata_info(sdata,
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100251 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100252 ret = IEEE80211_STA_DISABLE_VHT;
253 goto out;
254 }
255
256 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200257 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100258 sdata_info(sdata,
259 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100260 ret = IEEE80211_STA_DISABLE_VHT;
261 goto out;
262 }
263
264 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
265 ret = 0;
266 goto out;
267 }
268
269 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200270 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100271 sdata_info(sdata,
272 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100273 ret = IEEE80211_STA_DISABLE_VHT;
274 goto out;
275 }
276
277 *chandef = vht_chandef;
278
279 ret = 0;
280
281out:
Johannes Berg963a18522014-02-21 20:34:34 +0100282 /*
283 * When tracking the current AP, don't do any further checks if the
284 * new chandef is identical to the one we're currently using for the
285 * connection. This keeps us from playing ping-pong with regulatory,
286 * without it the following can happen (for example):
287 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
288 * - AP advertises regdom US
289 * - CRDA loads regdom US with 80 MHz prohibited (old database)
290 * - the code below detects an unsupported channel, downgrades, and
291 * we disconnect from the AP in the caller
292 * - disconnect causes CRDA to reload world regdomain and the game
293 * starts anew.
294 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
295 *
296 * It seems possible that there are still scenarios with CSA or real
297 * bandwidth changes where a this could happen, but those cases are
298 * less common and wouldn't completely prevent using the AP.
299 */
300 if (tracking &&
301 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
302 return ret;
303
Johannes Berg586e01e2013-02-14 12:13:53 +0100304 /* don't print the message below for VHT mismatch if VHT is disabled */
305 if (ret & IEEE80211_STA_DISABLE_VHT)
306 vht_chandef = *chandef;
307
Johannes Bergddfe49b2013-07-31 20:52:03 +0200308 /*
309 * Ignore the DISABLED flag when we're already connected and only
310 * tracking the APs beacon for bandwidth changes - otherwise we
311 * might get disconnected here if we connect to an AP, update our
312 * regulatory information based on the AP's country IE and the
313 * information we have is wrong/outdated and disables the channel
314 * that we're actually using for the connection to the AP.
315 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100316 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200317 tracking ? 0 :
318 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100319 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
320 ret = IEEE80211_STA_DISABLE_HT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200321 IEEE80211_STA_DISABLE_VHT |
322 IEEE80211_STA_DISABLE_HE;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700323 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100324 }
325
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200326 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100327 }
328
Daniel Gabay8cadb202020-01-31 13:12:52 +0200329 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
330 IEEE80211_CHAN_NO_HE))
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200331 ret |= IEEE80211_STA_DISABLE_HE;
332
Johannes Berg5cdaed12013-07-31 11:23:06 +0200333 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100334 sdata_info(sdata,
335 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
336
337 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
338 return ret;
339}
340
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100341static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
342 struct sta_info *sta,
Eliad Peller53b954e2014-07-24 11:20:05 +0300343 const struct ieee80211_ht_cap *ht_cap,
Johannes Berg2a333a02020-05-28 21:34:35 +0200344 const struct ieee80211_vht_cap *vht_cap,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100345 const struct ieee80211_ht_operation *ht_oper,
346 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300347 const struct ieee80211_he_operation *he_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100348 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200349{
350 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100351 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300352 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
353 struct ieee80211_supported_band *sband =
354 local->hw.wiphy->bands[chan->band];
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100355 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200356 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100357 u32 flags;
358 enum ieee80211_sta_rx_bandwidth new_sta_bw;
Johannes Berg2a333a02020-05-28 21:34:35 +0200359 u32 vht_cap_info = 0;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100360 int ret;
361
362 /* if HT was/is disabled, don't track any bandwidth changes */
363 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
364 return 0;
365
366 /* don't check VHT if we associated as non-VHT station */
367 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
368 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200369
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300370 /* don't check HE if we associated as non-HE station */
371 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
372 !ieee80211_get_he_sta_cap(sband))
373 he_oper = NULL;
374
Johannes Berg11289582013-02-07 17:36:12 +0100375 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100376 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100377
Avri Altman017b45b2013-11-18 19:06:48 +0200378 /*
379 * if bss configuration changed store the new one -
380 * this may be applicable even if channel is identical
381 */
382 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
383 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
384 *changed |= BSS_CHANGED_HT;
385 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
386 }
387
Johannes Berg2a333a02020-05-28 21:34:35 +0200388 if (vht_cap)
389 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
390
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300391 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
Johannes Berg2a333a02020-05-28 21:34:35 +0200392 flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300393 ht_oper, vht_oper, he_oper,
Eliad Peller53b954e2014-07-24 11:20:05 +0300394 &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100395
396 /*
397 * Downgrade the new channel if we associated with restricted
398 * capabilities. For example, if we associated as a 20 MHz STA
399 * to a 40 MHz AP (due to regulatory, capabilities or config
400 * reasons) then switching to a 40 MHz channel now won't do us
401 * any good -- we couldn't use it with the AP.
402 */
403 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
404 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200405 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100406 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
407 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200408 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100409 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
410 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200411 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100412
413 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
414 return 0;
415
416 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -0700417 "AP %pM changed bandwidth, new config is %d.%03d MHz, "
418 "width %d (%d.%03d/%d MHz)\n",
419 ifmgd->bssid, chandef.chan->center_freq,
420 chandef.chan->freq_offset, chandef.width,
421 chandef.center_freq1, chandef.freq1_offset,
422 chandef.center_freq2);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100423
424 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
425 IEEE80211_STA_DISABLE_VHT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200426 IEEE80211_STA_DISABLE_HE |
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100427 IEEE80211_STA_DISABLE_40MHZ |
428 IEEE80211_STA_DISABLE_80P80MHZ |
429 IEEE80211_STA_DISABLE_160MHZ)) ||
430 !cfg80211_chandef_valid(&chandef)) {
431 sdata_info(sdata,
432 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
433 ifmgd->bssid);
434 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200435 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200436
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100437 switch (chandef.width) {
438 case NL80211_CHAN_WIDTH_20_NOHT:
439 case NL80211_CHAN_WIDTH_20:
440 new_sta_bw = IEEE80211_STA_RX_BW_20;
441 break;
442 case NL80211_CHAN_WIDTH_40:
443 new_sta_bw = IEEE80211_STA_RX_BW_40;
444 break;
445 case NL80211_CHAN_WIDTH_80:
446 new_sta_bw = IEEE80211_STA_RX_BW_80;
447 break;
448 case NL80211_CHAN_WIDTH_80P80:
449 case NL80211_CHAN_WIDTH_160:
450 new_sta_bw = IEEE80211_STA_RX_BW_160;
451 break;
452 default:
453 return -EINVAL;
454 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200455
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100456 if (new_sta_bw > sta->cur_max_bandwidth)
457 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200458
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100459 if (new_sta_bw < sta->sta.bandwidth) {
460 sta->sta.bandwidth = new_sta_bw;
461 rate_control_rate_update(local, sband, sta,
462 IEEE80211_RC_BW_CHANGED);
463 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530464
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100465 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
466 if (ret) {
467 sdata_info(sdata,
468 "AP %pM changed bandwidth to incompatible one - disconnect\n",
469 ifmgd->bssid);
470 return ret;
471 }
472
473 if (new_sta_bw > sta->sta.bandwidth) {
474 sta->sta.bandwidth = new_sta_bw;
475 rate_control_rate_update(local, sband, sta,
476 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200477 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200478
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100479 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200480}
481
Johannes Berg9c6bd792008-09-11 00:01:52 +0200482/* frame sending functions */
483
Johannes Berg66e67e42012-01-20 13:55:27 +0100484static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200485 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100486 struct ieee80211_supported_band *sband,
487 struct ieee80211_channel *channel,
488 enum ieee80211_smps_mode smps)
489{
Johannes Berg66e67e42012-01-20 13:55:27 +0100490 u8 *pos;
491 u32 flags = channel->flags;
492 u16 cap;
493 struct ieee80211_sta_ht_cap ht_cap;
494
495 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
496
Johannes Berg66e67e42012-01-20 13:55:27 +0100497 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
498 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
499
Johannes Berg66e67e42012-01-20 13:55:27 +0100500 /* determine capability flags */
501 cap = ht_cap.cap;
502
Johannes Berg9dde6422012-05-16 23:43:19 +0200503 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100504 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
505 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
506 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
507 cap &= ~IEEE80211_HT_CAP_SGI_40;
508 }
509 break;
510 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
511 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
512 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
513 cap &= ~IEEE80211_HT_CAP_SGI_40;
514 }
515 break;
516 }
517
Johannes Berg24398e32012-03-28 10:58:36 +0200518 /*
519 * If 40 MHz was disabled associate as though we weren't
520 * capable of 40 MHz -- some broken APs will never fall
521 * back to trying to transmit in 20 MHz.
522 */
523 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
524 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
525 cap &= ~IEEE80211_HT_CAP_SGI_40;
526 }
527
Johannes Berg66e67e42012-01-20 13:55:27 +0100528 /* set SM PS mode properly */
529 cap &= ~IEEE80211_HT_CAP_SM_PS;
530 switch (smps) {
531 case IEEE80211_SMPS_AUTOMATIC:
532 case IEEE80211_SMPS_NUM_MODES:
533 WARN_ON(1);
Gustavo A. R. Silva02049ce2017-10-17 18:14:50 -0500534 /* fall through */
Johannes Berg66e67e42012-01-20 13:55:27 +0100535 case IEEE80211_SMPS_OFF:
536 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
537 IEEE80211_HT_CAP_SM_PS_SHIFT;
538 break;
539 case IEEE80211_SMPS_STATIC:
540 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
541 IEEE80211_HT_CAP_SM_PS_SHIFT;
542 break;
543 case IEEE80211_SMPS_DYNAMIC:
544 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
545 IEEE80211_HT_CAP_SM_PS_SHIFT;
546 break;
547 }
548
549 /* reserve and fill IE */
550 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
551 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
552}
553
Sara Sharon322cd402015-07-08 15:41:43 +0300554/* This function determines vht capability flags for the association
555 * and builds the IE.
556 * Note - the function may set the owner of the MU-MIMO capability
557 */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000558static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
559 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100560 struct ieee80211_supported_band *sband,
561 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000562{
Sara Sharon322cd402015-07-08 15:41:43 +0300563 struct ieee80211_local *local = sdata->local;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000564 u8 *pos;
565 u32 cap;
566 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200567 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000568
569 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
570
571 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100572 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000573
574 /* determine capability flags */
575 cap = vht_cap.cap;
576
Johannes Bergf2d9d272012-11-22 14:11:39 +0100577 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
Johannes Berg575f0532014-11-24 16:51:33 +0100578 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
579
580 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
581 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
582 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
583 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100584 }
585
586 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
587 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
Johannes Berg575f0532014-11-24 16:51:33 +0100588 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100589 }
590
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100591 /*
592 * Some APs apparently get confused if our capabilities are better
593 * than theirs, so restrict what we advertise in the assoc request.
594 */
595 if (!(ap_vht_cap->vht_cap_info &
596 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
Sara Sharon322cd402015-07-08 15:41:43 +0300597 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
598 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
599 else if (!(ap_vht_cap->vht_cap_info &
600 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
601 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
602
603 /*
604 * If some other vif is using the MU-MIMO capablity we cannot associate
605 * using MU-MIMO - this will lead to contradictions in the group-id
606 * mechanism.
607 * Ownership is defined since association request, in order to avoid
608 * simultaneous associations with MU-MIMO.
609 */
610 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
611 bool disable_mu_mimo = false;
612 struct ieee80211_sub_if_data *other;
613
614 list_for_each_entry_rcu(other, &local->interfaces, list) {
Sara Sharonb5a33d52016-02-16 12:48:18 +0200615 if (other->vif.mu_mimo_owner) {
Sara Sharon322cd402015-07-08 15:41:43 +0300616 disable_mu_mimo = true;
617 break;
618 }
619 }
620 if (disable_mu_mimo)
621 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
622 else
Sara Sharonb5a33d52016-02-16 12:48:18 +0200623 sdata->vif.mu_mimo_owner = true;
Sara Sharon322cd402015-07-08 15:41:43 +0300624 }
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100625
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200626 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
627
628 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
629 our_bf_sts = cap & mask;
630
631 if (ap_bf_sts < our_bf_sts) {
632 cap &= ~mask;
633 cap |= ap_bf_sts;
634 }
635
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000636 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000637 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000638 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
639}
640
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300641/* This function determines HE capability flags for the association
642 * and builds the IE.
643 */
644static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
645 struct sk_buff *skb,
646 struct ieee80211_supported_band *sband)
647{
648 u8 *pos;
649 const struct ieee80211_sta_he_cap *he_cap = NULL;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200650 struct ieee80211_chanctx_conf *chanctx_conf;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300651 u8 he_cap_size;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200652 bool reg_cap = false;
653
654 rcu_read_lock();
655 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
656 if (!WARN_ON_ONCE(!chanctx_conf))
657 reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
658 &chanctx_conf->def,
659 IEEE80211_CHAN_NO_HE);
660
661 rcu_read_unlock();
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300662
663 he_cap = ieee80211_get_he_sta_cap(sband);
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200664 if (!he_cap || !reg_cap)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300665 return;
666
667 /*
668 * TODO: the 1 added is because this temporarily is under the EXTENSION
669 * IE. Get rid of it when it moves.
670 */
671 he_cap_size =
672 2 + 1 + sizeof(he_cap->he_cap_elem) +
673 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
674 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
675 he_cap->he_cap_elem.phy_cap_info);
676 pos = skb_put(skb, he_cap_size);
677 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200678
679 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300680}
681
Johannes Berg66e67e42012-01-20 13:55:27 +0100682static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
683{
684 struct ieee80211_local *local = sdata->local;
685 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
686 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
687 struct sk_buff *skb;
688 struct ieee80211_mgmt *mgmt;
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200689 u8 *pos, qos_info, *ie_start;
Johannes Berg66e67e42012-01-20 13:55:27 +0100690 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200691 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100692 u16 capab;
693 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200694 struct ieee80211_chanctx_conf *chanctx_conf;
695 struct ieee80211_channel *chan;
Johannes Berg44f6d422017-06-10 13:52:44 +0300696 u32 rates = 0;
Johannes Berg2ff69b02020-01-31 13:31:11 +0200697 struct element *ext_capa = NULL;
698
699 /* we know it's writable, cast away the const */
700 if (assoc_data->ie_len)
701 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
702 assoc_data->ie,
703 assoc_data->ie_len);
Johannes Berg66e67e42012-01-20 13:55:27 +0100704
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200705 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100706
Johannes Berg55de9082012-07-26 17:24:39 +0200707 rcu_read_lock();
708 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
709 if (WARN_ON(!chanctx_conf)) {
710 rcu_read_unlock();
711 return;
712 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100713 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200714 rcu_read_unlock();
715 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200716 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100717
718 if (assoc_data->supp_rates_len) {
719 /*
720 * Get all rates supported by the device and the AP as
721 * some APs don't like getting a superset of their rates
722 * in the association request (e.g. D-Link DAP 1353 in
723 * b-only mode)...
724 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200725 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
726 assoc_data->supp_rates,
727 assoc_data->supp_rates_len,
728 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100729 } else {
730 /*
731 * In case AP not provide any supported rates information
732 * before association, we send information element(s) with
733 * all rates that we support.
734 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200735 rates_len = 0;
736 for (i = 0; i < sband->n_bitrates; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200737 rates |= BIT(i);
738 rates_len++;
739 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100740 }
741
742 skb = alloc_skb(local->hw.extra_tx_headroom +
743 sizeof(*mgmt) + /* bit too much but doesn't matter */
744 2 + assoc_data->ssid_len + /* SSID */
745 4 + rates_len + /* (extended) rates */
746 4 + /* power capability */
747 2 + 2 * sband->n_channels + /* supported channels */
748 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000749 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300750 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
751 sizeof(struct ieee80211_he_mcs_nss_supp) +
752 IEEE80211_HE_PPE_THRES_MAX_LEN +
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200753 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100754 assoc_data->ie_len + /* extra IEs */
Jouni Malinen39404fe2016-10-27 00:42:05 +0300755 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100756 9, /* WMM */
757 GFP_KERNEL);
758 if (!skb)
759 return;
760
761 skb_reserve(skb, local->hw.extra_tx_headroom);
762
763 capab = WLAN_CAPABILITY_ESS;
764
Johannes Berg57fbcce2016-04-12 15:56:15 +0200765 if (sband->band == NL80211_BAND_2GHZ) {
Johannes Bergea1b2b452015-06-02 20:15:49 +0200766 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
767 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Johannes Berg66e67e42012-01-20 13:55:27 +0100768 }
769
770 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
771 capab |= WLAN_CAPABILITY_PRIVACY;
772
773 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200774 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
Johannes Berg66e67e42012-01-20 13:55:27 +0100775 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
776
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300777 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
778 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
779
Johannes Bergb080db52017-06-16 14:29:19 +0200780 mgmt = skb_put_zero(skb, 24);
Johannes Berg66e67e42012-01-20 13:55:27 +0100781 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
782 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
783 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
784
785 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
786 skb_put(skb, 10);
787 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
788 IEEE80211_STYPE_REASSOC_REQ);
789 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
790 mgmt->u.reassoc_req.listen_interval =
791 cpu_to_le16(local->hw.conf.listen_interval);
792 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
793 ETH_ALEN);
794 } else {
795 skb_put(skb, 4);
796 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
797 IEEE80211_STYPE_ASSOC_REQ);
798 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
799 mgmt->u.assoc_req.listen_interval =
800 cpu_to_le16(local->hw.conf.listen_interval);
801 }
802
803 /* SSID */
804 pos = skb_put(skb, 2 + assoc_data->ssid_len);
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200805 ie_start = pos;
Johannes Berg66e67e42012-01-20 13:55:27 +0100806 *pos++ = WLAN_EID_SSID;
807 *pos++ = assoc_data->ssid_len;
808 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
809
810 /* add all rates which were marked to be used above */
811 supp_rates_len = rates_len;
812 if (supp_rates_len > 8)
813 supp_rates_len = 8;
814
815 pos = skb_put(skb, supp_rates_len + 2);
816 *pos++ = WLAN_EID_SUPP_RATES;
817 *pos++ = supp_rates_len;
818
819 count = 0;
820 for (i = 0; i < sband->n_bitrates; i++) {
821 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200822 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
823 5 * (1 << shift));
824 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100825 if (++count == 8)
826 break;
827 }
828 }
829
830 if (rates_len > count) {
831 pos = skb_put(skb, rates_len - count + 2);
832 *pos++ = WLAN_EID_EXT_SUPP_RATES;
833 *pos++ = rates_len - count;
834
835 for (i++; i < sband->n_bitrates; i++) {
836 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200837 int rate;
838 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
839 5 * (1 << shift));
840 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100841 }
842 }
843 }
844
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300845 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
846 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100847 pos = skb_put(skb, 4);
848 *pos++ = WLAN_EID_PWR_CAPABILITY;
849 *pos++ = 2;
850 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200851 /* max tx power */
852 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300853 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100854
Johannes Berg2ff69b02020-01-31 13:31:11 +0200855 /*
856 * Per spec, we shouldn't include the list of channels if we advertise
857 * support for extended channel switching, but we've always done that;
858 * (for now?) apply this restriction only on the (new) 6 GHz band.
859 */
860 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
861 (sband->band != NL80211_BAND_6GHZ ||
862 !ext_capa || ext_capa->datalen < 1 ||
863 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100864 /* TODO: get this in reg domain format */
865 pos = skb_put(skb, 2 * sband->n_channels + 2);
866 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
867 *pos++ = 2 * sband->n_channels;
868 for (i = 0; i < sband->n_channels; i++) {
869 *pos++ = ieee80211_frequency_to_channel(
870 sband->channels[i].center_freq);
871 *pos++ = 1; /* one channel in the subband*/
872 }
873 }
874
Sara Sharoncaf56332019-01-16 23:03:25 +0200875 /* Set MBSSID support for HE AP if needed */
876 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
Johannes Berg2ff69b02020-01-31 13:31:11 +0200877 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
878 ext_capa && ext_capa->datalen >= 3)
879 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
Sara Sharoncaf56332019-01-16 23:03:25 +0200880
Johannes Berg66e67e42012-01-20 13:55:27 +0100881 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200882 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100883 static const u8 before_ht[] = {
884 WLAN_EID_SSID,
885 WLAN_EID_SUPP_RATES,
886 WLAN_EID_EXT_SUPP_RATES,
887 WLAN_EID_PWR_CAPABILITY,
888 WLAN_EID_SUPPORTED_CHANNELS,
889 WLAN_EID_RSN,
890 WLAN_EID_QOS_CAPA,
891 WLAN_EID_RRM_ENABLED_CAPABILITIES,
892 WLAN_EID_MOBILITY_DOMAIN,
Johannes Berg8ed28742014-10-27 12:03:19 +0100893 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
894 WLAN_EID_RIC_DATA, /* reassoc only */
Johannes Berg66e67e42012-01-20 13:55:27 +0100895 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
896 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100897 static const u8 after_ric[] = {
898 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
899 WLAN_EID_HT_CAPABILITY,
900 WLAN_EID_BSS_COEX_2040,
Johannes Berga7f26d82017-08-05 11:44:32 +0300901 /* luckily this is almost always there */
Johannes Berg8ed28742014-10-27 12:03:19 +0100902 WLAN_EID_EXT_CAPABILITY,
903 WLAN_EID_QOS_TRAFFIC_CAPA,
904 WLAN_EID_TIM_BCAST_REQ,
905 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300906 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg8ed28742014-10-27 12:03:19 +0100907 WLAN_EID_VHT_CAPABILITY,
908 WLAN_EID_OPMODE_NOTIF,
909 };
910
911 noffset = ieee80211_ie_split_ric(assoc_data->ie,
912 assoc_data->ie_len,
913 before_ht,
914 ARRAY_SIZE(before_ht),
915 after_ric,
916 ARRAY_SIZE(after_ric),
917 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800918 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100919 offset = noffset;
920 }
921
Johannes Bergf2d9d272012-11-22 14:11:39 +0100922 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
923 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
924 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
925
Johannes Berg57fa5e82020-05-28 21:34:36 +0200926 if (sband->band != NL80211_BAND_6GHZ &&
927 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200928 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200929 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100930
Johannes Berg3de38022014-02-04 09:54:07 +0100931 /* if present, add any custom IEs that go before VHT */
932 if (assoc_data->ie_len) {
933 static const u8 before_vht[] = {
Johannes Berga7f26d82017-08-05 11:44:32 +0300934 /*
935 * no need to list the ones split off before HT
936 * or generated here
937 */
Johannes Berg3de38022014-02-04 09:54:07 +0100938 WLAN_EID_BSS_COEX_2040,
939 WLAN_EID_EXT_CAPABILITY,
940 WLAN_EID_QOS_TRAFFIC_CAPA,
941 WLAN_EID_TIM_BCAST_REQ,
942 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300943 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg3de38022014-02-04 09:54:07 +0100944 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100945
946 /* RIC already taken above, so no need to handle here anymore */
Johannes Berg3de38022014-02-04 09:54:07 +0100947 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
948 before_vht, ARRAY_SIZE(before_vht),
949 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800950 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg3de38022014-02-04 09:54:07 +0100951 offset = noffset;
952 }
953
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300954 /* if present, add any custom IEs that go before HE */
955 if (assoc_data->ie_len) {
956 static const u8 before_he[] = {
957 /*
958 * no need to list the ones split off before VHT
959 * or generated here
960 */
961 WLAN_EID_OPMODE_NOTIF,
962 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
963 /* 11ai elements */
964 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
965 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
966 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
967 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
968 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
969 /* TODO: add 11ah/11aj/11ak elements */
970 };
971
972 /* RIC already taken above, so no need to handle here anymore */
973 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
974 before_he, ARRAY_SIZE(before_he),
975 offset);
976 pos = skb_put(skb, noffset - offset);
977 memcpy(pos, assoc_data->ie + offset, noffset - offset);
978 offset = noffset;
979 }
980
Johannes Berg57fa5e82020-05-28 21:34:36 +0200981 if (sband->band != NL80211_BAND_6GHZ &&
982 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100983 ieee80211_add_vht_ie(sdata, skb, sband,
984 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000985
Shaul Triebitzdc7eb0f2018-12-15 11:03:20 +0200986 /*
987 * If AP doesn't support HT, mark HE as disabled.
988 * If on the 5GHz band, make sure it supports VHT.
989 */
990 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
991 (sband->band == NL80211_BAND_5GHZ &&
992 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
993 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
994
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300995 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
996 ieee80211_add_he_ie(sdata, skb, sband);
997
998 /* if present, add any custom non-vendor IEs that go after HE */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200999 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001000 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1001 assoc_data->ie_len,
1002 offset);
yuan linyub952f4d2017-06-18 22:52:04 +08001003 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001004 offset = noffset;
1005 }
1006
Johannes Berg76f03032012-03-08 15:02:05 +01001007 if (assoc_data->wmm) {
1008 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001009 qos_info = ifmgd->uapsd_queues;
1010 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +01001011 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1012 } else {
1013 qos_info = 0;
1014 }
1015
Arik Nemtsov40b861a2014-07-17 17:14:23 +03001016 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
Johannes Berg66e67e42012-01-20 13:55:27 +01001017 }
1018
1019 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001020 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001021 noffset = assoc_data->ie_len;
yuan linyub952f4d2017-06-18 22:52:04 +08001022 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001023 }
1024
Jouni Malinen39404fe2016-10-27 00:42:05 +03001025 if (assoc_data->fils_kek_len &&
1026 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
1027 dev_kfree_skb(skb);
1028 return;
1029 }
1030
Jouni Malinen4d9ec732019-02-15 02:14:33 +02001031 pos = skb_tail_pointer(skb);
1032 kfree(ifmgd->assoc_req_ies);
1033 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1034 ifmgd->assoc_req_ies_len = pos - ie_start;
1035
Ilan Peerd4e36e52018-04-20 13:49:25 +03001036 drv_mgd_prepare_tx(local, sdata, 0);
Johannes Berga1845fc2012-06-27 13:18:36 +02001037
Johannes Berg66e67e42012-01-20 13:55:27 +01001038 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg30686bf2015-06-02 21:39:54 +02001039 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01001040 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1041 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +01001042 ieee80211_tx_skb(sdata, skb);
1043}
1044
Kalle Valo572e0012009-02-10 17:09:31 +02001045void ieee80211_send_pspoll(struct ieee80211_local *local,
1046 struct ieee80211_sub_if_data *sdata)
1047{
Kalle Valo572e0012009-02-10 17:09:31 +02001048 struct ieee80211_pspoll *pspoll;
1049 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +02001050
Kalle Valod8cd1892010-01-05 20:16:26 +02001051 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1052 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +02001053 return;
Kalle Valo572e0012009-02-10 17:09:31 +02001054
Kalle Valod8cd1892010-01-05 20:16:26 +02001055 pspoll = (struct ieee80211_pspoll *) skb->data;
1056 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +02001057
Johannes Berg62ae67b2009-11-18 18:42:05 +01001058 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1059 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +02001060}
1061
Johannes Berg965beda2009-04-16 13:17:24 +02001062void ieee80211_send_nullfunc(struct ieee80211_local *local,
1063 struct ieee80211_sub_if_data *sdata,
Johannes Berg076cdcb2015-09-24 16:14:55 +02001064 bool powersave)
Johannes Berg965beda2009-04-16 13:17:24 +02001065{
1066 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +02001067 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301068 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +02001069
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001070 /* Don't send NDPs when STA is connected HE */
1071 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1072 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1073 return;
1074
Ben Caradoc-Davies7c181f42018-03-19 12:57:44 +13001075 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1076 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
Kalle Valod8cd1892010-01-05 20:16:26 +02001077 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +02001078 return;
1079
Kalle Valod8cd1892010-01-05 20:16:26 +02001080 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +02001081 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +02001082 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +02001083
Seth Forshee6c17b772013-02-11 11:21:07 -06001084 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1085 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +02001086
Johannes Berg30686bf2015-06-02 21:39:54 +02001087 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Pontus Fuchsff40b422013-06-04 12:44:52 +02001088 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1089
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001090 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301091 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1092
Johannes Berg62ae67b2009-11-18 18:42:05 +01001093 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +02001094}
1095
Felix Fietkaud5242152010-01-08 18:06:26 +01001096static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1097 struct ieee80211_sub_if_data *sdata)
1098{
1099 struct sk_buff *skb;
1100 struct ieee80211_hdr *nullfunc;
1101 __le16 fc;
1102
1103 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1104 return;
1105
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001106 /* Don't send NDPs when connected HE */
1107 if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1108 return;
1109
Felix Fietkaud5242152010-01-08 18:06:26 +01001110 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -07001111 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +01001112 return;
Joe Perchesd15b8452011-08-29 14:17:31 -07001113
Felix Fietkaud5242152010-01-08 18:06:26 +01001114 skb_reserve(skb, local->hw.extra_tx_headroom);
1115
Johannes Bergb080db52017-06-16 14:29:19 +02001116 nullfunc = skb_put_zero(skb, 30);
Felix Fietkaud5242152010-01-08 18:06:26 +01001117 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1118 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1119 nullfunc->frame_control = fc;
1120 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1121 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1122 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1123 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1124
1125 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1126 ieee80211_tx_skb(sdata, skb);
1127}
1128
Johannes Bergcc32abd2009-05-15 11:52:31 +02001129/* spectrum management related things */
1130static void ieee80211_chswitch_work(struct work_struct *work)
1131{
1132 struct ieee80211_sub_if_data *sdata =
1133 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +01001134 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001135 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001136 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001137
Johannes Berg9607e6b662009-12-23 13:15:31 +01001138 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +02001139 return;
1140
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001141 sdata_lock(sdata);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001142 mutex_lock(&local->mtx);
1143 mutex_lock(&local->chanctx_mtx);
1144
Johannes Berg77fdaa12009-07-07 03:45:17 +02001145 if (!ifmgd->associated)
1146 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001147
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001148 if (!sdata->vif.csa_active)
1149 goto out;
1150
1151 /*
1152 * using reservation isn't immediate as it may be deferred until later
1153 * with multi-vif. once reservation is complete it will re-schedule the
1154 * work with no reserved_chanctx so verify chandef to check if it
1155 * completed successfully
1156 */
1157
1158 if (sdata->reserved_chanctx) {
Ilan Peer0007e942018-08-31 11:31:10 +03001159 struct ieee80211_supported_band *sband = NULL;
1160 struct sta_info *mgd_sta = NULL;
1161 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1162
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001163 /*
1164 * with multi-vif csa driver may call ieee80211_csa_finish()
1165 * many times while waiting for other interfaces to use their
1166 * reservations
1167 */
1168 if (sdata->reserved_ready)
1169 goto out;
1170
Ilan Peer0007e942018-08-31 11:31:10 +03001171 if (sdata->vif.bss_conf.chandef.width !=
1172 sdata->csa_chandef.width) {
1173 /*
1174 * For managed interface, we need to also update the AP
1175 * station bandwidth and align the rate scale algorithm
1176 * on the bandwidth change. Here we only consider the
1177 * bandwidth of the new channel definition (as channel
1178 * switch flow does not have the full HT/VHT/HE
1179 * information), assuming that if additional changes are
1180 * required they would be done as part of the processing
1181 * of the next beacon from the AP.
1182 */
1183 switch (sdata->csa_chandef.width) {
1184 case NL80211_CHAN_WIDTH_20_NOHT:
1185 case NL80211_CHAN_WIDTH_20:
1186 default:
1187 bw = IEEE80211_STA_RX_BW_20;
1188 break;
1189 case NL80211_CHAN_WIDTH_40:
1190 bw = IEEE80211_STA_RX_BW_40;
1191 break;
1192 case NL80211_CHAN_WIDTH_80:
1193 bw = IEEE80211_STA_RX_BW_80;
1194 break;
1195 case NL80211_CHAN_WIDTH_80P80:
1196 case NL80211_CHAN_WIDTH_160:
1197 bw = IEEE80211_STA_RX_BW_160;
1198 break;
1199 }
1200
1201 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1202 sband =
1203 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1204 }
1205
1206 if (sdata->vif.bss_conf.chandef.width >
1207 sdata->csa_chandef.width) {
1208 mgd_sta->sta.bandwidth = bw;
1209 rate_control_rate_update(local, sband, mgd_sta,
1210 IEEE80211_RC_BW_CHANGED);
1211 }
1212
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001213 ret = ieee80211_vif_use_reserved_context(sdata);
1214 if (ret) {
1215 sdata_info(sdata,
1216 "failed to use reserved channel context, disconnecting (err=%d)\n",
1217 ret);
1218 ieee80211_queue_work(&sdata->local->hw,
1219 &ifmgd->csa_connection_drop_work);
1220 goto out;
1221 }
1222
Ilan Peer0007e942018-08-31 11:31:10 +03001223 if (sdata->vif.bss_conf.chandef.width <
1224 sdata->csa_chandef.width) {
1225 mgd_sta->sta.bandwidth = bw;
1226 rate_control_rate_update(local, sband, mgd_sta,
1227 IEEE80211_RC_BW_CHANGED);
1228 }
1229
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001230 goto out;
1231 }
1232
1233 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1234 &sdata->csa_chandef)) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001235 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001236 "failed to finalize channel switch, disconnecting\n");
Arik Nemtsov7578d572013-09-01 17:15:51 +03001237 ieee80211_queue_work(&sdata->local->hw,
1238 &ifmgd->csa_connection_drop_work);
1239 goto out;
1240 }
Karl Beldan675a0b02013-03-25 16:26:57 +01001241
Luciano Coelho0c21e632014-10-08 09:48:39 +03001242 ifmgd->csa_waiting_bcn = true;
Luciano Coelhof1d65582014-10-08 09:48:38 +03001243
Michal Kaziore5593f52014-04-09 15:45:36 +02001244 ieee80211_sta_reset_beacon_monitor(sdata);
1245 ieee80211_sta_reset_conn_monitor(sdata);
1246
Michal Kazior59af6922014-04-09 15:10:59 +02001247out:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001248 mutex_unlock(&local->chanctx_mtx);
1249 mutex_unlock(&local->mtx);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001250 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001251}
1252
Luciano Coelho0c21e632014-10-08 09:48:39 +03001253static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1254{
1255 struct ieee80211_local *local = sdata->local;
1256 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1257 int ret;
1258
1259 sdata_assert_lock(sdata);
1260
1261 WARN_ON(!sdata->vif.csa_active);
1262
1263 if (sdata->csa_block_tx) {
1264 ieee80211_wake_vif_queues(local, sdata,
1265 IEEE80211_QUEUE_STOP_REASON_CSA);
1266 sdata->csa_block_tx = false;
1267 }
1268
1269 sdata->vif.csa_active = false;
1270 ifmgd->csa_waiting_bcn = false;
1271
1272 ret = drv_post_channel_switch(sdata);
1273 if (ret) {
1274 sdata_info(sdata,
1275 "driver post channel switch failed, disconnecting\n");
1276 ieee80211_queue_work(&local->hw,
1277 &ifmgd->csa_connection_drop_work);
1278 return;
1279 }
Luciano Coelho688b1ec2014-12-16 16:01:39 +02001280
1281 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
Luciano Coelho0c21e632014-10-08 09:48:39 +03001282}
1283
Johannes Berg5ce6e432010-05-11 16:20:57 +02001284void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1285{
Johannes Berg55de9082012-07-26 17:24:39 +02001286 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1287 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001288
1289 trace_api_chswitch_done(sdata, success);
1290 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001291 sdata_info(sdata,
1292 "driver channel switch failed, disconnecting\n");
1293 ieee80211_queue_work(&sdata->local->hw,
1294 &ifmgd->csa_connection_drop_work);
1295 } else {
1296 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001297 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001298}
1299EXPORT_SYMBOL(ieee80211_chswitch_done);
1300
Kees Cook34f11cd2017-10-16 16:35:49 -07001301static void ieee80211_chswitch_timer(struct timer_list *t)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001302{
1303 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07001304 from_timer(sdata, t, u.mgd.chswitch_timer);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001305
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001306 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001307}
1308
Johannes Berg37799e52013-03-26 14:02:26 +01001309static void
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001310ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1311{
1312 struct ieee80211_local *local = sdata->local;
1313
1314 if (!local->ops->abort_channel_switch)
1315 return;
1316
1317 mutex_lock(&local->mtx);
1318
1319 mutex_lock(&local->chanctx_mtx);
1320 ieee80211_vif_unreserve_chanctx(sdata);
1321 mutex_unlock(&local->chanctx_mtx);
1322
1323 if (sdata->csa_block_tx)
1324 ieee80211_wake_vif_queues(local, sdata,
1325 IEEE80211_QUEUE_STOP_REASON_CSA);
1326
1327 sdata->csa_block_tx = false;
1328 sdata->vif.csa_active = false;
1329
1330 mutex_unlock(&local->mtx);
1331
1332 drv_abort_channel_switch(sdata);
1333}
1334
1335static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001336ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03001337 u64 timestamp, u32 device_timestamp,
1338 struct ieee802_11_elems *elems,
Johannes Berg04a161f2013-05-03 09:35:35 +02001339 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001340{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001341 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001342 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001343 struct cfg80211_bss *cbss = ifmgd->associated;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001344 struct ieee80211_chanctx_conf *conf;
Johannes Berg55de9082012-07-26 17:24:39 +02001345 struct ieee80211_chanctx *chanctx;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001346 enum nl80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001347 struct ieee80211_csa_ie csa_ie;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001348 struct ieee80211_channel_switch ch_switch;
Johannes Berg2a333a02020-05-28 21:34:35 +02001349 struct ieee80211_bss *bss;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001350 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001351
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001352 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001353
Johannes Berg37799e52013-03-26 14:02:26 +01001354 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001355 return;
1356
Johannes Bergb4f286a12013-03-26 14:13:58 +01001357 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001358 return;
1359
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001360 current_band = cbss->channel->band;
Johannes Berg2a333a02020-05-28 21:34:35 +02001361 bss = (void *)cbss->priv;
Luciano Coelho84469a42014-10-28 13:33:04 +02001362 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
Johannes Berg2a333a02020-05-28 21:34:35 +02001363 bss->vht_cap_info,
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001364 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001365 ifmgd->associated->bssid, &csa_ie);
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001366
1367 if (!res) {
1368 ch_switch.timestamp = timestamp;
1369 ch_switch.device_timestamp = device_timestamp;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001370 ch_switch.block_tx = csa_ie.mode;
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001371 ch_switch.chandef = csa_ie.chandef;
1372 ch_switch.count = csa_ie.count;
1373 ch_switch.delay = csa_ie.max_switch_time;
1374 }
1375
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001376 if (res < 0) {
Johannes Bergb4f286a12013-03-26 14:13:58 +01001377 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001378 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001379 return;
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001380 }
1381
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001382 if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1383 if (res)
1384 ieee80211_sta_abort_chanswitch(sdata);
1385 else
1386 drv_channel_switch_rx_beacon(sdata, &ch_switch);
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001387 return;
1388 } else if (sdata->vif.csa_active || res) {
1389 /* disregard subsequent announcements if already processing */
1390 return;
1391 }
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001392
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001393 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001394 IEEE80211_CHAN_DISABLED)) {
1395 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001396 "AP %pM switches to unsupported channel "
1397 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1398 "disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001399 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001400 csa_ie.chandef.chan->center_freq,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001401 csa_ie.chandef.chan->freq_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001402 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001403 csa_ie.chandef.freq1_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001404 csa_ie.chandef.center_freq2);
Johannes Berg85220d72013-03-25 18:29:27 +01001405 ieee80211_queue_work(&local->hw,
1406 &ifmgd->csa_connection_drop_work);
1407 return;
1408 }
1409
Johannes Bergf84eaa12015-03-12 08:53:26 +02001410 if (cfg80211_chandef_identical(&csa_ie.chandef,
Sara Sharon97928752019-02-06 13:17:16 +02001411 &sdata->vif.bss_conf.chandef) &&
1412 (!csa_ie.mode || !beacon)) {
Johannes Bergf84eaa12015-03-12 08:53:26 +02001413 if (ifmgd->csa_ignored_same_chan)
1414 return;
1415 sdata_info(sdata,
1416 "AP %pM tries to chanswitch to same channel, ignore\n",
1417 ifmgd->associated->bssid);
1418 ifmgd->csa_ignored_same_chan = true;
1419 return;
1420 }
1421
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001422 /*
1423 * Drop all TDLS peers - either we disconnect or move to a different
1424 * channel from this point on. There's no telling what our peer will do.
1425 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1426 * have an incompatible wider chandef.
1427 */
1428 ieee80211_teardown_tdls_peers(sdata);
1429
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001430 mutex_lock(&local->mtx);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001431 mutex_lock(&local->chanctx_mtx);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001432 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1433 lockdep_is_held(&local->chanctx_mtx));
1434 if (!conf) {
1435 sdata_info(sdata,
1436 "no channel context assigned to vif?, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001437 goto drop_connection;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001438 }
1439
1440 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1441
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001442 if (local->use_chanctx &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001443 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001444 sdata_info(sdata,
1445 "driver doesn't support chan-switch with channel contexts\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001446 goto drop_connection;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001447 }
1448
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001449 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1450 sdata_info(sdata,
1451 "preparing for channel switch failed, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001452 goto drop_connection;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001453 }
1454
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001455 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1456 chanctx->mode, false);
1457 if (res) {
Johannes Berg55de9082012-07-26 17:24:39 +02001458 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001459 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1460 res);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001461 goto drop_connection;
Johannes Berg55de9082012-07-26 17:24:39 +02001462 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001463 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001464
Michal Kazior59af6922014-04-09 15:10:59 +02001465 sdata->vif.csa_active = true;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001466 sdata->csa_chandef = csa_ie.chandef;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001467 sdata->csa_block_tx = csa_ie.mode;
Johannes Bergf84eaa12015-03-12 08:53:26 +02001468 ifmgd->csa_ignored_same_chan = false;
Michal Kazior59af6922014-04-09 15:10:59 +02001469
1470 if (sdata->csa_block_tx)
Luciano Coelhoa46992b2014-06-13 16:30:07 +03001471 ieee80211_stop_vif_queues(local, sdata,
1472 IEEE80211_QUEUE_STOP_REASON_CSA);
Michal Kazior59af6922014-04-09 15:10:59 +02001473 mutex_unlock(&local->mtx);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001474
Luciano Coelho2f457292014-11-07 14:31:36 +02001475 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1476 csa_ie.count);
1477
Johannes Bergb4f286a12013-03-26 14:13:58 +01001478 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001479 /* use driver's channel switch callback */
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001480 drv_channel_switch(local, sdata, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001481 return;
1482 }
1483
1484 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001485 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001486 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001487 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001488 mod_timer(&ifmgd->chswitch_timer,
Luciano Coelhoff1e4172014-10-28 13:33:05 +02001489 TU_TO_EXP_TIME((csa_ie.count - 1) *
1490 cbss->beacon_interval));
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001491 return;
1492 drop_connection:
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001493 /*
1494 * This is just so that the disconnect flow will know that
1495 * we were trying to switch channel and failed. In case the
1496 * mode is 1 (we are not allowed to Tx), we will know not to
1497 * send a deauthentication frame. Those two fields will be
1498 * reset when the disconnection worker runs.
1499 */
1500 sdata->vif.csa_active = true;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001501 sdata->csa_block_tx = csa_ie.mode;
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001502
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001503 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1504 mutex_unlock(&local->chanctx_mtx);
1505 mutex_unlock(&local->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001506}
1507
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001508static bool
1509ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1510 struct ieee80211_channel *channel,
1511 const u8 *country_ie, u8 country_ie_len,
1512 const u8 *pwr_constr_elem,
1513 int *chan_pwr, int *pwr_reduction)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001514{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001515 struct ieee80211_country_ie_triplet *triplet;
1516 int chan = ieee80211_frequency_to_channel(channel->center_freq);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001517 int i, chan_increment;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001518 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001519
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001520 /* Invalid IE */
1521 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001522 return false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001523
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001524 triplet = (void *)(country_ie + 3);
1525 country_ie_len -= 3;
1526
1527 switch (channel->band) {
1528 default:
1529 WARN_ON_ONCE(1);
1530 /* fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +02001531 case NL80211_BAND_2GHZ:
1532 case NL80211_BAND_60GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001533 chan_increment = 1;
1534 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001535 case NL80211_BAND_5GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001536 chan_increment = 4;
1537 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001538 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001539
1540 /* find channel */
1541 while (country_ie_len >= 3) {
1542 u8 first_channel = triplet->chans.first_channel;
1543
1544 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1545 goto next;
1546
1547 for (i = 0; i < triplet->chans.num_channels; i++) {
1548 if (first_channel + i * chan_increment == chan) {
1549 have_chan_pwr = true;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001550 *chan_pwr = triplet->chans.max_power;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001551 break;
1552 }
1553 }
1554 if (have_chan_pwr)
1555 break;
1556
1557 next:
1558 triplet++;
1559 country_ie_len -= 3;
1560 }
1561
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001562 if (have_chan_pwr && pwr_constr_elem)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001563 *pwr_reduction = *pwr_constr_elem;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001564 else
1565 *pwr_reduction = 0;
1566
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001567 return have_chan_pwr;
1568}
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001569
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001570static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1571 struct ieee80211_channel *channel,
1572 const u8 *cisco_dtpc_ie,
1573 int *pwr_level)
1574{
1575 /* From practical testing, the first data byte of the DTPC element
1576 * seems to contain the requested dBm level, and the CLI on Cisco
1577 * APs clearly state the range is -127 to 127 dBm, which indicates
1578 * a signed byte, although it seemingly never actually goes negative.
1579 * The other byte seems to always be zero.
1580 */
1581 *pwr_level = (__s8)cisco_dtpc_ie[4];
1582}
1583
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001584static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1585 struct ieee80211_channel *channel,
1586 struct ieee80211_mgmt *mgmt,
1587 const u8 *country_ie, u8 country_ie_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001588 const u8 *pwr_constr_ie,
1589 const u8 *cisco_dtpc_ie)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001590{
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001591 bool has_80211h_pwr = false, has_cisco_pwr = false;
1592 int chan_pwr = 0, pwr_reduction_80211h = 0;
1593 int pwr_level_cisco, pwr_level_80211h;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001594 int new_ap_level;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001595 __le16 capab = mgmt->u.probe_resp.capab_info;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001596
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001597 if (country_ie &&
1598 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1599 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001600 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1601 sdata, channel, country_ie, country_ie_len,
1602 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001603 pwr_level_80211h =
1604 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001605 }
1606
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001607 if (cisco_dtpc_ie) {
1608 ieee80211_find_cisco_dtpc(
1609 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1610 has_cisco_pwr = true;
1611 }
1612
1613 if (!has_80211h_pwr && !has_cisco_pwr)
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001614 return 0;
1615
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001616 /* If we have both 802.11h and Cisco DTPC, apply both limits
1617 * by picking the smallest of the two power levels advertised.
1618 */
1619 if (has_80211h_pwr &&
1620 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
Johannes Berga87da0c2015-12-08 16:04:37 +02001621 new_ap_level = pwr_level_80211h;
1622
1623 if (sdata->ap_power_level == new_ap_level)
1624 return 0;
1625
John Linvillecef2fc12015-03-31 10:49:14 -04001626 sdata_dbg(sdata,
1627 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1628 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1629 sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001630 } else { /* has_cisco_pwr is always true here. */
Johannes Berga87da0c2015-12-08 16:04:37 +02001631 new_ap_level = pwr_level_cisco;
1632
1633 if (sdata->ap_power_level == new_ap_level)
1634 return 0;
1635
John Linvillecef2fc12015-03-31 10:49:14 -04001636 sdata_dbg(sdata,
1637 "Limiting TX power to %d dBm as advertised by %pM\n",
1638 pwr_level_cisco, sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001639 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001640
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001641 sdata->ap_power_level = new_ap_level;
1642 if (__ieee80211_recalc_txpower(sdata))
1643 return BSS_CHANGED_TXPOWER;
1644 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001645}
1646
Johannes Berg965beda2009-04-16 13:17:24 +02001647/* powersave */
1648static void ieee80211_enable_ps(struct ieee80211_local *local,
1649 struct ieee80211_sub_if_data *sdata)
1650{
1651 struct ieee80211_conf *conf = &local->hw.conf;
1652
Johannes Bergd5edaed2009-04-22 23:02:51 +02001653 /*
1654 * If we are scanning right now then the parameters will
1655 * take effect when scan finishes.
1656 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001657 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001658 return;
1659
Johannes Berg965beda2009-04-16 13:17:24 +02001660 if (conf->dynamic_ps_timeout > 0 &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001661 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001662 mod_timer(&local->dynamic_ps_timer, jiffies +
1663 msecs_to_jiffies(conf->dynamic_ps_timeout));
1664 } else {
Johannes Berg30686bf2015-06-02 21:39:54 +02001665 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
Johannes Berg076cdcb2015-09-24 16:14:55 +02001666 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301667
Johannes Berg30686bf2015-06-02 21:39:54 +02001668 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1669 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001670 return;
1671
1672 conf->flags |= IEEE80211_CONF_PS;
1673 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001674 }
1675}
1676
1677static void ieee80211_change_ps(struct ieee80211_local *local)
1678{
1679 struct ieee80211_conf *conf = &local->hw.conf;
1680
1681 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001682 ieee80211_enable_ps(local, local->ps_sdata);
1683 } else if (conf->flags & IEEE80211_CONF_PS) {
1684 conf->flags &= ~IEEE80211_CONF_PS;
1685 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1686 del_timer_sync(&local->dynamic_ps_timer);
1687 cancel_work_sync(&local->dynamic_ps_enable_work);
1688 }
1689}
1690
Jason Young808118c2011-03-10 16:43:19 -08001691static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1692{
1693 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1694 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001695 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001696
1697 if (!mgd->powersave)
1698 return false;
1699
Johannes Berg05cb9102011-10-28 11:59:47 +02001700 if (mgd->broken_ap)
1701 return false;
1702
Jason Young808118c2011-03-10 16:43:19 -08001703 if (!mgd->associated)
1704 return false;
1705
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001706 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001707 return false;
1708
Alexander Bondar989c6502013-05-16 17:34:17 +03001709 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001710 return false;
1711
Jason Young808118c2011-03-10 16:43:19 -08001712 rcu_read_lock();
1713 sta = sta_info_get(sdata, mgd->bssid);
1714 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001715 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001716 rcu_read_unlock();
1717
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001718 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001719}
1720
Johannes Berg965beda2009-04-16 13:17:24 +02001721/* need to hold RTNL or interface lock */
Johannes Berg4a733ef2015-10-14 18:02:43 +02001722void ieee80211_recalc_ps(struct ieee80211_local *local)
Johannes Berg965beda2009-04-16 13:17:24 +02001723{
1724 struct ieee80211_sub_if_data *sdata, *found = NULL;
1725 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001726 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001727
Johannes Berg30686bf2015-06-02 21:39:54 +02001728 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001729 local->ps_sdata = NULL;
1730 return;
1731 }
1732
1733 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001734 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001735 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301736 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1737 /* If an AP vif is found, then disable PS
1738 * by setting the count to zero thereby setting
1739 * ps_sdata to NULL.
1740 */
1741 count = 0;
1742 break;
1743 }
Johannes Berg965beda2009-04-16 13:17:24 +02001744 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1745 continue;
1746 found = sdata;
1747 count++;
1748 }
1749
Jason Young808118c2011-03-10 16:43:19 -08001750 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg4a733ef2015-10-14 18:02:43 +02001751 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg10f644a2009-04-16 13:17:25 +02001752
Juuso Oikarinenff616382010-06-09 09:51:52 +03001753 timeout = local->dynamic_ps_forced_timeout;
Johannes Berg4a733ef2015-10-14 18:02:43 +02001754 if (timeout < 0)
1755 timeout = 100;
Johannes Berg09b85562013-02-06 23:07:41 +01001756 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001757
Johannes Berg4a733ef2015-10-14 18:02:43 +02001758 /* If the TIM IE is invalid, pretend the value is 1 */
1759 if (!dtimper)
1760 dtimper = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001761
Johannes Berg4a733ef2015-10-14 18:02:43 +02001762 local->hw.conf.ps_dtim_period = dtimper;
1763 local->ps_sdata = found;
Johannes Berg10f644a2009-04-16 13:17:25 +02001764 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001765 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001766 }
Johannes Berg965beda2009-04-16 13:17:24 +02001767
1768 ieee80211_change_ps(local);
1769}
1770
Eliad Pellerab095872012-07-27 12:33:22 +03001771void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1772{
1773 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1774
1775 if (sdata->vif.bss_conf.ps != ps_allowed) {
1776 sdata->vif.bss_conf.ps = ps_allowed;
1777 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1778 }
1779}
1780
Johannes Berg965beda2009-04-16 13:17:24 +02001781void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1782{
1783 struct ieee80211_local *local =
1784 container_of(work, struct ieee80211_local,
1785 dynamic_ps_disable_work);
1786
1787 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1788 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1789 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1790 }
1791
1792 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001793 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +03001794 IEEE80211_QUEUE_STOP_REASON_PS,
1795 false);
Johannes Berg965beda2009-04-16 13:17:24 +02001796}
1797
1798void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1799{
1800 struct ieee80211_local *local =
1801 container_of(work, struct ieee80211_local,
1802 dynamic_ps_enable_work);
1803 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001804 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301805 unsigned long flags;
1806 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001807
1808 /* can only happen when PS was just disabled anyway */
1809 if (!sdata)
1810 return;
1811
Christian Lamparter5e340692011-06-30 21:08:43 +02001812 ifmgd = &sdata->u.mgd;
1813
Johannes Berg965beda2009-04-16 13:17:24 +02001814 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1815 return;
1816
Johannes Berg09b85562013-02-06 23:07:41 +01001817 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001818 /* don't enter PS if TX frames are pending */
1819 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301820 mod_timer(&local->dynamic_ps_timer, jiffies +
1821 msecs_to_jiffies(
1822 local->hw.conf.dynamic_ps_timeout));
1823 return;
1824 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001825
1826 /*
1827 * transmission can be stopped by others which leads to
1828 * dynamic_ps_timer expiry. Postpone the ps timer if it
1829 * is not the actual idle state.
1830 */
1831 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1832 for (q = 0; q < local->hw.queues; q++) {
1833 if (local->queue_stop_reasons[q]) {
1834 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1835 flags);
1836 mod_timer(&local->dynamic_ps_timer, jiffies +
1837 msecs_to_jiffies(
1838 local->hw.conf.dynamic_ps_timeout));
1839 return;
1840 }
1841 }
1842 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301843 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301844
Johannes Berg30686bf2015-06-02 21:39:54 +02001845 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301846 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001847 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301848 mod_timer(&local->dynamic_ps_timer, jiffies +
1849 msecs_to_jiffies(
1850 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001851 } else {
Johannes Berg076cdcb2015-09-24 16:14:55 +02001852 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301853 /* Flush to get the tx status of nullfunc frame */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001854 ieee80211_flush_queues(local, sdata, false);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301855 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301856 }
1857
Johannes Berg30686bf2015-06-02 21:39:54 +02001858 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1859 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301860 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1861 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1862 local->hw.conf.flags |= IEEE80211_CONF_PS;
1863 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1864 }
Johannes Berg965beda2009-04-16 13:17:24 +02001865}
1866
Kees Cook34f11cd2017-10-16 16:35:49 -07001867void ieee80211_dynamic_ps_timer(struct timer_list *t)
Johannes Berg965beda2009-04-16 13:17:24 +02001868{
Kees Cook34f11cd2017-10-16 16:35:49 -07001869 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
Johannes Berg965beda2009-04-16 13:17:24 +02001870
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001871 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001872}
1873
Simon Wunderlich164eb022013-02-08 18:16:20 +01001874void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1875{
Geliang Tanga85a7e22016-01-01 23:48:52 +08001876 struct delayed_work *delayed_work = to_delayed_work(work);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001877 struct ieee80211_sub_if_data *sdata =
1878 container_of(delayed_work, struct ieee80211_sub_if_data,
1879 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001880 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001881
Johannes Berg34a37402013-12-18 09:43:33 +01001882 mutex_lock(&sdata->local->mtx);
1883 if (sdata->wdev.cac_started) {
1884 ieee80211_vif_release_channel(sdata);
1885 cfg80211_cac_event(sdata->dev, &chandef,
1886 NL80211_RADAR_CAC_FINISHED,
1887 GFP_KERNEL);
1888 }
1889 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001890}
1891
Johannes Berg02219b32014-10-07 10:38:50 +03001892static bool
1893__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1894{
1895 struct ieee80211_local *local = sdata->local;
1896 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
John Linvillecc72f6e2015-01-06 14:39:33 -05001897 bool ret = false;
Johannes Berg02219b32014-10-07 10:38:50 +03001898 int ac;
1899
1900 if (local->hw.queues < IEEE80211_NUM_ACS)
1901 return false;
1902
1903 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1904 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1905 int non_acm_ac;
1906 unsigned long now = jiffies;
1907
1908 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1909 tx_tspec->admitted_time &&
1910 time_after(now, tx_tspec->time_slice_start + HZ)) {
1911 tx_tspec->consumed_tx_time = 0;
1912 tx_tspec->time_slice_start = now;
1913
1914 if (tx_tspec->downgraded)
1915 tx_tspec->action =
1916 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1917 }
1918
1919 switch (tx_tspec->action) {
1920 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1921 /* take the original parameters */
1922 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1923 sdata_err(sdata,
1924 "failed to set TX queue parameters for queue %d\n",
1925 ac);
1926 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1927 tx_tspec->downgraded = false;
1928 ret = true;
1929 break;
1930 case TX_TSPEC_ACTION_DOWNGRADE:
1931 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1932 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1933 ret = true;
1934 break;
1935 }
1936 /* downgrade next lower non-ACM AC */
1937 for (non_acm_ac = ac + 1;
1938 non_acm_ac < IEEE80211_NUM_ACS;
1939 non_acm_ac++)
1940 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1941 break;
Johannes Berg93db1d92016-09-14 09:23:51 +02001942 /* Usually the loop will result in using BK even if it
1943 * requires admission control, but such a configuration
1944 * makes no sense and we have to transmit somehow - the
1945 * AC selection does the same thing.
1946 * If we started out trying to downgrade from BK, then
1947 * the extra condition here might be needed.
Johannes Berg02219b32014-10-07 10:38:50 +03001948 */
Johannes Berg93db1d92016-09-14 09:23:51 +02001949 if (non_acm_ac >= IEEE80211_NUM_ACS)
1950 non_acm_ac = IEEE80211_AC_BK;
Johannes Berg02219b32014-10-07 10:38:50 +03001951 if (drv_conf_tx(local, sdata, ac,
1952 &sdata->tx_conf[non_acm_ac]))
1953 sdata_err(sdata,
1954 "failed to set TX queue parameters for queue %d\n",
1955 ac);
1956 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1957 ret = true;
1958 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1959 tx_tspec->time_slice_start + HZ - now + 1);
1960 break;
1961 case TX_TSPEC_ACTION_NONE:
1962 /* nothing now */
1963 break;
1964 }
1965 }
1966
1967 return ret;
1968}
1969
1970void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1971{
1972 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1973 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1974}
1975
1976static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1977{
1978 struct ieee80211_sub_if_data *sdata;
1979
1980 sdata = container_of(work, struct ieee80211_sub_if_data,
1981 u.mgd.tx_tspec_wk.work);
1982 ieee80211_sta_handle_tspec_ac_params(sdata);
1983}
1984
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001985/* MLME */
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001986static bool
1987ieee80211_sta_wmm_params(struct ieee80211_local *local,
1988 struct ieee80211_sub_if_data *sdata,
1989 const u8 *wmm_param, size_t wmm_param_len,
1990 const struct ieee80211_mu_edca_param_set *mu_edca)
Jiri Bencf0706e82007-05-05 11:45:53 -07001991{
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001992 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
Johannes Berg4ced3f72010-07-19 16:39:04 +02001993 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001994 size_t left;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001995 int count, mu_edca_count, ac;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001996 const u8 *pos;
1997 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001998
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001999 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02002000 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002001
Johannes Berg32c50572012-03-28 11:04:29 +02002002 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02002003 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02002004
2005 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02002006 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02002007
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02002009 return false;
Kalle Valoab133152010-01-12 10:42:31 +02002010
2011 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02002012 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02002013
Jiri Bencf0706e82007-05-05 11:45:53 -07002014 count = wmm_param[6] & 0x0f;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002015 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2016 * if mu_edca was preset before and now it disappeared tell
2017 * the driver about it.
2018 */
2019 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2020 if (count == ifmgd->wmm_last_param_set &&
2021 mu_edca_count == ifmgd->mu_edca_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02002022 return false;
Johannes Berg46900292009-02-15 12:44:28 +01002023 ifmgd->wmm_last_param_set = count;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002024 ifmgd->mu_edca_last_param_set = mu_edca_count;
Jiri Bencf0706e82007-05-05 11:45:53 -07002025
2026 pos = wmm_param + 8;
2027 left = wmm_param_len - 8;
2028
2029 memset(&params, 0, sizeof(params));
2030
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002031 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002032 for (; left >= 4; left -= 4, pos += 4) {
2033 int aci = (pos[0] >> 5) & 0x03;
2034 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02002035 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002036
2037 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002038 case 1: /* AC_BK */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002039 ac = IEEE80211_AC_BK;
Johannes Berg988c0f72008-04-17 19:21:22 +02002040 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002041 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02002042 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2043 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002044 params[ac].mu_edca = !!mu_edca;
2045 if (mu_edca)
2046 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
Jiri Bencf0706e82007-05-05 11:45:53 -07002047 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002048 case 2: /* AC_VI */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002049 ac = IEEE80211_AC_VI;
Johannes Berg988c0f72008-04-17 19:21:22 +02002050 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002051 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02002052 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2053 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002054 params[ac].mu_edca = !!mu_edca;
2055 if (mu_edca)
2056 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
Jiri Bencf0706e82007-05-05 11:45:53 -07002057 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002058 case 3: /* AC_VO */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002059 ac = IEEE80211_AC_VO;
Johannes Berg988c0f72008-04-17 19:21:22 +02002060 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002061 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02002062 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2063 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002064 params[ac].mu_edca = !!mu_edca;
2065 if (mu_edca)
2066 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
Jiri Bencf0706e82007-05-05 11:45:53 -07002067 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002068 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07002069 default:
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002070 ac = IEEE80211_AC_BE;
Johannes Berg988c0f72008-04-17 19:21:22 +02002071 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002072 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02002073 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2074 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002075 params[ac].mu_edca = !!mu_edca;
2076 if (mu_edca)
2077 params[ac].mu_edca_param_rec = mu_edca->ac_be;
Jiri Bencf0706e82007-05-05 11:45:53 -07002078 break;
2079 }
2080
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002081 params[ac].aifs = pos[0] & 0x0f;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002082
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002083 if (params[ac].aifs < 2) {
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002084 sdata_info(sdata,
2085 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002086 params[ac].aifs, aci);
2087 params[ac].aifs = 2;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002088 }
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002089 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2090 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2091 params[ac].txop = get_unaligned_le16(pos + 2);
2092 params[ac].acm = acm;
2093 params[ac].uapsd = uapsd;
Kalle Valoab133152010-01-12 10:42:31 +02002094
Ilan Peer911a2642018-04-03 11:35:22 +03002095 if (params[ac].cw_min == 0 ||
Emmanuel Grumbachc470bdc2018-03-26 16:21:04 +03002096 params[ac].cw_min > params[ac].cw_max) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002097 sdata_info(sdata,
2098 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2099 params[ac].cw_min, params[ac].cw_max, aci);
2100 return false;
2101 }
Haim Dreyfusse552af02018-03-28 13:24:10 +03002102 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002103 }
2104
Brian Norris05aaa5c2019-07-26 15:47:58 -07002105 /* WMM specification requires all 4 ACIs. */
2106 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2107 if (params[ac].cw_min == 0) {
2108 sdata_info(sdata,
2109 "AP has invalid WMM params (missing AC %d), using defaults\n",
2110 ac);
2111 return false;
2112 }
2113 }
2114
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002115 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002116 mlme_dbg(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002117 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2118 ac, params[ac].acm,
2119 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2120 params[ac].txop, params[ac].uapsd,
2121 ifmgd->tx_tspec[ac].downgraded);
2122 sdata->tx_conf[ac] = params[ac];
2123 if (!ifmgd->tx_tspec[ac].downgraded &&
2124 drv_conf_tx(local, sdata, ac, &params[ac]))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002125 sdata_err(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002126 "failed to set TX queue parameters for AC %d\n",
2127 ac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002128 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002129
2130 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02002131 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02002132 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002133}
2134
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002135static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2136{
2137 lockdep_assert_held(&sdata->local->mtx);
2138
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002139 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002140 ieee80211_run_deferred_scan(sdata->local);
2141}
2142
2143static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2144{
2145 mutex_lock(&sdata->local->mtx);
2146 __ieee80211_stop_poll(sdata);
2147 mutex_unlock(&sdata->local->mtx);
2148}
2149
Johannes Berg7a5158e2008-10-08 10:59:33 +02002150static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2151 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02002152{
Johannes Bergbda39332008-10-11 01:51:51 +02002153 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302154 struct ieee80211_supported_band *sband;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002155 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002156 bool use_protection;
2157 bool use_short_preamble;
2158 bool use_short_slot;
2159
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302160 sband = ieee80211_get_sband(sdata);
2161 if (!sband)
2162 return changed;
2163
Johannes Berg7a5158e2008-10-08 10:59:33 +02002164 if (erp_valid) {
2165 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2166 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2167 } else {
2168 use_protection = false;
2169 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2170 }
2171
2172 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302173 if (sband->band == NL80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01002174 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02002175
Johannes Berg471b3ef2007-12-28 14:32:58 +01002176 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01002177 bss_conf->use_cts_prot = use_protection;
2178 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02002179 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02002180
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002181 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002182 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002183 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02002184 }
Daniel Draked9430a32007-07-27 15:43:24 +02002185
Johannes Berg7a5158e2008-10-08 10:59:33 +02002186 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02002187 bss_conf->use_short_slot = use_short_slot;
2188 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04002189 }
2190
2191 return changed;
2192}
2193
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002194static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002195 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02002196 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07002197{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002198 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002199 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002200 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002201
Johannes Bergae5eb022008-10-14 16:58:37 +02002202 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002203 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03002204 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002205
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002206 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07002207 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002208
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002209 sdata->u.mgd.associated = cbss;
2210 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01002211
Johannes Berge8e4f522017-03-08 11:12:10 +01002212 ieee80211_check_rate_mask(sdata);
2213
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002214 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2215
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01002216 if (sdata->vif.p2p ||
2217 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002218 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002219
Johannes Berg9caf0362012-11-29 01:25:20 +01002220 rcu_read_lock();
2221 ies = rcu_dereference(cbss->ies);
2222 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002223 int ret;
2224
2225 ret = cfg80211_get_p2p_attr(
2226 ies->data, ies->len,
2227 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002228 (u8 *) &bss_conf->p2p_noa_attr,
2229 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01002230 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002231 sdata->u.mgd.p2p_noa_index =
2232 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01002233 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01002234 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01002235 }
Johannes Berg9caf0362012-11-29 01:25:20 +01002236 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01002237 }
2238
Johannes Bergb291ba12009-07-10 15:29:03 +02002239 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002240 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002241
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002242 ieee80211_led_assoc(local, 1);
2243
Alexander Bondar989c6502013-05-16 17:34:17 +03002244 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02002245 /*
2246 * If the AP is buggy we may get here with no DTIM period
2247 * known, so assume it's 1 which is the only safe assumption
2248 * in that case, although if the TIM IE is broken powersave
2249 * probably just won't work at all.
2250 */
2251 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03002252 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03002253 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02002254 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03002255 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02002256 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02002257 }
Johannes Berge5b900d2010-07-29 16:08:55 +02002258
Juuso Oikarinen68542962010-06-09 13:43:26 +03002259 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02002260
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002261 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01002262 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03002263 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002264 bss_info_changed |= BSS_CHANGED_CQM;
2265
Juuso Oikarinen68542962010-06-09 13:43:26 +03002266 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002267 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002268 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002269
Johannes Bergae5eb022008-10-14 16:58:37 +02002270 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03002271
Johannes Berg056508d2009-07-30 21:43:55 +02002272 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002273 ieee80211_recalc_ps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02002274 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02002275
Johannes Berg04ecd252012-09-11 14:34:12 +02002276 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03002277 ieee80211_recalc_ps_vif(sdata);
2278
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002279 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002280}
2281
Jouni Malinene69e95d2010-03-29 23:29:31 -07002282static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01002283 u16 stype, u16 reason, bool tx,
2284 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03002285{
Johannes Berg46900292009-02-15 12:44:28 +01002286 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002287 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01002288 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002289
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002290 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002291
Johannes Berg37ad3882012-02-24 13:50:54 +01002292 if (WARN_ON_ONCE(tx && !frame_buf))
2293 return;
2294
Johannes Bergb291ba12009-07-10 15:29:03 +02002295 if (WARN_ON(!ifmgd->associated))
2296 return;
2297
David Spinadel79543d82012-06-12 09:59:45 +03002298 ieee80211_stop_poll(sdata);
2299
Johannes Berg77fdaa12009-07-07 03:45:17 +02002300 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002301 netif_carrier_off(sdata->dev);
2302
Eliad Peller88bc40e2012-07-12 17:35:33 +03002303 /*
2304 * if we want to get out of ps before disassoc (why?) we have
2305 * to do it before sending disassoc, as otherwise the null-packet
2306 * won't be valid.
2307 */
2308 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2309 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2310 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2311 }
2312 local->ps_sdata = NULL;
2313
Eliad Pellerab095872012-07-27 12:33:22 +03002314 /* disable per-vif ps */
2315 ieee80211_recalc_ps_vif(sdata);
2316
Emmanuel Grumbach14f2ae82015-01-22 23:30:19 +02002317 /* make sure ongoing transmission finishes */
2318 synchronize_net();
2319
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002320 /*
2321 * drop any frame before deauth/disassoc, this can be data or
2322 * management frame. Since we are disconnecting, we should not
2323 * insist sending these frames which can take time and delay
2324 * the disconnection and possible the roaming.
2325 */
Eliad Pellerf8239812012-06-27 14:18:22 +03002326 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002327 ieee80211_flush_queues(local, sdata, true);
Eliad Pellerf8239812012-06-27 14:18:22 +03002328
Johannes Berg37ad3882012-02-24 13:50:54 +01002329 /* deauthenticate/disassociate now */
Ilan Peer94ba9272018-02-19 14:48:41 +02002330 if (tx || frame_buf) {
Ilan Peer94ba9272018-02-19 14:48:41 +02002331 /*
2332 * In multi channel scenarios guarantee that the virtual
2333 * interface is granted immediate airtime to transmit the
2334 * deauthentication frame by calling mgd_prepare_tx, if the
2335 * driver requested so.
2336 */
2337 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2338 !ifmgd->have_beacon)
Ilan Peerd4e36e52018-04-20 13:49:25 +03002339 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Ilan Peer94ba9272018-02-19 14:48:41 +02002340
Johannes Berg4b08d1b2019-08-30 14:24:51 +03002341 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2342 ifmgd->bssid, stype, reason,
2343 tx, frame_buf);
Ilan Peer94ba9272018-02-19 14:48:41 +02002344 }
Johannes Berg37ad3882012-02-24 13:50:54 +01002345
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002346 /* flush out frame - make sure the deauth was actually sent */
Johannes Berg37ad3882012-02-24 13:50:54 +01002347 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002348 ieee80211_flush_queues(local, sdata, false);
Johannes Berg37ad3882012-02-24 13:50:54 +01002349
Eliad Peller88a9e312012-06-01 11:14:03 +03002350 /* clear bssid only after building the needed mgmt frames */
Joe Perchesc84a67a2015-03-02 19:54:57 -08002351 eth_zero_addr(ifmgd->bssid);
Eliad Peller88a9e312012-06-01 11:14:03 +03002352
Johannes Berg37ad3882012-02-24 13:50:54 +01002353 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01002354 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01002355
2356 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002357 changed |= ieee80211_reset_erp_info(sdata);
2358
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002359 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02002360 changed |= BSS_CHANGED_ASSOC;
2361 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002362
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002363 ifmgd->p2p_noa_index = -1;
2364 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2365 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002366
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002367 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08002368 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2369 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002370 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2371 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002372
2373 /* reset MU-MIMO ownership and group data */
2374 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2375 sizeof(sdata->vif.bss_conf.mu_group.membership));
2376 memset(sdata->vif.bss_conf.mu_group.position, 0,
2377 sizeof(sdata->vif.bss_conf.mu_group.position));
2378 changed |= BSS_CHANGED_MU_GROUPS;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002379 sdata->vif.mu_mimo_owner = false;
Johannes Berg413ad502009-05-08 21:21:06 +02002380
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002381 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302382
Kalle Valo520eb822008-12-18 23:35:27 +02002383 del_timer_sync(&local->dynamic_ps_timer);
2384 cancel_work_sync(&local->dynamic_ps_enable_work);
2385
Juuso Oikarinen68542962010-06-09 13:43:26 +03002386 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002387 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002388 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002389
Johannes Berg3abead52012-03-02 15:56:59 +01002390 sdata->vif.bss_conf.qos = false;
2391 changed |= BSS_CHANGED_QOS;
2392
Johannes Berg0aaffa92010-05-05 15:28:27 +02002393 /* The BSSID (not really interesting) and HT changed */
2394 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02002395 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02002396
Johannes Berg3abead52012-03-02 15:56:59 +01002397 /* disassociated - set to defaults now */
Johannes Bergcec66282015-10-22 17:46:04 +02002398 ieee80211_set_wmm_default(sdata, false, false);
Johannes Berg3abead52012-03-02 15:56:59 +01002399
Johannes Bergb9dcf712010-08-27 12:35:54 +02002400 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2401 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2402 del_timer_sync(&sdata->u.mgd.timer);
2403 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02002404
Johannes Berg826262c2012-12-10 16:38:14 +02002405 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03002406 sdata->vif.bss_conf.beacon_rate = NULL;
2407
Alexander Bondar989c6502013-05-16 17:34:17 +03002408 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02002409
Johannes Berg028e8da02012-11-26 11:57:41 +01002410 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002411 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01002412 ieee80211_vif_release_channel(sdata);
Michal Kazior59af6922014-04-09 15:10:59 +02002413
2414 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002415 ifmgd->csa_waiting_bcn = false;
Johannes Bergf84eaa12015-03-12 08:53:26 +02002416 ifmgd->csa_ignored_same_chan = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002417 if (sdata->csa_block_tx) {
2418 ieee80211_wake_vif_queues(local, sdata,
2419 IEEE80211_QUEUE_STOP_REASON_CSA);
2420 sdata->csa_block_tx = false;
2421 }
Johannes Berg34a37402013-12-18 09:43:33 +01002422 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002423
Johannes Berg02219b32014-10-07 10:38:50 +03002424 /* existing TX TSPEC sessions no longer exist */
2425 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2426 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2427
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002428 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002429}
Jiri Bencf0706e82007-05-05 11:45:53 -07002430
Kalle Valo3cf335d52009-03-22 21:57:06 +02002431void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2432 struct ieee80211_hdr *hdr)
2433{
2434 /*
2435 * We can postpone the mgd.timer whenever receiving unicast frames
2436 * from AP because we know that the connection is working both ways
2437 * at that time. But multicast frames (and hence also beacons) must
2438 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02002439 * data idle periods for sending the periodic probe request to the
2440 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02002441 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002442 if (is_multicast_ether_addr(hdr->addr1))
2443 return;
2444
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04002445 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02002446}
Jiri Bencf0706e82007-05-05 11:45:53 -07002447
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002448static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2449{
2450 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002451 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002452
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002453 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002454 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2455 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002456
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002457 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002458
2459 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002460 ieee80211_recalc_ps(local);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002461 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002462
Johannes Berg30686bf2015-06-02 21:39:54 +02002463 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002464 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002465
2466 /*
2467 * We've received a probe response, but are not sure whether
2468 * we have or will be receiving any beacons or data, so let's
2469 * schedule the timers again, just in case.
2470 */
2471 ieee80211_sta_reset_beacon_monitor(sdata);
2472
2473 mod_timer(&ifmgd->conn_mon_timer,
2474 round_jiffies_up(jiffies +
2475 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002476out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002477 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002478}
2479
Johannes Berg02219b32014-10-07 10:38:50 +03002480static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2481 struct ieee80211_hdr *hdr,
2482 u16 tx_time)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002483{
Johannes Berg02219b32014-10-07 10:38:50 +03002484 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02002485 u16 tid = ieee80211_get_tid(hdr);
Johannes Berg02219b32014-10-07 10:38:50 +03002486 int ac = ieee80211_ac_from_tid(tid);
2487 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2488 unsigned long now = jiffies;
2489
2490 if (likely(!tx_tspec->admitted_time))
2491 return;
2492
2493 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2494 tx_tspec->consumed_tx_time = 0;
2495 tx_tspec->time_slice_start = now;
2496
2497 if (tx_tspec->downgraded) {
2498 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2499 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2500 }
2501 }
2502
2503 if (tx_tspec->downgraded)
2504 return;
2505
2506 tx_tspec->consumed_tx_time += tx_time;
2507
2508 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2509 tx_tspec->downgraded = true;
2510 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2511 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2512 }
2513}
2514
2515void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2516 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2517{
2518 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2519
Felix Fietkau75706d02010-12-02 21:01:07 +01002520 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002521 return;
2522
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002523 if (ieee80211_is_any_nullfunc(hdr->frame_control) &&
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002524 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002525 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002526 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002527 else
2528 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002529 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002530 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002531 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002532
2533 if (ack)
2534 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002535}
2536
Johannes Berg45ad6832018-05-28 15:47:39 +02002537static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2538 const u8 *src, const u8 *dst,
2539 const u8 *ssid, size_t ssid_len,
2540 struct ieee80211_channel *channel)
2541{
2542 struct sk_buff *skb;
2543
2544 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2545 ssid, ssid_len, NULL, 0,
2546 IEEE80211_PROBE_FLAG_DIRECTED);
2547 if (skb)
2548 ieee80211_tx_skb(sdata, skb);
2549}
2550
Maxim Levitskya43abf22009-07-31 18:54:12 +03002551static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2552{
2553 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2554 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002555 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002556 u8 unicast_limit = max(1, max_probe_tries - 3);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002557 struct sta_info *sta;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002558
2559 /*
2560 * Try sending broadcast probe requests for the last three
2561 * probe requests after the first ones failed since some
2562 * buggy APs only support broadcast probe requests.
2563 */
2564 if (ifmgd->probe_send_count >= unicast_limit)
2565 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002566
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002567 /*
2568 * When the hardware reports an accurate Tx ACK status, it's
2569 * better to send a nullfunc frame instead of a probe request,
2570 * as it will kick us off the AP quickly if we aren't associated
2571 * anymore. The timeout will be reset if the frame is ACKed by
2572 * the AP.
2573 */
Soumik Das992e68b2012-05-20 15:31:13 +05302574 ifmgd->probe_send_count++;
2575
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002576 if (dst) {
2577 mutex_lock(&sdata->local->sta_mtx);
2578 sta = sta_info_get(sdata, dst);
2579 if (!WARN_ON(!sta))
2580 ieee80211_check_fast_rx(sta);
2581 mutex_unlock(&sdata->local->sta_mtx);
2582 }
2583
Johannes Berg30686bf2015-06-02 21:39:54 +02002584 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002585 ifmgd->nullfunc_failed = false;
Shay Barf39b07f2019-07-03 16:18:48 +03002586 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2587 ifmgd->probe_send_count--;
2588 else
2589 ieee80211_send_nullfunc(sdata->local, sdata, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002590 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002591 int ssid_len;
2592
Johannes Berg9caf0362012-11-29 01:25:20 +01002593 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002594 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002595 if (WARN_ON_ONCE(ssid == NULL))
2596 ssid_len = 0;
2597 else
2598 ssid_len = ssid[1];
2599
Johannes Berg45ad6832018-05-28 15:47:39 +02002600 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2601 ssid + 2, ssid_len,
2602 ifmgd->associated->channel);
Johannes Berg9caf0362012-11-29 01:25:20 +01002603 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002604 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002605
Ben Greear180205b2011-02-04 15:30:24 -08002606 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002607 run_again(sdata, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002608}
2609
Johannes Bergb291ba12009-07-10 15:29:03 +02002610static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2611 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002612{
Kalle Valo04de8382009-03-22 21:57:35 +02002613 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002614 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002615
Johannes Berg9607e6b662009-12-23 13:15:31 +01002616 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002617 return;
2618
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002619 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002620
2621 if (!ifmgd->associated)
2622 goto out;
2623
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002624 mutex_lock(&sdata->local->mtx);
2625
2626 if (sdata->local->tmp_channel || sdata->local->scanning) {
2627 mutex_unlock(&sdata->local->mtx);
2628 goto out;
2629 }
2630
Ben Greeara13fbe52013-03-25 11:19:35 -07002631 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002632 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002633 "detected beacon loss from AP (missed %d beacons) - probing\n",
2634 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002635
Johannes Berg98f03342014-11-26 12:42:02 +01002636 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
Ben Greeara13fbe52013-03-25 11:19:35 -07002637 }
Kalle Valo04de8382009-03-22 21:57:35 +02002638
Johannes Bergb291ba12009-07-10 15:29:03 +02002639 /*
2640 * The driver/our work has already reported this event or the
2641 * connection monitoring has kicked in and we have already sent
2642 * a probe request. Or maybe the AP died and the driver keeps
2643 * reporting until we disassociate...
2644 *
2645 * In either case we have to ignore the current call to this
2646 * function (except for setting the correct probe reason bit)
2647 * because otherwise we would reset the timer every time and
2648 * never check whether we received a probe response!
2649 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002650 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02002651 already = true;
2652
Eliad Peller12b5f342013-11-18 19:06:46 +02002653 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2654
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002655 mutex_unlock(&sdata->local->mtx);
2656
Johannes Bergb291ba12009-07-10 15:29:03 +02002657 if (already)
2658 goto out;
2659
Johannes Berg4e751842009-06-10 15:16:52 +02002660 mutex_lock(&sdata->local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002661 ieee80211_recalc_ps(sdata->local);
Johannes Berg4e751842009-06-10 15:16:52 +02002662 mutex_unlock(&sdata->local->iflist_mtx);
2663
Maxim Levitskya43abf22009-07-31 18:54:12 +03002664 ifmgd->probe_send_count = 0;
2665 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002666 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002667 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002668}
2669
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002670struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2671 struct ieee80211_vif *vif)
2672{
2673 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2674 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002675 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002676 struct sk_buff *skb;
2677 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002678 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002679
2680 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2681 return NULL;
2682
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002683 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002684
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002685 if (ifmgd->associated)
2686 cbss = ifmgd->associated;
2687 else if (ifmgd->auth_data)
2688 cbss = ifmgd->auth_data->bss;
2689 else if (ifmgd->assoc_data)
2690 cbss = ifmgd->assoc_data->bss;
2691 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002692 return NULL;
2693
Johannes Berg9caf0362012-11-29 01:25:20 +01002694 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002695 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Will Deacon41525612019-10-04 10:51:31 +01002696 if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2697 "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002698 ssid_len = 0;
2699 else
2700 ssid_len = ssid[1];
2701
Johannes Berga344d672014-06-12 22:24:31 +02002702 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002703 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002704 ssid + 2, ssid_len,
Johannes Berg00387f32018-05-28 15:47:38 +02002705 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
Johannes Berg9caf0362012-11-29 01:25:20 +01002706 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002707
2708 return skb;
2709}
2710EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2711
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002712static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2713 const u8 *buf, size_t len, bool tx,
2714 u16 reason)
2715{
2716 struct ieee80211_event event = {
2717 .type = MLME_EVENT,
2718 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2719 .u.mlme.reason = reason,
2720 };
2721
2722 if (tx)
2723 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2724 else
2725 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2726
2727 drv_event_callback(sdata->local, sdata, &event);
2728}
2729
Johannes Bergeef9e542013-01-29 13:09:34 +01002730static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002731{
Michal Kazior59af6922014-04-09 15:10:59 +02002732 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002733 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002734 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002735 bool tx;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002736
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002737 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002738 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002739 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002740 return;
2741 }
2742
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002743 tx = !sdata->csa_block_tx;
2744
David Spinadel20eb7ea2016-05-03 16:05:02 +03002745 /* AP is probably out of range (or not reachable for another reason) so
2746 * remove the bss struct for that AP.
2747 */
2748 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2749
Johannes Berg37ad3882012-02-24 13:50:54 +01002750 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2751 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002752 tx, frame_buf);
Michal Kazior59af6922014-04-09 15:10:59 +02002753 mutex_lock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03002754 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002755 ifmgd->csa_waiting_bcn = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002756 if (sdata->csa_block_tx) {
2757 ieee80211_wake_vif_queues(local, sdata,
2758 IEEE80211_QUEUE_STOP_REASON_CSA);
2759 sdata->csa_block_tx = false;
2760 }
Michal Kazior59af6922014-04-09 15:10:59 +02002761 mutex_unlock(&local->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002762
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002763 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002764 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2765
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002766 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002767}
2768
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002769static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002770{
2771 struct ieee80211_sub_if_data *sdata =
2772 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002773 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002774 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Paul Stewarta85e1d52011-12-09 11:01:49 -08002775
Johannes Berg976bd9e2015-10-16 17:18:11 +02002776 if (ifmgd->associated)
2777 ifmgd->beacon_loss_count++;
Johannes Bergb291ba12009-07-10 15:29:03 +02002778
Johannes Berg682bd382013-01-29 13:13:50 +01002779 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002780 sdata_info(sdata, "Connection to AP %pM lost\n",
2781 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002782 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002783 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002784 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002785 }
2786}
2787
2788static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2789{
2790 struct ieee80211_sub_if_data *sdata =
2791 container_of(work, struct ieee80211_sub_if_data,
2792 u.mgd.csa_connection_drop_work);
2793
Johannes Bergeef9e542013-01-29 13:09:34 +01002794 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002795}
2796
Kalle Valo04de8382009-03-22 21:57:35 +02002797void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2798{
2799 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002800 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002801
Johannes Bergb5878a22010-04-07 16:48:40 +02002802 trace_api_beacon_loss(sdata);
2803
Johannes Berg682bd382013-01-29 13:13:50 +01002804 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002805 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002806}
2807EXPORT_SYMBOL(ieee80211_beacon_loss);
2808
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002809void ieee80211_connection_loss(struct ieee80211_vif *vif)
2810{
2811 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2812 struct ieee80211_hw *hw = &sdata->local->hw;
2813
Johannes Bergb5878a22010-04-07 16:48:40 +02002814 trace_api_connection_loss(sdata);
2815
Johannes Berg682bd382013-01-29 13:13:50 +01002816 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002817 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2818}
2819EXPORT_SYMBOL(ieee80211_connection_loss);
2820
2821
Johannes Berg66e67e42012-01-20 13:55:27 +01002822static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2823 bool assoc)
2824{
2825 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2826
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002827 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002828
Johannes Berg66e67e42012-01-20 13:55:27 +01002829 if (!assoc) {
Emmanuel Grumbachc1e140b2015-01-19 16:53:06 +02002830 /*
2831 * we are not authenticated yet, the only timer that could be
2832 * running is the timeout for the authentication response which
2833 * which is not relevant anymore.
2834 */
2835 del_timer_sync(&sdata->u.mgd.timer);
Johannes Berg66e67e42012-01-20 13:55:27 +01002836 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2837
Joe Perchesc84a67a2015-03-02 19:54:57 -08002838 eth_zero_addr(sdata->u.mgd.bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002839 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002840 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002841 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002842 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002843 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002844 }
2845
Johannes Berg5b112d32013-02-01 01:49:58 +01002846 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002847 kfree(auth_data);
2848 sdata->u.mgd.auth_data = NULL;
2849}
2850
Johannes Bergc9c99f82015-06-01 14:10:09 +02002851static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
Johannes Berge6f462d2016-12-08 17:22:09 +01002852 bool assoc, bool abandon)
Johannes Bergc9c99f82015-06-01 14:10:09 +02002853{
2854 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2855
2856 sdata_assert_lock(sdata);
2857
2858 if (!assoc) {
2859 /*
2860 * we are not associated yet, the only timer that could be
2861 * running is the timeout for the association response which
2862 * which is not relevant anymore.
2863 */
2864 del_timer_sync(&sdata->u.mgd.timer);
2865 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2866
2867 eth_zero_addr(sdata->u.mgd.bssid);
2868 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2869 sdata->u.mgd.flags = 0;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002870 sdata->vif.mu_mimo_owner = false;
2871
Johannes Bergc9c99f82015-06-01 14:10:09 +02002872 mutex_lock(&sdata->local->mtx);
2873 ieee80211_vif_release_channel(sdata);
2874 mutex_unlock(&sdata->local->mtx);
Johannes Berge6f462d2016-12-08 17:22:09 +01002875
2876 if (abandon)
2877 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
Johannes Bergc9c99f82015-06-01 14:10:09 +02002878 }
2879
2880 kfree(assoc_data);
2881 sdata->u.mgd.assoc_data = NULL;
2882}
2883
Johannes Berg66e67e42012-01-20 13:55:27 +01002884static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2885 struct ieee80211_mgmt *mgmt, size_t len)
2886{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002887 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002888 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2889 u8 *pos;
2890 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002891 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002892
2893 pos = mgmt->u.auth.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02002894 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2895 mgmt->bssid, auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002896 if (!elems.challenge)
2897 return;
2898 auth_data->expected_transaction = 4;
Ilan Peerd4e36e52018-04-20 13:49:25 +03002899 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg30686bf2015-06-02 21:39:54 +02002900 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01002901 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2902 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002903 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002904 elems.challenge - 2, elems.challenge_len + 2,
2905 auth_data->bss->bssid, auth_data->bss->bssid,
2906 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002907 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002908}
2909
Jouni Malinenfc107a92018-10-11 00:21:19 +03002910static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2911 const u8 *bssid)
2912{
Jouni Malinenefb543e2018-10-11 00:21:21 +03002913 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002914 struct sta_info *sta;
Wei Yongjun33483a62018-10-16 02:35:30 +00002915 bool result = true;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002916
Jouni Malinenefb543e2018-10-11 00:21:21 +03002917 sdata_info(sdata, "authenticated\n");
2918 ifmgd->auth_data->done = true;
2919 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2920 ifmgd->auth_data->timeout_started = true;
2921 run_again(sdata, ifmgd->auth_data->timeout);
2922
Jouni Malinenfc107a92018-10-11 00:21:19 +03002923 /* move station state to auth */
2924 mutex_lock(&sdata->local->sta_mtx);
2925 sta = sta_info_get(sdata, bssid);
2926 if (!sta) {
2927 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002928 result = false;
2929 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002930 }
2931 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2932 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002933 result = false;
2934 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002935 }
Jouni Malinenfc107a92018-10-11 00:21:19 +03002936
Wei Yongjun33483a62018-10-16 02:35:30 +00002937out:
2938 mutex_unlock(&sdata->local->sta_mtx);
2939 return result;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002940}
2941
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002942static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2943 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002944{
2945 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2946 u8 bssid[ETH_ALEN];
2947 u16 auth_alg, auth_transaction, status_code;
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002948 struct ieee80211_event event = {
2949 .type = MLME_EVENT,
2950 .u.mlme.data = AUTH_EVENT,
2951 };
Johannes Berg66e67e42012-01-20 13:55:27 +01002952
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002953 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002954
2955 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002956 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002957
2958 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002959 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002960
2961 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2962
Joe Perchesb203ca32012-05-08 18:56:52 +00002963 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002964 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002965
2966 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2967 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2968 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2969
2970 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinenefb543e2018-10-11 00:21:21 +03002971 (auth_alg != WLAN_AUTH_SAE &&
2972 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2973 (auth_alg == WLAN_AUTH_SAE &&
2974 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2975 auth_transaction > 2))) {
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002976 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2977 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2978 auth_transaction,
2979 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002980 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002981 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002982
2983 if (status_code != WLAN_STATUS_SUCCESS) {
Andrei Otcheretianskia4055e72020-03-26 15:09:34 +02002984 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2985
2986 if (auth_alg == WLAN_AUTH_SAE &&
2987 status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED)
2988 return;
2989
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002990 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2991 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002992 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002993 event.u.mlme.status = MLME_DENIED;
2994 event.u.mlme.reason = status_code;
2995 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002996 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002997 }
2998
2999 switch (ifmgd->auth_data->algorithm) {
3000 case WLAN_AUTH_OPEN:
3001 case WLAN_AUTH_LEAP:
3002 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003003 case WLAN_AUTH_SAE:
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03003004 case WLAN_AUTH_FILS_SK:
3005 case WLAN_AUTH_FILS_SK_PFS:
3006 case WLAN_AUTH_FILS_PK:
Johannes Berg66e67e42012-01-20 13:55:27 +01003007 break;
3008 case WLAN_AUTH_SHARED_KEY:
3009 if (ifmgd->auth_data->expected_transaction != 4) {
3010 ieee80211_auth_challenge(sdata, mgmt, len);
3011 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003012 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003013 }
3014 break;
3015 default:
3016 WARN_ONCE(1, "invalid auth alg %d",
3017 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003018 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003019 }
3020
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003021 event.u.mlme.status = MLME_SUCCESS;
3022 drv_event_callback(sdata->local, sdata, &event);
Jouni Malinenefb543e2018-10-11 00:21:21 +03003023 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3024 (auth_transaction == 2 &&
3025 ifmgd->auth_data->expected_transaction == 2)) {
3026 if (!ieee80211_mark_sta_auth(sdata, bssid))
Andrei Otcheretianski0daa63e2020-02-21 10:47:20 +01003027 return; /* ignore frame -- wait for timeout */
Jouni Malinenefb543e2018-10-11 00:21:21 +03003028 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3029 auth_transaction == 2) {
3030 sdata_info(sdata, "SAE peer confirmed\n");
3031 ifmgd->auth_data->peer_confirmed = true;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003032 }
3033
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003034 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003035}
3036
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003037#define case_WLAN(type) \
3038 case WLAN_REASON_##type: return #type
3039
Yu Wang79c92ca2019-05-10 17:04:52 +08003040const char *ieee80211_get_reason_code_string(u16 reason_code)
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003041{
3042 switch (reason_code) {
3043 case_WLAN(UNSPECIFIED);
3044 case_WLAN(PREV_AUTH_NOT_VALID);
3045 case_WLAN(DEAUTH_LEAVING);
3046 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3047 case_WLAN(DISASSOC_AP_BUSY);
3048 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3049 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3050 case_WLAN(DISASSOC_STA_HAS_LEFT);
3051 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3052 case_WLAN(DISASSOC_BAD_POWER);
3053 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3054 case_WLAN(INVALID_IE);
3055 case_WLAN(MIC_FAILURE);
3056 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3057 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3058 case_WLAN(IE_DIFFERENT);
3059 case_WLAN(INVALID_GROUP_CIPHER);
3060 case_WLAN(INVALID_PAIRWISE_CIPHER);
3061 case_WLAN(INVALID_AKMP);
3062 case_WLAN(UNSUPP_RSN_VERSION);
3063 case_WLAN(INVALID_RSN_IE_CAP);
3064 case_WLAN(IEEE8021X_FAILED);
3065 case_WLAN(CIPHER_SUITE_REJECTED);
3066 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3067 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3068 case_WLAN(DISASSOC_LOW_ACK);
3069 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3070 case_WLAN(QSTA_LEAVE_QBSS);
3071 case_WLAN(QSTA_NOT_USE);
3072 case_WLAN(QSTA_REQUIRE_SETUP);
3073 case_WLAN(QSTA_TIMEOUT);
3074 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3075 case_WLAN(MESH_PEER_CANCELED);
3076 case_WLAN(MESH_MAX_PEERS);
3077 case_WLAN(MESH_CONFIG);
3078 case_WLAN(MESH_CLOSE);
3079 case_WLAN(MESH_MAX_RETRIES);
3080 case_WLAN(MESH_CONFIRM_TIMEOUT);
3081 case_WLAN(MESH_INVALID_GTK);
3082 case_WLAN(MESH_INCONSISTENT_PARAM);
3083 case_WLAN(MESH_INVALID_SECURITY);
3084 case_WLAN(MESH_PATH_ERROR);
3085 case_WLAN(MESH_PATH_NOFORWARD);
3086 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3087 case_WLAN(MAC_EXISTS_IN_MBSS);
3088 case_WLAN(MESH_CHAN_REGULATORY);
3089 case_WLAN(MESH_CHAN);
3090 default: return "<unknown>";
3091 }
3092}
Johannes Berg66e67e42012-01-20 13:55:27 +01003093
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003094static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3095 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003096{
Johannes Berg46900292009-02-15 12:44:28 +01003097 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003098 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003099
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003100 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003101
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003102 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003103 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003104
Yu Wang79c92ca2019-05-10 17:04:52 +08003105 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3106 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3107 return;
3108 }
3109
Johannes Bergc9c99f82015-06-01 14:10:09 +02003110 if (ifmgd->associated &&
3111 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3112 const u8 *bssid = ifmgd->associated->bssid;
3113
3114 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3115 bssid, reason_code,
3116 ieee80211_get_reason_code_string(reason_code));
3117
3118 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3119
3120 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3121 reason_code);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003122 return;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003123 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003124
Johannes Bergc9c99f82015-06-01 14:10:09 +02003125 if (ifmgd->assoc_data &&
3126 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3127 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07003128
Johannes Bergc9c99f82015-06-01 14:10:09 +02003129 sdata_info(sdata,
3130 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3131 bssid, reason_code,
3132 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003133
Johannes Berge6f462d2016-12-08 17:22:09 +01003134 ieee80211_destroy_assoc_data(sdata, false, true);
Jiri Bencf0706e82007-05-05 11:45:53 -07003135
Johannes Bergc9c99f82015-06-01 14:10:09 +02003136 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3137 return;
3138 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003139}
3140
3141
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003142static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3143 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003144{
Johannes Berg46900292009-02-15 12:44:28 +01003145 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003146 u16 reason_code;
3147
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003148 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003149
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003150 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003151 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003152
Johannes Berg66e67e42012-01-20 13:55:27 +01003153 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00003154 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003155 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003156
3157 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3158
Yu Wang79c92ca2019-05-10 17:04:52 +08003159 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3160 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3161 return;
3162 }
3163
Arkadiusz Miskiewicz68506e92017-02-15 14:21:27 +01003164 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3165 mgmt->sa, reason_code,
3166 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003167
Johannes Berg37ad3882012-02-24 13:50:54 +01003168 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3169
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02003170 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003171}
3172
Christian Lamparterc74d0842011-10-15 00:14:49 +02003173static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3174 u8 *supp_rates, unsigned int supp_rates_len,
3175 u32 *rates, u32 *basic_rates,
3176 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003177 int *min_rate, int *min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03003178 int shift)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003179{
3180 int i, j;
3181
3182 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003183 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003184 bool is_basic = !!(supp_rates[i] & 0x80);
3185
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003186 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003187 *have_higher_than_11mbit = true;
3188
3189 /*
Ilan Peer4826e722020-03-26 15:09:36 +02003190 * Skip HT, VHT and HE BSS membership selectors since they're
3191 * not rates.
Christian Lamparterc74d0842011-10-15 00:14:49 +02003192 *
Johannes Berga6289d32017-03-06 22:59:04 +01003193 * Note: Even though the membership selector and the basic
Christian Lamparterc74d0842011-10-15 00:14:49 +02003194 * rate flag share the same bit, they are not exactly
3195 * the same.
3196 */
Johannes Berga6289d32017-03-06 22:59:04 +01003197 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
Ilan Peer4826e722020-03-26 15:09:36 +02003198 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3199 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY))
Christian Lamparterc74d0842011-10-15 00:14:49 +02003200 continue;
3201
3202 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003203 struct ieee80211_rate *br;
3204 int brate;
3205
3206 br = &sband->bitrates[j];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003207
3208 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3209 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02003210 *rates |= BIT(j);
3211 if (is_basic)
3212 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003213 if ((rate * 5) < *min_rate) {
3214 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003215 *min_rate_index = j;
3216 }
3217 break;
3218 }
3219 }
3220 }
3221}
Jiri Bencf0706e82007-05-05 11:45:53 -07003222
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003223static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3224 const struct ieee802_11_elems *elems)
3225{
3226 if (elems->ext_capab_len < 10)
3227 return false;
3228
3229 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3230 return false;
3231
3232 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3233 IEEE80211_HE_MAC_CAP0_TWT_RES;
3234}
3235
John Crispinc9d32452019-05-28 13:49:47 +02003236static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3237 struct sta_info *sta,
3238 struct ieee802_11_elems *elems)
3239{
3240 bool twt = ieee80211_twt_req_supported(sta, elems);
3241
3242 if (sdata->vif.bss_conf.twt_requester != twt) {
3243 sdata->vif.bss_conf.twt_requester = twt;
3244 return BSS_CHANGED_TWT;
3245 }
3246 return 0;
3247}
3248
Johannes Berg66e67e42012-01-20 13:55:27 +01003249static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3250 struct cfg80211_bss *cbss,
Johannes Bergf61d7882019-10-28 12:52:42 +01003251 struct ieee80211_mgmt *mgmt, size_t len,
3252 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07003253{
Johannes Berg46900292009-02-15 12:44:28 +01003254 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01003255 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01003256 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003257 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003258 u16 capab_info, aid;
Johannes Bergbda39332008-10-11 01:51:51 +02003259 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02003260 const struct cfg80211_bss_ies *bss_ies = NULL;
3261 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Berg57fa5e82020-05-28 21:34:36 +02003262 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Johannes Bergae5eb022008-10-14 16:58:37 +02003263 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003264 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02003265 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003266
Johannes Bergaf6b6372009-12-23 13:15:35 +01003267 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07003268
Jiri Bencf0706e82007-05-05 11:45:53 -07003269 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003270 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07003271
Johannes Bergc7c477b2017-12-01 13:50:51 +02003272 /*
3273 * The 5 MSB of the AID field are reserved
3274 * (802.11-2016 9.4.1.8 AID field)
3275 */
3276 aid &= 0x7ff;
Johannes Berg1dd84aa2007-10-10 12:03:41 +02003277
Johannes Berg05cb9102011-10-28 11:59:47 +02003278 ifmgd->broken_ap = false;
3279
3280 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003281 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3282 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02003283 aid = 0;
3284 ifmgd->broken_ap = true;
3285 }
3286
Johannes Bergf61d7882019-10-28 12:52:42 +01003287 if (!elems->supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003288 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01003289 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07003290 }
3291
Johannes Berg1db364c2020-04-17 12:38:04 +02003292 sdata->vif.bss_conf.aid = aid;
Arik Nemtsov9041c1f2014-11-09 18:50:15 +02003293 ifmgd->tdls_chan_switch_prohibited =
Johannes Bergf61d7882019-10-28 12:52:42 +01003294 elems->ext_capab && elems->ext_capab_len >= 5 &&
3295 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
Jiri Bencf0706e82007-05-05 11:45:53 -07003296
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003297 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02003298 * Some APs are erroneously not including some information in their
3299 * (re)association response frames. Try to recover by using the data
3300 * from the beacon or probe response. This seems to afflict mobile
3301 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3302 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3303 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003304 if (!is_6ghz &&
3305 ((assoc_data->wmm && !elems->wmm_param) ||
3306 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3307 (!elems->ht_cap_elem || !elems->ht_operation)) ||
3308 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3309 (!elems->vht_cap_elem || !elems->vht_operation)))) {
Johannes Berg35d865a2013-05-28 10:54:03 +02003310 const struct cfg80211_bss_ies *ies;
3311 struct ieee802_11_elems bss_elems;
3312
3313 rcu_read_lock();
3314 ies = rcu_dereference(cbss->ies);
3315 if (ies)
3316 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3317 GFP_ATOMIC);
3318 rcu_read_unlock();
3319 if (!bss_ies)
3320 return false;
3321
3322 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003323 false, &bss_elems,
3324 mgmt->bssid,
3325 assoc_data->bss->bssid);
Johannes Berg35d865a2013-05-28 10:54:03 +02003326 if (assoc_data->wmm &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003327 !elems->wmm_param && bss_elems.wmm_param) {
3328 elems->wmm_param = bss_elems.wmm_param;
Johannes Berg35d865a2013-05-28 10:54:03 +02003329 sdata_info(sdata,
3330 "AP bug: WMM param missing from AssocResp\n");
3331 }
3332
3333 /*
3334 * Also check if we requested HT/VHT, otherwise the AP doesn't
3335 * have to include the IEs in the (re)association response.
3336 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003337 if (!elems->ht_cap_elem && bss_elems.ht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003338 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003339 elems->ht_cap_elem = bss_elems.ht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003340 sdata_info(sdata,
3341 "AP bug: HT capability missing from AssocResp\n");
3342 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003343 if (!elems->ht_operation && bss_elems.ht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003344 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003345 elems->ht_operation = bss_elems.ht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003346 sdata_info(sdata,
3347 "AP bug: HT operation missing from AssocResp\n");
3348 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003349 if (!elems->vht_cap_elem && bss_elems.vht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003350 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003351 elems->vht_cap_elem = bss_elems.vht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003352 sdata_info(sdata,
3353 "AP bug: VHT capa missing from AssocResp\n");
3354 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003355 if (!elems->vht_operation && bss_elems.vht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003356 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003357 elems->vht_operation = bss_elems.vht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003358 sdata_info(sdata,
3359 "AP bug: VHT operation missing from AssocResp\n");
3360 }
3361 }
3362
3363 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003364 * We previously checked these in the beacon/probe response, so
3365 * they should be present here. This is just a safety net.
3366 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003367 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003368 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003369 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003370 "HT AP is missing WMM params or HT capability/operation\n");
3371 ret = false;
3372 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003373 }
3374
Johannes Berg57fa5e82020-05-28 21:34:36 +02003375 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003376 (!elems->vht_cap_elem || !elems->vht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003377 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003378 "VHT AP is missing VHT capability/operation\n");
3379 ret = false;
3380 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003381 }
3382
Johannes Berg57fa5e82020-05-28 21:34:36 +02003383 if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3384 !elems->he_6ghz_capa) {
3385 sdata_info(sdata,
3386 "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3387 ret = false;
3388 goto out;
3389 }
3390
Guy Eilam2a33bee2011-08-17 15:18:15 +03003391 mutex_lock(&sdata->local->sta_mtx);
3392 /*
3393 * station info was already allocated and inserted before
3394 * the association and should be available to us
3395 */
Johannes Berg7852e362012-01-20 13:55:24 +01003396 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003397 if (WARN_ON(!sta)) {
3398 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003399 ret = false;
3400 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07003401 }
3402
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05303403 sband = ieee80211_get_sband(sdata);
3404 if (!sband) {
3405 mutex_unlock(&sdata->local->sta_mtx);
3406 ret = false;
3407 goto out;
3408 }
Johannes Berg8318d782008-01-24 19:38:38 +01003409
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003410 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003411 (!elems->he_cap || !elems->he_operation)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003412 mutex_unlock(&sdata->local->sta_mtx);
3413 sdata_info(sdata,
3414 "HE AP is missing HE capability/operation\n");
3415 ret = false;
3416 goto out;
3417 }
3418
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003419 /* Set up internal HT/VHT capabilities */
Johannes Bergf61d7882019-10-28 12:52:42 +01003420 if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08003421 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003422 elems->ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02003423
Johannes Bergf61d7882019-10-28 12:52:42 +01003424 if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Mahesh Palivela818255e2012-10-10 11:33:04 +00003425 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003426 elems->vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00003427
Johannes Bergf61d7882019-10-28 12:52:42 +01003428 if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3429 elems->he_cap) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003430 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003431 elems->he_cap,
3432 elems->he_cap_len,
Johannes Berg1bb9a8a2020-05-28 21:34:38 +02003433 elems->he_6ghz_capa,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003434 sta);
3435
3436 bss_conf->he_support = sta->sta.he_cap.has_he;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003437 if (elems->rsnx && elems->rsnx_len &&
3438 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3439 wiphy_ext_feature_isset(local->hw.wiphy,
3440 NL80211_EXT_FEATURE_PROTECTED_TWT))
3441 bss_conf->twt_protected = true;
3442 else
3443 bss_conf->twt_protected = false;
3444
Johannes Bergf61d7882019-10-28 12:52:42 +01003445 changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003446 } else {
3447 bss_conf->he_support = false;
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003448 bss_conf->twt_requester = false;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003449 bss_conf->twt_protected = false;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003450 }
3451
3452 if (bss_conf->he_support) {
John Crispindd56e902019-12-17 15:19:19 +01003453 bss_conf->he_bss_color.color =
Johannes Bergf61d7882019-10-28 12:52:42 +01003454 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003455 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
John Crispindd56e902019-12-17 15:19:19 +01003456 bss_conf->he_bss_color.partial =
3457 le32_get_bits(elems->he_operation->he_oper_params,
3458 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
3459 bss_conf->he_bss_color.disabled =
3460 le32_get_bits(elems->he_operation->he_oper_params,
3461 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3462 changed |= BSS_CHANGED_HE_BSS_COLOR;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003463
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003464 bss_conf->htc_trig_based_pkt_ext =
Johannes Bergf61d7882019-10-28 12:52:42 +01003465 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003466 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003467 bss_conf->frame_time_rts_th =
Johannes Bergf61d7882019-10-28 12:52:42 +01003468 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003469 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003470
3471 bss_conf->multi_sta_back_32bit =
3472 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3473 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3474
3475 bss_conf->ack_enabled =
3476 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3477 IEEE80211_HE_MAC_CAP2_ACK_EN;
3478
Johannes Bergf61d7882019-10-28 12:52:42 +01003479 bss_conf->uora_exists = !!elems->uora_element;
3480 if (elems->uora_element)
3481 bss_conf->uora_ocw_range = elems->uora_element[0];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003482
Johannes Bergf61d7882019-10-28 12:52:42 +01003483 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3484 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003485 /* TODO: OPEN: what happens if BSS color disable is set? */
3486 }
3487
Sara Sharon78ac51f2019-01-16 18:22:56 +02003488 if (cbss->transmitted_bss) {
3489 bss_conf->nontransmitted = true;
3490 ether_addr_copy(bss_conf->transmitter_bssid,
3491 cbss->transmitted_bss->bssid);
3492 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3493 bss_conf->bssid_index = cbss->bssid_index;
3494 }
3495
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003496 /*
3497 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3498 * in their association response, so ignore that data for our own
3499 * configuration. If it changed since the last beacon, we'll get the
3500 * next beacon and update then.
3501 */
Johannes Berg11289582013-02-07 17:36:12 +01003502
Johannes Bergbee7f5862013-02-07 22:24:55 +01003503 /*
3504 * If an operating mode notification IE is present, override the
3505 * NSS calculation (that would be done in rate_control_rate_init())
3506 * and use the # of streams from that element.
3507 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003508 if (elems->opmode_notif &&
3509 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
Johannes Bergbee7f5862013-02-07 22:24:55 +01003510 u8 nss;
3511
Johannes Bergf61d7882019-10-28 12:52:42 +01003512 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
Johannes Bergbee7f5862013-02-07 22:24:55 +01003513 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3514 nss += 1;
3515 sta->sta.rx_nss = nss;
3516 }
3517
Johannes Berg4b7679a2008-09-18 18:14:18 +02003518 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003519
Tamizh chelvam93f04902015-10-07 10:40:04 +05303520 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02003521 set_sta_flag(sta, WLAN_STA_MFP);
Tamizh chelvam93f04902015-10-07 10:40:04 +05303522 sta->sta.mfp = true;
3523 } else {
3524 sta->sta.mfp = false;
3525 }
Jouni Malinen5394af42009-01-08 13:31:59 +02003526
Johannes Bergf61d7882019-10-28 12:52:42 +01003527 sta->sta.wme = elems->wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
Johannes Bergddf4ac52008-10-22 11:41:38 +02003528
Johannes Berg3e4d40f2013-02-07 17:19:08 +01003529 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01003530 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3531 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3532 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003533 sdata_info(sdata,
3534 "failed to move station %pM to desired state\n",
3535 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01003536 WARN_ON(__sta_info_destroy(sta));
3537 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003538 ret = false;
3539 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01003540 }
3541
Johannes Berg7852e362012-01-20 13:55:24 +01003542 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02003543
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003544 /*
3545 * Always handle WMM once after association regardless
3546 * of the first value the AP uses. Setting -1 here has
3547 * that effect because the AP values is an unsigned
3548 * 4-bit value.
3549 */
3550 ifmgd->wmm_last_param_set = -1;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02003551 ifmgd->mu_edca_last_param_set = -1;
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003552
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003553 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Bergcec66282015-10-22 17:46:04 +02003554 ieee80211_set_wmm_default(sdata, false, false);
Johannes Bergf61d7882019-10-28 12:52:42 +01003555 } else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3556 elems->wmm_param_len,
3557 elems->mu_edca_param_set)) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003558 /* still enable QoS since we might have HT/VHT */
3559 ieee80211_set_wmm_default(sdata, false, true);
3560 /* set the disable-WMM flag in this case to disable
3561 * tracking WMM parameter changes in the beacon if
3562 * the parameters weren't actually valid. Doing so
3563 * avoids changing parameters very strangely when
3564 * the AP is going back and forth between valid and
3565 * invalid parameters.
3566 */
3567 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3568 }
Johannes Berg3abead52012-03-02 15:56:59 +01003569 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07003570
Johannes Bergf61d7882019-10-28 12:52:42 +01003571 if (elems->max_idle_period_ie) {
Avraham Sterne38a0172017-04-26 10:58:47 +03003572 bss_conf->max_idle_period =
Johannes Bergf61d7882019-10-28 12:52:42 +01003573 le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
Avraham Sterne38a0172017-04-26 10:58:47 +03003574 bss_conf->protected_keep_alive =
Johannes Bergf61d7882019-10-28 12:52:42 +01003575 !!(elems->max_idle_period_ie->idle_options &
Avraham Sterne38a0172017-04-26 10:58:47 +03003576 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3577 changed |= BSS_CHANGED_KEEP_ALIVE;
3578 } else {
3579 bss_conf->max_idle_period = 0;
3580 bss_conf->protected_keep_alive = false;
3581 }
3582
Johannes Berg1db364c2020-04-17 12:38:04 +02003583 /* set assoc capability (AID was already set earlier),
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003584 * ieee80211_set_associated() will tell the driver */
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003585 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003586 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003587
Kalle Valo15b7b062009-03-22 21:57:14 +02003588 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01003589 * If we're using 4-addr mode, let the AP know that we're
3590 * doing so, so that it can create the STA VLAN on its side
3591 */
3592 if (ifmgd->use_4addr)
3593 ieee80211_send_4addr_nullfunc(local, sdata);
3594
3595 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02003596 * Start timer to probe the connection to the AP now.
3597 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02003598 */
Johannes Bergb291ba12009-07-10 15:29:03 +02003599 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003600 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02003601
Johannes Berg35d865a2013-05-28 10:54:03 +02003602 ret = true;
3603 out:
3604 kfree(bss_ies);
3605 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003606}
3607
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003608static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3609 struct ieee80211_mgmt *mgmt,
3610 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01003611{
3612 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3613 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3614 u16 capab_info, status_code, aid;
3615 struct ieee802_11_elems elems;
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003616 int ac, uapsd_queues = -1;
Johannes Berg66e67e42012-01-20 13:55:27 +01003617 u8 *pos;
3618 bool reassoc;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003619 struct cfg80211_bss *bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003620 struct ieee80211_event event = {
3621 .type = MLME_EVENT,
3622 .u.mlme.data = ASSOC_EVENT,
3623 };
Jiri Bencf0706e82007-05-05 11:45:53 -07003624
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003625 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003626
3627 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003628 return;
Joe Perchesb203ca32012-05-08 18:56:52 +00003629 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003630 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003631
3632 /*
3633 * AssocResp and ReassocResp have identical structure, so process both
3634 * of them in this function.
3635 */
3636
3637 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003638 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003639
Simon Dinkin7a7d3e42017-08-31 13:09:36 +03003640 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
Johannes Berg66e67e42012-01-20 13:55:27 +01003641 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3642 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3643 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3644
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003645 sdata_info(sdata,
3646 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3647 reassoc ? "Rea" : "A", mgmt->sa,
3648 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01003649
Jouni Malinen39404fe2016-10-27 00:42:05 +03003650 if (assoc_data->fils_kek_len &&
3651 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3652 return;
3653
Johannes Berg66e67e42012-01-20 13:55:27 +01003654 pos = mgmt->u.assoc_resp.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02003655 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3656 mgmt->bssid, assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003657
3658 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01003659 elems.timeout_int &&
3660 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003661 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01003662 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01003663 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003664 sdata_info(sdata,
3665 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3666 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01003667 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01003668 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003669 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003670 run_again(sdata, assoc_data->timeout);
3671 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003672 }
3673
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003674 bss = assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01003675
3676 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003677 sdata_info(sdata, "%pM denied association (code=%d)\n",
3678 mgmt->sa, status_code);
Johannes Berge6f462d2016-12-08 17:22:09 +01003679 ieee80211_destroy_assoc_data(sdata, false, false);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003680 event.u.mlme.status = MLME_DENIED;
3681 event.u.mlme.reason = status_code;
3682 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01003683 } else {
Johannes Bergf61d7882019-10-28 12:52:42 +01003684 if (!ieee80211_assoc_success(sdata, bss, mgmt, len, &elems)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003685 /* oops -- internal error -- send timeout for now */
Johannes Berge6f462d2016-12-08 17:22:09 +01003686 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02003687 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003688 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003689 }
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003690 event.u.mlme.status = MLME_SUCCESS;
3691 drv_event_callback(sdata->local, sdata, &event);
John W. Linville635d9992012-07-09 16:34:34 -04003692 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01003693
3694 /*
3695 * destroy assoc_data afterwards, as otherwise an idle
3696 * recalc after assoc_data is NULL but before associated
3697 * is set can cause the interface to go idle
3698 */
Johannes Berge6f462d2016-12-08 17:22:09 +01003699 ieee80211_destroy_assoc_data(sdata, true, false);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003700
3701 /* get uapsd queues configuration */
3702 uapsd_queues = 0;
3703 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3704 if (sdata->tx_conf[ac].uapsd)
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03003705 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
Johannes Berg66e67e42012-01-20 13:55:27 +01003706 }
3707
Jouni Malinen4d9ec732019-02-15 02:14:33 +02003708 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues,
3709 ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003710}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003711
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003712static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003713 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003714 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003715{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003716 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02003717 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01003718 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01003719
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003720 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01003721
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003722 channel = ieee80211_get_channel_khz(local->hw.wiphy,
3723 ieee80211_rx_status_to_khz(rx_status));
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02003724 if (!channel)
Johannes Berg5bda6172008-09-08 11:05:10 +02003725 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003726
Sara Sharon4abb52a2019-01-16 12:14:41 +02003727 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03003728 if (bss) {
Alexander Bondar817cee72013-05-19 14:23:57 +03003729 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
Johannes Bergd2722f82014-03-04 11:43:28 +01003730 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03003731 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003732}
3733
3734
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003735static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01003736 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003737{
Johannes Bergaf6b6372009-12-23 13:15:35 +01003738 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02003739 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003740 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003741 struct ieee80211_channel *channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003742 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03003743
Kalle Valo15b7b062009-03-22 21:57:14 +02003744 ifmgd = &sdata->u.mgd;
3745
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003746 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003747
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003748 /*
3749 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3750 * "If a 6 GHz AP receives a Probe Request frame and responds with
3751 * a Probe Response frame [..], the Address 1 field of the Probe
3752 * Response frame shall be set to the broadcast address [..]"
3753 * So, on 6GHz band we should also accept broadcast responses.
3754 */
3755 channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3756 rx_status->freq);
3757 if (!channel)
3758 return;
3759
3760 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3761 (channel->band != NL80211_BAND_6GHZ ||
3762 !is_broadcast_ether_addr(mgmt->da)))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03003763 return; /* ignore ProbeResp to foreign address */
3764
Ester Kummerae6a44e2008-06-27 18:54:48 +03003765 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3766 if (baselen > len)
3767 return;
3768
Sara Sharon4abb52a2019-01-16 12:14:41 +02003769 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003770
Johannes Berg77fdaa12009-07-07 03:45:17 +02003771 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00003772 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003773 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003774}
3775
Johannes Bergd91f36d2009-04-16 13:17:26 +02003776/*
3777 * This is the canonical list of information elements we care about,
3778 * the filter code also gives us all changes to the Microsoft OUI
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07003779 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3780 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3781 * changes to requested client power.
Johannes Bergd91f36d2009-04-16 13:17:26 +02003782 *
3783 * We implement beacon filtering in software since that means we can
3784 * avoid processing the frame here and in cfg80211, and userspace
3785 * will not be able to tell whether the hardware supports it or not.
3786 *
3787 * XXX: This list needs to be dynamic -- userspace needs to be able to
3788 * add items it requires. It also needs to be able to tell us to
3789 * look out for other vendor IEs.
3790 */
3791static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02003792 (1ULL << WLAN_EID_COUNTRY) |
3793 (1ULL << WLAN_EID_ERP_INFO) |
3794 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3795 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3796 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg70a3fd62015-03-12 08:53:29 +02003797 (1ULL << WLAN_EID_HT_OPERATION) |
3798 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
Johannes Bergd91f36d2009-04-16 13:17:26 +02003799
Tosoni1ad22fb2018-03-14 16:58:34 +00003800static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3801 struct ieee80211_if_managed *ifmgd,
3802 struct ieee80211_bss_conf *bss_conf,
3803 struct ieee80211_local *local,
3804 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003805{
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003806 /* Track average RSSI from the Beacon frames of the current AP */
Tosoni1ad22fb2018-03-14 16:58:34 +00003807
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003808 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3809 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Johannes Berg338c17a2015-08-28 10:52:54 +02003810 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003811 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003812 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003813 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003814 } else {
Jouni Malinen391a2002010-08-27 22:22:00 +03003815 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003816 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003817
Johannes Berg338c17a2015-08-28 10:52:54 +02003818 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3819
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003820 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3821 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003822 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003823 int last_sig = ifmgd->last_ave_beacon_signal;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003824 struct ieee80211_event event = {
3825 .type = RSSI_EVENT,
3826 };
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003827
3828 /*
3829 * if signal crosses either of the boundaries, invoke callback
3830 * with appropriate parameters
3831 */
3832 if (sig > ifmgd->rssi_max_thold &&
3833 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3834 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003835 event.u.rssi.data = RSSI_EVENT_HIGH;
3836 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003837 } else if (sig < ifmgd->rssi_min_thold &&
3838 (last_sig >= ifmgd->rssi_max_thold ||
3839 last_sig == 0)) {
3840 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003841 event.u.rssi.data = RSSI_EVENT_LOW;
3842 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003843 }
3844 }
3845
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003846 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003847 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003848 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003849 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003850 int last_event = ifmgd->last_cqm_event_signal;
3851 int thold = bss_conf->cqm_rssi_thold;
3852 int hyst = bss_conf->cqm_rssi_hyst;
Johannes Berg338c17a2015-08-28 10:52:54 +02003853
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003854 if (sig < thold &&
3855 (last_event == 0 || sig < last_event - hyst)) {
3856 ifmgd->last_cqm_event_signal = sig;
3857 ieee80211_cqm_rssi_notify(
3858 &sdata->vif,
3859 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003860 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003861 } else if (sig > thold &&
3862 (last_event == 0 || sig > last_event + hyst)) {
3863 ifmgd->last_cqm_event_signal = sig;
3864 ieee80211_cqm_rssi_notify(
3865 &sdata->vif,
3866 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003867 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003868 }
3869 }
3870
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003871 if (bss_conf->cqm_rssi_low &&
3872 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3873 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3874 int last_event = ifmgd->last_cqm_event_signal;
3875 int low = bss_conf->cqm_rssi_low;
3876 int high = bss_conf->cqm_rssi_high;
3877
3878 if (sig < low &&
3879 (last_event == 0 || last_event >= low)) {
3880 ifmgd->last_cqm_event_signal = sig;
3881 ieee80211_cqm_rssi_notify(
3882 &sdata->vif,
3883 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3884 sig, GFP_KERNEL);
3885 } else if (sig > high &&
3886 (last_event == 0 || last_event <= high)) {
3887 ifmgd->last_cqm_event_signal = sig;
3888 ieee80211_cqm_rssi_notify(
3889 &sdata->vif,
3890 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3891 sig, GFP_KERNEL);
3892 }
3893 }
Tosoni1ad22fb2018-03-14 16:58:34 +00003894}
3895
Sara Sharon78ac51f2019-01-16 18:22:56 +02003896static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3897 struct cfg80211_bss *bss)
3898{
3899 if (ether_addr_equal(tx_bssid, bss->bssid))
3900 return true;
3901 if (!bss->transmitted_bss)
3902 return false;
3903 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3904}
3905
Tosoni1ad22fb2018-03-14 16:58:34 +00003906static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3907 struct ieee80211_mgmt *mgmt, size_t len,
3908 struct ieee80211_rx_status *rx_status)
3909{
3910 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3911 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3912 size_t baselen;
3913 struct ieee802_11_elems elems;
3914 struct ieee80211_local *local = sdata->local;
3915 struct ieee80211_chanctx_conf *chanctx_conf;
3916 struct ieee80211_channel *chan;
3917 struct sta_info *sta;
3918 u32 changed = 0;
3919 bool erp_valid;
3920 u8 erp_value = 0;
3921 u32 ncrc;
3922 u8 *bssid;
3923 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3924
3925 sdata_assert_lock(sdata);
3926
3927 /* Process beacon from the current BSS */
3928 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3929 if (baselen > len)
3930 return;
3931
3932 rcu_read_lock();
3933 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3934 if (!chanctx_conf) {
3935 rcu_read_unlock();
3936 return;
3937 }
3938
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003939 if (ieee80211_rx_status_to_khz(rx_status) !=
3940 ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00003941 rcu_read_unlock();
3942 return;
3943 }
3944 chan = chanctx_conf->def.chan;
3945 rcu_read_unlock();
3946
3947 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Sara Sharon78ac51f2019-01-16 18:22:56 +02003948 ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->assoc_data->bss)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00003949 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003950 len - baselen, false, &elems,
3951 mgmt->bssid,
3952 ifmgd->assoc_data->bss->bssid);
Tosoni1ad22fb2018-03-14 16:58:34 +00003953
Sara Sharon4abb52a2019-01-16 12:14:41 +02003954 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Sara Sharon78ac51f2019-01-16 18:22:56 +02003955
3956 if (elems.dtim_period)
3957 ifmgd->dtim_period = elems.dtim_period;
Tosoni1ad22fb2018-03-14 16:58:34 +00003958 ifmgd->have_beacon = true;
3959 ifmgd->assoc_data->need_beacon = false;
3960 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3961 sdata->vif.bss_conf.sync_tsf =
3962 le64_to_cpu(mgmt->u.beacon.timestamp);
3963 sdata->vif.bss_conf.sync_device_ts =
3964 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02003965 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Tosoni1ad22fb2018-03-14 16:58:34 +00003966 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02003967
3968 if (elems.mbssid_config_ie)
3969 bss_conf->profile_periodicity =
3970 elems.mbssid_config_ie->profile_periodicity;
3971
3972 if (elems.ext_capab_len >= 11 &&
3973 (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
3974 bss_conf->ema_ap = true;
3975
Tosoni1ad22fb2018-03-14 16:58:34 +00003976 /* continue assoc process */
3977 ifmgd->assoc_data->timeout = jiffies;
3978 ifmgd->assoc_data->timeout_started = true;
3979 run_again(sdata, ifmgd->assoc_data->timeout);
3980 return;
3981 }
3982
3983 if (!ifmgd->associated ||
Sara Sharon78ac51f2019-01-16 18:22:56 +02003984 !ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->associated))
Tosoni1ad22fb2018-03-14 16:58:34 +00003985 return;
3986 bssid = ifmgd->associated->bssid;
3987
3988 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3989 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
3990 local, rx_status);
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003991
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003992 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003993 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01003994 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003995 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03003996 }
3997
Johannes Bergb291ba12009-07-10 15:29:03 +02003998 /*
3999 * Push the beacon loss detection into the future since
4000 * we are processing a beacon from the AP just now.
4001 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04004002 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02004003
Johannes Bergd91f36d2009-04-16 13:17:26 +02004004 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4005 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01004006 len - baselen, false, &elems,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004007 care_about_ies, ncrc,
4008 mgmt->bssid, bssid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02004009
Johannes Berg90d13e82015-09-24 16:13:07 +02004010 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Johannes Berg1db364c2020-04-17 12:38:04 +02004011 ieee80211_check_tim(elems.tim, elems.tim_len, bss_conf->aid)) {
Johannes Berg90d13e82015-09-24 16:13:07 +02004012 if (local->hw.conf.dynamic_ps_timeout > 0) {
4013 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4014 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4015 ieee80211_hw_config(local,
4016 IEEE80211_CONF_CHANGE_PS);
Kalle Valo572e0012009-02-10 17:09:31 +02004017 }
Johannes Berg076cdcb2015-09-24 16:14:55 +02004018 ieee80211_send_nullfunc(local, sdata, false);
Johannes Berg90d13e82015-09-24 16:13:07 +02004019 } else if (!local->pspolling && sdata->u.mgd.powersave) {
4020 local->pspolling = true;
4021
4022 /*
4023 * Here is assumed that the driver will be
4024 * able to send ps-poll frame and receive a
4025 * response even though power save mode is
4026 * enabled, but some drivers might require
4027 * to disable power save here. This needs
4028 * to be investigated.
4029 */
4030 ieee80211_send_pspoll(local, sdata);
Vivek Natarajana97b77b2008-12-23 18:39:02 -08004031 }
4032 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02004033
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01004034 if (sdata->vif.p2p ||
4035 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004036 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01004037 int ret;
4038
4039 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
4040 len - baselen,
4041 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01004042 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004043 if (ret >= 2) {
4044 if (sdata->u.mgd.p2p_noa_index != noa.index) {
4045 /* valid noa_attr and index changed */
4046 sdata->u.mgd.p2p_noa_index = noa.index;
4047 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4048 changed |= BSS_CHANGED_P2P_PS;
4049 /*
4050 * make sure we update all information, the CRC
4051 * mechanism doesn't look at P2P attributes.
4052 */
4053 ifmgd->beacon_crc_valid = false;
4054 }
4055 } else if (sdata->u.mgd.p2p_noa_index != -1) {
4056 /* noa_attr not found and we had valid noa_attr before */
4057 sdata->u.mgd.p2p_noa_index = -1;
4058 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01004059 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01004060 ifmgd->beacon_crc_valid = false;
4061 }
4062 }
4063
Luciano Coelho0c21e632014-10-08 09:48:39 +03004064 if (ifmgd->csa_waiting_bcn)
4065 ieee80211_chswitch_post_beacon(sdata);
4066
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004067 /*
4068 * Update beacon timing and dtim count on every beacon appearance. This
4069 * will allow the driver to use the most updated values. Do it before
4070 * comparing this one with last received beacon.
4071 * IMPORTANT: These parameters would possibly be out of sync by the time
4072 * the driver will use them. The synchronized view is currently
4073 * guaranteed only in certain callbacks.
4074 */
Johannes Berg30686bf2015-06-02 21:39:54 +02004075 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004076 sdata->vif.bss_conf.sync_tsf =
4077 le64_to_cpu(mgmt->u.beacon.timestamp);
4078 sdata->vif.bss_conf.sync_device_ts =
4079 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004080 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004081 }
4082
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004083 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004084 return;
Jouni Malinen30196672009-05-19 17:01:43 +03004085 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004086 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03004087
Sara Sharon4abb52a2019-01-16 12:14:41 +02004088 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Johannes Berg7d257452012-07-06 17:37:43 +02004089
Johannes Bergd70b7612013-08-23 15:48:48 +02004090 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004091 rx_status->device_timestamp,
Johannes Bergd70b7612013-08-23 15:48:48 +02004092 &elems, true);
4093
Johannes Berg095d81c2013-10-15 12:25:07 +02004094 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4095 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004096 elems.wmm_param_len,
4097 elems.mu_edca_param_set))
Johannes Berg7d257452012-07-06 17:37:43 +02004098 changed |= BSS_CHANGED_QOS;
4099
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004100 /*
4101 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01004102 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004103 */
Alexander Bondar989c6502013-05-16 17:34:17 +03004104 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004105 /* a few bogus AP send dtim_period = 0 or no TIM IE */
Sara Sharon78ac51f2019-01-16 18:22:56 +02004106 bss_conf->dtim_period = elems.dtim_period ?: 1;
Johannes Bergef429da2013-02-05 17:48:40 +01004107
Alexander Bondar989c6502013-05-16 17:34:17 +03004108 changed |= BSS_CHANGED_BEACON_INFO;
4109 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03004110
4111 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02004112 ieee80211_recalc_ps(local);
Alexander Bondar482a9c72013-06-03 17:29:33 +03004113 mutex_unlock(&local->iflist_mtx);
4114
Alexander Bondarce857882013-05-06 17:17:04 +03004115 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004116 }
4117
Johannes Berg1946bed2013-03-27 14:31:53 +01004118 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02004119 erp_valid = true;
4120 erp_value = elems.erp_info[0];
4121 } else {
4122 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04004123 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02004124 changed |= ieee80211_handle_bss_capability(sdata,
4125 le16_to_cpu(mgmt->u.beacon.capab_info),
4126 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07004127
Johannes Berg11289582013-02-07 17:36:12 +01004128 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01004129 sta = sta_info_get(sdata, bssid);
4130
John Crispinc9d32452019-05-28 13:49:47 +02004131 changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4132
Johannes Berg2a333a02020-05-28 21:34:35 +02004133 if (ieee80211_config_bw(sdata, sta, elems.ht_cap_elem,
4134 elems.vht_cap_elem, elems.ht_operation,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004135 elems.vht_operation, elems.he_operation,
4136 bssid, &changed)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004137 mutex_unlock(&local->sta_mtx);
Johannes Berg89f774e2016-01-25 15:46:36 +02004138 sdata_info(sdata,
4139 "failed to follow AP %pM bandwidth change, disconnect\n",
4140 bssid);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004141 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4142 WLAN_REASON_DEAUTH_LEAVING,
4143 true, deauth_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004144 ieee80211_report_disconnect(sdata, deauth_buf,
4145 sizeof(deauth_buf), true,
4146 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004147 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004148 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01004149
4150 if (sta && elems.opmode_notif)
4151 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
Eyal Shapiracf1e05c2015-12-08 16:04:36 +02004152 rx_status->band);
Johannes Berg11289582013-02-07 17:36:12 +01004153 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02004154
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07004155 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4156 elems.country_elem,
4157 elems.country_elem_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07004158 elems.pwr_constr_elem,
4159 elems.cisco_dtpc_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08004160
Johannes Berg471b3ef2007-12-28 14:32:58 +01004161 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07004162}
4163
Johannes Berg1fa57d02010-06-10 10:21:32 +02004164void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4165 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07004166{
4167 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07004168 struct ieee80211_mgmt *mgmt;
4169 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004170 struct ieee802_11_elems elems;
4171 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07004172
Jiri Bencf0706e82007-05-05 11:45:53 -07004173 rx_status = (struct ieee80211_rx_status *) skb->cb;
4174 mgmt = (struct ieee80211_mgmt *) skb->data;
4175 fc = le16_to_cpu(mgmt->frame_control);
4176
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004177 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004178
Johannes Berg66e67e42012-01-20 13:55:27 +01004179 switch (fc & IEEE80211_FCTL_STYPE) {
4180 case IEEE80211_STYPE_BEACON:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004181 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01004182 break;
4183 case IEEE80211_STYPE_PROBE_RESP:
4184 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4185 break;
4186 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004187 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004188 break;
4189 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004190 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004191 break;
4192 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004193 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004194 break;
4195 case IEEE80211_STYPE_ASSOC_RESP:
4196 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004197 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004198 break;
4199 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01004200 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004201 ies_len = skb->len -
4202 offsetof(struct ieee80211_mgmt,
4203 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01004204
4205 if (ies_len < 0)
4206 break;
4207
Sara Sharon4abb52a2019-01-16 12:14:41 +02004208 /* CSA IE cannot be overridden, no need for BSSID */
Johannes Berg37799e52013-03-26 14:02:26 +01004209 ieee802_11_parse_elems(
4210 mgmt->u.action.u.chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004211 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg37799e52013-03-26 14:02:26 +01004212
4213 if (elems.parse_error)
4214 break;
4215
Johannes Berg66e67e42012-01-20 13:55:27 +01004216 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004217 rx_status->mactime,
4218 rx_status->device_timestamp,
4219 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004220 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4221 ies_len = skb->len -
4222 offsetof(struct ieee80211_mgmt,
4223 u.action.u.ext_chan_switch.variable);
4224
4225 if (ies_len < 0)
4226 break;
4227
Sara Sharon4abb52a2019-01-16 12:14:41 +02004228 /*
4229 * extended CSA IE can't be overridden, no need for
4230 * BSSID
4231 */
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004232 ieee802_11_parse_elems(
4233 mgmt->u.action.u.ext_chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004234 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004235
4236 if (elems.parse_error)
4237 break;
4238
4239 /* for the handling code pretend this was also an IE */
4240 elems.ext_chansw_ie =
4241 &mgmt->u.action.u.ext_chan_switch.data;
4242
4243 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004244 rx_status->mactime,
4245 rx_status->device_timestamp,
4246 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004247 }
Johannes Berg37799e52013-03-26 14:02:26 +01004248 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004249 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004250 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07004251}
4252
Kees Cook34f11cd2017-10-16 16:35:49 -07004253static void ieee80211_sta_timer(struct timer_list *t)
Jiri Bencf0706e82007-05-05 11:45:53 -07004254{
4255 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004256 from_timer(sdata, t, u.mgd.timer);
Jiri Bencf0706e82007-05-05 11:45:53 -07004257
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01004258 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07004259}
4260
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004261static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01004262 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004263{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004264 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004265
Johannes Berg37ad3882012-02-24 13:50:54 +01004266 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01004267 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01004268
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004269 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4270 reason);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004271}
4272
Johannes Berg46cad4b2015-08-15 22:39:54 +03004273static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
Johannes Berg66e67e42012-01-20 13:55:27 +01004274{
4275 struct ieee80211_local *local = sdata->local;
4276 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4277 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004278 u32 tx_flags = 0;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004279 u16 trans = 1;
4280 u16 status = 0;
Ilan Peerd4e36e52018-04-20 13:49:25 +03004281 u16 prepare_tx_duration = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004282
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004283 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004284
4285 if (WARN_ON_ONCE(!auth_data))
4286 return -EINVAL;
4287
Johannes Berg66e67e42012-01-20 13:55:27 +01004288 auth_data->tries++;
4289
4290 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004291 sdata_info(sdata, "authentication with %pM timed out\n",
4292 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004293
4294 /*
4295 * Most likely AP is not in the range so remove the
4296 * bss struct for that AP.
4297 */
4298 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4299
4300 return -ETIMEDOUT;
4301 }
4302
Ilan Peerd4e36e52018-04-20 13:49:25 +03004303 if (auth_data->algorithm == WLAN_AUTH_SAE)
4304 prepare_tx_duration =
4305 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4306
4307 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
Johannes Berga1845fc2012-06-27 13:18:36 +02004308
Johannes Berg46cad4b2015-08-15 22:39:54 +03004309 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4310 auth_data->bss->bssid, auth_data->tries,
4311 IEEE80211_AUTH_MAX_TRIES);
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004312
Johannes Berg46cad4b2015-08-15 22:39:54 +03004313 auth_data->expected_transaction = 2;
Johannes Berg66e67e42012-01-20 13:55:27 +01004314
Johannes Berg46cad4b2015-08-15 22:39:54 +03004315 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4316 trans = auth_data->sae_trans;
4317 status = auth_data->sae_status;
4318 auth_data->expected_transaction = trans;
Johannes Berg66e67e42012-01-20 13:55:27 +01004319 }
4320
Johannes Berg46cad4b2015-08-15 22:39:54 +03004321 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4322 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4323 IEEE80211_TX_INTFL_MLME_CONN_TX;
4324
4325 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4326 auth_data->data, auth_data->data_len,
4327 auth_data->bss->bssid,
4328 auth_data->bss->bssid, NULL, 0, 0,
4329 tx_flags);
4330
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02004331 if (tx_flags == 0) {
Ilan Peer407879b2018-04-20 13:49:20 +03004332 if (auth_data->algorithm == WLAN_AUTH_SAE)
4333 auth_data->timeout = jiffies +
4334 IEEE80211_AUTH_TIMEOUT_SAE;
4335 else
4336 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004337 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004338 auth_data->timeout =
4339 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004340 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004341
Ilan Peer407879b2018-04-20 13:49:20 +03004342 auth_data->timeout_started = true;
4343 run_again(sdata, auth_data->timeout);
4344
Johannes Berg66e67e42012-01-20 13:55:27 +01004345 return 0;
4346}
4347
4348static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4349{
4350 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4351 struct ieee80211_local *local = sdata->local;
4352
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004353 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004354
Johannes Berg66e67e42012-01-20 13:55:27 +01004355 assoc_data->tries++;
4356 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004357 sdata_info(sdata, "association with %pM timed out\n",
4358 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004359
4360 /*
4361 * Most likely AP is not in the range so remove the
4362 * bss struct for that AP.
4363 */
4364 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4365
4366 return -ETIMEDOUT;
4367 }
4368
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004369 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4370 assoc_data->bss->bssid, assoc_data->tries,
4371 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01004372 ieee80211_send_assoc(sdata);
4373
Johannes Berg30686bf2015-06-02 21:39:54 +02004374 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004375 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004376 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004377 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01004378 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004379 assoc_data->timeout =
4380 round_jiffies_up(jiffies +
4381 IEEE80211_ASSOC_TIMEOUT_LONG);
4382 assoc_data->timeout_started = true;
4383 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004384 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004385
4386 return 0;
4387}
4388
Johannes Berg1672c0e32013-01-29 15:02:27 +01004389void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4390 __le16 fc, bool acked)
4391{
4392 struct ieee80211_local *local = sdata->local;
4393
4394 sdata->u.mgd.status_fc = fc;
4395 sdata->u.mgd.status_acked = acked;
4396 sdata->u.mgd.status_received = true;
4397
4398 ieee80211_queue_work(&local->hw, &sdata->work);
4399}
4400
Johannes Berg1fa57d02010-06-10 10:21:32 +02004401void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004402{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004403 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02004404 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004405
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004406 sdata_lock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004407
Johannes Berg1672c0e32013-01-29 15:02:27 +01004408 if (ifmgd->status_received) {
4409 __le16 fc = ifmgd->status_fc;
4410 bool status_acked = ifmgd->status_acked;
4411
4412 ifmgd->status_received = false;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004413 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004414 if (status_acked) {
Ilan Peer407879b2018-04-20 13:49:20 +03004415 if (ifmgd->auth_data->algorithm ==
4416 WLAN_AUTH_SAE)
4417 ifmgd->auth_data->timeout =
4418 jiffies +
4419 IEEE80211_AUTH_TIMEOUT_SAE;
4420 else
4421 ifmgd->auth_data->timeout =
4422 jiffies +
4423 IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004424 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004425 } else {
4426 ifmgd->auth_data->timeout = jiffies - 1;
4427 }
Johannes Berg89afe612013-02-13 15:39:57 +01004428 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004429 } else if (ifmgd->assoc_data &&
4430 (ieee80211_is_assoc_req(fc) ||
4431 ieee80211_is_reassoc_req(fc))) {
4432 if (status_acked) {
4433 ifmgd->assoc_data->timeout =
4434 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004435 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004436 } else {
4437 ifmgd->assoc_data->timeout = jiffies - 1;
4438 }
Johannes Berg89afe612013-02-13 15:39:57 +01004439 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004440 }
4441 }
4442
Johannes Berg89afe612013-02-13 15:39:57 +01004443 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004444 time_after(jiffies, ifmgd->auth_data->timeout)) {
4445 if (ifmgd->auth_data->done) {
4446 /*
4447 * ok ... we waited for assoc but userspace didn't,
4448 * so let's just kill the auth data
4449 */
4450 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg46cad4b2015-08-15 22:39:54 +03004451 } else if (ieee80211_auth(sdata)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004452 u8 bssid[ETH_ALEN];
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004453 struct ieee80211_event event = {
4454 .type = MLME_EVENT,
4455 .u.mlme.data = AUTH_EVENT,
4456 .u.mlme.status = MLME_TIMEOUT,
4457 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004458
4459 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4460
4461 ieee80211_destroy_auth_data(sdata, false);
4462
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004463 cfg80211_auth_timeout(sdata->dev, bssid);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004464 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004465 }
Johannes Berg89afe612013-02-13 15:39:57 +01004466 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004467 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004468
Johannes Berg89afe612013-02-13 15:39:57 +01004469 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004470 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03004471 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01004472 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02004473 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004474 struct ieee80211_event event = {
4475 .type = MLME_EVENT,
4476 .u.mlme.data = ASSOC_EVENT,
4477 .u.mlme.status = MLME_TIMEOUT,
4478 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004479
Johannes Berge6f462d2016-12-08 17:22:09 +01004480 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004481 cfg80211_assoc_timeout(sdata->dev, bss);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004482 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004483 }
Johannes Berg89afe612013-02-13 15:39:57 +01004484 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004485 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004486
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004487 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02004488 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03004489 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004490 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03004491
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004492 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004493
Johannes Berg30686bf2015-06-02 21:39:54 +02004494 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Ben Greear180205b2011-02-04 15:30:24 -08004495 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004496 else
Ben Greear180205b2011-02-04 15:30:24 -08004497 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004498
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004499 /* ACK received for nullfunc probing frame */
4500 if (!ifmgd->probe_send_count)
4501 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004502 else if (ifmgd->nullfunc_failed) {
4503 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004504 mlme_dbg(sdata,
4505 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4506 bssid, ifmgd->probe_send_count,
4507 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004508 ieee80211_mgd_probe_ap_send(sdata);
4509 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004510 mlme_dbg(sdata,
4511 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4512 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02004513 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004514 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4515 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004516 }
4517 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004518 run_again(sdata, ifmgd->probe_timeout);
Johannes Berg30686bf2015-06-02 21:39:54 +02004519 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004520 mlme_dbg(sdata,
4521 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4522 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02004523 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004524 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004525 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004526 mlme_dbg(sdata,
4527 "No probe response from AP %pM after %dms, try %d/%i\n",
4528 bssid, probe_wait_ms,
4529 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03004530 ieee80211_mgd_probe_ap_send(sdata);
4531 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02004532 /*
4533 * We actually lost the connection ... or did we?
4534 * Let's make sure!
4535 */
Johannes Berg89f774e2016-01-25 15:46:36 +02004536 mlme_dbg(sdata,
4537 "No probe response from AP %pM after %dms, disconnecting.\n",
4538 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004539
Johannes Berg95acac62011-07-12 12:30:59 +02004540 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004541 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02004542 }
4543 }
4544
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004545 sdata_unlock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004546}
Johannes Berg0a51b272008-09-08 17:44:25 +02004547
Kees Cook34f11cd2017-10-16 16:35:49 -07004548static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004549{
4550 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004551 from_timer(sdata, t, u.mgd.bcn_mon_timer);
Luciano Coelho0c21e632014-10-08 09:48:39 +03004552 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004553
Luciano Coelho0c21e632014-10-08 09:48:39 +03004554 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004555 return;
4556
Johannes Berg682bd382013-01-29 13:13:50 +01004557 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004558 ieee80211_queue_work(&sdata->local->hw,
4559 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004560}
4561
Kees Cook34f11cd2017-10-16 16:35:49 -07004562static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004563{
4564 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004565 from_timer(sdata, t, u.mgd.conn_mon_timer);
Johannes Bergb291ba12009-07-10 15:29:03 +02004566 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4567 struct ieee80211_local *local = sdata->local;
4568
Luciano Coelho0c21e632014-10-08 09:48:39 +03004569 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004570 return;
4571
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04004572 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004573}
4574
4575static void ieee80211_sta_monitor_work(struct work_struct *work)
4576{
4577 struct ieee80211_sub_if_data *sdata =
4578 container_of(work, struct ieee80211_sub_if_data,
4579 u.mgd.monitor_work);
4580
4581 ieee80211_mgd_probe_ap(sdata, false);
4582}
4583
Johannes Berga1678f82008-09-11 00:01:47 +02004584static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4585{
Kalle Vaload935682009-04-19 08:47:19 +03004586 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02004587 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03004588
Johannes Bergb291ba12009-07-10 15:29:03 +02004589 /* let's probe the connection once */
Johannes Berg30686bf2015-06-02 21:39:54 +02004590 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Eliad Peller494f1fe2012-02-19 15:26:09 +02004591 ieee80211_queue_work(&sdata->local->hw,
4592 &sdata->u.mgd.monitor_work);
Kalle Vaload935682009-04-19 08:47:19 +03004593 }
Johannes Berga1678f82008-09-11 00:01:47 +02004594}
4595
Johannes Bergb8360ab2013-04-29 14:57:44 +02004596#ifdef CONFIG_PM
Johannes Berg1a1cb742014-03-19 09:55:55 +01004597void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4598{
4599 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4600 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4601
4602 sdata_lock(sdata);
4603
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004604 if (ifmgd->auth_data || ifmgd->assoc_data) {
4605 const u8 *bssid = ifmgd->auth_data ?
4606 ifmgd->auth_data->bss->bssid :
4607 ifmgd->assoc_data->bss->bssid;
4608
Johannes Berg1a1cb742014-03-19 09:55:55 +01004609 /*
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004610 * If we are trying to authenticate / associate while suspending,
4611 * cfg80211 won't know and won't actually abort those attempts,
4612 * thus we need to do that ourselves.
Johannes Berg1a1cb742014-03-19 09:55:55 +01004613 */
Johannes Berg4b08d1b2019-08-30 14:24:51 +03004614 ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
Johannes Berg1a1cb742014-03-19 09:55:55 +01004615 IEEE80211_STYPE_DEAUTH,
4616 WLAN_REASON_DEAUTH_LEAVING,
4617 false, frame_buf);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004618 if (ifmgd->assoc_data)
Johannes Berge6f462d2016-12-08 17:22:09 +01004619 ieee80211_destroy_assoc_data(sdata, false, true);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004620 if (ifmgd->auth_data)
4621 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg1a1cb742014-03-19 09:55:55 +01004622 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4623 IEEE80211_DEAUTH_FRAME_LEN);
4624 }
4625
Johannes Bergbe72afe2015-03-01 09:10:03 +02004626 /* This is a bit of a hack - we should find a better and more generic
4627 * solution to this. Normally when suspending, cfg80211 will in fact
4628 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4629 * auth (not so important) or assoc (this is the problem) process.
4630 *
4631 * As a consequence, it can happen that we are in the process of both
4632 * associating and suspending, and receive an association response
4633 * after cfg80211 has checked if it needs to disconnect, but before
4634 * we actually set the flag to drop incoming frames. This will then
4635 * cause the workqueue flush to process the association response in
4636 * the suspend, resulting in a successful association just before it
4637 * tries to remove the interface from the driver, which now though
4638 * has a channel context assigned ... this results in issues.
4639 *
4640 * To work around this (for now) simply deauth here again if we're
4641 * now connected.
4642 */
4643 if (ifmgd->associated && !sdata->local->wowlan) {
4644 u8 bssid[ETH_ALEN];
4645 struct cfg80211_deauth_request req = {
4646 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4647 .bssid = bssid,
4648 };
4649
4650 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4651 ieee80211_mgd_deauth(sdata, &req);
4652 }
4653
Johannes Berg1a1cb742014-03-19 09:55:55 +01004654 sdata_unlock(sdata);
4655}
4656
Johannes Bergb8360ab2013-04-29 14:57:44 +02004657void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4658{
4659 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4660
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004661 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004662 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004663 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004664 return;
4665 }
4666
4667 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4668 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4669 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4670 ieee80211_sta_connection_lost(sdata,
4671 ifmgd->associated->bssid,
4672 WLAN_REASON_UNSPECIFIED,
4673 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004674 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004675 return;
4676 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004677 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004678}
4679#endif
4680
Johannes Berg9c6bd792008-09-11 00:01:52 +02004681/* interface setup */
4682void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4683{
Johannes Berg46900292009-02-15 12:44:28 +01004684 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004685
Johannes Berg46900292009-02-15 12:44:28 +01004686 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004687 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01004688 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004689 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4690 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02004691 INIT_WORK(&ifmgd->csa_connection_drop_work,
4692 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03004693 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03004694 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4695 ieee80211_tdls_peer_del_work);
Kees Cook34f11cd2017-10-16 16:35:49 -07004696 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4697 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4698 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4699 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
Johannes Berg02219b32014-10-07 10:38:50 +03004700 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4701 ieee80211_sta_handle_tspec_ac_params_wk);
Johannes Berg9c6bd792008-09-11 00:01:52 +02004702
Johannes Bergab1faea2009-07-01 21:41:17 +02004703 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05304704 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02004705 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4706 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004707 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02004708
Eliad Peller0d8614b2014-09-10 14:07:36 +03004709 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
Johannes Berg0f782312009-12-01 13:37:02 +01004710 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4711 else
4712 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02004713
4714 /* Setup TDLS data */
4715 spin_lock_init(&ifmgd->teardown_lock);
4716 ifmgd->teardown_skb = NULL;
4717 ifmgd->orig_teardown_skb = NULL;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004718}
4719
4720/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02004721void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4722{
Johannes Berg31ee67a2012-07-06 21:18:24 +02004723 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02004724
4725 /* Restart STA timers */
4726 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07004727 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4728 if (ieee80211_sdata_running(sdata))
4729 ieee80211_restart_sta_timer(sdata);
4730 }
Johannes Berga1678f82008-09-11 00:01:47 +02004731 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02004732}
Johannes Berg10f644a2009-04-16 13:17:25 +02004733
Johannes Bergf2d9d272012-11-22 14:11:39 +01004734static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4735 struct cfg80211_bss *cbss)
4736{
4737 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4738 const u8 *ht_cap_ie, *vht_cap_ie;
4739 const struct ieee80211_ht_cap *ht_cap;
4740 const struct ieee80211_vht_cap *vht_cap;
4741 u8 chains = 1;
4742
4743 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4744 return chains;
4745
Johannes Berg9caf0362012-11-29 01:25:20 +01004746 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004747 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4748 ht_cap = (void *)(ht_cap_ie + 2);
4749 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4750 /*
4751 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4752 * "Tx Unequal Modulation Supported" fields.
4753 */
4754 }
4755
4756 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4757 return chains;
4758
Johannes Berg9caf0362012-11-29 01:25:20 +01004759 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004760 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4761 u8 nss;
4762 u16 tx_mcs_map;
4763
4764 vht_cap = (void *)(vht_cap_ie + 2);
4765 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4766 for (nss = 8; nss > 0; nss--) {
4767 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4768 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4769 break;
4770 }
4771 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4772 chains = max(chains, nss);
4773 }
4774
4775 return chains;
4776}
4777
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004778static bool
4779ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4780 const struct ieee80211_he_operation *he_op)
4781{
4782 const struct ieee80211_sta_he_cap *sta_he_cap =
4783 ieee80211_get_he_sta_cap(sband);
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004784 u16 ap_min_req_set;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004785 int i;
4786
4787 if (!sta_he_cap || !he_op)
4788 return false;
4789
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004790 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4791
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004792 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4793 for (i = 0; i < 3; i++) {
4794 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4795 &sta_he_cap->he_mcs_nss_supp;
4796 u16 sta_mcs_map_rx =
4797 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4798 u16 sta_mcs_map_tx =
4799 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4800 u8 nss;
4801 bool verified = true;
4802
4803 /*
4804 * For each band there is a maximum of 8 spatial streams
4805 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4806 * of 2 bits per NSS (1-8), with the values defined in enum
4807 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4808 * capabilities aren't less than the AP's minimum requirements
4809 * for this HE BSS per SS.
4810 * It is enough to find one such band that meets the reqs.
4811 */
4812 for (nss = 8; nss > 0; nss--) {
4813 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4814 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4815 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4816
4817 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4818 continue;
4819
4820 /*
4821 * Make sure the HE AP doesn't require MCSs that aren't
4822 * supported by the client
4823 */
4824 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4825 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4826 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4827 verified = false;
4828 break;
4829 }
4830 }
4831
4832 if (verified)
4833 return true;
4834 }
4835
4836 /* If here, STA doesn't meet AP's HE min requirements */
4837 return false;
4838}
4839
Johannes Bergb17166a2012-07-27 11:41:27 +02004840static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4841 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01004842{
4843 struct ieee80211_local *local = sdata->local;
4844 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Peller53b954e2014-07-24 11:20:05 +03004845 const struct ieee80211_ht_cap *ht_cap = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004846 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004847 const struct ieee80211_vht_operation *vht_oper = NULL;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004848 const struct ieee80211_he_operation *he_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004849 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01004850 struct cfg80211_chan_def chandef;
Johannes Berg57fa5e82020-05-28 21:34:36 +02004851 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Johannes Berg2a333a02020-05-28 21:34:35 +02004852 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004853 int ret;
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004854 u32 i;
4855 bool have_80mhz;
Johannes Berga1cf7752012-03-08 15:02:07 +01004856
Johannes Berg24398e32012-03-28 10:58:36 +02004857 sband = local->hw.wiphy->bands[cbss->channel->band];
4858
Johannes Bergf2d9d272012-11-22 14:11:39 +01004859 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4860 IEEE80211_STA_DISABLE_80P80MHZ |
4861 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02004862
Johannes Berg75e296e2020-01-31 13:12:39 +02004863 /* disable HT/VHT/HE if we don't support them */
Johannes Berg57fa5e82020-05-28 21:34:36 +02004864 if (!sband->ht_cap.ht_supported && !is_6ghz) {
Johannes Berg75e296e2020-01-31 13:12:39 +02004865 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4866 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4867 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4868 }
4869
Johannes Berg57fa5e82020-05-28 21:34:36 +02004870 if (!sband->vht_cap.vht_supported && !is_6ghz) {
Johannes Berg75e296e2020-01-31 13:12:39 +02004871 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg57fa5e82020-05-28 21:34:36 +02004872 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4873 }
Johannes Berg75e296e2020-01-31 13:12:39 +02004874
4875 if (!ieee80211_get_he_sta_cap(sband))
4876 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4877
Johannes Berg9caf0362012-11-29 01:25:20 +01004878 rcu_read_lock();
4879
Johannes Berg57fa5e82020-05-28 21:34:36 +02004880 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
Eliad Peller53b954e2014-07-24 11:20:05 +03004881 const u8 *ht_oper_ie, *ht_cap_ie;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004882
Johannes Berg9caf0362012-11-29 01:25:20 +01004883 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02004884 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4885 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6effa2013-02-07 23:33:32 +01004886
Eliad Peller53b954e2014-07-24 11:20:05 +03004887 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4888 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4889 ht_cap = (void *)(ht_cap_ie + 2);
4890
4891 if (!ht_cap) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004892 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4893 ht_oper = NULL;
4894 }
Johannes Berg24398e32012-03-28 10:58:36 +02004895 }
4896
Johannes Berg57fa5e82020-05-28 21:34:36 +02004897 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004898 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004899
Johannes Berg9caf0362012-11-29 01:25:20 +01004900 vht_oper_ie = ieee80211_bss_get_ie(cbss,
4901 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004902 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
4903 vht_oper = (void *)(vht_oper_ie + 2);
4904 if (vht_oper && !ht_oper) {
4905 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004906 sdata_info(sdata,
Johannes Berg75e296e2020-01-31 13:12:39 +02004907 "AP advertised VHT without HT, disabling HT/VHT/HE\n");
Johannes Bergcb145022013-02-07 20:41:50 +01004908 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4909 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02004910 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg24398e32012-03-28 10:58:36 +02004911 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01004912
4913 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4914 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
4915 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4916 vht_oper = NULL;
4917 }
Johannes Berg24398e32012-03-28 10:58:36 +02004918 }
4919
Shaul Triebitz002245e2018-12-15 11:03:19 +02004920 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004921 const struct cfg80211_bss_ies *ies;
4922 const u8 *he_oper_ie;
4923
4924 ies = rcu_dereference(cbss->ies);
4925 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
4926 ies->data, ies->len);
4927 if (he_oper_ie &&
4928 he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
4929 he_oper = (void *)(he_oper_ie + 3);
4930 else
4931 he_oper = NULL;
4932
Johannes Bergf0c04072018-06-29 09:51:39 +02004933 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004934 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4935 }
4936
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004937 /* Allow VHT if at least one channel on the sband supports 80 MHz */
4938 have_80mhz = false;
4939 for (i = 0; i < sband->n_channels; i++) {
4940 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4941 IEEE80211_CHAN_NO_80MHZ))
4942 continue;
4943
4944 have_80mhz = true;
4945 break;
4946 }
4947
4948 if (!have_80mhz)
4949 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4950
Johannes Bergf2d9d272012-11-22 14:11:39 +01004951 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
4952 cbss->channel,
Johannes Berg2a333a02020-05-28 21:34:35 +02004953 bss->vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004954 ht_oper, vht_oper, he_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02004955 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02004956
Johannes Bergf2d9d272012-11-22 14:11:39 +01004957 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
4958 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02004959
Johannes Berg9caf0362012-11-29 01:25:20 +01004960 rcu_read_unlock();
4961
Johannes Berg57fa5e82020-05-28 21:34:36 +02004962 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
4963 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
4964 return -EINVAL;
4965 }
4966
Johannes Berg04ecd252012-09-11 14:34:12 +02004967 /* will change later if needed */
4968 sdata->smps_mode = IEEE80211_SMPS_OFF;
4969
Johannes Berg34a37402013-12-18 09:43:33 +01004970 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004971 /*
4972 * If this fails (possibly due to channel context sharing
4973 * on incompatible channels, e.g. 80+80 and 160 sharing the
4974 * same control channel) try to use a smaller bandwidth.
4975 */
4976 ret = ieee80211_vif_use_channel(sdata, &chandef,
4977 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02004978
4979 /* don't downgrade for 5 and 10 MHz channels, though. */
4980 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4981 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01004982 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02004983
Johannes Bergd601cd82013-02-07 20:54:51 +01004984 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02004985 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01004986 ret = ieee80211_vif_use_channel(sdata, &chandef,
4987 IEEE80211_CHANCTX_SHARED);
4988 }
Johannes Berg34a37402013-12-18 09:43:33 +01004989 out:
4990 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004991 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02004992}
4993
Sara Sharon78ac51f2019-01-16 18:22:56 +02004994static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
4995 u8 *dtim_count, u8 *dtim_period)
4996{
4997 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
4998 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
4999 ies->len);
5000 const struct ieee80211_tim_ie *tim = NULL;
5001 const struct ieee80211_bssid_index *idx;
5002 bool valid = tim_ie && tim_ie[1] >= 2;
5003
5004 if (valid)
5005 tim = (void *)(tim_ie + 2);
5006
5007 if (dtim_count)
5008 *dtim_count = valid ? tim->dtim_count : 0;
5009
5010 if (dtim_period)
5011 *dtim_period = valid ? tim->dtim_period : 0;
5012
5013 /* Check if value is overridden by non-transmitted profile */
5014 if (!idx_ie || idx_ie[1] < 3)
5015 return valid;
5016
5017 idx = (void *)(idx_ie + 2);
5018
5019 if (dtim_count)
5020 *dtim_count = idx->dtim_count;
5021
5022 if (dtim_period)
5023 *dtim_period = idx->dtim_period;
5024
5025 return true;
5026}
5027
Johannes Bergb17166a2012-07-27 11:41:27 +02005028static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005029 struct cfg80211_bss *cbss, bool assoc,
5030 bool override)
Johannes Bergb17166a2012-07-27 11:41:27 +02005031{
5032 struct ieee80211_local *local = sdata->local;
5033 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5034 struct ieee80211_bss *bss = (void *)cbss->priv;
5035 struct sta_info *new_sta = NULL;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005036 struct ieee80211_supported_band *sband;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005037 bool have_sta = false;
Johannes Bergb17166a2012-07-27 11:41:27 +02005038 int err;
5039
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005040 sband = local->hw.wiphy->bands[cbss->channel->band];
5041
Johannes Bergb17166a2012-07-27 11:41:27 +02005042 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5043 return -EINVAL;
5044
Luca Coelhof8860ce2017-05-02 17:56:21 +03005045 /* If a reconfig is happening, bail out */
5046 if (local->in_reconfig)
5047 return -EBUSY;
5048
Johannes Bergb17166a2012-07-27 11:41:27 +02005049 if (assoc) {
5050 rcu_read_lock();
5051 have_sta = sta_info_get(sdata, cbss->bssid);
5052 rcu_read_unlock();
5053 }
5054
5055 if (!have_sta) {
5056 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5057 if (!new_sta)
5058 return -ENOMEM;
5059 }
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005060
Johannes Bergc87905b2017-06-10 13:52:43 +03005061 /*
5062 * Set up the information for the new channel before setting the
5063 * new channel. We can't - completely race-free - change the basic
5064 * rates bitmap and the channel (sband) that it refers to, but if
5065 * we set it up before we at least avoid calling into the driver's
5066 * bss_info_changed() method with invalid information (since we do
5067 * call that from changing the channel - only for IDLE and perhaps
5068 * some others, but ...).
5069 *
5070 * So to avoid that, just set up all the new information before the
5071 * channel, but tell the driver to apply it only afterwards, since
5072 * it might need the new channel for that.
5073 */
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005074 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005075 u32 rates = 0, basic_rates = 0;
5076 bool have_higher_than_11mbit;
5077 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005078 const struct cfg80211_bss_ies *ies;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005079 int shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005080
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005081 ieee80211_get_rates(sband, bss->supp_rates,
5082 bss->supp_rates_len,
5083 &rates, &basic_rates,
5084 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02005085 &min_rate, &min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03005086 shift);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005087
5088 /*
5089 * This used to be a workaround for basic rates missing
5090 * in the association response frame. Now that we no
5091 * longer use the basic rates from there, it probably
5092 * doesn't happen any more, but keep the workaround so
5093 * in case some *other* APs are buggy in different ways
5094 * we can connect -- with a warning.
Ilan Peer302ff8b2020-03-26 15:09:39 +02005095 * Allow this workaround only in case the AP provided at least
5096 * one rate.
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005097 */
Ilan Peer302ff8b2020-03-26 15:09:39 +02005098 if (min_rate_index < 0) {
5099 sdata_info(sdata,
5100 "No legacy rates in association response\n");
5101
5102 sta_info_free(local, new_sta);
5103 return -EINVAL;
5104 } else if (!basic_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005105 sdata_info(sdata,
5106 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005107 basic_rates = BIT(min_rate_index);
5108 }
5109
Johannes Bergbd718fc2019-05-29 15:25:35 +03005110 if (rates)
5111 new_sta->sta.supp_rates[cbss->channel->band] = rates;
5112 else
5113 sdata_info(sdata,
5114 "No rates found, keeping mandatory only\n");
5115
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005116 sdata->vif.bss_conf.basic_rates = basic_rates;
5117
5118 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg57fbcce2016-04-12 15:56:15 +02005119 if (cbss->channel->band == NL80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005120 have_higher_than_11mbit)
5121 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5122 else
5123 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5124
5125 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5126
Johannes Berg8c358bc2012-05-22 22:13:05 +02005127 /* set timing information */
5128 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005129 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01005130 ies = rcu_dereference(cbss->beacon_ies);
5131 if (ies) {
Johannes Bergef429da2013-02-05 17:48:40 +01005132 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5133 sdata->vif.bss_conf.sync_device_ts =
5134 bss->device_ts_beacon;
Sara Sharon78ac51f2019-01-16 18:22:56 +02005135
5136 ieee80211_get_dtim(ies,
5137 &sdata->vif.bss_conf.sync_dtim_count,
5138 NULL);
Johannes Berg30686bf2015-06-02 21:39:54 +02005139 } else if (!ieee80211_hw_check(&sdata->local->hw,
5140 TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005141 ies = rcu_dereference(cbss->proberesp_ies);
5142 /* must be non-NULL since beacon IEs were NULL */
5143 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5144 sdata->vif.bss_conf.sync_device_ts =
5145 bss->device_ts_presp;
5146 sdata->vif.bss_conf.sync_dtim_count = 0;
5147 } else {
5148 sdata->vif.bss_conf.sync_tsf = 0;
5149 sdata->vif.bss_conf.sync_device_ts = 0;
5150 sdata->vif.bss_conf.sync_dtim_count = 0;
5151 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01005152 rcu_read_unlock();
Johannes Bergc87905b2017-06-10 13:52:43 +03005153 }
Johannes Berg8c358bc2012-05-22 22:13:05 +02005154
Johannes Bergc87905b2017-06-10 13:52:43 +03005155 if (new_sta || override) {
5156 err = ieee80211_prep_channel(sdata, cbss);
5157 if (err) {
5158 if (new_sta)
5159 sta_info_free(local, new_sta);
5160 return -EINVAL;
5161 }
5162 }
5163
5164 if (new_sta) {
5165 /*
5166 * tell driver about BSSID, basic rates and timing
5167 * this was set up above, before setting the channel
5168 */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005169 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02005170 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5171 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01005172
5173 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005174 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01005175
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005176 err = sta_info_insert(new_sta);
5177 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01005178 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005179 sdata_info(sdata,
5180 "failed to insert STA entry for the AP (error %d)\n",
5181 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01005182 return err;
5183 }
5184 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00005185 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01005186
David Spinadel7d830a12015-03-17 19:58:38 +02005187 /* Cancel scan to ensure that nothing interferes with connection */
5188 if (local->scanning)
5189 ieee80211_scan_cancel(local);
5190
Johannes Berga1cf7752012-03-08 15:02:07 +01005191 return 0;
5192}
5193
Johannes Berg77fdaa12009-07-07 03:45:17 +02005194/* config hooks */
5195int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5196 struct cfg80211_auth_request *req)
5197{
Johannes Berg66e67e42012-01-20 13:55:27 +01005198 struct ieee80211_local *local = sdata->local;
5199 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5200 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005201 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01005202 int err;
Jouni Malinenefb543e2018-10-11 00:21:21 +03005203 bool cont_auth;
Johannes Berg66e67e42012-01-20 13:55:27 +01005204
5205 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02005206
5207 switch (req->auth_type) {
5208 case NL80211_AUTHTYPE_OPEN_SYSTEM:
5209 auth_alg = WLAN_AUTH_OPEN;
5210 break;
5211 case NL80211_AUTHTYPE_SHARED_KEY:
Ard Biesheuvel5fdb3732019-06-12 18:19:54 +02005212 if (fips_enabled)
Johannes Berg9dca9c42010-07-21 10:09:25 +02005213 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005214 auth_alg = WLAN_AUTH_SHARED_KEY;
5215 break;
5216 case NL80211_AUTHTYPE_FT:
5217 auth_alg = WLAN_AUTH_FT;
5218 break;
5219 case NL80211_AUTHTYPE_NETWORK_EAP:
5220 auth_alg = WLAN_AUTH_LEAP;
5221 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005222 case NL80211_AUTHTYPE_SAE:
5223 auth_alg = WLAN_AUTH_SAE;
5224 break;
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03005225 case NL80211_AUTHTYPE_FILS_SK:
5226 auth_alg = WLAN_AUTH_FILS_SK;
5227 break;
5228 case NL80211_AUTHTYPE_FILS_SK_PFS:
5229 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5230 break;
5231 case NL80211_AUTHTYPE_FILS_PK:
5232 auth_alg = WLAN_AUTH_FILS_PK;
5233 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005234 default:
5235 return -EOPNOTSUPP;
5236 }
5237
Jouni Malinenefb543e2018-10-11 00:21:21 +03005238 if (ifmgd->assoc_data)
Jouni Malinen8d7432a2018-10-11 00:21:20 +03005239 return -EBUSY;
5240
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005241 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005242 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01005243 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005244 return -ENOMEM;
5245
Johannes Berg66e67e42012-01-20 13:55:27 +01005246 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005247
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005248 if (req->auth_data_len >= 4) {
Jouni Malinen6ec63612016-10-27 00:41:59 +03005249 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5250 __le16 *pos = (__le16 *) req->auth_data;
5251
5252 auth_data->sae_trans = le16_to_cpu(pos[0]);
5253 auth_data->sae_status = le16_to_cpu(pos[1]);
5254 }
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005255 memcpy(auth_data->data, req->auth_data + 4,
5256 req->auth_data_len - 4);
5257 auth_data->data_len += req->auth_data_len - 4;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005258 }
5259
Jouni Malinenefb543e2018-10-11 00:21:21 +03005260 /* Check if continuing authentication or trying to authenticate with the
5261 * same BSS that we were in the process of authenticating with and avoid
5262 * removal and re-addition of the STA entry in
5263 * ieee80211_prep_connection().
5264 */
5265 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5266
Johannes Berg77fdaa12009-07-07 03:45:17 +02005267 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005268 memcpy(&auth_data->data[auth_data->data_len],
5269 req->ie, req->ie_len);
5270 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005271 }
5272
Johannes Bergfffd0932009-07-08 14:22:54 +02005273 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005274 auth_data->key_len = req->key_len;
5275 auth_data->key_idx = req->key_idx;
5276 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02005277 }
5278
Johannes Berg66e67e42012-01-20 13:55:27 +01005279 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005280
Johannes Berg66e67e42012-01-20 13:55:27 +01005281 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01005282
Jouni Malinenefb543e2018-10-11 00:21:21 +03005283 if (ifmgd->auth_data) {
5284 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5285 auth_data->peer_confirmed =
5286 ifmgd->auth_data->peer_confirmed;
5287 }
5288 ieee80211_destroy_auth_data(sdata, cont_auth);
5289 }
Guy Eilam2a33bee2011-08-17 15:18:15 +03005290
Johannes Berg66e67e42012-01-20 13:55:27 +01005291 /* prep auth_data so we don't go into idle on disassoc */
5292 ifmgd->auth_data = auth_data;
5293
Jouni Malinenefb543e2018-10-11 00:21:21 +03005294 /* If this is continuation of an ongoing SAE authentication exchange
5295 * (i.e., request to send SAE Confirm) and the peer has already
5296 * confirmed, mark authentication completed since we are about to send
5297 * out SAE Confirm.
5298 */
5299 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5300 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5301 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5302
Johannes Berg7b119dc2013-04-10 21:38:36 +02005303 if (ifmgd->associated) {
5304 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5305
Johannes Berg89f774e2016-01-25 15:46:36 +02005306 sdata_info(sdata,
5307 "disconnect from AP %pM for new auth to %pM\n",
5308 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005309 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5310 WLAN_REASON_UNSPECIFIED,
5311 false, frame_buf);
5312
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005313 ieee80211_report_disconnect(sdata, frame_buf,
5314 sizeof(frame_buf), true,
5315 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005316 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005317
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005318 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005319
Jouni Malinenefb543e2018-10-11 00:21:21 +03005320 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Johannes Berga1cf7752012-03-08 15:02:07 +01005321 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01005322 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005323
Johannes Berg46cad4b2015-08-15 22:39:54 +03005324 err = ieee80211_auth(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01005325 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005326 sta_info_destroy_addr(sdata, req->bss->bssid);
5327 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005328 }
5329
Johannes Berg66e67e42012-01-20 13:55:27 +01005330 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01005331 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005332 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02005333
Johannes Berg66e67e42012-01-20 13:55:27 +01005334 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005335 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005336 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005337 ifmgd->auth_data = NULL;
Michal Kaziord01f8582015-06-03 08:36:13 +02005338 mutex_lock(&sdata->local->mtx);
5339 ieee80211_vif_release_channel(sdata);
5340 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01005341 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005342 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005343}
5344
Johannes Berg77fdaa12009-07-07 03:45:17 +02005345int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5346 struct cfg80211_assoc_request *req)
5347{
Johannes Berg57fa5e82020-05-28 21:34:36 +02005348 bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
Johannes Berg66e67e42012-01-20 13:55:27 +01005349 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005350 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01005351 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01005352 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005353 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005354 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005355 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005356 int i, err;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005357 bool override = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005358
Johannes Berg66e67e42012-01-20 13:55:27 +01005359 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5360 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01005361 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005362
Johannes Berg9caf0362012-11-29 01:25:20 +01005363 rcu_read_lock();
5364 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Will Deacon41525612019-10-04 10:51:31 +01005365 if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
Johannes Berg9caf0362012-11-29 01:25:20 +01005366 rcu_read_unlock();
5367 kfree(assoc_data);
5368 return -EINVAL;
5369 }
5370 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5371 assoc_data->ssid_len = ssidie[1];
5372 rcu_read_unlock();
5373
Johannes Berg7b119dc2013-04-10 21:38:36 +02005374 if (ifmgd->associated) {
5375 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5376
Johannes Berg89f774e2016-01-25 15:46:36 +02005377 sdata_info(sdata,
5378 "disconnect from AP %pM for new assoc to %pM\n",
5379 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005380 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5381 WLAN_REASON_UNSPECIFIED,
5382 false, frame_buf);
5383
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005384 ieee80211_report_disconnect(sdata, frame_buf,
5385 sizeof(frame_buf), true,
5386 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005387 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005388
5389 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5390 err = -EBUSY;
5391 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005392 }
5393
Johannes Berg66e67e42012-01-20 13:55:27 +01005394 if (ifmgd->assoc_data) {
5395 err = -EBUSY;
5396 goto err_free;
5397 }
5398
5399 if (ifmgd->auth_data) {
5400 bool match;
5401
5402 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00005403 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005404 ieee80211_destroy_auth_data(sdata, match);
5405 }
5406
5407 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02005408
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03005409 ifmgd->beacon_crc_valid = false;
5410
Johannes Berg095d81c2013-10-15 12:25:07 +02005411 assoc_data->wmm = bss->wmm_used &&
5412 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg095d81c2013-10-15 12:25:07 +02005413
Johannes Bergde5036a2012-03-08 15:02:03 +01005414 /*
5415 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5416 * We still associate in non-HT mode (11a/b/g) if any one of these
5417 * ciphers is configured as pairwise.
5418 * We can set this to true for non-11n hardware, that'll be checked
5419 * separately along with the peer capabilities.
5420 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02005421 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02005422 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5423 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02005424 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01005425 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005426 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005427 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg1c4cb922012-05-30 15:57:00 +02005428 netdev_info(sdata->dev,
Johannes Berg54626322019-08-30 14:24:46 +03005429 "disabling HT/VHT/HE due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02005430 }
5431 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005432
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005433 sband = local->hw.wiphy->bands[req->bss->channel->band];
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005434
Johannes Berg75e296e2020-01-31 13:12:39 +02005435 /* also disable HT/VHT/HE if the AP doesn't use WMM */
5436 if (!bss->wmm_used) {
5437 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005438 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005439 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5440 netdev_info(sdata->dev,
5441 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005442 }
5443
Ben Greearef96a8422011-11-18 11:32:00 -08005444 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5445 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5446 sizeof(ifmgd->ht_capa_mask));
5447
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01005448 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5449 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5450 sizeof(ifmgd->vht_capa_mask));
5451
Johannes Berg77fdaa12009-07-07 03:45:17 +02005452 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005453 memcpy(assoc_data->ie, req->ie, req->ie_len);
5454 assoc_data->ie_len = req->ie_len;
5455 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005456
Jouni Malinen39404fe2016-10-27 00:42:05 +03005457 if (req->fils_kek) {
5458 /* should already be checked in cfg80211 - so warn */
5459 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5460 err = -EINVAL;
5461 goto err_free;
5462 }
5463 memcpy(assoc_data->fils_kek, req->fils_kek,
5464 req->fils_kek_len);
5465 assoc_data->fils_kek_len = req->fils_kek_len;
5466 }
5467
5468 if (req->fils_nonces)
5469 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5470 2 * FILS_NONCE_LEN);
5471
Johannes Berg66e67e42012-01-20 13:55:27 +01005472 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01005473
Johannes Bergaf6b6372009-12-23 13:15:35 +01005474 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5475 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02005476 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005477 else
Johannes Berg04ecd252012-09-11 14:34:12 +02005478 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005479 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02005480 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005481
Johannes Berg66e67e42012-01-20 13:55:27 +01005482 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01005483 assoc_data->supp_rates = bss->supp_rates;
5484 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02005485
Johannes Berg9caf0362012-11-29 01:25:20 +01005486 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02005487 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5488 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5489 assoc_data->ap_ht_param =
5490 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
Johannes Berg57fa5e82020-05-28 21:34:36 +02005491 else if (!is_6ghz)
Johannes Berga8243b72012-11-22 14:32:09 +01005492 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005493 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5494 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5495 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5496 sizeof(struct ieee80211_vht_cap));
Johannes Berg57fa5e82020-05-28 21:34:36 +02005497 else if (!is_6ghz)
5498 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5499 IEEE80211_STA_DISABLE_HE;
Johannes Berg9caf0362012-11-29 01:25:20 +01005500 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01005501
Johannes Berg848955c2014-11-11 12:48:42 +01005502 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02005503 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
Johannes Berg848955c2014-11-11 12:48:42 +01005504 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5505 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5506
Kalle Valoab133152010-01-12 10:42:31 +02005507 if (bss->wmm_used && bss->uapsd_supported &&
Johannes Berg848955c2014-11-11 12:48:42 +01005508 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01005509 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02005510 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5511 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01005512 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02005513 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5514 }
5515
Johannes Berg77fdaa12009-07-07 03:45:17 +02005516 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01005517 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005518
5519 if (req->use_mfp) {
5520 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5521 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5522 } else {
5523 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5524 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5525 }
5526
Assaf Krausscd2f5dd2014-09-03 15:25:02 +03005527 if (req->flags & ASSOC_REQ_USE_RRM)
5528 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5529 else
5530 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5531
Johannes Berg77fdaa12009-07-07 03:45:17 +02005532 if (req->crypto.control_port)
5533 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5534 else
5535 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5536
Johannes Berga621fa42010-08-27 14:26:54 +03005537 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5538 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Denis Kenzior018f6fb2018-03-26 12:52:51 -05005539 sdata->control_port_over_nl80211 =
5540 req->crypto.control_port_over_nl80211;
Markus Theil7f3f96c2020-03-12 10:10:54 +01005541 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02005542 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5543 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03005544
Johannes Berg66e67e42012-01-20 13:55:27 +01005545 /* kick off associate process */
5546
5547 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02005548 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03005549 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01005550
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005551 /* override HT/VHT configuration only if the AP and we support it */
5552 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5553 struct ieee80211_sta_ht_cap sta_ht_cap;
5554
5555 if (req->flags & ASSOC_REQ_DISABLE_HT)
5556 override = true;
5557
5558 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5559 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5560
5561 /* check for 40 MHz disable override */
5562 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5563 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5564 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5565 override = true;
5566
5567 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5568 req->flags & ASSOC_REQ_DISABLE_VHT)
5569 override = true;
5570 }
5571
5572 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5573 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5574 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005575 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005576 }
5577
5578 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5579 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5580
5581 err = ieee80211_prep_connection(sdata, req->bss, true, override);
Johannes Berga1cf7752012-03-08 15:02:07 +01005582 if (err)
5583 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01005584
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005585 rcu_read_lock();
5586 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02005587
Johannes Berg30686bf2015-06-02 21:39:54 +02005588 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005589 !beacon_ies) {
5590 /*
5591 * Wait up to one beacon interval ...
5592 * should this be more if we miss one?
5593 */
5594 sdata_info(sdata, "waiting for beacon from %pM\n",
5595 ifmgd->bssid);
5596 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01005597 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005598 assoc_data->need_beacon = true;
5599 } else if (beacon_ies) {
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005600 const struct element *elem;
Johannes Bergef429da2013-02-05 17:48:40 +01005601 u8 dtim_count = 0;
5602
Sara Sharon78ac51f2019-01-16 18:22:56 +02005603 ieee80211_get_dtim(beacon_ies, &dtim_count,
5604 &ifmgd->dtim_period);
5605
Alexander Bondar989c6502013-05-16 17:34:17 +03005606 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005607 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005608 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01005609
Johannes Berg30686bf2015-06-02 21:39:54 +02005610 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005611 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5612 sdata->vif.bss_conf.sync_device_ts =
5613 bss->device_ts_beacon;
5614 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5615 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02005616
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005617 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5618 beacon_ies->data, beacon_ies->len);
5619 if (elem && elem->datalen >= 3)
5620 sdata->vif.bss_conf.profile_periodicity = elem->data[2];
Sara Sharon78ac51f2019-01-16 18:22:56 +02005621
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005622 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5623 beacon_ies->data, beacon_ies->len);
5624 if (elem && elem->datalen >= 11 &&
5625 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
Sara Sharon78ac51f2019-01-16 18:22:56 +02005626 sdata->vif.bss_conf.ema_ap = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005627 } else {
5628 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005629 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005630 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005631 rcu_read_unlock();
5632
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005633 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01005634
Paul Stewartfcff4f12012-02-23 17:59:53 -08005635 if (bss->corrupt_data) {
5636 char *corrupt_type = "data";
5637 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5638 if (bss->corrupt_data &
5639 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5640 corrupt_type = "beacon and probe response";
5641 else
5642 corrupt_type = "beacon";
5643 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5644 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005645 sdata_info(sdata, "associating with AP with corrupt %s\n",
5646 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08005647 }
5648
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005649 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01005650 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005651 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005652 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005653 ifmgd->assoc_data = NULL;
5654 err_free:
5655 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005656 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005657}
5658
5659int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005660 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005661{
5662 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005663 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005664 bool tx = !req->local_state_change;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005665
Johannes Bergc9c3a062014-03-19 09:11:19 +01005666 if (ifmgd->auth_data &&
5667 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5668 sdata_info(sdata,
5669 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5670 req->bssid, req->reason_code,
5671 ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005672
Ilan Peerd4e36e52018-04-20 13:49:25 +03005673 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005674 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Johannes Berg37ad3882012-02-24 13:50:54 +01005675 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005676 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01005677 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01005678 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005679 ieee80211_report_disconnect(sdata, frame_buf,
5680 sizeof(frame_buf), true,
5681 req->reason_code);
Johannes Berg86552012012-10-29 09:46:31 +01005682
Johannes Bergc9c3a062014-03-19 09:11:19 +01005683 return 0;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03005684 }
5685
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005686 if (ifmgd->assoc_data &&
5687 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5688 sdata_info(sdata,
5689 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5690 req->bssid, req->reason_code,
5691 ieee80211_get_reason_code_string(req->reason_code));
5692
Ilan Peerd4e36e52018-04-20 13:49:25 +03005693 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005694 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005695 IEEE80211_STYPE_DEAUTH,
5696 req->reason_code, tx,
5697 frame_buf);
Johannes Berge6f462d2016-12-08 17:22:09 +01005698 ieee80211_destroy_assoc_data(sdata, false, true);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005699 ieee80211_report_disconnect(sdata, frame_buf,
5700 sizeof(frame_buf), true,
5701 req->reason_code);
5702 return 0;
5703 }
5704
Johannes Berg86552012012-10-29 09:46:31 +01005705 if (ifmgd->associated &&
5706 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Bergc9c3a062014-03-19 09:11:19 +01005707 sdata_info(sdata,
5708 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5709 req->bssid, req->reason_code,
5710 ieee80211_get_reason_code_string(req->reason_code));
5711
Johannes Berg86552012012-10-29 09:46:31 +01005712 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5713 req->reason_code, tx, frame_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005714 ieee80211_report_disconnect(sdata, frame_buf,
5715 sizeof(frame_buf), true,
5716 req->reason_code);
Johannes Bergc9c3a062014-03-19 09:11:19 +01005717 return 0;
5718 }
Johannes Berg86552012012-10-29 09:46:31 +01005719
Johannes Bergc9c3a062014-03-19 09:11:19 +01005720 return -ENOTCONN;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005721}
5722
5723int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005724 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005725{
5726 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07005727 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005728 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02005729
Johannes Berg8d8b2612009-07-25 11:58:36 +02005730 /*
5731 * cfg80211 should catch this ... but it's racy since
5732 * we can receive a disassoc frame, process it, hand it
5733 * to cfg80211 while that's in a locked section already
5734 * trying to tell us that the user wants to disconnect.
5735 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005736 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005737 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005738
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005739 sdata_info(sdata,
Calvin Owensdfa1ad22014-02-11 12:36:24 -06005740 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5741 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005742
Jouni Malinene69e95d2010-03-29 23:29:31 -07005743 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01005744 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5745 req->reason_code, !req->local_state_change,
5746 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005747
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005748 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5749 req->reason_code);
Johannes Bergbc83b682009-11-29 12:19:06 +01005750
Johannes Berg77fdaa12009-07-07 03:45:17 +02005751 return 0;
5752}
Jouni Malinen026331c2010-02-15 12:53:10 +02005753
Eliad Pellerafa762f2012-04-23 14:45:15 +03005754void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005755{
5756 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5757
Ben Greear49921852013-02-20 09:41:09 -08005758 /*
5759 * Make sure some work items will not run after this,
5760 * they will not do anything but might not have been
5761 * cancelled when disconnecting.
5762 */
5763 cancel_work_sync(&ifmgd->monitor_work);
5764 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5765 cancel_work_sync(&ifmgd->request_smps_work);
5766 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5767 cancel_work_sync(&ifmgd->chswitch_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03005768 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
Ben Greear49921852013-02-20 09:41:09 -08005769
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005770 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02005771 if (ifmgd->assoc_data) {
5772 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berge6f462d2016-12-08 17:22:09 +01005773 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02005774 cfg80211_assoc_timeout(sdata->dev, bss);
5775 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005776 if (ifmgd->auth_data)
5777 ieee80211_destroy_auth_data(sdata, false);
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005778 spin_lock_bh(&ifmgd->teardown_lock);
5779 if (ifmgd->teardown_skb) {
5780 kfree_skb(ifmgd->teardown_skb);
5781 ifmgd->teardown_skb = NULL;
5782 ifmgd->orig_teardown_skb = NULL;
5783 }
Jouni Malinen4d9ec732019-02-15 02:14:33 +02005784 kfree(ifmgd->assoc_req_ies);
5785 ifmgd->assoc_req_ies = NULL;
5786 ifmgd->assoc_req_ies_len = 0;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005787 spin_unlock_bh(&ifmgd->teardown_lock);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005788 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005789 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005790}
5791
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005792void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5793 enum nl80211_cqm_rssi_threshold_event rssi_event,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005794 s32 rssi_level,
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005795 gfp_t gfp)
5796{
5797 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5798
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005799 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
Johannes Bergb5878a22010-04-07 16:48:40 +02005800
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +01005801 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005802}
5803EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Johannes Berg98f03342014-11-26 12:42:02 +01005804
5805void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5806{
5807 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5808
5809 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5810
5811 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5812}
5813EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);