blob: 64b6ddb674562a97fdc1d2b615c178e89ab8f500 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03008 * Copyright 2013-2014 Intel Mobile Communications GmbH
Avraham Sterne38a0172017-04-26 10:58:47 +03009 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Ilan Peer94ba9272018-02-19 14:48:41 +020010 * Copyright (C) 2018 Intel Corporation
Jiri Bencf0706e82007-05-05 11:45:53 -070011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070017#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/if_ether.h>
19#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070021#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040022#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010023#include <linux/rtnetlink.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020024#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040026#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020028#include <asm/unaligned.h>
Johannes Berg60f8b39c2008-09-08 17:44:22 +020029
Jiri Bencf0706e82007-05-05 11:45:53 -070030#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020031#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040032#include "rate.h"
33#include "led.h"
Jouni Malinen39404fe2016-10-27 00:42:05 +030034#include "fils_aead.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070035
Johannes Berg1672c0e32013-01-29 15:02:27 +010036#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020037#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010038#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
Ilan Peer407879b2018-04-20 13:49:20 +030039#define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010040#define IEEE80211_AUTH_MAX_TRIES 3
41#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
42#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020043#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010044#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
45#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010046
Ben Greear180205b2011-02-04 15:30:24 -080047static int max_nullfunc_tries = 2;
48module_param(max_nullfunc_tries, int, 0644);
49MODULE_PARM_DESC(max_nullfunc_tries,
50 "Maximum nullfunc tx tries before disconnecting (reason 4).");
51
52static int max_probe_tries = 5;
53module_param(max_probe_tries, int, 0644);
54MODULE_PARM_DESC(max_probe_tries,
55 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020056
57/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010058 * Beacon loss timeout is calculated as N frames times the
59 * advertised beacon interval. This may need to be somewhat
60 * higher than what hardware might detect to account for
61 * delays in the host processing frames. But since we also
62 * probe on beacon miss before declaring the connection lost
63 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020064 */
Ben Greear59c1ec22013-03-19 14:19:56 -070065static int beacon_loss_count = 7;
66module_param(beacon_loss_count, int, 0644);
67MODULE_PARM_DESC(beacon_loss_count,
68 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010069
Johannes Bergb291ba12009-07-10 15:29:03 +020070/*
71 * Time the connection can be idle before we probe
72 * it to see if we can still talk to the AP.
73 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030074#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020075/*
76 * Time we wait for a probe response after sending
77 * a probe request because of beacon loss or for
78 * checking the connection still works.
79 */
Ben Greear180205b2011-02-04 15:30:24 -080080static int probe_wait_ms = 500;
81module_param(probe_wait_ms, int, 0644);
82MODULE_PARM_DESC(probe_wait_ms,
83 "Maximum time(ms) to wait for probe response"
84 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070085
Jouni Malinen17e4ec12010-03-29 23:28:30 -070086/*
Jouni Malinen391a2002010-08-27 22:22:00 +030087 * How many Beacon frames need to have been used in average signal strength
88 * before starting to indicate signal change events.
89 */
90#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
91
Johannes Berg77fdaa12009-07-07 03:45:17 +020092/*
Johannes Bergca386f32009-07-10 02:39:48 +020093 * We can have multiple work items (and connection probing)
94 * scheduling this timer, but we need to take care to only
95 * reschedule it when it should fire _earlier_ than it was
96 * asked for before, or if it's not pending right now. This
97 * function ensures that. Note that it then is required to
98 * run this function for all timeouts after the first one
99 * has happened -- the work that runs from this timer will
100 * do that.
101 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200102static void run_again(struct ieee80211_sub_if_data *sdata,
103 unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200104{
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200105 sdata_assert_lock(sdata);
Johannes Bergca386f32009-07-10 02:39:48 +0200106
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200107 if (!timer_pending(&sdata->u.mgd.timer) ||
108 time_before(timeout, sdata->u.mgd.timer.expires))
109 mod_timer(&sdata->u.mgd.timer, timeout);
Johannes Bergca386f32009-07-10 02:39:48 +0200110}
111
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400112void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200113{
Johannes Bergc1288b12012-01-19 09:29:57 +0100114 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200115 return;
116
Johannes Berg30686bf2015-06-02 21:39:54 +0200117 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Johannes Berg7eeff742012-07-18 10:27:27 +0200118 return;
119
Johannes Bergb291ba12009-07-10 15:29:03 +0200120 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100121 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200122}
123
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400124void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
125{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400126 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
127
Johannes Bergb100e5d2016-03-17 15:41:37 +0200128 if (unlikely(!ifmgd->associated))
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100129 return;
130
Johannes Bergb100e5d2016-03-17 15:41:37 +0200131 if (ifmgd->probe_send_count)
132 ifmgd->probe_send_count = 0;
Eliad Peller448cd2e2014-02-11 12:30:18 +0200133
Johannes Berg30686bf2015-06-02 21:39:54 +0200134 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400135 return;
136
Johannes Bergb100e5d2016-03-17 15:41:37 +0200137 mod_timer(&ifmgd->conn_mon_timer,
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400138 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
139}
140
Johannes Berg5484e232008-09-08 17:44:27 +0200141static int ecw2cw(int ecw)
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200142{
Johannes Berg5484e232008-09-08 17:44:27 +0200143 return (1 << ecw) - 1;
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200144}
145
Johannes Berg6565ec92013-02-08 14:52:32 +0100146static u32
147ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
148 struct ieee80211_supported_band *sband,
149 struct ieee80211_channel *channel,
150 const struct ieee80211_ht_operation *ht_oper,
151 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300152 const struct ieee80211_he_operation *he_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200153 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100154{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100156 struct cfg80211_chan_def vht_chandef;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200157 struct ieee80211_sta_ht_cap sta_ht_cap;
Johannes Berg6565ec92013-02-08 14:52:32 +0100158 u32 ht_cfreq, ret;
159
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200160 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
161 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
162
Johannes Berg6565ec92013-02-08 14:52:32 +0100163 chandef->chan = channel;
164 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
165 chandef->center_freq1 = channel->center_freq;
166 chandef->center_freq2 = 0;
167
Johannes Bergb1b1ae22017-10-13 15:26:01 +0300168 if (!ht_oper || !sta_ht_cap.ht_supported) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100169 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
170 goto out;
171 }
172
173 chandef->width = NL80211_CHAN_WIDTH_20;
174
175 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
176 channel->band);
177 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200178 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100179 /*
180 * It's possible that some APs are confused here;
181 * Netgear WNDR3700 sometimes reports 4 higher than
182 * the actual channel in association responses, but
183 * since we look at probe response/beacon data here
184 * it should be OK.
185 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200186 sdata_info(sdata,
187 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
188 channel->center_freq, ht_cfreq,
189 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100190 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
191 goto out;
192 }
193
194 /* check 40 MHz support, if we have it */
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200195 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100196 ieee80211_chandef_ht_oper(ht_oper, chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100197 } else {
198 /* 40 MHz (and 80 MHz) must be supported for VHT */
199 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100200 /* also mark 40 MHz disabled */
201 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100202 goto out;
203 }
204
205 if (!vht_oper || !sband->vht_cap.vht_supported) {
206 ret = IEEE80211_STA_DISABLE_VHT;
207 goto out;
208 }
209
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100210 vht_chandef = *chandef;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300211 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
212 (le32_to_cpu(he_oper->he_oper_params) &
213 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
214 struct ieee80211_vht_operation he_oper_vht_cap;
215
216 /*
217 * Set only first 3 bytes (other 2 aren't used in
218 * ieee80211_chandef_vht_oper() anyway)
219 */
220 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
221 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
222
Johannes Berg7eb26df2018-08-31 11:31:18 +0300223 if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
224 &he_oper_vht_cap, ht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300225 &vht_chandef)) {
226 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
227 sdata_info(sdata,
228 "HE AP VHT information is invalid, disable HE\n");
229 ret = IEEE80211_STA_DISABLE_HE;
230 goto out;
231 }
Johannes Berg7eb26df2018-08-31 11:31:18 +0300232 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_oper,
233 ht_oper, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200234 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100235 sdata_info(sdata,
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100236 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100237 ret = IEEE80211_STA_DISABLE_VHT;
238 goto out;
239 }
240
241 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200242 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100243 sdata_info(sdata,
244 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100245 ret = IEEE80211_STA_DISABLE_VHT;
246 goto out;
247 }
248
249 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
250 ret = 0;
251 goto out;
252 }
253
254 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200255 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100256 sdata_info(sdata,
257 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100258 ret = IEEE80211_STA_DISABLE_VHT;
259 goto out;
260 }
261
262 *chandef = vht_chandef;
263
264 ret = 0;
265
266out:
Johannes Berg963a18522014-02-21 20:34:34 +0100267 /*
268 * When tracking the current AP, don't do any further checks if the
269 * new chandef is identical to the one we're currently using for the
270 * connection. This keeps us from playing ping-pong with regulatory,
271 * without it the following can happen (for example):
272 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
273 * - AP advertises regdom US
274 * - CRDA loads regdom US with 80 MHz prohibited (old database)
275 * - the code below detects an unsupported channel, downgrades, and
276 * we disconnect from the AP in the caller
277 * - disconnect causes CRDA to reload world regdomain and the game
278 * starts anew.
279 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
280 *
281 * It seems possible that there are still scenarios with CSA or real
282 * bandwidth changes where a this could happen, but those cases are
283 * less common and wouldn't completely prevent using the AP.
284 */
285 if (tracking &&
286 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
287 return ret;
288
Johannes Berg586e01e2013-02-14 12:13:53 +0100289 /* don't print the message below for VHT mismatch if VHT is disabled */
290 if (ret & IEEE80211_STA_DISABLE_VHT)
291 vht_chandef = *chandef;
292
Johannes Bergddfe49b2013-07-31 20:52:03 +0200293 /*
294 * Ignore the DISABLED flag when we're already connected and only
295 * tracking the APs beacon for bandwidth changes - otherwise we
296 * might get disconnected here if we connect to an AP, update our
297 * regulatory information based on the AP's country IE and the
298 * information we have is wrong/outdated and disables the channel
299 * that we're actually using for the connection to the AP.
300 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100301 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200302 tracking ? 0 :
303 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100304 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
305 ret = IEEE80211_STA_DISABLE_HT |
306 IEEE80211_STA_DISABLE_VHT;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700307 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100308 }
309
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200310 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100311 }
312
Johannes Berg5cdaed12013-07-31 11:23:06 +0200313 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100314 sdata_info(sdata,
315 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
316
317 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
318 return ret;
319}
320
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100321static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
322 struct sta_info *sta,
Eliad Peller53b954e2014-07-24 11:20:05 +0300323 const struct ieee80211_ht_cap *ht_cap,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100324 const struct ieee80211_ht_operation *ht_oper,
325 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300326 const struct ieee80211_he_operation *he_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100327 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200328{
329 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100330 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300331 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
332 struct ieee80211_supported_band *sband =
333 local->hw.wiphy->bands[chan->band];
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100334 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200335 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100336 u32 flags;
337 enum ieee80211_sta_rx_bandwidth new_sta_bw;
338 int ret;
339
340 /* if HT was/is disabled, don't track any bandwidth changes */
341 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
342 return 0;
343
344 /* don't check VHT if we associated as non-VHT station */
345 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
346 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200347
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300348 /* don't check HE if we associated as non-HE station */
349 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
350 !ieee80211_get_he_sta_cap(sband))
351 he_oper = NULL;
352
Johannes Berg11289582013-02-07 17:36:12 +0100353 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100354 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100355
Avri Altman017b45b2013-11-18 19:06:48 +0200356 /*
357 * if bss configuration changed store the new one -
358 * this may be applicable even if channel is identical
359 */
360 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
361 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
362 *changed |= BSS_CHANGED_HT;
363 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
364 }
365
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300366 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
Eliad Peller53b954e2014-07-24 11:20:05 +0300367 flags = ieee80211_determine_chantype(sdata, sband, chan,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300368 ht_oper, vht_oper, he_oper,
Eliad Peller53b954e2014-07-24 11:20:05 +0300369 &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100370
371 /*
372 * Downgrade the new channel if we associated with restricted
373 * capabilities. For example, if we associated as a 20 MHz STA
374 * to a 40 MHz AP (due to regulatory, capabilities or config
375 * reasons) then switching to a 40 MHz channel now won't do us
376 * any good -- we couldn't use it with the AP.
377 */
378 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
379 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200380 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100381 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
382 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200383 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100384 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
385 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200386 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100387
388 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
389 return 0;
390
391 sdata_info(sdata,
392 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
393 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
394 chandef.center_freq1, chandef.center_freq2);
395
396 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
397 IEEE80211_STA_DISABLE_VHT |
398 IEEE80211_STA_DISABLE_40MHZ |
399 IEEE80211_STA_DISABLE_80P80MHZ |
400 IEEE80211_STA_DISABLE_160MHZ)) ||
401 !cfg80211_chandef_valid(&chandef)) {
402 sdata_info(sdata,
403 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
404 ifmgd->bssid);
405 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200406 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200407
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100408 switch (chandef.width) {
409 case NL80211_CHAN_WIDTH_20_NOHT:
410 case NL80211_CHAN_WIDTH_20:
411 new_sta_bw = IEEE80211_STA_RX_BW_20;
412 break;
413 case NL80211_CHAN_WIDTH_40:
414 new_sta_bw = IEEE80211_STA_RX_BW_40;
415 break;
416 case NL80211_CHAN_WIDTH_80:
417 new_sta_bw = IEEE80211_STA_RX_BW_80;
418 break;
419 case NL80211_CHAN_WIDTH_80P80:
420 case NL80211_CHAN_WIDTH_160:
421 new_sta_bw = IEEE80211_STA_RX_BW_160;
422 break;
423 default:
424 return -EINVAL;
425 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200426
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100427 if (new_sta_bw > sta->cur_max_bandwidth)
428 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200429
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100430 if (new_sta_bw < sta->sta.bandwidth) {
431 sta->sta.bandwidth = new_sta_bw;
432 rate_control_rate_update(local, sband, sta,
433 IEEE80211_RC_BW_CHANGED);
434 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530435
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100436 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
437 if (ret) {
438 sdata_info(sdata,
439 "AP %pM changed bandwidth to incompatible one - disconnect\n",
440 ifmgd->bssid);
441 return ret;
442 }
443
444 if (new_sta_bw > sta->sta.bandwidth) {
445 sta->sta.bandwidth = new_sta_bw;
446 rate_control_rate_update(local, sband, sta,
447 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200448 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200449
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100450 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200451}
452
Johannes Berg9c6bd792008-09-11 00:01:52 +0200453/* frame sending functions */
454
Johannes Berg66e67e42012-01-20 13:55:27 +0100455static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200456 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100457 struct ieee80211_supported_band *sband,
458 struct ieee80211_channel *channel,
459 enum ieee80211_smps_mode smps)
460{
Johannes Berg66e67e42012-01-20 13:55:27 +0100461 u8 *pos;
462 u32 flags = channel->flags;
463 u16 cap;
464 struct ieee80211_sta_ht_cap ht_cap;
465
466 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
467
Johannes Berg66e67e42012-01-20 13:55:27 +0100468 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
469 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
470
Johannes Berg66e67e42012-01-20 13:55:27 +0100471 /* determine capability flags */
472 cap = ht_cap.cap;
473
Johannes Berg9dde6422012-05-16 23:43:19 +0200474 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100475 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
476 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
477 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
478 cap &= ~IEEE80211_HT_CAP_SGI_40;
479 }
480 break;
481 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
482 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
483 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
484 cap &= ~IEEE80211_HT_CAP_SGI_40;
485 }
486 break;
487 }
488
Johannes Berg24398e32012-03-28 10:58:36 +0200489 /*
490 * If 40 MHz was disabled associate as though we weren't
491 * capable of 40 MHz -- some broken APs will never fall
492 * back to trying to transmit in 20 MHz.
493 */
494 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
495 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
496 cap &= ~IEEE80211_HT_CAP_SGI_40;
497 }
498
Johannes Berg66e67e42012-01-20 13:55:27 +0100499 /* set SM PS mode properly */
500 cap &= ~IEEE80211_HT_CAP_SM_PS;
501 switch (smps) {
502 case IEEE80211_SMPS_AUTOMATIC:
503 case IEEE80211_SMPS_NUM_MODES:
504 WARN_ON(1);
Gustavo A. R. Silva02049ce2017-10-17 18:14:50 -0500505 /* fall through */
Johannes Berg66e67e42012-01-20 13:55:27 +0100506 case IEEE80211_SMPS_OFF:
507 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
508 IEEE80211_HT_CAP_SM_PS_SHIFT;
509 break;
510 case IEEE80211_SMPS_STATIC:
511 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
512 IEEE80211_HT_CAP_SM_PS_SHIFT;
513 break;
514 case IEEE80211_SMPS_DYNAMIC:
515 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
516 IEEE80211_HT_CAP_SM_PS_SHIFT;
517 break;
518 }
519
520 /* reserve and fill IE */
521 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
522 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
523}
524
Sara Sharon322cd402015-07-08 15:41:43 +0300525/* This function determines vht capability flags for the association
526 * and builds the IE.
527 * Note - the function may set the owner of the MU-MIMO capability
528 */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000529static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
530 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100531 struct ieee80211_supported_band *sband,
532 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000533{
Sara Sharon322cd402015-07-08 15:41:43 +0300534 struct ieee80211_local *local = sdata->local;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000535 u8 *pos;
536 u32 cap;
537 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200538 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000539
540 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
541
542 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100543 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000544
545 /* determine capability flags */
546 cap = vht_cap.cap;
547
Johannes Bergf2d9d272012-11-22 14:11:39 +0100548 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
Johannes Berg575f0532014-11-24 16:51:33 +0100549 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
550
551 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
552 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
553 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
554 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100555 }
556
557 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
558 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
Johannes Berg575f0532014-11-24 16:51:33 +0100559 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100560 }
561
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100562 /*
563 * Some APs apparently get confused if our capabilities are better
564 * than theirs, so restrict what we advertise in the assoc request.
565 */
566 if (!(ap_vht_cap->vht_cap_info &
567 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
Sara Sharon322cd402015-07-08 15:41:43 +0300568 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
569 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
570 else if (!(ap_vht_cap->vht_cap_info &
571 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
572 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
573
574 /*
575 * If some other vif is using the MU-MIMO capablity we cannot associate
576 * using MU-MIMO - this will lead to contradictions in the group-id
577 * mechanism.
578 * Ownership is defined since association request, in order to avoid
579 * simultaneous associations with MU-MIMO.
580 */
581 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
582 bool disable_mu_mimo = false;
583 struct ieee80211_sub_if_data *other;
584
585 list_for_each_entry_rcu(other, &local->interfaces, list) {
Sara Sharonb5a33d52016-02-16 12:48:18 +0200586 if (other->vif.mu_mimo_owner) {
Sara Sharon322cd402015-07-08 15:41:43 +0300587 disable_mu_mimo = true;
588 break;
589 }
590 }
591 if (disable_mu_mimo)
592 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
593 else
Sara Sharonb5a33d52016-02-16 12:48:18 +0200594 sdata->vif.mu_mimo_owner = true;
Sara Sharon322cd402015-07-08 15:41:43 +0300595 }
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100596
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200597 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
598
599 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
600 our_bf_sts = cap & mask;
601
602 if (ap_bf_sts < our_bf_sts) {
603 cap &= ~mask;
604 cap |= ap_bf_sts;
605 }
606
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000607 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000608 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000609 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
610}
611
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300612/* This function determines HE capability flags for the association
613 * and builds the IE.
614 */
615static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
616 struct sk_buff *skb,
617 struct ieee80211_supported_band *sband)
618{
619 u8 *pos;
620 const struct ieee80211_sta_he_cap *he_cap = NULL;
621 u8 he_cap_size;
622
623 he_cap = ieee80211_get_he_sta_cap(sband);
624 if (!he_cap)
625 return;
626
627 /*
628 * TODO: the 1 added is because this temporarily is under the EXTENSION
629 * IE. Get rid of it when it moves.
630 */
631 he_cap_size =
632 2 + 1 + sizeof(he_cap->he_cap_elem) +
633 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
634 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
635 he_cap->he_cap_elem.phy_cap_info);
636 pos = skb_put(skb, he_cap_size);
637 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
638}
639
Johannes Berg66e67e42012-01-20 13:55:27 +0100640static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
641{
642 struct ieee80211_local *local = sdata->local;
643 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
644 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
645 struct sk_buff *skb;
646 struct ieee80211_mgmt *mgmt;
647 u8 *pos, qos_info;
648 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200649 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100650 u16 capab;
651 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200652 struct ieee80211_chanctx_conf *chanctx_conf;
653 struct ieee80211_channel *chan;
Johannes Berg44f6d422017-06-10 13:52:44 +0300654 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100655
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200656 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100657
Johannes Berg55de9082012-07-26 17:24:39 +0200658 rcu_read_lock();
659 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
660 if (WARN_ON(!chanctx_conf)) {
661 rcu_read_unlock();
662 return;
663 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100664 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200665 rcu_read_unlock();
666 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200667 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100668
669 if (assoc_data->supp_rates_len) {
670 /*
671 * Get all rates supported by the device and the AP as
672 * some APs don't like getting a superset of their rates
673 * in the association request (e.g. D-Link DAP 1353 in
674 * b-only mode)...
675 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200676 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
677 assoc_data->supp_rates,
678 assoc_data->supp_rates_len,
679 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100680 } else {
681 /*
682 * In case AP not provide any supported rates information
683 * before association, we send information element(s) with
684 * all rates that we support.
685 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200686 rates_len = 0;
687 for (i = 0; i < sband->n_bitrates; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200688 rates |= BIT(i);
689 rates_len++;
690 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100691 }
692
693 skb = alloc_skb(local->hw.extra_tx_headroom +
694 sizeof(*mgmt) + /* bit too much but doesn't matter */
695 2 + assoc_data->ssid_len + /* SSID */
696 4 + rates_len + /* (extended) rates */
697 4 + /* power capability */
698 2 + 2 * sband->n_channels + /* supported channels */
699 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000700 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300701 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
702 sizeof(struct ieee80211_he_mcs_nss_supp) +
703 IEEE80211_HE_PPE_THRES_MAX_LEN +
Johannes Berg66e67e42012-01-20 13:55:27 +0100704 assoc_data->ie_len + /* extra IEs */
Jouni Malinen39404fe2016-10-27 00:42:05 +0300705 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100706 9, /* WMM */
707 GFP_KERNEL);
708 if (!skb)
709 return;
710
711 skb_reserve(skb, local->hw.extra_tx_headroom);
712
713 capab = WLAN_CAPABILITY_ESS;
714
Johannes Berg57fbcce2016-04-12 15:56:15 +0200715 if (sband->band == NL80211_BAND_2GHZ) {
Johannes Bergea1b2b452015-06-02 20:15:49 +0200716 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
717 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Johannes Berg66e67e42012-01-20 13:55:27 +0100718 }
719
720 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
721 capab |= WLAN_CAPABILITY_PRIVACY;
722
723 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200724 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
Johannes Berg66e67e42012-01-20 13:55:27 +0100725 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
726
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300727 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
728 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
729
Johannes Bergb080db52017-06-16 14:29:19 +0200730 mgmt = skb_put_zero(skb, 24);
Johannes Berg66e67e42012-01-20 13:55:27 +0100731 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
732 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
733 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
734
735 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
736 skb_put(skb, 10);
737 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
738 IEEE80211_STYPE_REASSOC_REQ);
739 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
740 mgmt->u.reassoc_req.listen_interval =
741 cpu_to_le16(local->hw.conf.listen_interval);
742 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
743 ETH_ALEN);
744 } else {
745 skb_put(skb, 4);
746 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
747 IEEE80211_STYPE_ASSOC_REQ);
748 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
749 mgmt->u.assoc_req.listen_interval =
750 cpu_to_le16(local->hw.conf.listen_interval);
751 }
752
753 /* SSID */
754 pos = skb_put(skb, 2 + assoc_data->ssid_len);
755 *pos++ = WLAN_EID_SSID;
756 *pos++ = assoc_data->ssid_len;
757 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
758
759 /* add all rates which were marked to be used above */
760 supp_rates_len = rates_len;
761 if (supp_rates_len > 8)
762 supp_rates_len = 8;
763
764 pos = skb_put(skb, supp_rates_len + 2);
765 *pos++ = WLAN_EID_SUPP_RATES;
766 *pos++ = supp_rates_len;
767
768 count = 0;
769 for (i = 0; i < sband->n_bitrates; i++) {
770 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200771 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
772 5 * (1 << shift));
773 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100774 if (++count == 8)
775 break;
776 }
777 }
778
779 if (rates_len > count) {
780 pos = skb_put(skb, rates_len - count + 2);
781 *pos++ = WLAN_EID_EXT_SUPP_RATES;
782 *pos++ = rates_len - count;
783
784 for (i++; i < sband->n_bitrates; i++) {
785 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200786 int rate;
787 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
788 5 * (1 << shift));
789 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100790 }
791 }
792 }
793
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300794 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
795 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100796 pos = skb_put(skb, 4);
797 *pos++ = WLAN_EID_PWR_CAPABILITY;
798 *pos++ = 2;
799 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200800 /* max tx power */
801 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300802 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100803
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300804 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100805 /* TODO: get this in reg domain format */
806 pos = skb_put(skb, 2 * sband->n_channels + 2);
807 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
808 *pos++ = 2 * sband->n_channels;
809 for (i = 0; i < sband->n_channels; i++) {
810 *pos++ = ieee80211_frequency_to_channel(
811 sband->channels[i].center_freq);
812 *pos++ = 1; /* one channel in the subband*/
813 }
814 }
815
816 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200817 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100818 static const u8 before_ht[] = {
819 WLAN_EID_SSID,
820 WLAN_EID_SUPP_RATES,
821 WLAN_EID_EXT_SUPP_RATES,
822 WLAN_EID_PWR_CAPABILITY,
823 WLAN_EID_SUPPORTED_CHANNELS,
824 WLAN_EID_RSN,
825 WLAN_EID_QOS_CAPA,
826 WLAN_EID_RRM_ENABLED_CAPABILITIES,
827 WLAN_EID_MOBILITY_DOMAIN,
Johannes Berg8ed28742014-10-27 12:03:19 +0100828 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
829 WLAN_EID_RIC_DATA, /* reassoc only */
Johannes Berg66e67e42012-01-20 13:55:27 +0100830 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
831 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100832 static const u8 after_ric[] = {
833 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
834 WLAN_EID_HT_CAPABILITY,
835 WLAN_EID_BSS_COEX_2040,
Johannes Berga7f26d82017-08-05 11:44:32 +0300836 /* luckily this is almost always there */
Johannes Berg8ed28742014-10-27 12:03:19 +0100837 WLAN_EID_EXT_CAPABILITY,
838 WLAN_EID_QOS_TRAFFIC_CAPA,
839 WLAN_EID_TIM_BCAST_REQ,
840 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300841 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg8ed28742014-10-27 12:03:19 +0100842 WLAN_EID_VHT_CAPABILITY,
843 WLAN_EID_OPMODE_NOTIF,
844 };
845
846 noffset = ieee80211_ie_split_ric(assoc_data->ie,
847 assoc_data->ie_len,
848 before_ht,
849 ARRAY_SIZE(before_ht),
850 after_ric,
851 ARRAY_SIZE(after_ric),
852 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800853 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100854 offset = noffset;
855 }
856
Johannes Bergf2d9d272012-11-22 14:11:39 +0100857 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
858 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
859 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
860
Johannes Berga8243b72012-11-22 14:32:09 +0100861 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200862 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200863 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100864
Johannes Berg3de38022014-02-04 09:54:07 +0100865 /* if present, add any custom IEs that go before VHT */
866 if (assoc_data->ie_len) {
867 static const u8 before_vht[] = {
Johannes Berga7f26d82017-08-05 11:44:32 +0300868 /*
869 * no need to list the ones split off before HT
870 * or generated here
871 */
Johannes Berg3de38022014-02-04 09:54:07 +0100872 WLAN_EID_BSS_COEX_2040,
873 WLAN_EID_EXT_CAPABILITY,
874 WLAN_EID_QOS_TRAFFIC_CAPA,
875 WLAN_EID_TIM_BCAST_REQ,
876 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300877 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg3de38022014-02-04 09:54:07 +0100878 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100879
880 /* RIC already taken above, so no need to handle here anymore */
Johannes Berg3de38022014-02-04 09:54:07 +0100881 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
882 before_vht, ARRAY_SIZE(before_vht),
883 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800884 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg3de38022014-02-04 09:54:07 +0100885 offset = noffset;
886 }
887
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300888 /* if present, add any custom IEs that go before HE */
889 if (assoc_data->ie_len) {
890 static const u8 before_he[] = {
891 /*
892 * no need to list the ones split off before VHT
893 * or generated here
894 */
895 WLAN_EID_OPMODE_NOTIF,
896 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
897 /* 11ai elements */
898 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
899 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
900 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
901 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
902 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
903 /* TODO: add 11ah/11aj/11ak elements */
904 };
905
906 /* RIC already taken above, so no need to handle here anymore */
907 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
908 before_he, ARRAY_SIZE(before_he),
909 offset);
910 pos = skb_put(skb, noffset - offset);
911 memcpy(pos, assoc_data->ie + offset, noffset - offset);
912 offset = noffset;
913 }
914
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000915 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100916 ieee80211_add_vht_ie(sdata, skb, sband,
917 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000918
Shaul Triebitzdc7eb0f2018-12-15 11:03:20 +0200919 /*
920 * If AP doesn't support HT, mark HE as disabled.
921 * If on the 5GHz band, make sure it supports VHT.
922 */
923 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
924 (sband->band == NL80211_BAND_5GHZ &&
925 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
926 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
927
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300928 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
929 ieee80211_add_he_ie(sdata, skb, sband);
930
931 /* if present, add any custom non-vendor IEs that go after HE */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200932 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100933 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
934 assoc_data->ie_len,
935 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800936 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100937 offset = noffset;
938 }
939
Johannes Berg76f03032012-03-08 15:02:05 +0100940 if (assoc_data->wmm) {
941 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200942 qos_info = ifmgd->uapsd_queues;
943 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100944 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
945 } else {
946 qos_info = 0;
947 }
948
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300949 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
Johannes Berg66e67e42012-01-20 13:55:27 +0100950 }
951
952 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200953 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100954 noffset = assoc_data->ie_len;
yuan linyub952f4d2017-06-18 22:52:04 +0800955 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100956 }
957
Jouni Malinen39404fe2016-10-27 00:42:05 +0300958 if (assoc_data->fils_kek_len &&
959 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
960 dev_kfree_skb(skb);
961 return;
962 }
963
Ilan Peerd4e36e52018-04-20 13:49:25 +0300964 drv_mgd_prepare_tx(local, sdata, 0);
Johannes Berga1845fc2012-06-27 13:18:36 +0200965
Johannes Berg66e67e42012-01-20 13:55:27 +0100966 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg30686bf2015-06-02 21:39:54 +0200967 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +0100968 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
969 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100970 ieee80211_tx_skb(sdata, skb);
971}
972
Kalle Valo572e0012009-02-10 17:09:31 +0200973void ieee80211_send_pspoll(struct ieee80211_local *local,
974 struct ieee80211_sub_if_data *sdata)
975{
Kalle Valo572e0012009-02-10 17:09:31 +0200976 struct ieee80211_pspoll *pspoll;
977 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200978
Kalle Valod8cd1892010-01-05 20:16:26 +0200979 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
980 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200981 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200982
Kalle Valod8cd1892010-01-05 20:16:26 +0200983 pspoll = (struct ieee80211_pspoll *) skb->data;
984 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200985
Johannes Berg62ae67b2009-11-18 18:42:05 +0100986 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
987 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200988}
989
Johannes Berg965beda2009-04-16 13:17:24 +0200990void ieee80211_send_nullfunc(struct ieee80211_local *local,
991 struct ieee80211_sub_if_data *sdata,
Johannes Berg076cdcb2015-09-24 16:14:55 +0200992 bool powersave)
Johannes Berg965beda2009-04-16 13:17:24 +0200993{
994 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200995 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530996 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200997
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300998 /* Don't send NDPs when STA is connected HE */
999 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1000 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1001 return;
1002
Ben Caradoc-Davies7c181f42018-03-19 12:57:44 +13001003 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1004 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
Kalle Valod8cd1892010-01-05 20:16:26 +02001005 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +02001006 return;
1007
Kalle Valod8cd1892010-01-05 20:16:26 +02001008 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +02001009 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +02001010 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +02001011
Seth Forshee6c17b772013-02-11 11:21:07 -06001012 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1013 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +02001014
Johannes Berg30686bf2015-06-02 21:39:54 +02001015 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Pontus Fuchsff40b422013-06-04 12:44:52 +02001016 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1017
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001018 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301019 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1020
Johannes Berg62ae67b2009-11-18 18:42:05 +01001021 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +02001022}
1023
Felix Fietkaud5242152010-01-08 18:06:26 +01001024static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1025 struct ieee80211_sub_if_data *sdata)
1026{
1027 struct sk_buff *skb;
1028 struct ieee80211_hdr *nullfunc;
1029 __le16 fc;
1030
1031 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1032 return;
1033
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001034 /* Don't send NDPs when connected HE */
1035 if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1036 return;
1037
Felix Fietkaud5242152010-01-08 18:06:26 +01001038 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -07001039 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +01001040 return;
Joe Perchesd15b8452011-08-29 14:17:31 -07001041
Felix Fietkaud5242152010-01-08 18:06:26 +01001042 skb_reserve(skb, local->hw.extra_tx_headroom);
1043
Johannes Bergb080db52017-06-16 14:29:19 +02001044 nullfunc = skb_put_zero(skb, 30);
Felix Fietkaud5242152010-01-08 18:06:26 +01001045 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1046 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1047 nullfunc->frame_control = fc;
1048 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1049 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1050 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1051 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1052
1053 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1054 ieee80211_tx_skb(sdata, skb);
1055}
1056
Johannes Bergcc32abd2009-05-15 11:52:31 +02001057/* spectrum management related things */
1058static void ieee80211_chswitch_work(struct work_struct *work)
1059{
1060 struct ieee80211_sub_if_data *sdata =
1061 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +01001062 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001063 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001064 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001065
Johannes Berg9607e6b662009-12-23 13:15:31 +01001066 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +02001067 return;
1068
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001069 sdata_lock(sdata);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001070 mutex_lock(&local->mtx);
1071 mutex_lock(&local->chanctx_mtx);
1072
Johannes Berg77fdaa12009-07-07 03:45:17 +02001073 if (!ifmgd->associated)
1074 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001075
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001076 if (!sdata->vif.csa_active)
1077 goto out;
1078
1079 /*
1080 * using reservation isn't immediate as it may be deferred until later
1081 * with multi-vif. once reservation is complete it will re-schedule the
1082 * work with no reserved_chanctx so verify chandef to check if it
1083 * completed successfully
1084 */
1085
1086 if (sdata->reserved_chanctx) {
Ilan Peer0007e942018-08-31 11:31:10 +03001087 struct ieee80211_supported_band *sband = NULL;
1088 struct sta_info *mgd_sta = NULL;
1089 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1090
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001091 /*
1092 * with multi-vif csa driver may call ieee80211_csa_finish()
1093 * many times while waiting for other interfaces to use their
1094 * reservations
1095 */
1096 if (sdata->reserved_ready)
1097 goto out;
1098
Ilan Peer0007e942018-08-31 11:31:10 +03001099 if (sdata->vif.bss_conf.chandef.width !=
1100 sdata->csa_chandef.width) {
1101 /*
1102 * For managed interface, we need to also update the AP
1103 * station bandwidth and align the rate scale algorithm
1104 * on the bandwidth change. Here we only consider the
1105 * bandwidth of the new channel definition (as channel
1106 * switch flow does not have the full HT/VHT/HE
1107 * information), assuming that if additional changes are
1108 * required they would be done as part of the processing
1109 * of the next beacon from the AP.
1110 */
1111 switch (sdata->csa_chandef.width) {
1112 case NL80211_CHAN_WIDTH_20_NOHT:
1113 case NL80211_CHAN_WIDTH_20:
1114 default:
1115 bw = IEEE80211_STA_RX_BW_20;
1116 break;
1117 case NL80211_CHAN_WIDTH_40:
1118 bw = IEEE80211_STA_RX_BW_40;
1119 break;
1120 case NL80211_CHAN_WIDTH_80:
1121 bw = IEEE80211_STA_RX_BW_80;
1122 break;
1123 case NL80211_CHAN_WIDTH_80P80:
1124 case NL80211_CHAN_WIDTH_160:
1125 bw = IEEE80211_STA_RX_BW_160;
1126 break;
1127 }
1128
1129 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1130 sband =
1131 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1132 }
1133
1134 if (sdata->vif.bss_conf.chandef.width >
1135 sdata->csa_chandef.width) {
1136 mgd_sta->sta.bandwidth = bw;
1137 rate_control_rate_update(local, sband, mgd_sta,
1138 IEEE80211_RC_BW_CHANGED);
1139 }
1140
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001141 ret = ieee80211_vif_use_reserved_context(sdata);
1142 if (ret) {
1143 sdata_info(sdata,
1144 "failed to use reserved channel context, disconnecting (err=%d)\n",
1145 ret);
1146 ieee80211_queue_work(&sdata->local->hw,
1147 &ifmgd->csa_connection_drop_work);
1148 goto out;
1149 }
1150
Ilan Peer0007e942018-08-31 11:31:10 +03001151 if (sdata->vif.bss_conf.chandef.width <
1152 sdata->csa_chandef.width) {
1153 mgd_sta->sta.bandwidth = bw;
1154 rate_control_rate_update(local, sband, mgd_sta,
1155 IEEE80211_RC_BW_CHANGED);
1156 }
1157
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001158 goto out;
1159 }
1160
1161 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1162 &sdata->csa_chandef)) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001163 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001164 "failed to finalize channel switch, disconnecting\n");
Arik Nemtsov7578d572013-09-01 17:15:51 +03001165 ieee80211_queue_work(&sdata->local->hw,
1166 &ifmgd->csa_connection_drop_work);
1167 goto out;
1168 }
Karl Beldan675a0b02013-03-25 16:26:57 +01001169
Johannes Berg77fdaa12009-07-07 03:45:17 +02001170 /* XXX: shouldn't really modify cfg80211-owned data! */
Luciano Coelho33787fc2013-11-11 20:34:54 +02001171 ifmgd->associated->channel = sdata->csa_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001172
Luciano Coelho0c21e632014-10-08 09:48:39 +03001173 ifmgd->csa_waiting_bcn = true;
Luciano Coelhof1d65582014-10-08 09:48:38 +03001174
Michal Kaziore5593f52014-04-09 15:45:36 +02001175 ieee80211_sta_reset_beacon_monitor(sdata);
1176 ieee80211_sta_reset_conn_monitor(sdata);
1177
Michal Kazior59af6922014-04-09 15:10:59 +02001178out:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001179 mutex_unlock(&local->chanctx_mtx);
1180 mutex_unlock(&local->mtx);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001181 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001182}
1183
Luciano Coelho0c21e632014-10-08 09:48:39 +03001184static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1185{
1186 struct ieee80211_local *local = sdata->local;
1187 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1188 int ret;
1189
1190 sdata_assert_lock(sdata);
1191
1192 WARN_ON(!sdata->vif.csa_active);
1193
1194 if (sdata->csa_block_tx) {
1195 ieee80211_wake_vif_queues(local, sdata,
1196 IEEE80211_QUEUE_STOP_REASON_CSA);
1197 sdata->csa_block_tx = false;
1198 }
1199
1200 sdata->vif.csa_active = false;
1201 ifmgd->csa_waiting_bcn = false;
1202
1203 ret = drv_post_channel_switch(sdata);
1204 if (ret) {
1205 sdata_info(sdata,
1206 "driver post channel switch failed, disconnecting\n");
1207 ieee80211_queue_work(&local->hw,
1208 &ifmgd->csa_connection_drop_work);
1209 return;
1210 }
Luciano Coelho688b1ec2014-12-16 16:01:39 +02001211
1212 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
Luciano Coelho0c21e632014-10-08 09:48:39 +03001213}
1214
Johannes Berg5ce6e432010-05-11 16:20:57 +02001215void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1216{
Johannes Berg55de9082012-07-26 17:24:39 +02001217 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1218 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001219
1220 trace_api_chswitch_done(sdata, success);
1221 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001222 sdata_info(sdata,
1223 "driver channel switch failed, disconnecting\n");
1224 ieee80211_queue_work(&sdata->local->hw,
1225 &ifmgd->csa_connection_drop_work);
1226 } else {
1227 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001228 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001229}
1230EXPORT_SYMBOL(ieee80211_chswitch_done);
1231
Kees Cook34f11cd2017-10-16 16:35:49 -07001232static void ieee80211_chswitch_timer(struct timer_list *t)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001233{
1234 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07001235 from_timer(sdata, t, u.mgd.chswitch_timer);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001236
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001237 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001238}
1239
Johannes Berg37799e52013-03-26 14:02:26 +01001240static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001241ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03001242 u64 timestamp, u32 device_timestamp,
1243 struct ieee802_11_elems *elems,
Johannes Berg04a161f2013-05-03 09:35:35 +02001244 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001245{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001246 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001247 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001248 struct cfg80211_bss *cbss = ifmgd->associated;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001249 struct ieee80211_chanctx_conf *conf;
Johannes Berg55de9082012-07-26 17:24:39 +02001250 struct ieee80211_chanctx *chanctx;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001251 enum nl80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001252 struct ieee80211_csa_ie csa_ie;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001253 struct ieee80211_channel_switch ch_switch;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001254 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001255
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001256 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001257
Johannes Berg37799e52013-03-26 14:02:26 +01001258 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001259 return;
1260
Johannes Bergb4f286a12013-03-26 14:13:58 +01001261 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001262 return;
1263
Johannes Berg37799e52013-03-26 14:02:26 +01001264 /* disregard subsequent announcements if we are already processing */
Luciano Coelho3e2a0222014-07-05 11:43:01 +03001265 if (sdata->vif.csa_active)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001266 return;
1267
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001268 current_band = cbss->channel->band;
Luciano Coelho84469a42014-10-28 13:33:04 +02001269 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001270 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001271 ifmgd->associated->bssid, &csa_ie);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001272 if (res < 0)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001273 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001274 &ifmgd->csa_connection_drop_work);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001275 if (res)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001276 return;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001277
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001278 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001279 IEEE80211_CHAN_DISABLED)) {
1280 sdata_info(sdata,
1281 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001282 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001283 csa_ie.chandef.chan->center_freq,
1284 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1285 csa_ie.chandef.center_freq2);
Johannes Berg85220d72013-03-25 18:29:27 +01001286 ieee80211_queue_work(&local->hw,
1287 &ifmgd->csa_connection_drop_work);
1288 return;
1289 }
1290
Johannes Bergf84eaa12015-03-12 08:53:26 +02001291 if (cfg80211_chandef_identical(&csa_ie.chandef,
1292 &sdata->vif.bss_conf.chandef)) {
1293 if (ifmgd->csa_ignored_same_chan)
1294 return;
1295 sdata_info(sdata,
1296 "AP %pM tries to chanswitch to same channel, ignore\n",
1297 ifmgd->associated->bssid);
1298 ifmgd->csa_ignored_same_chan = true;
1299 return;
1300 }
1301
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001302 /*
1303 * Drop all TDLS peers - either we disconnect or move to a different
1304 * channel from this point on. There's no telling what our peer will do.
1305 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1306 * have an incompatible wider chandef.
1307 */
1308 ieee80211_teardown_tdls_peers(sdata);
1309
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001310 mutex_lock(&local->mtx);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001311 mutex_lock(&local->chanctx_mtx);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001312 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1313 lockdep_is_held(&local->chanctx_mtx));
1314 if (!conf) {
1315 sdata_info(sdata,
1316 "no channel context assigned to vif?, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001317 goto drop_connection;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001318 }
1319
1320 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1321
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001322 if (local->use_chanctx &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001323 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001324 sdata_info(sdata,
1325 "driver doesn't support chan-switch with channel contexts\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001326 goto drop_connection;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001327 }
1328
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001329 ch_switch.timestamp = timestamp;
1330 ch_switch.device_timestamp = device_timestamp;
1331 ch_switch.block_tx = csa_ie.mode;
1332 ch_switch.chandef = csa_ie.chandef;
1333 ch_switch.count = csa_ie.count;
1334
1335 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1336 sdata_info(sdata,
1337 "preparing for channel switch failed, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001338 goto drop_connection;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001339 }
1340
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001341 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1342 chanctx->mode, false);
1343 if (res) {
Johannes Berg55de9082012-07-26 17:24:39 +02001344 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001345 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1346 res);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001347 goto drop_connection;
Johannes Berg55de9082012-07-26 17:24:39 +02001348 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001349 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001350
Michal Kazior59af6922014-04-09 15:10:59 +02001351 sdata->vif.csa_active = true;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001352 sdata->csa_chandef = csa_ie.chandef;
Michal Kazior59af6922014-04-09 15:10:59 +02001353 sdata->csa_block_tx = csa_ie.mode;
Johannes Bergf84eaa12015-03-12 08:53:26 +02001354 ifmgd->csa_ignored_same_chan = false;
Michal Kazior59af6922014-04-09 15:10:59 +02001355
1356 if (sdata->csa_block_tx)
Luciano Coelhoa46992b2014-06-13 16:30:07 +03001357 ieee80211_stop_vif_queues(local, sdata,
1358 IEEE80211_QUEUE_STOP_REASON_CSA);
Michal Kazior59af6922014-04-09 15:10:59 +02001359 mutex_unlock(&local->mtx);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001360
Luciano Coelho2f457292014-11-07 14:31:36 +02001361 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1362 csa_ie.count);
1363
Johannes Bergb4f286a12013-03-26 14:13:58 +01001364 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001365 /* use driver's channel switch callback */
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001366 drv_channel_switch(local, sdata, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001367 return;
1368 }
1369
1370 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001371 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001372 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001373 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001374 mod_timer(&ifmgd->chswitch_timer,
Luciano Coelhoff1e4172014-10-28 13:33:05 +02001375 TU_TO_EXP_TIME((csa_ie.count - 1) *
1376 cbss->beacon_interval));
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001377 return;
1378 drop_connection:
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001379 /*
1380 * This is just so that the disconnect flow will know that
1381 * we were trying to switch channel and failed. In case the
1382 * mode is 1 (we are not allowed to Tx), we will know not to
1383 * send a deauthentication frame. Those two fields will be
1384 * reset when the disconnection worker runs.
1385 */
1386 sdata->vif.csa_active = true;
1387 sdata->csa_block_tx = csa_ie.mode;
1388
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001389 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1390 mutex_unlock(&local->chanctx_mtx);
1391 mutex_unlock(&local->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001392}
1393
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001394static bool
1395ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1396 struct ieee80211_channel *channel,
1397 const u8 *country_ie, u8 country_ie_len,
1398 const u8 *pwr_constr_elem,
1399 int *chan_pwr, int *pwr_reduction)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001400{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001401 struct ieee80211_country_ie_triplet *triplet;
1402 int chan = ieee80211_frequency_to_channel(channel->center_freq);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001403 int i, chan_increment;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001404 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001405
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001406 /* Invalid IE */
1407 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001408 return false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001409
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001410 triplet = (void *)(country_ie + 3);
1411 country_ie_len -= 3;
1412
1413 switch (channel->band) {
1414 default:
1415 WARN_ON_ONCE(1);
1416 /* fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +02001417 case NL80211_BAND_2GHZ:
1418 case NL80211_BAND_60GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001419 chan_increment = 1;
1420 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001421 case NL80211_BAND_5GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001422 chan_increment = 4;
1423 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001424 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001425
1426 /* find channel */
1427 while (country_ie_len >= 3) {
1428 u8 first_channel = triplet->chans.first_channel;
1429
1430 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1431 goto next;
1432
1433 for (i = 0; i < triplet->chans.num_channels; i++) {
1434 if (first_channel + i * chan_increment == chan) {
1435 have_chan_pwr = true;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001436 *chan_pwr = triplet->chans.max_power;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001437 break;
1438 }
1439 }
1440 if (have_chan_pwr)
1441 break;
1442
1443 next:
1444 triplet++;
1445 country_ie_len -= 3;
1446 }
1447
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001448 if (have_chan_pwr && pwr_constr_elem)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001449 *pwr_reduction = *pwr_constr_elem;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001450 else
1451 *pwr_reduction = 0;
1452
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001453 return have_chan_pwr;
1454}
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001455
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001456static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1457 struct ieee80211_channel *channel,
1458 const u8 *cisco_dtpc_ie,
1459 int *pwr_level)
1460{
1461 /* From practical testing, the first data byte of the DTPC element
1462 * seems to contain the requested dBm level, and the CLI on Cisco
1463 * APs clearly state the range is -127 to 127 dBm, which indicates
1464 * a signed byte, although it seemingly never actually goes negative.
1465 * The other byte seems to always be zero.
1466 */
1467 *pwr_level = (__s8)cisco_dtpc_ie[4];
1468}
1469
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001470static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1471 struct ieee80211_channel *channel,
1472 struct ieee80211_mgmt *mgmt,
1473 const u8 *country_ie, u8 country_ie_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001474 const u8 *pwr_constr_ie,
1475 const u8 *cisco_dtpc_ie)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001476{
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001477 bool has_80211h_pwr = false, has_cisco_pwr = false;
1478 int chan_pwr = 0, pwr_reduction_80211h = 0;
1479 int pwr_level_cisco, pwr_level_80211h;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001480 int new_ap_level;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001481 __le16 capab = mgmt->u.probe_resp.capab_info;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001482
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001483 if (country_ie &&
1484 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1485 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001486 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1487 sdata, channel, country_ie, country_ie_len,
1488 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001489 pwr_level_80211h =
1490 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001491 }
1492
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001493 if (cisco_dtpc_ie) {
1494 ieee80211_find_cisco_dtpc(
1495 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1496 has_cisco_pwr = true;
1497 }
1498
1499 if (!has_80211h_pwr && !has_cisco_pwr)
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001500 return 0;
1501
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001502 /* If we have both 802.11h and Cisco DTPC, apply both limits
1503 * by picking the smallest of the two power levels advertised.
1504 */
1505 if (has_80211h_pwr &&
1506 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
Johannes Berga87da0c2015-12-08 16:04:37 +02001507 new_ap_level = pwr_level_80211h;
1508
1509 if (sdata->ap_power_level == new_ap_level)
1510 return 0;
1511
John Linvillecef2fc12015-03-31 10:49:14 -04001512 sdata_dbg(sdata,
1513 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1514 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1515 sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001516 } else { /* has_cisco_pwr is always true here. */
Johannes Berga87da0c2015-12-08 16:04:37 +02001517 new_ap_level = pwr_level_cisco;
1518
1519 if (sdata->ap_power_level == new_ap_level)
1520 return 0;
1521
John Linvillecef2fc12015-03-31 10:49:14 -04001522 sdata_dbg(sdata,
1523 "Limiting TX power to %d dBm as advertised by %pM\n",
1524 pwr_level_cisco, sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001525 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001526
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001527 sdata->ap_power_level = new_ap_level;
1528 if (__ieee80211_recalc_txpower(sdata))
1529 return BSS_CHANGED_TXPOWER;
1530 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001531}
1532
Johannes Berg965beda2009-04-16 13:17:24 +02001533/* powersave */
1534static void ieee80211_enable_ps(struct ieee80211_local *local,
1535 struct ieee80211_sub_if_data *sdata)
1536{
1537 struct ieee80211_conf *conf = &local->hw.conf;
1538
Johannes Bergd5edaed2009-04-22 23:02:51 +02001539 /*
1540 * If we are scanning right now then the parameters will
1541 * take effect when scan finishes.
1542 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001543 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001544 return;
1545
Johannes Berg965beda2009-04-16 13:17:24 +02001546 if (conf->dynamic_ps_timeout > 0 &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001547 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001548 mod_timer(&local->dynamic_ps_timer, jiffies +
1549 msecs_to_jiffies(conf->dynamic_ps_timeout));
1550 } else {
Johannes Berg30686bf2015-06-02 21:39:54 +02001551 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
Johannes Berg076cdcb2015-09-24 16:14:55 +02001552 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301553
Johannes Berg30686bf2015-06-02 21:39:54 +02001554 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1555 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001556 return;
1557
1558 conf->flags |= IEEE80211_CONF_PS;
1559 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001560 }
1561}
1562
1563static void ieee80211_change_ps(struct ieee80211_local *local)
1564{
1565 struct ieee80211_conf *conf = &local->hw.conf;
1566
1567 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001568 ieee80211_enable_ps(local, local->ps_sdata);
1569 } else if (conf->flags & IEEE80211_CONF_PS) {
1570 conf->flags &= ~IEEE80211_CONF_PS;
1571 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1572 del_timer_sync(&local->dynamic_ps_timer);
1573 cancel_work_sync(&local->dynamic_ps_enable_work);
1574 }
1575}
1576
Jason Young808118c2011-03-10 16:43:19 -08001577static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1578{
1579 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1580 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001581 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001582
1583 if (!mgd->powersave)
1584 return false;
1585
Johannes Berg05cb9102011-10-28 11:59:47 +02001586 if (mgd->broken_ap)
1587 return false;
1588
Jason Young808118c2011-03-10 16:43:19 -08001589 if (!mgd->associated)
1590 return false;
1591
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001592 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001593 return false;
1594
Alexander Bondar989c6502013-05-16 17:34:17 +03001595 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001596 return false;
1597
Jason Young808118c2011-03-10 16:43:19 -08001598 rcu_read_lock();
1599 sta = sta_info_get(sdata, mgd->bssid);
1600 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001601 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001602 rcu_read_unlock();
1603
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001604 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001605}
1606
Johannes Berg965beda2009-04-16 13:17:24 +02001607/* need to hold RTNL or interface lock */
Johannes Berg4a733ef2015-10-14 18:02:43 +02001608void ieee80211_recalc_ps(struct ieee80211_local *local)
Johannes Berg965beda2009-04-16 13:17:24 +02001609{
1610 struct ieee80211_sub_if_data *sdata, *found = NULL;
1611 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001612 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001613
Johannes Berg30686bf2015-06-02 21:39:54 +02001614 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001615 local->ps_sdata = NULL;
1616 return;
1617 }
1618
1619 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001620 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001621 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301622 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1623 /* If an AP vif is found, then disable PS
1624 * by setting the count to zero thereby setting
1625 * ps_sdata to NULL.
1626 */
1627 count = 0;
1628 break;
1629 }
Johannes Berg965beda2009-04-16 13:17:24 +02001630 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1631 continue;
1632 found = sdata;
1633 count++;
1634 }
1635
Jason Young808118c2011-03-10 16:43:19 -08001636 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg4a733ef2015-10-14 18:02:43 +02001637 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg10f644a2009-04-16 13:17:25 +02001638
Juuso Oikarinenff616382010-06-09 09:51:52 +03001639 timeout = local->dynamic_ps_forced_timeout;
Johannes Berg4a733ef2015-10-14 18:02:43 +02001640 if (timeout < 0)
1641 timeout = 100;
Johannes Berg09b85562013-02-06 23:07:41 +01001642 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001643
Johannes Berg4a733ef2015-10-14 18:02:43 +02001644 /* If the TIM IE is invalid, pretend the value is 1 */
1645 if (!dtimper)
1646 dtimper = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001647
Johannes Berg4a733ef2015-10-14 18:02:43 +02001648 local->hw.conf.ps_dtim_period = dtimper;
1649 local->ps_sdata = found;
Johannes Berg10f644a2009-04-16 13:17:25 +02001650 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001651 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001652 }
Johannes Berg965beda2009-04-16 13:17:24 +02001653
1654 ieee80211_change_ps(local);
1655}
1656
Eliad Pellerab095872012-07-27 12:33:22 +03001657void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1658{
1659 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1660
1661 if (sdata->vif.bss_conf.ps != ps_allowed) {
1662 sdata->vif.bss_conf.ps = ps_allowed;
1663 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1664 }
1665}
1666
Johannes Berg965beda2009-04-16 13:17:24 +02001667void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1668{
1669 struct ieee80211_local *local =
1670 container_of(work, struct ieee80211_local,
1671 dynamic_ps_disable_work);
1672
1673 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1674 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1675 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1676 }
1677
1678 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001679 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +03001680 IEEE80211_QUEUE_STOP_REASON_PS,
1681 false);
Johannes Berg965beda2009-04-16 13:17:24 +02001682}
1683
1684void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1685{
1686 struct ieee80211_local *local =
1687 container_of(work, struct ieee80211_local,
1688 dynamic_ps_enable_work);
1689 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001690 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301691 unsigned long flags;
1692 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001693
1694 /* can only happen when PS was just disabled anyway */
1695 if (!sdata)
1696 return;
1697
Christian Lamparter5e340692011-06-30 21:08:43 +02001698 ifmgd = &sdata->u.mgd;
1699
Johannes Berg965beda2009-04-16 13:17:24 +02001700 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1701 return;
1702
Johannes Berg09b85562013-02-06 23:07:41 +01001703 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001704 /* don't enter PS if TX frames are pending */
1705 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301706 mod_timer(&local->dynamic_ps_timer, jiffies +
1707 msecs_to_jiffies(
1708 local->hw.conf.dynamic_ps_timeout));
1709 return;
1710 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001711
1712 /*
1713 * transmission can be stopped by others which leads to
1714 * dynamic_ps_timer expiry. Postpone the ps timer if it
1715 * is not the actual idle state.
1716 */
1717 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1718 for (q = 0; q < local->hw.queues; q++) {
1719 if (local->queue_stop_reasons[q]) {
1720 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1721 flags);
1722 mod_timer(&local->dynamic_ps_timer, jiffies +
1723 msecs_to_jiffies(
1724 local->hw.conf.dynamic_ps_timeout));
1725 return;
1726 }
1727 }
1728 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301729 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301730
Johannes Berg30686bf2015-06-02 21:39:54 +02001731 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301732 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga15983832013-03-26 22:02:42 +01001733 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301734 mod_timer(&local->dynamic_ps_timer, jiffies +
1735 msecs_to_jiffies(
1736 local->hw.conf.dynamic_ps_timeout));
Johannes Berga15983832013-03-26 22:02:42 +01001737 } else {
Johannes Berg076cdcb2015-09-24 16:14:55 +02001738 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301739 /* Flush to get the tx status of nullfunc frame */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001740 ieee80211_flush_queues(local, sdata, false);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301741 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301742 }
1743
Johannes Berg30686bf2015-06-02 21:39:54 +02001744 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1745 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301746 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1747 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1748 local->hw.conf.flags |= IEEE80211_CONF_PS;
1749 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1750 }
Johannes Berg965beda2009-04-16 13:17:24 +02001751}
1752
Kees Cook34f11cd2017-10-16 16:35:49 -07001753void ieee80211_dynamic_ps_timer(struct timer_list *t)
Johannes Berg965beda2009-04-16 13:17:24 +02001754{
Kees Cook34f11cd2017-10-16 16:35:49 -07001755 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
Johannes Berg965beda2009-04-16 13:17:24 +02001756
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001757 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001758}
1759
Simon Wunderlich164eb022013-02-08 18:16:20 +01001760void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1761{
Geliang Tanga85a7e22016-01-01 23:48:52 +08001762 struct delayed_work *delayed_work = to_delayed_work(work);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001763 struct ieee80211_sub_if_data *sdata =
1764 container_of(delayed_work, struct ieee80211_sub_if_data,
1765 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001766 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001767
Johannes Berg34a37402013-12-18 09:43:33 +01001768 mutex_lock(&sdata->local->mtx);
1769 if (sdata->wdev.cac_started) {
1770 ieee80211_vif_release_channel(sdata);
1771 cfg80211_cac_event(sdata->dev, &chandef,
1772 NL80211_RADAR_CAC_FINISHED,
1773 GFP_KERNEL);
1774 }
1775 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001776}
1777
Johannes Berg02219b32014-10-07 10:38:50 +03001778static bool
1779__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1780{
1781 struct ieee80211_local *local = sdata->local;
1782 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
John Linvillecc72f6e2015-01-06 14:39:33 -05001783 bool ret = false;
Johannes Berg02219b32014-10-07 10:38:50 +03001784 int ac;
1785
1786 if (local->hw.queues < IEEE80211_NUM_ACS)
1787 return false;
1788
1789 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1790 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1791 int non_acm_ac;
1792 unsigned long now = jiffies;
1793
1794 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1795 tx_tspec->admitted_time &&
1796 time_after(now, tx_tspec->time_slice_start + HZ)) {
1797 tx_tspec->consumed_tx_time = 0;
1798 tx_tspec->time_slice_start = now;
1799
1800 if (tx_tspec->downgraded)
1801 tx_tspec->action =
1802 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1803 }
1804
1805 switch (tx_tspec->action) {
1806 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1807 /* take the original parameters */
1808 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1809 sdata_err(sdata,
1810 "failed to set TX queue parameters for queue %d\n",
1811 ac);
1812 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1813 tx_tspec->downgraded = false;
1814 ret = true;
1815 break;
1816 case TX_TSPEC_ACTION_DOWNGRADE:
1817 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1818 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1819 ret = true;
1820 break;
1821 }
1822 /* downgrade next lower non-ACM AC */
1823 for (non_acm_ac = ac + 1;
1824 non_acm_ac < IEEE80211_NUM_ACS;
1825 non_acm_ac++)
1826 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1827 break;
Johannes Berg93db1d92016-09-14 09:23:51 +02001828 /* Usually the loop will result in using BK even if it
1829 * requires admission control, but such a configuration
1830 * makes no sense and we have to transmit somehow - the
1831 * AC selection does the same thing.
1832 * If we started out trying to downgrade from BK, then
1833 * the extra condition here might be needed.
Johannes Berg02219b32014-10-07 10:38:50 +03001834 */
Johannes Berg93db1d92016-09-14 09:23:51 +02001835 if (non_acm_ac >= IEEE80211_NUM_ACS)
1836 non_acm_ac = IEEE80211_AC_BK;
Johannes Berg02219b32014-10-07 10:38:50 +03001837 if (drv_conf_tx(local, sdata, ac,
1838 &sdata->tx_conf[non_acm_ac]))
1839 sdata_err(sdata,
1840 "failed to set TX queue parameters for queue %d\n",
1841 ac);
1842 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1843 ret = true;
1844 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1845 tx_tspec->time_slice_start + HZ - now + 1);
1846 break;
1847 case TX_TSPEC_ACTION_NONE:
1848 /* nothing now */
1849 break;
1850 }
1851 }
1852
1853 return ret;
1854}
1855
1856void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1857{
1858 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1859 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1860}
1861
1862static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1863{
1864 struct ieee80211_sub_if_data *sdata;
1865
1866 sdata = container_of(work, struct ieee80211_sub_if_data,
1867 u.mgd.tx_tspec_wk.work);
1868 ieee80211_sta_handle_tspec_ac_params(sdata);
1869}
1870
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001871/* MLME */
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001872static bool
1873ieee80211_sta_wmm_params(struct ieee80211_local *local,
1874 struct ieee80211_sub_if_data *sdata,
1875 const u8 *wmm_param, size_t wmm_param_len,
1876 const struct ieee80211_mu_edca_param_set *mu_edca)
Jiri Bencf0706e82007-05-05 11:45:53 -07001877{
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001878 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
Johannes Berg4ced3f72010-07-19 16:39:04 +02001879 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001880 size_t left;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001881 int count, mu_edca_count, ac;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001882 const u8 *pos;
1883 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001884
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001885 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001886 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001887
Johannes Berg32c50572012-03-28 11:04:29 +02001888 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001889 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001890
1891 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001892 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001893
Jiri Bencf0706e82007-05-05 11:45:53 -07001894 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001895 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001896
1897 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001898 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001899
Jiri Bencf0706e82007-05-05 11:45:53 -07001900 count = wmm_param[6] & 0x0f;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001901 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
1902 * if mu_edca was preset before and now it disappeared tell
1903 * the driver about it.
1904 */
1905 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
1906 if (count == ifmgd->wmm_last_param_set &&
1907 mu_edca_count == ifmgd->mu_edca_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001908 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001909 ifmgd->wmm_last_param_set = count;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02001910 ifmgd->mu_edca_last_param_set = mu_edca_count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001911
1912 pos = wmm_param + 8;
1913 left = wmm_param_len - 8;
1914
1915 memset(&params, 0, sizeof(params));
1916
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001917 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001918 for (; left >= 4; left -= 4, pos += 4) {
1919 int aci = (pos[0] >> 5) & 0x03;
1920 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001921 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001922
1923 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001924 case 1: /* AC_BK */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001925 ac = IEEE80211_AC_BK;
Johannes Berg988c0f72008-04-17 19:21:22 +02001926 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001927 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001928 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1929 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001930 params[ac].mu_edca = !!mu_edca;
1931 if (mu_edca)
1932 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
Jiri Bencf0706e82007-05-05 11:45:53 -07001933 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001934 case 2: /* AC_VI */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001935 ac = IEEE80211_AC_VI;
Johannes Berg988c0f72008-04-17 19:21:22 +02001936 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001937 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001938 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1939 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001940 params[ac].mu_edca = !!mu_edca;
1941 if (mu_edca)
1942 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
Jiri Bencf0706e82007-05-05 11:45:53 -07001943 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001944 case 3: /* AC_VO */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001945 ac = IEEE80211_AC_VO;
Johannes Berg988c0f72008-04-17 19:21:22 +02001946 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001947 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001948 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1949 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001950 params[ac].mu_edca = !!mu_edca;
1951 if (mu_edca)
1952 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
Jiri Bencf0706e82007-05-05 11:45:53 -07001953 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001954 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001955 default:
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001956 ac = IEEE80211_AC_BE;
Johannes Berg988c0f72008-04-17 19:21:22 +02001957 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001958 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001959 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1960 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001961 params[ac].mu_edca = !!mu_edca;
1962 if (mu_edca)
1963 params[ac].mu_edca_param_rec = mu_edca->ac_be;
Jiri Bencf0706e82007-05-05 11:45:53 -07001964 break;
1965 }
1966
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001967 params[ac].aifs = pos[0] & 0x0f;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02001968
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001969 if (params[ac].aifs < 2) {
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02001970 sdata_info(sdata,
1971 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001972 params[ac].aifs, aci);
1973 params[ac].aifs = 2;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02001974 }
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001975 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1976 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
1977 params[ac].txop = get_unaligned_le16(pos + 2);
1978 params[ac].acm = acm;
1979 params[ac].uapsd = uapsd;
Kalle Valoab133152010-01-12 10:42:31 +02001980
Ilan Peer911a2642018-04-03 11:35:22 +03001981 if (params[ac].cw_min == 0 ||
Emmanuel Grumbachc470bdc2018-03-26 16:21:04 +03001982 params[ac].cw_min > params[ac].cw_max) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001983 sdata_info(sdata,
1984 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
1985 params[ac].cw_min, params[ac].cw_max, aci);
1986 return false;
1987 }
Haim Dreyfusse552af02018-03-28 13:24:10 +03001988 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001989 }
1990
1991 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001992 mlme_dbg(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02001993 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
1994 ac, params[ac].acm,
1995 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
1996 params[ac].txop, params[ac].uapsd,
1997 ifmgd->tx_tspec[ac].downgraded);
1998 sdata->tx_conf[ac] = params[ac];
1999 if (!ifmgd->tx_tspec[ac].downgraded &&
2000 drv_conf_tx(local, sdata, ac, &params[ac]))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002001 sdata_err(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002002 "failed to set TX queue parameters for AC %d\n",
2003 ac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002004 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002005
2006 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02002007 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02002008 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002009}
2010
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002011static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2012{
2013 lockdep_assert_held(&sdata->local->mtx);
2014
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002015 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002016 ieee80211_run_deferred_scan(sdata->local);
2017}
2018
2019static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2020{
2021 mutex_lock(&sdata->local->mtx);
2022 __ieee80211_stop_poll(sdata);
2023 mutex_unlock(&sdata->local->mtx);
2024}
2025
Johannes Berg7a5158e2008-10-08 10:59:33 +02002026static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2027 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02002028{
Johannes Bergbda39332008-10-11 01:51:51 +02002029 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302030 struct ieee80211_supported_band *sband;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002031 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002032 bool use_protection;
2033 bool use_short_preamble;
2034 bool use_short_slot;
2035
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302036 sband = ieee80211_get_sband(sdata);
2037 if (!sband)
2038 return changed;
2039
Johannes Berg7a5158e2008-10-08 10:59:33 +02002040 if (erp_valid) {
2041 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2042 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2043 } else {
2044 use_protection = false;
2045 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2046 }
2047
2048 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302049 if (sband->band == NL80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01002050 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02002051
Johannes Berg471b3ef2007-12-28 14:32:58 +01002052 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01002053 bss_conf->use_cts_prot = use_protection;
2054 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02002055 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02002056
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002057 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002058 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002059 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02002060 }
Daniel Draked9430a32007-07-27 15:43:24 +02002061
Johannes Berg7a5158e2008-10-08 10:59:33 +02002062 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02002063 bss_conf->use_short_slot = use_short_slot;
2064 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04002065 }
2066
2067 return changed;
2068}
2069
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002070static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002071 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02002072 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07002073{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002074 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002075 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002076 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002077
Johannes Bergae5eb022008-10-14 16:58:37 +02002078 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002079 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03002080 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002081
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002082 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07002083 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002084
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002085 sdata->u.mgd.associated = cbss;
2086 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01002087
Johannes Berge8e4f522017-03-08 11:12:10 +01002088 ieee80211_check_rate_mask(sdata);
2089
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002090 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2091
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01002092 if (sdata->vif.p2p ||
2093 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002094 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002095
Johannes Berg9caf0362012-11-29 01:25:20 +01002096 rcu_read_lock();
2097 ies = rcu_dereference(cbss->ies);
2098 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002099 int ret;
2100
2101 ret = cfg80211_get_p2p_attr(
2102 ies->data, ies->len,
2103 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002104 (u8 *) &bss_conf->p2p_noa_attr,
2105 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01002106 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002107 sdata->u.mgd.p2p_noa_index =
2108 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01002109 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01002110 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01002111 }
Johannes Berg9caf0362012-11-29 01:25:20 +01002112 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01002113 }
2114
Johannes Bergb291ba12009-07-10 15:29:03 +02002115 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002116 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002117
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002118 ieee80211_led_assoc(local, 1);
2119
Alexander Bondar989c6502013-05-16 17:34:17 +03002120 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02002121 /*
2122 * If the AP is buggy we may get here with no DTIM period
2123 * known, so assume it's 1 which is the only safe assumption
2124 * in that case, although if the TIM IE is broken powersave
2125 * probably just won't work at all.
2126 */
2127 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03002128 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03002129 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02002130 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03002131 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02002132 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02002133 }
Johannes Berge5b900d2010-07-29 16:08:55 +02002134
Juuso Oikarinen68542962010-06-09 13:43:26 +03002135 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02002136
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002137 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01002138 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03002139 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002140 bss_info_changed |= BSS_CHANGED_CQM;
2141
Juuso Oikarinen68542962010-06-09 13:43:26 +03002142 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002143 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002144 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002145
Johannes Bergae5eb022008-10-14 16:58:37 +02002146 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03002147
Johannes Berg056508d2009-07-30 21:43:55 +02002148 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002149 ieee80211_recalc_ps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02002150 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02002151
Johannes Berg04ecd252012-09-11 14:34:12 +02002152 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03002153 ieee80211_recalc_ps_vif(sdata);
2154
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002155 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002156}
2157
Jouni Malinene69e95d2010-03-29 23:29:31 -07002158static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01002159 u16 stype, u16 reason, bool tx,
2160 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03002161{
Johannes Berg46900292009-02-15 12:44:28 +01002162 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002163 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01002164 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002165
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002166 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002167
Johannes Berg37ad3882012-02-24 13:50:54 +01002168 if (WARN_ON_ONCE(tx && !frame_buf))
2169 return;
2170
Johannes Bergb291ba12009-07-10 15:29:03 +02002171 if (WARN_ON(!ifmgd->associated))
2172 return;
2173
David Spinadel79543d82012-06-12 09:59:45 +03002174 ieee80211_stop_poll(sdata);
2175
Johannes Berg77fdaa12009-07-07 03:45:17 +02002176 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002177 netif_carrier_off(sdata->dev);
2178
Eliad Peller88bc40e2012-07-12 17:35:33 +03002179 /*
2180 * if we want to get out of ps before disassoc (why?) we have
2181 * to do it before sending disassoc, as otherwise the null-packet
2182 * won't be valid.
2183 */
2184 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2185 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2186 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2187 }
2188 local->ps_sdata = NULL;
2189
Eliad Pellerab095872012-07-27 12:33:22 +03002190 /* disable per-vif ps */
2191 ieee80211_recalc_ps_vif(sdata);
2192
Emmanuel Grumbach14f2ae82015-01-22 23:30:19 +02002193 /* make sure ongoing transmission finishes */
2194 synchronize_net();
2195
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002196 /*
2197 * drop any frame before deauth/disassoc, this can be data or
2198 * management frame. Since we are disconnecting, we should not
2199 * insist sending these frames which can take time and delay
2200 * the disconnection and possible the roaming.
2201 */
Eliad Pellerf8239812012-06-27 14:18:22 +03002202 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002203 ieee80211_flush_queues(local, sdata, true);
Eliad Pellerf8239812012-06-27 14:18:22 +03002204
Johannes Berg37ad3882012-02-24 13:50:54 +01002205 /* deauthenticate/disassociate now */
Ilan Peer94ba9272018-02-19 14:48:41 +02002206 if (tx || frame_buf) {
Ilan Peer94ba9272018-02-19 14:48:41 +02002207 /*
2208 * In multi channel scenarios guarantee that the virtual
2209 * interface is granted immediate airtime to transmit the
2210 * deauthentication frame by calling mgd_prepare_tx, if the
2211 * driver requested so.
2212 */
2213 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2214 !ifmgd->have_beacon)
Ilan Peerd4e36e52018-04-20 13:49:25 +03002215 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Ilan Peer94ba9272018-02-19 14:48:41 +02002216
Eliad Peller88a9e312012-06-01 11:14:03 +03002217 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
2218 reason, tx, frame_buf);
Ilan Peer94ba9272018-02-19 14:48:41 +02002219 }
Johannes Berg37ad3882012-02-24 13:50:54 +01002220
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002221 /* flush out frame - make sure the deauth was actually sent */
Johannes Berg37ad3882012-02-24 13:50:54 +01002222 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002223 ieee80211_flush_queues(local, sdata, false);
Johannes Berg37ad3882012-02-24 13:50:54 +01002224
Eliad Peller88a9e312012-06-01 11:14:03 +03002225 /* clear bssid only after building the needed mgmt frames */
Joe Perchesc84a67a2015-03-02 19:54:57 -08002226 eth_zero_addr(ifmgd->bssid);
Eliad Peller88a9e312012-06-01 11:14:03 +03002227
Johannes Berg37ad3882012-02-24 13:50:54 +01002228 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01002229 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01002230
2231 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002232 changed |= ieee80211_reset_erp_info(sdata);
2233
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002234 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02002235 changed |= BSS_CHANGED_ASSOC;
2236 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002237
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002238 ifmgd->p2p_noa_index = -1;
2239 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2240 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002241
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002242 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08002243 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2244 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002245 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2246 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002247
2248 /* reset MU-MIMO ownership and group data */
2249 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2250 sizeof(sdata->vif.bss_conf.mu_group.membership));
2251 memset(sdata->vif.bss_conf.mu_group.position, 0,
2252 sizeof(sdata->vif.bss_conf.mu_group.position));
2253 changed |= BSS_CHANGED_MU_GROUPS;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002254 sdata->vif.mu_mimo_owner = false;
Johannes Berg413ad502009-05-08 21:21:06 +02002255
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002256 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302257
Kalle Valo520eb822008-12-18 23:35:27 +02002258 del_timer_sync(&local->dynamic_ps_timer);
2259 cancel_work_sync(&local->dynamic_ps_enable_work);
2260
Juuso Oikarinen68542962010-06-09 13:43:26 +03002261 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002262 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002263 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002264
Johannes Berg3abead52012-03-02 15:56:59 +01002265 sdata->vif.bss_conf.qos = false;
2266 changed |= BSS_CHANGED_QOS;
2267
Johannes Berg0aaffa92010-05-05 15:28:27 +02002268 /* The BSSID (not really interesting) and HT changed */
2269 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02002270 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02002271
Johannes Berg3abead52012-03-02 15:56:59 +01002272 /* disassociated - set to defaults now */
Johannes Bergcec66282015-10-22 17:46:04 +02002273 ieee80211_set_wmm_default(sdata, false, false);
Johannes Berg3abead52012-03-02 15:56:59 +01002274
Johannes Bergb9dcf712010-08-27 12:35:54 +02002275 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2276 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2277 del_timer_sync(&sdata->u.mgd.timer);
2278 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02002279
Johannes Berg826262c2012-12-10 16:38:14 +02002280 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03002281 sdata->vif.bss_conf.beacon_rate = NULL;
2282
Alexander Bondar989c6502013-05-16 17:34:17 +03002283 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02002284
Johannes Berg028e8da02012-11-26 11:57:41 +01002285 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002286 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01002287 ieee80211_vif_release_channel(sdata);
Michal Kazior59af6922014-04-09 15:10:59 +02002288
2289 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002290 ifmgd->csa_waiting_bcn = false;
Johannes Bergf84eaa12015-03-12 08:53:26 +02002291 ifmgd->csa_ignored_same_chan = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002292 if (sdata->csa_block_tx) {
2293 ieee80211_wake_vif_queues(local, sdata,
2294 IEEE80211_QUEUE_STOP_REASON_CSA);
2295 sdata->csa_block_tx = false;
2296 }
Johannes Berg34a37402013-12-18 09:43:33 +01002297 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002298
Johannes Berg02219b32014-10-07 10:38:50 +03002299 /* existing TX TSPEC sessions no longer exist */
2300 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2301 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2302
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002303 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002304}
Jiri Bencf0706e82007-05-05 11:45:53 -07002305
Kalle Valo3cf335d52009-03-22 21:57:06 +02002306void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2307 struct ieee80211_hdr *hdr)
2308{
2309 /*
2310 * We can postpone the mgd.timer whenever receiving unicast frames
2311 * from AP because we know that the connection is working both ways
2312 * at that time. But multicast frames (and hence also beacons) must
2313 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02002314 * data idle periods for sending the periodic probe request to the
2315 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02002316 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002317 if (is_multicast_ether_addr(hdr->addr1))
2318 return;
2319
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04002320 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02002321}
Jiri Bencf0706e82007-05-05 11:45:53 -07002322
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002323static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2324{
2325 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002326 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002327
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002328 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002329 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2330 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002331
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002332 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002333
2334 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002335 ieee80211_recalc_ps(local);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002336 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002337
Johannes Berg30686bf2015-06-02 21:39:54 +02002338 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002339 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002340
2341 /*
2342 * We've received a probe response, but are not sure whether
2343 * we have or will be receiving any beacons or data, so let's
2344 * schedule the timers again, just in case.
2345 */
2346 ieee80211_sta_reset_beacon_monitor(sdata);
2347
2348 mod_timer(&ifmgd->conn_mon_timer,
2349 round_jiffies_up(jiffies +
2350 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002351out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002352 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002353}
2354
Johannes Berg02219b32014-10-07 10:38:50 +03002355static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2356 struct ieee80211_hdr *hdr,
2357 u16 tx_time)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002358{
Johannes Berg02219b32014-10-07 10:38:50 +03002359 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02002360 u16 tid = ieee80211_get_tid(hdr);
Johannes Berg02219b32014-10-07 10:38:50 +03002361 int ac = ieee80211_ac_from_tid(tid);
2362 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2363 unsigned long now = jiffies;
2364
2365 if (likely(!tx_tspec->admitted_time))
2366 return;
2367
2368 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2369 tx_tspec->consumed_tx_time = 0;
2370 tx_tspec->time_slice_start = now;
2371
2372 if (tx_tspec->downgraded) {
2373 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2374 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2375 }
2376 }
2377
2378 if (tx_tspec->downgraded)
2379 return;
2380
2381 tx_tspec->consumed_tx_time += tx_time;
2382
2383 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2384 tx_tspec->downgraded = true;
2385 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2386 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2387 }
2388}
2389
2390void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2391 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2392{
2393 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2394
Felix Fietkau75706d02010-12-02 21:01:07 +01002395 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002396 return;
2397
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002398 if (ieee80211_is_nullfunc(hdr->frame_control) &&
2399 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002400 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002401 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002402 else
2403 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002404 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002405 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002406 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002407
2408 if (ack)
2409 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002410}
2411
Johannes Berg45ad6832018-05-28 15:47:39 +02002412static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2413 const u8 *src, const u8 *dst,
2414 const u8 *ssid, size_t ssid_len,
2415 struct ieee80211_channel *channel)
2416{
2417 struct sk_buff *skb;
2418
2419 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2420 ssid, ssid_len, NULL, 0,
2421 IEEE80211_PROBE_FLAG_DIRECTED);
2422 if (skb)
2423 ieee80211_tx_skb(sdata, skb);
2424}
2425
Maxim Levitskya43abf22009-07-31 18:54:12 +03002426static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2427{
2428 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2429 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002430 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002431 u8 unicast_limit = max(1, max_probe_tries - 3);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002432 struct sta_info *sta;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002433
2434 /*
2435 * Try sending broadcast probe requests for the last three
2436 * probe requests after the first ones failed since some
2437 * buggy APs only support broadcast probe requests.
2438 */
2439 if (ifmgd->probe_send_count >= unicast_limit)
2440 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002441
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002442 /*
2443 * When the hardware reports an accurate Tx ACK status, it's
2444 * better to send a nullfunc frame instead of a probe request,
2445 * as it will kick us off the AP quickly if we aren't associated
2446 * anymore. The timeout will be reset if the frame is ACKed by
2447 * the AP.
2448 */
Soumik Das992e68b2012-05-20 15:31:13 +05302449 ifmgd->probe_send_count++;
2450
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002451 if (dst) {
2452 mutex_lock(&sdata->local->sta_mtx);
2453 sta = sta_info_get(sdata, dst);
2454 if (!WARN_ON(!sta))
2455 ieee80211_check_fast_rx(sta);
2456 mutex_unlock(&sdata->local->sta_mtx);
2457 }
2458
Johannes Berg30686bf2015-06-02 21:39:54 +02002459 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002460 ifmgd->nullfunc_failed = false;
Johannes Berg076cdcb2015-09-24 16:14:55 +02002461 ieee80211_send_nullfunc(sdata->local, sdata, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002462 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002463 int ssid_len;
2464
Johannes Berg9caf0362012-11-29 01:25:20 +01002465 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002466 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002467 if (WARN_ON_ONCE(ssid == NULL))
2468 ssid_len = 0;
2469 else
2470 ssid_len = ssid[1];
2471
Johannes Berg45ad6832018-05-28 15:47:39 +02002472 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2473 ssid + 2, ssid_len,
2474 ifmgd->associated->channel);
Johannes Berg9caf0362012-11-29 01:25:20 +01002475 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002476 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002477
Ben Greear180205b2011-02-04 15:30:24 -08002478 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002479 run_again(sdata, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002480}
2481
Johannes Bergb291ba12009-07-10 15:29:03 +02002482static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2483 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002484{
Kalle Valo04de8382009-03-22 21:57:35 +02002485 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002486 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002487
Johannes Berg9607e6b662009-12-23 13:15:31 +01002488 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002489 return;
2490
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002491 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002492
2493 if (!ifmgd->associated)
2494 goto out;
2495
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002496 mutex_lock(&sdata->local->mtx);
2497
2498 if (sdata->local->tmp_channel || sdata->local->scanning) {
2499 mutex_unlock(&sdata->local->mtx);
2500 goto out;
2501 }
2502
Ben Greeara13fbe52013-03-25 11:19:35 -07002503 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002504 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002505 "detected beacon loss from AP (missed %d beacons) - probing\n",
2506 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002507
Johannes Berg98f03342014-11-26 12:42:02 +01002508 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
Ben Greeara13fbe52013-03-25 11:19:35 -07002509 }
Kalle Valo04de8382009-03-22 21:57:35 +02002510
Johannes Bergb291ba12009-07-10 15:29:03 +02002511 /*
2512 * The driver/our work has already reported this event or the
2513 * connection monitoring has kicked in and we have already sent
2514 * a probe request. Or maybe the AP died and the driver keeps
2515 * reporting until we disassociate...
2516 *
2517 * In either case we have to ignore the current call to this
2518 * function (except for setting the correct probe reason bit)
2519 * because otherwise we would reset the timer every time and
2520 * never check whether we received a probe response!
2521 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002522 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02002523 already = true;
2524
Eliad Peller12b5f342013-11-18 19:06:46 +02002525 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2526
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002527 mutex_unlock(&sdata->local->mtx);
2528
Johannes Bergb291ba12009-07-10 15:29:03 +02002529 if (already)
2530 goto out;
2531
Johannes Berg4e751842009-06-10 15:16:52 +02002532 mutex_lock(&sdata->local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002533 ieee80211_recalc_ps(sdata->local);
Johannes Berg4e751842009-06-10 15:16:52 +02002534 mutex_unlock(&sdata->local->iflist_mtx);
2535
Maxim Levitskya43abf22009-07-31 18:54:12 +03002536 ifmgd->probe_send_count = 0;
2537 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002538 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002539 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002540}
2541
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002542struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2543 struct ieee80211_vif *vif)
2544{
2545 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2546 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002547 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002548 struct sk_buff *skb;
2549 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002550 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002551
2552 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2553 return NULL;
2554
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002555 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002556
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002557 if (ifmgd->associated)
2558 cbss = ifmgd->associated;
2559 else if (ifmgd->auth_data)
2560 cbss = ifmgd->auth_data->bss;
2561 else if (ifmgd->assoc_data)
2562 cbss = ifmgd->assoc_data->bss;
2563 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002564 return NULL;
2565
Johannes Berg9caf0362012-11-29 01:25:20 +01002566 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002567 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002568 if (WARN_ON_ONCE(ssid == NULL))
2569 ssid_len = 0;
2570 else
2571 ssid_len = ssid[1];
2572
Johannes Berga344d672014-06-12 22:24:31 +02002573 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002574 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002575 ssid + 2, ssid_len,
Johannes Berg00387f32018-05-28 15:47:38 +02002576 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
Johannes Berg9caf0362012-11-29 01:25:20 +01002577 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002578
2579 return skb;
2580}
2581EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2582
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002583static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2584 const u8 *buf, size_t len, bool tx,
2585 u16 reason)
2586{
2587 struct ieee80211_event event = {
2588 .type = MLME_EVENT,
2589 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2590 .u.mlme.reason = reason,
2591 };
2592
2593 if (tx)
2594 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2595 else
2596 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2597
2598 drv_event_callback(sdata->local, sdata, &event);
2599}
2600
Johannes Bergeef9e542013-01-29 13:09:34 +01002601static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002602{
Michal Kazior59af6922014-04-09 15:10:59 +02002603 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002604 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002605 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002606 bool tx;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002607
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002608 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002609 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002610 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002611 return;
2612 }
2613
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002614 tx = !sdata->csa_block_tx;
2615
David Spinadel20eb7ea2016-05-03 16:05:02 +03002616 /* AP is probably out of range (or not reachable for another reason) so
2617 * remove the bss struct for that AP.
2618 */
2619 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2620
Johannes Berg37ad3882012-02-24 13:50:54 +01002621 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2622 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002623 tx, frame_buf);
Michal Kazior59af6922014-04-09 15:10:59 +02002624 mutex_lock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03002625 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002626 ifmgd->csa_waiting_bcn = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002627 if (sdata->csa_block_tx) {
2628 ieee80211_wake_vif_queues(local, sdata,
2629 IEEE80211_QUEUE_STOP_REASON_CSA);
2630 sdata->csa_block_tx = false;
2631 }
Michal Kazior59af6922014-04-09 15:10:59 +02002632 mutex_unlock(&local->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002633
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002634 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002635 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2636
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002637 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002638}
2639
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002640static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002641{
2642 struct ieee80211_sub_if_data *sdata =
2643 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002644 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002645 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Paul Stewarta85e1d52011-12-09 11:01:49 -08002646
Johannes Berg976bd9e2015-10-16 17:18:11 +02002647 if (ifmgd->associated)
2648 ifmgd->beacon_loss_count++;
Johannes Bergb291ba12009-07-10 15:29:03 +02002649
Johannes Berg682bd382013-01-29 13:13:50 +01002650 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002651 sdata_info(sdata, "Connection to AP %pM lost\n",
2652 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002653 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002654 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002655 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002656 }
2657}
2658
2659static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2660{
2661 struct ieee80211_sub_if_data *sdata =
2662 container_of(work, struct ieee80211_sub_if_data,
2663 u.mgd.csa_connection_drop_work);
2664
Johannes Bergeef9e542013-01-29 13:09:34 +01002665 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002666}
2667
Kalle Valo04de8382009-03-22 21:57:35 +02002668void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2669{
2670 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002671 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002672
Johannes Bergb5878a22010-04-07 16:48:40 +02002673 trace_api_beacon_loss(sdata);
2674
Johannes Berg682bd382013-01-29 13:13:50 +01002675 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002676 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002677}
2678EXPORT_SYMBOL(ieee80211_beacon_loss);
2679
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002680void ieee80211_connection_loss(struct ieee80211_vif *vif)
2681{
2682 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2683 struct ieee80211_hw *hw = &sdata->local->hw;
2684
Johannes Bergb5878a22010-04-07 16:48:40 +02002685 trace_api_connection_loss(sdata);
2686
Johannes Berg682bd382013-01-29 13:13:50 +01002687 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002688 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2689}
2690EXPORT_SYMBOL(ieee80211_connection_loss);
2691
2692
Johannes Berg66e67e42012-01-20 13:55:27 +01002693static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2694 bool assoc)
2695{
2696 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2697
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002698 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002699
Johannes Berg66e67e42012-01-20 13:55:27 +01002700 if (!assoc) {
Emmanuel Grumbachc1e140b2015-01-19 16:53:06 +02002701 /*
2702 * we are not authenticated yet, the only timer that could be
2703 * running is the timeout for the authentication response which
2704 * which is not relevant anymore.
2705 */
2706 del_timer_sync(&sdata->u.mgd.timer);
Johannes Berg66e67e42012-01-20 13:55:27 +01002707 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2708
Joe Perchesc84a67a2015-03-02 19:54:57 -08002709 eth_zero_addr(sdata->u.mgd.bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002710 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002711 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002712 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002713 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002714 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002715 }
2716
Johannes Berg5b112d32013-02-01 01:49:58 +01002717 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002718 kfree(auth_data);
2719 sdata->u.mgd.auth_data = NULL;
2720}
2721
Johannes Bergc9c99f82015-06-01 14:10:09 +02002722static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
Johannes Berge6f462d2016-12-08 17:22:09 +01002723 bool assoc, bool abandon)
Johannes Bergc9c99f82015-06-01 14:10:09 +02002724{
2725 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2726
2727 sdata_assert_lock(sdata);
2728
2729 if (!assoc) {
2730 /*
2731 * we are not associated yet, the only timer that could be
2732 * running is the timeout for the association response which
2733 * which is not relevant anymore.
2734 */
2735 del_timer_sync(&sdata->u.mgd.timer);
2736 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2737
2738 eth_zero_addr(sdata->u.mgd.bssid);
2739 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2740 sdata->u.mgd.flags = 0;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002741 sdata->vif.mu_mimo_owner = false;
2742
Johannes Bergc9c99f82015-06-01 14:10:09 +02002743 mutex_lock(&sdata->local->mtx);
2744 ieee80211_vif_release_channel(sdata);
2745 mutex_unlock(&sdata->local->mtx);
Johannes Berge6f462d2016-12-08 17:22:09 +01002746
2747 if (abandon)
2748 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
Johannes Bergc9c99f82015-06-01 14:10:09 +02002749 }
2750
2751 kfree(assoc_data);
2752 sdata->u.mgd.assoc_data = NULL;
2753}
2754
Johannes Berg66e67e42012-01-20 13:55:27 +01002755static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2756 struct ieee80211_mgmt *mgmt, size_t len)
2757{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002758 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002759 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2760 u8 *pos;
2761 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002762 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002763
2764 pos = mgmt->u.auth.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02002765 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2766 mgmt->bssid, auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002767 if (!elems.challenge)
2768 return;
2769 auth_data->expected_transaction = 4;
Ilan Peerd4e36e52018-04-20 13:49:25 +03002770 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg30686bf2015-06-02 21:39:54 +02002771 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01002772 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2773 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002774 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002775 elems.challenge - 2, elems.challenge_len + 2,
2776 auth_data->bss->bssid, auth_data->bss->bssid,
2777 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002778 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002779}
2780
Jouni Malinenfc107a92018-10-11 00:21:19 +03002781static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2782 const u8 *bssid)
2783{
Jouni Malinenefb543e2018-10-11 00:21:21 +03002784 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002785 struct sta_info *sta;
Wei Yongjun33483a62018-10-16 02:35:30 +00002786 bool result = true;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002787
Jouni Malinenefb543e2018-10-11 00:21:21 +03002788 sdata_info(sdata, "authenticated\n");
2789 ifmgd->auth_data->done = true;
2790 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2791 ifmgd->auth_data->timeout_started = true;
2792 run_again(sdata, ifmgd->auth_data->timeout);
2793
Jouni Malinenfc107a92018-10-11 00:21:19 +03002794 /* move station state to auth */
2795 mutex_lock(&sdata->local->sta_mtx);
2796 sta = sta_info_get(sdata, bssid);
2797 if (!sta) {
2798 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002799 result = false;
2800 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002801 }
2802 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2803 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002804 result = false;
2805 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002806 }
Jouni Malinenfc107a92018-10-11 00:21:19 +03002807
Wei Yongjun33483a62018-10-16 02:35:30 +00002808out:
2809 mutex_unlock(&sdata->local->sta_mtx);
2810 return result;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002811}
2812
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002813static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2814 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002815{
2816 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2817 u8 bssid[ETH_ALEN];
2818 u16 auth_alg, auth_transaction, status_code;
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002819 struct ieee80211_event event = {
2820 .type = MLME_EVENT,
2821 .u.mlme.data = AUTH_EVENT,
2822 };
Johannes Berg66e67e42012-01-20 13:55:27 +01002823
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002824 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002825
2826 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002827 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002828
2829 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002830 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002831
2832 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2833
Joe Perchesb203ca32012-05-08 18:56:52 +00002834 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002835 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002836
2837 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2838 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2839 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2840
2841 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinenefb543e2018-10-11 00:21:21 +03002842 (auth_alg != WLAN_AUTH_SAE &&
2843 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2844 (auth_alg == WLAN_AUTH_SAE &&
2845 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2846 auth_transaction > 2))) {
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002847 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2848 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2849 auth_transaction,
2850 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002851 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002852 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002853
2854 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002855 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2856 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002857 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002858 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002859 event.u.mlme.status = MLME_DENIED;
2860 event.u.mlme.reason = status_code;
2861 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002862 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002863 }
2864
2865 switch (ifmgd->auth_data->algorithm) {
2866 case WLAN_AUTH_OPEN:
2867 case WLAN_AUTH_LEAP:
2868 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002869 case WLAN_AUTH_SAE:
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03002870 case WLAN_AUTH_FILS_SK:
2871 case WLAN_AUTH_FILS_SK_PFS:
2872 case WLAN_AUTH_FILS_PK:
Johannes Berg66e67e42012-01-20 13:55:27 +01002873 break;
2874 case WLAN_AUTH_SHARED_KEY:
2875 if (ifmgd->auth_data->expected_transaction != 4) {
2876 ieee80211_auth_challenge(sdata, mgmt, len);
2877 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002878 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002879 }
2880 break;
2881 default:
2882 WARN_ONCE(1, "invalid auth alg %d",
2883 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002884 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002885 }
2886
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002887 event.u.mlme.status = MLME_SUCCESS;
2888 drv_event_callback(sdata->local, sdata, &event);
Jouni Malinenefb543e2018-10-11 00:21:21 +03002889 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
2890 (auth_transaction == 2 &&
2891 ifmgd->auth_data->expected_transaction == 2)) {
2892 if (!ieee80211_mark_sta_auth(sdata, bssid))
2893 goto out_err;
2894 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2895 auth_transaction == 2) {
2896 sdata_info(sdata, "SAE peer confirmed\n");
2897 ifmgd->auth_data->peer_confirmed = true;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002898 }
2899
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002900 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002901 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002902 out_err:
2903 mutex_unlock(&sdata->local->sta_mtx);
2904 /* ignore frame -- wait for timeout */
Johannes Berg66e67e42012-01-20 13:55:27 +01002905}
2906
Calvin Owensdfa1ad22014-02-11 12:36:24 -06002907#define case_WLAN(type) \
2908 case WLAN_REASON_##type: return #type
2909
2910static const char *ieee80211_get_reason_code_string(u16 reason_code)
2911{
2912 switch (reason_code) {
2913 case_WLAN(UNSPECIFIED);
2914 case_WLAN(PREV_AUTH_NOT_VALID);
2915 case_WLAN(DEAUTH_LEAVING);
2916 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2917 case_WLAN(DISASSOC_AP_BUSY);
2918 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2919 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2920 case_WLAN(DISASSOC_STA_HAS_LEFT);
2921 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2922 case_WLAN(DISASSOC_BAD_POWER);
2923 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2924 case_WLAN(INVALID_IE);
2925 case_WLAN(MIC_FAILURE);
2926 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2927 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2928 case_WLAN(IE_DIFFERENT);
2929 case_WLAN(INVALID_GROUP_CIPHER);
2930 case_WLAN(INVALID_PAIRWISE_CIPHER);
2931 case_WLAN(INVALID_AKMP);
2932 case_WLAN(UNSUPP_RSN_VERSION);
2933 case_WLAN(INVALID_RSN_IE_CAP);
2934 case_WLAN(IEEE8021X_FAILED);
2935 case_WLAN(CIPHER_SUITE_REJECTED);
2936 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2937 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2938 case_WLAN(DISASSOC_LOW_ACK);
2939 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
2940 case_WLAN(QSTA_LEAVE_QBSS);
2941 case_WLAN(QSTA_NOT_USE);
2942 case_WLAN(QSTA_REQUIRE_SETUP);
2943 case_WLAN(QSTA_TIMEOUT);
2944 case_WLAN(QSTA_CIPHER_NOT_SUPP);
2945 case_WLAN(MESH_PEER_CANCELED);
2946 case_WLAN(MESH_MAX_PEERS);
2947 case_WLAN(MESH_CONFIG);
2948 case_WLAN(MESH_CLOSE);
2949 case_WLAN(MESH_MAX_RETRIES);
2950 case_WLAN(MESH_CONFIRM_TIMEOUT);
2951 case_WLAN(MESH_INVALID_GTK);
2952 case_WLAN(MESH_INCONSISTENT_PARAM);
2953 case_WLAN(MESH_INVALID_SECURITY);
2954 case_WLAN(MESH_PATH_ERROR);
2955 case_WLAN(MESH_PATH_NOFORWARD);
2956 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
2957 case_WLAN(MAC_EXISTS_IN_MBSS);
2958 case_WLAN(MESH_CHAN_REGULATORY);
2959 case_WLAN(MESH_CHAN);
2960 default: return "<unknown>";
2961 }
2962}
Johannes Berg66e67e42012-01-20 13:55:27 +01002963
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002964static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2965 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002966{
Johannes Berg46900292009-02-15 12:44:28 +01002967 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergc9c99f82015-06-01 14:10:09 +02002968 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002969
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002970 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002971
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002972 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002973 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002974
Johannes Bergc9c99f82015-06-01 14:10:09 +02002975 if (ifmgd->associated &&
2976 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
2977 const u8 *bssid = ifmgd->associated->bssid;
2978
2979 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
2980 bssid, reason_code,
2981 ieee80211_get_reason_code_string(reason_code));
2982
2983 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2984
2985 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
2986 reason_code);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002987 return;
Johannes Bergc9c99f82015-06-01 14:10:09 +02002988 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002989
Johannes Bergc9c99f82015-06-01 14:10:09 +02002990 if (ifmgd->assoc_data &&
2991 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2992 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002993
Johannes Bergc9c99f82015-06-01 14:10:09 +02002994 sdata_info(sdata,
2995 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
2996 bssid, reason_code,
2997 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07002998
Johannes Berge6f462d2016-12-08 17:22:09 +01002999 ieee80211_destroy_assoc_data(sdata, false, true);
Jiri Bencf0706e82007-05-05 11:45:53 -07003000
Johannes Bergc9c99f82015-06-01 14:10:09 +02003001 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3002 return;
3003 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003004}
3005
3006
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003007static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3008 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003009{
Johannes Berg46900292009-02-15 12:44:28 +01003010 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003011 u16 reason_code;
3012
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003013 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003014
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003015 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003016 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003017
Johannes Berg66e67e42012-01-20 13:55:27 +01003018 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00003019 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003020 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003021
3022 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3023
Arkadiusz Miskiewicz68506e92017-02-15 14:21:27 +01003024 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3025 mgmt->sa, reason_code,
3026 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003027
Johannes Berg37ad3882012-02-24 13:50:54 +01003028 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3029
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02003030 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003031}
3032
Christian Lamparterc74d0842011-10-15 00:14:49 +02003033static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3034 u8 *supp_rates, unsigned int supp_rates_len,
3035 u32 *rates, u32 *basic_rates,
3036 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003037 int *min_rate, int *min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03003038 int shift)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003039{
3040 int i, j;
3041
3042 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003043 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003044 bool is_basic = !!(supp_rates[i] & 0x80);
3045
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003046 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003047 *have_higher_than_11mbit = true;
3048
3049 /*
Johannes Berga6289d32017-03-06 22:59:04 +01003050 * Skip HT and VHT BSS membership selectors since they're not
3051 * rates.
Christian Lamparterc74d0842011-10-15 00:14:49 +02003052 *
Johannes Berga6289d32017-03-06 22:59:04 +01003053 * Note: Even though the membership selector and the basic
Christian Lamparterc74d0842011-10-15 00:14:49 +02003054 * rate flag share the same bit, they are not exactly
3055 * the same.
3056 */
Johannes Berga6289d32017-03-06 22:59:04 +01003057 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3058 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
Christian Lamparterc74d0842011-10-15 00:14:49 +02003059 continue;
3060
3061 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003062 struct ieee80211_rate *br;
3063 int brate;
3064
3065 br = &sband->bitrates[j];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003066
3067 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3068 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02003069 *rates |= BIT(j);
3070 if (is_basic)
3071 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003072 if ((rate * 5) < *min_rate) {
3073 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003074 *min_rate_index = j;
3075 }
3076 break;
3077 }
3078 }
3079 }
3080}
Jiri Bencf0706e82007-05-05 11:45:53 -07003081
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003082static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3083 const struct ieee802_11_elems *elems)
3084{
3085 if (elems->ext_capab_len < 10)
3086 return false;
3087
3088 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3089 return false;
3090
3091 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3092 IEEE80211_HE_MAC_CAP0_TWT_RES;
3093}
3094
Johannes Berg66e67e42012-01-20 13:55:27 +01003095static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3096 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01003097 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003098{
Johannes Berg46900292009-02-15 12:44:28 +01003099 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01003100 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01003101 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003102 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003103 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003104 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07003105 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02003106 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02003107 const struct cfg80211_bss_ies *bss_ies = NULL;
3108 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Bergae5eb022008-10-14 16:58:37 +02003109 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003110 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02003111 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003112
Johannes Bergaf6b6372009-12-23 13:15:35 +01003113 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07003114
Jiri Bencf0706e82007-05-05 11:45:53 -07003115 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003116 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07003117
Johannes Bergc7c477b2017-12-01 13:50:51 +02003118 /*
3119 * The 5 MSB of the AID field are reserved
3120 * (802.11-2016 9.4.1.8 AID field)
3121 */
3122 aid &= 0x7ff;
Johannes Berg1dd84aa2007-10-10 12:03:41 +02003123
Johannes Berg05cb9102011-10-28 11:59:47 +02003124 ifmgd->broken_ap = false;
3125
3126 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003127 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3128 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02003129 aid = 0;
3130 ifmgd->broken_ap = true;
3131 }
3132
Johannes Bergaf6b6372009-12-23 13:15:35 +01003133 pos = mgmt->u.assoc_resp.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02003134 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3135 mgmt->bssid, assoc_data->bss->bssid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003136
Jiri Bencf0706e82007-05-05 11:45:53 -07003137 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003138 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01003139 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07003140 }
3141
Johannes Berg46900292009-02-15 12:44:28 +01003142 ifmgd->aid = aid;
Arik Nemtsov9041c1f2014-11-09 18:50:15 +02003143 ifmgd->tdls_chan_switch_prohibited =
3144 elems.ext_capab && elems.ext_capab_len >= 5 &&
3145 (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
Jiri Bencf0706e82007-05-05 11:45:53 -07003146
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003147 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02003148 * Some APs are erroneously not including some information in their
3149 * (re)association response frames. Try to recover by using the data
3150 * from the beacon or probe response. This seems to afflict mobile
3151 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3152 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3153 */
3154 if ((assoc_data->wmm && !elems.wmm_param) ||
3155 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3156 (!elems.ht_cap_elem || !elems.ht_operation)) ||
3157 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3158 (!elems.vht_cap_elem || !elems.vht_operation))) {
3159 const struct cfg80211_bss_ies *ies;
3160 struct ieee802_11_elems bss_elems;
3161
3162 rcu_read_lock();
3163 ies = rcu_dereference(cbss->ies);
3164 if (ies)
3165 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3166 GFP_ATOMIC);
3167 rcu_read_unlock();
3168 if (!bss_ies)
3169 return false;
3170
3171 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003172 false, &bss_elems,
3173 mgmt->bssid,
3174 assoc_data->bss->bssid);
Johannes Berg35d865a2013-05-28 10:54:03 +02003175 if (assoc_data->wmm &&
3176 !elems.wmm_param && bss_elems.wmm_param) {
3177 elems.wmm_param = bss_elems.wmm_param;
3178 sdata_info(sdata,
3179 "AP bug: WMM param missing from AssocResp\n");
3180 }
3181
3182 /*
3183 * Also check if we requested HT/VHT, otherwise the AP doesn't
3184 * have to include the IEs in the (re)association response.
3185 */
3186 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
3187 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3188 elems.ht_cap_elem = bss_elems.ht_cap_elem;
3189 sdata_info(sdata,
3190 "AP bug: HT capability missing from AssocResp\n");
3191 }
3192 if (!elems.ht_operation && bss_elems.ht_operation &&
3193 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3194 elems.ht_operation = bss_elems.ht_operation;
3195 sdata_info(sdata,
3196 "AP bug: HT operation missing from AssocResp\n");
3197 }
3198 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
3199 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3200 elems.vht_cap_elem = bss_elems.vht_cap_elem;
3201 sdata_info(sdata,
3202 "AP bug: VHT capa missing from AssocResp\n");
3203 }
3204 if (!elems.vht_operation && bss_elems.vht_operation &&
3205 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3206 elems.vht_operation = bss_elems.vht_operation;
3207 sdata_info(sdata,
3208 "AP bug: VHT operation missing from AssocResp\n");
3209 }
3210 }
3211
3212 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003213 * We previously checked these in the beacon/probe response, so
3214 * they should be present here. This is just a safety net.
3215 */
3216 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3217 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
3218 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003219 "HT AP is missing WMM params or HT capability/operation\n");
3220 ret = false;
3221 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003222 }
3223
3224 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3225 (!elems.vht_cap_elem || !elems.vht_operation)) {
3226 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003227 "VHT AP is missing VHT capability/operation\n");
3228 ret = false;
3229 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003230 }
3231
Guy Eilam2a33bee2011-08-17 15:18:15 +03003232 mutex_lock(&sdata->local->sta_mtx);
3233 /*
3234 * station info was already allocated and inserted before
3235 * the association and should be available to us
3236 */
Johannes Berg7852e362012-01-20 13:55:24 +01003237 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003238 if (WARN_ON(!sta)) {
3239 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003240 ret = false;
3241 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07003242 }
3243
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05303244 sband = ieee80211_get_sband(sdata);
3245 if (!sband) {
3246 mutex_unlock(&sdata->local->sta_mtx);
3247 ret = false;
3248 goto out;
3249 }
Johannes Berg8318d782008-01-24 19:38:38 +01003250
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003251 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3252 (!elems.he_cap || !elems.he_operation)) {
3253 mutex_unlock(&sdata->local->sta_mtx);
3254 sdata_info(sdata,
3255 "HE AP is missing HE capability/operation\n");
3256 ret = false;
3257 goto out;
3258 }
3259
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003260 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01003261 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08003262 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003263 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02003264
Mahesh Palivela818255e2012-10-10 11:33:04 +00003265 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3266 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01003267 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00003268
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003269 if (elems.he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3270 elems.he_cap) {
3271 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3272 elems.he_cap,
3273 elems.he_cap_len,
3274 sta);
3275
3276 bss_conf->he_support = sta->sta.he_cap.has_he;
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003277 bss_conf->twt_requester =
3278 ieee80211_twt_req_supported(sta, &elems);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003279 } else {
3280 bss_conf->he_support = false;
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003281 bss_conf->twt_requester = false;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003282 }
3283
3284 if (bss_conf->he_support) {
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003285 bss_conf->bss_color =
3286 le32_get_bits(elems.he_operation->he_oper_params,
3287 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003288
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003289 bss_conf->htc_trig_based_pkt_ext =
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003290 le32_get_bits(elems.he_operation->he_oper_params,
3291 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003292 bss_conf->frame_time_rts_th =
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003293 le32_get_bits(elems.he_operation->he_oper_params,
3294 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003295
3296 bss_conf->multi_sta_back_32bit =
3297 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3298 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3299
3300 bss_conf->ack_enabled =
3301 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3302 IEEE80211_HE_MAC_CAP2_ACK_EN;
3303
3304 bss_conf->uora_exists = !!elems.uora_element;
3305 if (elems.uora_element)
3306 bss_conf->uora_ocw_range = elems.uora_element[0];
3307
3308 /* TODO: OPEN: what happens if BSS color disable is set? */
3309 }
3310
Sara Sharon78ac51f2019-01-16 18:22:56 +02003311 if (cbss->transmitted_bss) {
3312 bss_conf->nontransmitted = true;
3313 ether_addr_copy(bss_conf->transmitter_bssid,
3314 cbss->transmitted_bss->bssid);
3315 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3316 bss_conf->bssid_index = cbss->bssid_index;
3317 }
3318
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003319 /*
3320 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3321 * in their association response, so ignore that data for our own
3322 * configuration. If it changed since the last beacon, we'll get the
3323 * next beacon and update then.
3324 */
Johannes Berg11289582013-02-07 17:36:12 +01003325
Johannes Bergbee7f5862013-02-07 22:24:55 +01003326 /*
3327 * If an operating mode notification IE is present, override the
3328 * NSS calculation (that would be done in rate_control_rate_init())
3329 * and use the # of streams from that element.
3330 */
3331 if (elems.opmode_notif &&
3332 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3333 u8 nss;
3334
3335 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3336 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3337 nss += 1;
3338 sta->sta.rx_nss = nss;
3339 }
3340
Johannes Berg4b7679a2008-09-18 18:14:18 +02003341 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003342
Tamizh chelvam93f04902015-10-07 10:40:04 +05303343 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02003344 set_sta_flag(sta, WLAN_STA_MFP);
Tamizh chelvam93f04902015-10-07 10:40:04 +05303345 sta->sta.mfp = true;
3346 } else {
3347 sta->sta.mfp = false;
3348 }
Jouni Malinen5394af42009-01-08 13:31:59 +02003349
Johannes Berg527871d2015-03-21 08:09:55 +01003350 sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
Johannes Bergddf4ac52008-10-22 11:41:38 +02003351
Johannes Berg3e4d40f2013-02-07 17:19:08 +01003352 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01003353 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3354 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3355 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003356 sdata_info(sdata,
3357 "failed to move station %pM to desired state\n",
3358 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01003359 WARN_ON(__sta_info_destroy(sta));
3360 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003361 ret = false;
3362 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01003363 }
3364
Johannes Berg7852e362012-01-20 13:55:24 +01003365 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02003366
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003367 /*
3368 * Always handle WMM once after association regardless
3369 * of the first value the AP uses. Setting -1 here has
3370 * that effect because the AP values is an unsigned
3371 * 4-bit value.
3372 */
3373 ifmgd->wmm_last_param_set = -1;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02003374 ifmgd->mu_edca_last_param_set = -1;
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003375
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003376 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Bergcec66282015-10-22 17:46:04 +02003377 ieee80211_set_wmm_default(sdata, false, false);
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003378 } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003379 elems.wmm_param_len,
3380 elems.mu_edca_param_set)) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003381 /* still enable QoS since we might have HT/VHT */
3382 ieee80211_set_wmm_default(sdata, false, true);
3383 /* set the disable-WMM flag in this case to disable
3384 * tracking WMM parameter changes in the beacon if
3385 * the parameters weren't actually valid. Doing so
3386 * avoids changing parameters very strangely when
3387 * the AP is going back and forth between valid and
3388 * invalid parameters.
3389 */
3390 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3391 }
Johannes Berg3abead52012-03-02 15:56:59 +01003392 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07003393
Avraham Sterne38a0172017-04-26 10:58:47 +03003394 if (elems.max_idle_period_ie) {
3395 bss_conf->max_idle_period =
3396 le16_to_cpu(elems.max_idle_period_ie->max_idle_period);
3397 bss_conf->protected_keep_alive =
3398 !!(elems.max_idle_period_ie->idle_options &
3399 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3400 changed |= BSS_CHANGED_KEEP_ALIVE;
3401 } else {
3402 bss_conf->max_idle_period = 0;
3403 bss_conf->protected_keep_alive = false;
3404 }
3405
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003406 /* set AID and assoc capability,
3407 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01003408 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003409 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003410 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003411
Kalle Valo15b7b062009-03-22 21:57:14 +02003412 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01003413 * If we're using 4-addr mode, let the AP know that we're
3414 * doing so, so that it can create the STA VLAN on its side
3415 */
3416 if (ifmgd->use_4addr)
3417 ieee80211_send_4addr_nullfunc(local, sdata);
3418
3419 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02003420 * Start timer to probe the connection to the AP now.
3421 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02003422 */
Johannes Bergb291ba12009-07-10 15:29:03 +02003423 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003424 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02003425
Johannes Berg35d865a2013-05-28 10:54:03 +02003426 ret = true;
3427 out:
3428 kfree(bss_ies);
3429 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003430}
3431
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003432static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3433 struct ieee80211_mgmt *mgmt,
3434 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01003435{
3436 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3437 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3438 u16 capab_info, status_code, aid;
3439 struct ieee802_11_elems elems;
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003440 int ac, uapsd_queues = -1;
Johannes Berg66e67e42012-01-20 13:55:27 +01003441 u8 *pos;
3442 bool reassoc;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003443 struct cfg80211_bss *bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003444 struct ieee80211_event event = {
3445 .type = MLME_EVENT,
3446 .u.mlme.data = ASSOC_EVENT,
3447 };
Jiri Bencf0706e82007-05-05 11:45:53 -07003448
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003449 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003450
3451 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003452 return;
Joe Perchesb203ca32012-05-08 18:56:52 +00003453 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003454 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003455
3456 /*
3457 * AssocResp and ReassocResp have identical structure, so process both
3458 * of them in this function.
3459 */
3460
3461 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003462 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003463
Simon Dinkin7a7d3e42017-08-31 13:09:36 +03003464 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
Johannes Berg66e67e42012-01-20 13:55:27 +01003465 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3466 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3467 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3468
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003469 sdata_info(sdata,
3470 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3471 reassoc ? "Rea" : "A", mgmt->sa,
3472 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01003473
Jouni Malinen39404fe2016-10-27 00:42:05 +03003474 if (assoc_data->fils_kek_len &&
3475 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3476 return;
3477
Johannes Berg66e67e42012-01-20 13:55:27 +01003478 pos = mgmt->u.assoc_resp.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02003479 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3480 mgmt->bssid, assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003481
3482 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01003483 elems.timeout_int &&
3484 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003485 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01003486 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01003487 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003488 sdata_info(sdata,
3489 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3490 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01003491 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01003492 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003493 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003494 run_again(sdata, assoc_data->timeout);
3495 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003496 }
3497
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003498 bss = assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01003499
3500 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003501 sdata_info(sdata, "%pM denied association (code=%d)\n",
3502 mgmt->sa, status_code);
Johannes Berge6f462d2016-12-08 17:22:09 +01003503 ieee80211_destroy_assoc_data(sdata, false, false);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003504 event.u.mlme.status = MLME_DENIED;
3505 event.u.mlme.reason = status_code;
3506 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01003507 } else {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003508 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003509 /* oops -- internal error -- send timeout for now */
Johannes Berge6f462d2016-12-08 17:22:09 +01003510 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02003511 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003512 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003513 }
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003514 event.u.mlme.status = MLME_SUCCESS;
3515 drv_event_callback(sdata->local, sdata, &event);
John W. Linville635d9992012-07-09 16:34:34 -04003516 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01003517
3518 /*
3519 * destroy assoc_data afterwards, as otherwise an idle
3520 * recalc after assoc_data is NULL but before associated
3521 * is set can cause the interface to go idle
3522 */
Johannes Berge6f462d2016-12-08 17:22:09 +01003523 ieee80211_destroy_assoc_data(sdata, true, false);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003524
3525 /* get uapsd queues configuration */
3526 uapsd_queues = 0;
3527 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3528 if (sdata->tx_conf[ac].uapsd)
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03003529 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
Johannes Berg66e67e42012-01-20 13:55:27 +01003530 }
3531
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003532 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues);
Johannes Berg66e67e42012-01-20 13:55:27 +01003533}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003534
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003535static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003536 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003537 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003538{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003539 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02003540 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01003541 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01003542
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003543 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01003544
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02003545 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
3546 if (!channel)
Johannes Berg5bda6172008-09-08 11:05:10 +02003547 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003548
Sara Sharon4abb52a2019-01-16 12:14:41 +02003549 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03003550 if (bss) {
Alexander Bondar817cee72013-05-19 14:23:57 +03003551 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
Johannes Bergd2722f82014-03-04 11:43:28 +01003552 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03003553 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003554}
3555
3556
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003557static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01003558 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003559{
Johannes Bergaf6b6372009-12-23 13:15:35 +01003560 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02003561 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003562 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3563 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03003564
Kalle Valo15b7b062009-03-22 21:57:14 +02003565 ifmgd = &sdata->u.mgd;
3566
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003567 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003568
Joe Perchesb203ca32012-05-08 18:56:52 +00003569 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03003570 return; /* ignore ProbeResp to foreign address */
3571
Ester Kummerae6a44e2008-06-27 18:54:48 +03003572 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3573 if (baselen > len)
3574 return;
3575
Sara Sharon4abb52a2019-01-16 12:14:41 +02003576 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003577
Johannes Berg77fdaa12009-07-07 03:45:17 +02003578 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00003579 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003580 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003581}
3582
Johannes Bergd91f36d2009-04-16 13:17:26 +02003583/*
3584 * This is the canonical list of information elements we care about,
3585 * the filter code also gives us all changes to the Microsoft OUI
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07003586 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3587 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3588 * changes to requested client power.
Johannes Bergd91f36d2009-04-16 13:17:26 +02003589 *
3590 * We implement beacon filtering in software since that means we can
3591 * avoid processing the frame here and in cfg80211, and userspace
3592 * will not be able to tell whether the hardware supports it or not.
3593 *
3594 * XXX: This list needs to be dynamic -- userspace needs to be able to
3595 * add items it requires. It also needs to be able to tell us to
3596 * look out for other vendor IEs.
3597 */
3598static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02003599 (1ULL << WLAN_EID_COUNTRY) |
3600 (1ULL << WLAN_EID_ERP_INFO) |
3601 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3602 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3603 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg70a3fd62015-03-12 08:53:29 +02003604 (1ULL << WLAN_EID_HT_OPERATION) |
3605 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
Johannes Bergd91f36d2009-04-16 13:17:26 +02003606
Tosoni1ad22fb2018-03-14 16:58:34 +00003607static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3608 struct ieee80211_if_managed *ifmgd,
3609 struct ieee80211_bss_conf *bss_conf,
3610 struct ieee80211_local *local,
3611 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003612{
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003613 /* Track average RSSI from the Beacon frames of the current AP */
Tosoni1ad22fb2018-03-14 16:58:34 +00003614
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003615 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3616 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Johannes Berg338c17a2015-08-28 10:52:54 +02003617 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003618 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003619 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003620 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003621 } else {
Jouni Malinen391a2002010-08-27 22:22:00 +03003622 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003623 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003624
Johannes Berg338c17a2015-08-28 10:52:54 +02003625 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3626
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003627 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3628 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003629 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003630 int last_sig = ifmgd->last_ave_beacon_signal;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003631 struct ieee80211_event event = {
3632 .type = RSSI_EVENT,
3633 };
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003634
3635 /*
3636 * if signal crosses either of the boundaries, invoke callback
3637 * with appropriate parameters
3638 */
3639 if (sig > ifmgd->rssi_max_thold &&
3640 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3641 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003642 event.u.rssi.data = RSSI_EVENT_HIGH;
3643 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003644 } else if (sig < ifmgd->rssi_min_thold &&
3645 (last_sig >= ifmgd->rssi_max_thold ||
3646 last_sig == 0)) {
3647 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003648 event.u.rssi.data = RSSI_EVENT_LOW;
3649 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003650 }
3651 }
3652
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003653 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003654 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003655 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003656 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003657 int last_event = ifmgd->last_cqm_event_signal;
3658 int thold = bss_conf->cqm_rssi_thold;
3659 int hyst = bss_conf->cqm_rssi_hyst;
Johannes Berg338c17a2015-08-28 10:52:54 +02003660
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003661 if (sig < thold &&
3662 (last_event == 0 || sig < last_event - hyst)) {
3663 ifmgd->last_cqm_event_signal = sig;
3664 ieee80211_cqm_rssi_notify(
3665 &sdata->vif,
3666 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003667 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003668 } else if (sig > thold &&
3669 (last_event == 0 || sig > last_event + hyst)) {
3670 ifmgd->last_cqm_event_signal = sig;
3671 ieee80211_cqm_rssi_notify(
3672 &sdata->vif,
3673 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003674 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003675 }
3676 }
3677
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003678 if (bss_conf->cqm_rssi_low &&
3679 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3680 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3681 int last_event = ifmgd->last_cqm_event_signal;
3682 int low = bss_conf->cqm_rssi_low;
3683 int high = bss_conf->cqm_rssi_high;
3684
3685 if (sig < low &&
3686 (last_event == 0 || last_event >= low)) {
3687 ifmgd->last_cqm_event_signal = sig;
3688 ieee80211_cqm_rssi_notify(
3689 &sdata->vif,
3690 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3691 sig, GFP_KERNEL);
3692 } else if (sig > high &&
3693 (last_event == 0 || last_event <= high)) {
3694 ifmgd->last_cqm_event_signal = sig;
3695 ieee80211_cqm_rssi_notify(
3696 &sdata->vif,
3697 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3698 sig, GFP_KERNEL);
3699 }
3700 }
Tosoni1ad22fb2018-03-14 16:58:34 +00003701}
3702
Sara Sharon78ac51f2019-01-16 18:22:56 +02003703static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3704 struct cfg80211_bss *bss)
3705{
3706 if (ether_addr_equal(tx_bssid, bss->bssid))
3707 return true;
3708 if (!bss->transmitted_bss)
3709 return false;
3710 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3711}
3712
Tosoni1ad22fb2018-03-14 16:58:34 +00003713static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3714 struct ieee80211_mgmt *mgmt, size_t len,
3715 struct ieee80211_rx_status *rx_status)
3716{
3717 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3718 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3719 size_t baselen;
3720 struct ieee802_11_elems elems;
3721 struct ieee80211_local *local = sdata->local;
3722 struct ieee80211_chanctx_conf *chanctx_conf;
3723 struct ieee80211_channel *chan;
3724 struct sta_info *sta;
3725 u32 changed = 0;
3726 bool erp_valid;
3727 u8 erp_value = 0;
3728 u32 ncrc;
3729 u8 *bssid;
3730 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3731
3732 sdata_assert_lock(sdata);
3733
3734 /* Process beacon from the current BSS */
3735 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3736 if (baselen > len)
3737 return;
3738
3739 rcu_read_lock();
3740 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3741 if (!chanctx_conf) {
3742 rcu_read_unlock();
3743 return;
3744 }
3745
3746 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
3747 rcu_read_unlock();
3748 return;
3749 }
3750 chan = chanctx_conf->def.chan;
3751 rcu_read_unlock();
3752
3753 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Sara Sharon78ac51f2019-01-16 18:22:56 +02003754 ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->assoc_data->bss)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00003755 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003756 len - baselen, false, &elems,
3757 mgmt->bssid,
3758 ifmgd->assoc_data->bss->bssid);
Tosoni1ad22fb2018-03-14 16:58:34 +00003759
Sara Sharon4abb52a2019-01-16 12:14:41 +02003760 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Sara Sharon78ac51f2019-01-16 18:22:56 +02003761
3762 if (elems.dtim_period)
3763 ifmgd->dtim_period = elems.dtim_period;
Tosoni1ad22fb2018-03-14 16:58:34 +00003764 ifmgd->have_beacon = true;
3765 ifmgd->assoc_data->need_beacon = false;
3766 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3767 sdata->vif.bss_conf.sync_tsf =
3768 le64_to_cpu(mgmt->u.beacon.timestamp);
3769 sdata->vif.bss_conf.sync_device_ts =
3770 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02003771 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Tosoni1ad22fb2018-03-14 16:58:34 +00003772 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02003773
3774 if (elems.mbssid_config_ie)
3775 bss_conf->profile_periodicity =
3776 elems.mbssid_config_ie->profile_periodicity;
3777
3778 if (elems.ext_capab_len >= 11 &&
3779 (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
3780 bss_conf->ema_ap = true;
3781
Tosoni1ad22fb2018-03-14 16:58:34 +00003782 /* continue assoc process */
3783 ifmgd->assoc_data->timeout = jiffies;
3784 ifmgd->assoc_data->timeout_started = true;
3785 run_again(sdata, ifmgd->assoc_data->timeout);
3786 return;
3787 }
3788
3789 if (!ifmgd->associated ||
Sara Sharon78ac51f2019-01-16 18:22:56 +02003790 !ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->associated))
Tosoni1ad22fb2018-03-14 16:58:34 +00003791 return;
3792 bssid = ifmgd->associated->bssid;
3793
3794 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3795 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
3796 local, rx_status);
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003797
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003798 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003799 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01003800 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003801 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03003802 }
3803
Johannes Bergb291ba12009-07-10 15:29:03 +02003804 /*
3805 * Push the beacon loss detection into the future since
3806 * we are processing a beacon from the AP just now.
3807 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003808 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003809
Johannes Bergd91f36d2009-04-16 13:17:26 +02003810 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3811 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003812 len - baselen, false, &elems,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003813 care_about_ies, ncrc,
3814 mgmt->bssid, bssid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02003815
Johannes Berg90d13e82015-09-24 16:13:07 +02003816 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
3817 ieee80211_check_tim(elems.tim, elems.tim_len, ifmgd->aid)) {
3818 if (local->hw.conf.dynamic_ps_timeout > 0) {
3819 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3820 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3821 ieee80211_hw_config(local,
3822 IEEE80211_CONF_CHANGE_PS);
Kalle Valo572e0012009-02-10 17:09:31 +02003823 }
Johannes Berg076cdcb2015-09-24 16:14:55 +02003824 ieee80211_send_nullfunc(local, sdata, false);
Johannes Berg90d13e82015-09-24 16:13:07 +02003825 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3826 local->pspolling = true;
3827
3828 /*
3829 * Here is assumed that the driver will be
3830 * able to send ps-poll frame and receive a
3831 * response even though power save mode is
3832 * enabled, but some drivers might require
3833 * to disable power save here. This needs
3834 * to be investigated.
3835 */
3836 ieee80211_send_pspoll(local, sdata);
Vivek Natarajana97b77b2008-12-23 18:39:02 -08003837 }
3838 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003839
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01003840 if (sdata->vif.p2p ||
3841 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003842 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01003843 int ret;
3844
3845 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3846 len - baselen,
3847 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01003848 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003849 if (ret >= 2) {
3850 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3851 /* valid noa_attr and index changed */
3852 sdata->u.mgd.p2p_noa_index = noa.index;
3853 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3854 changed |= BSS_CHANGED_P2P_PS;
3855 /*
3856 * make sure we update all information, the CRC
3857 * mechanism doesn't look at P2P attributes.
3858 */
3859 ifmgd->beacon_crc_valid = false;
3860 }
3861 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3862 /* noa_attr not found and we had valid noa_attr before */
3863 sdata->u.mgd.p2p_noa_index = -1;
3864 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01003865 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01003866 ifmgd->beacon_crc_valid = false;
3867 }
3868 }
3869
Luciano Coelho0c21e632014-10-08 09:48:39 +03003870 if (ifmgd->csa_waiting_bcn)
3871 ieee80211_chswitch_post_beacon(sdata);
3872
Alexander Bondar2ecc3902015-03-01 09:10:00 +02003873 /*
3874 * Update beacon timing and dtim count on every beacon appearance. This
3875 * will allow the driver to use the most updated values. Do it before
3876 * comparing this one with last received beacon.
3877 * IMPORTANT: These parameters would possibly be out of sync by the time
3878 * the driver will use them. The synchronized view is currently
3879 * guaranteed only in certain callbacks.
3880 */
Johannes Berg30686bf2015-06-02 21:39:54 +02003881 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Alexander Bondar2ecc3902015-03-01 09:10:00 +02003882 sdata->vif.bss_conf.sync_tsf =
3883 le64_to_cpu(mgmt->u.beacon.timestamp);
3884 sdata->vif.bss_conf.sync_device_ts =
3885 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02003886 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Alexander Bondar2ecc3902015-03-01 09:10:00 +02003887 }
3888
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003889 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003890 return;
Jouni Malinen30196672009-05-19 17:01:43 +03003891 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003892 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03003893
Sara Sharon4abb52a2019-01-16 12:14:41 +02003894 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Johannes Berg7d257452012-07-06 17:37:43 +02003895
Johannes Bergd70b7612013-08-23 15:48:48 +02003896 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
Luciano Coelho2ba45382014-10-08 09:48:35 +03003897 rx_status->device_timestamp,
Johannes Bergd70b7612013-08-23 15:48:48 +02003898 &elems, true);
3899
Johannes Berg095d81c2013-10-15 12:25:07 +02003900 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
3901 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003902 elems.wmm_param_len,
3903 elems.mu_edca_param_set))
Johannes Berg7d257452012-07-06 17:37:43 +02003904 changed |= BSS_CHANGED_QOS;
3905
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003906 /*
3907 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01003908 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003909 */
Alexander Bondar989c6502013-05-16 17:34:17 +03003910 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003911 /* a few bogus AP send dtim_period = 0 or no TIM IE */
Sara Sharon78ac51f2019-01-16 18:22:56 +02003912 bss_conf->dtim_period = elems.dtim_period ?: 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003913
Alexander Bondar989c6502013-05-16 17:34:17 +03003914 changed |= BSS_CHANGED_BEACON_INFO;
3915 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03003916
3917 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02003918 ieee80211_recalc_ps(local);
Alexander Bondar482a9c72013-06-03 17:29:33 +03003919 mutex_unlock(&local->iflist_mtx);
3920
Alexander Bondarce857882013-05-06 17:17:04 +03003921 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003922 }
3923
Johannes Berg1946bed2013-03-27 14:31:53 +01003924 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003925 erp_valid = true;
3926 erp_value = elems.erp_info[0];
3927 } else {
3928 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003929 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003930 changed |= ieee80211_handle_bss_capability(sdata,
3931 le16_to_cpu(mgmt->u.beacon.capab_info),
3932 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003933
Johannes Berg11289582013-02-07 17:36:12 +01003934 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01003935 sta = sta_info_get(sdata, bssid);
3936
Eliad Peller53b954e2014-07-24 11:20:05 +03003937 if (ieee80211_config_bw(sdata, sta,
3938 elems.ht_cap_elem, elems.ht_operation,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003939 elems.vht_operation, elems.he_operation,
3940 bssid, &changed)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003941 mutex_unlock(&local->sta_mtx);
Johannes Berg89f774e2016-01-25 15:46:36 +02003942 sdata_info(sdata,
3943 "failed to follow AP %pM bandwidth change, disconnect\n",
3944 bssid);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003945 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3946 WLAN_REASON_DEAUTH_LEAVING,
3947 true, deauth_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02003948 ieee80211_report_disconnect(sdata, deauth_buf,
3949 sizeof(deauth_buf), true,
3950 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003951 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003952 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01003953
3954 if (sta && elems.opmode_notif)
3955 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
Eyal Shapiracf1e05c2015-12-08 16:04:36 +02003956 rx_status->band);
Johannes Berg11289582013-02-07 17:36:12 +01003957 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003958
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07003959 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
3960 elems.country_elem,
3961 elems.country_elem_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07003962 elems.pwr_constr_elem,
3963 elems.cisco_dtpc_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003964
Johannes Berg471b3ef2007-12-28 14:32:58 +01003965 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003966}
3967
Johannes Berg1fa57d02010-06-10 10:21:32 +02003968void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3969 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003970{
3971 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003972 struct ieee80211_mgmt *mgmt;
3973 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003974 struct ieee802_11_elems elems;
3975 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07003976
Jiri Bencf0706e82007-05-05 11:45:53 -07003977 rx_status = (struct ieee80211_rx_status *) skb->cb;
3978 mgmt = (struct ieee80211_mgmt *) skb->data;
3979 fc = le16_to_cpu(mgmt->frame_control);
3980
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003981 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003982
Johannes Berg66e67e42012-01-20 13:55:27 +01003983 switch (fc & IEEE80211_FCTL_STYPE) {
3984 case IEEE80211_STYPE_BEACON:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003985 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003986 break;
3987 case IEEE80211_STYPE_PROBE_RESP:
3988 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3989 break;
3990 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003991 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003992 break;
3993 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003994 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003995 break;
3996 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003997 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003998 break;
3999 case IEEE80211_STYPE_ASSOC_RESP:
4000 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004001 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004002 break;
4003 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01004004 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004005 ies_len = skb->len -
4006 offsetof(struct ieee80211_mgmt,
4007 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01004008
4009 if (ies_len < 0)
4010 break;
4011
Sara Sharon4abb52a2019-01-16 12:14:41 +02004012 /* CSA IE cannot be overridden, no need for BSSID */
Johannes Berg37799e52013-03-26 14:02:26 +01004013 ieee802_11_parse_elems(
4014 mgmt->u.action.u.chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004015 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg37799e52013-03-26 14:02:26 +01004016
4017 if (elems.parse_error)
4018 break;
4019
Johannes Berg66e67e42012-01-20 13:55:27 +01004020 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004021 rx_status->mactime,
4022 rx_status->device_timestamp,
4023 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004024 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4025 ies_len = skb->len -
4026 offsetof(struct ieee80211_mgmt,
4027 u.action.u.ext_chan_switch.variable);
4028
4029 if (ies_len < 0)
4030 break;
4031
Sara Sharon4abb52a2019-01-16 12:14:41 +02004032 /*
4033 * extended CSA IE can't be overridden, no need for
4034 * BSSID
4035 */
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004036 ieee802_11_parse_elems(
4037 mgmt->u.action.u.ext_chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004038 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004039
4040 if (elems.parse_error)
4041 break;
4042
4043 /* for the handling code pretend this was also an IE */
4044 elems.ext_chansw_ie =
4045 &mgmt->u.action.u.ext_chan_switch.data;
4046
4047 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004048 rx_status->mactime,
4049 rx_status->device_timestamp,
4050 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004051 }
Johannes Berg37799e52013-03-26 14:02:26 +01004052 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004053 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004054 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07004055}
4056
Kees Cook34f11cd2017-10-16 16:35:49 -07004057static void ieee80211_sta_timer(struct timer_list *t)
Jiri Bencf0706e82007-05-05 11:45:53 -07004058{
4059 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004060 from_timer(sdata, t, u.mgd.timer);
Jiri Bencf0706e82007-05-05 11:45:53 -07004061
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01004062 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07004063}
4064
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004065static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01004066 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004067{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004068 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004069
Johannes Berg37ad3882012-02-24 13:50:54 +01004070 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01004071 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01004072
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004073 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4074 reason);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004075}
4076
Johannes Berg46cad4b2015-08-15 22:39:54 +03004077static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
Johannes Berg66e67e42012-01-20 13:55:27 +01004078{
4079 struct ieee80211_local *local = sdata->local;
4080 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4081 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004082 u32 tx_flags = 0;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004083 u16 trans = 1;
4084 u16 status = 0;
Ilan Peerd4e36e52018-04-20 13:49:25 +03004085 u16 prepare_tx_duration = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004086
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004087 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004088
4089 if (WARN_ON_ONCE(!auth_data))
4090 return -EINVAL;
4091
Johannes Berg66e67e42012-01-20 13:55:27 +01004092 auth_data->tries++;
4093
4094 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004095 sdata_info(sdata, "authentication with %pM timed out\n",
4096 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004097
4098 /*
4099 * Most likely AP is not in the range so remove the
4100 * bss struct for that AP.
4101 */
4102 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4103
4104 return -ETIMEDOUT;
4105 }
4106
Ilan Peerd4e36e52018-04-20 13:49:25 +03004107 if (auth_data->algorithm == WLAN_AUTH_SAE)
4108 prepare_tx_duration =
4109 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4110
4111 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
Johannes Berga1845fc2012-06-27 13:18:36 +02004112
Johannes Berg46cad4b2015-08-15 22:39:54 +03004113 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4114 auth_data->bss->bssid, auth_data->tries,
4115 IEEE80211_AUTH_MAX_TRIES);
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004116
Johannes Berg46cad4b2015-08-15 22:39:54 +03004117 auth_data->expected_transaction = 2;
Johannes Berg66e67e42012-01-20 13:55:27 +01004118
Johannes Berg46cad4b2015-08-15 22:39:54 +03004119 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4120 trans = auth_data->sae_trans;
4121 status = auth_data->sae_status;
4122 auth_data->expected_transaction = trans;
Johannes Berg66e67e42012-01-20 13:55:27 +01004123 }
4124
Johannes Berg46cad4b2015-08-15 22:39:54 +03004125 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4126 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4127 IEEE80211_TX_INTFL_MLME_CONN_TX;
4128
4129 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4130 auth_data->data, auth_data->data_len,
4131 auth_data->bss->bssid,
4132 auth_data->bss->bssid, NULL, 0, 0,
4133 tx_flags);
4134
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02004135 if (tx_flags == 0) {
Ilan Peer407879b2018-04-20 13:49:20 +03004136 if (auth_data->algorithm == WLAN_AUTH_SAE)
4137 auth_data->timeout = jiffies +
4138 IEEE80211_AUTH_TIMEOUT_SAE;
4139 else
4140 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004141 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004142 auth_data->timeout =
4143 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004144 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004145
Ilan Peer407879b2018-04-20 13:49:20 +03004146 auth_data->timeout_started = true;
4147 run_again(sdata, auth_data->timeout);
4148
Johannes Berg66e67e42012-01-20 13:55:27 +01004149 return 0;
4150}
4151
4152static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4153{
4154 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4155 struct ieee80211_local *local = sdata->local;
4156
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004157 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004158
Johannes Berg66e67e42012-01-20 13:55:27 +01004159 assoc_data->tries++;
4160 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004161 sdata_info(sdata, "association with %pM timed out\n",
4162 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004163
4164 /*
4165 * Most likely AP is not in the range so remove the
4166 * bss struct for that AP.
4167 */
4168 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4169
4170 return -ETIMEDOUT;
4171 }
4172
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004173 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4174 assoc_data->bss->bssid, assoc_data->tries,
4175 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01004176 ieee80211_send_assoc(sdata);
4177
Johannes Berg30686bf2015-06-02 21:39:54 +02004178 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004179 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004180 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004181 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01004182 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004183 assoc_data->timeout =
4184 round_jiffies_up(jiffies +
4185 IEEE80211_ASSOC_TIMEOUT_LONG);
4186 assoc_data->timeout_started = true;
4187 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004188 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004189
4190 return 0;
4191}
4192
Johannes Berg1672c0e32013-01-29 15:02:27 +01004193void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4194 __le16 fc, bool acked)
4195{
4196 struct ieee80211_local *local = sdata->local;
4197
4198 sdata->u.mgd.status_fc = fc;
4199 sdata->u.mgd.status_acked = acked;
4200 sdata->u.mgd.status_received = true;
4201
4202 ieee80211_queue_work(&local->hw, &sdata->work);
4203}
4204
Johannes Berg1fa57d02010-06-10 10:21:32 +02004205void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004206{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004207 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02004208 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004209
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004210 sdata_lock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004211
Johannes Berg1672c0e32013-01-29 15:02:27 +01004212 if (ifmgd->status_received) {
4213 __le16 fc = ifmgd->status_fc;
4214 bool status_acked = ifmgd->status_acked;
4215
4216 ifmgd->status_received = false;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004217 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004218 if (status_acked) {
Ilan Peer407879b2018-04-20 13:49:20 +03004219 if (ifmgd->auth_data->algorithm ==
4220 WLAN_AUTH_SAE)
4221 ifmgd->auth_data->timeout =
4222 jiffies +
4223 IEEE80211_AUTH_TIMEOUT_SAE;
4224 else
4225 ifmgd->auth_data->timeout =
4226 jiffies +
4227 IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004228 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004229 } else {
4230 ifmgd->auth_data->timeout = jiffies - 1;
4231 }
Johannes Berg89afe612013-02-13 15:39:57 +01004232 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004233 } else if (ifmgd->assoc_data &&
4234 (ieee80211_is_assoc_req(fc) ||
4235 ieee80211_is_reassoc_req(fc))) {
4236 if (status_acked) {
4237 ifmgd->assoc_data->timeout =
4238 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004239 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004240 } else {
4241 ifmgd->assoc_data->timeout = jiffies - 1;
4242 }
Johannes Berg89afe612013-02-13 15:39:57 +01004243 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004244 }
4245 }
4246
Johannes Berg89afe612013-02-13 15:39:57 +01004247 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004248 time_after(jiffies, ifmgd->auth_data->timeout)) {
4249 if (ifmgd->auth_data->done) {
4250 /*
4251 * ok ... we waited for assoc but userspace didn't,
4252 * so let's just kill the auth data
4253 */
4254 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg46cad4b2015-08-15 22:39:54 +03004255 } else if (ieee80211_auth(sdata)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004256 u8 bssid[ETH_ALEN];
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004257 struct ieee80211_event event = {
4258 .type = MLME_EVENT,
4259 .u.mlme.data = AUTH_EVENT,
4260 .u.mlme.status = MLME_TIMEOUT,
4261 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004262
4263 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4264
4265 ieee80211_destroy_auth_data(sdata, false);
4266
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004267 cfg80211_auth_timeout(sdata->dev, bssid);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004268 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004269 }
Johannes Berg89afe612013-02-13 15:39:57 +01004270 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004271 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004272
Johannes Berg89afe612013-02-13 15:39:57 +01004273 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004274 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03004275 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01004276 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02004277 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004278 struct ieee80211_event event = {
4279 .type = MLME_EVENT,
4280 .u.mlme.data = ASSOC_EVENT,
4281 .u.mlme.status = MLME_TIMEOUT,
4282 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004283
Johannes Berge6f462d2016-12-08 17:22:09 +01004284 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004285 cfg80211_assoc_timeout(sdata->dev, bss);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004286 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004287 }
Johannes Berg89afe612013-02-13 15:39:57 +01004288 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004289 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004290
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004291 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02004292 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03004293 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004294 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03004295
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004296 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004297
Johannes Berg30686bf2015-06-02 21:39:54 +02004298 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Ben Greear180205b2011-02-04 15:30:24 -08004299 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004300 else
Ben Greear180205b2011-02-04 15:30:24 -08004301 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004302
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004303 /* ACK received for nullfunc probing frame */
4304 if (!ifmgd->probe_send_count)
4305 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004306 else if (ifmgd->nullfunc_failed) {
4307 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004308 mlme_dbg(sdata,
4309 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4310 bssid, ifmgd->probe_send_count,
4311 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004312 ieee80211_mgd_probe_ap_send(sdata);
4313 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004314 mlme_dbg(sdata,
4315 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4316 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02004317 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004318 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4319 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004320 }
4321 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004322 run_again(sdata, ifmgd->probe_timeout);
Johannes Berg30686bf2015-06-02 21:39:54 +02004323 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004324 mlme_dbg(sdata,
4325 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4326 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02004327 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004328 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004329 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004330 mlme_dbg(sdata,
4331 "No probe response from AP %pM after %dms, try %d/%i\n",
4332 bssid, probe_wait_ms,
4333 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03004334 ieee80211_mgd_probe_ap_send(sdata);
4335 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02004336 /*
4337 * We actually lost the connection ... or did we?
4338 * Let's make sure!
4339 */
Johannes Berg89f774e2016-01-25 15:46:36 +02004340 mlme_dbg(sdata,
4341 "No probe response from AP %pM after %dms, disconnecting.\n",
4342 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004343
Johannes Berg95acac62011-07-12 12:30:59 +02004344 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004345 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02004346 }
4347 }
4348
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004349 sdata_unlock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004350}
Johannes Berg0a51b272008-09-08 17:44:25 +02004351
Kees Cook34f11cd2017-10-16 16:35:49 -07004352static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004353{
4354 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004355 from_timer(sdata, t, u.mgd.bcn_mon_timer);
Luciano Coelho0c21e632014-10-08 09:48:39 +03004356 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004357
Luciano Coelho0c21e632014-10-08 09:48:39 +03004358 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004359 return;
4360
Johannes Berg682bd382013-01-29 13:13:50 +01004361 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004362 ieee80211_queue_work(&sdata->local->hw,
4363 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004364}
4365
Kees Cook34f11cd2017-10-16 16:35:49 -07004366static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004367{
4368 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004369 from_timer(sdata, t, u.mgd.conn_mon_timer);
Johannes Bergb291ba12009-07-10 15:29:03 +02004370 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4371 struct ieee80211_local *local = sdata->local;
4372
Luciano Coelho0c21e632014-10-08 09:48:39 +03004373 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004374 return;
4375
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04004376 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004377}
4378
4379static void ieee80211_sta_monitor_work(struct work_struct *work)
4380{
4381 struct ieee80211_sub_if_data *sdata =
4382 container_of(work, struct ieee80211_sub_if_data,
4383 u.mgd.monitor_work);
4384
4385 ieee80211_mgd_probe_ap(sdata, false);
4386}
4387
Johannes Berga1678f82008-09-11 00:01:47 +02004388static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4389{
Kalle Vaload935682009-04-19 08:47:19 +03004390 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02004391 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03004392
Johannes Bergb291ba12009-07-10 15:29:03 +02004393 /* let's probe the connection once */
Johannes Berg30686bf2015-06-02 21:39:54 +02004394 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Eliad Peller494f1fe2012-02-19 15:26:09 +02004395 ieee80211_queue_work(&sdata->local->hw,
4396 &sdata->u.mgd.monitor_work);
Kalle Vaload935682009-04-19 08:47:19 +03004397 }
Johannes Berga1678f82008-09-11 00:01:47 +02004398}
4399
Johannes Bergb8360ab2013-04-29 14:57:44 +02004400#ifdef CONFIG_PM
Johannes Berg1a1cb742014-03-19 09:55:55 +01004401void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4402{
4403 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4404 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4405
4406 sdata_lock(sdata);
4407
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004408 if (ifmgd->auth_data || ifmgd->assoc_data) {
4409 const u8 *bssid = ifmgd->auth_data ?
4410 ifmgd->auth_data->bss->bssid :
4411 ifmgd->assoc_data->bss->bssid;
4412
Johannes Berg1a1cb742014-03-19 09:55:55 +01004413 /*
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004414 * If we are trying to authenticate / associate while suspending,
4415 * cfg80211 won't know and won't actually abort those attempts,
4416 * thus we need to do that ourselves.
Johannes Berg1a1cb742014-03-19 09:55:55 +01004417 */
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004418 ieee80211_send_deauth_disassoc(sdata, bssid,
Johannes Berg1a1cb742014-03-19 09:55:55 +01004419 IEEE80211_STYPE_DEAUTH,
4420 WLAN_REASON_DEAUTH_LEAVING,
4421 false, frame_buf);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004422 if (ifmgd->assoc_data)
Johannes Berge6f462d2016-12-08 17:22:09 +01004423 ieee80211_destroy_assoc_data(sdata, false, true);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004424 if (ifmgd->auth_data)
4425 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg1a1cb742014-03-19 09:55:55 +01004426 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4427 IEEE80211_DEAUTH_FRAME_LEN);
4428 }
4429
Johannes Bergbe72afe2015-03-01 09:10:03 +02004430 /* This is a bit of a hack - we should find a better and more generic
4431 * solution to this. Normally when suspending, cfg80211 will in fact
4432 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4433 * auth (not so important) or assoc (this is the problem) process.
4434 *
4435 * As a consequence, it can happen that we are in the process of both
4436 * associating and suspending, and receive an association response
4437 * after cfg80211 has checked if it needs to disconnect, but before
4438 * we actually set the flag to drop incoming frames. This will then
4439 * cause the workqueue flush to process the association response in
4440 * the suspend, resulting in a successful association just before it
4441 * tries to remove the interface from the driver, which now though
4442 * has a channel context assigned ... this results in issues.
4443 *
4444 * To work around this (for now) simply deauth here again if we're
4445 * now connected.
4446 */
4447 if (ifmgd->associated && !sdata->local->wowlan) {
4448 u8 bssid[ETH_ALEN];
4449 struct cfg80211_deauth_request req = {
4450 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4451 .bssid = bssid,
4452 };
4453
4454 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4455 ieee80211_mgd_deauth(sdata, &req);
4456 }
4457
Johannes Berg1a1cb742014-03-19 09:55:55 +01004458 sdata_unlock(sdata);
4459}
4460
Johannes Bergb8360ab2013-04-29 14:57:44 +02004461void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4462{
4463 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4464
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004465 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004466 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004467 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004468 return;
4469 }
4470
4471 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4472 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4473 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4474 ieee80211_sta_connection_lost(sdata,
4475 ifmgd->associated->bssid,
4476 WLAN_REASON_UNSPECIFIED,
4477 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004478 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004479 return;
4480 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004481 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004482}
4483#endif
4484
Johannes Berg9c6bd792008-09-11 00:01:52 +02004485/* interface setup */
4486void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4487{
Johannes Berg46900292009-02-15 12:44:28 +01004488 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004489
Johannes Berg46900292009-02-15 12:44:28 +01004490 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004491 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01004492 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004493 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4494 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02004495 INIT_WORK(&ifmgd->csa_connection_drop_work,
4496 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03004497 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03004498 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4499 ieee80211_tdls_peer_del_work);
Kees Cook34f11cd2017-10-16 16:35:49 -07004500 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4501 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4502 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4503 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
Johannes Berg02219b32014-10-07 10:38:50 +03004504 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4505 ieee80211_sta_handle_tspec_ac_params_wk);
Johannes Berg9c6bd792008-09-11 00:01:52 +02004506
Johannes Bergab1faea2009-07-01 21:41:17 +02004507 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05304508 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02004509 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4510 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004511 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02004512
Eliad Peller0d8614b2014-09-10 14:07:36 +03004513 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
Johannes Berg0f782312009-12-01 13:37:02 +01004514 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4515 else
4516 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02004517
4518 /* Setup TDLS data */
4519 spin_lock_init(&ifmgd->teardown_lock);
4520 ifmgd->teardown_skb = NULL;
4521 ifmgd->orig_teardown_skb = NULL;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004522}
4523
4524/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02004525void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4526{
Johannes Berg31ee67a2012-07-06 21:18:24 +02004527 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02004528
4529 /* Restart STA timers */
4530 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07004531 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4532 if (ieee80211_sdata_running(sdata))
4533 ieee80211_restart_sta_timer(sdata);
4534 }
Johannes Berga1678f82008-09-11 00:01:47 +02004535 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02004536}
Johannes Berg10f644a2009-04-16 13:17:25 +02004537
Johannes Bergf2d9d272012-11-22 14:11:39 +01004538static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4539 struct cfg80211_bss *cbss)
4540{
4541 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4542 const u8 *ht_cap_ie, *vht_cap_ie;
4543 const struct ieee80211_ht_cap *ht_cap;
4544 const struct ieee80211_vht_cap *vht_cap;
4545 u8 chains = 1;
4546
4547 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4548 return chains;
4549
Johannes Berg9caf0362012-11-29 01:25:20 +01004550 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004551 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4552 ht_cap = (void *)(ht_cap_ie + 2);
4553 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4554 /*
4555 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4556 * "Tx Unequal Modulation Supported" fields.
4557 */
4558 }
4559
4560 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4561 return chains;
4562
Johannes Berg9caf0362012-11-29 01:25:20 +01004563 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004564 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4565 u8 nss;
4566 u16 tx_mcs_map;
4567
4568 vht_cap = (void *)(vht_cap_ie + 2);
4569 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4570 for (nss = 8; nss > 0; nss--) {
4571 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4572 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4573 break;
4574 }
4575 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4576 chains = max(chains, nss);
4577 }
4578
4579 return chains;
4580}
4581
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004582static bool
4583ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4584 const struct ieee80211_he_operation *he_op)
4585{
4586 const struct ieee80211_sta_he_cap *sta_he_cap =
4587 ieee80211_get_he_sta_cap(sband);
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004588 u16 ap_min_req_set;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004589 int i;
4590
4591 if (!sta_he_cap || !he_op)
4592 return false;
4593
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004594 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4595
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004596 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4597 for (i = 0; i < 3; i++) {
4598 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4599 &sta_he_cap->he_mcs_nss_supp;
4600 u16 sta_mcs_map_rx =
4601 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4602 u16 sta_mcs_map_tx =
4603 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4604 u8 nss;
4605 bool verified = true;
4606
4607 /*
4608 * For each band there is a maximum of 8 spatial streams
4609 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4610 * of 2 bits per NSS (1-8), with the values defined in enum
4611 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4612 * capabilities aren't less than the AP's minimum requirements
4613 * for this HE BSS per SS.
4614 * It is enough to find one such band that meets the reqs.
4615 */
4616 for (nss = 8; nss > 0; nss--) {
4617 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4618 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4619 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4620
4621 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4622 continue;
4623
4624 /*
4625 * Make sure the HE AP doesn't require MCSs that aren't
4626 * supported by the client
4627 */
4628 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4629 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4630 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4631 verified = false;
4632 break;
4633 }
4634 }
4635
4636 if (verified)
4637 return true;
4638 }
4639
4640 /* If here, STA doesn't meet AP's HE min requirements */
4641 return false;
4642}
4643
Johannes Bergb17166a2012-07-27 11:41:27 +02004644static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4645 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01004646{
4647 struct ieee80211_local *local = sdata->local;
4648 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Peller53b954e2014-07-24 11:20:05 +03004649 const struct ieee80211_ht_cap *ht_cap = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004650 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004651 const struct ieee80211_vht_operation *vht_oper = NULL;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004652 const struct ieee80211_he_operation *he_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004653 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01004654 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004655 int ret;
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004656 u32 i;
4657 bool have_80mhz;
Johannes Berga1cf7752012-03-08 15:02:07 +01004658
Johannes Berg24398e32012-03-28 10:58:36 +02004659 sband = local->hw.wiphy->bands[cbss->channel->band];
4660
Johannes Bergf2d9d272012-11-22 14:11:39 +01004661 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4662 IEEE80211_STA_DISABLE_80P80MHZ |
4663 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02004664
Johannes Berg9caf0362012-11-29 01:25:20 +01004665 rcu_read_lock();
4666
Johannes Bergf2d9d272012-11-22 14:11:39 +01004667 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
4668 sband->ht_cap.ht_supported) {
Eliad Peller53b954e2014-07-24 11:20:05 +03004669 const u8 *ht_oper_ie, *ht_cap_ie;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004670
Johannes Berg9caf0362012-11-29 01:25:20 +01004671 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02004672 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4673 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6effa2013-02-07 23:33:32 +01004674
Eliad Peller53b954e2014-07-24 11:20:05 +03004675 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4676 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4677 ht_cap = (void *)(ht_cap_ie + 2);
4678
4679 if (!ht_cap) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004680 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4681 ht_oper = NULL;
4682 }
Johannes Berg24398e32012-03-28 10:58:36 +02004683 }
4684
Johannes Bergf2d9d272012-11-22 14:11:39 +01004685 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
4686 sband->vht_cap.vht_supported) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004687 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004688
Johannes Berg9caf0362012-11-29 01:25:20 +01004689 vht_oper_ie = ieee80211_bss_get_ie(cbss,
4690 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004691 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
4692 vht_oper = (void *)(vht_oper_ie + 2);
4693 if (vht_oper && !ht_oper) {
4694 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004695 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01004696 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01004697 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4698 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02004699 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01004700
4701 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4702 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
4703 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4704 vht_oper = NULL;
4705 }
Johannes Berg24398e32012-03-28 10:58:36 +02004706 }
4707
Shaul Triebitz002245e2018-12-15 11:03:19 +02004708 if (!ieee80211_get_he_sta_cap(sband))
4709 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4710
4711 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004712 const struct cfg80211_bss_ies *ies;
4713 const u8 *he_oper_ie;
4714
4715 ies = rcu_dereference(cbss->ies);
4716 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
4717 ies->data, ies->len);
4718 if (he_oper_ie &&
4719 he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
4720 he_oper = (void *)(he_oper_ie + 3);
4721 else
4722 he_oper = NULL;
4723
Johannes Bergf0c04072018-06-29 09:51:39 +02004724 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004725 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4726 }
4727
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004728 /* Allow VHT if at least one channel on the sband supports 80 MHz */
4729 have_80mhz = false;
4730 for (i = 0; i < sband->n_channels; i++) {
4731 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4732 IEEE80211_CHAN_NO_80MHZ))
4733 continue;
4734
4735 have_80mhz = true;
4736 break;
4737 }
4738
4739 if (!have_80mhz)
4740 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4741
Johannes Bergf2d9d272012-11-22 14:11:39 +01004742 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
4743 cbss->channel,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004744 ht_oper, vht_oper, he_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02004745 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02004746
Johannes Bergf2d9d272012-11-22 14:11:39 +01004747 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
4748 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02004749
Johannes Berg9caf0362012-11-29 01:25:20 +01004750 rcu_read_unlock();
4751
Johannes Berg04ecd252012-09-11 14:34:12 +02004752 /* will change later if needed */
4753 sdata->smps_mode = IEEE80211_SMPS_OFF;
4754
Johannes Berg34a37402013-12-18 09:43:33 +01004755 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004756 /*
4757 * If this fails (possibly due to channel context sharing
4758 * on incompatible channels, e.g. 80+80 and 160 sharing the
4759 * same control channel) try to use a smaller bandwidth.
4760 */
4761 ret = ieee80211_vif_use_channel(sdata, &chandef,
4762 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02004763
4764 /* don't downgrade for 5 and 10 MHz channels, though. */
4765 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4766 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01004767 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02004768
Johannes Bergd601cd82013-02-07 20:54:51 +01004769 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02004770 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01004771 ret = ieee80211_vif_use_channel(sdata, &chandef,
4772 IEEE80211_CHANCTX_SHARED);
4773 }
Johannes Berg34a37402013-12-18 09:43:33 +01004774 out:
4775 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004776 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02004777}
4778
Sara Sharon78ac51f2019-01-16 18:22:56 +02004779static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
4780 u8 *dtim_count, u8 *dtim_period)
4781{
4782 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
4783 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
4784 ies->len);
4785 const struct ieee80211_tim_ie *tim = NULL;
4786 const struct ieee80211_bssid_index *idx;
4787 bool valid = tim_ie && tim_ie[1] >= 2;
4788
4789 if (valid)
4790 tim = (void *)(tim_ie + 2);
4791
4792 if (dtim_count)
4793 *dtim_count = valid ? tim->dtim_count : 0;
4794
4795 if (dtim_period)
4796 *dtim_period = valid ? tim->dtim_period : 0;
4797
4798 /* Check if value is overridden by non-transmitted profile */
4799 if (!idx_ie || idx_ie[1] < 3)
4800 return valid;
4801
4802 idx = (void *)(idx_ie + 2);
4803
4804 if (dtim_count)
4805 *dtim_count = idx->dtim_count;
4806
4807 if (dtim_period)
4808 *dtim_period = idx->dtim_period;
4809
4810 return true;
4811}
4812
Johannes Bergb17166a2012-07-27 11:41:27 +02004813static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03004814 struct cfg80211_bss *cbss, bool assoc,
4815 bool override)
Johannes Bergb17166a2012-07-27 11:41:27 +02004816{
4817 struct ieee80211_local *local = sdata->local;
4818 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4819 struct ieee80211_bss *bss = (void *)cbss->priv;
4820 struct sta_info *new_sta = NULL;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02004821 struct ieee80211_supported_band *sband;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03004822 bool have_sta = false;
Johannes Bergb17166a2012-07-27 11:41:27 +02004823 int err;
4824
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02004825 sband = local->hw.wiphy->bands[cbss->channel->band];
4826
Johannes Bergb17166a2012-07-27 11:41:27 +02004827 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
4828 return -EINVAL;
4829
Luca Coelhof8860ce2017-05-02 17:56:21 +03004830 /* If a reconfig is happening, bail out */
4831 if (local->in_reconfig)
4832 return -EBUSY;
4833
Johannes Bergb17166a2012-07-27 11:41:27 +02004834 if (assoc) {
4835 rcu_read_lock();
4836 have_sta = sta_info_get(sdata, cbss->bssid);
4837 rcu_read_unlock();
4838 }
4839
4840 if (!have_sta) {
4841 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
4842 if (!new_sta)
4843 return -ENOMEM;
4844 }
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02004845
Johannes Bergc87905b2017-06-10 13:52:43 +03004846 /*
4847 * Set up the information for the new channel before setting the
4848 * new channel. We can't - completely race-free - change the basic
4849 * rates bitmap and the channel (sband) that it refers to, but if
4850 * we set it up before we at least avoid calling into the driver's
4851 * bss_info_changed() method with invalid information (since we do
4852 * call that from changing the channel - only for IDLE and perhaps
4853 * some others, but ...).
4854 *
4855 * So to avoid that, just set up all the new information before the
4856 * channel, but tell the driver to apply it only afterwards, since
4857 * it might need the new channel for that.
4858 */
Johannes Berg13e0c8e2012-07-27 10:43:16 +02004859 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004860 u32 rates = 0, basic_rates = 0;
4861 bool have_higher_than_11mbit;
4862 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Berg8cef2c92013-02-05 16:54:31 +01004863 const struct cfg80211_bss_ies *ies;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02004864 int shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004865
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004866 ieee80211_get_rates(sband, bss->supp_rates,
4867 bss->supp_rates_len,
4868 &rates, &basic_rates,
4869 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02004870 &min_rate, &min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03004871 shift);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004872
4873 /*
4874 * This used to be a workaround for basic rates missing
4875 * in the association response frame. Now that we no
4876 * longer use the basic rates from there, it probably
4877 * doesn't happen any more, but keep the workaround so
4878 * in case some *other* APs are buggy in different ways
4879 * we can connect -- with a warning.
4880 */
4881 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004882 sdata_info(sdata,
4883 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004884 basic_rates = BIT(min_rate_index);
4885 }
4886
Johannes Berg13e0c8e2012-07-27 10:43:16 +02004887 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004888 sdata->vif.bss_conf.basic_rates = basic_rates;
4889
4890 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg57fbcce2016-04-12 15:56:15 +02004891 if (cbss->channel->band == NL80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004892 have_higher_than_11mbit)
4893 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4894 else
4895 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4896
4897 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4898
Johannes Berg8c358bc2012-05-22 22:13:05 +02004899 /* set timing information */
4900 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01004901 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01004902 ies = rcu_dereference(cbss->beacon_ies);
4903 if (ies) {
Johannes Bergef429da2013-02-05 17:48:40 +01004904 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4905 sdata->vif.bss_conf.sync_device_ts =
4906 bss->device_ts_beacon;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004907
4908 ieee80211_get_dtim(ies,
4909 &sdata->vif.bss_conf.sync_dtim_count,
4910 NULL);
Johannes Berg30686bf2015-06-02 21:39:54 +02004911 } else if (!ieee80211_hw_check(&sdata->local->hw,
4912 TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01004913 ies = rcu_dereference(cbss->proberesp_ies);
4914 /* must be non-NULL since beacon IEs were NULL */
4915 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4916 sdata->vif.bss_conf.sync_device_ts =
4917 bss->device_ts_presp;
4918 sdata->vif.bss_conf.sync_dtim_count = 0;
4919 } else {
4920 sdata->vif.bss_conf.sync_tsf = 0;
4921 sdata->vif.bss_conf.sync_device_ts = 0;
4922 sdata->vif.bss_conf.sync_dtim_count = 0;
4923 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01004924 rcu_read_unlock();
Johannes Bergc87905b2017-06-10 13:52:43 +03004925 }
Johannes Berg8c358bc2012-05-22 22:13:05 +02004926
Johannes Bergc87905b2017-06-10 13:52:43 +03004927 if (new_sta || override) {
4928 err = ieee80211_prep_channel(sdata, cbss);
4929 if (err) {
4930 if (new_sta)
4931 sta_info_free(local, new_sta);
4932 return -EINVAL;
4933 }
4934 }
4935
4936 if (new_sta) {
4937 /*
4938 * tell driver about BSSID, basic rates and timing
4939 * this was set up above, before setting the channel
4940 */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004941 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02004942 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
4943 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01004944
4945 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02004946 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01004947
Johannes Berg13e0c8e2012-07-27 10:43:16 +02004948 err = sta_info_insert(new_sta);
4949 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01004950 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004951 sdata_info(sdata,
4952 "failed to insert STA entry for the AP (error %d)\n",
4953 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01004954 return err;
4955 }
4956 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00004957 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01004958
David Spinadel7d830a12015-03-17 19:58:38 +02004959 /* Cancel scan to ensure that nothing interferes with connection */
4960 if (local->scanning)
4961 ieee80211_scan_cancel(local);
4962
Johannes Berga1cf7752012-03-08 15:02:07 +01004963 return 0;
4964}
4965
Johannes Berg77fdaa12009-07-07 03:45:17 +02004966/* config hooks */
4967int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4968 struct cfg80211_auth_request *req)
4969{
Johannes Berg66e67e42012-01-20 13:55:27 +01004970 struct ieee80211_local *local = sdata->local;
4971 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4972 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004973 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01004974 int err;
Jouni Malinenefb543e2018-10-11 00:21:21 +03004975 bool cont_auth;
Johannes Berg66e67e42012-01-20 13:55:27 +01004976
4977 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02004978
4979 switch (req->auth_type) {
4980 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4981 auth_alg = WLAN_AUTH_OPEN;
4982 break;
4983 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01004984 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02004985 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004986 auth_alg = WLAN_AUTH_SHARED_KEY;
4987 break;
4988 case NL80211_AUTHTYPE_FT:
4989 auth_alg = WLAN_AUTH_FT;
4990 break;
4991 case NL80211_AUTHTYPE_NETWORK_EAP:
4992 auth_alg = WLAN_AUTH_LEAP;
4993 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004994 case NL80211_AUTHTYPE_SAE:
4995 auth_alg = WLAN_AUTH_SAE;
4996 break;
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03004997 case NL80211_AUTHTYPE_FILS_SK:
4998 auth_alg = WLAN_AUTH_FILS_SK;
4999 break;
5000 case NL80211_AUTHTYPE_FILS_SK_PFS:
5001 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5002 break;
5003 case NL80211_AUTHTYPE_FILS_PK:
5004 auth_alg = WLAN_AUTH_FILS_PK;
5005 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005006 default:
5007 return -EOPNOTSUPP;
5008 }
5009
Jouni Malinenefb543e2018-10-11 00:21:21 +03005010 if (ifmgd->assoc_data)
Jouni Malinen8d7432a2018-10-11 00:21:20 +03005011 return -EBUSY;
5012
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005013 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005014 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01005015 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005016 return -ENOMEM;
5017
Johannes Berg66e67e42012-01-20 13:55:27 +01005018 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005019
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005020 if (req->auth_data_len >= 4) {
Jouni Malinen6ec63612016-10-27 00:41:59 +03005021 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5022 __le16 *pos = (__le16 *) req->auth_data;
5023
5024 auth_data->sae_trans = le16_to_cpu(pos[0]);
5025 auth_data->sae_status = le16_to_cpu(pos[1]);
5026 }
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005027 memcpy(auth_data->data, req->auth_data + 4,
5028 req->auth_data_len - 4);
5029 auth_data->data_len += req->auth_data_len - 4;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005030 }
5031
Jouni Malinenefb543e2018-10-11 00:21:21 +03005032 /* Check if continuing authentication or trying to authenticate with the
5033 * same BSS that we were in the process of authenticating with and avoid
5034 * removal and re-addition of the STA entry in
5035 * ieee80211_prep_connection().
5036 */
5037 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5038
Johannes Berg77fdaa12009-07-07 03:45:17 +02005039 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005040 memcpy(&auth_data->data[auth_data->data_len],
5041 req->ie, req->ie_len);
5042 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005043 }
5044
Johannes Bergfffd0932009-07-08 14:22:54 +02005045 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005046 auth_data->key_len = req->key_len;
5047 auth_data->key_idx = req->key_idx;
5048 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02005049 }
5050
Johannes Berg66e67e42012-01-20 13:55:27 +01005051 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005052
Johannes Berg66e67e42012-01-20 13:55:27 +01005053 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01005054
Jouni Malinenefb543e2018-10-11 00:21:21 +03005055 if (ifmgd->auth_data) {
5056 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5057 auth_data->peer_confirmed =
5058 ifmgd->auth_data->peer_confirmed;
5059 }
5060 ieee80211_destroy_auth_data(sdata, cont_auth);
5061 }
Guy Eilam2a33bee2011-08-17 15:18:15 +03005062
Johannes Berg66e67e42012-01-20 13:55:27 +01005063 /* prep auth_data so we don't go into idle on disassoc */
5064 ifmgd->auth_data = auth_data;
5065
Jouni Malinenefb543e2018-10-11 00:21:21 +03005066 /* If this is continuation of an ongoing SAE authentication exchange
5067 * (i.e., request to send SAE Confirm) and the peer has already
5068 * confirmed, mark authentication completed since we are about to send
5069 * out SAE Confirm.
5070 */
5071 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5072 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5073 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5074
Johannes Berg7b119dc2013-04-10 21:38:36 +02005075 if (ifmgd->associated) {
5076 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5077
Johannes Berg89f774e2016-01-25 15:46:36 +02005078 sdata_info(sdata,
5079 "disconnect from AP %pM for new auth to %pM\n",
5080 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005081 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5082 WLAN_REASON_UNSPECIFIED,
5083 false, frame_buf);
5084
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005085 ieee80211_report_disconnect(sdata, frame_buf,
5086 sizeof(frame_buf), true,
5087 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005088 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005089
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005090 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005091
Jouni Malinenefb543e2018-10-11 00:21:21 +03005092 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Johannes Berga1cf7752012-03-08 15:02:07 +01005093 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01005094 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005095
Johannes Berg46cad4b2015-08-15 22:39:54 +03005096 err = ieee80211_auth(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01005097 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005098 sta_info_destroy_addr(sdata, req->bss->bssid);
5099 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005100 }
5101
Johannes Berg66e67e42012-01-20 13:55:27 +01005102 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01005103 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005104 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02005105
Johannes Berg66e67e42012-01-20 13:55:27 +01005106 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005107 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005108 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005109 ifmgd->auth_data = NULL;
Michal Kaziord01f8582015-06-03 08:36:13 +02005110 mutex_lock(&sdata->local->mtx);
5111 ieee80211_vif_release_channel(sdata);
5112 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01005113 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005114 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005115}
5116
Johannes Berg77fdaa12009-07-07 03:45:17 +02005117int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5118 struct cfg80211_assoc_request *req)
5119{
Johannes Berg66e67e42012-01-20 13:55:27 +01005120 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005121 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01005122 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01005123 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005124 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005125 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005126 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005127 int i, err;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005128 bool override = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005129
Johannes Berg66e67e42012-01-20 13:55:27 +01005130 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5131 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01005132 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005133
Johannes Berg9caf0362012-11-29 01:25:20 +01005134 rcu_read_lock();
5135 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
5136 if (!ssidie) {
5137 rcu_read_unlock();
5138 kfree(assoc_data);
5139 return -EINVAL;
5140 }
5141 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5142 assoc_data->ssid_len = ssidie[1];
5143 rcu_read_unlock();
5144
Johannes Berg7b119dc2013-04-10 21:38:36 +02005145 if (ifmgd->associated) {
5146 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5147
Johannes Berg89f774e2016-01-25 15:46:36 +02005148 sdata_info(sdata,
5149 "disconnect from AP %pM for new assoc to %pM\n",
5150 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005151 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5152 WLAN_REASON_UNSPECIFIED,
5153 false, frame_buf);
5154
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005155 ieee80211_report_disconnect(sdata, frame_buf,
5156 sizeof(frame_buf), true,
5157 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005158 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005159
5160 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5161 err = -EBUSY;
5162 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005163 }
5164
Johannes Berg66e67e42012-01-20 13:55:27 +01005165 if (ifmgd->assoc_data) {
5166 err = -EBUSY;
5167 goto err_free;
5168 }
5169
5170 if (ifmgd->auth_data) {
5171 bool match;
5172
5173 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00005174 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005175 ieee80211_destroy_auth_data(sdata, match);
5176 }
5177
5178 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02005179
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03005180 ifmgd->beacon_crc_valid = false;
5181
Johannes Berg095d81c2013-10-15 12:25:07 +02005182 assoc_data->wmm = bss->wmm_used &&
5183 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg095d81c2013-10-15 12:25:07 +02005184
Johannes Bergde5036a2012-03-08 15:02:03 +01005185 /*
5186 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5187 * We still associate in non-HT mode (11a/b/g) if any one of these
5188 * ciphers is configured as pairwise.
5189 * We can set this to true for non-11n hardware, that'll be checked
5190 * separately along with the peer capabilities.
5191 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02005192 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02005193 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5194 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02005195 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01005196 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005197 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005198 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg1c4cb922012-05-30 15:57:00 +02005199 netdev_info(sdata->dev,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005200 "disabling HE/HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02005201 }
5202 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005203
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005204 /* Also disable HT if we don't support it or the AP doesn't use WMM */
5205 sband = local->hw.wiphy->bands[req->bss->channel->band];
5206 if (!sband->ht_cap.ht_supported ||
Johannes Berg095d81c2013-10-15 12:25:07 +02005207 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5208 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Berga8243b72012-11-22 14:32:09 +01005209 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg095d81c2013-10-15 12:25:07 +02005210 if (!bss->wmm_used &&
5211 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
Johannes Berg1b49de22012-07-27 10:29:14 +02005212 netdev_info(sdata->dev,
5213 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02005214 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005215
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005216 /* disable VHT if we don't support it or the AP doesn't use WMM */
5217 if (!sband->vht_cap.vht_supported ||
Johannes Berg095d81c2013-10-15 12:25:07 +02005218 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5219 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005220 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg095d81c2013-10-15 12:25:07 +02005221 if (!bss->wmm_used &&
5222 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
Johannes Berg1b49de22012-07-27 10:29:14 +02005223 netdev_info(sdata->dev,
5224 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005225 }
5226
Ben Greearef96a8422011-11-18 11:32:00 -08005227 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5228 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5229 sizeof(ifmgd->ht_capa_mask));
5230
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01005231 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5232 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5233 sizeof(ifmgd->vht_capa_mask));
5234
Johannes Berg77fdaa12009-07-07 03:45:17 +02005235 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005236 memcpy(assoc_data->ie, req->ie, req->ie_len);
5237 assoc_data->ie_len = req->ie_len;
5238 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005239
Jouni Malinen39404fe2016-10-27 00:42:05 +03005240 if (req->fils_kek) {
5241 /* should already be checked in cfg80211 - so warn */
5242 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5243 err = -EINVAL;
5244 goto err_free;
5245 }
5246 memcpy(assoc_data->fils_kek, req->fils_kek,
5247 req->fils_kek_len);
5248 assoc_data->fils_kek_len = req->fils_kek_len;
5249 }
5250
5251 if (req->fils_nonces)
5252 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5253 2 * FILS_NONCE_LEN);
5254
Johannes Berg66e67e42012-01-20 13:55:27 +01005255 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01005256
Johannes Bergaf6b6372009-12-23 13:15:35 +01005257 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5258 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02005259 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005260 else
Johannes Berg04ecd252012-09-11 14:34:12 +02005261 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005262 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02005263 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005264
Johannes Berg66e67e42012-01-20 13:55:27 +01005265 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01005266 assoc_data->supp_rates = bss->supp_rates;
5267 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02005268
Johannes Berg9caf0362012-11-29 01:25:20 +01005269 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02005270 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5271 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5272 assoc_data->ap_ht_param =
5273 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
5274 else
Johannes Berga8243b72012-11-22 14:32:09 +01005275 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005276 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5277 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5278 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5279 sizeof(struct ieee80211_vht_cap));
5280 else
5281 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01005282 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01005283
Johannes Berg848955c2014-11-11 12:48:42 +01005284 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02005285 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
Johannes Berg848955c2014-11-11 12:48:42 +01005286 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5287 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5288
Kalle Valoab133152010-01-12 10:42:31 +02005289 if (bss->wmm_used && bss->uapsd_supported &&
Johannes Berg848955c2014-11-11 12:48:42 +01005290 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01005291 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02005292 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5293 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01005294 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02005295 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5296 }
5297
Johannes Berg77fdaa12009-07-07 03:45:17 +02005298 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01005299 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005300
5301 if (req->use_mfp) {
5302 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5303 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5304 } else {
5305 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5306 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5307 }
5308
Assaf Krausscd2f5dd2014-09-03 15:25:02 +03005309 if (req->flags & ASSOC_REQ_USE_RRM)
5310 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5311 else
5312 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5313
Johannes Berg77fdaa12009-07-07 03:45:17 +02005314 if (req->crypto.control_port)
5315 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5316 else
5317 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5318
Johannes Berga621fa42010-08-27 14:26:54 +03005319 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5320 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Denis Kenzior018f6fb2018-03-26 12:52:51 -05005321 sdata->control_port_over_nl80211 =
5322 req->crypto.control_port_over_nl80211;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02005323 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5324 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03005325
Johannes Berg66e67e42012-01-20 13:55:27 +01005326 /* kick off associate process */
5327
5328 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02005329 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03005330 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01005331
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005332 /* override HT/VHT configuration only if the AP and we support it */
5333 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5334 struct ieee80211_sta_ht_cap sta_ht_cap;
5335
5336 if (req->flags & ASSOC_REQ_DISABLE_HT)
5337 override = true;
5338
5339 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5340 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5341
5342 /* check for 40 MHz disable override */
5343 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5344 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5345 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5346 override = true;
5347
5348 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5349 req->flags & ASSOC_REQ_DISABLE_VHT)
5350 override = true;
5351 }
5352
5353 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5354 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5355 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5356 }
5357
5358 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5359 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5360
5361 err = ieee80211_prep_connection(sdata, req->bss, true, override);
Johannes Berga1cf7752012-03-08 15:02:07 +01005362 if (err)
5363 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01005364
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005365 rcu_read_lock();
5366 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02005367
Johannes Berg30686bf2015-06-02 21:39:54 +02005368 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005369 !beacon_ies) {
5370 /*
5371 * Wait up to one beacon interval ...
5372 * should this be more if we miss one?
5373 */
5374 sdata_info(sdata, "waiting for beacon from %pM\n",
5375 ifmgd->bssid);
5376 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01005377 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005378 assoc_data->need_beacon = true;
5379 } else if (beacon_ies) {
Sara Sharon78ac51f2019-01-16 18:22:56 +02005380 const u8 *ie;
Johannes Bergef429da2013-02-05 17:48:40 +01005381 u8 dtim_count = 0;
5382
Sara Sharon78ac51f2019-01-16 18:22:56 +02005383 ieee80211_get_dtim(beacon_ies, &dtim_count,
5384 &ifmgd->dtim_period);
5385
Alexander Bondar989c6502013-05-16 17:34:17 +03005386 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005387 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005388 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01005389
Johannes Berg30686bf2015-06-02 21:39:54 +02005390 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005391 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5392 sdata->vif.bss_conf.sync_device_ts =
5393 bss->device_ts_beacon;
5394 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5395 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02005396
5397 ie = cfg80211_find_ext_ie(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5398 beacon_ies->data, beacon_ies->len);
5399 if (ie && ie[1] >= 3)
5400 sdata->vif.bss_conf.profile_periodicity = ie[4];
5401
5402 ie = cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY,
5403 beacon_ies->data, beacon_ies->len);
5404 if (ie && ie[1] >= 11 &&
5405 (ie[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
5406 sdata->vif.bss_conf.ema_ap = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005407 } else {
5408 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005409 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005410 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005411 rcu_read_unlock();
5412
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005413 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01005414
Paul Stewartfcff4f12012-02-23 17:59:53 -08005415 if (bss->corrupt_data) {
5416 char *corrupt_type = "data";
5417 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5418 if (bss->corrupt_data &
5419 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5420 corrupt_type = "beacon and probe response";
5421 else
5422 corrupt_type = "beacon";
5423 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5424 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005425 sdata_info(sdata, "associating with AP with corrupt %s\n",
5426 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08005427 }
5428
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005429 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01005430 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005431 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005432 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005433 ifmgd->assoc_data = NULL;
5434 err_free:
5435 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005436 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005437}
5438
5439int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005440 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005441{
5442 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005443 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005444 bool tx = !req->local_state_change;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005445
Johannes Bergc9c3a062014-03-19 09:11:19 +01005446 if (ifmgd->auth_data &&
5447 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5448 sdata_info(sdata,
5449 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5450 req->bssid, req->reason_code,
5451 ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005452
Ilan Peerd4e36e52018-04-20 13:49:25 +03005453 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg37ad3882012-02-24 13:50:54 +01005454 ieee80211_send_deauth_disassoc(sdata, req->bssid,
5455 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005456 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01005457 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01005458 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005459 ieee80211_report_disconnect(sdata, frame_buf,
5460 sizeof(frame_buf), true,
5461 req->reason_code);
Johannes Berg86552012012-10-29 09:46:31 +01005462
Johannes Bergc9c3a062014-03-19 09:11:19 +01005463 return 0;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03005464 }
5465
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005466 if (ifmgd->assoc_data &&
5467 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5468 sdata_info(sdata,
5469 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5470 req->bssid, req->reason_code,
5471 ieee80211_get_reason_code_string(req->reason_code));
5472
Ilan Peerd4e36e52018-04-20 13:49:25 +03005473 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005474 ieee80211_send_deauth_disassoc(sdata, req->bssid,
5475 IEEE80211_STYPE_DEAUTH,
5476 req->reason_code, tx,
5477 frame_buf);
Johannes Berge6f462d2016-12-08 17:22:09 +01005478 ieee80211_destroy_assoc_data(sdata, false, true);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005479 ieee80211_report_disconnect(sdata, frame_buf,
5480 sizeof(frame_buf), true,
5481 req->reason_code);
5482 return 0;
5483 }
5484
Johannes Berg86552012012-10-29 09:46:31 +01005485 if (ifmgd->associated &&
5486 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Bergc9c3a062014-03-19 09:11:19 +01005487 sdata_info(sdata,
5488 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5489 req->bssid, req->reason_code,
5490 ieee80211_get_reason_code_string(req->reason_code));
5491
Johannes Berg86552012012-10-29 09:46:31 +01005492 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5493 req->reason_code, tx, frame_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005494 ieee80211_report_disconnect(sdata, frame_buf,
5495 sizeof(frame_buf), true,
5496 req->reason_code);
Johannes Bergc9c3a062014-03-19 09:11:19 +01005497 return 0;
5498 }
Johannes Berg86552012012-10-29 09:46:31 +01005499
Johannes Bergc9c3a062014-03-19 09:11:19 +01005500 return -ENOTCONN;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005501}
5502
5503int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005504 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005505{
5506 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07005507 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005508 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02005509
Johannes Berg8d8b2612009-07-25 11:58:36 +02005510 /*
5511 * cfg80211 should catch this ... but it's racy since
5512 * we can receive a disassoc frame, process it, hand it
5513 * to cfg80211 while that's in a locked section already
5514 * trying to tell us that the user wants to disconnect.
5515 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005516 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005517 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005518
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005519 sdata_info(sdata,
Calvin Owensdfa1ad22014-02-11 12:36:24 -06005520 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5521 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005522
Jouni Malinene69e95d2010-03-29 23:29:31 -07005523 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01005524 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5525 req->reason_code, !req->local_state_change,
5526 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005527
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005528 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5529 req->reason_code);
Johannes Bergbc83b682009-11-29 12:19:06 +01005530
Johannes Berg77fdaa12009-07-07 03:45:17 +02005531 return 0;
5532}
Jouni Malinen026331c2010-02-15 12:53:10 +02005533
Eliad Pellerafa762f2012-04-23 14:45:15 +03005534void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005535{
5536 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5537
Ben Greear49921852013-02-20 09:41:09 -08005538 /*
5539 * Make sure some work items will not run after this,
5540 * they will not do anything but might not have been
5541 * cancelled when disconnecting.
5542 */
5543 cancel_work_sync(&ifmgd->monitor_work);
5544 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5545 cancel_work_sync(&ifmgd->request_smps_work);
5546 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5547 cancel_work_sync(&ifmgd->chswitch_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03005548 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
Ben Greear49921852013-02-20 09:41:09 -08005549
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005550 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02005551 if (ifmgd->assoc_data) {
5552 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berge6f462d2016-12-08 17:22:09 +01005553 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02005554 cfg80211_assoc_timeout(sdata->dev, bss);
5555 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005556 if (ifmgd->auth_data)
5557 ieee80211_destroy_auth_data(sdata, false);
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005558 spin_lock_bh(&ifmgd->teardown_lock);
5559 if (ifmgd->teardown_skb) {
5560 kfree_skb(ifmgd->teardown_skb);
5561 ifmgd->teardown_skb = NULL;
5562 ifmgd->orig_teardown_skb = NULL;
5563 }
5564 spin_unlock_bh(&ifmgd->teardown_lock);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005565 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005566 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005567}
5568
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005569void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5570 enum nl80211_cqm_rssi_threshold_event rssi_event,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005571 s32 rssi_level,
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005572 gfp_t gfp)
5573{
5574 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5575
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005576 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
Johannes Bergb5878a22010-04-07 16:48:40 +02005577
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +01005578 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005579}
5580EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Johannes Berg98f03342014-11-26 12:42:02 +01005581
5582void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5583{
5584 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5585
5586 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5587
5588 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5589}
5590EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);