blob: 488826f188a7a8a7f903fea8d9249ac9819b6c7c [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.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 Berg60f8b392008-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"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020034#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010035#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
36#define IEEE80211_AUTH_MAX_TRIES 3
37#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
38#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020039#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010040#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
41#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010042
Ben Greear180205b2011-02-04 15:30:24 -080043static int max_nullfunc_tries = 2;
44module_param(max_nullfunc_tries, int, 0644);
45MODULE_PARM_DESC(max_nullfunc_tries,
46 "Maximum nullfunc tx tries before disconnecting (reason 4).");
47
48static int max_probe_tries = 5;
49module_param(max_probe_tries, int, 0644);
50MODULE_PARM_DESC(max_probe_tries,
51 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020052
53/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010054 * Beacon loss timeout is calculated as N frames times the
55 * advertised beacon interval. This may need to be somewhat
56 * higher than what hardware might detect to account for
57 * delays in the host processing frames. But since we also
58 * probe on beacon miss before declaring the connection lost
59 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020060 */
Ben Greear59c1ec22013-03-19 14:19:56 -070061static int beacon_loss_count = 7;
62module_param(beacon_loss_count, int, 0644);
63MODULE_PARM_DESC(beacon_loss_count,
64 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010065
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time the connection can be idle before we probe
68 * it to see if we can still talk to the AP.
69 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030070#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020071/*
72 * Time we wait for a probe response after sending
73 * a probe request because of beacon loss or for
74 * checking the connection still works.
75 */
Ben Greear180205b2011-02-04 15:30:24 -080076static int probe_wait_ms = 500;
77module_param(probe_wait_ms, int, 0644);
78MODULE_PARM_DESC(probe_wait_ms,
79 "Maximum time(ms) to wait for probe response"
80 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070081
Jouni Malinen17e4ec12010-03-29 23:28:30 -070082/*
83 * Weight given to the latest Beacon frame when calculating average signal
84 * strength for Beacon frames received in the current BSS. This must be
85 * between 1 and 15.
86 */
87#define IEEE80211_SIGNAL_AVE_WEIGHT 3
88
Jouni Malinen391a2002010-08-27 22:22:00 +030089/*
90 * How many Beacon frames need to have been used in average signal strength
91 * before starting to indicate signal change events.
92 */
93#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
94
Johannes Berg77fdaa12009-07-07 03:45:17 +020095/*
Johannes Bergca386f32009-07-10 02:39:48 +020096 * We can have multiple work items (and connection probing)
97 * scheduling this timer, but we need to take care to only
98 * reschedule it when it should fire _earlier_ than it was
99 * asked for before, or if it's not pending right now. This
100 * function ensures that. Note that it then is required to
101 * run this function for all timeouts after the first one
102 * has happened -- the work that runs from this timer will
103 * do that.
104 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200105static void run_again(struct ieee80211_sub_if_data *sdata,
106 unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200107{
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200108 sdata_assert_lock(sdata);
Johannes Bergca386f32009-07-10 02:39:48 +0200109
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200110 if (!timer_pending(&sdata->u.mgd.timer) ||
111 time_before(timeout, sdata->u.mgd.timer.expires))
112 mod_timer(&sdata->u.mgd.timer, timeout);
Johannes Bergca386f32009-07-10 02:39:48 +0200113}
114
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400115void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200116{
Johannes Bergc1288b12012-01-19 09:29:57 +0100117 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200118 return;
119
Johannes Berg7eeff742012-07-18 10:27:27 +0200120 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
121 return;
122
Johannes Bergb291ba12009-07-10 15:29:03 +0200123 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100124 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200125}
126
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400127void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
128{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
130
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100131 if (unlikely(!sdata->u.mgd.associated))
132 return;
133
Eliad Peller448cd2e2014-02-11 12:30:18 +0200134 ifmgd->probe_send_count = 0;
135
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400136 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
137 return;
138
139 mod_timer(&sdata->u.mgd.conn_mon_timer,
140 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
141}
142
Johannes Berg5484e232008-09-08 17:44:27 +0200143static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200144{
Johannes Berg5484e232008-09-08 17:44:27 +0200145 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200146}
147
Johannes Berg6565ec92013-02-08 14:52:32 +0100148static u32
149ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
150 struct ieee80211_supported_band *sband,
151 struct ieee80211_channel *channel,
152 const struct ieee80211_ht_operation *ht_oper,
153 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200154 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100155{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100157 struct cfg80211_chan_def vht_chandef;
158 u32 ht_cfreq, ret;
159
160 chandef->chan = channel;
161 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
162 chandef->center_freq1 = channel->center_freq;
163 chandef->center_freq2 = 0;
164
165 if (!ht_oper || !sband->ht_cap.ht_supported) {
166 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
167 goto out;
168 }
169
170 chandef->width = NL80211_CHAN_WIDTH_20;
171
172 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
173 channel->band);
174 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200175 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100176 /*
177 * It's possible that some APs are confused here;
178 * Netgear WNDR3700 sometimes reports 4 higher than
179 * the actual channel in association responses, but
180 * since we look at probe response/beacon data here
181 * it should be OK.
182 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200183 sdata_info(sdata,
184 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
185 channel->center_freq, ht_cfreq,
186 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100187 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
188 goto out;
189 }
190
191 /* check 40 MHz support, if we have it */
192 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
193 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
194 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
195 chandef->width = NL80211_CHAN_WIDTH_40;
196 chandef->center_freq1 += 10;
197 break;
198 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
199 chandef->width = NL80211_CHAN_WIDTH_40;
200 chandef->center_freq1 -= 10;
201 break;
202 }
203 } else {
204 /* 40 MHz (and 80 MHz) must be supported for VHT */
205 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100206 /* also mark 40 MHz disabled */
207 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100208 goto out;
209 }
210
211 if (!vht_oper || !sband->vht_cap.vht_supported) {
212 ret = IEEE80211_STA_DISABLE_VHT;
213 goto out;
214 }
215
216 vht_chandef.chan = channel;
217 vht_chandef.center_freq1 =
218 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
219 channel->band);
220 vht_chandef.center_freq2 = 0;
221
Johannes Berg6565ec92013-02-08 14:52:32 +0100222 switch (vht_oper->chan_width) {
223 case IEEE80211_VHT_CHANWIDTH_USE_HT:
224 vht_chandef.width = chandef->width;
Johannes Bergcb664982014-02-27 20:47:53 +0100225 vht_chandef.center_freq1 = chandef->center_freq1;
Johannes Berg6565ec92013-02-08 14:52:32 +0100226 break;
227 case IEEE80211_VHT_CHANWIDTH_80MHZ:
228 vht_chandef.width = NL80211_CHAN_WIDTH_80;
229 break;
230 case IEEE80211_VHT_CHANWIDTH_160MHZ:
231 vht_chandef.width = NL80211_CHAN_WIDTH_160;
232 break;
233 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
234 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
Johannes Berg6553bf042013-03-28 10:26:17 +0100235 vht_chandef.center_freq2 =
236 ieee80211_channel_to_frequency(
237 vht_oper->center_freq_seg2_idx,
238 channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100239 break;
240 default:
Johannes Berg5cdaed12013-07-31 11:23:06 +0200241 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100242 sdata_info(sdata,
243 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
244 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100245 ret = IEEE80211_STA_DISABLE_VHT;
246 goto out;
247 }
248
249 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200250 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100251 sdata_info(sdata,
252 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100253 ret = IEEE80211_STA_DISABLE_VHT;
254 goto out;
255 }
256
257 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
258 ret = 0;
259 goto out;
260 }
261
262 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200263 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100264 sdata_info(sdata,
265 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100266 ret = IEEE80211_STA_DISABLE_VHT;
267 goto out;
268 }
269
270 *chandef = vht_chandef;
271
272 ret = 0;
273
274out:
Johannes Berg963a18522014-02-21 20:34:34 +0100275 /*
276 * When tracking the current AP, don't do any further checks if the
277 * new chandef is identical to the one we're currently using for the
278 * connection. This keeps us from playing ping-pong with regulatory,
279 * without it the following can happen (for example):
280 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
281 * - AP advertises regdom US
282 * - CRDA loads regdom US with 80 MHz prohibited (old database)
283 * - the code below detects an unsupported channel, downgrades, and
284 * we disconnect from the AP in the caller
285 * - disconnect causes CRDA to reload world regdomain and the game
286 * starts anew.
287 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
288 *
289 * It seems possible that there are still scenarios with CSA or real
290 * bandwidth changes where a this could happen, but those cases are
291 * less common and wouldn't completely prevent using the AP.
292 */
293 if (tracking &&
294 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
295 return ret;
296
Johannes Berg586e01e2013-02-14 12:13:53 +0100297 /* don't print the message below for VHT mismatch if VHT is disabled */
298 if (ret & IEEE80211_STA_DISABLE_VHT)
299 vht_chandef = *chandef;
300
Johannes Bergddfe49b2013-07-31 20:52:03 +0200301 /*
302 * Ignore the DISABLED flag when we're already connected and only
303 * tracking the APs beacon for bandwidth changes - otherwise we
304 * might get disconnected here if we connect to an AP, update our
305 * regulatory information based on the AP's country IE and the
306 * information we have is wrong/outdated and disables the channel
307 * that we're actually using for the connection to the AP.
308 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100309 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200310 tracking ? 0 :
311 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100312 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
313 ret = IEEE80211_STA_DISABLE_HT |
314 IEEE80211_STA_DISABLE_VHT;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700315 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100316 }
317
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200318 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100319 }
320
Johannes Berg5cdaed12013-07-31 11:23:06 +0200321 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100322 sdata_info(sdata,
323 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
324
325 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
326 return ret;
327}
328
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100329static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
330 struct sta_info *sta,
331 const struct ieee80211_ht_operation *ht_oper,
332 const struct ieee80211_vht_operation *vht_oper,
333 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200334{
335 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100336 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200337 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200338 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100339 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200340 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100341 u32 flags;
342 enum ieee80211_sta_rx_bandwidth new_sta_bw;
343 int ret;
344
345 /* if HT was/is disabled, don't track any bandwidth changes */
346 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
347 return 0;
348
349 /* don't check VHT if we associated as non-VHT station */
350 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
351 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200352
Johannes Berg11289582013-02-07 17:36:12 +0100353 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100354 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100355
Avri Altman017b45b2013-11-18 19:06:48 +0200356 /*
357 * if bss configuration changed store the new one -
358 * this may be applicable even if channel is identical
359 */
360 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
361 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
362 *changed |= BSS_CHANGED_HT;
363 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
364 }
365
Johannes Bergf2d93302013-02-08 13:15:55 +0100366 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200367 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200368
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100369 /* calculate new channel (type) based on HT/VHT operation IEs */
370 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200371 vht_oper, &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100372
373 /*
374 * Downgrade the new channel if we associated with restricted
375 * capabilities. For example, if we associated as a 20 MHz STA
376 * to a 40 MHz AP (due to regulatory, capabilities or config
377 * reasons) then switching to a 40 MHz channel now won't do us
378 * any good -- we couldn't use it with the AP.
379 */
380 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
381 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200382 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100383 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
384 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200385 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100386 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
387 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200388 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100389
390 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
391 return 0;
392
393 sdata_info(sdata,
394 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
395 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
396 chandef.center_freq1, chandef.center_freq2);
397
398 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
399 IEEE80211_STA_DISABLE_VHT |
400 IEEE80211_STA_DISABLE_40MHZ |
401 IEEE80211_STA_DISABLE_80P80MHZ |
402 IEEE80211_STA_DISABLE_160MHZ)) ||
403 !cfg80211_chandef_valid(&chandef)) {
404 sdata_info(sdata,
405 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
406 ifmgd->bssid);
407 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200408 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200409
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100410 switch (chandef.width) {
411 case NL80211_CHAN_WIDTH_20_NOHT:
412 case NL80211_CHAN_WIDTH_20:
413 new_sta_bw = IEEE80211_STA_RX_BW_20;
414 break;
415 case NL80211_CHAN_WIDTH_40:
416 new_sta_bw = IEEE80211_STA_RX_BW_40;
417 break;
418 case NL80211_CHAN_WIDTH_80:
419 new_sta_bw = IEEE80211_STA_RX_BW_80;
420 break;
421 case NL80211_CHAN_WIDTH_80P80:
422 case NL80211_CHAN_WIDTH_160:
423 new_sta_bw = IEEE80211_STA_RX_BW_160;
424 break;
425 default:
426 return -EINVAL;
427 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200428
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100429 if (new_sta_bw > sta->cur_max_bandwidth)
430 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200431
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100432 if (new_sta_bw < sta->sta.bandwidth) {
433 sta->sta.bandwidth = new_sta_bw;
434 rate_control_rate_update(local, sband, sta,
435 IEEE80211_RC_BW_CHANGED);
436 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530437
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100438 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
439 if (ret) {
440 sdata_info(sdata,
441 "AP %pM changed bandwidth to incompatible one - disconnect\n",
442 ifmgd->bssid);
443 return ret;
444 }
445
446 if (new_sta_bw > sta->sta.bandwidth) {
447 sta->sta.bandwidth = new_sta_bw;
448 rate_control_rate_update(local, sband, sta,
449 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200450 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200451
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100452 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200453}
454
Johannes Berg9c6bd792008-09-11 00:01:52 +0200455/* frame sending functions */
456
Johannes Berg66e67e42012-01-20 13:55:27 +0100457static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200458 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100459 struct ieee80211_supported_band *sband,
460 struct ieee80211_channel *channel,
461 enum ieee80211_smps_mode smps)
462{
Johannes Berg66e67e42012-01-20 13:55:27 +0100463 u8 *pos;
464 u32 flags = channel->flags;
465 u16 cap;
466 struct ieee80211_sta_ht_cap ht_cap;
467
468 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
469
Johannes Berg66e67e42012-01-20 13:55:27 +0100470 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
471 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
472
Johannes Berg66e67e42012-01-20 13:55:27 +0100473 /* determine capability flags */
474 cap = ht_cap.cap;
475
Johannes Berg9dde6422012-05-16 23:43:19 +0200476 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100477 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
478 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
479 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
480 cap &= ~IEEE80211_HT_CAP_SGI_40;
481 }
482 break;
483 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
484 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
485 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
486 cap &= ~IEEE80211_HT_CAP_SGI_40;
487 }
488 break;
489 }
490
Johannes Berg24398e32012-03-28 10:58:36 +0200491 /*
492 * If 40 MHz was disabled associate as though we weren't
493 * capable of 40 MHz -- some broken APs will never fall
494 * back to trying to transmit in 20 MHz.
495 */
496 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
497 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
498 cap &= ~IEEE80211_HT_CAP_SGI_40;
499 }
500
Johannes Berg66e67e42012-01-20 13:55:27 +0100501 /* set SM PS mode properly */
502 cap &= ~IEEE80211_HT_CAP_SM_PS;
503 switch (smps) {
504 case IEEE80211_SMPS_AUTOMATIC:
505 case IEEE80211_SMPS_NUM_MODES:
506 WARN_ON(1);
507 case IEEE80211_SMPS_OFF:
508 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
509 IEEE80211_HT_CAP_SM_PS_SHIFT;
510 break;
511 case IEEE80211_SMPS_STATIC:
512 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
513 IEEE80211_HT_CAP_SM_PS_SHIFT;
514 break;
515 case IEEE80211_SMPS_DYNAMIC:
516 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
517 IEEE80211_HT_CAP_SM_PS_SHIFT;
518 break;
519 }
520
521 /* reserve and fill IE */
522 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
523 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
524}
525
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000526static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
527 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100528 struct ieee80211_supported_band *sband,
529 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000530{
531 u8 *pos;
532 u32 cap;
533 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200534 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000535
536 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
537
538 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100539 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000540
541 /* determine capability flags */
542 cap = vht_cap.cap;
543
Johannes Bergf2d9d272012-11-22 14:11:39 +0100544 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
545 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
546 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
547 }
548
549 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
550 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
551 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
552 }
553
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100554 /*
555 * Some APs apparently get confused if our capabilities are better
556 * than theirs, so restrict what we advertise in the assoc request.
557 */
558 if (!(ap_vht_cap->vht_cap_info &
559 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
560 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
561
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200562 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
563
564 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
565 our_bf_sts = cap & mask;
566
567 if (ap_bf_sts < our_bf_sts) {
568 cap &= ~mask;
569 cap |= ap_bf_sts;
570 }
571
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000572 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000573 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000574 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
575}
576
Johannes Berg66e67e42012-01-20 13:55:27 +0100577static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
578{
579 struct ieee80211_local *local = sdata->local;
580 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
581 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
582 struct sk_buff *skb;
583 struct ieee80211_mgmt *mgmt;
584 u8 *pos, qos_info;
585 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200586 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100587 u16 capab;
588 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200589 struct ieee80211_chanctx_conf *chanctx_conf;
590 struct ieee80211_channel *chan;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200591 u32 rate_flags, rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100592
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200593 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100594
Johannes Berg55de9082012-07-26 17:24:39 +0200595 rcu_read_lock();
596 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
597 if (WARN_ON(!chanctx_conf)) {
598 rcu_read_unlock();
599 return;
600 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100601 chan = chanctx_conf->def.chan;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200602 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200603 rcu_read_unlock();
604 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200605 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100606
607 if (assoc_data->supp_rates_len) {
608 /*
609 * Get all rates supported by the device and the AP as
610 * some APs don't like getting a superset of their rates
611 * in the association request (e.g. D-Link DAP 1353 in
612 * b-only mode)...
613 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200614 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
615 assoc_data->supp_rates,
616 assoc_data->supp_rates_len,
617 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100618 } else {
619 /*
620 * In case AP not provide any supported rates information
621 * before association, we send information element(s) with
622 * all rates that we support.
623 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200624 rates_len = 0;
625 for (i = 0; i < sband->n_bitrates; i++) {
626 if ((rate_flags & sband->bitrates[i].flags)
627 != rate_flags)
628 continue;
629 rates |= BIT(i);
630 rates_len++;
631 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100632 }
633
634 skb = alloc_skb(local->hw.extra_tx_headroom +
635 sizeof(*mgmt) + /* bit too much but doesn't matter */
636 2 + assoc_data->ssid_len + /* SSID */
637 4 + rates_len + /* (extended) rates */
638 4 + /* power capability */
639 2 + 2 * sband->n_channels + /* supported channels */
640 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000641 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100642 assoc_data->ie_len + /* extra IEs */
643 9, /* WMM */
644 GFP_KERNEL);
645 if (!skb)
646 return;
647
648 skb_reserve(skb, local->hw.extra_tx_headroom);
649
650 capab = WLAN_CAPABILITY_ESS;
651
652 if (sband->band == IEEE80211_BAND_2GHZ) {
653 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
654 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
655 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
656 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
657 }
658
659 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
660 capab |= WLAN_CAPABILITY_PRIVACY;
661
662 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
663 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
664 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
665
666 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
667 memset(mgmt, 0, 24);
668 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
669 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
670 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
671
672 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
673 skb_put(skb, 10);
674 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
675 IEEE80211_STYPE_REASSOC_REQ);
676 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
677 mgmt->u.reassoc_req.listen_interval =
678 cpu_to_le16(local->hw.conf.listen_interval);
679 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
680 ETH_ALEN);
681 } else {
682 skb_put(skb, 4);
683 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
684 IEEE80211_STYPE_ASSOC_REQ);
685 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
686 mgmt->u.assoc_req.listen_interval =
687 cpu_to_le16(local->hw.conf.listen_interval);
688 }
689
690 /* SSID */
691 pos = skb_put(skb, 2 + assoc_data->ssid_len);
692 *pos++ = WLAN_EID_SSID;
693 *pos++ = assoc_data->ssid_len;
694 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
695
696 /* add all rates which were marked to be used above */
697 supp_rates_len = rates_len;
698 if (supp_rates_len > 8)
699 supp_rates_len = 8;
700
701 pos = skb_put(skb, supp_rates_len + 2);
702 *pos++ = WLAN_EID_SUPP_RATES;
703 *pos++ = supp_rates_len;
704
705 count = 0;
706 for (i = 0; i < sband->n_bitrates; i++) {
707 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200708 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
709 5 * (1 << shift));
710 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100711 if (++count == 8)
712 break;
713 }
714 }
715
716 if (rates_len > count) {
717 pos = skb_put(skb, rates_len - count + 2);
718 *pos++ = WLAN_EID_EXT_SUPP_RATES;
719 *pos++ = rates_len - count;
720
721 for (i++; i < sband->n_bitrates; i++) {
722 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200723 int rate;
724 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
725 5 * (1 << shift));
726 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100727 }
728 }
729 }
730
731 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
732 /* 1. power capabilities */
733 pos = skb_put(skb, 4);
734 *pos++ = WLAN_EID_PWR_CAPABILITY;
735 *pos++ = 2;
736 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200737 /* max tx power */
738 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Johannes Berg66e67e42012-01-20 13:55:27 +0100739
740 /* 2. supported channels */
741 /* TODO: get this in reg domain format */
742 pos = skb_put(skb, 2 * sband->n_channels + 2);
743 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
744 *pos++ = 2 * sband->n_channels;
745 for (i = 0; i < sband->n_channels; i++) {
746 *pos++ = ieee80211_frequency_to_channel(
747 sband->channels[i].center_freq);
748 *pos++ = 1; /* one channel in the subband*/
749 }
750 }
751
752 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200753 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100754 static const u8 before_ht[] = {
755 WLAN_EID_SSID,
756 WLAN_EID_SUPP_RATES,
757 WLAN_EID_EXT_SUPP_RATES,
758 WLAN_EID_PWR_CAPABILITY,
759 WLAN_EID_SUPPORTED_CHANNELS,
760 WLAN_EID_RSN,
761 WLAN_EID_QOS_CAPA,
762 WLAN_EID_RRM_ENABLED_CAPABILITIES,
763 WLAN_EID_MOBILITY_DOMAIN,
764 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
765 };
766 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
767 before_ht, ARRAY_SIZE(before_ht),
768 offset);
769 pos = skb_put(skb, noffset - offset);
770 memcpy(pos, assoc_data->ie + offset, noffset - offset);
771 offset = noffset;
772 }
773
Johannes Bergf2d9d272012-11-22 14:11:39 +0100774 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
775 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
776 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
777
Johannes Berga8243b72012-11-22 14:32:09 +0100778 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200779 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200780 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100781
Johannes Berg3de38022014-02-04 09:54:07 +0100782 /* if present, add any custom IEs that go before VHT */
783 if (assoc_data->ie_len) {
784 static const u8 before_vht[] = {
785 WLAN_EID_SSID,
786 WLAN_EID_SUPP_RATES,
787 WLAN_EID_EXT_SUPP_RATES,
788 WLAN_EID_PWR_CAPABILITY,
789 WLAN_EID_SUPPORTED_CHANNELS,
790 WLAN_EID_RSN,
791 WLAN_EID_QOS_CAPA,
792 WLAN_EID_RRM_ENABLED_CAPABILITIES,
793 WLAN_EID_MOBILITY_DOMAIN,
794 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
795 WLAN_EID_HT_CAPABILITY,
796 WLAN_EID_BSS_COEX_2040,
797 WLAN_EID_EXT_CAPABILITY,
798 WLAN_EID_QOS_TRAFFIC_CAPA,
799 WLAN_EID_TIM_BCAST_REQ,
800 WLAN_EID_INTERWORKING,
801 };
802 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
803 before_vht, ARRAY_SIZE(before_vht),
804 offset);
805 pos = skb_put(skb, noffset - offset);
806 memcpy(pos, assoc_data->ie + offset, noffset - offset);
807 offset = noffset;
808 }
809
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000810 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100811 ieee80211_add_vht_ie(sdata, skb, sband,
812 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000813
Johannes Berg66e67e42012-01-20 13:55:27 +0100814 /* if present, add any custom non-vendor IEs that go after HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200815 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100816 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
817 assoc_data->ie_len,
818 offset);
819 pos = skb_put(skb, noffset - offset);
820 memcpy(pos, assoc_data->ie + offset, noffset - offset);
821 offset = noffset;
822 }
823
Johannes Berg76f03032012-03-08 15:02:05 +0100824 if (assoc_data->wmm) {
825 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200826 qos_info = ifmgd->uapsd_queues;
827 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100828 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
829 } else {
830 qos_info = 0;
831 }
832
833 pos = skb_put(skb, 9);
834 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
835 *pos++ = 7; /* len */
836 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
837 *pos++ = 0x50;
838 *pos++ = 0xf2;
839 *pos++ = 2; /* WME */
840 *pos++ = 0; /* WME info */
841 *pos++ = 1; /* WME ver */
842 *pos++ = qos_info;
843 }
844
845 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200846 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100847 noffset = assoc_data->ie_len;
848 pos = skb_put(skb, noffset - offset);
849 memcpy(pos, assoc_data->ie + offset, noffset - offset);
850 }
851
Johannes Berga1845fc2012-06-27 13:18:36 +0200852 drv_mgd_prepare_tx(local, sdata);
853
Johannes Berg66e67e42012-01-20 13:55:27 +0100854 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100855 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
856 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
857 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100858 ieee80211_tx_skb(sdata, skb);
859}
860
Kalle Valo572e0012009-02-10 17:09:31 +0200861void ieee80211_send_pspoll(struct ieee80211_local *local,
862 struct ieee80211_sub_if_data *sdata)
863{
Kalle Valo572e0012009-02-10 17:09:31 +0200864 struct ieee80211_pspoll *pspoll;
865 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200866
Kalle Valod8cd1892010-01-05 20:16:26 +0200867 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
868 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200869 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200870
Kalle Valod8cd1892010-01-05 20:16:26 +0200871 pspoll = (struct ieee80211_pspoll *) skb->data;
872 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200873
Johannes Berg62ae67b2009-11-18 18:42:05 +0100874 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
875 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200876}
877
Johannes Berg965beda2009-04-16 13:17:24 +0200878void ieee80211_send_nullfunc(struct ieee80211_local *local,
879 struct ieee80211_sub_if_data *sdata,
880 int powersave)
881{
882 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200883 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530884 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200885
Kalle Valod8cd1892010-01-05 20:16:26 +0200886 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
887 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200888 return;
889
Kalle Valod8cd1892010-01-05 20:16:26 +0200890 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200891 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200892 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200893
Seth Forshee6c17b772013-02-11 11:21:07 -0600894 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
895 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +0200896
897 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
898 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
899
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +0200900 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530901 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
902
Johannes Berg62ae67b2009-11-18 18:42:05 +0100903 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200904}
905
Felix Fietkaud5242152010-01-08 18:06:26 +0100906static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
907 struct ieee80211_sub_if_data *sdata)
908{
909 struct sk_buff *skb;
910 struct ieee80211_hdr *nullfunc;
911 __le16 fc;
912
913 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
914 return;
915
916 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700917 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100918 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700919
Felix Fietkaud5242152010-01-08 18:06:26 +0100920 skb_reserve(skb, local->hw.extra_tx_headroom);
921
922 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
923 memset(nullfunc, 0, 30);
924 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
925 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
926 nullfunc->frame_control = fc;
927 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
928 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
929 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
930 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
931
932 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
933 ieee80211_tx_skb(sdata, skb);
934}
935
Johannes Bergcc32abd2009-05-15 11:52:31 +0200936/* spectrum management related things */
937static void ieee80211_chswitch_work(struct work_struct *work)
938{
939 struct ieee80211_sub_if_data *sdata =
940 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100941 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200942 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +0300943 u32 changed = 0;
944 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200945
Johannes Berg9607e6b2009-12-23 13:15:31 +0100946 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200947 return;
948
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200949 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200950 if (!ifmgd->associated)
951 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200952
Johannes Berg34a37402013-12-18 09:43:33 +0100953 mutex_lock(&local->mtx);
Luciano Coelho33787fc2013-11-11 20:34:54 +0200954 ret = ieee80211_vif_change_channel(sdata, &changed);
Johannes Berg34a37402013-12-18 09:43:33 +0100955 mutex_unlock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +0300956 if (ret) {
957 sdata_info(sdata,
958 "vif channel switch failed, disconnecting\n");
959 ieee80211_queue_work(&sdata->local->hw,
960 &ifmgd->csa_connection_drop_work);
961 goto out;
962 }
Karl Beldan675a0b02013-03-25 16:26:57 +0100963
Arik Nemtsov7578d572013-09-01 17:15:51 +0300964 if (!local->use_chanctx) {
Luciano Coelho33787fc2013-11-11 20:34:54 +0200965 local->_oper_chandef = sdata->csa_chandef;
Arik Nemtsov7578d572013-09-01 17:15:51 +0300966 /* Call "hw_config" only if doing sw channel switch.
967 * Otherwise update the channel directly
968 */
969 if (!local->ops->channel_switch)
970 ieee80211_hw_config(local, 0);
971 else
972 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200973 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200974
Johannes Berg77fdaa12009-07-07 03:45:17 +0200975 /* XXX: shouldn't really modify cfg80211-owned data! */
Luciano Coelho33787fc2013-11-11 20:34:54 +0200976 ifmgd->associated->channel = sdata->csa_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200977
Johannes Berg57eebdf2012-08-01 15:50:46 +0200978 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +0100979 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100980 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200981 IEEE80211_QUEUE_STOP_REASON_CSA);
Arik Nemtsov7578d572013-09-01 17:15:51 +0300982
983 ieee80211_bss_info_change_notify(sdata, changed);
984
Johannes Berg77fdaa12009-07-07 03:45:17 +0200985 out:
Arik Nemtsov7578d572013-09-01 17:15:51 +0300986 sdata->vif.csa_active = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200987 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200988 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200989}
990
Johannes Berg5ce6e432010-05-11 16:20:57 +0200991void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
992{
Johannes Berg55de9082012-07-26 17:24:39 +0200993 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
994 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200995
996 trace_api_chswitch_done(sdata, success);
997 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200998 sdata_info(sdata,
999 "driver channel switch failed, disconnecting\n");
1000 ieee80211_queue_work(&sdata->local->hw,
1001 &ifmgd->csa_connection_drop_work);
1002 } else {
1003 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001004 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001005}
1006EXPORT_SYMBOL(ieee80211_chswitch_done);
1007
Johannes Bergcc32abd2009-05-15 11:52:31 +02001008static void ieee80211_chswitch_timer(unsigned long data)
1009{
1010 struct ieee80211_sub_if_data *sdata =
1011 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001012
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001013 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001014}
1015
Johannes Berg37799e52013-03-26 14:02:26 +01001016static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001017ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Johannes Berg04a161f2013-05-03 09:35:35 +02001018 u64 timestamp, struct ieee802_11_elems *elems,
1019 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001020{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001021 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001022 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001023 struct cfg80211_bss *cbss = ifmgd->associated;
Johannes Berg55de9082012-07-26 17:24:39 +02001024 struct ieee80211_chanctx *chanctx;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001025 enum ieee80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001026 struct ieee80211_csa_ie csa_ie;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001027 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001028
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001029 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001030
Johannes Berg37799e52013-03-26 14:02:26 +01001031 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001032 return;
1033
Johannes Bergb4f286a12013-03-26 14:13:58 +01001034 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001035 return;
1036
Johannes Berg37799e52013-03-26 14:02:26 +01001037 /* disregard subsequent announcements if we are already processing */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001038 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1039 return;
1040
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001041 current_band = cbss->channel->band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001042 memset(&csa_ie, 0, sizeof(csa_ie));
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001043 res = ieee80211_parse_ch_switch_ie(sdata, elems, beacon, current_band,
1044 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001045 ifmgd->associated->bssid, &csa_ie);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001046 if (res < 0)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001047 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001048 &ifmgd->csa_connection_drop_work);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001049 if (res)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001050 return;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001051
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001052 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001053 IEEE80211_CHAN_DISABLED)) {
1054 sdata_info(sdata,
1055 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001056 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001057 csa_ie.chandef.chan->center_freq,
1058 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1059 csa_ie.chandef.center_freq2);
Johannes Berg85220d72013-03-25 18:29:27 +01001060 ieee80211_queue_work(&local->hw,
1061 &ifmgd->csa_connection_drop_work);
1062 return;
1063 }
1064
Johannes Berg57eebdf2012-08-01 15:50:46 +02001065 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
Johannes Berg55de9082012-07-26 17:24:39 +02001066
Johannes Bergb4f286a12013-03-26 14:13:58 +01001067 mutex_lock(&local->chanctx_mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03001068 if (local->use_chanctx) {
1069 u32 num_chanctx = 0;
1070 list_for_each_entry(chanctx, &local->chanctx_list, list)
1071 num_chanctx++;
1072
1073 if (num_chanctx > 1 ||
1074 !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) {
1075 sdata_info(sdata,
1076 "not handling chan-switch with channel contexts\n");
1077 ieee80211_queue_work(&local->hw,
1078 &ifmgd->csa_connection_drop_work);
1079 mutex_unlock(&local->chanctx_mtx);
1080 return;
1081 }
1082 }
1083
Johannes Berg55de9082012-07-26 17:24:39 +02001084 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001085 ieee80211_queue_work(&local->hw,
1086 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001087 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001088 return;
1089 }
1090 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1091 struct ieee80211_chanctx, conf);
Michal Kaziorc0166da2014-04-09 15:29:33 +02001092 if (ieee80211_chanctx_refcount(local, chanctx) > 1) {
Johannes Berg55de9082012-07-26 17:24:39 +02001093 sdata_info(sdata,
1094 "channel switch with multiple interfaces on the same channel, disconnecting\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001095 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001096 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001097 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001098 return;
1099 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001100 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001101
Luciano Coelho33787fc2013-11-11 20:34:54 +02001102 sdata->csa_chandef = csa_ie.chandef;
Michal Kaziorc46a73f2014-01-29 07:56:19 +01001103 sdata->vif.csa_active = true;
Johannes Berg55de9082012-07-26 17:24:39 +02001104
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001105 if (csa_ie.mode)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001106 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001107 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001108 IEEE80211_QUEUE_STOP_REASON_CSA);
1109
Johannes Bergb4f286a12013-03-26 14:13:58 +01001110 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001111 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001112 struct ieee80211_channel_switch ch_switch = {
1113 .timestamp = timestamp,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001114 .block_tx = csa_ie.mode,
1115 .chandef = csa_ie.chandef,
1116 .count = csa_ie.count,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001117 };
1118
Johannes Bergb4f286a12013-03-26 14:13:58 +01001119 drv_channel_switch(local, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001120 return;
1121 }
1122
1123 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001124 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001125 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001126 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001127 mod_timer(&ifmgd->chswitch_timer,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001128 TU_TO_EXP_TIME(csa_ie.count * cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001129}
1130
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001131static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1132 struct ieee80211_channel *channel,
1133 const u8 *country_ie, u8 country_ie_len,
1134 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001135{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001136 struct ieee80211_country_ie_triplet *triplet;
1137 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1138 int i, chan_pwr, chan_increment, new_ap_level;
1139 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001140
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001141 /* Invalid IE */
1142 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001143 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001144
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001145 triplet = (void *)(country_ie + 3);
1146 country_ie_len -= 3;
1147
1148 switch (channel->band) {
1149 default:
1150 WARN_ON_ONCE(1);
1151 /* fall through */
1152 case IEEE80211_BAND_2GHZ:
1153 case IEEE80211_BAND_60GHZ:
1154 chan_increment = 1;
1155 break;
1156 case IEEE80211_BAND_5GHZ:
1157 chan_increment = 4;
1158 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001159 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001160
1161 /* find channel */
1162 while (country_ie_len >= 3) {
1163 u8 first_channel = triplet->chans.first_channel;
1164
1165 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1166 goto next;
1167
1168 for (i = 0; i < triplet->chans.num_channels; i++) {
1169 if (first_channel + i * chan_increment == chan) {
1170 have_chan_pwr = true;
1171 chan_pwr = triplet->chans.max_power;
1172 break;
1173 }
1174 }
1175 if (have_chan_pwr)
1176 break;
1177
1178 next:
1179 triplet++;
1180 country_ie_len -= 3;
1181 }
1182
1183 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001184 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001185
1186 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1187
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001188 if (sdata->ap_power_level == new_ap_level)
1189 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001190
1191 sdata_info(sdata,
1192 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1193 new_ap_level, chan_pwr, *pwr_constr_elem,
1194 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001195 sdata->ap_power_level = new_ap_level;
1196 if (__ieee80211_recalc_txpower(sdata))
1197 return BSS_CHANGED_TXPOWER;
1198 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001199}
1200
Johannes Berg965beda2009-04-16 13:17:24 +02001201/* powersave */
1202static void ieee80211_enable_ps(struct ieee80211_local *local,
1203 struct ieee80211_sub_if_data *sdata)
1204{
1205 struct ieee80211_conf *conf = &local->hw.conf;
1206
Johannes Bergd5edaed2009-04-22 23:02:51 +02001207 /*
1208 * If we are scanning right now then the parameters will
1209 * take effect when scan finishes.
1210 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001211 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001212 return;
1213
Johannes Berg965beda2009-04-16 13:17:24 +02001214 if (conf->dynamic_ps_timeout > 0 &&
1215 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1216 mod_timer(&local->dynamic_ps_timer, jiffies +
1217 msecs_to_jiffies(conf->dynamic_ps_timeout));
1218 } else {
1219 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1220 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301221
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001222 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1223 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1224 return;
1225
1226 conf->flags |= IEEE80211_CONF_PS;
1227 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001228 }
1229}
1230
1231static void ieee80211_change_ps(struct ieee80211_local *local)
1232{
1233 struct ieee80211_conf *conf = &local->hw.conf;
1234
1235 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001236 ieee80211_enable_ps(local, local->ps_sdata);
1237 } else if (conf->flags & IEEE80211_CONF_PS) {
1238 conf->flags &= ~IEEE80211_CONF_PS;
1239 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1240 del_timer_sync(&local->dynamic_ps_timer);
1241 cancel_work_sync(&local->dynamic_ps_enable_work);
1242 }
1243}
1244
Jason Young808118c2011-03-10 16:43:19 -08001245static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1246{
1247 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1248 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001249 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001250
1251 if (!mgd->powersave)
1252 return false;
1253
Johannes Berg05cb9102011-10-28 11:59:47 +02001254 if (mgd->broken_ap)
1255 return false;
1256
Jason Young808118c2011-03-10 16:43:19 -08001257 if (!mgd->associated)
1258 return false;
1259
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001260 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001261 return false;
1262
Alexander Bondar989c6502013-05-16 17:34:17 +03001263 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001264 return false;
1265
Jason Young808118c2011-03-10 16:43:19 -08001266 rcu_read_lock();
1267 sta = sta_info_get(sdata, mgd->bssid);
1268 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001269 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001270 rcu_read_unlock();
1271
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001272 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001273}
1274
Johannes Berg965beda2009-04-16 13:17:24 +02001275/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001276void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001277{
1278 struct ieee80211_sub_if_data *sdata, *found = NULL;
1279 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001280 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001281
1282 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1283 local->ps_sdata = NULL;
1284 return;
1285 }
1286
1287 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001288 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001289 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301290 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1291 /* If an AP vif is found, then disable PS
1292 * by setting the count to zero thereby setting
1293 * ps_sdata to NULL.
1294 */
1295 count = 0;
1296 break;
1297 }
Johannes Berg965beda2009-04-16 13:17:24 +02001298 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1299 continue;
1300 found = sdata;
1301 count++;
1302 }
1303
Jason Young808118c2011-03-10 16:43:19 -08001304 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001305 s32 beaconint_us;
1306
1307 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001308 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001309
1310 beaconint_us = ieee80211_tu_to_usec(
1311 found->vif.bss_conf.beacon_int);
1312
Juuso Oikarinenff616382010-06-09 09:51:52 +03001313 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001314 if (timeout < 0) {
1315 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001316 * Go to full PSM if the user configures a very low
1317 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001318 * The 2000 second value is there for compatibility
1319 * until the PM_QOS_NETWORK_LATENCY is configured
1320 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001321 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001322 if (latency > (1900 * USEC_PER_MSEC) &&
1323 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001324 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001325 else
1326 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001327 }
Johannes Berg09b85562013-02-06 23:07:41 +01001328 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001329
Johannes Berg04fe2032009-04-22 18:44:37 +02001330 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001331 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001332 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001333 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001334 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001335
1336 /* If the TIM IE is invalid, pretend the value is 1 */
1337 if (!dtimper)
1338 dtimper = 1;
1339 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001340 maxslp = min_t(int, dtimper,
1341 latency / beaconint_us);
1342
Johannes Berg9ccebe62009-04-23 10:32:36 +02001343 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001344 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001345 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001346 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001347 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001348 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001349 }
Johannes Berg965beda2009-04-16 13:17:24 +02001350
1351 ieee80211_change_ps(local);
1352}
1353
Eliad Pellerab095872012-07-27 12:33:22 +03001354void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1355{
1356 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1357
1358 if (sdata->vif.bss_conf.ps != ps_allowed) {
1359 sdata->vif.bss_conf.ps = ps_allowed;
1360 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1361 }
1362}
1363
Johannes Berg965beda2009-04-16 13:17:24 +02001364void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1365{
1366 struct ieee80211_local *local =
1367 container_of(work, struct ieee80211_local,
1368 dynamic_ps_disable_work);
1369
1370 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1371 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1372 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1373 }
1374
1375 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001376 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001377 IEEE80211_QUEUE_STOP_REASON_PS);
1378}
1379
1380void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1381{
1382 struct ieee80211_local *local =
1383 container_of(work, struct ieee80211_local,
1384 dynamic_ps_enable_work);
1385 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001386 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301387 unsigned long flags;
1388 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001389
1390 /* can only happen when PS was just disabled anyway */
1391 if (!sdata)
1392 return;
1393
Christian Lamparter5e340692011-06-30 21:08:43 +02001394 ifmgd = &sdata->u.mgd;
1395
Johannes Berg965beda2009-04-16 13:17:24 +02001396 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1397 return;
1398
Johannes Berg09b85562013-02-06 23:07:41 +01001399 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001400 /* don't enter PS if TX frames are pending */
1401 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301402 mod_timer(&local->dynamic_ps_timer, jiffies +
1403 msecs_to_jiffies(
1404 local->hw.conf.dynamic_ps_timeout));
1405 return;
1406 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001407
1408 /*
1409 * transmission can be stopped by others which leads to
1410 * dynamic_ps_timer expiry. Postpone the ps timer if it
1411 * is not the actual idle state.
1412 */
1413 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1414 for (q = 0; q < local->hw.queues; q++) {
1415 if (local->queue_stop_reasons[q]) {
1416 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1417 flags);
1418 mod_timer(&local->dynamic_ps_timer, jiffies +
1419 msecs_to_jiffies(
1420 local->hw.conf.dynamic_ps_timeout));
1421 return;
1422 }
1423 }
1424 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301425 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301426
Vivek Natarajan375177b2010-02-09 14:50:28 +05301427 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301428 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001429 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301430 mod_timer(&local->dynamic_ps_timer, jiffies +
1431 msecs_to_jiffies(
1432 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001433 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301434 ieee80211_send_nullfunc(local, sdata, 1);
1435 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001436 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301437 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301438 }
1439
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001440 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1441 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301442 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1443 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1444 local->hw.conf.flags |= IEEE80211_CONF_PS;
1445 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1446 }
Johannes Berg965beda2009-04-16 13:17:24 +02001447}
1448
1449void ieee80211_dynamic_ps_timer(unsigned long data)
1450{
1451 struct ieee80211_local *local = (void *) data;
1452
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001453 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001454 return;
1455
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001456 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001457}
1458
Simon Wunderlich164eb022013-02-08 18:16:20 +01001459void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1460{
1461 struct delayed_work *delayed_work =
1462 container_of(work, struct delayed_work, work);
1463 struct ieee80211_sub_if_data *sdata =
1464 container_of(delayed_work, struct ieee80211_sub_if_data,
1465 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001466 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001467
Johannes Berg34a37402013-12-18 09:43:33 +01001468 mutex_lock(&sdata->local->mtx);
1469 if (sdata->wdev.cac_started) {
1470 ieee80211_vif_release_channel(sdata);
1471 cfg80211_cac_event(sdata->dev, &chandef,
1472 NL80211_RADAR_CAC_FINISHED,
1473 GFP_KERNEL);
1474 }
1475 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001476}
1477
Johannes Berg60f8b392008-09-08 17:44:22 +02001478/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001479static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001480 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001481 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001482{
Jiri Bencf0706e82007-05-05 11:45:53 -07001483 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001484 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001485 size_t left;
1486 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001487 const u8 *pos;
1488 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001489
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001490 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001491 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001492
Johannes Berg32c50572012-03-28 11:04:29 +02001493 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001494 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001495
1496 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001497 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001498
Jiri Bencf0706e82007-05-05 11:45:53 -07001499 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001500 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001501
1502 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001503 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001504
Jiri Bencf0706e82007-05-05 11:45:53 -07001505 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001506 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001507 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001508 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001509
1510 pos = wmm_param + 8;
1511 left = wmm_param_len - 8;
1512
1513 memset(&params, 0, sizeof(params));
1514
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001515 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001516 for (; left >= 4; left -= 4, pos += 4) {
1517 int aci = (pos[0] >> 5) & 0x03;
1518 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001519 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001520 int queue;
1521
1522 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001523 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001524 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001525 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001526 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001527 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1528 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001529 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001530 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001531 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001532 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001533 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001534 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1535 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001536 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001537 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001538 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001539 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001540 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001541 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1542 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001543 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001544 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001545 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001546 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001547 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001548 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001549 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1550 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001551 break;
1552 }
1553
1554 params.aifs = pos[0] & 0x0f;
1555 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1556 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001557 params.txop = get_unaligned_le16(pos + 2);
Alexander Bondar908f8d02013-04-07 09:53:30 +03001558 params.acm = acm;
Kalle Valoab133152010-01-12 10:42:31 +02001559 params.uapsd = uapsd;
1560
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001561 mlme_dbg(sdata,
1562 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1563 queue, aci, acm,
1564 params.aifs, params.cw_min, params.cw_max,
1565 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001566 sdata->tx_conf[queue] = params;
1567 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001568 sdata_err(sdata,
1569 "failed to set TX queue parameters for queue %d\n",
1570 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001571 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001572
1573 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001574 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001575 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001576}
1577
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001578static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1579{
1580 lockdep_assert_held(&sdata->local->mtx);
1581
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001582 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001583 ieee80211_run_deferred_scan(sdata->local);
1584}
1585
1586static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1587{
1588 mutex_lock(&sdata->local->mtx);
1589 __ieee80211_stop_poll(sdata);
1590 mutex_unlock(&sdata->local->mtx);
1591}
1592
Johannes Berg7a5158e2008-10-08 10:59:33 +02001593static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1594 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001595{
Johannes Bergbda39332008-10-11 01:51:51 +02001596 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001597 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001598 bool use_protection;
1599 bool use_short_preamble;
1600 bool use_short_slot;
1601
1602 if (erp_valid) {
1603 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1604 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1605 } else {
1606 use_protection = false;
1607 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1608 }
1609
1610 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001611 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001612 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001613
Johannes Berg471b3ef2007-12-28 14:32:58 +01001614 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001615 bss_conf->use_cts_prot = use_protection;
1616 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001617 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001618
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001619 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001620 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001621 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001622 }
Daniel Draked9430a32007-07-27 15:43:24 +02001623
Johannes Berg7a5158e2008-10-08 10:59:33 +02001624 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001625 bss_conf->use_short_slot = use_short_slot;
1626 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001627 }
1628
1629 return changed;
1630}
1631
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001632static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001633 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001634 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001635{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001636 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001637 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001638 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001639
Johannes Bergae5eb022008-10-14 16:58:37 +02001640 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001641 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001642 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001643
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001644 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001645 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001646
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001647 sdata->u.mgd.associated = cbss;
1648 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001649
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001650 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1651
Johannes Berg488dd7b2012-10-29 20:08:01 +01001652 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001653 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001654
Johannes Berg9caf0362012-11-29 01:25:20 +01001655 rcu_read_lock();
1656 ies = rcu_dereference(cbss->ies);
1657 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001658 int ret;
1659
1660 ret = cfg80211_get_p2p_attr(
1661 ies->data, ies->len,
1662 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001663 (u8 *) &bss_conf->p2p_noa_attr,
1664 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001665 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001666 sdata->u.mgd.p2p_noa_index =
1667 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001668 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001669 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001670 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001671 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001672 }
1673
Johannes Bergb291ba12009-07-10 15:29:03 +02001674 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001675 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001676
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001677 ieee80211_led_assoc(local, 1);
1678
Alexander Bondar989c6502013-05-16 17:34:17 +03001679 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001680 /*
1681 * If the AP is buggy we may get here with no DTIM period
1682 * known, so assume it's 1 which is the only safe assumption
1683 * in that case, although if the TIM IE is broken powersave
1684 * probably just won't work at all.
1685 */
1686 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03001687 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03001688 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02001689 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03001690 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02001691 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001692 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001693
Juuso Oikarinen68542962010-06-09 13:43:26 +03001694 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001695
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001696 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001697 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001698 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001699 bss_info_changed |= BSS_CHANGED_CQM;
1700
Juuso Oikarinen68542962010-06-09 13:43:26 +03001701 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001702 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001703 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001704
Johannes Bergae5eb022008-10-14 16:58:37 +02001705 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001706
Johannes Berg056508d2009-07-30 21:43:55 +02001707 mutex_lock(&local->iflist_mtx);
1708 ieee80211_recalc_ps(local, -1);
1709 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001710
Johannes Berg04ecd252012-09-11 14:34:12 +02001711 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001712 ieee80211_recalc_ps_vif(sdata);
1713
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001714 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001715}
1716
Jouni Malinene69e95d2010-03-29 23:29:31 -07001717static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001718 u16 stype, u16 reason, bool tx,
1719 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001720{
Johannes Berg46900292009-02-15 12:44:28 +01001721 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001722 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001723 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001724
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001725 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001726
Johannes Berg37ad3882012-02-24 13:50:54 +01001727 if (WARN_ON_ONCE(tx && !frame_buf))
1728 return;
1729
Johannes Bergb291ba12009-07-10 15:29:03 +02001730 if (WARN_ON(!ifmgd->associated))
1731 return;
1732
David Spinadel79543d82012-06-12 09:59:45 +03001733 ieee80211_stop_poll(sdata);
1734
Johannes Berg77fdaa12009-07-07 03:45:17 +02001735 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001736 netif_carrier_off(sdata->dev);
1737
Eliad Peller88bc40e2012-07-12 17:35:33 +03001738 /*
1739 * if we want to get out of ps before disassoc (why?) we have
1740 * to do it before sending disassoc, as otherwise the null-packet
1741 * won't be valid.
1742 */
1743 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1744 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1745 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1746 }
1747 local->ps_sdata = NULL;
1748
Eliad Pellerab095872012-07-27 12:33:22 +03001749 /* disable per-vif ps */
1750 ieee80211_recalc_ps_vif(sdata);
1751
Eliad Pellerf8239812012-06-27 14:18:22 +03001752 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1753 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001754 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001755
Johannes Berg37ad3882012-02-24 13:50:54 +01001756 /* deauthenticate/disassociate now */
1757 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001758 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1759 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001760
1761 /* flush out frame */
1762 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001763 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001764
Eliad Peller88a9e312012-06-01 11:14:03 +03001765 /* clear bssid only after building the needed mgmt frames */
1766 memset(ifmgd->bssid, 0, ETH_ALEN);
1767
Johannes Berg37ad3882012-02-24 13:50:54 +01001768 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01001769 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001770
1771 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001772 changed |= ieee80211_reset_erp_info(sdata);
1773
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001774 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001775 changed |= BSS_CHANGED_ASSOC;
1776 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001777
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001778 ifmgd->p2p_noa_index = -1;
1779 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1780 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001781
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001782 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001783 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1784 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001785 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1786 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001787
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001788 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301789
Kalle Valo520eb822008-12-18 23:35:27 +02001790 del_timer_sync(&local->dynamic_ps_timer);
1791 cancel_work_sync(&local->dynamic_ps_enable_work);
1792
Juuso Oikarinen68542962010-06-09 13:43:26 +03001793 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001794 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001795 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001796
Johannes Berg3abead52012-03-02 15:56:59 +01001797 sdata->vif.bss_conf.qos = false;
1798 changed |= BSS_CHANGED_QOS;
1799
Johannes Berg0aaffa92010-05-05 15:28:27 +02001800 /* The BSSID (not really interesting) and HT changed */
1801 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001802 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001803
Johannes Berg3abead52012-03-02 15:56:59 +01001804 /* disassociated - set to defaults now */
1805 ieee80211_set_wmm_default(sdata, false);
1806
Johannes Bergb9dcf712010-08-27 12:35:54 +02001807 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1808 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1809 del_timer_sync(&sdata->u.mgd.timer);
1810 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001811
Johannes Berg826262c2012-12-10 16:38:14 +02001812 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03001813 sdata->vif.bss_conf.beacon_rate = NULL;
1814
Alexander Bondar989c6502013-05-16 17:34:17 +03001815 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02001816
Johannes Berg028e8da02012-11-26 11:57:41 +01001817 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01001818 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01001819 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01001820 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001821
1822 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001823}
Jiri Bencf0706e82007-05-05 11:45:53 -07001824
Kalle Valo3cf335d2009-03-22 21:57:06 +02001825void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1826 struct ieee80211_hdr *hdr)
1827{
1828 /*
1829 * We can postpone the mgd.timer whenever receiving unicast frames
1830 * from AP because we know that the connection is working both ways
1831 * at that time. But multicast frames (and hence also beacons) must
1832 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001833 * data idle periods for sending the periodic probe request to the
1834 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001835 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001836 if (is_multicast_ether_addr(hdr->addr1))
1837 return;
1838
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001839 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001840}
Jiri Bencf0706e82007-05-05 11:45:53 -07001841
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001842static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1843{
1844 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001845 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001846
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001847 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001848 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
1849 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001850
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001851 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001852
1853 mutex_lock(&local->iflist_mtx);
1854 ieee80211_recalc_ps(local, -1);
1855 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001856
1857 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001858 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001859
1860 /*
1861 * We've received a probe response, but are not sure whether
1862 * we have or will be receiving any beacons or data, so let's
1863 * schedule the timers again, just in case.
1864 */
1865 ieee80211_sta_reset_beacon_monitor(sdata);
1866
1867 mod_timer(&ifmgd->conn_mon_timer,
1868 round_jiffies_up(jiffies +
1869 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001870out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001871 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001872}
1873
1874void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001875 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001876{
Felix Fietkau75706d02010-12-02 21:01:07 +01001877 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001878 return;
1879
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001880 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1881 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001882 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001883 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001884 else
1885 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001886 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001887 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001888 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001889
1890 if (ack)
1891 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001892}
1893
Maxim Levitskya43abf22009-07-31 18:54:12 +03001894static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1895{
1896 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1897 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001898 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001899 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001900
1901 /*
1902 * Try sending broadcast probe requests for the last three
1903 * probe requests after the first ones failed since some
1904 * buggy APs only support broadcast probe requests.
1905 */
1906 if (ifmgd->probe_send_count >= unicast_limit)
1907 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001908
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001909 /*
1910 * When the hardware reports an accurate Tx ACK status, it's
1911 * better to send a nullfunc frame instead of a probe request,
1912 * as it will kick us off the AP quickly if we aren't associated
1913 * anymore. The timeout will be reset if the frame is ACKed by
1914 * the AP.
1915 */
Soumik Das992e68b2012-05-20 15:31:13 +05301916 ifmgd->probe_send_count++;
1917
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001918 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1919 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001920 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001921 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001922 int ssid_len;
1923
Johannes Berg9caf0362012-11-29 01:25:20 +01001924 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001925 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001926 if (WARN_ON_ONCE(ssid == NULL))
1927 ssid_len = 0;
1928 else
1929 ssid_len = ssid[1];
1930
1931 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001932 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001933 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001934 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001935 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001936
Ben Greear180205b2011-02-04 15:30:24 -08001937 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001938 run_again(sdata, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301939 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01001940 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001941}
1942
Johannes Bergb291ba12009-07-10 15:29:03 +02001943static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1944 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001945{
Kalle Valo04de8382009-03-22 21:57:35 +02001946 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001947 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001948
Johannes Berg9607e6b2009-12-23 13:15:31 +01001949 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001950 return;
1951
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001952 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001953
1954 if (!ifmgd->associated)
1955 goto out;
1956
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001957 mutex_lock(&sdata->local->mtx);
1958
1959 if (sdata->local->tmp_channel || sdata->local->scanning) {
1960 mutex_unlock(&sdata->local->mtx);
1961 goto out;
1962 }
1963
Ben Greeara13fbe52013-03-25 11:19:35 -07001964 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001965 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07001966 "detected beacon loss from AP (missed %d beacons) - probing\n",
1967 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001968
Ben Greeara13fbe52013-03-25 11:19:35 -07001969 ieee80211_cqm_rssi_notify(&sdata->vif,
1970 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
1971 GFP_KERNEL);
1972 }
Kalle Valo04de8382009-03-22 21:57:35 +02001973
Johannes Bergb291ba12009-07-10 15:29:03 +02001974 /*
1975 * The driver/our work has already reported this event or the
1976 * connection monitoring has kicked in and we have already sent
1977 * a probe request. Or maybe the AP died and the driver keeps
1978 * reporting until we disassociate...
1979 *
1980 * In either case we have to ignore the current call to this
1981 * function (except for setting the correct probe reason bit)
1982 * because otherwise we would reset the timer every time and
1983 * never check whether we received a probe response!
1984 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001985 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02001986 already = true;
1987
Eliad Peller12b5f342013-11-18 19:06:46 +02001988 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1989
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001990 mutex_unlock(&sdata->local->mtx);
1991
Johannes Bergb291ba12009-07-10 15:29:03 +02001992 if (already)
1993 goto out;
1994
Johannes Berg4e751842009-06-10 15:16:52 +02001995 mutex_lock(&sdata->local->iflist_mtx);
1996 ieee80211_recalc_ps(sdata->local, -1);
1997 mutex_unlock(&sdata->local->iflist_mtx);
1998
Maxim Levitskya43abf22009-07-31 18:54:12 +03001999 ifmgd->probe_send_count = 0;
2000 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002001 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002002 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002003}
2004
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002005struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2006 struct ieee80211_vif *vif)
2007{
2008 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2009 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002010 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002011 struct sk_buff *skb;
2012 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002013 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002014
2015 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2016 return NULL;
2017
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002018 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002019
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002020 if (ifmgd->associated)
2021 cbss = ifmgd->associated;
2022 else if (ifmgd->auth_data)
2023 cbss = ifmgd->auth_data->bss;
2024 else if (ifmgd->assoc_data)
2025 cbss = ifmgd->assoc_data->bss;
2026 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002027 return NULL;
2028
Johannes Berg9caf0362012-11-29 01:25:20 +01002029 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002030 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002031 if (WARN_ON_ONCE(ssid == NULL))
2032 ssid_len = 0;
2033 else
2034 ssid_len = ssid[1];
2035
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002036 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002037 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002038 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002039 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002040 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002041
2042 return skb;
2043}
2044EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2045
Johannes Bergeef9e542013-01-29 13:09:34 +01002046static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002047{
2048 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002049 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002050
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002051 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002052 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002053 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002054 return;
2055 }
2056
Johannes Berg37ad3882012-02-24 13:50:54 +01002057 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2058 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002059 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002060 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Arik Nemtsov7578d572013-09-01 17:15:51 +03002061 sdata->vif.csa_active = false;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002062 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002063 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002064 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002065
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002066 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
2067 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002068 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002069}
2070
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002071static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002072{
2073 struct ieee80211_sub_if_data *sdata =
2074 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002075 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002076 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2077 struct sta_info *sta;
2078
2079 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302080 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002081 sta = sta_info_get(sdata, ifmgd->bssid);
2082 if (sta)
2083 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302084 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002085 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002086
Johannes Berg682bd382013-01-29 13:13:50 +01002087 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002088 sdata_info(sdata, "Connection to AP %pM lost\n",
2089 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002090 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002091 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002092 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002093 }
2094}
2095
2096static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2097{
2098 struct ieee80211_sub_if_data *sdata =
2099 container_of(work, struct ieee80211_sub_if_data,
2100 u.mgd.csa_connection_drop_work);
2101
Johannes Bergeef9e542013-01-29 13:09:34 +01002102 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002103}
2104
Kalle Valo04de8382009-03-22 21:57:35 +02002105void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2106{
2107 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002108 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002109
Johannes Bergb5878a22010-04-07 16:48:40 +02002110 trace_api_beacon_loss(sdata);
2111
Johannes Berg682bd382013-01-29 13:13:50 +01002112 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002113 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002114}
2115EXPORT_SYMBOL(ieee80211_beacon_loss);
2116
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002117void ieee80211_connection_loss(struct ieee80211_vif *vif)
2118{
2119 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2120 struct ieee80211_hw *hw = &sdata->local->hw;
2121
Johannes Bergb5878a22010-04-07 16:48:40 +02002122 trace_api_connection_loss(sdata);
2123
Johannes Berg682bd382013-01-29 13:13:50 +01002124 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002125 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2126}
2127EXPORT_SYMBOL(ieee80211_connection_loss);
2128
2129
Johannes Berg66e67e42012-01-20 13:55:27 +01002130static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2131 bool assoc)
2132{
2133 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2134
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002135 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002136
Johannes Berg66e67e42012-01-20 13:55:27 +01002137 if (!assoc) {
2138 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2139
2140 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2141 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002142 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002143 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002144 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002145 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002146 }
2147
Johannes Berg5b112d32013-02-01 01:49:58 +01002148 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002149 kfree(auth_data);
2150 sdata->u.mgd.auth_data = NULL;
2151}
2152
2153static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2154 struct ieee80211_mgmt *mgmt, size_t len)
2155{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002156 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002157 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2158 u8 *pos;
2159 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002160 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002161
2162 pos = mgmt->u.auth.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002163 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002164 if (!elems.challenge)
2165 return;
2166 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002167 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002168 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2169 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2170 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002171 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002172 elems.challenge - 2, elems.challenge_len + 2,
2173 auth_data->bss->bssid, auth_data->bss->bssid,
2174 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002175 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002176}
2177
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002178static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2179 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002180{
2181 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2182 u8 bssid[ETH_ALEN];
2183 u16 auth_alg, auth_transaction, status_code;
2184 struct sta_info *sta;
2185
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002186 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002187
2188 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002189 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002190
2191 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002192 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002193
2194 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2195
Joe Perchesb203ca32012-05-08 18:56:52 +00002196 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002197 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002198
2199 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2200 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2201 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2202
2203 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002204 auth_transaction != ifmgd->auth_data->expected_transaction) {
2205 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2206 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2207 auth_transaction,
2208 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002209 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002210 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002211
2212 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002213 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2214 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002215 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002216 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002217 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002218 }
2219
2220 switch (ifmgd->auth_data->algorithm) {
2221 case WLAN_AUTH_OPEN:
2222 case WLAN_AUTH_LEAP:
2223 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002224 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002225 break;
2226 case WLAN_AUTH_SHARED_KEY:
2227 if (ifmgd->auth_data->expected_transaction != 4) {
2228 ieee80211_auth_challenge(sdata, mgmt, len);
2229 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002230 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002231 }
2232 break;
2233 default:
2234 WARN_ONCE(1, "invalid auth alg %d",
2235 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002236 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002237 }
2238
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002239 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002240 ifmgd->auth_data->done = true;
2241 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002242 ifmgd->auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002243 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01002244
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002245 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2246 ifmgd->auth_data->expected_transaction != 2) {
2247 /*
2248 * Report auth frame to user space for processing since another
2249 * round of Authentication frames is still needed.
2250 */
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002251 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002252 return;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002253 }
2254
Johannes Berg66e67e42012-01-20 13:55:27 +01002255 /* move station state to auth */
2256 mutex_lock(&sdata->local->sta_mtx);
2257 sta = sta_info_get(sdata, bssid);
2258 if (!sta) {
2259 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2260 goto out_err;
2261 }
2262 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002263 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002264 goto out_err;
2265 }
2266 mutex_unlock(&sdata->local->sta_mtx);
2267
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002268 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002269 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002270 out_err:
2271 mutex_unlock(&sdata->local->sta_mtx);
2272 /* ignore frame -- wait for timeout */
Johannes Berg66e67e42012-01-20 13:55:27 +01002273}
2274
Calvin Owensdfa1ad22014-02-11 12:36:24 -06002275#define case_WLAN(type) \
2276 case WLAN_REASON_##type: return #type
2277
2278static const char *ieee80211_get_reason_code_string(u16 reason_code)
2279{
2280 switch (reason_code) {
2281 case_WLAN(UNSPECIFIED);
2282 case_WLAN(PREV_AUTH_NOT_VALID);
2283 case_WLAN(DEAUTH_LEAVING);
2284 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2285 case_WLAN(DISASSOC_AP_BUSY);
2286 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2287 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2288 case_WLAN(DISASSOC_STA_HAS_LEFT);
2289 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2290 case_WLAN(DISASSOC_BAD_POWER);
2291 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2292 case_WLAN(INVALID_IE);
2293 case_WLAN(MIC_FAILURE);
2294 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2295 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2296 case_WLAN(IE_DIFFERENT);
2297 case_WLAN(INVALID_GROUP_CIPHER);
2298 case_WLAN(INVALID_PAIRWISE_CIPHER);
2299 case_WLAN(INVALID_AKMP);
2300 case_WLAN(UNSUPP_RSN_VERSION);
2301 case_WLAN(INVALID_RSN_IE_CAP);
2302 case_WLAN(IEEE8021X_FAILED);
2303 case_WLAN(CIPHER_SUITE_REJECTED);
2304 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2305 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2306 case_WLAN(DISASSOC_LOW_ACK);
2307 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
2308 case_WLAN(QSTA_LEAVE_QBSS);
2309 case_WLAN(QSTA_NOT_USE);
2310 case_WLAN(QSTA_REQUIRE_SETUP);
2311 case_WLAN(QSTA_TIMEOUT);
2312 case_WLAN(QSTA_CIPHER_NOT_SUPP);
2313 case_WLAN(MESH_PEER_CANCELED);
2314 case_WLAN(MESH_MAX_PEERS);
2315 case_WLAN(MESH_CONFIG);
2316 case_WLAN(MESH_CLOSE);
2317 case_WLAN(MESH_MAX_RETRIES);
2318 case_WLAN(MESH_CONFIRM_TIMEOUT);
2319 case_WLAN(MESH_INVALID_GTK);
2320 case_WLAN(MESH_INCONSISTENT_PARAM);
2321 case_WLAN(MESH_INVALID_SECURITY);
2322 case_WLAN(MESH_PATH_ERROR);
2323 case_WLAN(MESH_PATH_NOFORWARD);
2324 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
2325 case_WLAN(MAC_EXISTS_IN_MBSS);
2326 case_WLAN(MESH_CHAN_REGULATORY);
2327 case_WLAN(MESH_CHAN);
2328 default: return "<unknown>";
2329 }
2330}
Johannes Berg66e67e42012-01-20 13:55:27 +01002331
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002332static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2333 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002334{
Johannes Berg46900292009-02-15 12:44:28 +01002335 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002336 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002337 u16 reason_code;
2338
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002339 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002340
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002341 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002342 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002343
Johannes Berg66e67e42012-01-20 13:55:27 +01002344 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002345 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002346 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002347
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002348 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002349
2350 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2351
Calvin Owensdfa1ad22014-02-11 12:36:24 -06002352 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
2353 bssid, reason_code, ieee80211_get_reason_code_string(reason_code));
Jiri Bencf0706e82007-05-05 11:45:53 -07002354
Johannes Berg37ad3882012-02-24 13:50:54 +01002355 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2356
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002357 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002358}
2359
2360
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002361static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2362 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002363{
Johannes Berg46900292009-02-15 12:44:28 +01002364 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002365 u16 reason_code;
2366
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002367 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002368
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002369 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002370 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002371
Johannes Berg66e67e42012-01-20 13:55:27 +01002372 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002373 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002374 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002375
2376 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2377
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002378 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2379 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002380
Johannes Berg37ad3882012-02-24 13:50:54 +01002381 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2382
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002383 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002384}
2385
Christian Lamparterc74d0842011-10-15 00:14:49 +02002386static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2387 u8 *supp_rates, unsigned int supp_rates_len,
2388 u32 *rates, u32 *basic_rates,
2389 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002390 int *min_rate, int *min_rate_index,
2391 int shift, u32 rate_flags)
Christian Lamparterc74d0842011-10-15 00:14:49 +02002392{
2393 int i, j;
2394
2395 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002396 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002397 bool is_basic = !!(supp_rates[i] & 0x80);
2398
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002399 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02002400 *have_higher_than_11mbit = true;
2401
2402 /*
2403 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2404 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2405 *
2406 * Note: Even through the membership selector and the basic
2407 * rate flag share the same bit, they are not exactly
2408 * the same.
2409 */
2410 if (!!(supp_rates[i] & 0x80) &&
2411 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2412 continue;
2413
2414 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002415 struct ieee80211_rate *br;
2416 int brate;
2417
2418 br = &sband->bitrates[j];
2419 if ((rate_flags & br->flags) != rate_flags)
2420 continue;
2421
2422 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2423 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02002424 *rates |= BIT(j);
2425 if (is_basic)
2426 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002427 if ((rate * 5) < *min_rate) {
2428 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002429 *min_rate_index = j;
2430 }
2431 break;
2432 }
2433 }
2434 }
2435}
Jiri Bencf0706e82007-05-05 11:45:53 -07002436
Johannes Berg66e67e42012-01-20 13:55:27 +01002437static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2438 bool assoc)
2439{
2440 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2441
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002442 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002443
Johannes Berg66e67e42012-01-20 13:55:27 +01002444 if (!assoc) {
2445 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2446
2447 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2448 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002449 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002450 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002451 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002452 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002453 }
2454
2455 kfree(assoc_data);
2456 sdata->u.mgd.assoc_data = NULL;
2457}
2458
2459static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2460 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002461 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002462{
Johannes Berg46900292009-02-15 12:44:28 +01002463 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002464 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002465 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002466 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002467 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002468 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002469 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002470 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02002471 const struct cfg80211_bss_ies *bss_ies = NULL;
2472 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Bergae5eb022008-10-14 16:58:37 +02002473 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002474 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02002475 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002476
Johannes Bergaf6b6372009-12-23 13:15:35 +01002477 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002478
Jiri Bencf0706e82007-05-05 11:45:53 -07002479 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002480 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002481
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002482 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002483 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2484 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002485 aid &= ~(BIT(15) | BIT(14));
2486
Johannes Berg05cb9102011-10-28 11:59:47 +02002487 ifmgd->broken_ap = false;
2488
2489 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002490 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2491 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002492 aid = 0;
2493 ifmgd->broken_ap = true;
2494 }
2495
Johannes Bergaf6b6372009-12-23 13:15:35 +01002496 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002497 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002498
Jiri Bencf0706e82007-05-05 11:45:53 -07002499 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002500 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002501 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002502 }
2503
Johannes Berg46900292009-02-15 12:44:28 +01002504 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002505
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002506 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02002507 * Some APs are erroneously not including some information in their
2508 * (re)association response frames. Try to recover by using the data
2509 * from the beacon or probe response. This seems to afflict mobile
2510 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2511 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2512 */
2513 if ((assoc_data->wmm && !elems.wmm_param) ||
2514 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2515 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2516 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2517 (!elems.vht_cap_elem || !elems.vht_operation))) {
2518 const struct cfg80211_bss_ies *ies;
2519 struct ieee802_11_elems bss_elems;
2520
2521 rcu_read_lock();
2522 ies = rcu_dereference(cbss->ies);
2523 if (ies)
2524 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2525 GFP_ATOMIC);
2526 rcu_read_unlock();
2527 if (!bss_ies)
2528 return false;
2529
2530 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2531 false, &bss_elems);
2532 if (assoc_data->wmm &&
2533 !elems.wmm_param && bss_elems.wmm_param) {
2534 elems.wmm_param = bss_elems.wmm_param;
2535 sdata_info(sdata,
2536 "AP bug: WMM param missing from AssocResp\n");
2537 }
2538
2539 /*
2540 * Also check if we requested HT/VHT, otherwise the AP doesn't
2541 * have to include the IEs in the (re)association response.
2542 */
2543 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2544 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2545 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2546 sdata_info(sdata,
2547 "AP bug: HT capability missing from AssocResp\n");
2548 }
2549 if (!elems.ht_operation && bss_elems.ht_operation &&
2550 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2551 elems.ht_operation = bss_elems.ht_operation;
2552 sdata_info(sdata,
2553 "AP bug: HT operation missing from AssocResp\n");
2554 }
2555 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2556 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2557 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2558 sdata_info(sdata,
2559 "AP bug: VHT capa missing from AssocResp\n");
2560 }
2561 if (!elems.vht_operation && bss_elems.vht_operation &&
2562 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2563 elems.vht_operation = bss_elems.vht_operation;
2564 sdata_info(sdata,
2565 "AP bug: VHT operation missing from AssocResp\n");
2566 }
2567 }
2568
2569 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002570 * We previously checked these in the beacon/probe response, so
2571 * they should be present here. This is just a safety net.
2572 */
2573 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2574 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2575 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002576 "HT AP is missing WMM params or HT capability/operation\n");
2577 ret = false;
2578 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002579 }
2580
2581 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2582 (!elems.vht_cap_elem || !elems.vht_operation)) {
2583 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002584 "VHT AP is missing VHT capability/operation\n");
2585 ret = false;
2586 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002587 }
2588
Guy Eilam2a33bee2011-08-17 15:18:15 +03002589 mutex_lock(&sdata->local->sta_mtx);
2590 /*
2591 * station info was already allocated and inserted before
2592 * the association and should be available to us
2593 */
Johannes Berg7852e362012-01-20 13:55:24 +01002594 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002595 if (WARN_ON(!sta)) {
2596 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002597 ret = false;
2598 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07002599 }
2600
Johannes Berg55de9082012-07-26 17:24:39 +02002601 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002602
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002603 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002604 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002605 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002606 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002607
Mahesh Palivela818255e2012-10-10 11:33:04 +00002608 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2609 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002610 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002611
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002612 /*
2613 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2614 * in their association response, so ignore that data for our own
2615 * configuration. If it changed since the last beacon, we'll get the
2616 * next beacon and update then.
2617 */
Johannes Berg11289582013-02-07 17:36:12 +01002618
Johannes Bergbee7f582013-02-07 22:24:55 +01002619 /*
2620 * If an operating mode notification IE is present, override the
2621 * NSS calculation (that would be done in rate_control_rate_init())
2622 * and use the # of streams from that element.
2623 */
2624 if (elems.opmode_notif &&
2625 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2626 u8 nss;
2627
2628 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2629 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2630 nss += 1;
2631 sta->sta.rx_nss = nss;
2632 }
2633
Johannes Berg4b7679a2008-09-18 18:14:18 +02002634 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002635
Johannes Berg46900292009-02-15 12:44:28 +01002636 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002637 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002638
Johannes Bergddf4ac52008-10-22 11:41:38 +02002639 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002640 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002641
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002642 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002643 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2644 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2645 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002646 sdata_info(sdata,
2647 "failed to move station %pM to desired state\n",
2648 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002649 WARN_ON(__sta_info_destroy(sta));
2650 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002651 ret = false;
2652 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01002653 }
2654
Johannes Berg7852e362012-01-20 13:55:24 +01002655 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002656
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002657 /*
2658 * Always handle WMM once after association regardless
2659 * of the first value the AP uses. Setting -1 here has
2660 * that effect because the AP values is an unsigned
2661 * 4-bit value.
2662 */
2663 ifmgd->wmm_last_param_set = -1;
2664
Johannes Berg095d81c2013-10-15 12:25:07 +02002665 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002666 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002667 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002668 else
Johannes Berg3abead52012-03-02 15:56:59 +01002669 ieee80211_set_wmm_default(sdata, false);
2670 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002671
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002672 /* set AID and assoc capability,
2673 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002674 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002675 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002676 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002677
Kalle Valo15b7b062009-03-22 21:57:14 +02002678 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002679 * If we're using 4-addr mode, let the AP know that we're
2680 * doing so, so that it can create the STA VLAN on its side
2681 */
2682 if (ifmgd->use_4addr)
2683 ieee80211_send_4addr_nullfunc(local, sdata);
2684
2685 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002686 * Start timer to probe the connection to the AP now.
2687 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002688 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002689 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002690 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002691
Johannes Berg35d865a2013-05-28 10:54:03 +02002692 ret = true;
2693 out:
2694 kfree(bss_ies);
2695 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002696}
2697
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002698static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2699 struct ieee80211_mgmt *mgmt,
2700 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002701{
2702 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2703 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2704 u16 capab_info, status_code, aid;
2705 struct ieee802_11_elems elems;
2706 u8 *pos;
2707 bool reassoc;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002708 struct cfg80211_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002709
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002710 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002711
2712 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002713 return;
Joe Perchesb203ca32012-05-08 18:56:52 +00002714 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002715 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002716
2717 /*
2718 * AssocResp and ReassocResp have identical structure, so process both
2719 * of them in this function.
2720 */
2721
2722 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002723 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002724
2725 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2726 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2727 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2728 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2729
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002730 sdata_info(sdata,
2731 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2732 reassoc ? "Rea" : "A", mgmt->sa,
2733 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002734
2735 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002736 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002737
2738 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002739 elems.timeout_int &&
2740 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002741 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002742 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002743 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002744 sdata_info(sdata,
2745 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2746 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002747 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002748 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002749 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002750 run_again(sdata, assoc_data->timeout);
2751 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002752 }
2753
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002754 bss = assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01002755
2756 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002757 sdata_info(sdata, "%pM denied association (code=%d)\n",
2758 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002759 ieee80211_destroy_assoc_data(sdata, false);
2760 } else {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002761 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002762 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002763 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02002764 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002765 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002766 }
John W. Linville635d9992012-07-09 16:34:34 -04002767 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002768
2769 /*
2770 * destroy assoc_data afterwards, as otherwise an idle
2771 * recalc after assoc_data is NULL but before associated
2772 * is set can cause the interface to go idle
2773 */
2774 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002775 }
2776
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002777 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002778}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002779
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002780static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002781 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002782 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002783 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002784{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002785 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02002786 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002787 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002788
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002789 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01002790
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02002791 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
2792 if (!channel)
Johannes Berg5bda6172008-09-08 11:05:10 +02002793 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002794
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002795 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002796 channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03002797 if (bss) {
Alexander Bondar817cee72013-05-19 14:23:57 +03002798 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
Johannes Bergd2722f82014-03-04 11:43:28 +01002799 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03002800 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002801}
2802
2803
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002804static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002805 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002806{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002807 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002808 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002809 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2810 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002811 struct ieee802_11_elems elems;
2812
Kalle Valo15b7b062009-03-22 21:57:14 +02002813 ifmgd = &sdata->u.mgd;
2814
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002815 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002816
Joe Perchesb203ca32012-05-08 18:56:52 +00002817 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002818 return; /* ignore ProbeResp to foreign address */
2819
Ester Kummerae6a44e2008-06-27 18:54:48 +03002820 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2821 if (baselen > len)
2822 return;
2823
2824 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002825 false, &elems);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002826
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002827 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002828
Johannes Berg77fdaa12009-07-07 03:45:17 +02002829 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002830 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002831 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002832
2833 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002834 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002835 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002836 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002837 ifmgd->auth_data->tries = 0;
2838 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002839 ifmgd->auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002840 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01002841 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002842}
2843
Johannes Bergd91f36d2009-04-16 13:17:26 +02002844/*
2845 * This is the canonical list of information elements we care about,
2846 * the filter code also gives us all changes to the Microsoft OUI
2847 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2848 *
2849 * We implement beacon filtering in software since that means we can
2850 * avoid processing the frame here and in cfg80211, and userspace
2851 * will not be able to tell whether the hardware supports it or not.
2852 *
2853 * XXX: This list needs to be dynamic -- userspace needs to be able to
2854 * add items it requires. It also needs to be able to tell us to
2855 * look out for other vendor IEs.
2856 */
2857static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002858 (1ULL << WLAN_EID_COUNTRY) |
2859 (1ULL << WLAN_EID_ERP_INFO) |
2860 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2861 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2862 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002863 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002864
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002865static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2866 struct ieee80211_mgmt *mgmt, size_t len,
2867 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002868{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002869 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002870 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002871 size_t baselen;
2872 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002873 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002874 struct ieee80211_chanctx_conf *chanctx_conf;
2875 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002876 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002877 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002878 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002879 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002880 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002881 u8 *bssid;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002882 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002883
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002884 sdata_assert_lock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07002885
Ester Kummerae6a44e2008-06-27 18:54:48 +03002886 /* Process beacon from the current BSS */
2887 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2888 if (baselen > len)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002889 return;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002890
Johannes Berg55de9082012-07-26 17:24:39 +02002891 rcu_read_lock();
2892 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2893 if (!chanctx_conf) {
2894 rcu_read_unlock();
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002895 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002896 }
2897
Johannes Berg4bf88532012-11-09 11:39:59 +01002898 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002899 rcu_read_unlock();
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002900 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002901 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002902 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002903 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002904
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002905 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002906 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002907 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002908 len - baselen, false, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002909
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002910 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Alexander Bondar482a9c72013-06-03 17:29:33 +03002911 if (elems.tim && !elems.parse_error) {
2912 const struct ieee80211_tim_ie *tim_ie = elems.tim;
2913 ifmgd->dtim_period = tim_ie->dtim_period;
2914 }
Alexander Bondar989c6502013-05-16 17:34:17 +03002915 ifmgd->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002916 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002917 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2918 sdata->vif.bss_conf.sync_tsf =
2919 le64_to_cpu(mgmt->u.beacon.timestamp);
2920 sdata->vif.bss_conf.sync_device_ts =
2921 rx_status->device_timestamp;
2922 if (elems.tim)
2923 sdata->vif.bss_conf.sync_dtim_count =
2924 elems.tim->dtim_count;
2925 else
2926 sdata->vif.bss_conf.sync_dtim_count = 0;
2927 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002928 /* continue assoc process */
2929 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002930 ifmgd->assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002931 run_again(sdata, ifmgd->assoc_data->timeout);
2932 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002933 }
2934
2935 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002936 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002937 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002938 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002939
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002940 /* Track average RSSI from the Beacon frames of the current AP */
2941 ifmgd->last_beacon_signal = rx_status->signal;
2942 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2943 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002944 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002945 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002946 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002947 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002948 } else {
2949 ifmgd->ave_beacon_signal =
2950 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2951 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2952 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002953 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002954 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002955
2956 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2957 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2958 int sig = ifmgd->ave_beacon_signal;
2959 int last_sig = ifmgd->last_ave_beacon_signal;
2960
2961 /*
2962 * if signal crosses either of the boundaries, invoke callback
2963 * with appropriate parameters
2964 */
2965 if (sig > ifmgd->rssi_max_thold &&
2966 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2967 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002968 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002969 } else if (sig < ifmgd->rssi_min_thold &&
2970 (last_sig >= ifmgd->rssi_max_thold ||
2971 last_sig == 0)) {
2972 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002973 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002974 }
2975 }
2976
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002977 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002978 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002979 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002980 int sig = ifmgd->ave_beacon_signal / 16;
2981 int last_event = ifmgd->last_cqm_event_signal;
2982 int thold = bss_conf->cqm_rssi_thold;
2983 int hyst = bss_conf->cqm_rssi_hyst;
2984 if (sig < thold &&
2985 (last_event == 0 || sig < last_event - hyst)) {
2986 ifmgd->last_cqm_event_signal = sig;
2987 ieee80211_cqm_rssi_notify(
2988 &sdata->vif,
2989 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2990 GFP_KERNEL);
2991 } else if (sig > thold &&
2992 (last_event == 0 || sig > last_event + hyst)) {
2993 ifmgd->last_cqm_event_signal = sig;
2994 ieee80211_cqm_rssi_notify(
2995 &sdata->vif,
2996 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2997 GFP_KERNEL);
2998 }
2999 }
3000
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003001 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003002 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01003003 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003004 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03003005 }
3006
Johannes Bergb291ba12009-07-10 15:29:03 +02003007 /*
3008 * Push the beacon loss detection into the future since
3009 * we are processing a beacon from the AP just now.
3010 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003011 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003012
Johannes Bergd91f36d2009-04-16 13:17:26 +02003013 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3014 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003015 len - baselen, false, &elems,
Johannes Bergd91f36d2009-04-16 13:17:26 +02003016 care_about_ies, ncrc);
3017
Vivek Natarajan25c9c872009-03-02 20:20:30 +05303018 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02003019 bool directed_tim = ieee80211_check_tim(elems.tim,
3020 elems.tim_len,
3021 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02003022 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02003023 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01003024 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3025 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3026 ieee80211_hw_config(local,
3027 IEEE80211_CONF_CHANGE_PS);
3028 }
Kalle Valo572e0012009-02-10 17:09:31 +02003029 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05303030 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02003031 local->pspolling = true;
3032
3033 /*
3034 * Here is assumed that the driver will be
3035 * able to send ps-poll frame and receive a
3036 * response even though power save mode is
3037 * enabled, but some drivers might require
3038 * to disable power save here. This needs
3039 * to be investigated.
3040 */
3041 ieee80211_send_pspoll(local, sdata);
3042 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08003043 }
3044 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003045
Johannes Berg488dd7b2012-10-29 20:08:01 +01003046 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003047 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01003048 int ret;
3049
3050 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3051 len - baselen,
3052 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01003053 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003054 if (ret >= 2) {
3055 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3056 /* valid noa_attr and index changed */
3057 sdata->u.mgd.p2p_noa_index = noa.index;
3058 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3059 changed |= BSS_CHANGED_P2P_PS;
3060 /*
3061 * make sure we update all information, the CRC
3062 * mechanism doesn't look at P2P attributes.
3063 */
3064 ifmgd->beacon_crc_valid = false;
3065 }
3066 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3067 /* noa_attr not found and we had valid noa_attr before */
3068 sdata->u.mgd.p2p_noa_index = -1;
3069 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01003070 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01003071 ifmgd->beacon_crc_valid = false;
3072 }
3073 }
3074
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003075 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003076 return;
Jouni Malinen30196672009-05-19 17:01:43 +03003077 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003078 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03003079
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02003080 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02003081
Johannes Bergd70b7612013-08-23 15:48:48 +02003082 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
3083 &elems, true);
3084
Johannes Berg095d81c2013-10-15 12:25:07 +02003085 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
3086 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Johannes Berg7d257452012-07-06 17:37:43 +02003087 elems.wmm_param_len))
3088 changed |= BSS_CHANGED_QOS;
3089
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003090 /*
3091 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01003092 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003093 */
Alexander Bondar989c6502013-05-16 17:34:17 +03003094 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003095 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3096 if (elems.tim)
3097 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3098 else
3099 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003100
3101 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3102 sdata->vif.bss_conf.sync_tsf =
3103 le64_to_cpu(mgmt->u.beacon.timestamp);
3104 sdata->vif.bss_conf.sync_device_ts =
3105 rx_status->device_timestamp;
3106 if (elems.tim)
3107 sdata->vif.bss_conf.sync_dtim_count =
3108 elems.tim->dtim_count;
3109 else
3110 sdata->vif.bss_conf.sync_dtim_count = 0;
3111 }
3112
Alexander Bondar989c6502013-05-16 17:34:17 +03003113 changed |= BSS_CHANGED_BEACON_INFO;
3114 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03003115
3116 mutex_lock(&local->iflist_mtx);
3117 ieee80211_recalc_ps(local, -1);
3118 mutex_unlock(&local->iflist_mtx);
3119
Alexander Bondarce857882013-05-06 17:17:04 +03003120 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003121 }
3122
Johannes Berg1946bed2013-03-27 14:31:53 +01003123 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003124 erp_valid = true;
3125 erp_value = elems.erp_info[0];
3126 } else {
3127 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003128 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003129 changed |= ieee80211_handle_bss_capability(sdata,
3130 le16_to_cpu(mgmt->u.beacon.capab_info),
3131 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003132
Johannes Berg11289582013-02-07 17:36:12 +01003133 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003134 sta = sta_info_get(sdata, bssid);
3135
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003136 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3137 elems.vht_operation, bssid, &changed)) {
3138 mutex_unlock(&local->sta_mtx);
3139 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3140 WLAN_REASON_DEAUTH_LEAVING,
3141 true, deauth_buf);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003142 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
3143 sizeof(deauth_buf));
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003144 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003145 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003146
3147 if (sta && elems.opmode_notif)
3148 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3149 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003150 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003151
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003152 if (elems.country_elem && elems.pwr_constr_elem &&
3153 mgmt->u.probe_resp.capab_info &
3154 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003155 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3156 elems.country_elem,
3157 elems.country_elem_len,
3158 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003159
Johannes Berg471b3ef2007-12-28 14:32:58 +01003160 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003161}
3162
Johannes Berg1fa57d02010-06-10 10:21:32 +02003163void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3164 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003165{
3166 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003167 struct ieee80211_mgmt *mgmt;
3168 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003169 struct ieee802_11_elems elems;
3170 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07003171
Jiri Bencf0706e82007-05-05 11:45:53 -07003172 rx_status = (struct ieee80211_rx_status *) skb->cb;
3173 mgmt = (struct ieee80211_mgmt *) skb->data;
3174 fc = le16_to_cpu(mgmt->frame_control);
3175
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003176 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003177
Johannes Berg66e67e42012-01-20 13:55:27 +01003178 switch (fc & IEEE80211_FCTL_STYPE) {
3179 case IEEE80211_STYPE_BEACON:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003180 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003181 break;
3182 case IEEE80211_STYPE_PROBE_RESP:
3183 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3184 break;
3185 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003186 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003187 break;
3188 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003189 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003190 break;
3191 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003192 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003193 break;
3194 case IEEE80211_STYPE_ASSOC_RESP:
3195 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003196 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003197 break;
3198 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01003199 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003200 ies_len = skb->len -
3201 offsetof(struct ieee80211_mgmt,
3202 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01003203
3204 if (ies_len < 0)
3205 break;
3206
3207 ieee802_11_parse_elems(
3208 mgmt->u.action.u.chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003209 ies_len, true, &elems);
Johannes Berg37799e52013-03-26 14:02:26 +01003210
3211 if (elems.parse_error)
3212 break;
3213
Johannes Berg66e67e42012-01-20 13:55:27 +01003214 ieee80211_sta_process_chanswitch(sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01003215 rx_status->mactime,
Johannes Berg04a161f2013-05-03 09:35:35 +02003216 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003217 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3218 ies_len = skb->len -
3219 offsetof(struct ieee80211_mgmt,
3220 u.action.u.ext_chan_switch.variable);
3221
3222 if (ies_len < 0)
3223 break;
3224
3225 ieee802_11_parse_elems(
3226 mgmt->u.action.u.ext_chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003227 ies_len, true, &elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003228
3229 if (elems.parse_error)
3230 break;
3231
3232 /* for the handling code pretend this was also an IE */
3233 elems.ext_chansw_ie =
3234 &mgmt->u.action.u.ext_chan_switch.data;
3235
3236 ieee80211_sta_process_chanswitch(sdata,
3237 rx_status->mactime,
Johannes Berg04a161f2013-05-03 09:35:35 +02003238 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003239 }
Johannes Berg37799e52013-03-26 14:02:26 +01003240 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003241 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003242 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003243}
3244
Johannes Berg9c6bd792008-09-11 00:01:52 +02003245static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003246{
3247 struct ieee80211_sub_if_data *sdata =
3248 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003249
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003250 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003251}
3252
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003253static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003254 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003255{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003256 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003257
Johannes Berg37ad3882012-02-24 13:50:54 +01003258 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003259 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01003260
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003261 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3262 IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003263}
3264
Johannes Berg66e67e42012-01-20 13:55:27 +01003265static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3266{
3267 struct ieee80211_local *local = sdata->local;
3268 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3269 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003270 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003271
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003272 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003273
3274 if (WARN_ON_ONCE(!auth_data))
3275 return -EINVAL;
3276
Johannes Berg66e67e42012-01-20 13:55:27 +01003277 auth_data->tries++;
3278
3279 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003280 sdata_info(sdata, "authentication with %pM timed out\n",
3281 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003282
3283 /*
3284 * Most likely AP is not in the range so remove the
3285 * bss struct for that AP.
3286 */
3287 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3288
3289 return -ETIMEDOUT;
3290 }
3291
Johannes Berga1845fc2012-06-27 13:18:36 +02003292 drv_mgd_prepare_tx(local, sdata);
3293
Johannes Berg66e67e42012-01-20 13:55:27 +01003294 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003295 u16 trans = 1;
3296 u16 status = 0;
3297
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003298 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3299 auth_data->bss->bssid, auth_data->tries,
3300 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003301
3302 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003303
3304 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3305 trans = auth_data->sae_trans;
3306 status = auth_data->sae_status;
3307 auth_data->expected_transaction = trans;
3308 }
3309
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003310 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3311 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3312 IEEE80211_TX_INTFL_MLME_CONN_TX;
3313
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003314 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3315 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003316 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003317 auth_data->bss->bssid, NULL, 0, 0,
3318 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003319 } else {
3320 const u8 *ssidie;
3321
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003322 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3323 auth_data->bss->bssid, auth_data->tries,
3324 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003325
Johannes Berg9caf0362012-11-29 01:25:20 +01003326 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003327 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003328 if (!ssidie) {
3329 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003330 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003331 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003332 /*
3333 * Direct probe is sent to broadcast address as some APs
3334 * will not answer to direct packet in unassociated state.
3335 */
3336 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003337 NULL, 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02003338 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003339 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003340 }
3341
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003342 if (tx_flags == 0) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01003343 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Bergcb236d22013-07-29 23:07:43 +02003344 auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003345 run_again(sdata, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003346 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02003347 auth_data->timeout =
3348 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3349 auth_data->timeout_started = true;
3350 run_again(sdata, auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003351 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003352
3353 return 0;
3354}
3355
3356static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3357{
3358 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3359 struct ieee80211_local *local = sdata->local;
3360
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003361 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003362
Johannes Berg66e67e42012-01-20 13:55:27 +01003363 assoc_data->tries++;
3364 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003365 sdata_info(sdata, "association with %pM timed out\n",
3366 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003367
3368 /*
3369 * Most likely AP is not in the range so remove the
3370 * bss struct for that AP.
3371 */
3372 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3373
3374 return -ETIMEDOUT;
3375 }
3376
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003377 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3378 assoc_data->bss->bssid, assoc_data->tries,
3379 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003380 ieee80211_send_assoc(sdata);
3381
Johannes Berg1672c0e32013-01-29 15:02:27 +01003382 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3383 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003384 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003385 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003386 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02003387 assoc_data->timeout =
3388 round_jiffies_up(jiffies +
3389 IEEE80211_ASSOC_TIMEOUT_LONG);
3390 assoc_data->timeout_started = true;
3391 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003392 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003393
3394 return 0;
3395}
3396
Johannes Berg1672c0e32013-01-29 15:02:27 +01003397void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3398 __le16 fc, bool acked)
3399{
3400 struct ieee80211_local *local = sdata->local;
3401
3402 sdata->u.mgd.status_fc = fc;
3403 sdata->u.mgd.status_acked = acked;
3404 sdata->u.mgd.status_received = true;
3405
3406 ieee80211_queue_work(&local->hw, &sdata->work);
3407}
3408
Johannes Berg1fa57d02010-06-10 10:21:32 +02003409void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003410{
Johannes Berg60f8b392008-09-08 17:44:22 +02003411 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003412 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003413
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003414 sdata_lock(sdata);
Johannes Berg60f8b392008-09-08 17:44:22 +02003415
Johannes Berg1672c0e32013-01-29 15:02:27 +01003416 if (ifmgd->status_received) {
3417 __le16 fc = ifmgd->status_fc;
3418 bool status_acked = ifmgd->status_acked;
3419
3420 ifmgd->status_received = false;
3421 if (ifmgd->auth_data &&
3422 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3423 if (status_acked) {
3424 ifmgd->auth_data->timeout =
3425 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003426 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003427 } else {
3428 ifmgd->auth_data->timeout = jiffies - 1;
3429 }
Johannes Berg89afe612013-02-13 15:39:57 +01003430 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003431 } else if (ifmgd->assoc_data &&
3432 (ieee80211_is_assoc_req(fc) ||
3433 ieee80211_is_reassoc_req(fc))) {
3434 if (status_acked) {
3435 ifmgd->assoc_data->timeout =
3436 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003437 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003438 } else {
3439 ifmgd->assoc_data->timeout = jiffies - 1;
3440 }
Johannes Berg89afe612013-02-13 15:39:57 +01003441 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003442 }
3443 }
3444
Johannes Berg89afe612013-02-13 15:39:57 +01003445 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003446 time_after(jiffies, ifmgd->auth_data->timeout)) {
3447 if (ifmgd->auth_data->done) {
3448 /*
3449 * ok ... we waited for assoc but userspace didn't,
3450 * so let's just kill the auth data
3451 */
3452 ieee80211_destroy_auth_data(sdata, false);
3453 } else if (ieee80211_probe_auth(sdata)) {
3454 u8 bssid[ETH_ALEN];
3455
3456 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3457
3458 ieee80211_destroy_auth_data(sdata, false);
3459
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003460 cfg80211_auth_timeout(sdata->dev, bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003461 }
Johannes Berg89afe612013-02-13 15:39:57 +01003462 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003463 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01003464
Johannes Berg89afe612013-02-13 15:39:57 +01003465 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003466 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03003467 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003468 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02003469 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01003470
3471 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02003472 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003473 }
Johannes Berg89afe612013-02-13 15:39:57 +01003474 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003475 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01003476
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003477 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02003478 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003479 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003480 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003481
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003482 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003483
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003484 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003485 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003486 else
Ben Greear180205b2011-02-04 15:30:24 -08003487 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003488
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003489 /* ACK received for nullfunc probing frame */
3490 if (!ifmgd->probe_send_count)
3491 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003492 else if (ifmgd->nullfunc_failed) {
3493 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003494 mlme_dbg(sdata,
3495 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3496 bssid, ifmgd->probe_send_count,
3497 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003498 ieee80211_mgd_probe_ap_send(sdata);
3499 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003500 mlme_dbg(sdata,
3501 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3502 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003503 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003504 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3505 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003506 }
3507 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003508 run_again(sdata, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003509 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003510 mlme_dbg(sdata,
3511 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3512 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003513 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003514 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003515 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003516 mlme_dbg(sdata,
3517 "No probe response from AP %pM after %dms, try %d/%i\n",
3518 bssid, probe_wait_ms,
3519 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003520 ieee80211_mgd_probe_ap_send(sdata);
3521 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003522 /*
3523 * We actually lost the connection ... or did we?
3524 * Let's make sure!
3525 */
Ben Greearb38afa82010-10-07 16:12:06 -07003526 wiphy_debug(local->hw.wiphy,
3527 "%s: No probe response from AP %pM"
3528 " after %dms, disconnecting.\n",
3529 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003530 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003531
Johannes Berg95acac62011-07-12 12:30:59 +02003532 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003533 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003534 }
3535 }
3536
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003537 sdata_unlock(sdata);
Johannes Berg60f8b392008-09-08 17:44:22 +02003538}
Johannes Berg0a51b272008-09-08 17:44:25 +02003539
Johannes Bergb291ba12009-07-10 15:29:03 +02003540static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3541{
3542 struct ieee80211_sub_if_data *sdata =
3543 (struct ieee80211_sub_if_data *) data;
3544 struct ieee80211_local *local = sdata->local;
3545
3546 if (local->quiescing)
3547 return;
3548
Johannes Berg682bd382013-01-29 13:13:50 +01003549 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003550 ieee80211_queue_work(&sdata->local->hw,
3551 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003552}
3553
3554static void ieee80211_sta_conn_mon_timer(unsigned long data)
3555{
3556 struct ieee80211_sub_if_data *sdata =
3557 (struct ieee80211_sub_if_data *) data;
3558 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3559 struct ieee80211_local *local = sdata->local;
3560
3561 if (local->quiescing)
3562 return;
3563
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003564 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003565}
3566
3567static void ieee80211_sta_monitor_work(struct work_struct *work)
3568{
3569 struct ieee80211_sub_if_data *sdata =
3570 container_of(work, struct ieee80211_sub_if_data,
3571 u.mgd.monitor_work);
3572
3573 ieee80211_mgd_probe_ap(sdata, false);
3574}
3575
Johannes Berga1678f82008-09-11 00:01:47 +02003576static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3577{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003578 u32 flags;
3579
Kalle Vaload935682009-04-19 08:47:19 +03003580 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003581 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003582
Johannes Bergb291ba12009-07-10 15:29:03 +02003583 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003584 flags = sdata->local->hw.flags;
3585 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3586 ieee80211_queue_work(&sdata->local->hw,
3587 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003588 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003589 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003590 }
Johannes Berga1678f82008-09-11 00:01:47 +02003591}
3592
Johannes Bergb8360ab2013-04-29 14:57:44 +02003593#ifdef CONFIG_PM
Johannes Berg1a1cb742014-03-19 09:55:55 +01003594void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
3595{
3596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3597 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3598
3599 sdata_lock(sdata);
3600
3601 if (ifmgd->auth_data) {
3602 /*
3603 * If we are trying to authenticate while suspending, cfg80211
3604 * won't know and won't actually abort those attempts, thus we
3605 * need to do that ourselves.
3606 */
3607 ieee80211_send_deauth_disassoc(sdata,
3608 ifmgd->auth_data->bss->bssid,
3609 IEEE80211_STYPE_DEAUTH,
3610 WLAN_REASON_DEAUTH_LEAVING,
3611 false, frame_buf);
3612 ieee80211_destroy_auth_data(sdata, false);
3613 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3614 IEEE80211_DEAUTH_FRAME_LEN);
3615 }
3616
3617 sdata_unlock(sdata);
3618}
3619
Johannes Bergb8360ab2013-04-29 14:57:44 +02003620void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3621{
3622 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3623
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003624 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003625 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003626 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003627 return;
3628 }
3629
3630 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3631 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3632 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3633 ieee80211_sta_connection_lost(sdata,
3634 ifmgd->associated->bssid,
3635 WLAN_REASON_UNSPECIFIED,
3636 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003637 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003638 return;
3639 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003640 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003641}
3642#endif
3643
Johannes Berg9c6bd792008-09-11 00:01:52 +02003644/* interface setup */
3645void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3646{
Johannes Berg46900292009-02-15 12:44:28 +01003647 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003648
Johannes Berg46900292009-02-15 12:44:28 +01003649 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003650 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003651 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003652 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3653 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003654 INIT_WORK(&ifmgd->csa_connection_drop_work,
3655 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03003656 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Johannes Berg46900292009-02-15 12:44:28 +01003657 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003658 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003659 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3660 (unsigned long) sdata);
3661 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3662 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003663 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303664 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003665
Johannes Bergab1faea2009-07-01 21:41:17 +02003666 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303667 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003668 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3669 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003670 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003671
Johannes Berg0f782312009-12-01 13:37:02 +01003672 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3673 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3674 else
3675 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003676}
3677
3678/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003679void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3680{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003681 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003682
3683 /* Restart STA timers */
3684 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07003685 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3686 if (ieee80211_sdata_running(sdata))
3687 ieee80211_restart_sta_timer(sdata);
3688 }
Johannes Berga1678f82008-09-11 00:01:47 +02003689 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003690}
Johannes Berg10f644a2009-04-16 13:17:25 +02003691
3692int ieee80211_max_network_latency(struct notifier_block *nb,
3693 unsigned long data, void *dummy)
3694{
3695 s32 latency_usec = (s32) data;
3696 struct ieee80211_local *local =
3697 container_of(nb, struct ieee80211_local,
3698 network_latency_notifier);
3699
3700 mutex_lock(&local->iflist_mtx);
3701 ieee80211_recalc_ps(local, latency_usec);
3702 mutex_unlock(&local->iflist_mtx);
3703
Zhao, Gang8bd811a2014-04-21 12:53:05 +08003704 return NOTIFY_OK;
Johannes Berg10f644a2009-04-16 13:17:25 +02003705}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003706
Johannes Bergf2d9d272012-11-22 14:11:39 +01003707static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3708 struct cfg80211_bss *cbss)
3709{
3710 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3711 const u8 *ht_cap_ie, *vht_cap_ie;
3712 const struct ieee80211_ht_cap *ht_cap;
3713 const struct ieee80211_vht_cap *vht_cap;
3714 u8 chains = 1;
3715
3716 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3717 return chains;
3718
Johannes Berg9caf0362012-11-29 01:25:20 +01003719 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003720 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3721 ht_cap = (void *)(ht_cap_ie + 2);
3722 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3723 /*
3724 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3725 * "Tx Unequal Modulation Supported" fields.
3726 */
3727 }
3728
3729 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3730 return chains;
3731
Johannes Berg9caf0362012-11-29 01:25:20 +01003732 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003733 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3734 u8 nss;
3735 u16 tx_mcs_map;
3736
3737 vht_cap = (void *)(vht_cap_ie + 2);
3738 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3739 for (nss = 8; nss > 0; nss--) {
3740 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3741 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3742 break;
3743 }
3744 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3745 chains = max(chains, nss);
3746 }
3747
3748 return chains;
3749}
3750
Johannes Bergb17166a2012-07-27 11:41:27 +02003751static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3752 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003753{
3754 struct ieee80211_local *local = sdata->local;
3755 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003756 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003757 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003758 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003759 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003760 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003761
Johannes Berg24398e32012-03-28 10:58:36 +02003762 sband = local->hw.wiphy->bands[cbss->channel->band];
3763
Johannes Bergf2d9d272012-11-22 14:11:39 +01003764 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3765 IEEE80211_STA_DISABLE_80P80MHZ |
3766 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003767
Johannes Berg9caf0362012-11-29 01:25:20 +01003768 rcu_read_lock();
3769
Johannes Bergf2d9d272012-11-22 14:11:39 +01003770 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3771 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003772 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003773
Johannes Berg9caf0362012-11-29 01:25:20 +01003774 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003775 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3776 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003777
3778 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3779 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3780 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3781 ht_oper = NULL;
3782 }
Johannes Berg24398e32012-03-28 10:58:36 +02003783 }
3784
Johannes Bergf2d9d272012-11-22 14:11:39 +01003785 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3786 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003787 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003788
Johannes Berg9caf0362012-11-29 01:25:20 +01003789 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3790 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003791 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3792 vht_oper = (void *)(vht_oper_ie + 2);
3793 if (vht_oper && !ht_oper) {
3794 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003795 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003796 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003797 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3798 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003799 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003800
3801 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3802 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3803 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3804 vht_oper = NULL;
3805 }
Johannes Berg24398e32012-03-28 10:58:36 +02003806 }
3807
Johannes Bergf2d9d272012-11-22 14:11:39 +01003808 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3809 cbss->channel,
3810 ht_oper, vht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02003811 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02003812
Johannes Bergf2d9d272012-11-22 14:11:39 +01003813 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3814 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003815
Johannes Berg9caf0362012-11-29 01:25:20 +01003816 rcu_read_unlock();
3817
Johannes Berg04ecd252012-09-11 14:34:12 +02003818 /* will change later if needed */
3819 sdata->smps_mode = IEEE80211_SMPS_OFF;
3820
Johannes Berg34a37402013-12-18 09:43:33 +01003821 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003822 /*
3823 * If this fails (possibly due to channel context sharing
3824 * on incompatible channels, e.g. 80+80 and 160 sharing the
3825 * same control channel) try to use a smaller bandwidth.
3826 */
3827 ret = ieee80211_vif_use_channel(sdata, &chandef,
3828 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02003829
3830 /* don't downgrade for 5 and 10 MHz channels, though. */
3831 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
3832 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01003833 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02003834
Johannes Bergd601cd82013-02-07 20:54:51 +01003835 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02003836 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003837 ret = ieee80211_vif_use_channel(sdata, &chandef,
3838 IEEE80211_CHANCTX_SHARED);
3839 }
Johannes Berg34a37402013-12-18 09:43:33 +01003840 out:
3841 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003842 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003843}
3844
3845static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3846 struct cfg80211_bss *cbss, bool assoc)
3847{
3848 struct ieee80211_local *local = sdata->local;
3849 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3850 struct ieee80211_bss *bss = (void *)cbss->priv;
3851 struct sta_info *new_sta = NULL;
3852 bool have_sta = false;
3853 int err;
3854
3855 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3856 return -EINVAL;
3857
3858 if (assoc) {
3859 rcu_read_lock();
3860 have_sta = sta_info_get(sdata, cbss->bssid);
3861 rcu_read_unlock();
3862 }
3863
3864 if (!have_sta) {
3865 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3866 if (!new_sta)
3867 return -ENOMEM;
3868 }
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003869 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003870 u32 rates = 0, basic_rates = 0;
3871 bool have_higher_than_11mbit;
3872 int min_rate = INT_MAX, min_rate_index = -1;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003873 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergb17166a2012-07-27 11:41:27 +02003874 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003875 const struct cfg80211_bss_ies *ies;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003876 int shift;
3877 u32 rate_flags;
Johannes Bergb17166a2012-07-27 11:41:27 +02003878
3879 sband = local->hw.wiphy->bands[cbss->channel->band];
3880
3881 err = ieee80211_prep_channel(sdata, cbss);
3882 if (err) {
3883 sta_info_free(local, new_sta);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003884 return -EINVAL;
Johannes Bergb17166a2012-07-27 11:41:27 +02003885 }
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003886 shift = ieee80211_vif_get_shift(&sdata->vif);
3887
3888 rcu_read_lock();
3889 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3890 if (WARN_ON(!chanctx_conf)) {
3891 rcu_read_unlock();
Eytan Lifshitzc368dda2014-02-06 21:01:32 +02003892 sta_info_free(local, new_sta);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003893 return -EINVAL;
3894 }
3895 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
3896 rcu_read_unlock();
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003897
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003898 ieee80211_get_rates(sband, bss->supp_rates,
3899 bss->supp_rates_len,
3900 &rates, &basic_rates,
3901 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003902 &min_rate, &min_rate_index,
3903 shift, rate_flags);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003904
3905 /*
3906 * This used to be a workaround for basic rates missing
3907 * in the association response frame. Now that we no
3908 * longer use the basic rates from there, it probably
3909 * doesn't happen any more, but keep the workaround so
3910 * in case some *other* APs are buggy in different ways
3911 * we can connect -- with a warning.
3912 */
3913 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003914 sdata_info(sdata,
3915 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003916 basic_rates = BIT(min_rate_index);
3917 }
3918
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003919 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003920 sdata->vif.bss_conf.basic_rates = basic_rates;
3921
3922 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003923 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003924 have_higher_than_11mbit)
3925 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3926 else
3927 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3928
3929 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3930
Johannes Berg8c358bc2012-05-22 22:13:05 +02003931 /* set timing information */
3932 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003933 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003934 ies = rcu_dereference(cbss->beacon_ies);
3935 if (ies) {
3936 const u8 *tim_ie;
3937
3938 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3939 sdata->vif.bss_conf.sync_device_ts =
3940 bss->device_ts_beacon;
3941 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3942 ies->data, ies->len);
3943 if (tim_ie && tim_ie[1] >= 2)
3944 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3945 else
3946 sdata->vif.bss_conf.sync_dtim_count = 0;
3947 } else if (!(local->hw.flags &
3948 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3949 ies = rcu_dereference(cbss->proberesp_ies);
3950 /* must be non-NULL since beacon IEs were NULL */
3951 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3952 sdata->vif.bss_conf.sync_device_ts =
3953 bss->device_ts_presp;
3954 sdata->vif.bss_conf.sync_dtim_count = 0;
3955 } else {
3956 sdata->vif.bss_conf.sync_tsf = 0;
3957 sdata->vif.bss_conf.sync_device_ts = 0;
3958 sdata->vif.bss_conf.sync_dtim_count = 0;
3959 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003960 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003961
3962 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003963 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003964 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3965 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003966
3967 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003968 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003969
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003970 err = sta_info_insert(new_sta);
3971 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003972 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003973 sdata_info(sdata,
3974 "failed to insert STA entry for the AP (error %d)\n",
3975 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003976 return err;
3977 }
3978 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003979 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003980
3981 return 0;
3982}
3983
Johannes Berg77fdaa12009-07-07 03:45:17 +02003984/* config hooks */
3985int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3986 struct cfg80211_auth_request *req)
3987{
Johannes Berg66e67e42012-01-20 13:55:27 +01003988 struct ieee80211_local *local = sdata->local;
3989 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3990 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003991 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003992 int err;
3993
3994 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003995
3996 switch (req->auth_type) {
3997 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3998 auth_alg = WLAN_AUTH_OPEN;
3999 break;
4000 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01004001 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02004002 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004003 auth_alg = WLAN_AUTH_SHARED_KEY;
4004 break;
4005 case NL80211_AUTHTYPE_FT:
4006 auth_alg = WLAN_AUTH_FT;
4007 break;
4008 case NL80211_AUTHTYPE_NETWORK_EAP:
4009 auth_alg = WLAN_AUTH_LEAP;
4010 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004011 case NL80211_AUTHTYPE_SAE:
4012 auth_alg = WLAN_AUTH_SAE;
4013 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004014 default:
4015 return -EOPNOTSUPP;
4016 }
4017
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004018 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
4019 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01004020 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004021 return -ENOMEM;
4022
Johannes Berg66e67e42012-01-20 13:55:27 +01004023 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004024
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004025 if (req->sae_data_len >= 4) {
4026 __le16 *pos = (__le16 *) req->sae_data;
4027 auth_data->sae_trans = le16_to_cpu(pos[0]);
4028 auth_data->sae_status = le16_to_cpu(pos[1]);
4029 memcpy(auth_data->data, req->sae_data + 4,
4030 req->sae_data_len - 4);
4031 auth_data->data_len += req->sae_data_len - 4;
4032 }
4033
Johannes Berg77fdaa12009-07-07 03:45:17 +02004034 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004035 memcpy(&auth_data->data[auth_data->data_len],
4036 req->ie, req->ie_len);
4037 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004038 }
4039
Johannes Bergfffd0932009-07-08 14:22:54 +02004040 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004041 auth_data->key_len = req->key_len;
4042 auth_data->key_idx = req->key_idx;
4043 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02004044 }
4045
Johannes Berg66e67e42012-01-20 13:55:27 +01004046 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004047
Johannes Berg66e67e42012-01-20 13:55:27 +01004048 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01004049
Johannes Berg66e67e42012-01-20 13:55:27 +01004050 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4051 ifmgd->assoc_data) {
4052 err = -EBUSY;
4053 goto err_free;
4054 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004055
Johannes Berg66e67e42012-01-20 13:55:27 +01004056 if (ifmgd->auth_data)
4057 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03004058
Johannes Berg66e67e42012-01-20 13:55:27 +01004059 /* prep auth_data so we don't go into idle on disassoc */
4060 ifmgd->auth_data = auth_data;
4061
Johannes Berg7b119dc2013-04-10 21:38:36 +02004062 if (ifmgd->associated) {
4063 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4064
4065 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4066 WLAN_REASON_UNSPECIFIED,
4067 false, frame_buf);
4068
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004069 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4070 sizeof(frame_buf));
Johannes Berg7b119dc2013-04-10 21:38:36 +02004071 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004072
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004073 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004074
Johannes Berga1cf7752012-03-08 15:02:07 +01004075 err = ieee80211_prep_connection(sdata, req->bss, false);
4076 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01004077 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004078
Johannes Berg66e67e42012-01-20 13:55:27 +01004079 err = ieee80211_probe_auth(sdata);
4080 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004081 sta_info_destroy_addr(sdata, req->bss->bssid);
4082 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004083 }
4084
Johannes Berg66e67e42012-01-20 13:55:27 +01004085 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01004086 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004087 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02004088
Johannes Berg66e67e42012-01-20 13:55:27 +01004089 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004090 memset(ifmgd->bssid, 0, ETH_ALEN);
4091 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004092 ifmgd->auth_data = NULL;
4093 err_free:
4094 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01004095 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004096}
4097
Johannes Berg095d81c2013-10-15 12:25:07 +02004098static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
4099 const u8 *wmm_param, int len)
4100{
4101 const u8 *pos;
4102 size_t left;
4103
4104 if (len < 8)
4105 return false;
4106
4107 if (wmm_param[5] != 1 /* version */)
4108 return false;
4109
4110 pos = wmm_param + 8;
4111 left = len - 8;
4112
4113 for (; left >= 4; left -= 4, pos += 4) {
4114 u8 aifsn = pos[0] & 0x0f;
4115 u8 ecwmin = pos[1] & 0x0f;
4116 u8 ecwmax = (pos[1] & 0xf0) >> 4;
4117 int aci = (pos[0] >> 5) & 0x03;
4118
4119 if (aifsn < 2) {
4120 sdata_info(sdata,
4121 "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
4122 aifsn, aci);
4123 return false;
4124 }
4125 if (ecwmin > ecwmax) {
4126 sdata_info(sdata,
4127 "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
4128 ecwmin, ecwmax, aci);
4129 return false;
4130 }
4131 }
4132
4133 return true;
4134}
4135
Johannes Berg77fdaa12009-07-07 03:45:17 +02004136int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4137 struct cfg80211_assoc_request *req)
4138{
Johannes Berg66e67e42012-01-20 13:55:27 +01004139 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004140 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004141 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004142 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004143 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004144 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004145 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004146 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004147
Johannes Berg66e67e42012-01-20 13:55:27 +01004148 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4149 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004150 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004151
Johannes Berg9caf0362012-11-29 01:25:20 +01004152 rcu_read_lock();
4153 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4154 if (!ssidie) {
4155 rcu_read_unlock();
4156 kfree(assoc_data);
4157 return -EINVAL;
4158 }
4159 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4160 assoc_data->ssid_len = ssidie[1];
4161 rcu_read_unlock();
4162
Johannes Berg7b119dc2013-04-10 21:38:36 +02004163 if (ifmgd->associated) {
4164 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4165
4166 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4167 WLAN_REASON_UNSPECIFIED,
4168 false, frame_buf);
4169
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004170 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4171 sizeof(frame_buf));
Johannes Berg7b119dc2013-04-10 21:38:36 +02004172 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004173
4174 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4175 err = -EBUSY;
4176 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004177 }
4178
Johannes Berg66e67e42012-01-20 13:55:27 +01004179 if (ifmgd->assoc_data) {
4180 err = -EBUSY;
4181 goto err_free;
4182 }
4183
4184 if (ifmgd->auth_data) {
4185 bool match;
4186
4187 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004188 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004189 ieee80211_destroy_auth_data(sdata, match);
4190 }
4191
4192 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02004193
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004194 ifmgd->beacon_crc_valid = false;
4195
Johannes Berg095d81c2013-10-15 12:25:07 +02004196 assoc_data->wmm = bss->wmm_used &&
4197 (local->hw.queues >= IEEE80211_NUM_ACS);
4198 if (assoc_data->wmm) {
4199 /* try to check validity of WMM params IE */
4200 const struct cfg80211_bss_ies *ies;
4201 const u8 *wp, *start, *end;
4202
4203 rcu_read_lock();
4204 ies = rcu_dereference(req->bss->ies);
4205 start = ies->data;
4206 end = start + ies->len;
4207
4208 while (true) {
4209 wp = cfg80211_find_vendor_ie(
4210 WLAN_OUI_MICROSOFT,
4211 WLAN_OUI_TYPE_MICROSOFT_WMM,
4212 start, end - start);
4213 if (!wp)
4214 break;
4215 start = wp + wp[1] + 2;
4216 /* if this IE is too short, try the next */
4217 if (wp[1] <= 4)
4218 continue;
4219 /* if this IE is WMM params, we found what we wanted */
4220 if (wp[6] == 1)
4221 break;
4222 }
4223
4224 if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
4225 wp[1] - 2)) {
4226 assoc_data->wmm = false;
4227 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
4228 }
4229 rcu_read_unlock();
4230 }
4231
Johannes Bergde5036a2012-03-08 15:02:03 +01004232 /*
4233 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4234 * We still associate in non-HT mode (11a/b/g) if any one of these
4235 * ciphers is configured as pairwise.
4236 * We can set this to true for non-11n hardware, that'll be checked
4237 * separately along with the peer capabilities.
4238 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004239 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004240 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4241 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004242 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004243 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004244 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004245 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004246 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004247 }
4248 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004249
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004250 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004251 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004252 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4253 }
Ben Greearef96a8422011-11-18 11:32:00 -08004254
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004255 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4256 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4257
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004258 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4259 sband = local->hw.wiphy->bands[req->bss->channel->band];
4260 if (!sband->ht_cap.ht_supported ||
Johannes Berg095d81c2013-10-15 12:25:07 +02004261 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4262 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Berga8243b72012-11-22 14:32:09 +01004263 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg095d81c2013-10-15 12:25:07 +02004264 if (!bss->wmm_used &&
4265 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
Johannes Berg1b49de22012-07-27 10:29:14 +02004266 netdev_info(sdata->dev,
4267 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004268 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004269
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004270 /* disable VHT if we don't support it or the AP doesn't use WMM */
4271 if (!sband->vht_cap.vht_supported ||
Johannes Berg095d81c2013-10-15 12:25:07 +02004272 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4273 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004274 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg095d81c2013-10-15 12:25:07 +02004275 if (!bss->wmm_used &&
4276 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
Johannes Berg1b49de22012-07-27 10:29:14 +02004277 netdev_info(sdata->dev,
4278 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004279 }
4280
Ben Greearef96a8422011-11-18 11:32:00 -08004281 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4282 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4283 sizeof(ifmgd->ht_capa_mask));
4284
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004285 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4286 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4287 sizeof(ifmgd->vht_capa_mask));
4288
Johannes Berg77fdaa12009-07-07 03:45:17 +02004289 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004290 memcpy(assoc_data->ie, req->ie, req->ie_len);
4291 assoc_data->ie_len = req->ie_len;
4292 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004293
Johannes Berg66e67e42012-01-20 13:55:27 +01004294 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004295
Johannes Bergaf6b6372009-12-23 13:15:35 +01004296 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4297 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004298 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004299 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004300 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004301 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004302 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004303
Johannes Berg66e67e42012-01-20 13:55:27 +01004304 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01004305 assoc_data->supp_rates = bss->supp_rates;
4306 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004307
Johannes Berg9caf0362012-11-29 01:25:20 +01004308 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004309 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4310 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4311 assoc_data->ap_ht_param =
4312 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4313 else
Johannes Berga8243b72012-11-22 14:32:09 +01004314 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004315 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4316 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4317 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4318 sizeof(struct ieee80211_vht_cap));
4319 else
4320 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004321 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004322
Kalle Valoab133152010-01-12 10:42:31 +02004323 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004324 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4325 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004326 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004327 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4328 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004329 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004330 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4331 }
4332
Johannes Berg77fdaa12009-07-07 03:45:17 +02004333 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004334 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004335
4336 if (req->use_mfp) {
4337 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4338 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4339 } else {
4340 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4341 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4342 }
4343
4344 if (req->crypto.control_port)
4345 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4346 else
4347 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4348
Johannes Berga621fa42010-08-27 14:26:54 +03004349 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4350 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02004351 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
4352 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03004353
Johannes Berg66e67e42012-01-20 13:55:27 +01004354 /* kick off associate process */
4355
4356 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004357 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03004358 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01004359
Johannes Berga1cf7752012-03-08 15:02:07 +01004360 err = ieee80211_prep_connection(sdata, req->bss, true);
4361 if (err)
4362 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004363
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004364 rcu_read_lock();
4365 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004366
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004367 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4368 !beacon_ies) {
4369 /*
4370 * Wait up to one beacon interval ...
4371 * should this be more if we miss one?
4372 */
4373 sdata_info(sdata, "waiting for beacon from %pM\n",
4374 ifmgd->bssid);
4375 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004376 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004377 assoc_data->need_beacon = true;
4378 } else if (beacon_ies) {
4379 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4380 beacon_ies->data,
4381 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004382 u8 dtim_count = 0;
4383
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004384 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4385 const struct ieee80211_tim_ie *tim;
4386 tim = (void *)(tim_ie + 2);
4387 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004388 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004389 }
Alexander Bondar989c6502013-05-16 17:34:17 +03004390 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004391 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004392 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004393
4394 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4395 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4396 sdata->vif.bss_conf.sync_device_ts =
4397 bss->device_ts_beacon;
4398 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4399 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004400 } else {
4401 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004402 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004403 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004404 rcu_read_unlock();
4405
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004406 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004407
Paul Stewartfcff4f12012-02-23 17:59:53 -08004408 if (bss->corrupt_data) {
4409 char *corrupt_type = "data";
4410 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4411 if (bss->corrupt_data &
4412 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4413 corrupt_type = "beacon and probe response";
4414 else
4415 corrupt_type = "beacon";
4416 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4417 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004418 sdata_info(sdata, "associating with AP with corrupt %s\n",
4419 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004420 }
4421
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004422 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004423 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004424 memset(ifmgd->bssid, 0, ETH_ALEN);
4425 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004426 ifmgd->assoc_data = NULL;
4427 err_free:
4428 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01004429 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004430}
4431
4432int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004433 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004434{
4435 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004436 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004437 bool tx = !req->local_state_change;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004438
Johannes Bergc9c3a062014-03-19 09:11:19 +01004439 if (ifmgd->auth_data &&
4440 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
4441 sdata_info(sdata,
4442 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
4443 req->bssid, req->reason_code,
4444 ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02004445
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004446 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004447 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4448 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004449 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004450 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004451 ieee80211_destroy_auth_data(sdata, false);
Johannes Bergc9c3a062014-03-19 09:11:19 +01004452 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4453 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg86552012012-10-29 09:46:31 +01004454
Johannes Bergc9c3a062014-03-19 09:11:19 +01004455 return 0;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004456 }
4457
Johannes Berg86552012012-10-29 09:46:31 +01004458 if (ifmgd->associated &&
4459 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Bergc9c3a062014-03-19 09:11:19 +01004460 sdata_info(sdata,
4461 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
4462 req->bssid, req->reason_code,
4463 ieee80211_get_reason_code_string(req->reason_code));
4464
Johannes Berg86552012012-10-29 09:46:31 +01004465 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4466 req->reason_code, tx, frame_buf);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004467 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4468 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergc9c3a062014-03-19 09:11:19 +01004469 return 0;
4470 }
Johannes Berg86552012012-10-29 09:46:31 +01004471
Johannes Bergc9c3a062014-03-19 09:11:19 +01004472 return -ENOTCONN;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004473}
4474
4475int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004476 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004477{
4478 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004479 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004480 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004481
Johannes Berg8d8b2612009-07-25 11:58:36 +02004482 /*
4483 * cfg80211 should catch this ... but it's racy since
4484 * we can receive a disassoc frame, process it, hand it
4485 * to cfg80211 while that's in a locked section already
4486 * trying to tell us that the user wants to disconnect.
4487 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004488 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004489 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004490
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004491 sdata_info(sdata,
Calvin Owensdfa1ad22014-02-11 12:36:24 -06004492 "disassociating from %pM by local choice (Reason: %u=%s)\n",
4493 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
Johannes Berg0ff71612009-09-26 14:45:41 +02004494
Jouni Malinene69e95d2010-03-29 23:29:31 -07004495 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004496 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4497 req->reason_code, !req->local_state_change,
4498 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004499
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004500 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4501 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004502
Johannes Berg77fdaa12009-07-07 03:45:17 +02004503 return 0;
4504}
Jouni Malinen026331c2010-02-15 12:53:10 +02004505
Eliad Pellerafa762f2012-04-23 14:45:15 +03004506void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004507{
4508 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4509
Ben Greear49921852013-02-20 09:41:09 -08004510 /*
4511 * Make sure some work items will not run after this,
4512 * they will not do anything but might not have been
4513 * cancelled when disconnecting.
4514 */
4515 cancel_work_sync(&ifmgd->monitor_work);
4516 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4517 cancel_work_sync(&ifmgd->request_smps_work);
4518 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4519 cancel_work_sync(&ifmgd->chswitch_work);
4520
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004521 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02004522 if (ifmgd->assoc_data) {
4523 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004524 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004525 cfg80211_assoc_timeout(sdata->dev, bss);
4526 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004527 if (ifmgd->auth_data)
4528 ieee80211_destroy_auth_data(sdata, false);
4529 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004530 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004531}
4532
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004533void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4534 enum nl80211_cqm_rssi_threshold_event rssi_event,
4535 gfp_t gfp)
4536{
4537 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4538
Johannes Bergb5878a22010-04-07 16:48:40 +02004539 trace_api_cqm_rssi_notify(sdata, rssi_event);
4540
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004541 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4542}
4543EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);