blob: 67829667d39478ec2fa3b3c7fa874a084344e446 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jiri Bencf0706e82007-05-05 11:45:53 -07002/*
3 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02004 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07005 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03009 * Copyright 2013-2014 Intel Mobile Communications GmbH
Avraham Sterne38a0172017-04-26 10:58:47 +030010 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Sara Sharon2bf973f2020-01-31 13:12:51 +020011 * Copyright (C) 2018 - 2020 Intel Corporation
Jiri Bencf0706e82007-05-05 11:45:53 -070012 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Ard Biesheuvel5fdb3732019-06-12 18:19:54 +020015#include <linux/fips.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070016#include <linux/if_ether.h>
17#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070019#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040020#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010021#include <linux/rtnetlink.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b39c2008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jouni Malinen39404fe2016-10-27 00:42:05 +030032#include "fils_aead.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070033
Johannes Berg1672c0e32013-01-29 15:02:27 +010034#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020035#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010036#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
Ilan Peer407879b2018-04-20 13:49:20 +030037#define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010038#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
40#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020041#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010042#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
43#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010044
Ben Greear180205b2011-02-04 15:30:24 -080045static int max_nullfunc_tries = 2;
46module_param(max_nullfunc_tries, int, 0644);
47MODULE_PARM_DESC(max_nullfunc_tries,
48 "Maximum nullfunc tx tries before disconnecting (reason 4).");
49
50static int max_probe_tries = 5;
51module_param(max_probe_tries, int, 0644);
52MODULE_PARM_DESC(max_probe_tries,
53 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020054
55/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010056 * Beacon loss timeout is calculated as N frames times the
57 * advertised beacon interval. This may need to be somewhat
58 * higher than what hardware might detect to account for
59 * delays in the host processing frames. But since we also
60 * probe on beacon miss before declaring the connection lost
61 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020062 */
Ben Greear59c1ec22013-03-19 14:19:56 -070063static int beacon_loss_count = 7;
64module_param(beacon_loss_count, int, 0644);
65MODULE_PARM_DESC(beacon_loss_count,
66 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010067
Johannes Bergb291ba12009-07-10 15:29:03 +020068/*
69 * Time the connection can be idle before we probe
70 * it to see if we can still talk to the AP.
71 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030072#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020073/*
74 * Time we wait for a probe response after sending
75 * a probe request because of beacon loss or for
76 * checking the connection still works.
77 */
Ben Greear180205b2011-02-04 15:30:24 -080078static int probe_wait_ms = 500;
79module_param(probe_wait_ms, int, 0644);
80MODULE_PARM_DESC(probe_wait_ms,
81 "Maximum time(ms) to wait for probe response"
82 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070083
Jouni Malinen17e4ec12010-03-29 23:28:30 -070084/*
Jouni Malinen391a2002010-08-27 22:22:00 +030085 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg77fdaa12009-07-07 03:45:17 +020090/*
Johannes Bergca386f32009-07-10 02:39:48 +020091 * We can have multiple work items (and connection probing)
92 * scheduling this timer, but we need to take care to only
93 * reschedule it when it should fire _earlier_ than it was
94 * asked for before, or if it's not pending right now. This
95 * function ensures that. Note that it then is required to
96 * run this function for all timeouts after the first one
97 * has happened -- the work that runs from this timer will
98 * do that.
99 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200100static void run_again(struct ieee80211_sub_if_data *sdata,
101 unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200102{
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200103 sdata_assert_lock(sdata);
Johannes Bergca386f32009-07-10 02:39:48 +0200104
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200105 if (!timer_pending(&sdata->u.mgd.timer) ||
106 time_before(timeout, sdata->u.mgd.timer.expires))
107 mod_timer(&sdata->u.mgd.timer, timeout);
Johannes Bergca386f32009-07-10 02:39:48 +0200108}
109
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400110void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200111{
Johannes Bergc1288b12012-01-19 09:29:57 +0100112 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200113 return;
114
Johannes Berg30686bf2015-06-02 21:39:54 +0200115 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Johannes Berg7eeff742012-07-18 10:27:27 +0200116 return;
117
Johannes Bergb291ba12009-07-10 15:29:03 +0200118 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100119 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200120}
121
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400122void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
123{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
125
Johannes Bergb100e5d2016-03-17 15:41:37 +0200126 if (unlikely(!ifmgd->associated))
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100127 return;
128
Johannes Bergb100e5d2016-03-17 15:41:37 +0200129 if (ifmgd->probe_send_count)
130 ifmgd->probe_send_count = 0;
Eliad Peller448cd2e2014-02-11 12:30:18 +0200131
Johannes Berg30686bf2015-06-02 21:39:54 +0200132 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400133 return;
134
Johannes Bergb100e5d2016-03-17 15:41:37 +0200135 mod_timer(&ifmgd->conn_mon_timer,
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400136 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
137}
138
Johannes Berg5484e232008-09-08 17:44:27 +0200139static int ecw2cw(int ecw)
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200140{
Johannes Berg5484e232008-09-08 17:44:27 +0200141 return (1 << ecw) - 1;
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200142}
143
Johannes Berg6565ec92013-02-08 14:52:32 +0100144static u32
145ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
146 struct ieee80211_supported_band *sband,
147 struct ieee80211_channel *channel,
Johannes Berg2a333a02020-05-28 21:34:35 +0200148 u32 vht_cap_info,
Johannes Berg6565ec92013-02-08 14:52:32 +0100149 const struct ieee80211_ht_operation *ht_oper,
150 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300151 const struct ieee80211_he_operation *he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700152 const struct ieee80211_s1g_oper_ie *s1g_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200153 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100154{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100156 struct cfg80211_chan_def vht_chandef;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200157 struct ieee80211_sta_ht_cap sta_ht_cap;
Johannes Berg6565ec92013-02-08 14:52:32 +0100158 u32 ht_cfreq, ret;
159
Alexei Avshalom Lazar2a380752019-08-18 17:35:17 +0300160 memset(chandef, 0, sizeof(struct cfg80211_chan_def));
Johannes Berg6565ec92013-02-08 14:52:32 +0100161 chandef->chan = channel;
162 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
163 chandef->center_freq1 = channel->center_freq;
Thomas Pedersenb60119602020-04-01 18:18:04 -0700164 chandef->freq1_offset = channel->freq_offset;
Johannes Berg6565ec92013-02-08 14:52:32 +0100165
Johannes Berg57fa5e82020-05-28 21:34:36 +0200166 if (channel->band == NL80211_BAND_6GHZ) {
167 if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, chandef))
168 ret = IEEE80211_STA_DISABLE_HT |
169 IEEE80211_STA_DISABLE_VHT |
170 IEEE80211_STA_DISABLE_HE;
Johannes Berg523f3ec2020-06-03 11:15:03 +0200171 else
172 ret = 0;
Johannes Berg57fa5e82020-05-28 21:34:36 +0200173 vht_chandef = *chandef;
174 goto out;
Thomas Pedersen75f87ea2020-10-01 10:47:48 -0700175 } else if (sband->band == NL80211_BAND_S1GHZ) {
176 if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) {
177 sdata_info(sdata,
178 "Missing S1G Operation Element? Trying operating == primary\n");
179 chandef->width = ieee80211_s1g_channel_width(channel);
180 }
Johannes Berg57fa5e82020-05-28 21:34:36 +0200181
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700182 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_40MHZ |
183 IEEE80211_STA_DISABLE_VHT |
184 IEEE80211_STA_DISABLE_80P80MHZ |
185 IEEE80211_STA_DISABLE_160MHZ;
186 goto out;
187 }
188
Thomas Pedersen75f87ea2020-10-01 10:47:48 -0700189 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
190 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
191
Johannes Bergb1b1ae22017-10-13 15:26:01 +0300192 if (!ht_oper || !sta_ht_cap.ht_supported) {
Johannes Berg75e296e2020-01-31 13:12:39 +0200193 ret = IEEE80211_STA_DISABLE_HT |
194 IEEE80211_STA_DISABLE_VHT |
195 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100196 goto out;
197 }
198
199 chandef->width = NL80211_CHAN_WIDTH_20;
200
201 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
202 channel->band);
203 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200204 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100205 /*
206 * It's possible that some APs are confused here;
207 * Netgear WNDR3700 sometimes reports 4 higher than
208 * the actual channel in association responses, but
209 * since we look at probe response/beacon data here
210 * it should be OK.
211 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200212 sdata_info(sdata,
213 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
214 channel->center_freq, ht_cfreq,
215 ht_oper->primary_chan, channel->band);
Johannes Berg75e296e2020-01-31 13:12:39 +0200216 ret = IEEE80211_STA_DISABLE_HT |
217 IEEE80211_STA_DISABLE_VHT |
218 IEEE80211_STA_DISABLE_HE;
Johannes Berg6565ec92013-02-08 14:52:32 +0100219 goto out;
220 }
221
222 /* check 40 MHz support, if we have it */
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +0200223 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100224 ieee80211_chandef_ht_oper(ht_oper, chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100225 } else {
226 /* 40 MHz (and 80 MHz) must be supported for VHT */
227 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100228 /* also mark 40 MHz disabled */
229 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100230 goto out;
231 }
232
233 if (!vht_oper || !sband->vht_cap.vht_supported) {
234 ret = IEEE80211_STA_DISABLE_VHT;
235 goto out;
236 }
237
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100238 vht_chandef = *chandef;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300239 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
240 (le32_to_cpu(he_oper->he_oper_params) &
241 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
242 struct ieee80211_vht_operation he_oper_vht_cap;
243
244 /*
245 * Set only first 3 bytes (other 2 aren't used in
246 * ieee80211_chandef_vht_oper() anyway)
247 */
248 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
249 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
250
Johannes Berg2a333a02020-05-28 21:34:35 +0200251 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
Johannes Berg7eb26df2018-08-31 11:31:18 +0300252 &he_oper_vht_cap, ht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300253 &vht_chandef)) {
254 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
255 sdata_info(sdata,
256 "HE AP VHT information is invalid, disable HE\n");
257 ret = IEEE80211_STA_DISABLE_HE;
258 goto out;
259 }
Johannes Berg2a333a02020-05-28 21:34:35 +0200260 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
261 vht_cap_info,
262 vht_oper, ht_oper,
263 &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200264 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100265 sdata_info(sdata,
Johannes Berg8ac3c7042015-12-18 15:08:34 +0100266 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100267 ret = IEEE80211_STA_DISABLE_VHT;
268 goto out;
269 }
270
271 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200272 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100273 sdata_info(sdata,
274 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100275 ret = IEEE80211_STA_DISABLE_VHT;
276 goto out;
277 }
278
279 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
280 ret = 0;
281 goto out;
282 }
283
284 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200285 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100286 sdata_info(sdata,
287 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100288 ret = IEEE80211_STA_DISABLE_VHT;
289 goto out;
290 }
291
292 *chandef = vht_chandef;
293
294 ret = 0;
295
296out:
Johannes Berg963a18522014-02-21 20:34:34 +0100297 /*
298 * When tracking the current AP, don't do any further checks if the
299 * new chandef is identical to the one we're currently using for the
300 * connection. This keeps us from playing ping-pong with regulatory,
301 * without it the following can happen (for example):
302 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
303 * - AP advertises regdom US
304 * - CRDA loads regdom US with 80 MHz prohibited (old database)
305 * - the code below detects an unsupported channel, downgrades, and
306 * we disconnect from the AP in the caller
307 * - disconnect causes CRDA to reload world regdomain and the game
308 * starts anew.
309 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
310 *
311 * It seems possible that there are still scenarios with CSA or real
312 * bandwidth changes where a this could happen, but those cases are
313 * less common and wouldn't completely prevent using the AP.
314 */
315 if (tracking &&
316 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
317 return ret;
318
Johannes Berg586e01e2013-02-14 12:13:53 +0100319 /* don't print the message below for VHT mismatch if VHT is disabled */
320 if (ret & IEEE80211_STA_DISABLE_VHT)
321 vht_chandef = *chandef;
322
Johannes Bergddfe49b2013-07-31 20:52:03 +0200323 /*
324 * Ignore the DISABLED flag when we're already connected and only
325 * tracking the APs beacon for bandwidth changes - otherwise we
326 * might get disconnected here if we connect to an AP, update our
327 * regulatory information based on the AP's country IE and the
328 * information we have is wrong/outdated and disables the channel
329 * that we're actually using for the connection to the AP.
330 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100331 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200332 tracking ? 0 :
333 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100334 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
335 ret = IEEE80211_STA_DISABLE_HT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200336 IEEE80211_STA_DISABLE_VHT |
337 IEEE80211_STA_DISABLE_HE;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700338 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100339 }
340
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200341 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100342 }
343
Daniel Gabay8cadb202020-01-31 13:12:52 +0200344 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
345 IEEE80211_CHAN_NO_HE))
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200346 ret |= IEEE80211_STA_DISABLE_HE;
347
Johannes Berg5cdaed12013-07-31 11:23:06 +0200348 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100349 sdata_info(sdata,
350 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
351
352 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
353 return ret;
354}
355
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100356static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
357 struct sta_info *sta,
Eliad Peller53b954e2014-07-24 11:20:05 +0300358 const struct ieee80211_ht_cap *ht_cap,
Johannes Berg2a333a02020-05-28 21:34:35 +0200359 const struct ieee80211_vht_cap *vht_cap,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100360 const struct ieee80211_ht_operation *ht_oper,
361 const struct ieee80211_vht_operation *vht_oper,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300362 const struct ieee80211_he_operation *he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700363 const struct ieee80211_s1g_oper_ie *s1g_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100364 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200365{
366 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100367 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300368 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
369 struct ieee80211_supported_band *sband =
370 local->hw.wiphy->bands[chan->band];
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100371 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200372 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100373 u32 flags;
374 enum ieee80211_sta_rx_bandwidth new_sta_bw;
Johannes Berg2a333a02020-05-28 21:34:35 +0200375 u32 vht_cap_info = 0;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100376 int ret;
377
378 /* if HT was/is disabled, don't track any bandwidth changes */
379 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
380 return 0;
381
382 /* don't check VHT if we associated as non-VHT station */
383 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
384 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200385
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300386 /* don't check HE if we associated as non-HE station */
387 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
388 !ieee80211_get_he_sta_cap(sband))
389 he_oper = NULL;
390
Johannes Berg11289582013-02-07 17:36:12 +0100391 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100392 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100393
Avri Altman017b45b2013-11-18 19:06:48 +0200394 /*
395 * if bss configuration changed store the new one -
396 * this may be applicable even if channel is identical
397 */
398 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
399 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
400 *changed |= BSS_CHANGED_HT;
401 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
402 }
403
Johannes Berg2a333a02020-05-28 21:34:35 +0200404 if (vht_cap)
405 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
406
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300407 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
Johannes Berg2a333a02020-05-28 21:34:35 +0200408 flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300409 ht_oper, vht_oper, he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700410 s1g_oper, &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100411
412 /*
413 * Downgrade the new channel if we associated with restricted
414 * capabilities. For example, if we associated as a 20 MHz STA
415 * to a 40 MHz AP (due to regulatory, capabilities or config
416 * reasons) then switching to a 40 MHz channel now won't do us
417 * any good -- we couldn't use it with the AP.
418 */
419 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
420 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200421 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100422 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
423 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200424 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100425 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
426 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200427 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100428
429 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
430 return 0;
431
432 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -0700433 "AP %pM changed bandwidth, new config is %d.%03d MHz, "
434 "width %d (%d.%03d/%d MHz)\n",
435 ifmgd->bssid, chandef.chan->center_freq,
436 chandef.chan->freq_offset, chandef.width,
437 chandef.center_freq1, chandef.freq1_offset,
438 chandef.center_freq2);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100439
440 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
441 IEEE80211_STA_DISABLE_VHT |
Johannes Berg75e296e2020-01-31 13:12:39 +0200442 IEEE80211_STA_DISABLE_HE |
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100443 IEEE80211_STA_DISABLE_40MHZ |
444 IEEE80211_STA_DISABLE_80P80MHZ |
445 IEEE80211_STA_DISABLE_160MHZ)) ||
446 !cfg80211_chandef_valid(&chandef)) {
447 sdata_info(sdata,
448 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
449 ifmgd->bssid);
450 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200451 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200452
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100453 switch (chandef.width) {
454 case NL80211_CHAN_WIDTH_20_NOHT:
455 case NL80211_CHAN_WIDTH_20:
456 new_sta_bw = IEEE80211_STA_RX_BW_20;
457 break;
458 case NL80211_CHAN_WIDTH_40:
459 new_sta_bw = IEEE80211_STA_RX_BW_40;
460 break;
461 case NL80211_CHAN_WIDTH_80:
462 new_sta_bw = IEEE80211_STA_RX_BW_80;
463 break;
464 case NL80211_CHAN_WIDTH_80P80:
465 case NL80211_CHAN_WIDTH_160:
466 new_sta_bw = IEEE80211_STA_RX_BW_160;
467 break;
468 default:
469 return -EINVAL;
470 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200471
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100472 if (new_sta_bw > sta->cur_max_bandwidth)
473 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200474
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100475 if (new_sta_bw < sta->sta.bandwidth) {
476 sta->sta.bandwidth = new_sta_bw;
477 rate_control_rate_update(local, sband, sta,
478 IEEE80211_RC_BW_CHANGED);
479 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530480
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100481 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
482 if (ret) {
483 sdata_info(sdata,
484 "AP %pM changed bandwidth to incompatible one - disconnect\n",
485 ifmgd->bssid);
486 return ret;
487 }
488
489 if (new_sta_bw > sta->sta.bandwidth) {
490 sta->sta.bandwidth = new_sta_bw;
491 rate_control_rate_update(local, sband, sta,
492 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200493 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200494
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100495 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200496}
497
Johannes Berg9c6bd792008-09-11 00:01:52 +0200498/* frame sending functions */
499
Johannes Berg66e67e42012-01-20 13:55:27 +0100500static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200501 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100502 struct ieee80211_supported_band *sband,
503 struct ieee80211_channel *channel,
504 enum ieee80211_smps_mode smps)
505{
Johannes Berg66e67e42012-01-20 13:55:27 +0100506 u8 *pos;
507 u32 flags = channel->flags;
508 u16 cap;
509 struct ieee80211_sta_ht_cap ht_cap;
510
511 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
512
Johannes Berg66e67e42012-01-20 13:55:27 +0100513 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
514 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
515
Johannes Berg66e67e42012-01-20 13:55:27 +0100516 /* determine capability flags */
517 cap = ht_cap.cap;
518
Johannes Berg9dde6422012-05-16 23:43:19 +0200519 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100520 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
521 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
522 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
523 cap &= ~IEEE80211_HT_CAP_SGI_40;
524 }
525 break;
526 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
527 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
528 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
529 cap &= ~IEEE80211_HT_CAP_SGI_40;
530 }
531 break;
532 }
533
Johannes Berg24398e32012-03-28 10:58:36 +0200534 /*
535 * If 40 MHz was disabled associate as though we weren't
536 * capable of 40 MHz -- some broken APs will never fall
537 * back to trying to transmit in 20 MHz.
538 */
539 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
540 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
541 cap &= ~IEEE80211_HT_CAP_SGI_40;
542 }
543
Johannes Berg66e67e42012-01-20 13:55:27 +0100544 /* set SM PS mode properly */
545 cap &= ~IEEE80211_HT_CAP_SM_PS;
546 switch (smps) {
547 case IEEE80211_SMPS_AUTOMATIC:
548 case IEEE80211_SMPS_NUM_MODES:
549 WARN_ON(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -0500550 fallthrough;
Johannes Berg66e67e42012-01-20 13:55:27 +0100551 case IEEE80211_SMPS_OFF:
552 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
553 IEEE80211_HT_CAP_SM_PS_SHIFT;
554 break;
555 case IEEE80211_SMPS_STATIC:
556 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
557 IEEE80211_HT_CAP_SM_PS_SHIFT;
558 break;
559 case IEEE80211_SMPS_DYNAMIC:
560 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
561 IEEE80211_HT_CAP_SM_PS_SHIFT;
562 break;
563 }
564
565 /* reserve and fill IE */
566 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
567 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
568}
569
Sara Sharon322cd402015-07-08 15:41:43 +0300570/* This function determines vht capability flags for the association
571 * and builds the IE.
572 * Note - the function may set the owner of the MU-MIMO capability
573 */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000574static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
575 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100576 struct ieee80211_supported_band *sband,
577 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000578{
Sara Sharon322cd402015-07-08 15:41:43 +0300579 struct ieee80211_local *local = sdata->local;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000580 u8 *pos;
581 u32 cap;
582 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200583 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000584
585 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
586
587 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100588 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000589
590 /* determine capability flags */
591 cap = vht_cap.cap;
592
Johannes Bergf2d9d272012-11-22 14:11:39 +0100593 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
Johannes Berg575f0532014-11-24 16:51:33 +0100594 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
595
596 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
597 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
598 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
599 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100600 }
601
602 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
603 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
Johannes Berg575f0532014-11-24 16:51:33 +0100604 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
Johannes Bergf2d9d272012-11-22 14:11:39 +0100605 }
606
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100607 /*
608 * Some APs apparently get confused if our capabilities are better
609 * than theirs, so restrict what we advertise in the assoc request.
610 */
611 if (!(ap_vht_cap->vht_cap_info &
612 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
Sara Sharon322cd402015-07-08 15:41:43 +0300613 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
614 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
615 else if (!(ap_vht_cap->vht_cap_info &
616 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
617 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
618
619 /*
620 * If some other vif is using the MU-MIMO capablity we cannot associate
621 * using MU-MIMO - this will lead to contradictions in the group-id
622 * mechanism.
623 * Ownership is defined since association request, in order to avoid
624 * simultaneous associations with MU-MIMO.
625 */
626 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
627 bool disable_mu_mimo = false;
628 struct ieee80211_sub_if_data *other;
629
630 list_for_each_entry_rcu(other, &local->interfaces, list) {
Sara Sharonb5a33d52016-02-16 12:48:18 +0200631 if (other->vif.mu_mimo_owner) {
Sara Sharon322cd402015-07-08 15:41:43 +0300632 disable_mu_mimo = true;
633 break;
634 }
635 }
636 if (disable_mu_mimo)
637 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
638 else
Sara Sharonb5a33d52016-02-16 12:48:18 +0200639 sdata->vif.mu_mimo_owner = true;
Sara Sharon322cd402015-07-08 15:41:43 +0300640 }
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100641
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200642 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
643
644 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
645 our_bf_sts = cap & mask;
646
647 if (ap_bf_sts < our_bf_sts) {
648 cap &= ~mask;
649 cap |= ap_bf_sts;
650 }
651
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000652 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000653 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000654 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
655}
656
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300657/* This function determines HE capability flags for the association
658 * and builds the IE.
659 */
660static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
661 struct sk_buff *skb,
662 struct ieee80211_supported_band *sband)
663{
664 u8 *pos;
665 const struct ieee80211_sta_he_cap *he_cap = NULL;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200666 struct ieee80211_chanctx_conf *chanctx_conf;
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300667 u8 he_cap_size;
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200668 bool reg_cap = false;
669
670 rcu_read_lock();
671 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
672 if (!WARN_ON_ONCE(!chanctx_conf))
673 reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
674 &chanctx_conf->def,
675 IEEE80211_CHAN_NO_HE);
676
677 rcu_read_unlock();
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300678
679 he_cap = ieee80211_get_he_sta_cap(sband);
Haim Dreyfussb5db1ac2020-01-31 13:12:44 +0200680 if (!he_cap || !reg_cap)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300681 return;
682
683 /*
684 * TODO: the 1 added is because this temporarily is under the EXTENSION
685 * IE. Get rid of it when it moves.
686 */
687 he_cap_size =
688 2 + 1 + sizeof(he_cap->he_cap_elem) +
689 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
690 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
691 he_cap->he_cap_elem.phy_cap_info);
692 pos = skb_put(skb, he_cap_size);
693 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200694
695 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300696}
697
Johannes Berg66e67e42012-01-20 13:55:27 +0100698static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
699{
700 struct ieee80211_local *local = sdata->local;
701 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
702 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
703 struct sk_buff *skb;
704 struct ieee80211_mgmt *mgmt;
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200705 u8 *pos, qos_info, *ie_start;
Johannes Berg66e67e42012-01-20 13:55:27 +0100706 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200707 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100708 u16 capab;
709 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200710 struct ieee80211_chanctx_conf *chanctx_conf;
711 struct ieee80211_channel *chan;
Johannes Berg44f6d422017-06-10 13:52:44 +0300712 u32 rates = 0;
Thomas Pedersen05d10952020-09-21 19:28:10 -0700713 __le16 listen_int;
Johannes Berg2ff69b02020-01-31 13:31:11 +0200714 struct element *ext_capa = NULL;
715
716 /* we know it's writable, cast away the const */
717 if (assoc_data->ie_len)
718 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
719 assoc_data->ie,
720 assoc_data->ie_len);
Johannes Berg66e67e42012-01-20 13:55:27 +0100721
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200722 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100723
Johannes Berg55de9082012-07-26 17:24:39 +0200724 rcu_read_lock();
725 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
726 if (WARN_ON(!chanctx_conf)) {
727 rcu_read_unlock();
728 return;
729 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100730 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200731 rcu_read_unlock();
732 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200733 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100734
735 if (assoc_data->supp_rates_len) {
736 /*
737 * Get all rates supported by the device and the AP as
738 * some APs don't like getting a superset of their rates
739 * in the association request (e.g. D-Link DAP 1353 in
740 * b-only mode)...
741 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200742 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
743 assoc_data->supp_rates,
744 assoc_data->supp_rates_len,
745 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100746 } else {
747 /*
748 * In case AP not provide any supported rates information
749 * before association, we send information element(s) with
750 * all rates that we support.
751 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200752 rates_len = 0;
753 for (i = 0; i < sband->n_bitrates; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200754 rates |= BIT(i);
755 rates_len++;
756 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100757 }
758
759 skb = alloc_skb(local->hw.extra_tx_headroom +
760 sizeof(*mgmt) + /* bit too much but doesn't matter */
761 2 + assoc_data->ssid_len + /* SSID */
762 4 + rates_len + /* (extended) rates */
763 4 + /* power capability */
764 2 + 2 * sband->n_channels + /* supported channels */
765 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000766 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300767 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
768 sizeof(struct ieee80211_he_mcs_nss_supp) +
769 IEEE80211_HE_PPE_THRES_MAX_LEN +
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200770 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100771 assoc_data->ie_len + /* extra IEs */
Jouni Malinen39404fe2016-10-27 00:42:05 +0300772 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
Johannes Berg66e67e42012-01-20 13:55:27 +0100773 9, /* WMM */
774 GFP_KERNEL);
775 if (!skb)
776 return;
777
778 skb_reserve(skb, local->hw.extra_tx_headroom);
779
780 capab = WLAN_CAPABILITY_ESS;
781
Johannes Berg57fbcce2016-04-12 15:56:15 +0200782 if (sband->band == NL80211_BAND_2GHZ) {
Johannes Bergea1b2b452015-06-02 20:15:49 +0200783 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
784 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Johannes Berg66e67e42012-01-20 13:55:27 +0100785 }
786
787 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
788 capab |= WLAN_CAPABILITY_PRIVACY;
789
790 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200791 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
Johannes Berg66e67e42012-01-20 13:55:27 +0100792 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
793
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300794 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
795 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
796
Johannes Bergb080db52017-06-16 14:29:19 +0200797 mgmt = skb_put_zero(skb, 24);
Johannes Berg66e67e42012-01-20 13:55:27 +0100798 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
799 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
800 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
801
Thomas Pedersen05d10952020-09-21 19:28:10 -0700802 listen_int = cpu_to_le16(sband->band == NL80211_BAND_S1GHZ ?
803 ieee80211_encode_usf(local->hw.conf.listen_interval) :
804 local->hw.conf.listen_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +0100805 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
806 skb_put(skb, 10);
807 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
808 IEEE80211_STYPE_REASSOC_REQ);
809 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
Thomas Pedersen05d10952020-09-21 19:28:10 -0700810 mgmt->u.reassoc_req.listen_interval = listen_int;
Johannes Berg66e67e42012-01-20 13:55:27 +0100811 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
812 ETH_ALEN);
813 } else {
814 skb_put(skb, 4);
815 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
816 IEEE80211_STYPE_ASSOC_REQ);
817 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Thomas Pedersen05d10952020-09-21 19:28:10 -0700818 mgmt->u.assoc_req.listen_interval = listen_int;
Johannes Berg66e67e42012-01-20 13:55:27 +0100819 }
820
821 /* SSID */
822 pos = skb_put(skb, 2 + assoc_data->ssid_len);
Jouni Malinen4d9ec732019-02-15 02:14:33 +0200823 ie_start = pos;
Johannes Berg66e67e42012-01-20 13:55:27 +0100824 *pos++ = WLAN_EID_SSID;
825 *pos++ = assoc_data->ssid_len;
826 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
827
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700828 if (sband->band == NL80211_BAND_S1GHZ)
829 goto skip_rates;
830
Johannes Berg66e67e42012-01-20 13:55:27 +0100831 /* add all rates which were marked to be used above */
832 supp_rates_len = rates_len;
833 if (supp_rates_len > 8)
834 supp_rates_len = 8;
835
836 pos = skb_put(skb, supp_rates_len + 2);
837 *pos++ = WLAN_EID_SUPP_RATES;
838 *pos++ = supp_rates_len;
839
840 count = 0;
841 for (i = 0; i < sband->n_bitrates; i++) {
842 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200843 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
844 5 * (1 << shift));
845 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100846 if (++count == 8)
847 break;
848 }
849 }
850
851 if (rates_len > count) {
852 pos = skb_put(skb, rates_len - count + 2);
853 *pos++ = WLAN_EID_EXT_SUPP_RATES;
854 *pos++ = rates_len - count;
855
856 for (i++; i < sband->n_bitrates; i++) {
857 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200858 int rate;
859 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
860 5 * (1 << shift));
861 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100862 }
863 }
864 }
865
Thomas Pedersen1d00ce82020-09-21 19:28:15 -0700866skip_rates:
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300867 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
868 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100869 pos = skb_put(skb, 4);
870 *pos++ = WLAN_EID_PWR_CAPABILITY;
871 *pos++ = 2;
872 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200873 /* max tx power */
874 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Assaf Krausscd2f5dd2014-09-03 15:25:02 +0300875 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100876
Johannes Berg2ff69b02020-01-31 13:31:11 +0200877 /*
878 * Per spec, we shouldn't include the list of channels if we advertise
879 * support for extended channel switching, but we've always done that;
880 * (for now?) apply this restriction only on the (new) 6 GHz band.
881 */
882 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
883 (sband->band != NL80211_BAND_6GHZ ||
884 !ext_capa || ext_capa->datalen < 1 ||
885 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100886 /* TODO: get this in reg domain format */
887 pos = skb_put(skb, 2 * sband->n_channels + 2);
888 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
889 *pos++ = 2 * sband->n_channels;
890 for (i = 0; i < sband->n_channels; i++) {
891 *pos++ = ieee80211_frequency_to_channel(
892 sband->channels[i].center_freq);
893 *pos++ = 1; /* one channel in the subband*/
894 }
895 }
896
Sara Sharoncaf56332019-01-16 23:03:25 +0200897 /* Set MBSSID support for HE AP if needed */
898 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
Johannes Berg2ff69b02020-01-31 13:31:11 +0200899 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
900 ext_capa && ext_capa->datalen >= 3)
901 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
Sara Sharoncaf56332019-01-16 23:03:25 +0200902
Johannes Berg66e67e42012-01-20 13:55:27 +0100903 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200904 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100905 static const u8 before_ht[] = {
906 WLAN_EID_SSID,
907 WLAN_EID_SUPP_RATES,
908 WLAN_EID_EXT_SUPP_RATES,
909 WLAN_EID_PWR_CAPABILITY,
910 WLAN_EID_SUPPORTED_CHANNELS,
911 WLAN_EID_RSN,
912 WLAN_EID_QOS_CAPA,
913 WLAN_EID_RRM_ENABLED_CAPABILITIES,
914 WLAN_EID_MOBILITY_DOMAIN,
Johannes Berg8ed28742014-10-27 12:03:19 +0100915 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
916 WLAN_EID_RIC_DATA, /* reassoc only */
Johannes Berg66e67e42012-01-20 13:55:27 +0100917 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
918 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100919 static const u8 after_ric[] = {
920 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
921 WLAN_EID_HT_CAPABILITY,
922 WLAN_EID_BSS_COEX_2040,
Johannes Berga7f26d82017-08-05 11:44:32 +0300923 /* luckily this is almost always there */
Johannes Berg8ed28742014-10-27 12:03:19 +0100924 WLAN_EID_EXT_CAPABILITY,
925 WLAN_EID_QOS_TRAFFIC_CAPA,
926 WLAN_EID_TIM_BCAST_REQ,
927 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300928 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg8ed28742014-10-27 12:03:19 +0100929 WLAN_EID_VHT_CAPABILITY,
930 WLAN_EID_OPMODE_NOTIF,
931 };
932
933 noffset = ieee80211_ie_split_ric(assoc_data->ie,
934 assoc_data->ie_len,
935 before_ht,
936 ARRAY_SIZE(before_ht),
937 after_ric,
938 ARRAY_SIZE(after_ric),
939 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800940 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +0100941 offset = noffset;
942 }
943
Johannes Bergf2d9d272012-11-22 14:11:39 +0100944 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
945 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
946 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
947
Johannes Berg57fa5e82020-05-28 21:34:36 +0200948 if (sband->band != NL80211_BAND_6GHZ &&
949 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200950 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200951 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100952
Johannes Berg3de38022014-02-04 09:54:07 +0100953 /* if present, add any custom IEs that go before VHT */
954 if (assoc_data->ie_len) {
955 static const u8 before_vht[] = {
Johannes Berga7f26d82017-08-05 11:44:32 +0300956 /*
957 * no need to list the ones split off before HT
958 * or generated here
959 */
Johannes Berg3de38022014-02-04 09:54:07 +0100960 WLAN_EID_BSS_COEX_2040,
961 WLAN_EID_EXT_CAPABILITY,
962 WLAN_EID_QOS_TRAFFIC_CAPA,
963 WLAN_EID_TIM_BCAST_REQ,
964 WLAN_EID_INTERWORKING,
Johannes Berga7f26d82017-08-05 11:44:32 +0300965 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
Johannes Berg3de38022014-02-04 09:54:07 +0100966 };
Johannes Berg8ed28742014-10-27 12:03:19 +0100967
968 /* RIC already taken above, so no need to handle here anymore */
Johannes Berg3de38022014-02-04 09:54:07 +0100969 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
970 before_vht, ARRAY_SIZE(before_vht),
971 offset);
yuan linyub952f4d2017-06-18 22:52:04 +0800972 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg3de38022014-02-04 09:54:07 +0100973 offset = noffset;
974 }
975
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300976 /* if present, add any custom IEs that go before HE */
977 if (assoc_data->ie_len) {
978 static const u8 before_he[] = {
979 /*
980 * no need to list the ones split off before VHT
981 * or generated here
982 */
983 WLAN_EID_OPMODE_NOTIF,
984 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
985 /* 11ai elements */
986 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
987 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
988 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
989 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
990 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
991 /* TODO: add 11ah/11aj/11ak elements */
992 };
993
994 /* RIC already taken above, so no need to handle here anymore */
995 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
996 before_he, ARRAY_SIZE(before_he),
997 offset);
998 pos = skb_put(skb, noffset - offset);
999 memcpy(pos, assoc_data->ie + offset, noffset - offset);
1000 offset = noffset;
1001 }
1002
Johannes Berg57fa5e82020-05-28 21:34:36 +02001003 if (sband->band != NL80211_BAND_6GHZ &&
1004 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +01001005 ieee80211_add_vht_ie(sdata, skb, sband,
1006 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +00001007
Shaul Triebitzdc7eb0f2018-12-15 11:03:20 +02001008 /*
1009 * If AP doesn't support HT, mark HE as disabled.
1010 * If on the 5GHz band, make sure it supports VHT.
1011 */
1012 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
1013 (sband->band == NL80211_BAND_5GHZ &&
1014 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
1015 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
1016
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001017 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1018 ieee80211_add_he_ie(sdata, skb, sband);
1019
1020 /* if present, add any custom non-vendor IEs that go after HE */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001021 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001022 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1023 assoc_data->ie_len,
1024 offset);
yuan linyub952f4d2017-06-18 22:52:04 +08001025 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001026 offset = noffset;
1027 }
1028
Johannes Berg76f03032012-03-08 15:02:05 +01001029 if (assoc_data->wmm) {
1030 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001031 qos_info = ifmgd->uapsd_queues;
1032 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +01001033 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1034 } else {
1035 qos_info = 0;
1036 }
1037
Arik Nemtsov40b861a2014-07-17 17:14:23 +03001038 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
Johannes Berg66e67e42012-01-20 13:55:27 +01001039 }
1040
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07001041 if (sband->band == NL80211_BAND_S1GHZ) {
1042 ieee80211_add_aid_request_ie(sdata, skb);
Thomas Pedersen7957c6c2020-09-21 19:28:05 -07001043 ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07001044 }
Thomas Pedersen7957c6c2020-09-21 19:28:05 -07001045
Johannes Berg66e67e42012-01-20 13:55:27 +01001046 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +02001047 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01001048 noffset = assoc_data->ie_len;
yuan linyub952f4d2017-06-18 22:52:04 +08001049 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
Johannes Berg66e67e42012-01-20 13:55:27 +01001050 }
1051
Jouni Malinen39404fe2016-10-27 00:42:05 +03001052 if (assoc_data->fils_kek_len &&
1053 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
1054 dev_kfree_skb(skb);
1055 return;
1056 }
1057
Jouni Malinen4d9ec732019-02-15 02:14:33 +02001058 pos = skb_tail_pointer(skb);
1059 kfree(ifmgd->assoc_req_ies);
1060 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1061 ifmgd->assoc_req_ies_len = pos - ie_start;
1062
Ilan Peerd4e36e52018-04-20 13:49:25 +03001063 drv_mgd_prepare_tx(local, sdata, 0);
Johannes Berga1845fc2012-06-27 13:18:36 +02001064
Johannes Berg66e67e42012-01-20 13:55:27 +01001065 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg30686bf2015-06-02 21:39:54 +02001066 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01001067 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1068 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +01001069 ieee80211_tx_skb(sdata, skb);
1070}
1071
Kalle Valo572e0012009-02-10 17:09:31 +02001072void ieee80211_send_pspoll(struct ieee80211_local *local,
1073 struct ieee80211_sub_if_data *sdata)
1074{
Kalle Valo572e0012009-02-10 17:09:31 +02001075 struct ieee80211_pspoll *pspoll;
1076 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +02001077
Kalle Valod8cd1892010-01-05 20:16:26 +02001078 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1079 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +02001080 return;
Kalle Valo572e0012009-02-10 17:09:31 +02001081
Kalle Valod8cd1892010-01-05 20:16:26 +02001082 pspoll = (struct ieee80211_pspoll *) skb->data;
1083 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +02001084
Johannes Berg62ae67b2009-11-18 18:42:05 +01001085 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1086 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +02001087}
1088
Johannes Berg965beda2009-04-16 13:17:24 +02001089void ieee80211_send_nullfunc(struct ieee80211_local *local,
1090 struct ieee80211_sub_if_data *sdata,
Johannes Berg076cdcb2015-09-24 16:14:55 +02001091 bool powersave)
Johannes Berg965beda2009-04-16 13:17:24 +02001092{
1093 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +02001094 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301095 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +02001096
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001097 /* Don't send NDPs when STA is connected HE */
1098 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1099 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1100 return;
1101
Ben Caradoc-Davies7c181f42018-03-19 12:57:44 +13001102 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1103 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
Kalle Valod8cd1892010-01-05 20:16:26 +02001104 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +02001105 return;
1106
Kalle Valod8cd1892010-01-05 20:16:26 +02001107 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +02001108 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +02001109 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +02001110
Seth Forshee6c17b772013-02-11 11:21:07 -06001111 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1112 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +02001113
Johannes Berg30686bf2015-06-02 21:39:54 +02001114 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Pontus Fuchsff40b422013-06-04 12:44:52 +02001115 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1116
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001117 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +05301118 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1119
Johannes Berg62ae67b2009-11-18 18:42:05 +01001120 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +02001121}
1122
Felix Fietkaud5242152010-01-08 18:06:26 +01001123static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1124 struct ieee80211_sub_if_data *sdata)
1125{
1126 struct sk_buff *skb;
1127 struct ieee80211_hdr *nullfunc;
1128 __le16 fc;
1129
1130 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1131 return;
1132
Luca Coelho41cbb0f2018-06-09 09:14:44 +03001133 /* Don't send NDPs when connected HE */
1134 if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1135 return;
1136
Felix Fietkaud5242152010-01-08 18:06:26 +01001137 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -07001138 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +01001139 return;
Joe Perchesd15b8452011-08-29 14:17:31 -07001140
Felix Fietkaud5242152010-01-08 18:06:26 +01001141 skb_reserve(skb, local->hw.extra_tx_headroom);
1142
Johannes Bergb080db52017-06-16 14:29:19 +02001143 nullfunc = skb_put_zero(skb, 30);
Felix Fietkaud5242152010-01-08 18:06:26 +01001144 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1145 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1146 nullfunc->frame_control = fc;
1147 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1148 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1149 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1150 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1151
1152 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1153 ieee80211_tx_skb(sdata, skb);
1154}
1155
Johannes Bergcc32abd2009-05-15 11:52:31 +02001156/* spectrum management related things */
1157static void ieee80211_chswitch_work(struct work_struct *work)
1158{
1159 struct ieee80211_sub_if_data *sdata =
1160 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +01001161 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001162 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001163 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001164
Johannes Berg9607e6b662009-12-23 13:15:31 +01001165 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +02001166 return;
1167
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001168 sdata_lock(sdata);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001169 mutex_lock(&local->mtx);
1170 mutex_lock(&local->chanctx_mtx);
1171
Johannes Berg77fdaa12009-07-07 03:45:17 +02001172 if (!ifmgd->associated)
1173 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001174
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001175 if (!sdata->vif.csa_active)
1176 goto out;
1177
1178 /*
1179 * using reservation isn't immediate as it may be deferred until later
1180 * with multi-vif. once reservation is complete it will re-schedule the
1181 * work with no reserved_chanctx so verify chandef to check if it
1182 * completed successfully
1183 */
1184
1185 if (sdata->reserved_chanctx) {
Ilan Peer0007e942018-08-31 11:31:10 +03001186 struct ieee80211_supported_band *sband = NULL;
1187 struct sta_info *mgd_sta = NULL;
1188 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1189
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001190 /*
1191 * with multi-vif csa driver may call ieee80211_csa_finish()
1192 * many times while waiting for other interfaces to use their
1193 * reservations
1194 */
1195 if (sdata->reserved_ready)
1196 goto out;
1197
Ilan Peer0007e942018-08-31 11:31:10 +03001198 if (sdata->vif.bss_conf.chandef.width !=
1199 sdata->csa_chandef.width) {
1200 /*
1201 * For managed interface, we need to also update the AP
1202 * station bandwidth and align the rate scale algorithm
1203 * on the bandwidth change. Here we only consider the
1204 * bandwidth of the new channel definition (as channel
1205 * switch flow does not have the full HT/VHT/HE
1206 * information), assuming that if additional changes are
1207 * required they would be done as part of the processing
1208 * of the next beacon from the AP.
1209 */
1210 switch (sdata->csa_chandef.width) {
1211 case NL80211_CHAN_WIDTH_20_NOHT:
1212 case NL80211_CHAN_WIDTH_20:
1213 default:
1214 bw = IEEE80211_STA_RX_BW_20;
1215 break;
1216 case NL80211_CHAN_WIDTH_40:
1217 bw = IEEE80211_STA_RX_BW_40;
1218 break;
1219 case NL80211_CHAN_WIDTH_80:
1220 bw = IEEE80211_STA_RX_BW_80;
1221 break;
1222 case NL80211_CHAN_WIDTH_80P80:
1223 case NL80211_CHAN_WIDTH_160:
1224 bw = IEEE80211_STA_RX_BW_160;
1225 break;
1226 }
1227
1228 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1229 sband =
1230 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1231 }
1232
1233 if (sdata->vif.bss_conf.chandef.width >
1234 sdata->csa_chandef.width) {
1235 mgd_sta->sta.bandwidth = bw;
1236 rate_control_rate_update(local, sband, mgd_sta,
1237 IEEE80211_RC_BW_CHANGED);
1238 }
1239
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001240 ret = ieee80211_vif_use_reserved_context(sdata);
1241 if (ret) {
1242 sdata_info(sdata,
1243 "failed to use reserved channel context, disconnecting (err=%d)\n",
1244 ret);
1245 ieee80211_queue_work(&sdata->local->hw,
1246 &ifmgd->csa_connection_drop_work);
1247 goto out;
1248 }
1249
Ilan Peer0007e942018-08-31 11:31:10 +03001250 if (sdata->vif.bss_conf.chandef.width <
1251 sdata->csa_chandef.width) {
1252 mgd_sta->sta.bandwidth = bw;
1253 rate_control_rate_update(local, sband, mgd_sta,
1254 IEEE80211_RC_BW_CHANGED);
1255 }
1256
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001257 goto out;
1258 }
1259
1260 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1261 &sdata->csa_chandef)) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001262 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001263 "failed to finalize channel switch, disconnecting\n");
Arik Nemtsov7578d572013-09-01 17:15:51 +03001264 ieee80211_queue_work(&sdata->local->hw,
1265 &ifmgd->csa_connection_drop_work);
1266 goto out;
1267 }
Karl Beldan675a0b02013-03-25 16:26:57 +01001268
Luciano Coelho0c21e632014-10-08 09:48:39 +03001269 ifmgd->csa_waiting_bcn = true;
Luciano Coelhof1d65582014-10-08 09:48:38 +03001270
Michal Kaziore5593f52014-04-09 15:45:36 +02001271 ieee80211_sta_reset_beacon_monitor(sdata);
1272 ieee80211_sta_reset_conn_monitor(sdata);
1273
Michal Kazior59af6922014-04-09 15:10:59 +02001274out:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001275 mutex_unlock(&local->chanctx_mtx);
1276 mutex_unlock(&local->mtx);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001277 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001278}
1279
Luciano Coelho0c21e632014-10-08 09:48:39 +03001280static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1281{
1282 struct ieee80211_local *local = sdata->local;
1283 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1284 int ret;
1285
1286 sdata_assert_lock(sdata);
1287
1288 WARN_ON(!sdata->vif.csa_active);
1289
1290 if (sdata->csa_block_tx) {
1291 ieee80211_wake_vif_queues(local, sdata,
1292 IEEE80211_QUEUE_STOP_REASON_CSA);
1293 sdata->csa_block_tx = false;
1294 }
1295
1296 sdata->vif.csa_active = false;
1297 ifmgd->csa_waiting_bcn = false;
1298
1299 ret = drv_post_channel_switch(sdata);
1300 if (ret) {
1301 sdata_info(sdata,
1302 "driver post channel switch failed, disconnecting\n");
1303 ieee80211_queue_work(&local->hw,
1304 &ifmgd->csa_connection_drop_work);
1305 return;
1306 }
Luciano Coelho688b1ec2014-12-16 16:01:39 +02001307
1308 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
Luciano Coelho0c21e632014-10-08 09:48:39 +03001309}
1310
Johannes Berg5ce6e432010-05-11 16:20:57 +02001311void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1312{
Johannes Berg55de9082012-07-26 17:24:39 +02001313 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1314 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001315
1316 trace_api_chswitch_done(sdata, success);
1317 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001318 sdata_info(sdata,
1319 "driver channel switch failed, disconnecting\n");
1320 ieee80211_queue_work(&sdata->local->hw,
1321 &ifmgd->csa_connection_drop_work);
1322 } else {
1323 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001324 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001325}
1326EXPORT_SYMBOL(ieee80211_chswitch_done);
1327
Kees Cook34f11cd2017-10-16 16:35:49 -07001328static void ieee80211_chswitch_timer(struct timer_list *t)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001329{
1330 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07001331 from_timer(sdata, t, u.mgd.chswitch_timer);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001332
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001333 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001334}
1335
Johannes Berg37799e52013-03-26 14:02:26 +01001336static void
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001337ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1338{
1339 struct ieee80211_local *local = sdata->local;
1340
1341 if (!local->ops->abort_channel_switch)
1342 return;
1343
1344 mutex_lock(&local->mtx);
1345
1346 mutex_lock(&local->chanctx_mtx);
1347 ieee80211_vif_unreserve_chanctx(sdata);
1348 mutex_unlock(&local->chanctx_mtx);
1349
1350 if (sdata->csa_block_tx)
1351 ieee80211_wake_vif_queues(local, sdata,
1352 IEEE80211_QUEUE_STOP_REASON_CSA);
1353
1354 sdata->csa_block_tx = false;
1355 sdata->vif.csa_active = false;
1356
1357 mutex_unlock(&local->mtx);
1358
1359 drv_abort_channel_switch(sdata);
1360}
1361
1362static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001363ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03001364 u64 timestamp, u32 device_timestamp,
1365 struct ieee802_11_elems *elems,
Johannes Berg04a161f2013-05-03 09:35:35 +02001366 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001367{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001368 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001369 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001370 struct cfg80211_bss *cbss = ifmgd->associated;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001371 struct ieee80211_chanctx_conf *conf;
Johannes Berg55de9082012-07-26 17:24:39 +02001372 struct ieee80211_chanctx *chanctx;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001373 enum nl80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001374 struct ieee80211_csa_ie csa_ie;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001375 struct ieee80211_channel_switch ch_switch;
Johannes Berg2a333a02020-05-28 21:34:35 +02001376 struct ieee80211_bss *bss;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001377 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001378
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001379 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001380
Johannes Berg37799e52013-03-26 14:02:26 +01001381 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001382 return;
1383
Johannes Bergb4f286a12013-03-26 14:13:58 +01001384 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001385 return;
1386
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001387 current_band = cbss->channel->band;
Johannes Berg2a333a02020-05-28 21:34:35 +02001388 bss = (void *)cbss->priv;
Luciano Coelho84469a42014-10-28 13:33:04 +02001389 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
Johannes Berg2a333a02020-05-28 21:34:35 +02001390 bss->vht_cap_info,
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001391 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001392 ifmgd->associated->bssid, &csa_ie);
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001393
1394 if (!res) {
1395 ch_switch.timestamp = timestamp;
1396 ch_switch.device_timestamp = device_timestamp;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001397 ch_switch.block_tx = csa_ie.mode;
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001398 ch_switch.chandef = csa_ie.chandef;
1399 ch_switch.count = csa_ie.count;
1400 ch_switch.delay = csa_ie.max_switch_time;
1401 }
1402
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001403 if (res < 0) {
Johannes Bergb4f286a12013-03-26 14:13:58 +01001404 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001405 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001406 return;
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001407 }
1408
Sara Sharonfafd2bc2019-02-06 13:17:15 +02001409 if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1410 if (res)
1411 ieee80211_sta_abort_chanswitch(sdata);
1412 else
1413 drv_channel_switch_rx_beacon(sdata, &ch_switch);
Sara Sharonb9cc81d2019-02-06 13:17:10 +02001414 return;
1415 } else if (sdata->vif.csa_active || res) {
1416 /* disregard subsequent announcements if already processing */
1417 return;
1418 }
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001419
Johannes Berg36609442020-11-29 17:30:54 +02001420 if (sdata->vif.bss_conf.chandef.chan->band !=
1421 csa_ie.chandef.chan->band) {
1422 sdata_info(sdata,
1423 "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1424 ifmgd->associated->bssid,
1425 csa_ie.chandef.chan->center_freq,
1426 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1427 csa_ie.chandef.center_freq2);
1428 goto lock_and_drop_connection;
1429 }
1430
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001431 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001432 IEEE80211_CHAN_DISABLED)) {
1433 sdata_info(sdata,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001434 "AP %pM switches to unsupported channel "
1435 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1436 "disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001437 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001438 csa_ie.chandef.chan->center_freq,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001439 csa_ie.chandef.chan->freq_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001440 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
Thomas Pedersenb60119602020-04-01 18:18:04 -07001441 csa_ie.chandef.freq1_offset,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001442 csa_ie.chandef.center_freq2);
Johannes Berg36609442020-11-29 17:30:54 +02001443 goto lock_and_drop_connection;
Johannes Berg85220d72013-03-25 18:29:27 +01001444 }
1445
Johannes Bergf84eaa12015-03-12 08:53:26 +02001446 if (cfg80211_chandef_identical(&csa_ie.chandef,
Sara Sharon97928752019-02-06 13:17:16 +02001447 &sdata->vif.bss_conf.chandef) &&
1448 (!csa_ie.mode || !beacon)) {
Johannes Bergf84eaa12015-03-12 08:53:26 +02001449 if (ifmgd->csa_ignored_same_chan)
1450 return;
1451 sdata_info(sdata,
1452 "AP %pM tries to chanswitch to same channel, ignore\n",
1453 ifmgd->associated->bssid);
1454 ifmgd->csa_ignored_same_chan = true;
1455 return;
1456 }
1457
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001458 /*
1459 * Drop all TDLS peers - either we disconnect or move to a different
1460 * channel from this point on. There's no telling what our peer will do.
1461 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1462 * have an incompatible wider chandef.
1463 */
1464 ieee80211_teardown_tdls_peers(sdata);
1465
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001466 mutex_lock(&local->mtx);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001467 mutex_lock(&local->chanctx_mtx);
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001468 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1469 lockdep_is_held(&local->chanctx_mtx));
1470 if (!conf) {
1471 sdata_info(sdata,
1472 "no channel context assigned to vif?, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001473 goto drop_connection;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001474 }
1475
1476 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1477
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001478 if (local->use_chanctx &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001479 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001480 sdata_info(sdata,
1481 "driver doesn't support chan-switch with channel contexts\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001482 goto drop_connection;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001483 }
1484
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001485 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1486 sdata_info(sdata,
1487 "preparing for channel switch failed, disconnecting\n");
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001488 goto drop_connection;
Luciano Coelho6d027bc2014-10-08 09:48:37 +03001489 }
1490
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001491 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1492 chanctx->mode, false);
1493 if (res) {
Johannes Berg55de9082012-07-26 17:24:39 +02001494 sdata_info(sdata,
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001495 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1496 res);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001497 goto drop_connection;
Johannes Berg55de9082012-07-26 17:24:39 +02001498 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001499 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001500
Michal Kazior59af6922014-04-09 15:10:59 +02001501 sdata->vif.csa_active = true;
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001502 sdata->csa_chandef = csa_ie.chandef;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001503 sdata->csa_block_tx = csa_ie.mode;
Johannes Bergf84eaa12015-03-12 08:53:26 +02001504 ifmgd->csa_ignored_same_chan = false;
Michal Kazior59af6922014-04-09 15:10:59 +02001505
1506 if (sdata->csa_block_tx)
Luciano Coelhoa46992b2014-06-13 16:30:07 +03001507 ieee80211_stop_vif_queues(local, sdata,
1508 IEEE80211_QUEUE_STOP_REASON_CSA);
Michal Kazior59af6922014-04-09 15:10:59 +02001509 mutex_unlock(&local->mtx);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001510
Luciano Coelho2f457292014-11-07 14:31:36 +02001511 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1512 csa_ie.count);
1513
Johannes Bergb4f286a12013-03-26 14:13:58 +01001514 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001515 /* use driver's channel switch callback */
Luciano Coelho0f791eb42014-10-08 09:48:40 +03001516 drv_channel_switch(local, sdata, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001517 return;
1518 }
1519
1520 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001521 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001522 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001523 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001524 mod_timer(&ifmgd->chswitch_timer,
Luciano Coelhoff1e4172014-10-28 13:33:05 +02001525 TU_TO_EXP_TIME((csa_ie.count - 1) *
1526 cbss->beacon_interval));
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001527 return;
Johannes Berg36609442020-11-29 17:30:54 +02001528 lock_and_drop_connection:
1529 mutex_lock(&local->mtx);
1530 mutex_lock(&local->chanctx_mtx);
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001531 drop_connection:
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001532 /*
1533 * This is just so that the disconnect flow will know that
1534 * we were trying to switch channel and failed. In case the
1535 * mode is 1 (we are not allowed to Tx), we will know not to
1536 * send a deauthentication frame. Those two fields will be
1537 * reset when the disconnection worker runs.
1538 */
1539 sdata->vif.csa_active = true;
Sara Sharon2bf973f2020-01-31 13:12:51 +02001540 sdata->csa_block_tx = csa_ie.mode;
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03001541
Johannes Bergf3b0bbb2015-03-12 08:53:25 +02001542 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1543 mutex_unlock(&local->chanctx_mtx);
1544 mutex_unlock(&local->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001545}
1546
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001547static bool
1548ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1549 struct ieee80211_channel *channel,
1550 const u8 *country_ie, u8 country_ie_len,
1551 const u8 *pwr_constr_elem,
1552 int *chan_pwr, int *pwr_reduction)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001553{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001554 struct ieee80211_country_ie_triplet *triplet;
1555 int chan = ieee80211_frequency_to_channel(channel->center_freq);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001556 int i, chan_increment;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001557 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001558
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001559 /* Invalid IE */
1560 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001561 return false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001562
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001563 triplet = (void *)(country_ie + 3);
1564 country_ie_len -= 3;
1565
1566 switch (channel->band) {
1567 default:
1568 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001569 fallthrough;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001570 case NL80211_BAND_2GHZ:
1571 case NL80211_BAND_60GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001572 chan_increment = 1;
1573 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001574 case NL80211_BAND_5GHZ:
Ilan Peer6fcb56c2020-05-28 21:34:45 +02001575 case NL80211_BAND_6GHZ:
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001576 chan_increment = 4;
1577 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001578 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001579
1580 /* find channel */
1581 while (country_ie_len >= 3) {
1582 u8 first_channel = triplet->chans.first_channel;
1583
1584 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1585 goto next;
1586
1587 for (i = 0; i < triplet->chans.num_channels; i++) {
1588 if (first_channel + i * chan_increment == chan) {
1589 have_chan_pwr = true;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001590 *chan_pwr = triplet->chans.max_power;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001591 break;
1592 }
1593 }
1594 if (have_chan_pwr)
1595 break;
1596
1597 next:
1598 triplet++;
1599 country_ie_len -= 3;
1600 }
1601
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001602 if (have_chan_pwr && pwr_constr_elem)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001603 *pwr_reduction = *pwr_constr_elem;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001604 else
1605 *pwr_reduction = 0;
1606
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001607 return have_chan_pwr;
1608}
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001609
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001610static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1611 struct ieee80211_channel *channel,
1612 const u8 *cisco_dtpc_ie,
1613 int *pwr_level)
1614{
1615 /* From practical testing, the first data byte of the DTPC element
1616 * seems to contain the requested dBm level, and the CLI on Cisco
1617 * APs clearly state the range is -127 to 127 dBm, which indicates
1618 * a signed byte, although it seemingly never actually goes negative.
1619 * The other byte seems to always be zero.
1620 */
1621 *pwr_level = (__s8)cisco_dtpc_ie[4];
1622}
1623
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001624static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1625 struct ieee80211_channel *channel,
1626 struct ieee80211_mgmt *mgmt,
1627 const u8 *country_ie, u8 country_ie_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001628 const u8 *pwr_constr_ie,
1629 const u8 *cisco_dtpc_ie)
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001630{
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001631 bool has_80211h_pwr = false, has_cisco_pwr = false;
1632 int chan_pwr = 0, pwr_reduction_80211h = 0;
1633 int pwr_level_cisco, pwr_level_80211h;
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001634 int new_ap_level;
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001635 __le16 capab = mgmt->u.probe_resp.capab_info;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001636
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001637 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
1638 return 0; /* TODO */
1639
Moshe Benjia5fee9c2014-12-14 11:05:50 +02001640 if (country_ie &&
1641 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1642 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001643 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1644 sdata, channel, country_ie, country_ie_len,
1645 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001646 pwr_level_80211h =
1647 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07001648 }
1649
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001650 if (cisco_dtpc_ie) {
1651 ieee80211_find_cisco_dtpc(
1652 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1653 has_cisco_pwr = true;
1654 }
1655
1656 if (!has_80211h_pwr && !has_cisco_pwr)
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001657 return 0;
1658
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001659 /* If we have both 802.11h and Cisco DTPC, apply both limits
1660 * by picking the smallest of the two power levels advertised.
1661 */
1662 if (has_80211h_pwr &&
1663 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
Johannes Berga87da0c2015-12-08 16:04:37 +02001664 new_ap_level = pwr_level_80211h;
1665
1666 if (sdata->ap_power_level == new_ap_level)
1667 return 0;
1668
John Linvillecef2fc12015-03-31 10:49:14 -04001669 sdata_dbg(sdata,
1670 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1671 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1672 sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001673 } else { /* has_cisco_pwr is always true here. */
Johannes Berga87da0c2015-12-08 16:04:37 +02001674 new_ap_level = pwr_level_cisco;
1675
1676 if (sdata->ap_power_level == new_ap_level)
1677 return 0;
1678
John Linvillecef2fc12015-03-31 10:49:14 -04001679 sdata_dbg(sdata,
1680 "Limiting TX power to %d dBm as advertised by %pM\n",
1681 pwr_level_cisco, sdata->u.mgd.bssid);
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07001682 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001683
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001684 sdata->ap_power_level = new_ap_level;
1685 if (__ieee80211_recalc_txpower(sdata))
1686 return BSS_CHANGED_TXPOWER;
1687 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001688}
1689
Johannes Berg965beda2009-04-16 13:17:24 +02001690/* powersave */
1691static void ieee80211_enable_ps(struct ieee80211_local *local,
1692 struct ieee80211_sub_if_data *sdata)
1693{
1694 struct ieee80211_conf *conf = &local->hw.conf;
1695
Johannes Bergd5edaed2009-04-22 23:02:51 +02001696 /*
1697 * If we are scanning right now then the parameters will
1698 * take effect when scan finishes.
1699 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001700 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001701 return;
1702
Johannes Berg965beda2009-04-16 13:17:24 +02001703 if (conf->dynamic_ps_timeout > 0 &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001704 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001705 mod_timer(&local->dynamic_ps_timer, jiffies +
1706 msecs_to_jiffies(conf->dynamic_ps_timeout));
1707 } else {
Johannes Berg30686bf2015-06-02 21:39:54 +02001708 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
Johannes Berg076cdcb2015-09-24 16:14:55 +02001709 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301710
Johannes Berg30686bf2015-06-02 21:39:54 +02001711 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1712 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001713 return;
1714
1715 conf->flags |= IEEE80211_CONF_PS;
1716 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001717 }
1718}
1719
1720static void ieee80211_change_ps(struct ieee80211_local *local)
1721{
1722 struct ieee80211_conf *conf = &local->hw.conf;
1723
1724 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001725 ieee80211_enable_ps(local, local->ps_sdata);
1726 } else if (conf->flags & IEEE80211_CONF_PS) {
1727 conf->flags &= ~IEEE80211_CONF_PS;
1728 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1729 del_timer_sync(&local->dynamic_ps_timer);
1730 cancel_work_sync(&local->dynamic_ps_enable_work);
1731 }
1732}
1733
Jason Young808118c2011-03-10 16:43:19 -08001734static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1735{
1736 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1737 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001738 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001739
1740 if (!mgd->powersave)
1741 return false;
1742
Johannes Berg05cb9102011-10-28 11:59:47 +02001743 if (mgd->broken_ap)
1744 return false;
1745
Jason Young808118c2011-03-10 16:43:19 -08001746 if (!mgd->associated)
1747 return false;
1748
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001749 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001750 return false;
1751
Alexander Bondar989c6502013-05-16 17:34:17 +03001752 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001753 return false;
1754
Jason Young808118c2011-03-10 16:43:19 -08001755 rcu_read_lock();
1756 sta = sta_info_get(sdata, mgd->bssid);
1757 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001758 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001759 rcu_read_unlock();
1760
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001761 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001762}
1763
Johannes Berg965beda2009-04-16 13:17:24 +02001764/* need to hold RTNL or interface lock */
Johannes Berg4a733ef2015-10-14 18:02:43 +02001765void ieee80211_recalc_ps(struct ieee80211_local *local)
Johannes Berg965beda2009-04-16 13:17:24 +02001766{
1767 struct ieee80211_sub_if_data *sdata, *found = NULL;
1768 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001769 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001770
Johannes Berg30686bf2015-06-02 21:39:54 +02001771 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
Johannes Berg965beda2009-04-16 13:17:24 +02001772 local->ps_sdata = NULL;
1773 return;
1774 }
1775
1776 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001777 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001778 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301779 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1780 /* If an AP vif is found, then disable PS
1781 * by setting the count to zero thereby setting
1782 * ps_sdata to NULL.
1783 */
1784 count = 0;
1785 break;
1786 }
Johannes Berg965beda2009-04-16 13:17:24 +02001787 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1788 continue;
1789 found = sdata;
1790 count++;
1791 }
1792
Jason Young808118c2011-03-10 16:43:19 -08001793 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg4a733ef2015-10-14 18:02:43 +02001794 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg10f644a2009-04-16 13:17:25 +02001795
Juuso Oikarinenff616382010-06-09 09:51:52 +03001796 timeout = local->dynamic_ps_forced_timeout;
Johannes Berg4a733ef2015-10-14 18:02:43 +02001797 if (timeout < 0)
1798 timeout = 100;
Johannes Berg09b85562013-02-06 23:07:41 +01001799 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001800
Johannes Berg4a733ef2015-10-14 18:02:43 +02001801 /* If the TIM IE is invalid, pretend the value is 1 */
1802 if (!dtimper)
1803 dtimper = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001804
Johannes Berg4a733ef2015-10-14 18:02:43 +02001805 local->hw.conf.ps_dtim_period = dtimper;
1806 local->ps_sdata = found;
Johannes Berg10f644a2009-04-16 13:17:25 +02001807 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001808 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001809 }
Johannes Berg965beda2009-04-16 13:17:24 +02001810
1811 ieee80211_change_ps(local);
1812}
1813
Eliad Pellerab095872012-07-27 12:33:22 +03001814void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1815{
1816 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1817
1818 if (sdata->vif.bss_conf.ps != ps_allowed) {
1819 sdata->vif.bss_conf.ps = ps_allowed;
1820 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1821 }
1822}
1823
Johannes Berg965beda2009-04-16 13:17:24 +02001824void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1825{
1826 struct ieee80211_local *local =
1827 container_of(work, struct ieee80211_local,
1828 dynamic_ps_disable_work);
1829
1830 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1831 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1832 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1833 }
1834
1835 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001836 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +03001837 IEEE80211_QUEUE_STOP_REASON_PS,
1838 false);
Johannes Berg965beda2009-04-16 13:17:24 +02001839}
1840
1841void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1842{
1843 struct ieee80211_local *local =
1844 container_of(work, struct ieee80211_local,
1845 dynamic_ps_enable_work);
1846 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001847 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301848 unsigned long flags;
1849 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001850
1851 /* can only happen when PS was just disabled anyway */
1852 if (!sdata)
1853 return;
1854
Christian Lamparter5e340692011-06-30 21:08:43 +02001855 ifmgd = &sdata->u.mgd;
1856
Johannes Berg965beda2009-04-16 13:17:24 +02001857 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1858 return;
1859
Johannes Berg09b85562013-02-06 23:07:41 +01001860 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001861 /* don't enter PS if TX frames are pending */
1862 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301863 mod_timer(&local->dynamic_ps_timer, jiffies +
1864 msecs_to_jiffies(
1865 local->hw.conf.dynamic_ps_timeout));
1866 return;
1867 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001868
1869 /*
1870 * transmission can be stopped by others which leads to
1871 * dynamic_ps_timer expiry. Postpone the ps timer if it
1872 * is not the actual idle state.
1873 */
1874 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1875 for (q = 0; q < local->hw.queues; q++) {
1876 if (local->queue_stop_reasons[q]) {
1877 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1878 flags);
1879 mod_timer(&local->dynamic_ps_timer, jiffies +
1880 msecs_to_jiffies(
1881 local->hw.conf.dynamic_ps_timeout));
1882 return;
1883 }
1884 }
1885 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301886 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301887
Johannes Berg30686bf2015-06-02 21:39:54 +02001888 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301889 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001890 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301891 mod_timer(&local->dynamic_ps_timer, jiffies +
1892 msecs_to_jiffies(
1893 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001894 } else {
Johannes Berg076cdcb2015-09-24 16:14:55 +02001895 ieee80211_send_nullfunc(local, sdata, true);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301896 /* Flush to get the tx status of nullfunc frame */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001897 ieee80211_flush_queues(local, sdata, false);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301898 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301899 }
1900
Johannes Berg30686bf2015-06-02 21:39:54 +02001901 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1902 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301903 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1904 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1905 local->hw.conf.flags |= IEEE80211_CONF_PS;
1906 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1907 }
Johannes Berg965beda2009-04-16 13:17:24 +02001908}
1909
Kees Cook34f11cd2017-10-16 16:35:49 -07001910void ieee80211_dynamic_ps_timer(struct timer_list *t)
Johannes Berg965beda2009-04-16 13:17:24 +02001911{
Kees Cook34f11cd2017-10-16 16:35:49 -07001912 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
Johannes Berg965beda2009-04-16 13:17:24 +02001913
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001914 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001915}
1916
Simon Wunderlich164eb022013-02-08 18:16:20 +01001917void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1918{
Geliang Tanga85a7e22016-01-01 23:48:52 +08001919 struct delayed_work *delayed_work = to_delayed_work(work);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001920 struct ieee80211_sub_if_data *sdata =
1921 container_of(delayed_work, struct ieee80211_sub_if_data,
1922 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001923 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001924
Johannes Berg34a37402013-12-18 09:43:33 +01001925 mutex_lock(&sdata->local->mtx);
1926 if (sdata->wdev.cac_started) {
1927 ieee80211_vif_release_channel(sdata);
1928 cfg80211_cac_event(sdata->dev, &chandef,
1929 NL80211_RADAR_CAC_FINISHED,
1930 GFP_KERNEL);
1931 }
1932 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001933}
1934
Johannes Berg02219b32014-10-07 10:38:50 +03001935static bool
1936__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1937{
1938 struct ieee80211_local *local = sdata->local;
1939 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
John Linvillecc72f6e2015-01-06 14:39:33 -05001940 bool ret = false;
Johannes Berg02219b32014-10-07 10:38:50 +03001941 int ac;
1942
1943 if (local->hw.queues < IEEE80211_NUM_ACS)
1944 return false;
1945
1946 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1947 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1948 int non_acm_ac;
1949 unsigned long now = jiffies;
1950
1951 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1952 tx_tspec->admitted_time &&
1953 time_after(now, tx_tspec->time_slice_start + HZ)) {
1954 tx_tspec->consumed_tx_time = 0;
1955 tx_tspec->time_slice_start = now;
1956
1957 if (tx_tspec->downgraded)
1958 tx_tspec->action =
1959 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1960 }
1961
1962 switch (tx_tspec->action) {
1963 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1964 /* take the original parameters */
1965 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1966 sdata_err(sdata,
1967 "failed to set TX queue parameters for queue %d\n",
1968 ac);
1969 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1970 tx_tspec->downgraded = false;
1971 ret = true;
1972 break;
1973 case TX_TSPEC_ACTION_DOWNGRADE:
1974 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1975 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1976 ret = true;
1977 break;
1978 }
1979 /* downgrade next lower non-ACM AC */
1980 for (non_acm_ac = ac + 1;
1981 non_acm_ac < IEEE80211_NUM_ACS;
1982 non_acm_ac++)
1983 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1984 break;
Johannes Berg93db1d92016-09-14 09:23:51 +02001985 /* Usually the loop will result in using BK even if it
1986 * requires admission control, but such a configuration
1987 * makes no sense and we have to transmit somehow - the
1988 * AC selection does the same thing.
1989 * If we started out trying to downgrade from BK, then
1990 * the extra condition here might be needed.
Johannes Berg02219b32014-10-07 10:38:50 +03001991 */
Johannes Berg93db1d92016-09-14 09:23:51 +02001992 if (non_acm_ac >= IEEE80211_NUM_ACS)
1993 non_acm_ac = IEEE80211_AC_BK;
Johannes Berg02219b32014-10-07 10:38:50 +03001994 if (drv_conf_tx(local, sdata, ac,
1995 &sdata->tx_conf[non_acm_ac]))
1996 sdata_err(sdata,
1997 "failed to set TX queue parameters for queue %d\n",
1998 ac);
1999 tx_tspec->action = TX_TSPEC_ACTION_NONE;
2000 ret = true;
2001 schedule_delayed_work(&ifmgd->tx_tspec_wk,
2002 tx_tspec->time_slice_start + HZ - now + 1);
2003 break;
2004 case TX_TSPEC_ACTION_NONE:
2005 /* nothing now */
2006 break;
2007 }
2008 }
2009
2010 return ret;
2011}
2012
2013void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
2014{
2015 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
2016 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
2017}
2018
2019static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
2020{
2021 struct ieee80211_sub_if_data *sdata;
2022
2023 sdata = container_of(work, struct ieee80211_sub_if_data,
2024 u.mgd.tx_tspec_wk.work);
2025 ieee80211_sta_handle_tspec_ac_params(sdata);
2026}
2027
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002028/* MLME */
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002029static bool
2030ieee80211_sta_wmm_params(struct ieee80211_local *local,
2031 struct ieee80211_sub_if_data *sdata,
2032 const u8 *wmm_param, size_t wmm_param_len,
2033 const struct ieee80211_mu_edca_param_set *mu_edca)
Jiri Bencf0706e82007-05-05 11:45:53 -07002034{
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002035 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
Johannes Berg4ced3f72010-07-19 16:39:04 +02002036 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002037 size_t left;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002038 int count, mu_edca_count, ac;
Johannes Berg4a3cb702013-02-12 16:43:19 +01002039 const u8 *pos;
2040 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002041
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002042 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02002043 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002044
Johannes Berg32c50572012-03-28 11:04:29 +02002045 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02002046 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02002047
2048 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02002049 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02002050
Jiri Bencf0706e82007-05-05 11:45:53 -07002051 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02002052 return false;
Kalle Valoab133152010-01-12 10:42:31 +02002053
2054 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02002055 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02002056
Jiri Bencf0706e82007-05-05 11:45:53 -07002057 count = wmm_param[6] & 0x0f;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002058 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2059 * if mu_edca was preset before and now it disappeared tell
2060 * the driver about it.
2061 */
2062 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2063 if (count == ifmgd->wmm_last_param_set &&
2064 mu_edca_count == ifmgd->mu_edca_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02002065 return false;
Johannes Berg46900292009-02-15 12:44:28 +01002066 ifmgd->wmm_last_param_set = count;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02002067 ifmgd->mu_edca_last_param_set = mu_edca_count;
Jiri Bencf0706e82007-05-05 11:45:53 -07002068
2069 pos = wmm_param + 8;
2070 left = wmm_param_len - 8;
2071
2072 memset(&params, 0, sizeof(params));
2073
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002074 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002075 for (; left >= 4; left -= 4, pos += 4) {
2076 int aci = (pos[0] >> 5) & 0x03;
2077 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02002078 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002079
2080 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002081 case 1: /* AC_BK */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002082 ac = IEEE80211_AC_BK;
Johannes Berg988c0f72008-04-17 19:21:22 +02002083 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002084 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02002085 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2086 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002087 params[ac].mu_edca = !!mu_edca;
2088 if (mu_edca)
2089 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
Jiri Bencf0706e82007-05-05 11:45:53 -07002090 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002091 case 2: /* AC_VI */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002092 ac = IEEE80211_AC_VI;
Johannes Berg988c0f72008-04-17 19:21:22 +02002093 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002094 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02002095 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2096 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002097 params[ac].mu_edca = !!mu_edca;
2098 if (mu_edca)
2099 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
Jiri Bencf0706e82007-05-05 11:45:53 -07002100 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002101 case 3: /* AC_VO */
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002102 ac = IEEE80211_AC_VO;
Johannes Berg988c0f72008-04-17 19:21:22 +02002103 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002104 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02002105 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2106 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002107 params[ac].mu_edca = !!mu_edca;
2108 if (mu_edca)
2109 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
Jiri Bencf0706e82007-05-05 11:45:53 -07002110 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02002111 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07002112 default:
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002113 ac = IEEE80211_AC_BE;
Johannes Berg988c0f72008-04-17 19:21:22 +02002114 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002115 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02002116 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2117 uapsd = true;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03002118 params[ac].mu_edca = !!mu_edca;
2119 if (mu_edca)
2120 params[ac].mu_edca_param_rec = mu_edca->ac_be;
Jiri Bencf0706e82007-05-05 11:45:53 -07002121 break;
2122 }
2123
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002124 params[ac].aifs = pos[0] & 0x0f;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002125
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002126 if (params[ac].aifs < 2) {
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002127 sdata_info(sdata,
2128 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002129 params[ac].aifs, aci);
2130 params[ac].aifs = 2;
Emmanuel Grumbach730a75502015-10-22 17:46:05 +02002131 }
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002132 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2133 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2134 params[ac].txop = get_unaligned_le16(pos + 2);
2135 params[ac].acm = acm;
2136 params[ac].uapsd = uapsd;
Kalle Valoab133152010-01-12 10:42:31 +02002137
Ilan Peer911a2642018-04-03 11:35:22 +03002138 if (params[ac].cw_min == 0 ||
Emmanuel Grumbachc470bdc2018-03-26 16:21:04 +03002139 params[ac].cw_min > params[ac].cw_max) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002140 sdata_info(sdata,
2141 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2142 params[ac].cw_min, params[ac].cw_max, aci);
2143 return false;
2144 }
Haim Dreyfusse552af02018-03-28 13:24:10 +03002145 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002146 }
2147
Brian Norris05aaa5c2019-07-26 15:47:58 -07002148 /* WMM specification requires all 4 ACIs. */
2149 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2150 if (params[ac].cw_min == 0) {
2151 sdata_info(sdata,
2152 "AP has invalid WMM params (missing AC %d), using defaults\n",
2153 ac);
2154 return false;
2155 }
2156 }
2157
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002158 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002159 mlme_dbg(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002160 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2161 ac, params[ac].acm,
2162 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2163 params[ac].txop, params[ac].uapsd,
2164 ifmgd->tx_tspec[ac].downgraded);
2165 sdata->tx_conf[ac] = params[ac];
2166 if (!ifmgd->tx_tspec[ac].downgraded &&
2167 drv_conf_tx(local, sdata, ac, &params[ac]))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002168 sdata_err(sdata,
Johannes Berg2ed77ea2015-10-22 17:46:06 +02002169 "failed to set TX queue parameters for AC %d\n",
2170 ac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002171 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02002172
2173 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02002174 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02002175 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002176}
2177
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002178static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2179{
2180 lockdep_assert_held(&sdata->local->mtx);
2181
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002182 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002183 ieee80211_run_deferred_scan(sdata->local);
2184}
2185
2186static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2187{
2188 mutex_lock(&sdata->local->mtx);
2189 __ieee80211_stop_poll(sdata);
2190 mutex_unlock(&sdata->local->mtx);
2191}
2192
Johannes Berg7a5158e2008-10-08 10:59:33 +02002193static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2194 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02002195{
Johannes Bergbda39332008-10-11 01:51:51 +02002196 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302197 struct ieee80211_supported_band *sband;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002198 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002199 bool use_protection;
2200 bool use_short_preamble;
2201 bool use_short_slot;
2202
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05302203 sband = ieee80211_get_sband(sdata);
2204 if (!sband)
2205 return changed;
2206
Johannes Berg7a5158e2008-10-08 10:59:33 +02002207 if (erp_valid) {
2208 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2209 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2210 } else {
2211 use_protection = false;
2212 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2213 }
2214
2215 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Tova Mussai07c12d62020-05-28 21:34:46 +02002216 if (sband->band == NL80211_BAND_5GHZ ||
2217 sband->band == NL80211_BAND_6GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01002218 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02002219
Johannes Berg471b3ef2007-12-28 14:32:58 +01002220 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01002221 bss_conf->use_cts_prot = use_protection;
2222 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02002223 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02002224
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002225 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02002226 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002227 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02002228 }
Daniel Draked9430a32007-07-27 15:43:24 +02002229
Johannes Berg7a5158e2008-10-08 10:59:33 +02002230 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02002231 bss_conf->use_short_slot = use_short_slot;
2232 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04002233 }
2234
2235 return changed;
2236}
2237
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002238static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002239 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02002240 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07002241{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002242 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002243 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002244 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002245
Johannes Bergae5eb022008-10-14 16:58:37 +02002246 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002247 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03002248 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002249
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002250 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07002251 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01002252
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002253 sdata->u.mgd.associated = cbss;
2254 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01002255
Johannes Berge8e4f522017-03-08 11:12:10 +01002256 ieee80211_check_rate_mask(sdata);
2257
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002258 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2259
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01002260 if (sdata->vif.p2p ||
2261 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002262 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002263
Johannes Berg9caf0362012-11-29 01:25:20 +01002264 rcu_read_lock();
2265 ies = rcu_dereference(cbss->ies);
2266 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01002267 int ret;
2268
2269 ret = cfg80211_get_p2p_attr(
2270 ies->data, ies->len,
2271 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002272 (u8 *) &bss_conf->p2p_noa_attr,
2273 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01002274 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002275 sdata->u.mgd.p2p_noa_index =
2276 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01002277 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01002278 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01002279 }
Johannes Berg9caf0362012-11-29 01:25:20 +01002280 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01002281 }
2282
Johannes Bergb291ba12009-07-10 15:29:03 +02002283 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002284 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002285
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002286 ieee80211_led_assoc(local, 1);
2287
Alexander Bondar989c6502013-05-16 17:34:17 +03002288 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02002289 /*
2290 * If the AP is buggy we may get here with no DTIM period
2291 * known, so assume it's 1 which is the only safe assumption
2292 * in that case, although if the TIM IE is broken powersave
2293 * probably just won't work at all.
2294 */
2295 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03002296 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03002297 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02002298 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03002299 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02002300 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02002301 }
Johannes Berge5b900d2010-07-29 16:08:55 +02002302
Juuso Oikarinen68542962010-06-09 13:43:26 +03002303 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02002304
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002305 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01002306 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03002307 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002308 bss_info_changed |= BSS_CHANGED_CQM;
2309
Juuso Oikarinen68542962010-06-09 13:43:26 +03002310 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002311 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002312 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002313
Johannes Bergae5eb022008-10-14 16:58:37 +02002314 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03002315
Johannes Berg056508d2009-07-30 21:43:55 +02002316 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002317 ieee80211_recalc_ps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02002318 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02002319
Johannes Berg04ecd252012-09-11 14:34:12 +02002320 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03002321 ieee80211_recalc_ps_vif(sdata);
2322
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002323 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002324}
2325
Jouni Malinene69e95d2010-03-29 23:29:31 -07002326static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01002327 u16 stype, u16 reason, bool tx,
2328 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03002329{
Johannes Berg46900292009-02-15 12:44:28 +01002330 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002331 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01002332 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002333
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002334 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002335
Johannes Berg37ad3882012-02-24 13:50:54 +01002336 if (WARN_ON_ONCE(tx && !frame_buf))
2337 return;
2338
Johannes Bergb291ba12009-07-10 15:29:03 +02002339 if (WARN_ON(!ifmgd->associated))
2340 return;
2341
David Spinadel79543d82012-06-12 09:59:45 +03002342 ieee80211_stop_poll(sdata);
2343
Johannes Berg77fdaa12009-07-07 03:45:17 +02002344 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002345 netif_carrier_off(sdata->dev);
2346
Eliad Peller88bc40e2012-07-12 17:35:33 +03002347 /*
2348 * if we want to get out of ps before disassoc (why?) we have
2349 * to do it before sending disassoc, as otherwise the null-packet
2350 * won't be valid.
2351 */
2352 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2353 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2354 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2355 }
2356 local->ps_sdata = NULL;
2357
Eliad Pellerab095872012-07-27 12:33:22 +03002358 /* disable per-vif ps */
2359 ieee80211_recalc_ps_vif(sdata);
2360
Emmanuel Grumbach14f2ae82015-01-22 23:30:19 +02002361 /* make sure ongoing transmission finishes */
2362 synchronize_net();
2363
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002364 /*
2365 * drop any frame before deauth/disassoc, this can be data or
2366 * management frame. Since we are disconnecting, we should not
2367 * insist sending these frames which can take time and delay
2368 * the disconnection and possible the roaming.
2369 */
Eliad Pellerf8239812012-06-27 14:18:22 +03002370 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002371 ieee80211_flush_queues(local, sdata, true);
Eliad Pellerf8239812012-06-27 14:18:22 +03002372
Johannes Berg37ad3882012-02-24 13:50:54 +01002373 /* deauthenticate/disassociate now */
Ilan Peer94ba9272018-02-19 14:48:41 +02002374 if (tx || frame_buf) {
Ilan Peer94ba9272018-02-19 14:48:41 +02002375 /*
2376 * In multi channel scenarios guarantee that the virtual
2377 * interface is granted immediate airtime to transmit the
2378 * deauthentication frame by calling mgd_prepare_tx, if the
2379 * driver requested so.
2380 */
2381 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2382 !ifmgd->have_beacon)
Ilan Peerd4e36e52018-04-20 13:49:25 +03002383 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Ilan Peer94ba9272018-02-19 14:48:41 +02002384
Johannes Berg4b08d1b2019-08-30 14:24:51 +03002385 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2386 ifmgd->bssid, stype, reason,
2387 tx, frame_buf);
Ilan Peer94ba9272018-02-19 14:48:41 +02002388 }
Johannes Berg37ad3882012-02-24 13:50:54 +01002389
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002390 /* flush out frame - make sure the deauth was actually sent */
Johannes Berg37ad3882012-02-24 13:50:54 +01002391 if (tx)
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02002392 ieee80211_flush_queues(local, sdata, false);
Johannes Berg37ad3882012-02-24 13:50:54 +01002393
Eliad Peller88a9e312012-06-01 11:14:03 +03002394 /* clear bssid only after building the needed mgmt frames */
Joe Perchesc84a67a2015-03-02 19:54:57 -08002395 eth_zero_addr(ifmgd->bssid);
Eliad Peller88a9e312012-06-01 11:14:03 +03002396
Johannes Berg37ad3882012-02-24 13:50:54 +01002397 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01002398 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01002399
2400 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002401 changed |= ieee80211_reset_erp_info(sdata);
2402
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002403 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02002404 changed |= BSS_CHANGED_ASSOC;
2405 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002406
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002407 ifmgd->p2p_noa_index = -1;
2408 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2409 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002410
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002411 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08002412 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2413 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01002414 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2415 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002416
2417 /* reset MU-MIMO ownership and group data */
2418 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2419 sizeof(sdata->vif.bss_conf.mu_group.membership));
2420 memset(sdata->vif.bss_conf.mu_group.position, 0,
2421 sizeof(sdata->vif.bss_conf.mu_group.position));
2422 changed |= BSS_CHANGED_MU_GROUPS;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002423 sdata->vif.mu_mimo_owner = false;
Johannes Berg413ad502009-05-08 21:21:06 +02002424
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002425 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302426
Kalle Valo520eb822008-12-18 23:35:27 +02002427 del_timer_sync(&local->dynamic_ps_timer);
2428 cancel_work_sync(&local->dynamic_ps_enable_work);
2429
Juuso Oikarinen68542962010-06-09 13:43:26 +03002430 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01002431 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03002432 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03002433
Johannes Berg3abead52012-03-02 15:56:59 +01002434 sdata->vif.bss_conf.qos = false;
2435 changed |= BSS_CHANGED_QOS;
2436
Johannes Berg0aaffa92010-05-05 15:28:27 +02002437 /* The BSSID (not really interesting) and HT changed */
2438 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02002439 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02002440
Johannes Berg3abead52012-03-02 15:56:59 +01002441 /* disassociated - set to defaults now */
Johannes Bergcec66282015-10-22 17:46:04 +02002442 ieee80211_set_wmm_default(sdata, false, false);
Johannes Berg3abead52012-03-02 15:56:59 +01002443
Johannes Bergb9dcf712010-08-27 12:35:54 +02002444 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2445 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2446 del_timer_sync(&sdata->u.mgd.timer);
2447 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02002448
Johannes Berg826262c2012-12-10 16:38:14 +02002449 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03002450 sdata->vif.bss_conf.beacon_rate = NULL;
2451
Alexander Bondar989c6502013-05-16 17:34:17 +03002452 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02002453
Johannes Berg028e8da02012-11-26 11:57:41 +01002454 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002455 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01002456 ieee80211_vif_release_channel(sdata);
Michal Kazior59af6922014-04-09 15:10:59 +02002457
2458 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002459 ifmgd->csa_waiting_bcn = false;
Johannes Bergf84eaa12015-03-12 08:53:26 +02002460 ifmgd->csa_ignored_same_chan = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002461 if (sdata->csa_block_tx) {
2462 ieee80211_wake_vif_queues(local, sdata,
2463 IEEE80211_QUEUE_STOP_REASON_CSA);
2464 sdata->csa_block_tx = false;
2465 }
Johannes Berg34a37402013-12-18 09:43:33 +01002466 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002467
Johannes Berg02219b32014-10-07 10:38:50 +03002468 /* existing TX TSPEC sessions no longer exist */
2469 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2470 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2471
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002472 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Tomas Winkleraa458d12008-09-09 00:32:12 +03002473}
Jiri Bencf0706e82007-05-05 11:45:53 -07002474
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002475static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2476{
2477 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002478 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002479
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002480 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002481 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2482 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002483
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002484 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002485
2486 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002487 ieee80211_recalc_ps(local);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002488 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002489
Johannes Berg30686bf2015-06-02 21:39:54 +02002490 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002491 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002492
2493 /*
2494 * We've received a probe response, but are not sure whether
2495 * we have or will be receiving any beacons or data, so let's
2496 * schedule the timers again, just in case.
2497 */
2498 ieee80211_sta_reset_beacon_monitor(sdata);
2499
2500 mod_timer(&ifmgd->conn_mon_timer,
2501 round_jiffies_up(jiffies +
2502 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002503out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002504 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002505}
2506
Johannes Berg02219b32014-10-07 10:38:50 +03002507static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2508 struct ieee80211_hdr *hdr,
2509 u16 tx_time)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002510{
Johannes Berg02219b32014-10-07 10:38:50 +03002511 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02002512 u16 tid = ieee80211_get_tid(hdr);
Johannes Berg02219b32014-10-07 10:38:50 +03002513 int ac = ieee80211_ac_from_tid(tid);
2514 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2515 unsigned long now = jiffies;
2516
2517 if (likely(!tx_tspec->admitted_time))
2518 return;
2519
2520 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2521 tx_tspec->consumed_tx_time = 0;
2522 tx_tspec->time_slice_start = now;
2523
2524 if (tx_tspec->downgraded) {
2525 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2526 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2527 }
2528 }
2529
2530 if (tx_tspec->downgraded)
2531 return;
2532
2533 tx_tspec->consumed_tx_time += tx_time;
2534
2535 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2536 tx_tspec->downgraded = true;
2537 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2538 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2539 }
2540}
2541
2542void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2543 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2544{
2545 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2546
Felix Fietkau9abf4e42020-09-08 14:36:56 +02002547 if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
2548 !sdata->u.mgd.probe_send_count)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002549 return;
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002550
Felix Fietkaue3f25902020-09-27 12:56:05 +02002551 if (ack)
2552 sdata->u.mgd.probe_send_count = 0;
2553 else
Felix Fietkau9abf4e42020-09-08 14:36:56 +02002554 sdata->u.mgd.nullfunc_failed = true;
2555 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002556}
2557
Johannes Berg45ad6832018-05-28 15:47:39 +02002558static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2559 const u8 *src, const u8 *dst,
2560 const u8 *ssid, size_t ssid_len,
2561 struct ieee80211_channel *channel)
2562{
2563 struct sk_buff *skb;
2564
2565 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2566 ssid, ssid_len, NULL, 0,
2567 IEEE80211_PROBE_FLAG_DIRECTED);
2568 if (skb)
2569 ieee80211_tx_skb(sdata, skb);
2570}
2571
Maxim Levitskya43abf22009-07-31 18:54:12 +03002572static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2573{
2574 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2575 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002576 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002577 u8 unicast_limit = max(1, max_probe_tries - 3);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002578 struct sta_info *sta;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002579
2580 /*
2581 * Try sending broadcast probe requests for the last three
2582 * probe requests after the first ones failed since some
2583 * buggy APs only support broadcast probe requests.
2584 */
2585 if (ifmgd->probe_send_count >= unicast_limit)
2586 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002587
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002588 /*
2589 * When the hardware reports an accurate Tx ACK status, it's
2590 * better to send a nullfunc frame instead of a probe request,
2591 * as it will kick us off the AP quickly if we aren't associated
2592 * anymore. The timeout will be reset if the frame is ACKed by
2593 * the AP.
2594 */
Soumik Das992e68b2012-05-20 15:31:13 +05302595 ifmgd->probe_send_count++;
2596
Johannes Berg49ddf8e2016-03-31 20:02:10 +03002597 if (dst) {
2598 mutex_lock(&sdata->local->sta_mtx);
2599 sta = sta_info_get(sdata, dst);
2600 if (!WARN_ON(!sta))
2601 ieee80211_check_fast_rx(sta);
2602 mutex_unlock(&sdata->local->sta_mtx);
2603 }
2604
Johannes Berg30686bf2015-06-02 21:39:54 +02002605 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002606 ifmgd->nullfunc_failed = false;
Shay Barf39b07f2019-07-03 16:18:48 +03002607 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2608 ifmgd->probe_send_count--;
2609 else
2610 ieee80211_send_nullfunc(sdata->local, sdata, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002611 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002612 int ssid_len;
2613
Johannes Berg9caf0362012-11-29 01:25:20 +01002614 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002615 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002616 if (WARN_ON_ONCE(ssid == NULL))
2617 ssid_len = 0;
2618 else
2619 ssid_len = ssid[1];
2620
Johannes Berg45ad6832018-05-28 15:47:39 +02002621 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2622 ssid + 2, ssid_len,
2623 ifmgd->associated->channel);
Johannes Berg9caf0362012-11-29 01:25:20 +01002624 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002625 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002626
Ben Greear180205b2011-02-04 15:30:24 -08002627 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002628 run_again(sdata, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002629}
2630
Johannes Bergb291ba12009-07-10 15:29:03 +02002631static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2632 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002633{
Kalle Valo04de8382009-03-22 21:57:35 +02002634 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002635 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002636
Johannes Berg9607e6b662009-12-23 13:15:31 +01002637 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002638 return;
2639
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002640 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002641
2642 if (!ifmgd->associated)
2643 goto out;
2644
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002645 mutex_lock(&sdata->local->mtx);
2646
2647 if (sdata->local->tmp_channel || sdata->local->scanning) {
2648 mutex_unlock(&sdata->local->mtx);
2649 goto out;
2650 }
2651
Ben Greeara13fbe52013-03-25 11:19:35 -07002652 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002653 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002654 "detected beacon loss from AP (missed %d beacons) - probing\n",
2655 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002656
Johannes Berg98f03342014-11-26 12:42:02 +01002657 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
Ben Greeara13fbe52013-03-25 11:19:35 -07002658 }
Kalle Valo04de8382009-03-22 21:57:35 +02002659
Johannes Bergb291ba12009-07-10 15:29:03 +02002660 /*
2661 * The driver/our work has already reported this event or the
2662 * connection monitoring has kicked in and we have already sent
2663 * a probe request. Or maybe the AP died and the driver keeps
2664 * reporting until we disassociate...
2665 *
2666 * In either case we have to ignore the current call to this
2667 * function (except for setting the correct probe reason bit)
2668 * because otherwise we would reset the timer every time and
2669 * never check whether we received a probe response!
2670 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002671 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02002672 already = true;
2673
Eliad Peller12b5f342013-11-18 19:06:46 +02002674 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2675
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002676 mutex_unlock(&sdata->local->mtx);
2677
Johannes Bergb291ba12009-07-10 15:29:03 +02002678 if (already)
2679 goto out;
2680
Johannes Berg4e751842009-06-10 15:16:52 +02002681 mutex_lock(&sdata->local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02002682 ieee80211_recalc_ps(sdata->local);
Johannes Berg4e751842009-06-10 15:16:52 +02002683 mutex_unlock(&sdata->local->iflist_mtx);
2684
Maxim Levitskya43abf22009-07-31 18:54:12 +03002685 ifmgd->probe_send_count = 0;
2686 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002687 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002688 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002689}
2690
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002691struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2692 struct ieee80211_vif *vif)
2693{
2694 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2695 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002696 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002697 struct sk_buff *skb;
2698 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002699 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002700
2701 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2702 return NULL;
2703
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002704 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002705
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002706 if (ifmgd->associated)
2707 cbss = ifmgd->associated;
2708 else if (ifmgd->auth_data)
2709 cbss = ifmgd->auth_data->bss;
2710 else if (ifmgd->assoc_data)
2711 cbss = ifmgd->assoc_data->bss;
2712 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002713 return NULL;
2714
Johannes Berg9caf0362012-11-29 01:25:20 +01002715 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002716 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Will Deacon41525612019-10-04 10:51:31 +01002717 if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2718 "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002719 ssid_len = 0;
2720 else
2721 ssid_len = ssid[1];
2722
Johannes Berga344d672014-06-12 22:24:31 +02002723 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002724 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002725 ssid + 2, ssid_len,
Johannes Berg00387f32018-05-28 15:47:38 +02002726 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
Johannes Berg9caf0362012-11-29 01:25:20 +01002727 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002728
2729 return skb;
2730}
2731EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2732
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002733static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2734 const u8 *buf, size_t len, bool tx,
2735 u16 reason)
2736{
2737 struct ieee80211_event event = {
2738 .type = MLME_EVENT,
2739 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2740 .u.mlme.reason = reason,
2741 };
2742
2743 if (tx)
2744 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2745 else
2746 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2747
2748 drv_event_callback(sdata->local, sdata, &event);
2749}
2750
Johannes Bergeef9e542013-01-29 13:09:34 +01002751static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002752{
Michal Kazior59af6922014-04-09 15:10:59 +02002753 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002754 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002755 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002756 bool tx;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002757
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002758 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002759 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002760 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002761 return;
2762 }
2763
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002764 tx = !sdata->csa_block_tx;
2765
David Spinadel20eb7ea2016-05-03 16:05:02 +03002766 /* AP is probably out of range (or not reachable for another reason) so
2767 * remove the bss struct for that AP.
2768 */
2769 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2770
Johannes Berg37ad3882012-02-24 13:50:54 +01002771 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2772 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002773 tx, frame_buf);
Michal Kazior59af6922014-04-09 15:10:59 +02002774 mutex_lock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03002775 sdata->vif.csa_active = false;
Luciano Coelho0c21e632014-10-08 09:48:39 +03002776 ifmgd->csa_waiting_bcn = false;
Luciano Coelhoa46992b2014-06-13 16:30:07 +03002777 if (sdata->csa_block_tx) {
2778 ieee80211_wake_vif_queues(local, sdata,
2779 IEEE80211_QUEUE_STOP_REASON_CSA);
2780 sdata->csa_block_tx = false;
2781 }
Michal Kazior59af6922014-04-09 15:10:59 +02002782 mutex_unlock(&local->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002783
Emmanuel Grumbach6c18b272018-08-31 11:31:12 +03002784 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02002785 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2786
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002787 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002788}
2789
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002790static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002791{
2792 struct ieee80211_sub_if_data *sdata =
2793 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002794 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002795 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Paul Stewarta85e1d52011-12-09 11:01:49 -08002796
Johannes Berg976bd9e2015-10-16 17:18:11 +02002797 if (ifmgd->associated)
2798 ifmgd->beacon_loss_count++;
Johannes Bergb291ba12009-07-10 15:29:03 +02002799
Johannes Berg682bd382013-01-29 13:13:50 +01002800 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002801 sdata_info(sdata, "Connection to AP %pM lost\n",
2802 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002803 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002804 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002805 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002806 }
2807}
2808
2809static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2810{
2811 struct ieee80211_sub_if_data *sdata =
2812 container_of(work, struct ieee80211_sub_if_data,
2813 u.mgd.csa_connection_drop_work);
2814
Johannes Bergeef9e542013-01-29 13:09:34 +01002815 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002816}
2817
Kalle Valo04de8382009-03-22 21:57:35 +02002818void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2819{
2820 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002821 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002822
Johannes Bergb5878a22010-04-07 16:48:40 +02002823 trace_api_beacon_loss(sdata);
2824
Johannes Berg682bd382013-01-29 13:13:50 +01002825 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002826 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002827}
2828EXPORT_SYMBOL(ieee80211_beacon_loss);
2829
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002830void ieee80211_connection_loss(struct ieee80211_vif *vif)
2831{
2832 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2833 struct ieee80211_hw *hw = &sdata->local->hw;
2834
Johannes Bergb5878a22010-04-07 16:48:40 +02002835 trace_api_connection_loss(sdata);
2836
Johannes Berg682bd382013-01-29 13:13:50 +01002837 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002838 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2839}
2840EXPORT_SYMBOL(ieee80211_connection_loss);
2841
2842
Johannes Berg66e67e42012-01-20 13:55:27 +01002843static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2844 bool assoc)
2845{
2846 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2847
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002848 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002849
Johannes Berg66e67e42012-01-20 13:55:27 +01002850 if (!assoc) {
Emmanuel Grumbachc1e140b2015-01-19 16:53:06 +02002851 /*
2852 * we are not authenticated yet, the only timer that could be
2853 * running is the timeout for the authentication response which
2854 * which is not relevant anymore.
2855 */
2856 del_timer_sync(&sdata->u.mgd.timer);
Johannes Berg66e67e42012-01-20 13:55:27 +01002857 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2858
Joe Perchesc84a67a2015-03-02 19:54:57 -08002859 eth_zero_addr(sdata->u.mgd.bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002860 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002861 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002862 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002863 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002864 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002865 }
2866
Johannes Berg5b112d32013-02-01 01:49:58 +01002867 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002868 kfree(auth_data);
2869 sdata->u.mgd.auth_data = NULL;
2870}
2871
Johannes Bergc9c99f82015-06-01 14:10:09 +02002872static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
Johannes Berge6f462d2016-12-08 17:22:09 +01002873 bool assoc, bool abandon)
Johannes Bergc9c99f82015-06-01 14:10:09 +02002874{
2875 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2876
2877 sdata_assert_lock(sdata);
2878
2879 if (!assoc) {
2880 /*
2881 * we are not associated yet, the only timer that could be
2882 * running is the timeout for the association response which
2883 * which is not relevant anymore.
2884 */
2885 del_timer_sync(&sdata->u.mgd.timer);
2886 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2887
2888 eth_zero_addr(sdata->u.mgd.bssid);
2889 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2890 sdata->u.mgd.flags = 0;
Sara Sharonb5a33d52016-02-16 12:48:18 +02002891 sdata->vif.mu_mimo_owner = false;
2892
Johannes Bergc9c99f82015-06-01 14:10:09 +02002893 mutex_lock(&sdata->local->mtx);
2894 ieee80211_vif_release_channel(sdata);
2895 mutex_unlock(&sdata->local->mtx);
Johannes Berge6f462d2016-12-08 17:22:09 +01002896
2897 if (abandon)
2898 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
Johannes Bergc9c99f82015-06-01 14:10:09 +02002899 }
2900
2901 kfree(assoc_data);
2902 sdata->u.mgd.assoc_data = NULL;
2903}
2904
Johannes Berg66e67e42012-01-20 13:55:27 +01002905static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2906 struct ieee80211_mgmt *mgmt, size_t len)
2907{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002908 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002909 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2910 u8 *pos;
2911 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002912 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002913
2914 pos = mgmt->u.auth.variable;
Sara Sharon4abb52a2019-01-16 12:14:41 +02002915 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2916 mgmt->bssid, auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002917 if (!elems.challenge)
2918 return;
2919 auth_data->expected_transaction = 4;
Ilan Peerd4e36e52018-04-20 13:49:25 +03002920 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg30686bf2015-06-02 21:39:54 +02002921 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Johannes Berg1672c0e32013-01-29 15:02:27 +01002922 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2923 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002924 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002925 elems.challenge - 2, elems.challenge_len + 2,
2926 auth_data->bss->bssid, auth_data->bss->bssid,
2927 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002928 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002929}
2930
Jouni Malinenfc107a92018-10-11 00:21:19 +03002931static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2932 const u8 *bssid)
2933{
Jouni Malinenefb543e2018-10-11 00:21:21 +03002934 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002935 struct sta_info *sta;
Wei Yongjun33483a62018-10-16 02:35:30 +00002936 bool result = true;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002937
Jouni Malinenefb543e2018-10-11 00:21:21 +03002938 sdata_info(sdata, "authenticated\n");
2939 ifmgd->auth_data->done = true;
2940 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2941 ifmgd->auth_data->timeout_started = true;
2942 run_again(sdata, ifmgd->auth_data->timeout);
2943
Jouni Malinenfc107a92018-10-11 00:21:19 +03002944 /* move station state to auth */
2945 mutex_lock(&sdata->local->sta_mtx);
2946 sta = sta_info_get(sdata, bssid);
2947 if (!sta) {
2948 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002949 result = false;
2950 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002951 }
2952 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2953 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Wei Yongjun33483a62018-10-16 02:35:30 +00002954 result = false;
2955 goto out;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002956 }
Jouni Malinenfc107a92018-10-11 00:21:19 +03002957
Wei Yongjun33483a62018-10-16 02:35:30 +00002958out:
2959 mutex_unlock(&sdata->local->sta_mtx);
2960 return result;
Jouni Malinenfc107a92018-10-11 00:21:19 +03002961}
2962
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002963static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2964 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002965{
2966 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2967 u8 bssid[ETH_ALEN];
2968 u16 auth_alg, auth_transaction, status_code;
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02002969 struct ieee80211_event event = {
2970 .type = MLME_EVENT,
2971 .u.mlme.data = AUTH_EVENT,
2972 };
Johannes Berg66e67e42012-01-20 13:55:27 +01002973
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002974 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002975
2976 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002977 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002978
2979 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002980 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002981
2982 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2983
Joe Perchesb203ca32012-05-08 18:56:52 +00002984 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002985 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002986
2987 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2988 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2989 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2990
2991 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinenefb543e2018-10-11 00:21:21 +03002992 (auth_alg != WLAN_AUTH_SAE &&
2993 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2994 (auth_alg == WLAN_AUTH_SAE &&
2995 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2996 auth_transaction > 2))) {
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002997 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2998 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2999 auth_transaction,
3000 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003001 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03003002 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003003
3004 if (status_code != WLAN_STATUS_SUCCESS) {
Andrei Otcheretianskia4055e72020-03-26 15:09:34 +02003005 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3006
3007 if (auth_alg == WLAN_AUTH_SAE &&
Jouni Malinen4e56cde2020-07-31 21:38:30 +03003008 (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
3009 (auth_transaction == 1 &&
3010 (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
3011 status_code == WLAN_STATUS_SAE_PK))))
Andrei Otcheretianskia4055e72020-03-26 15:09:34 +02003012 return;
3013
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003014 sdata_info(sdata, "%pM denied authentication (status %d)\n",
3015 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03003016 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003017 event.u.mlme.status = MLME_DENIED;
3018 event.u.mlme.reason = status_code;
3019 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003020 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003021 }
3022
3023 switch (ifmgd->auth_data->algorithm) {
3024 case WLAN_AUTH_OPEN:
3025 case WLAN_AUTH_LEAP:
3026 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003027 case WLAN_AUTH_SAE:
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03003028 case WLAN_AUTH_FILS_SK:
3029 case WLAN_AUTH_FILS_SK_PFS:
3030 case WLAN_AUTH_FILS_PK:
Johannes Berg66e67e42012-01-20 13:55:27 +01003031 break;
3032 case WLAN_AUTH_SHARED_KEY:
3033 if (ifmgd->auth_data->expected_transaction != 4) {
3034 ieee80211_auth_challenge(sdata, mgmt, len);
3035 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003036 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003037 }
3038 break;
3039 default:
3040 WARN_ONCE(1, "invalid auth alg %d",
3041 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003042 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003043 }
3044
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02003045 event.u.mlme.status = MLME_SUCCESS;
3046 drv_event_callback(sdata->local, sdata, &event);
Jouni Malinenefb543e2018-10-11 00:21:21 +03003047 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3048 (auth_transaction == 2 &&
3049 ifmgd->auth_data->expected_transaction == 2)) {
3050 if (!ieee80211_mark_sta_auth(sdata, bssid))
Andrei Otcheretianski0daa63e2020-02-21 10:47:20 +01003051 return; /* ignore frame -- wait for timeout */
Jouni Malinenefb543e2018-10-11 00:21:21 +03003052 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3053 auth_transaction == 2) {
3054 sdata_info(sdata, "SAE peer confirmed\n");
3055 ifmgd->auth_data->peer_confirmed = true;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003056 }
3057
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003058 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003059}
3060
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003061#define case_WLAN(type) \
3062 case WLAN_REASON_##type: return #type
3063
Yu Wang79c92ca2019-05-10 17:04:52 +08003064const char *ieee80211_get_reason_code_string(u16 reason_code)
Calvin Owensdfa1ad22014-02-11 12:36:24 -06003065{
3066 switch (reason_code) {
3067 case_WLAN(UNSPECIFIED);
3068 case_WLAN(PREV_AUTH_NOT_VALID);
3069 case_WLAN(DEAUTH_LEAVING);
3070 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3071 case_WLAN(DISASSOC_AP_BUSY);
3072 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3073 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3074 case_WLAN(DISASSOC_STA_HAS_LEFT);
3075 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3076 case_WLAN(DISASSOC_BAD_POWER);
3077 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3078 case_WLAN(INVALID_IE);
3079 case_WLAN(MIC_FAILURE);
3080 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3081 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3082 case_WLAN(IE_DIFFERENT);
3083 case_WLAN(INVALID_GROUP_CIPHER);
3084 case_WLAN(INVALID_PAIRWISE_CIPHER);
3085 case_WLAN(INVALID_AKMP);
3086 case_WLAN(UNSUPP_RSN_VERSION);
3087 case_WLAN(INVALID_RSN_IE_CAP);
3088 case_WLAN(IEEE8021X_FAILED);
3089 case_WLAN(CIPHER_SUITE_REJECTED);
3090 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3091 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3092 case_WLAN(DISASSOC_LOW_ACK);
3093 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3094 case_WLAN(QSTA_LEAVE_QBSS);
3095 case_WLAN(QSTA_NOT_USE);
3096 case_WLAN(QSTA_REQUIRE_SETUP);
3097 case_WLAN(QSTA_TIMEOUT);
3098 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3099 case_WLAN(MESH_PEER_CANCELED);
3100 case_WLAN(MESH_MAX_PEERS);
3101 case_WLAN(MESH_CONFIG);
3102 case_WLAN(MESH_CLOSE);
3103 case_WLAN(MESH_MAX_RETRIES);
3104 case_WLAN(MESH_CONFIRM_TIMEOUT);
3105 case_WLAN(MESH_INVALID_GTK);
3106 case_WLAN(MESH_INCONSISTENT_PARAM);
3107 case_WLAN(MESH_INVALID_SECURITY);
3108 case_WLAN(MESH_PATH_ERROR);
3109 case_WLAN(MESH_PATH_NOFORWARD);
3110 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3111 case_WLAN(MAC_EXISTS_IN_MBSS);
3112 case_WLAN(MESH_CHAN_REGULATORY);
3113 case_WLAN(MESH_CHAN);
3114 default: return "<unknown>";
3115 }
3116}
Johannes Berg66e67e42012-01-20 13:55:27 +01003117
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003118static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3119 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003120{
Johannes Berg46900292009-02-15 12:44:28 +01003121 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003122 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003123
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003124 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003125
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003126 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003127 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003128
Yu Wang79c92ca2019-05-10 17:04:52 +08003129 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3130 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3131 return;
3132 }
3133
Johannes Bergc9c99f82015-06-01 14:10:09 +02003134 if (ifmgd->associated &&
3135 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3136 const u8 *bssid = ifmgd->associated->bssid;
3137
3138 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3139 bssid, reason_code,
3140 ieee80211_get_reason_code_string(reason_code));
3141
3142 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3143
3144 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3145 reason_code);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003146 return;
Johannes Bergc9c99f82015-06-01 14:10:09 +02003147 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003148
Johannes Bergc9c99f82015-06-01 14:10:09 +02003149 if (ifmgd->assoc_data &&
3150 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3151 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07003152
Johannes Bergc9c99f82015-06-01 14:10:09 +02003153 sdata_info(sdata,
3154 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3155 bssid, reason_code,
3156 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003157
Johannes Berge6f462d2016-12-08 17:22:09 +01003158 ieee80211_destroy_assoc_data(sdata, false, true);
Jiri Bencf0706e82007-05-05 11:45:53 -07003159
Johannes Bergc9c99f82015-06-01 14:10:09 +02003160 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3161 return;
3162 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003163}
3164
3165
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003166static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3167 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003168{
Johannes Berg46900292009-02-15 12:44:28 +01003169 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003170 u16 reason_code;
3171
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003172 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003173
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003174 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003175 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003176
Johannes Berg66e67e42012-01-20 13:55:27 +01003177 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00003178 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003179 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003180
3181 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3182
Yu Wang79c92ca2019-05-10 17:04:52 +08003183 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3184 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3185 return;
3186 }
3187
Arkadiusz Miskiewicz68506e92017-02-15 14:21:27 +01003188 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3189 mgmt->sa, reason_code,
3190 ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07003191
Johannes Berg37ad3882012-02-24 13:50:54 +01003192 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3193
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02003194 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07003195}
3196
Christian Lamparterc74d0842011-10-15 00:14:49 +02003197static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3198 u8 *supp_rates, unsigned int supp_rates_len,
3199 u32 *rates, u32 *basic_rates,
3200 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003201 int *min_rate, int *min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03003202 int shift)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003203{
3204 int i, j;
3205
3206 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003207 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003208 bool is_basic = !!(supp_rates[i] & 0x80);
3209
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003210 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02003211 *have_higher_than_11mbit = true;
3212
3213 /*
Ilan Peer3598ae82020-11-29 17:30:47 +02003214 * Skip HT, VHT, HE and SAE H2E only BSS membership selectors
3215 * since they're not rates.
Christian Lamparterc74d0842011-10-15 00:14:49 +02003216 *
Johannes Berga6289d32017-03-06 22:59:04 +01003217 * Note: Even though the membership selector and the basic
Christian Lamparterc74d0842011-10-15 00:14:49 +02003218 * rate flag share the same bit, they are not exactly
3219 * the same.
3220 */
Johannes Berga6289d32017-03-06 22:59:04 +01003221 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
Ilan Peer4826e722020-03-26 15:09:36 +02003222 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
Ilan Peer3598ae82020-11-29 17:30:47 +02003223 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY) ||
3224 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E))
Christian Lamparterc74d0842011-10-15 00:14:49 +02003225 continue;
3226
3227 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003228 struct ieee80211_rate *br;
3229 int brate;
3230
3231 br = &sband->bitrates[j];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003232
3233 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3234 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02003235 *rates |= BIT(j);
3236 if (is_basic)
3237 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003238 if ((rate * 5) < *min_rate) {
3239 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003240 *min_rate_index = j;
3241 }
3242 break;
3243 }
3244 }
3245 }
3246}
Jiri Bencf0706e82007-05-05 11:45:53 -07003247
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003248static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3249 const struct ieee802_11_elems *elems)
3250{
3251 if (elems->ext_capab_len < 10)
3252 return false;
3253
3254 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3255 return false;
3256
3257 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3258 IEEE80211_HE_MAC_CAP0_TWT_RES;
3259}
3260
John Crispinc9d32452019-05-28 13:49:47 +02003261static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3262 struct sta_info *sta,
3263 struct ieee802_11_elems *elems)
3264{
3265 bool twt = ieee80211_twt_req_supported(sta, elems);
3266
3267 if (sdata->vif.bss_conf.twt_requester != twt) {
3268 sdata->vif.bss_conf.twt_requester = twt;
3269 return BSS_CHANGED_TWT;
3270 }
3271 return 0;
3272}
3273
Johannes Berg66e67e42012-01-20 13:55:27 +01003274static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3275 struct cfg80211_bss *cbss,
Johannes Bergf61d7882019-10-28 12:52:42 +01003276 struct ieee80211_mgmt *mgmt, size_t len,
3277 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07003278{
Johannes Berg46900292009-02-15 12:44:28 +01003279 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01003280 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01003281 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003282 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003283 u16 capab_info, aid;
Johannes Bergbda39332008-10-11 01:51:51 +02003284 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02003285 const struct cfg80211_bss_ies *bss_ies = NULL;
3286 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Berg57fa5e82020-05-28 21:34:36 +02003287 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003288 bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
Johannes Bergae5eb022008-10-14 16:58:37 +02003289 u32 changed = 0;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003290 u8 *pos;
Christian Lamparterc74d0842011-10-15 00:14:49 +02003291 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02003292 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003293
Johannes Bergaf6b6372009-12-23 13:15:35 +01003294 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07003295
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003296 pos = mgmt->u.assoc_resp.variable;
Jiri Bencf0706e82007-05-05 11:45:53 -07003297 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003298 if (is_s1g) {
3299 pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3300 aid = 0; /* TODO */
3301 }
Johannes Bergaf6b6372009-12-23 13:15:35 +01003302 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003303 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, elems,
3304 mgmt->bssid, assoc_data->bss->bssid);
3305
3306 if (elems->aid_resp)
3307 aid = le16_to_cpu(elems->aid_resp->aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07003308
Johannes Bergc7c477b2017-12-01 13:50:51 +02003309 /*
3310 * The 5 MSB of the AID field are reserved
3311 * (802.11-2016 9.4.1.8 AID field)
3312 */
3313 aid &= 0x7ff;
Johannes Berg1dd84aa2007-10-10 12:03:41 +02003314
Johannes Berg05cb9102011-10-28 11:59:47 +02003315 ifmgd->broken_ap = false;
3316
3317 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003318 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3319 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02003320 aid = 0;
3321 ifmgd->broken_ap = true;
3322 }
3323
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003324 if (!is_s1g && !elems->supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003325 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01003326 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07003327 }
3328
Johannes Berg1db364c2020-04-17 12:38:04 +02003329 sdata->vif.bss_conf.aid = aid;
Arik Nemtsov9041c1f2014-11-09 18:50:15 +02003330 ifmgd->tdls_chan_switch_prohibited =
Johannes Bergf61d7882019-10-28 12:52:42 +01003331 elems->ext_capab && elems->ext_capab_len >= 5 &&
3332 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
Jiri Bencf0706e82007-05-05 11:45:53 -07003333
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003334 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02003335 * Some APs are erroneously not including some information in their
3336 * (re)association response frames. Try to recover by using the data
3337 * from the beacon or probe response. This seems to afflict mobile
3338 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3339 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3340 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003341 if (!is_6ghz &&
3342 ((assoc_data->wmm && !elems->wmm_param) ||
3343 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3344 (!elems->ht_cap_elem || !elems->ht_operation)) ||
3345 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3346 (!elems->vht_cap_elem || !elems->vht_operation)))) {
Johannes Berg35d865a2013-05-28 10:54:03 +02003347 const struct cfg80211_bss_ies *ies;
3348 struct ieee802_11_elems bss_elems;
3349
3350 rcu_read_lock();
3351 ies = rcu_dereference(cbss->ies);
3352 if (ies)
3353 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3354 GFP_ATOMIC);
3355 rcu_read_unlock();
3356 if (!bss_ies)
3357 return false;
3358
3359 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003360 false, &bss_elems,
3361 mgmt->bssid,
3362 assoc_data->bss->bssid);
Johannes Berg35d865a2013-05-28 10:54:03 +02003363 if (assoc_data->wmm &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003364 !elems->wmm_param && bss_elems.wmm_param) {
3365 elems->wmm_param = bss_elems.wmm_param;
Johannes Berg35d865a2013-05-28 10:54:03 +02003366 sdata_info(sdata,
3367 "AP bug: WMM param missing from AssocResp\n");
3368 }
3369
3370 /*
3371 * Also check if we requested HT/VHT, otherwise the AP doesn't
3372 * have to include the IEs in the (re)association response.
3373 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003374 if (!elems->ht_cap_elem && bss_elems.ht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003375 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003376 elems->ht_cap_elem = bss_elems.ht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003377 sdata_info(sdata,
3378 "AP bug: HT capability missing from AssocResp\n");
3379 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003380 if (!elems->ht_operation && bss_elems.ht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003381 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003382 elems->ht_operation = bss_elems.ht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003383 sdata_info(sdata,
3384 "AP bug: HT operation missing from AssocResp\n");
3385 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003386 if (!elems->vht_cap_elem && bss_elems.vht_cap_elem &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003387 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003388 elems->vht_cap_elem = bss_elems.vht_cap_elem;
Johannes Berg35d865a2013-05-28 10:54:03 +02003389 sdata_info(sdata,
3390 "AP bug: VHT capa missing from AssocResp\n");
3391 }
Johannes Bergf61d7882019-10-28 12:52:42 +01003392 if (!elems->vht_operation && bss_elems.vht_operation &&
Johannes Berg35d865a2013-05-28 10:54:03 +02003393 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Johannes Bergf61d7882019-10-28 12:52:42 +01003394 elems->vht_operation = bss_elems.vht_operation;
Johannes Berg35d865a2013-05-28 10:54:03 +02003395 sdata_info(sdata,
3396 "AP bug: VHT operation missing from AssocResp\n");
3397 }
3398 }
3399
3400 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003401 * We previously checked these in the beacon/probe response, so
3402 * they should be present here. This is just a safety net.
3403 */
Johannes Berg57fa5e82020-05-28 21:34:36 +02003404 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003405 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003406 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003407 "HT AP is missing WMM params or HT capability/operation\n");
3408 ret = false;
3409 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003410 }
3411
Johannes Berg57fa5e82020-05-28 21:34:36 +02003412 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003413 (!elems->vht_cap_elem || !elems->vht_operation)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003414 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02003415 "VHT AP is missing VHT capability/operation\n");
3416 ret = false;
3417 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003418 }
3419
Johannes Berg57fa5e82020-05-28 21:34:36 +02003420 if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3421 !elems->he_6ghz_capa) {
3422 sdata_info(sdata,
3423 "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3424 ret = false;
3425 goto out;
3426 }
3427
Guy Eilam2a33bee2011-08-17 15:18:15 +03003428 mutex_lock(&sdata->local->sta_mtx);
3429 /*
3430 * station info was already allocated and inserted before
3431 * the association and should be available to us
3432 */
Johannes Berg7852e362012-01-20 13:55:24 +01003433 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003434 if (WARN_ON(!sta)) {
3435 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003436 ret = false;
3437 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07003438 }
3439
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05303440 sband = ieee80211_get_sband(sdata);
3441 if (!sband) {
3442 mutex_unlock(&sdata->local->sta_mtx);
3443 ret = false;
3444 goto out;
3445 }
Johannes Berg8318d782008-01-24 19:38:38 +01003446
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003447 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
Johannes Bergf61d7882019-10-28 12:52:42 +01003448 (!elems->he_cap || !elems->he_operation)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003449 mutex_unlock(&sdata->local->sta_mtx);
3450 sdata_info(sdata,
3451 "HE AP is missing HE capability/operation\n");
3452 ret = false;
3453 goto out;
3454 }
3455
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003456 /* Set up internal HT/VHT capabilities */
Johannes Bergf61d7882019-10-28 12:52:42 +01003457 if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08003458 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003459 elems->ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02003460
Johannes Bergf61d7882019-10-28 12:52:42 +01003461 if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Mahesh Palivela818255e2012-10-10 11:33:04 +00003462 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003463 elems->vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00003464
Johannes Bergf61d7882019-10-28 12:52:42 +01003465 if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3466 elems->he_cap) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003467 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
Johannes Bergf61d7882019-10-28 12:52:42 +01003468 elems->he_cap,
3469 elems->he_cap_len,
Johannes Berg1bb9a8a2020-05-28 21:34:38 +02003470 elems->he_6ghz_capa,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003471 sta);
3472
3473 bss_conf->he_support = sta->sta.he_cap.has_he;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003474 if (elems->rsnx && elems->rsnx_len &&
3475 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3476 wiphy_ext_feature_isset(local->hw.wiphy,
3477 NL80211_EXT_FEATURE_PROTECTED_TWT))
3478 bss_conf->twt_protected = true;
3479 else
3480 bss_conf->twt_protected = false;
3481
Johannes Bergf61d7882019-10-28 12:52:42 +01003482 changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003483 } else {
3484 bss_conf->he_support = false;
Emmanuel Grumbach55ebd6e2018-12-15 11:03:04 +02003485 bss_conf->twt_requester = false;
Shaul Triebitzd46b4ab82020-03-26 15:09:33 +02003486 bss_conf->twt_protected = false;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003487 }
3488
3489 if (bss_conf->he_support) {
John Crispindd56e902019-12-17 15:19:19 +01003490 bss_conf->he_bss_color.color =
Johannes Bergf61d7882019-10-28 12:52:42 +01003491 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003492 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
John Crispindd56e902019-12-17 15:19:19 +01003493 bss_conf->he_bss_color.partial =
3494 le32_get_bits(elems->he_operation->he_oper_params,
3495 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
Johannes Berg75e6b592020-07-30 13:00:52 +02003496 bss_conf->he_bss_color.enabled =
3497 !le32_get_bits(elems->he_operation->he_oper_params,
3498 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
P Praneesh322cd272020-07-09 08:16:21 +05303499
Johannes Berg75e6b592020-07-30 13:00:52 +02003500 if (bss_conf->he_bss_color.enabled)
P Praneesh322cd272020-07-09 08:16:21 +05303501 changed |= BSS_CHANGED_HE_BSS_COLOR;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003502
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003503 bss_conf->htc_trig_based_pkt_ext =
Johannes Bergf61d7882019-10-28 12:52:42 +01003504 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003505 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003506 bss_conf->frame_time_rts_th =
Johannes Bergf61d7882019-10-28 12:52:42 +01003507 le32_get_bits(elems->he_operation->he_oper_params,
Naftali Goldstein77cbbc32018-09-05 08:06:07 +03003508 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003509
Johannes Bergf61d7882019-10-28 12:52:42 +01003510 bss_conf->uora_exists = !!elems->uora_element;
3511 if (elems->uora_element)
3512 bss_conf->uora_ocw_range = elems->uora_element[0];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003513
Johannes Bergf61d7882019-10-28 12:52:42 +01003514 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3515 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003516 /* TODO: OPEN: what happens if BSS color disable is set? */
3517 }
3518
Sara Sharon78ac51f2019-01-16 18:22:56 +02003519 if (cbss->transmitted_bss) {
3520 bss_conf->nontransmitted = true;
3521 ether_addr_copy(bss_conf->transmitter_bssid,
3522 cbss->transmitted_bss->bssid);
3523 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3524 bss_conf->bssid_index = cbss->bssid_index;
3525 }
3526
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003527 /*
3528 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3529 * in their association response, so ignore that data for our own
3530 * configuration. If it changed since the last beacon, we'll get the
3531 * next beacon and update then.
3532 */
Johannes Berg11289582013-02-07 17:36:12 +01003533
Johannes Bergbee7f5862013-02-07 22:24:55 +01003534 /*
3535 * If an operating mode notification IE is present, override the
3536 * NSS calculation (that would be done in rate_control_rate_init())
3537 * and use the # of streams from that element.
3538 */
Johannes Bergf61d7882019-10-28 12:52:42 +01003539 if (elems->opmode_notif &&
3540 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
Johannes Bergbee7f5862013-02-07 22:24:55 +01003541 u8 nss;
3542
Johannes Bergf61d7882019-10-28 12:52:42 +01003543 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
Johannes Bergbee7f5862013-02-07 22:24:55 +01003544 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3545 nss += 1;
3546 sta->sta.rx_nss = nss;
3547 }
3548
Johannes Berg4b7679a2008-09-18 18:14:18 +02003549 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003550
Tamizh chelvam93f04902015-10-07 10:40:04 +05303551 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02003552 set_sta_flag(sta, WLAN_STA_MFP);
Tamizh chelvam93f04902015-10-07 10:40:04 +05303553 sta->sta.mfp = true;
3554 } else {
3555 sta->sta.mfp = false;
3556 }
Jouni Malinen5394af42009-01-08 13:31:59 +02003557
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003558 sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
3559 local->hw.queues >= IEEE80211_NUM_ACS;
Johannes Bergddf4ac52008-10-22 11:41:38 +02003560
Johannes Berg3e4d40f2013-02-07 17:19:08 +01003561 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01003562 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3563 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3564 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003565 sdata_info(sdata,
3566 "failed to move station %pM to desired state\n",
3567 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01003568 WARN_ON(__sta_info_destroy(sta));
3569 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02003570 ret = false;
3571 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01003572 }
3573
Felix Fietkau1ff4e8f2020-09-08 14:37:01 +02003574 if (sdata->wdev.use_4addr)
3575 drv_sta_set_4addr(local, sdata, &sta->sta, true);
3576
Johannes Berg7852e362012-01-20 13:55:24 +01003577 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02003578
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003579 /*
3580 * Always handle WMM once after association regardless
3581 * of the first value the AP uses. Setting -1 here has
3582 * that effect because the AP values is an unsigned
3583 * 4-bit value.
3584 */
3585 ifmgd->wmm_last_param_set = -1;
Shaul Triebitz2e249fc2018-12-15 11:03:15 +02003586 ifmgd->mu_edca_last_param_set = -1;
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03003587
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003588 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Bergcec66282015-10-22 17:46:04 +02003589 ieee80211_set_wmm_default(sdata, false, false);
Johannes Bergf61d7882019-10-28 12:52:42 +01003590 } else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3591 elems->wmm_param_len,
3592 elems->mu_edca_param_set)) {
Johannes Berg2ed77ea2015-10-22 17:46:06 +02003593 /* still enable QoS since we might have HT/VHT */
3594 ieee80211_set_wmm_default(sdata, false, true);
3595 /* set the disable-WMM flag in this case to disable
3596 * tracking WMM parameter changes in the beacon if
3597 * the parameters weren't actually valid. Doing so
3598 * avoids changing parameters very strangely when
3599 * the AP is going back and forth between valid and
3600 * invalid parameters.
3601 */
3602 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3603 }
Johannes Berg3abead52012-03-02 15:56:59 +01003604 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07003605
Johannes Bergf61d7882019-10-28 12:52:42 +01003606 if (elems->max_idle_period_ie) {
Avraham Sterne38a0172017-04-26 10:58:47 +03003607 bss_conf->max_idle_period =
Johannes Bergf61d7882019-10-28 12:52:42 +01003608 le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
Avraham Sterne38a0172017-04-26 10:58:47 +03003609 bss_conf->protected_keep_alive =
Johannes Bergf61d7882019-10-28 12:52:42 +01003610 !!(elems->max_idle_period_ie->idle_options &
Avraham Sterne38a0172017-04-26 10:58:47 +03003611 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3612 changed |= BSS_CHANGED_KEEP_ALIVE;
3613 } else {
3614 bss_conf->max_idle_period = 0;
3615 bss_conf->protected_keep_alive = false;
3616 }
3617
Johannes Berg1db364c2020-04-17 12:38:04 +02003618 /* set assoc capability (AID was already set earlier),
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003619 * ieee80211_set_associated() will tell the driver */
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07003620 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003621 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003622
Kalle Valo15b7b062009-03-22 21:57:14 +02003623 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01003624 * If we're using 4-addr mode, let the AP know that we're
3625 * doing so, so that it can create the STA VLAN on its side
3626 */
3627 if (ifmgd->use_4addr)
3628 ieee80211_send_4addr_nullfunc(local, sdata);
3629
3630 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02003631 * Start timer to probe the connection to the AP now.
3632 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02003633 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003634 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau9abf4e42020-09-08 14:36:56 +02003635 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02003636
Johannes Berg35d865a2013-05-28 10:54:03 +02003637 ret = true;
3638 out:
3639 kfree(bss_ies);
3640 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003641}
3642
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003643static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3644 struct ieee80211_mgmt *mgmt,
3645 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01003646{
3647 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3648 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3649 u16 capab_info, status_code, aid;
3650 struct ieee802_11_elems elems;
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003651 int ac, uapsd_queues = -1;
Johannes Berg66e67e42012-01-20 13:55:27 +01003652 u8 *pos;
3653 bool reassoc;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003654 struct cfg80211_bss *cbss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003655 struct ieee80211_event event = {
3656 .type = MLME_EVENT,
3657 .u.mlme.data = ASSOC_EVENT,
3658 };
Jiri Bencf0706e82007-05-05 11:45:53 -07003659
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003660 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003661
3662 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003663 return;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003664
Joe Perchesb203ca32012-05-08 18:56:52 +00003665 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003666 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003667
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003668 cbss = assoc_data->bss;
3669
Johannes Berg66e67e42012-01-20 13:55:27 +01003670 /*
3671 * AssocResp and ReassocResp have identical structure, so process both
3672 * of them in this function.
3673 */
3674
3675 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003676 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003677
Simon Dinkin7a7d3e42017-08-31 13:09:36 +03003678 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
Johannes Berg66e67e42012-01-20 13:55:27 +01003679 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3680 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003681 pos = mgmt->u.assoc_resp.variable;
Johannes Berg66e67e42012-01-20 13:55:27 +01003682 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003683 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
3684 pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3685 aid = 0; /* TODO */
3686 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003687
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003688 sdata_info(sdata,
3689 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3690 reassoc ? "Rea" : "A", mgmt->sa,
3691 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01003692
Jouni Malinen39404fe2016-10-27 00:42:05 +03003693 if (assoc_data->fils_kek_len &&
3694 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3695 return;
3696
Sara Sharon4abb52a2019-01-16 12:14:41 +02003697 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3698 mgmt->bssid, assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003699
3700 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01003701 elems.timeout_int &&
3702 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003703 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01003704 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01003705 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003706 sdata_info(sdata,
3707 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3708 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01003709 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01003710 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003711 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003712 run_again(sdata, assoc_data->timeout);
3713 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003714 }
3715
Johannes Berg66e67e42012-01-20 13:55:27 +01003716 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003717 sdata_info(sdata, "%pM denied association (code=%d)\n",
3718 mgmt->sa, status_code);
Johannes Berge6f462d2016-12-08 17:22:09 +01003719 ieee80211_destroy_assoc_data(sdata, false, false);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003720 event.u.mlme.status = MLME_DENIED;
3721 event.u.mlme.reason = status_code;
3722 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01003723 } else {
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003724 if (!ieee80211_assoc_success(sdata, cbss, mgmt, len, &elems)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003725 /* oops -- internal error -- send timeout for now */
Johannes Berge6f462d2016-12-08 17:22:09 +01003726 ieee80211_destroy_assoc_data(sdata, false, false);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003727 cfg80211_assoc_timeout(sdata->dev, cbss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003728 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01003729 }
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02003730 event.u.mlme.status = MLME_SUCCESS;
3731 drv_event_callback(sdata->local, sdata, &event);
John W. Linville635d9992012-07-09 16:34:34 -04003732 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01003733
3734 /*
3735 * destroy assoc_data afterwards, as otherwise an idle
3736 * recalc after assoc_data is NULL but before associated
3737 * is set can cause the interface to go idle
3738 */
Johannes Berge6f462d2016-12-08 17:22:09 +01003739 ieee80211_destroy_assoc_data(sdata, true, false);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +03003740
3741 /* get uapsd queues configuration */
3742 uapsd_queues = 0;
3743 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3744 if (sdata->tx_conf[ac].uapsd)
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03003745 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
Johannes Berg66e67e42012-01-20 13:55:27 +01003746 }
3747
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07003748 cfg80211_rx_assoc_resp(sdata->dev, cbss, (u8 *)mgmt, len, uapsd_queues,
Jouni Malinen4d9ec732019-02-15 02:14:33 +02003749 ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003750}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003751
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003752static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01003753 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon4abb52a2019-01-16 12:14:41 +02003754 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003755{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003756 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02003757 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01003758 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01003759
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003760 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01003761
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003762 channel = ieee80211_get_channel_khz(local->hw.wiphy,
3763 ieee80211_rx_status_to_khz(rx_status));
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02003764 if (!channel)
Johannes Berg5bda6172008-09-08 11:05:10 +02003765 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003766
Sara Sharon4abb52a2019-01-16 12:14:41 +02003767 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03003768 if (bss) {
Alexander Bondar817cee72013-05-19 14:23:57 +03003769 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
Johannes Bergd2722f82014-03-04 11:43:28 +01003770 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03003771 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003772}
3773
3774
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003775static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01003776 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003777{
Johannes Bergaf6b6372009-12-23 13:15:35 +01003778 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02003779 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003780 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003781 struct ieee80211_channel *channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003782 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03003783
Kalle Valo15b7b062009-03-22 21:57:14 +02003784 ifmgd = &sdata->u.mgd;
3785
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003786 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003787
Andrei Otcheretianski85b27ef2020-01-31 13:12:50 +02003788 /*
3789 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3790 * "If a 6 GHz AP receives a Probe Request frame and responds with
3791 * a Probe Response frame [..], the Address 1 field of the Probe
3792 * Response frame shall be set to the broadcast address [..]"
3793 * So, on 6GHz band we should also accept broadcast responses.
3794 */
3795 channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3796 rx_status->freq);
3797 if (!channel)
3798 return;
3799
3800 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3801 (channel->band != NL80211_BAND_6GHZ ||
3802 !is_broadcast_ether_addr(mgmt->da)))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03003803 return; /* ignore ProbeResp to foreign address */
3804
Ester Kummerae6a44e2008-06-27 18:54:48 +03003805 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3806 if (baselen > len)
3807 return;
3808
Sara Sharon4abb52a2019-01-16 12:14:41 +02003809 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003810
Johannes Berg77fdaa12009-07-07 03:45:17 +02003811 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00003812 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003813 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003814}
3815
Johannes Bergd91f36d2009-04-16 13:17:26 +02003816/*
3817 * This is the canonical list of information elements we care about,
3818 * the filter code also gives us all changes to the Microsoft OUI
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07003819 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3820 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3821 * changes to requested client power.
Johannes Bergd91f36d2009-04-16 13:17:26 +02003822 *
3823 * We implement beacon filtering in software since that means we can
3824 * avoid processing the frame here and in cfg80211, and userspace
3825 * will not be able to tell whether the hardware supports it or not.
3826 *
3827 * XXX: This list needs to be dynamic -- userspace needs to be able to
3828 * add items it requires. It also needs to be able to tell us to
3829 * look out for other vendor IEs.
3830 */
3831static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02003832 (1ULL << WLAN_EID_COUNTRY) |
3833 (1ULL << WLAN_EID_ERP_INFO) |
3834 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3835 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3836 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg70a3fd62015-03-12 08:53:29 +02003837 (1ULL << WLAN_EID_HT_OPERATION) |
3838 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
Johannes Bergd91f36d2009-04-16 13:17:26 +02003839
Tosoni1ad22fb2018-03-14 16:58:34 +00003840static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3841 struct ieee80211_if_managed *ifmgd,
3842 struct ieee80211_bss_conf *bss_conf,
3843 struct ieee80211_local *local,
3844 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003845{
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003846 /* Track average RSSI from the Beacon frames of the current AP */
Tosoni1ad22fb2018-03-14 16:58:34 +00003847
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003848 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3849 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Johannes Berg338c17a2015-08-28 10:52:54 +02003850 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003851 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003852 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003853 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003854 } else {
Jouni Malinen391a2002010-08-27 22:22:00 +03003855 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003856 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003857
Johannes Berg338c17a2015-08-28 10:52:54 +02003858 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3859
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003860 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3861 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003862 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003863 int last_sig = ifmgd->last_ave_beacon_signal;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003864 struct ieee80211_event event = {
3865 .type = RSSI_EVENT,
3866 };
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003867
3868 /*
3869 * if signal crosses either of the boundaries, invoke callback
3870 * with appropriate parameters
3871 */
3872 if (sig > ifmgd->rssi_max_thold &&
3873 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3874 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003875 event.u.rssi.data = RSSI_EVENT_HIGH;
3876 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003877 } else if (sig < ifmgd->rssi_min_thold &&
3878 (last_sig >= ifmgd->rssi_max_thold ||
3879 last_sig == 0)) {
3880 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbacha8182922015-03-16 23:23:34 +02003881 event.u.rssi.data = RSSI_EVENT_LOW;
3882 drv_event_callback(local, sdata, &event);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003883 }
3884 }
3885
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003886 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003887 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003888 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Johannes Berg338c17a2015-08-28 10:52:54 +02003889 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003890 int last_event = ifmgd->last_cqm_event_signal;
3891 int thold = bss_conf->cqm_rssi_thold;
3892 int hyst = bss_conf->cqm_rssi_hyst;
Johannes Berg338c17a2015-08-28 10:52:54 +02003893
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003894 if (sig < thold &&
3895 (last_event == 0 || sig < last_event - hyst)) {
3896 ifmgd->last_cqm_event_signal = sig;
3897 ieee80211_cqm_rssi_notify(
3898 &sdata->vif,
3899 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003900 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003901 } else if (sig > thold &&
3902 (last_event == 0 || sig > last_event + hyst)) {
3903 ifmgd->last_cqm_event_signal = sig;
3904 ieee80211_cqm_rssi_notify(
3905 &sdata->vif,
3906 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01003907 sig, GFP_KERNEL);
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003908 }
3909 }
3910
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01003911 if (bss_conf->cqm_rssi_low &&
3912 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3913 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3914 int last_event = ifmgd->last_cqm_event_signal;
3915 int low = bss_conf->cqm_rssi_low;
3916 int high = bss_conf->cqm_rssi_high;
3917
3918 if (sig < low &&
3919 (last_event == 0 || last_event >= low)) {
3920 ifmgd->last_cqm_event_signal = sig;
3921 ieee80211_cqm_rssi_notify(
3922 &sdata->vif,
3923 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3924 sig, GFP_KERNEL);
3925 } else if (sig > high &&
3926 (last_event == 0 || last_event <= high)) {
3927 ifmgd->last_cqm_event_signal = sig;
3928 ieee80211_cqm_rssi_notify(
3929 &sdata->vif,
3930 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3931 sig, GFP_KERNEL);
3932 }
3933 }
Tosoni1ad22fb2018-03-14 16:58:34 +00003934}
3935
Sara Sharon78ac51f2019-01-16 18:22:56 +02003936static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3937 struct cfg80211_bss *bss)
3938{
3939 if (ether_addr_equal(tx_bssid, bss->bssid))
3940 return true;
3941 if (!bss->transmitted_bss)
3942 return false;
3943 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3944}
3945
Tosoni1ad22fb2018-03-14 16:58:34 +00003946static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003947 struct ieee80211_hdr *hdr, size_t len,
Tosoni1ad22fb2018-03-14 16:58:34 +00003948 struct ieee80211_rx_status *rx_status)
3949{
3950 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3951 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003952 struct ieee80211_mgmt *mgmt = (void *) hdr;
Tosoni1ad22fb2018-03-14 16:58:34 +00003953 size_t baselen;
3954 struct ieee802_11_elems elems;
3955 struct ieee80211_local *local = sdata->local;
3956 struct ieee80211_chanctx_conf *chanctx_conf;
3957 struct ieee80211_channel *chan;
3958 struct sta_info *sta;
3959 u32 changed = 0;
3960 bool erp_valid;
3961 u8 erp_value = 0;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003962 u32 ncrc = 0;
3963 u8 *bssid, *variable = mgmt->u.beacon.variable;
Tosoni1ad22fb2018-03-14 16:58:34 +00003964 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3965
3966 sdata_assert_lock(sdata);
3967
3968 /* Process beacon from the current BSS */
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003969 bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
3970 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
3971 struct ieee80211_ext *ext = (void *) mgmt;
3972
3973 if (ieee80211_is_s1g_short_beacon(ext->frame_control))
3974 variable = ext->u.s1g_short_beacon.variable;
3975 else
3976 variable = ext->u.s1g_beacon.variable;
3977 }
3978
3979 baselen = (u8 *) variable - (u8 *) mgmt;
Tosoni1ad22fb2018-03-14 16:58:34 +00003980 if (baselen > len)
3981 return;
3982
3983 rcu_read_lock();
3984 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3985 if (!chanctx_conf) {
3986 rcu_read_unlock();
3987 return;
3988 }
3989
Thomas Pedersen3b23c1842020-04-01 18:18:05 -07003990 if (ieee80211_rx_status_to_khz(rx_status) !=
3991 ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
Tosoni1ad22fb2018-03-14 16:58:34 +00003992 rcu_read_unlock();
3993 return;
3994 }
3995 chan = chanctx_conf->def.chan;
3996 rcu_read_unlock();
3997
3998 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003999 ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->bss)) {
4000 ieee802_11_parse_elems(variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004001 len - baselen, false, &elems,
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004002 bssid,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004003 ifmgd->assoc_data->bss->bssid);
Tosoni1ad22fb2018-03-14 16:58:34 +00004004
Sara Sharon4abb52a2019-01-16 12:14:41 +02004005 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Sara Sharon78ac51f2019-01-16 18:22:56 +02004006
4007 if (elems.dtim_period)
4008 ifmgd->dtim_period = elems.dtim_period;
Tosoni1ad22fb2018-03-14 16:58:34 +00004009 ifmgd->have_beacon = true;
4010 ifmgd->assoc_data->need_beacon = false;
4011 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
4012 sdata->vif.bss_conf.sync_tsf =
4013 le64_to_cpu(mgmt->u.beacon.timestamp);
4014 sdata->vif.bss_conf.sync_device_ts =
4015 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004016 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Tosoni1ad22fb2018-03-14 16:58:34 +00004017 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02004018
4019 if (elems.mbssid_config_ie)
4020 bss_conf->profile_periodicity =
4021 elems.mbssid_config_ie->profile_periodicity;
4022
4023 if (elems.ext_capab_len >= 11 &&
4024 (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
4025 bss_conf->ema_ap = true;
4026
Tosoni1ad22fb2018-03-14 16:58:34 +00004027 /* continue assoc process */
4028 ifmgd->assoc_data->timeout = jiffies;
4029 ifmgd->assoc_data->timeout_started = true;
4030 run_again(sdata, ifmgd->assoc_data->timeout);
4031 return;
4032 }
4033
4034 if (!ifmgd->associated ||
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004035 !ieee80211_rx_our_beacon(bssid, ifmgd->associated))
Tosoni1ad22fb2018-03-14 16:58:34 +00004036 return;
4037 bssid = ifmgd->associated->bssid;
4038
4039 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4040 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
4041 local, rx_status);
Andrew Zaborowski2c3c5f8c2017-02-10 04:50:22 +01004042
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004043 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004044 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01004045 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004046 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03004047 }
4048
Johannes Bergb291ba12009-07-10 15:29:03 +02004049 /*
4050 * Push the beacon loss detection into the future since
4051 * we are processing a beacon from the AP just now.
4052 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04004053 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02004054
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004055 /* TODO: CRC urrently not calculated on S1G Beacon Compatibility
4056 * element (which carries the beacon interval). Don't forget to add a
4057 * bit to care_about_ies[] above if mac80211 is interested in a
4058 * changing S1G element.
4059 */
4060 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4061 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4062 ncrc = ieee802_11_parse_elems_crc(variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01004063 len - baselen, false, &elems,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004064 care_about_ies, ncrc,
4065 mgmt->bssid, bssid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02004066
Johannes Berg90d13e82015-09-24 16:13:07 +02004067 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Johannes Berg1db364c2020-04-17 12:38:04 +02004068 ieee80211_check_tim(elems.tim, elems.tim_len, bss_conf->aid)) {
Johannes Berg90d13e82015-09-24 16:13:07 +02004069 if (local->hw.conf.dynamic_ps_timeout > 0) {
4070 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4071 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4072 ieee80211_hw_config(local,
4073 IEEE80211_CONF_CHANGE_PS);
Kalle Valo572e0012009-02-10 17:09:31 +02004074 }
Johannes Berg076cdcb2015-09-24 16:14:55 +02004075 ieee80211_send_nullfunc(local, sdata, false);
Johannes Berg90d13e82015-09-24 16:13:07 +02004076 } else if (!local->pspolling && sdata->u.mgd.powersave) {
4077 local->pspolling = true;
4078
4079 /*
4080 * Here is assumed that the driver will be
4081 * able to send ps-poll frame and receive a
4082 * response even though power save mode is
4083 * enabled, but some drivers might require
4084 * to disable power save here. This needs
4085 * to be investigated.
4086 */
4087 ieee80211_send_pspoll(local, sdata);
Vivek Natarajana97b77b2008-12-23 18:39:02 -08004088 }
4089 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02004090
Janusz.Dziedzic@tieto.comb115b972015-10-27 08:38:40 +01004091 if (sdata->vif.p2p ||
4092 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004093 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01004094 int ret;
4095
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004096 ret = cfg80211_get_p2p_attr(variable,
Johannes Berg488dd7b2012-10-29 20:08:01 +01004097 len - baselen,
4098 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01004099 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004100 if (ret >= 2) {
4101 if (sdata->u.mgd.p2p_noa_index != noa.index) {
4102 /* valid noa_attr and index changed */
4103 sdata->u.mgd.p2p_noa_index = noa.index;
4104 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4105 changed |= BSS_CHANGED_P2P_PS;
4106 /*
4107 * make sure we update all information, the CRC
4108 * mechanism doesn't look at P2P attributes.
4109 */
4110 ifmgd->beacon_crc_valid = false;
4111 }
4112 } else if (sdata->u.mgd.p2p_noa_index != -1) {
4113 /* noa_attr not found and we had valid noa_attr before */
4114 sdata->u.mgd.p2p_noa_index = -1;
4115 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01004116 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01004117 ifmgd->beacon_crc_valid = false;
4118 }
4119 }
4120
Luciano Coelho0c21e632014-10-08 09:48:39 +03004121 if (ifmgd->csa_waiting_bcn)
4122 ieee80211_chswitch_post_beacon(sdata);
4123
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004124 /*
4125 * Update beacon timing and dtim count on every beacon appearance. This
4126 * will allow the driver to use the most updated values. Do it before
4127 * comparing this one with last received beacon.
4128 * IMPORTANT: These parameters would possibly be out of sync by the time
4129 * the driver will use them. The synchronized view is currently
4130 * guaranteed only in certain callbacks.
4131 */
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004132 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
4133 !ieee80211_is_s1g_beacon(hdr->frame_control)) {
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004134 sdata->vif.bss_conf.sync_tsf =
4135 le64_to_cpu(mgmt->u.beacon.timestamp);
4136 sdata->vif.bss_conf.sync_device_ts =
4137 rx_status->device_timestamp;
Sara Sharon78ac51f2019-01-16 18:22:56 +02004138 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Alexander Bondar2ecc3902015-03-01 09:10:00 +02004139 }
4140
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004141 if ((ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) ||
4142 ieee80211_is_s1g_short_beacon(mgmt->frame_control))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004143 return;
Jouni Malinen30196672009-05-19 17:01:43 +03004144 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004145 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03004146
Sara Sharon4abb52a2019-01-16 12:14:41 +02004147 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Johannes Berg7d257452012-07-06 17:37:43 +02004148
Johannes Bergd70b7612013-08-23 15:48:48 +02004149 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004150 rx_status->device_timestamp,
Johannes Bergd70b7612013-08-23 15:48:48 +02004151 &elems, true);
4152
Johannes Berg095d81c2013-10-15 12:25:07 +02004153 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4154 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004155 elems.wmm_param_len,
4156 elems.mu_edca_param_set))
Johannes Berg7d257452012-07-06 17:37:43 +02004157 changed |= BSS_CHANGED_QOS;
4158
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004159 /*
4160 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01004161 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004162 */
Alexander Bondar989c6502013-05-16 17:34:17 +03004163 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004164 /* a few bogus AP send dtim_period = 0 or no TIM IE */
Sara Sharon78ac51f2019-01-16 18:22:56 +02004165 bss_conf->dtim_period = elems.dtim_period ?: 1;
Johannes Bergef429da2013-02-05 17:48:40 +01004166
Alexander Bondar989c6502013-05-16 17:34:17 +03004167 changed |= BSS_CHANGED_BEACON_INFO;
4168 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03004169
4170 mutex_lock(&local->iflist_mtx);
Johannes Berg4a733ef2015-10-14 18:02:43 +02004171 ieee80211_recalc_ps(local);
Alexander Bondar482a9c72013-06-03 17:29:33 +03004172 mutex_unlock(&local->iflist_mtx);
4173
Alexander Bondarce857882013-05-06 17:17:04 +03004174 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004175 }
4176
Johannes Berg1946bed2013-03-27 14:31:53 +01004177 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02004178 erp_valid = true;
4179 erp_value = elems.erp_info[0];
4180 } else {
4181 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04004182 }
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004183
4184 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4185 changed |= ieee80211_handle_bss_capability(sdata,
4186 le16_to_cpu(mgmt->u.beacon.capab_info),
4187 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07004188
Johannes Berg11289582013-02-07 17:36:12 +01004189 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01004190 sta = sta_info_get(sdata, bssid);
4191
John Crispinc9d32452019-05-28 13:49:47 +02004192 changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4193
Johannes Berg2a333a02020-05-28 21:34:35 +02004194 if (ieee80211_config_bw(sdata, sta, elems.ht_cap_elem,
4195 elems.vht_cap_elem, elems.ht_operation,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004196 elems.vht_operation, elems.he_operation,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07004197 elems.s1g_oper, bssid, &changed)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004198 mutex_unlock(&local->sta_mtx);
Johannes Berg89f774e2016-01-25 15:46:36 +02004199 sdata_info(sdata,
4200 "failed to follow AP %pM bandwidth change, disconnect\n",
4201 bssid);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004202 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4203 WLAN_REASON_DEAUTH_LEAVING,
4204 true, deauth_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004205 ieee80211_report_disconnect(sdata, deauth_buf,
4206 sizeof(deauth_buf), true,
4207 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004208 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01004209 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01004210
4211 if (sta && elems.opmode_notif)
4212 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
Eyal Shapiracf1e05c2015-12-08 16:04:36 +02004213 rx_status->band);
Johannes Berg11289582013-02-07 17:36:12 +01004214 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02004215
Steinar H. Gunderson24a4e402014-09-03 06:22:10 -07004216 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4217 elems.country_elem,
4218 elems.country_elem_len,
Steinar H. Gundersonc8d65912014-09-03 06:48:37 -07004219 elems.pwr_constr_elem,
4220 elems.cisco_dtpc_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08004221
Johannes Berg471b3ef2007-12-28 14:32:58 +01004222 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07004223}
4224
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004225void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
4226 struct sk_buff *skb)
4227{
4228 struct ieee80211_rx_status *rx_status;
4229 struct ieee80211_hdr *hdr;
4230 u16 fc;
4231
4232 rx_status = (struct ieee80211_rx_status *) skb->cb;
4233 hdr = (struct ieee80211_hdr *) skb->data;
4234 fc = le16_to_cpu(hdr->frame_control);
4235
4236 sdata_lock(sdata);
4237 switch (fc & IEEE80211_FCTL_STYPE) {
4238 case IEEE80211_STYPE_S1G_BEACON:
4239 ieee80211_rx_mgmt_beacon(sdata, hdr, skb->len, rx_status);
4240 break;
4241 }
4242 sdata_unlock(sdata);
4243}
4244
Johannes Berg1fa57d02010-06-10 10:21:32 +02004245void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4246 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07004247{
4248 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07004249 struct ieee80211_mgmt *mgmt;
4250 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004251 struct ieee802_11_elems elems;
4252 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07004253
Jiri Bencf0706e82007-05-05 11:45:53 -07004254 rx_status = (struct ieee80211_rx_status *) skb->cb;
4255 mgmt = (struct ieee80211_mgmt *) skb->data;
4256 fc = le16_to_cpu(mgmt->frame_control);
4257
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004258 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004259
Johannes Berg66e67e42012-01-20 13:55:27 +01004260 switch (fc & IEEE80211_FCTL_STYPE) {
4261 case IEEE80211_STYPE_BEACON:
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004262 ieee80211_rx_mgmt_beacon(sdata, (void *)mgmt,
4263 skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01004264 break;
4265 case IEEE80211_STYPE_PROBE_RESP:
4266 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4267 break;
4268 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004269 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004270 break;
4271 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004272 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004273 break;
4274 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004275 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004276 break;
4277 case IEEE80211_STYPE_ASSOC_RESP:
4278 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004279 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01004280 break;
4281 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01004282 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004283 ies_len = skb->len -
4284 offsetof(struct ieee80211_mgmt,
4285 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01004286
4287 if (ies_len < 0)
4288 break;
4289
Sara Sharon4abb52a2019-01-16 12:14:41 +02004290 /* CSA IE cannot be overridden, no need for BSSID */
Johannes Berg37799e52013-03-26 14:02:26 +01004291 ieee802_11_parse_elems(
4292 mgmt->u.action.u.chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004293 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg37799e52013-03-26 14:02:26 +01004294
4295 if (elems.parse_error)
4296 break;
4297
Johannes Berg66e67e42012-01-20 13:55:27 +01004298 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004299 rx_status->mactime,
4300 rx_status->device_timestamp,
4301 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004302 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4303 ies_len = skb->len -
4304 offsetof(struct ieee80211_mgmt,
4305 u.action.u.ext_chan_switch.variable);
4306
4307 if (ies_len < 0)
4308 break;
4309
Sara Sharon4abb52a2019-01-16 12:14:41 +02004310 /*
4311 * extended CSA IE can't be overridden, no need for
4312 * BSSID
4313 */
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004314 ieee802_11_parse_elems(
4315 mgmt->u.action.u.ext_chan_switch.variable,
Sara Sharon4abb52a2019-01-16 12:14:41 +02004316 ies_len, true, &elems, mgmt->bssid, NULL);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01004317
4318 if (elems.parse_error)
4319 break;
4320
4321 /* for the handling code pretend this was also an IE */
4322 elems.ext_chansw_ie =
4323 &mgmt->u.action.u.ext_chan_switch.data;
4324
4325 ieee80211_sta_process_chanswitch(sdata,
Luciano Coelho2ba45382014-10-08 09:48:35 +03004326 rx_status->mactime,
4327 rx_status->device_timestamp,
4328 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004329 }
Johannes Berg37799e52013-03-26 14:02:26 +01004330 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004331 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004332 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07004333}
4334
Kees Cook34f11cd2017-10-16 16:35:49 -07004335static void ieee80211_sta_timer(struct timer_list *t)
Jiri Bencf0706e82007-05-05 11:45:53 -07004336{
4337 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004338 from_timer(sdata, t, u.mgd.timer);
Jiri Bencf0706e82007-05-05 11:45:53 -07004339
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01004340 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07004341}
4342
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004343static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01004344 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004345{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004346 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004347
Johannes Berg37ad3882012-02-24 13:50:54 +01004348 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01004349 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01004350
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02004351 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4352 reason);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004353}
4354
Johannes Berg46cad4b2015-08-15 22:39:54 +03004355static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
Johannes Berg66e67e42012-01-20 13:55:27 +01004356{
4357 struct ieee80211_local *local = sdata->local;
4358 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4359 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004360 u32 tx_flags = 0;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004361 u16 trans = 1;
4362 u16 status = 0;
Ilan Peerd4e36e52018-04-20 13:49:25 +03004363 u16 prepare_tx_duration = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004364
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004365 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004366
4367 if (WARN_ON_ONCE(!auth_data))
4368 return -EINVAL;
4369
Johannes Berg66e67e42012-01-20 13:55:27 +01004370 auth_data->tries++;
4371
4372 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004373 sdata_info(sdata, "authentication with %pM timed out\n",
4374 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004375
4376 /*
4377 * Most likely AP is not in the range so remove the
4378 * bss struct for that AP.
4379 */
4380 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4381
4382 return -ETIMEDOUT;
4383 }
4384
Ilan Peerd4e36e52018-04-20 13:49:25 +03004385 if (auth_data->algorithm == WLAN_AUTH_SAE)
4386 prepare_tx_duration =
4387 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4388
4389 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
Johannes Berga1845fc2012-06-27 13:18:36 +02004390
Johannes Berg46cad4b2015-08-15 22:39:54 +03004391 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4392 auth_data->bss->bssid, auth_data->tries,
4393 IEEE80211_AUTH_MAX_TRIES);
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004394
Johannes Berg46cad4b2015-08-15 22:39:54 +03004395 auth_data->expected_transaction = 2;
Johannes Berg66e67e42012-01-20 13:55:27 +01004396
Johannes Berg46cad4b2015-08-15 22:39:54 +03004397 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4398 trans = auth_data->sae_trans;
4399 status = auth_data->sae_status;
4400 auth_data->expected_transaction = trans;
Johannes Berg66e67e42012-01-20 13:55:27 +01004401 }
4402
Johannes Berg46cad4b2015-08-15 22:39:54 +03004403 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4404 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4405 IEEE80211_TX_INTFL_MLME_CONN_TX;
4406
4407 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4408 auth_data->data, auth_data->data_len,
4409 auth_data->bss->bssid,
4410 auth_data->bss->bssid, NULL, 0, 0,
4411 tx_flags);
4412
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02004413 if (tx_flags == 0) {
Ilan Peer407879b2018-04-20 13:49:20 +03004414 if (auth_data->algorithm == WLAN_AUTH_SAE)
4415 auth_data->timeout = jiffies +
4416 IEEE80211_AUTH_TIMEOUT_SAE;
4417 else
4418 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004419 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004420 auth_data->timeout =
4421 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004422 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004423
Ilan Peer407879b2018-04-20 13:49:20 +03004424 auth_data->timeout_started = true;
4425 run_again(sdata, auth_data->timeout);
4426
Johannes Berg66e67e42012-01-20 13:55:27 +01004427 return 0;
4428}
4429
4430static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4431{
4432 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4433 struct ieee80211_local *local = sdata->local;
4434
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004435 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01004436
Johannes Berg66e67e42012-01-20 13:55:27 +01004437 assoc_data->tries++;
4438 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004439 sdata_info(sdata, "association with %pM timed out\n",
4440 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004441
4442 /*
4443 * Most likely AP is not in the range so remove the
4444 * bss struct for that AP.
4445 */
4446 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4447
4448 return -ETIMEDOUT;
4449 }
4450
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004451 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4452 assoc_data->bss->bssid, assoc_data->tries,
4453 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01004454 ieee80211_send_assoc(sdata);
4455
Johannes Berg30686bf2015-06-02 21:39:54 +02004456 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004457 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01004458 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004459 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01004460 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02004461 assoc_data->timeout =
4462 round_jiffies_up(jiffies +
4463 IEEE80211_ASSOC_TIMEOUT_LONG);
4464 assoc_data->timeout_started = true;
4465 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004466 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004467
4468 return 0;
4469}
4470
Johannes Berg1672c0e32013-01-29 15:02:27 +01004471void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4472 __le16 fc, bool acked)
4473{
4474 struct ieee80211_local *local = sdata->local;
4475
4476 sdata->u.mgd.status_fc = fc;
4477 sdata->u.mgd.status_acked = acked;
4478 sdata->u.mgd.status_received = true;
4479
4480 ieee80211_queue_work(&local->hw, &sdata->work);
4481}
4482
Johannes Berg1fa57d02010-06-10 10:21:32 +02004483void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004484{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004485 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02004486 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004487
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004488 sdata_lock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004489
Johannes Berg1672c0e32013-01-29 15:02:27 +01004490 if (ifmgd->status_received) {
4491 __le16 fc = ifmgd->status_fc;
4492 bool status_acked = ifmgd->status_acked;
4493
4494 ifmgd->status_received = false;
Johannes Berg46cad4b2015-08-15 22:39:54 +03004495 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01004496 if (status_acked) {
Ilan Peer407879b2018-04-20 13:49:20 +03004497 if (ifmgd->auth_data->algorithm ==
4498 WLAN_AUTH_SAE)
4499 ifmgd->auth_data->timeout =
4500 jiffies +
4501 IEEE80211_AUTH_TIMEOUT_SAE;
4502 else
4503 ifmgd->auth_data->timeout =
4504 jiffies +
4505 IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004506 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004507 } else {
4508 ifmgd->auth_data->timeout = jiffies - 1;
4509 }
Johannes Berg89afe612013-02-13 15:39:57 +01004510 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004511 } else if (ifmgd->assoc_data &&
4512 (ieee80211_is_assoc_req(fc) ||
4513 ieee80211_is_reassoc_req(fc))) {
4514 if (status_acked) {
4515 ifmgd->assoc_data->timeout =
4516 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004517 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01004518 } else {
4519 ifmgd->assoc_data->timeout = jiffies - 1;
4520 }
Johannes Berg89afe612013-02-13 15:39:57 +01004521 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01004522 }
4523 }
4524
Johannes Berg89afe612013-02-13 15:39:57 +01004525 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004526 time_after(jiffies, ifmgd->auth_data->timeout)) {
4527 if (ifmgd->auth_data->done) {
4528 /*
4529 * ok ... we waited for assoc but userspace didn't,
4530 * so let's just kill the auth data
4531 */
4532 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg46cad4b2015-08-15 22:39:54 +03004533 } else if (ieee80211_auth(sdata)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004534 u8 bssid[ETH_ALEN];
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004535 struct ieee80211_event event = {
4536 .type = MLME_EVENT,
4537 .u.mlme.data = AUTH_EVENT,
4538 .u.mlme.status = MLME_TIMEOUT,
4539 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004540
4541 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4542
4543 ieee80211_destroy_auth_data(sdata, false);
4544
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004545 cfg80211_auth_timeout(sdata->dev, bssid);
Emmanuel Grumbacha9409092015-03-16 23:23:35 +02004546 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004547 }
Johannes Berg89afe612013-02-13 15:39:57 +01004548 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004549 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004550
Johannes Berg89afe612013-02-13 15:39:57 +01004551 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01004552 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03004553 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01004554 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02004555 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004556 struct ieee80211_event event = {
4557 .type = MLME_EVENT,
4558 .u.mlme.data = ASSOC_EVENT,
4559 .u.mlme.status = MLME_TIMEOUT,
4560 };
Johannes Berg66e67e42012-01-20 13:55:27 +01004561
Johannes Berge6f462d2016-12-08 17:22:09 +01004562 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004563 cfg80211_assoc_timeout(sdata->dev, bss);
Emmanuel Grumbachd0d1a122015-03-16 23:23:36 +02004564 drv_event_callback(sdata->local, sdata, &event);
Johannes Berg66e67e42012-01-20 13:55:27 +01004565 }
Johannes Berg89afe612013-02-13 15:39:57 +01004566 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004567 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004568
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02004569 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02004570 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03004571 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004572 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03004573
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004574 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004575
Johannes Berg30686bf2015-06-02 21:39:54 +02004576 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
Ben Greear180205b2011-02-04 15:30:24 -08004577 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004578 else
Ben Greear180205b2011-02-04 15:30:24 -08004579 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01004580
Felix Fietkau4e5ff372010-11-23 03:10:31 +01004581 /* ACK received for nullfunc probing frame */
4582 if (!ifmgd->probe_send_count)
4583 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004584 else if (ifmgd->nullfunc_failed) {
4585 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004586 mlme_dbg(sdata,
4587 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4588 bssid, ifmgd->probe_send_count,
4589 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004590 ieee80211_mgd_probe_ap_send(sdata);
4591 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004592 mlme_dbg(sdata,
4593 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4594 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02004595 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004596 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4597 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004598 }
4599 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004600 run_again(sdata, ifmgd->probe_timeout);
Johannes Berg30686bf2015-06-02 21:39:54 +02004601 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004602 mlme_dbg(sdata,
4603 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4604 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02004605 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004606 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004607 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004608 mlme_dbg(sdata,
4609 "No probe response from AP %pM after %dms, try %d/%i\n",
4610 bssid, probe_wait_ms,
4611 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03004612 ieee80211_mgd_probe_ap_send(sdata);
4613 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02004614 /*
4615 * We actually lost the connection ... or did we?
4616 * Let's make sure!
4617 */
Johannes Berg89f774e2016-01-25 15:46:36 +02004618 mlme_dbg(sdata,
4619 "No probe response from AP %pM after %dms, disconnecting.\n",
4620 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01004621
Johannes Berg95acac62011-07-12 12:30:59 +02004622 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01004623 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02004624 }
4625 }
4626
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004627 sdata_unlock(sdata);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004628}
Johannes Berg0a51b272008-09-08 17:44:25 +02004629
Kees Cook34f11cd2017-10-16 16:35:49 -07004630static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004631{
4632 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004633 from_timer(sdata, t, u.mgd.bcn_mon_timer);
Luciano Coelho0c21e632014-10-08 09:48:39 +03004634 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004635
Luciano Coelho0c21e632014-10-08 09:48:39 +03004636 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004637 return;
4638
Loic Poulain0b911112020-06-18 11:17:42 +02004639 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
4640 return;
4641
Johannes Berg682bd382013-01-29 13:13:50 +01004642 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004643 ieee80211_queue_work(&sdata->local->hw,
4644 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004645}
4646
Kees Cook34f11cd2017-10-16 16:35:49 -07004647static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
Johannes Bergb291ba12009-07-10 15:29:03 +02004648{
4649 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -07004650 from_timer(sdata, t, u.mgd.conn_mon_timer);
Johannes Bergb291ba12009-07-10 15:29:03 +02004651 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4652 struct ieee80211_local *local = sdata->local;
Felix Fietkau9abf4e42020-09-08 14:36:56 +02004653 struct sta_info *sta;
4654 unsigned long timeout;
Johannes Bergb291ba12009-07-10 15:29:03 +02004655
Luciano Coelho0c21e632014-10-08 09:48:39 +03004656 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
Michal Kaziore5593f52014-04-09 15:45:36 +02004657 return;
4658
Felix Fietkau9abf4e42020-09-08 14:36:56 +02004659 sta = sta_info_get(sdata, ifmgd->bssid);
4660 if (!sta)
4661 return;
4662
4663 timeout = sta->status_stats.last_ack;
4664 if (time_before(sta->status_stats.last_ack, sta->rx_stats.last_rx))
4665 timeout = sta->rx_stats.last_rx;
4666 timeout += IEEE80211_CONNECTION_IDLE_TIME;
4667
4668 if (time_is_before_jiffies(timeout)) {
4669 mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
4670 return;
4671 }
4672
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04004673 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02004674}
4675
4676static void ieee80211_sta_monitor_work(struct work_struct *work)
4677{
4678 struct ieee80211_sub_if_data *sdata =
4679 container_of(work, struct ieee80211_sub_if_data,
4680 u.mgd.monitor_work);
4681
4682 ieee80211_mgd_probe_ap(sdata, false);
4683}
4684
Johannes Berga1678f82008-09-11 00:01:47 +02004685static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4686{
Kalle Vaload935682009-04-19 08:47:19 +03004687 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02004688 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03004689
Johannes Bergb291ba12009-07-10 15:29:03 +02004690 /* let's probe the connection once */
Johannes Berg30686bf2015-06-02 21:39:54 +02004691 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
Eliad Peller494f1fe2012-02-19 15:26:09 +02004692 ieee80211_queue_work(&sdata->local->hw,
4693 &sdata->u.mgd.monitor_work);
Kalle Vaload935682009-04-19 08:47:19 +03004694 }
Johannes Berga1678f82008-09-11 00:01:47 +02004695}
4696
Johannes Bergb8360ab2013-04-29 14:57:44 +02004697#ifdef CONFIG_PM
Johannes Berg1a1cb742014-03-19 09:55:55 +01004698void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4699{
4700 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4701 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4702
4703 sdata_lock(sdata);
4704
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004705 if (ifmgd->auth_data || ifmgd->assoc_data) {
4706 const u8 *bssid = ifmgd->auth_data ?
4707 ifmgd->auth_data->bss->bssid :
4708 ifmgd->assoc_data->bss->bssid;
4709
Johannes Berg1a1cb742014-03-19 09:55:55 +01004710 /*
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004711 * If we are trying to authenticate / associate while suspending,
4712 * cfg80211 won't know and won't actually abort those attempts,
4713 * thus we need to do that ourselves.
Johannes Berg1a1cb742014-03-19 09:55:55 +01004714 */
Johannes Berg4b08d1b2019-08-30 14:24:51 +03004715 ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
Johannes Berg1a1cb742014-03-19 09:55:55 +01004716 IEEE80211_STYPE_DEAUTH,
4717 WLAN_REASON_DEAUTH_LEAVING,
4718 false, frame_buf);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004719 if (ifmgd->assoc_data)
Johannes Berge6f462d2016-12-08 17:22:09 +01004720 ieee80211_destroy_assoc_data(sdata, false, true);
Emmanuel Grumbachc52666a2014-05-13 12:54:09 +03004721 if (ifmgd->auth_data)
4722 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg1a1cb742014-03-19 09:55:55 +01004723 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4724 IEEE80211_DEAUTH_FRAME_LEN);
4725 }
4726
Johannes Bergbe72afe2015-03-01 09:10:03 +02004727 /* This is a bit of a hack - we should find a better and more generic
4728 * solution to this. Normally when suspending, cfg80211 will in fact
4729 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4730 * auth (not so important) or assoc (this is the problem) process.
4731 *
4732 * As a consequence, it can happen that we are in the process of both
4733 * associating and suspending, and receive an association response
4734 * after cfg80211 has checked if it needs to disconnect, but before
4735 * we actually set the flag to drop incoming frames. This will then
4736 * cause the workqueue flush to process the association response in
4737 * the suspend, resulting in a successful association just before it
4738 * tries to remove the interface from the driver, which now though
4739 * has a channel context assigned ... this results in issues.
4740 *
4741 * To work around this (for now) simply deauth here again if we're
4742 * now connected.
4743 */
4744 if (ifmgd->associated && !sdata->local->wowlan) {
4745 u8 bssid[ETH_ALEN];
4746 struct cfg80211_deauth_request req = {
4747 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4748 .bssid = bssid,
4749 };
4750
4751 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4752 ieee80211_mgd_deauth(sdata, &req);
4753 }
4754
Johannes Berg1a1cb742014-03-19 09:55:55 +01004755 sdata_unlock(sdata);
4756}
4757
Johannes Bergb8360ab2013-04-29 14:57:44 +02004758void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4759{
4760 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4761
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004762 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004763 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004764 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004765 return;
4766 }
4767
4768 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4769 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4770 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4771 ieee80211_sta_connection_lost(sdata,
4772 ifmgd->associated->bssid,
4773 WLAN_REASON_UNSPECIFIED,
4774 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004775 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004776 return;
4777 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004778 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02004779}
4780#endif
4781
Johannes Berg9c6bd792008-09-11 00:01:52 +02004782/* interface setup */
4783void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4784{
Johannes Berg46900292009-02-15 12:44:28 +01004785 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004786
Johannes Berg46900292009-02-15 12:44:28 +01004787 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02004788 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01004789 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02004790 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4791 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02004792 INIT_WORK(&ifmgd->csa_connection_drop_work,
4793 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03004794 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03004795 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4796 ieee80211_tdls_peer_del_work);
Kees Cook34f11cd2017-10-16 16:35:49 -07004797 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4798 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4799 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4800 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
Johannes Berg02219b32014-10-07 10:38:50 +03004801 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4802 ieee80211_sta_handle_tspec_ac_params_wk);
Johannes Berg9c6bd792008-09-11 00:01:52 +02004803
Johannes Bergab1faea2009-07-01 21:41:17 +02004804 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05304805 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02004806 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4807 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01004808 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02004809
Eliad Peller0d8614b2014-09-10 14:07:36 +03004810 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
Johannes Berg0f782312009-12-01 13:37:02 +01004811 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4812 else
4813 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02004814
4815 /* Setup TDLS data */
4816 spin_lock_init(&ifmgd->teardown_lock);
4817 ifmgd->teardown_skb = NULL;
4818 ifmgd->orig_teardown_skb = NULL;
Johannes Berg9c6bd792008-09-11 00:01:52 +02004819}
4820
4821/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02004822void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4823{
Johannes Berg31ee67a2012-07-06 21:18:24 +02004824 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02004825
4826 /* Restart STA timers */
4827 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07004828 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4829 if (ieee80211_sdata_running(sdata))
4830 ieee80211_restart_sta_timer(sdata);
4831 }
Johannes Berga1678f82008-09-11 00:01:47 +02004832 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02004833}
Johannes Berg10f644a2009-04-16 13:17:25 +02004834
Johannes Bergf2d9d272012-11-22 14:11:39 +01004835static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4836 struct cfg80211_bss *cbss)
4837{
4838 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4839 const u8 *ht_cap_ie, *vht_cap_ie;
4840 const struct ieee80211_ht_cap *ht_cap;
4841 const struct ieee80211_vht_cap *vht_cap;
4842 u8 chains = 1;
4843
4844 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4845 return chains;
4846
Johannes Berg9caf0362012-11-29 01:25:20 +01004847 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004848 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4849 ht_cap = (void *)(ht_cap_ie + 2);
4850 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4851 /*
4852 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4853 * "Tx Unequal Modulation Supported" fields.
4854 */
4855 }
4856
4857 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4858 return chains;
4859
Johannes Berg9caf0362012-11-29 01:25:20 +01004860 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01004861 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4862 u8 nss;
4863 u16 tx_mcs_map;
4864
4865 vht_cap = (void *)(vht_cap_ie + 2);
4866 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4867 for (nss = 8; nss > 0; nss--) {
4868 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4869 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4870 break;
4871 }
4872 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4873 chains = max(chains, nss);
4874 }
4875
4876 return chains;
4877}
4878
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004879static bool
4880ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4881 const struct ieee80211_he_operation *he_op)
4882{
4883 const struct ieee80211_sta_he_cap *sta_he_cap =
4884 ieee80211_get_he_sta_cap(sband);
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004885 u16 ap_min_req_set;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004886 int i;
4887
4888 if (!sta_he_cap || !he_op)
4889 return false;
4890
Gustavo A. R. Silva47aa7862018-06-18 07:41:34 -05004891 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4892
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004893 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4894 for (i = 0; i < 3; i++) {
4895 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4896 &sta_he_cap->he_mcs_nss_supp;
4897 u16 sta_mcs_map_rx =
4898 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4899 u16 sta_mcs_map_tx =
4900 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4901 u8 nss;
4902 bool verified = true;
4903
4904 /*
4905 * For each band there is a maximum of 8 spatial streams
4906 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4907 * of 2 bits per NSS (1-8), with the values defined in enum
4908 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4909 * capabilities aren't less than the AP's minimum requirements
4910 * for this HE BSS per SS.
4911 * It is enough to find one such band that meets the reqs.
4912 */
4913 for (nss = 8; nss > 0; nss--) {
4914 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4915 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4916 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4917
4918 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4919 continue;
4920
4921 /*
4922 * Make sure the HE AP doesn't require MCSs that aren't
4923 * supported by the client
4924 */
4925 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4926 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4927 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4928 verified = false;
4929 break;
4930 }
4931 }
4932
4933 if (verified)
4934 return true;
4935 }
4936
4937 /* If here, STA doesn't meet AP's HE min requirements */
4938 return false;
4939}
4940
Johannes Bergb17166a2012-07-27 11:41:27 +02004941static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4942 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01004943{
4944 struct ieee80211_local *local = sdata->local;
4945 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Peller53b954e2014-07-24 11:20:05 +03004946 const struct ieee80211_ht_cap *ht_cap = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004947 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004948 const struct ieee80211_vht_operation *vht_oper = NULL;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004949 const struct ieee80211_he_operation *he_oper = NULL;
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07004950 const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02004951 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01004952 struct cfg80211_chan_def chandef;
Johannes Berg57fa5e82020-05-28 21:34:36 +02004953 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
Wen Gong780a8c92020-09-11 10:29:02 +00004954 bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
Johannes Berg2a333a02020-05-28 21:34:35 +02004955 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004956 int ret;
Arik Nemtsov52a45f32015-08-15 22:39:53 +03004957 u32 i;
4958 bool have_80mhz;
Johannes Berga1cf7752012-03-08 15:02:07 +01004959
Johannes Berg24398e32012-03-28 10:58:36 +02004960 sband = local->hw.wiphy->bands[cbss->channel->band];
4961
Johannes Bergf2d9d272012-11-22 14:11:39 +01004962 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4963 IEEE80211_STA_DISABLE_80P80MHZ |
4964 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02004965
Johannes Berg75e296e2020-01-31 13:12:39 +02004966 /* disable HT/VHT/HE if we don't support them */
Johannes Berg57fa5e82020-05-28 21:34:36 +02004967 if (!sband->ht_cap.ht_supported && !is_6ghz) {
Johannes Berg75e296e2020-01-31 13:12:39 +02004968 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4969 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4970 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4971 }
4972
Wen Gong780a8c92020-09-11 10:29:02 +00004973 if (!sband->vht_cap.vht_supported && is_5ghz) {
Johannes Berg75e296e2020-01-31 13:12:39 +02004974 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg57fa5e82020-05-28 21:34:36 +02004975 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4976 }
Johannes Berg75e296e2020-01-31 13:12:39 +02004977
4978 if (!ieee80211_get_he_sta_cap(sband))
4979 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4980
Johannes Berg9caf0362012-11-29 01:25:20 +01004981 rcu_read_lock();
4982
Johannes Berg57fa5e82020-05-28 21:34:36 +02004983 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
Eliad Peller53b954e2014-07-24 11:20:05 +03004984 const u8 *ht_oper_ie, *ht_cap_ie;
Johannes Bergf2d9d272012-11-22 14:11:39 +01004985
Johannes Berg9caf0362012-11-29 01:25:20 +01004986 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02004987 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4988 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6effa2013-02-07 23:33:32 +01004989
Eliad Peller53b954e2014-07-24 11:20:05 +03004990 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4991 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4992 ht_cap = (void *)(ht_cap_ie + 2);
4993
4994 if (!ht_cap) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01004995 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4996 ht_oper = NULL;
4997 }
Johannes Berg24398e32012-03-28 10:58:36 +02004998 }
4999
Johannes Berg57fa5e82020-05-28 21:34:36 +02005000 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01005001 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01005002
Johannes Berg9caf0362012-11-29 01:25:20 +01005003 vht_oper_ie = ieee80211_bss_get_ie(cbss,
5004 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01005005 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
5006 vht_oper = (void *)(vht_oper_ie + 2);
5007 if (vht_oper && !ht_oper) {
5008 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005009 sdata_info(sdata,
Johannes Berg75e296e2020-01-31 13:12:39 +02005010 "AP advertised VHT without HT, disabling HT/VHT/HE\n");
Johannes Bergcb145022013-02-07 20:41:50 +01005011 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5012 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005013 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg24398e32012-03-28 10:58:36 +02005014 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01005015
5016 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
5017 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
5018 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5019 vht_oper = NULL;
5020 }
Johannes Berg24398e32012-03-28 10:58:36 +02005021 }
5022
Shaul Triebitz002245e2018-12-15 11:03:19 +02005023 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005024 const struct cfg80211_bss_ies *ies;
5025 const u8 *he_oper_ie;
5026
5027 ies = rcu_dereference(cbss->ies);
5028 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
5029 ies->data, ies->len);
5030 if (he_oper_ie &&
5031 he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
5032 he_oper = (void *)(he_oper_ie + 3);
5033 else
5034 he_oper = NULL;
5035
Johannes Bergf0c04072018-06-29 09:51:39 +02005036 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005037 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5038 }
5039
Arik Nemtsov52a45f32015-08-15 22:39:53 +03005040 /* Allow VHT if at least one channel on the sband supports 80 MHz */
5041 have_80mhz = false;
5042 for (i = 0; i < sband->n_channels; i++) {
5043 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5044 IEEE80211_CHAN_NO_80MHZ))
5045 continue;
5046
5047 have_80mhz = true;
5048 break;
5049 }
5050
5051 if (!have_80mhz)
5052 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5053
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005054 if (sband->band == NL80211_BAND_S1GHZ) {
5055 const u8 *s1g_oper_ie;
5056
5057 s1g_oper_ie = ieee80211_bss_get_ie(cbss,
5058 WLAN_EID_S1G_OPERATION);
5059 if (s1g_oper_ie && s1g_oper_ie[1] >= sizeof(*s1g_oper))
5060 s1g_oper = (void *)(s1g_oper_ie + 2);
5061 else
5062 sdata_info(sdata,
5063 "AP missing S1G operation element?\n");
5064 }
5065
Johannes Bergf2d9d272012-11-22 14:11:39 +01005066 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
5067 cbss->channel,
Johannes Berg2a333a02020-05-28 21:34:35 +02005068 bss->vht_cap_info,
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005069 ht_oper, vht_oper, he_oper,
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005070 s1g_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02005071 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02005072
Johannes Bergf2d9d272012-11-22 14:11:39 +01005073 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
5074 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02005075
Johannes Berg9caf0362012-11-29 01:25:20 +01005076 rcu_read_unlock();
5077
Johannes Berg57fa5e82020-05-28 21:34:36 +02005078 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
5079 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
5080 return -EINVAL;
5081 }
5082
Johannes Berg04ecd252012-09-11 14:34:12 +02005083 /* will change later if needed */
5084 sdata->smps_mode = IEEE80211_SMPS_OFF;
5085
Johannes Berg34a37402013-12-18 09:43:33 +01005086 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01005087 /*
5088 * If this fails (possibly due to channel context sharing
5089 * on incompatible channels, e.g. 80+80 and 160 sharing the
5090 * same control channel) try to use a smaller bandwidth.
5091 */
5092 ret = ieee80211_vif_use_channel(sdata, &chandef,
5093 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02005094
5095 /* don't downgrade for 5 and 10 MHz channels, though. */
5096 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
5097 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01005098 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02005099
Johannes Bergd601cd82013-02-07 20:54:51 +01005100 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02005101 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01005102 ret = ieee80211_vif_use_channel(sdata, &chandef,
5103 IEEE80211_CHANCTX_SHARED);
5104 }
Johannes Berg34a37402013-12-18 09:43:33 +01005105 out:
5106 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01005107 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02005108}
5109
Sara Sharon78ac51f2019-01-16 18:22:56 +02005110static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5111 u8 *dtim_count, u8 *dtim_period)
5112{
5113 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5114 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5115 ies->len);
5116 const struct ieee80211_tim_ie *tim = NULL;
5117 const struct ieee80211_bssid_index *idx;
5118 bool valid = tim_ie && tim_ie[1] >= 2;
5119
5120 if (valid)
5121 tim = (void *)(tim_ie + 2);
5122
5123 if (dtim_count)
5124 *dtim_count = valid ? tim->dtim_count : 0;
5125
5126 if (dtim_period)
5127 *dtim_period = valid ? tim->dtim_period : 0;
5128
5129 /* Check if value is overridden by non-transmitted profile */
5130 if (!idx_ie || idx_ie[1] < 3)
5131 return valid;
5132
5133 idx = (void *)(idx_ie + 2);
5134
5135 if (dtim_count)
5136 *dtim_count = idx->dtim_count;
5137
5138 if (dtim_period)
5139 *dtim_period = idx->dtim_period;
5140
5141 return true;
5142}
5143
Johannes Bergb17166a2012-07-27 11:41:27 +02005144static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005145 struct cfg80211_bss *cbss, bool assoc,
5146 bool override)
Johannes Bergb17166a2012-07-27 11:41:27 +02005147{
5148 struct ieee80211_local *local = sdata->local;
5149 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5150 struct ieee80211_bss *bss = (void *)cbss->priv;
5151 struct sta_info *new_sta = NULL;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005152 struct ieee80211_supported_band *sband;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005153 bool have_sta = false;
Johannes Bergb17166a2012-07-27 11:41:27 +02005154 int err;
5155
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005156 sband = local->hw.wiphy->bands[cbss->channel->band];
5157
Johannes Bergb17166a2012-07-27 11:41:27 +02005158 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5159 return -EINVAL;
5160
Luca Coelhof8860ce2017-05-02 17:56:21 +03005161 /* If a reconfig is happening, bail out */
5162 if (local->in_reconfig)
5163 return -EBUSY;
5164
Johannes Bergb17166a2012-07-27 11:41:27 +02005165 if (assoc) {
5166 rcu_read_lock();
5167 have_sta = sta_info_get(sdata, cbss->bssid);
5168 rcu_read_unlock();
5169 }
5170
5171 if (!have_sta) {
5172 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5173 if (!new_sta)
5174 return -ENOMEM;
5175 }
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005176
Johannes Bergc87905b2017-06-10 13:52:43 +03005177 /*
5178 * Set up the information for the new channel before setting the
5179 * new channel. We can't - completely race-free - change the basic
5180 * rates bitmap and the channel (sband) that it refers to, but if
5181 * we set it up before we at least avoid calling into the driver's
5182 * bss_info_changed() method with invalid information (since we do
5183 * call that from changing the channel - only for IDLE and perhaps
5184 * some others, but ...).
5185 *
5186 * So to avoid that, just set up all the new information before the
5187 * channel, but tell the driver to apply it only afterwards, since
5188 * it might need the new channel for that.
5189 */
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005190 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005191 u32 rates = 0, basic_rates = 0;
5192 bool have_higher_than_11mbit;
5193 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005194 const struct cfg80211_bss_ies *ies;
Chaya Rachel Ivgi179b8fc2014-12-14 10:38:59 +02005195 int shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005196
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005197 /* TODO: S1G Basic Rate Set is expressed elsewhere */
Thomas Pedersen12bf8fa2020-10-05 09:45:22 -07005198 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5199 ieee80211_s1g_sta_rate_init(new_sta);
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005200 goto skip_rates;
Thomas Pedersen12bf8fa2020-10-05 09:45:22 -07005201 }
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005202
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005203 ieee80211_get_rates(sband, bss->supp_rates,
5204 bss->supp_rates_len,
5205 &rates, &basic_rates,
5206 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02005207 &min_rate, &min_rate_index,
Johannes Berg44f6d422017-06-10 13:52:44 +03005208 shift);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005209
5210 /*
5211 * This used to be a workaround for basic rates missing
5212 * in the association response frame. Now that we no
5213 * longer use the basic rates from there, it probably
5214 * doesn't happen any more, but keep the workaround so
5215 * in case some *other* APs are buggy in different ways
5216 * we can connect -- with a warning.
Ilan Peer302ff8b2020-03-26 15:09:39 +02005217 * Allow this workaround only in case the AP provided at least
5218 * one rate.
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005219 */
Ilan Peer302ff8b2020-03-26 15:09:39 +02005220 if (min_rate_index < 0) {
5221 sdata_info(sdata,
5222 "No legacy rates in association response\n");
5223
5224 sta_info_free(local, new_sta);
5225 return -EINVAL;
5226 } else if (!basic_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005227 sdata_info(sdata,
5228 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005229 basic_rates = BIT(min_rate_index);
5230 }
5231
Johannes Bergbd718fc2019-05-29 15:25:35 +03005232 if (rates)
5233 new_sta->sta.supp_rates[cbss->channel->band] = rates;
5234 else
5235 sdata_info(sdata,
5236 "No rates found, keeping mandatory only\n");
5237
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005238 sdata->vif.bss_conf.basic_rates = basic_rates;
5239
5240 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg57fbcce2016-04-12 15:56:15 +02005241 if (cbss->channel->band == NL80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005242 have_higher_than_11mbit)
5243 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5244 else
5245 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5246
Thomas Pedersen1d00ce82020-09-21 19:28:15 -07005247skip_rates:
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005248 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5249
Johannes Berg8c358bc2012-05-22 22:13:05 +02005250 /* set timing information */
5251 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005252 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01005253 ies = rcu_dereference(cbss->beacon_ies);
5254 if (ies) {
Johannes Bergef429da2013-02-05 17:48:40 +01005255 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5256 sdata->vif.bss_conf.sync_device_ts =
5257 bss->device_ts_beacon;
Sara Sharon78ac51f2019-01-16 18:22:56 +02005258
5259 ieee80211_get_dtim(ies,
5260 &sdata->vif.bss_conf.sync_dtim_count,
5261 NULL);
Johannes Berg30686bf2015-06-02 21:39:54 +02005262 } else if (!ieee80211_hw_check(&sdata->local->hw,
5263 TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005264 ies = rcu_dereference(cbss->proberesp_ies);
5265 /* must be non-NULL since beacon IEs were NULL */
5266 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5267 sdata->vif.bss_conf.sync_device_ts =
5268 bss->device_ts_presp;
5269 sdata->vif.bss_conf.sync_dtim_count = 0;
5270 } else {
5271 sdata->vif.bss_conf.sync_tsf = 0;
5272 sdata->vif.bss_conf.sync_device_ts = 0;
5273 sdata->vif.bss_conf.sync_dtim_count = 0;
5274 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01005275 rcu_read_unlock();
Johannes Bergc87905b2017-06-10 13:52:43 +03005276 }
Johannes Berg8c358bc2012-05-22 22:13:05 +02005277
Johannes Bergc87905b2017-06-10 13:52:43 +03005278 if (new_sta || override) {
5279 err = ieee80211_prep_channel(sdata, cbss);
5280 if (err) {
5281 if (new_sta)
5282 sta_info_free(local, new_sta);
5283 return -EINVAL;
5284 }
5285 }
5286
5287 if (new_sta) {
5288 /*
5289 * tell driver about BSSID, basic rates and timing
5290 * this was set up above, before setting the channel
5291 */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01005292 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02005293 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5294 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01005295
5296 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005297 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01005298
Johannes Berg13e0c8e2012-07-27 10:43:16 +02005299 err = sta_info_insert(new_sta);
5300 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01005301 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005302 sdata_info(sdata,
5303 "failed to insert STA entry for the AP (error %d)\n",
5304 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01005305 return err;
5306 }
5307 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00005308 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01005309
David Spinadel7d830a12015-03-17 19:58:38 +02005310 /* Cancel scan to ensure that nothing interferes with connection */
5311 if (local->scanning)
5312 ieee80211_scan_cancel(local);
5313
Johannes Berga1cf7752012-03-08 15:02:07 +01005314 return 0;
5315}
5316
Johannes Berg77fdaa12009-07-07 03:45:17 +02005317/* config hooks */
5318int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5319 struct cfg80211_auth_request *req)
5320{
Johannes Berg66e67e42012-01-20 13:55:27 +01005321 struct ieee80211_local *local = sdata->local;
5322 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5323 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005324 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01005325 int err;
Jouni Malinenefb543e2018-10-11 00:21:21 +03005326 bool cont_auth;
Johannes Berg66e67e42012-01-20 13:55:27 +01005327
5328 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02005329
5330 switch (req->auth_type) {
5331 case NL80211_AUTHTYPE_OPEN_SYSTEM:
5332 auth_alg = WLAN_AUTH_OPEN;
5333 break;
5334 case NL80211_AUTHTYPE_SHARED_KEY:
Ard Biesheuvel5fdb3732019-06-12 18:19:54 +02005335 if (fips_enabled)
Johannes Berg9dca9c42010-07-21 10:09:25 +02005336 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005337 auth_alg = WLAN_AUTH_SHARED_KEY;
5338 break;
5339 case NL80211_AUTHTYPE_FT:
5340 auth_alg = WLAN_AUTH_FT;
5341 break;
5342 case NL80211_AUTHTYPE_NETWORK_EAP:
5343 auth_alg = WLAN_AUTH_LEAP;
5344 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005345 case NL80211_AUTHTYPE_SAE:
5346 auth_alg = WLAN_AUTH_SAE;
5347 break;
Jouni Malinendbc0c2c2016-10-27 00:42:04 +03005348 case NL80211_AUTHTYPE_FILS_SK:
5349 auth_alg = WLAN_AUTH_FILS_SK;
5350 break;
5351 case NL80211_AUTHTYPE_FILS_SK_PFS:
5352 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5353 break;
5354 case NL80211_AUTHTYPE_FILS_PK:
5355 auth_alg = WLAN_AUTH_FILS_PK;
5356 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005357 default:
5358 return -EOPNOTSUPP;
5359 }
5360
Jouni Malinenefb543e2018-10-11 00:21:21 +03005361 if (ifmgd->assoc_data)
Jouni Malinen8d7432a2018-10-11 00:21:20 +03005362 return -EBUSY;
5363
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005364 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005365 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01005366 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005367 return -ENOMEM;
5368
Johannes Berg66e67e42012-01-20 13:55:27 +01005369 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005370
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005371 if (req->auth_data_len >= 4) {
Jouni Malinen6ec63612016-10-27 00:41:59 +03005372 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5373 __le16 *pos = (__le16 *) req->auth_data;
5374
5375 auth_data->sae_trans = le16_to_cpu(pos[0]);
5376 auth_data->sae_status = le16_to_cpu(pos[1]);
5377 }
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03005378 memcpy(auth_data->data, req->auth_data + 4,
5379 req->auth_data_len - 4);
5380 auth_data->data_len += req->auth_data_len - 4;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005381 }
5382
Jouni Malinenefb543e2018-10-11 00:21:21 +03005383 /* Check if continuing authentication or trying to authenticate with the
5384 * same BSS that we were in the process of authenticating with and avoid
5385 * removal and re-addition of the STA entry in
5386 * ieee80211_prep_connection().
5387 */
5388 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5389
Johannes Berg77fdaa12009-07-07 03:45:17 +02005390 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03005391 memcpy(&auth_data->data[auth_data->data_len],
5392 req->ie, req->ie_len);
5393 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005394 }
5395
Johannes Bergfffd0932009-07-08 14:22:54 +02005396 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005397 auth_data->key_len = req->key_len;
5398 auth_data->key_idx = req->key_idx;
5399 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02005400 }
5401
Johannes Berg66e67e42012-01-20 13:55:27 +01005402 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005403
Johannes Berg66e67e42012-01-20 13:55:27 +01005404 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01005405
Jouni Malinenefb543e2018-10-11 00:21:21 +03005406 if (ifmgd->auth_data) {
5407 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5408 auth_data->peer_confirmed =
5409 ifmgd->auth_data->peer_confirmed;
5410 }
5411 ieee80211_destroy_auth_data(sdata, cont_auth);
5412 }
Guy Eilam2a33bee2011-08-17 15:18:15 +03005413
Johannes Berg66e67e42012-01-20 13:55:27 +01005414 /* prep auth_data so we don't go into idle on disassoc */
5415 ifmgd->auth_data = auth_data;
5416
Jouni Malinenefb543e2018-10-11 00:21:21 +03005417 /* If this is continuation of an ongoing SAE authentication exchange
5418 * (i.e., request to send SAE Confirm) and the peer has already
5419 * confirmed, mark authentication completed since we are about to send
5420 * out SAE Confirm.
5421 */
5422 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5423 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5424 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5425
Johannes Berg7b119dc2013-04-10 21:38:36 +02005426 if (ifmgd->associated) {
5427 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5428
Johannes Berg89f774e2016-01-25 15:46:36 +02005429 sdata_info(sdata,
5430 "disconnect from AP %pM for new auth to %pM\n",
5431 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005432 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5433 WLAN_REASON_UNSPECIFIED,
5434 false, frame_buf);
5435
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005436 ieee80211_report_disconnect(sdata, frame_buf,
5437 sizeof(frame_buf), true,
5438 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005439 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005440
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005441 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005442
Jouni Malinenefb543e2018-10-11 00:21:21 +03005443 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Johannes Berga1cf7752012-03-08 15:02:07 +01005444 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01005445 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005446
Johannes Berg46cad4b2015-08-15 22:39:54 +03005447 err = ieee80211_auth(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01005448 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005449 sta_info_destroy_addr(sdata, req->bss->bssid);
5450 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005451 }
5452
Johannes Berg66e67e42012-01-20 13:55:27 +01005453 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01005454 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005455 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02005456
Johannes Berg66e67e42012-01-20 13:55:27 +01005457 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005458 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005459 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005460 ifmgd->auth_data = NULL;
Michal Kaziord01f8582015-06-03 08:36:13 +02005461 mutex_lock(&sdata->local->mtx);
5462 ieee80211_vif_release_channel(sdata);
5463 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01005464 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005465 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005466}
5467
Johannes Berg77fdaa12009-07-07 03:45:17 +02005468int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5469 struct cfg80211_assoc_request *req)
5470{
Johannes Berg57fa5e82020-05-28 21:34:36 +02005471 bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
Johannes Bergc2f46812020-10-13 14:01:57 +02005472 bool is_5ghz = req->bss->channel->band == NL80211_BAND_5GHZ;
Johannes Berg66e67e42012-01-20 13:55:27 +01005473 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005474 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01005475 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01005476 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005477 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005478 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005479 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005480 int i, err;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005481 bool override = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005482
Johannes Berg66e67e42012-01-20 13:55:27 +01005483 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5484 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01005485 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005486
Johannes Berg9caf0362012-11-29 01:25:20 +01005487 rcu_read_lock();
5488 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Will Deacon41525612019-10-04 10:51:31 +01005489 if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
Johannes Berg9caf0362012-11-29 01:25:20 +01005490 rcu_read_unlock();
5491 kfree(assoc_data);
5492 return -EINVAL;
5493 }
5494 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5495 assoc_data->ssid_len = ssidie[1];
5496 rcu_read_unlock();
5497
Johannes Berg7b119dc2013-04-10 21:38:36 +02005498 if (ifmgd->associated) {
5499 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5500
Johannes Berg89f774e2016-01-25 15:46:36 +02005501 sdata_info(sdata,
5502 "disconnect from AP %pM for new assoc to %pM\n",
5503 ifmgd->associated->bssid, req->bss->bssid);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005504 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5505 WLAN_REASON_UNSPECIFIED,
5506 false, frame_buf);
5507
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005508 ieee80211_report_disconnect(sdata, frame_buf,
5509 sizeof(frame_buf), true,
5510 WLAN_REASON_UNSPECIFIED);
Johannes Berg7b119dc2013-04-10 21:38:36 +02005511 }
Johannes Berg66e67e42012-01-20 13:55:27 +01005512
5513 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5514 err = -EBUSY;
5515 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03005516 }
5517
Johannes Berg66e67e42012-01-20 13:55:27 +01005518 if (ifmgd->assoc_data) {
5519 err = -EBUSY;
5520 goto err_free;
5521 }
5522
5523 if (ifmgd->auth_data) {
5524 bool match;
5525
5526 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00005527 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01005528 ieee80211_destroy_auth_data(sdata, match);
5529 }
5530
5531 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02005532
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03005533 ifmgd->beacon_crc_valid = false;
5534
Johannes Berg095d81c2013-10-15 12:25:07 +02005535 assoc_data->wmm = bss->wmm_used &&
5536 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg095d81c2013-10-15 12:25:07 +02005537
Johannes Bergde5036a2012-03-08 15:02:03 +01005538 /*
5539 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5540 * We still associate in non-HT mode (11a/b/g) if any one of these
5541 * ciphers is configured as pairwise.
5542 * We can set this to true for non-11n hardware, that'll be checked
5543 * separately along with the peer capabilities.
5544 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02005545 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02005546 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5547 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02005548 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01005549 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005550 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03005551 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Johannes Berg1c4cb922012-05-30 15:57:00 +02005552 netdev_info(sdata->dev,
Johannes Berg54626322019-08-30 14:24:46 +03005553 "disabling HT/VHT/HE due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02005554 }
5555 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005556
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005557 sband = local->hw.wiphy->bands[req->bss->channel->band];
Johannes Berg4e74bfd2012-03-08 15:02:04 +01005558
Johannes Berg75e296e2020-01-31 13:12:39 +02005559 /* also disable HT/VHT/HE if the AP doesn't use WMM */
5560 if (!bss->wmm_used) {
5561 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005562 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005563 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5564 netdev_info(sdata->dev,
5565 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00005566 }
5567
Ben Greearef96a8422011-11-18 11:32:00 -08005568 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5569 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5570 sizeof(ifmgd->ht_capa_mask));
5571
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01005572 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5573 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5574 sizeof(ifmgd->vht_capa_mask));
5575
Thomas Pedersen7957c6c2020-09-21 19:28:05 -07005576 memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
5577 memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
5578 sizeof(ifmgd->s1g_capa_mask));
5579
Johannes Berg77fdaa12009-07-07 03:45:17 +02005580 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01005581 memcpy(assoc_data->ie, req->ie, req->ie_len);
5582 assoc_data->ie_len = req->ie_len;
5583 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02005584
Jouni Malinen39404fe2016-10-27 00:42:05 +03005585 if (req->fils_kek) {
5586 /* should already be checked in cfg80211 - so warn */
5587 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5588 err = -EINVAL;
5589 goto err_free;
5590 }
5591 memcpy(assoc_data->fils_kek, req->fils_kek,
5592 req->fils_kek_len);
5593 assoc_data->fils_kek_len = req->fils_kek_len;
5594 }
5595
5596 if (req->fils_nonces)
5597 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5598 2 * FILS_NONCE_LEN);
5599
Johannes Berg66e67e42012-01-20 13:55:27 +01005600 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01005601
Johannes Bergaf6b6372009-12-23 13:15:35 +01005602 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5603 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02005604 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005605 else
Johannes Berg04ecd252012-09-11 14:34:12 +02005606 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005607 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02005608 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01005609
Johannes Berg66e67e42012-01-20 13:55:27 +01005610 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01005611 assoc_data->supp_rates = bss->supp_rates;
5612 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02005613
Johannes Berg9caf0362012-11-29 01:25:20 +01005614 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02005615 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5616 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5617 assoc_data->ap_ht_param =
5618 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
Johannes Berg57fa5e82020-05-28 21:34:36 +02005619 else if (!is_6ghz)
Johannes Berga8243b72012-11-22 14:32:09 +01005620 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01005621 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5622 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5623 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5624 sizeof(struct ieee80211_vht_cap));
Johannes Bergc2f46812020-10-13 14:01:57 +02005625 else if (is_5ghz)
Johannes Berg57fa5e82020-05-28 21:34:36 +02005626 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5627 IEEE80211_STA_DISABLE_HE;
Johannes Berg9caf0362012-11-29 01:25:20 +01005628 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01005629
Johannes Berg848955c2014-11-11 12:48:42 +01005630 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02005631 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
Johannes Berg848955c2014-11-11 12:48:42 +01005632 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5633 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5634
Kalle Valoab133152010-01-12 10:42:31 +02005635 if (bss->wmm_used && bss->uapsd_supported &&
Johannes Berg848955c2014-11-11 12:48:42 +01005636 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01005637 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02005638 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5639 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01005640 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02005641 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5642 }
5643
Johannes Berg77fdaa12009-07-07 03:45:17 +02005644 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01005645 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005646
5647 if (req->use_mfp) {
5648 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5649 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5650 } else {
5651 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5652 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5653 }
5654
Assaf Krausscd2f5dd2014-09-03 15:25:02 +03005655 if (req->flags & ASSOC_REQ_USE_RRM)
5656 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5657 else
5658 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5659
Johannes Berg77fdaa12009-07-07 03:45:17 +02005660 if (req->crypto.control_port)
5661 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5662 else
5663 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5664
Johannes Berga621fa42010-08-27 14:26:54 +03005665 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5666 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Denis Kenzior018f6fb2018-03-26 12:52:51 -05005667 sdata->control_port_over_nl80211 =
5668 req->crypto.control_port_over_nl80211;
Markus Theil7f3f96c2020-03-12 10:10:54 +01005669 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02005670 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5671 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03005672
Johannes Berg66e67e42012-01-20 13:55:27 +01005673 /* kick off associate process */
5674
5675 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02005676 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03005677 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01005678
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005679 /* override HT/VHT configuration only if the AP and we support it */
5680 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5681 struct ieee80211_sta_ht_cap sta_ht_cap;
5682
5683 if (req->flags & ASSOC_REQ_DISABLE_HT)
5684 override = true;
5685
5686 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5687 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5688
5689 /* check for 40 MHz disable override */
5690 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5691 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5692 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5693 override = true;
5694
5695 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5696 req->flags & ASSOC_REQ_DISABLE_VHT)
5697 override = true;
5698 }
5699
5700 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5701 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5702 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg75e296e2020-01-31 13:12:39 +02005703 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Chaya Rachel Ivgic1041f12015-04-20 22:51:46 +03005704 }
5705
5706 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5707 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5708
5709 err = ieee80211_prep_connection(sdata, req->bss, true, override);
Johannes Berga1cf7752012-03-08 15:02:07 +01005710 if (err)
5711 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01005712
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005713 rcu_read_lock();
5714 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02005715
Johannes Berg30686bf2015-06-02 21:39:54 +02005716 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005717 !beacon_ies) {
5718 /*
5719 * Wait up to one beacon interval ...
5720 * should this be more if we miss one?
5721 */
5722 sdata_info(sdata, "waiting for beacon from %pM\n",
5723 ifmgd->bssid);
5724 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01005725 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005726 assoc_data->need_beacon = true;
5727 } else if (beacon_ies) {
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005728 const struct element *elem;
Johannes Bergef429da2013-02-05 17:48:40 +01005729 u8 dtim_count = 0;
5730
Sara Sharon78ac51f2019-01-16 18:22:56 +02005731 ieee80211_get_dtim(beacon_ies, &dtim_count,
5732 &ifmgd->dtim_period);
5733
Alexander Bondar989c6502013-05-16 17:34:17 +03005734 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005735 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005736 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01005737
Johannes Berg30686bf2015-06-02 21:39:54 +02005738 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
Johannes Bergef429da2013-02-05 17:48:40 +01005739 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5740 sdata->vif.bss_conf.sync_device_ts =
5741 bss->device_ts_beacon;
5742 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5743 }
Sara Sharon78ac51f2019-01-16 18:22:56 +02005744
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005745 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5746 beacon_ies->data, beacon_ies->len);
5747 if (elem && elem->datalen >= 3)
5748 sdata->vif.bss_conf.profile_periodicity = elem->data[2];
Sara Sharon78ac51f2019-01-16 18:22:56 +02005749
Shaul Triebitz3b3ec3d2020-05-28 21:34:37 +02005750 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5751 beacon_ies->data, beacon_ies->len);
5752 if (elem && elem->datalen >= 11 &&
5753 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
Sara Sharon78ac51f2019-01-16 18:22:56 +02005754 sdata->vif.bss_conf.ema_ap = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005755 } else {
5756 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01005757 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01005758 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02005759 rcu_read_unlock();
5760
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005761 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01005762
Paul Stewartfcff4f12012-02-23 17:59:53 -08005763 if (bss->corrupt_data) {
5764 char *corrupt_type = "data";
5765 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5766 if (bss->corrupt_data &
5767 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5768 corrupt_type = "beacon and probe response";
5769 else
5770 corrupt_type = "beacon";
5771 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5772 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005773 sdata_info(sdata, "associating with AP with corrupt %s\n",
5774 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08005775 }
5776
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005777 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01005778 err_clear:
Joe Perchesc84a67a2015-03-02 19:54:57 -08005779 eth_zero_addr(ifmgd->bssid);
Eliad Peller3d2abdf2012-09-04 17:44:45 +03005780 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01005781 ifmgd->assoc_data = NULL;
5782 err_free:
5783 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01005784 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005785}
5786
5787int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005788 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005789{
5790 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005791 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005792 bool tx = !req->local_state_change;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005793
Johannes Bergc9c3a062014-03-19 09:11:19 +01005794 if (ifmgd->auth_data &&
5795 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5796 sdata_info(sdata,
5797 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5798 req->bssid, req->reason_code,
5799 ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005800
Ilan Peerd4e36e52018-04-20 13:49:25 +03005801 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005802 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Johannes Berg37ad3882012-02-24 13:50:54 +01005803 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02005804 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01005805 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01005806 ieee80211_destroy_auth_data(sdata, false);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005807 ieee80211_report_disconnect(sdata, frame_buf,
5808 sizeof(frame_buf), true,
5809 req->reason_code);
Johannes Berg86552012012-10-29 09:46:31 +01005810
Johannes Bergc9c3a062014-03-19 09:11:19 +01005811 return 0;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03005812 }
5813
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005814 if (ifmgd->assoc_data &&
5815 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5816 sdata_info(sdata,
5817 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5818 req->bssid, req->reason_code,
5819 ieee80211_get_reason_code_string(req->reason_code));
5820
Ilan Peerd4e36e52018-04-20 13:49:25 +03005821 drv_mgd_prepare_tx(sdata->local, sdata, 0);
Johannes Berg4b08d1b2019-08-30 14:24:51 +03005822 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005823 IEEE80211_STYPE_DEAUTH,
5824 req->reason_code, tx,
5825 frame_buf);
Johannes Berge6f462d2016-12-08 17:22:09 +01005826 ieee80211_destroy_assoc_data(sdata, false, true);
Andrei Otcheretianskia64cba32015-10-25 10:59:38 +02005827 ieee80211_report_disconnect(sdata, frame_buf,
5828 sizeof(frame_buf), true,
5829 req->reason_code);
5830 return 0;
5831 }
5832
Johannes Berg86552012012-10-29 09:46:31 +01005833 if (ifmgd->associated &&
5834 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Bergc9c3a062014-03-19 09:11:19 +01005835 sdata_info(sdata,
5836 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5837 req->bssid, req->reason_code,
5838 ieee80211_get_reason_code_string(req->reason_code));
5839
Johannes Berg86552012012-10-29 09:46:31 +01005840 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5841 req->reason_code, tx, frame_buf);
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005842 ieee80211_report_disconnect(sdata, frame_buf,
5843 sizeof(frame_buf), true,
5844 req->reason_code);
Johannes Bergc9c3a062014-03-19 09:11:19 +01005845 return 0;
5846 }
Johannes Berg86552012012-10-29 09:46:31 +01005847
Johannes Bergc9c3a062014-03-19 09:11:19 +01005848 return -ENOTCONN;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005849}
5850
5851int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01005852 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005853{
5854 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07005855 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02005856 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02005857
Johannes Berg8d8b2612009-07-25 11:58:36 +02005858 /*
5859 * cfg80211 should catch this ... but it's racy since
5860 * we can receive a disassoc frame, process it, hand it
5861 * to cfg80211 while that's in a locked section already
5862 * trying to tell us that the user wants to disconnect.
5863 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005864 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02005865 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02005866
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02005867 sdata_info(sdata,
Calvin Owensdfa1ad22014-02-11 12:36:24 -06005868 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5869 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02005870
Jouni Malinene69e95d2010-03-29 23:29:31 -07005871 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01005872 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5873 req->reason_code, !req->local_state_change,
5874 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02005875
Emmanuel Grumbacha90faa92015-03-16 23:23:37 +02005876 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5877 req->reason_code);
Johannes Bergbc83b682009-11-29 12:19:06 +01005878
Johannes Berg77fdaa12009-07-07 03:45:17 +02005879 return 0;
5880}
Jouni Malinen026331c2010-02-15 12:53:10 +02005881
Eliad Pellerafa762f2012-04-23 14:45:15 +03005882void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005883{
5884 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5885
Ben Greear49921852013-02-20 09:41:09 -08005886 /*
5887 * Make sure some work items will not run after this,
5888 * they will not do anything but might not have been
5889 * cancelled when disconnecting.
5890 */
5891 cancel_work_sync(&ifmgd->monitor_work);
5892 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5893 cancel_work_sync(&ifmgd->request_smps_work);
5894 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5895 cancel_work_sync(&ifmgd->chswitch_work);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03005896 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
Ben Greear49921852013-02-20 09:41:09 -08005897
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005898 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02005899 if (ifmgd->assoc_data) {
5900 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berge6f462d2016-12-08 17:22:09 +01005901 ieee80211_destroy_assoc_data(sdata, false, false);
Johannes Berg959867f2013-06-19 13:05:42 +02005902 cfg80211_assoc_timeout(sdata->dev, bss);
5903 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005904 if (ifmgd->auth_data)
5905 ieee80211_destroy_auth_data(sdata, false);
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005906 spin_lock_bh(&ifmgd->teardown_lock);
5907 if (ifmgd->teardown_skb) {
5908 kfree_skb(ifmgd->teardown_skb);
5909 ifmgd->teardown_skb = NULL;
5910 ifmgd->orig_teardown_skb = NULL;
5911 }
Jouni Malinen4d9ec732019-02-15 02:14:33 +02005912 kfree(ifmgd->assoc_req_ies);
5913 ifmgd->assoc_req_ies = NULL;
5914 ifmgd->assoc_req_ies_len = 0;
Liad Kaufman1277b4a2014-11-09 18:50:08 +02005915 spin_unlock_bh(&ifmgd->teardown_lock);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005916 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02005917 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01005918}
5919
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005920void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5921 enum nl80211_cqm_rssi_threshold_event rssi_event,
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005922 s32 rssi_level,
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005923 gfp_t gfp)
5924{
5925 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5926
Andrzej Zaborowski769f07d2017-01-25 12:43:40 +01005927 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
Johannes Bergb5878a22010-04-07 16:48:40 +02005928
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +01005929 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02005930}
5931EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Johannes Berg98f03342014-11-26 12:42:02 +01005932
5933void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5934{
5935 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5936
5937 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5938
5939 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5940}
5941EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);