blob: 741448b308257c3a6f01c3d09ca36d7ddaa31ca3 [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)
34#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
35#define IEEE80211_AUTH_MAX_TRIES 3
36#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
39#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static int max_nullfunc_tries = 2;
42module_param(max_nullfunc_tries, int, 0644);
43MODULE_PARM_DESC(max_nullfunc_tries,
44 "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46static int max_probe_tries = 5;
47module_param(max_probe_tries, int, 0644);
48MODULE_PARM_DESC(max_probe_tries,
49 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * Beacon loss timeout is calculated as N frames times the
53 * advertised beacon interval. This may need to be somewhat
54 * higher than what hardware might detect to account for
55 * delays in the host processing frames. But since we also
56 * probe on beacon miss before declaring the connection lost
57 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Ben Greear59c1ec22013-03-19 14:19:56 -070059static int beacon_loss_count = 7;
60module_param(beacon_loss_count, int, 0644);
61MODULE_PARM_DESC(beacon_loss_count,
62 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010063
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time the connection can be idle before we probe
66 * it to see if we can still talk to the AP.
67 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030068#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020069/*
70 * Time we wait for a probe response after sending
71 * a probe request because of beacon loss or for
72 * checking the connection still works.
73 */
Ben Greear180205b2011-02-04 15:30:24 -080074static int probe_wait_ms = 500;
75module_param(probe_wait_ms, int, 0644);
76MODULE_PARM_DESC(probe_wait_ms,
77 "Maximum time(ms) to wait for probe response"
78 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070079
Jouni Malinen17e4ec12010-03-29 23:28:30 -070080/*
81 * Weight given to the latest Beacon frame when calculating average signal
82 * strength for Beacon frames received in the current BSS. This must be
83 * between 1 and 15.
84 */
85#define IEEE80211_SIGNAL_AVE_WEIGHT 3
86
Jouni Malinen391a2002010-08-27 22:22:00 +030087/*
88 * How many Beacon frames need to have been used in average signal strength
89 * before starting to indicate signal change events.
90 */
91#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100117
118 /* used when a processed beacon causes a deauth */
119 RX_MGMT_CFG80211_TX_DEAUTH,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120};
121
Johannes Berg5484e232008-09-08 17:44:27 +0200122/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
124{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200125 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200126}
127
Johannes Bergca386f32009-07-10 02:39:48 +0200128/*
129 * We can have multiple work items (and connection probing)
130 * scheduling this timer, but we need to take care to only
131 * reschedule it when it should fire _earlier_ than it was
132 * asked for before, or if it's not pending right now. This
133 * function ensures that. Note that it then is required to
134 * run this function for all timeouts after the first one
135 * has happened -- the work that runs from this timer will
136 * do that.
137 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100138static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200139{
140 ASSERT_MGD_MTX(ifmgd);
141
142 if (!timer_pending(&ifmgd->timer) ||
143 time_before(timeout, ifmgd->timer.expires))
144 mod_timer(&ifmgd->timer, timeout);
145}
146
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400147void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200148{
Johannes Bergc1288b12012-01-19 09:29:57 +0100149 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 return;
151
Johannes Berg7eeff742012-07-18 10:27:27 +0200152 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
153 return;
154
Johannes Bergb291ba12009-07-10 15:29:03 +0200155 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100156 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200157}
158
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400159void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
160{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400161 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
162
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100163 if (unlikely(!sdata->u.mgd.associated))
164 return;
165
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400166 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
167 return;
168
169 mod_timer(&sdata->u.mgd.conn_mon_timer,
170 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400171
172 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400173}
174
Johannes Berg5484e232008-09-08 17:44:27 +0200175static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200176{
Johannes Berg5484e232008-09-08 17:44:27 +0200177 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200178}
179
Johannes Berg6565ec92013-02-08 14:52:32 +0100180static u32 chandef_downgrade(struct cfg80211_chan_def *c)
181{
182 u32 ret;
183 int tmp;
184
185 switch (c->width) {
186 case NL80211_CHAN_WIDTH_20:
187 c->width = NL80211_CHAN_WIDTH_20_NOHT;
188 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
189 break;
190 case NL80211_CHAN_WIDTH_40:
191 c->width = NL80211_CHAN_WIDTH_20;
192 c->center_freq1 = c->chan->center_freq;
193 ret = IEEE80211_STA_DISABLE_40MHZ |
194 IEEE80211_STA_DISABLE_VHT;
195 break;
196 case NL80211_CHAN_WIDTH_80:
197 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
198 /* n_P40 */
199 tmp /= 2;
200 /* freq_P40 */
201 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
202 c->width = NL80211_CHAN_WIDTH_40;
203 ret = IEEE80211_STA_DISABLE_VHT;
204 break;
205 case NL80211_CHAN_WIDTH_80P80:
206 c->center_freq2 = 0;
207 c->width = NL80211_CHAN_WIDTH_80;
208 ret = IEEE80211_STA_DISABLE_80P80MHZ |
209 IEEE80211_STA_DISABLE_160MHZ;
210 break;
211 case NL80211_CHAN_WIDTH_160:
212 /* n_P20 */
213 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
214 /* n_P80 */
215 tmp /= 4;
216 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
217 c->width = NL80211_CHAN_WIDTH_80;
218 ret = IEEE80211_STA_DISABLE_80P80MHZ |
219 IEEE80211_STA_DISABLE_160MHZ;
220 break;
221 default:
222 case NL80211_CHAN_WIDTH_20_NOHT:
223 WARN_ON_ONCE(1);
224 c->width = NL80211_CHAN_WIDTH_20_NOHT;
225 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
226 break;
227 }
228
229 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
230
231 return ret;
232}
233
234static u32
235ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
236 struct ieee80211_supported_band *sband,
237 struct ieee80211_channel *channel,
238 const struct ieee80211_ht_operation *ht_oper,
239 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100240 struct cfg80211_chan_def *chandef, bool verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100241{
242 struct cfg80211_chan_def vht_chandef;
243 u32 ht_cfreq, ret;
244
245 chandef->chan = channel;
246 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
247 chandef->center_freq1 = channel->center_freq;
248 chandef->center_freq2 = 0;
249
250 if (!ht_oper || !sband->ht_cap.ht_supported) {
251 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
252 goto out;
253 }
254
255 chandef->width = NL80211_CHAN_WIDTH_20;
256
257 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
258 channel->band);
259 /* check that channel matches the right operating channel */
260 if (channel->center_freq != ht_cfreq) {
261 /*
262 * It's possible that some APs are confused here;
263 * Netgear WNDR3700 sometimes reports 4 higher than
264 * the actual channel in association responses, but
265 * since we look at probe response/beacon data here
266 * it should be OK.
267 */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100268 if (verbose)
269 sdata_info(sdata,
270 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
271 channel->center_freq, ht_cfreq,
272 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100273 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
274 goto out;
275 }
276
277 /* check 40 MHz support, if we have it */
278 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
279 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
280 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
281 chandef->width = NL80211_CHAN_WIDTH_40;
282 chandef->center_freq1 += 10;
283 break;
284 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
285 chandef->width = NL80211_CHAN_WIDTH_40;
286 chandef->center_freq1 -= 10;
287 break;
288 }
289 } else {
290 /* 40 MHz (and 80 MHz) must be supported for VHT */
291 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100292 /* also mark 40 MHz disabled */
293 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100294 goto out;
295 }
296
297 if (!vht_oper || !sband->vht_cap.vht_supported) {
298 ret = IEEE80211_STA_DISABLE_VHT;
299 goto out;
300 }
301
302 vht_chandef.chan = channel;
303 vht_chandef.center_freq1 =
304 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
305 channel->band);
306 vht_chandef.center_freq2 = 0;
307
Johannes Berg6565ec92013-02-08 14:52:32 +0100308 switch (vht_oper->chan_width) {
309 case IEEE80211_VHT_CHANWIDTH_USE_HT:
310 vht_chandef.width = chandef->width;
311 break;
312 case IEEE80211_VHT_CHANWIDTH_80MHZ:
313 vht_chandef.width = NL80211_CHAN_WIDTH_80;
314 break;
315 case IEEE80211_VHT_CHANWIDTH_160MHZ:
316 vht_chandef.width = NL80211_CHAN_WIDTH_160;
317 break;
318 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
319 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
Johannes Berg6553bf042013-03-28 10:26:17 +0100320 vht_chandef.center_freq2 =
321 ieee80211_channel_to_frequency(
322 vht_oper->center_freq_seg2_idx,
323 channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100324 break;
325 default:
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100326 if (verbose)
327 sdata_info(sdata,
328 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
329 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100330 ret = IEEE80211_STA_DISABLE_VHT;
331 goto out;
332 }
333
334 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100335 if (verbose)
336 sdata_info(sdata,
337 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100338 ret = IEEE80211_STA_DISABLE_VHT;
339 goto out;
340 }
341
342 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
343 ret = 0;
344 goto out;
345 }
346
347 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100348 if (verbose)
349 sdata_info(sdata,
350 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100351 ret = IEEE80211_STA_DISABLE_VHT;
352 goto out;
353 }
354
355 *chandef = vht_chandef;
356
357 ret = 0;
358
359out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100360 /* don't print the message below for VHT mismatch if VHT is disabled */
361 if (ret & IEEE80211_STA_DISABLE_VHT)
362 vht_chandef = *chandef;
363
Johannes Berg6565ec92013-02-08 14:52:32 +0100364 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
365 IEEE80211_CHAN_DISABLED)) {
366 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
367 ret = IEEE80211_STA_DISABLE_HT |
368 IEEE80211_STA_DISABLE_VHT;
369 goto out;
370 }
371
372 ret |= chandef_downgrade(chandef);
373 }
374
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100375 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100376 sdata_info(sdata,
377 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
378
379 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
380 return ret;
381}
382
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100383static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
384 struct sta_info *sta,
385 const struct ieee80211_ht_operation *ht_oper,
386 const struct ieee80211_vht_operation *vht_oper,
387 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200388{
389 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100390 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200391 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200392 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100393 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200394 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100395 u32 flags;
396 enum ieee80211_sta_rx_bandwidth new_sta_bw;
397 int ret;
398
399 /* if HT was/is disabled, don't track any bandwidth changes */
400 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
401 return 0;
402
403 /* don't check VHT if we associated as non-VHT station */
404 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
405 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200406
Johannes Berg11289582013-02-07 17:36:12 +0100407 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100408 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100409
Johannes Bergf2d93302013-02-08 13:15:55 +0100410 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200411 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200412
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100413 /* calculate new channel (type) based on HT/VHT operation IEs */
414 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
415 vht_oper, &chandef, false);
416
417 /*
418 * Downgrade the new channel if we associated with restricted
419 * capabilities. For example, if we associated as a 20 MHz STA
420 * to a 40 MHz AP (due to regulatory, capabilities or config
421 * reasons) then switching to a 40 MHz channel now won't do us
422 * any good -- we couldn't use it with the AP.
423 */
424 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
425 chandef.width == NL80211_CHAN_WIDTH_80P80)
426 flags |= chandef_downgrade(&chandef);
427 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
428 chandef.width == NL80211_CHAN_WIDTH_160)
429 flags |= chandef_downgrade(&chandef);
430 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
431 chandef.width > NL80211_CHAN_WIDTH_20)
432 flags |= chandef_downgrade(&chandef);
433
434 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
435 return 0;
436
437 sdata_info(sdata,
438 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
439 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
440 chandef.center_freq1, chandef.center_freq2);
441
442 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
443 IEEE80211_STA_DISABLE_VHT |
444 IEEE80211_STA_DISABLE_40MHZ |
445 IEEE80211_STA_DISABLE_80P80MHZ |
446 IEEE80211_STA_DISABLE_160MHZ)) ||
447 !cfg80211_chandef_valid(&chandef)) {
448 sdata_info(sdata,
449 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
450 ifmgd->bssid);
451 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200452 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200453
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100454 switch (chandef.width) {
455 case NL80211_CHAN_WIDTH_20_NOHT:
456 case NL80211_CHAN_WIDTH_20:
457 new_sta_bw = IEEE80211_STA_RX_BW_20;
458 break;
459 case NL80211_CHAN_WIDTH_40:
460 new_sta_bw = IEEE80211_STA_RX_BW_40;
461 break;
462 case NL80211_CHAN_WIDTH_80:
463 new_sta_bw = IEEE80211_STA_RX_BW_80;
464 break;
465 case NL80211_CHAN_WIDTH_80P80:
466 case NL80211_CHAN_WIDTH_160:
467 new_sta_bw = IEEE80211_STA_RX_BW_160;
468 break;
469 default:
470 return -EINVAL;
471 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200472
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100473 if (new_sta_bw > sta->cur_max_bandwidth)
474 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200475
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100476 if (new_sta_bw < sta->sta.bandwidth) {
477 sta->sta.bandwidth = new_sta_bw;
478 rate_control_rate_update(local, sband, sta,
479 IEEE80211_RC_BW_CHANGED);
480 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530481
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100482 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
483 if (ret) {
484 sdata_info(sdata,
485 "AP %pM changed bandwidth to incompatible one - disconnect\n",
486 ifmgd->bssid);
487 return ret;
488 }
489
490 if (new_sta_bw > sta->sta.bandwidth) {
491 sta->sta.bandwidth = new_sta_bw;
492 rate_control_rate_update(local, sband, sta,
493 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200494 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200495
Johannes Berg074d46d2012-03-15 19:45:16 +0100496 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200497
498 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100499 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
500 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200501 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200502 }
503
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100504 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200505}
506
Johannes Berg9c6bd792008-09-11 00:01:52 +0200507/* frame sending functions */
508
Johannes Berg66e67e42012-01-20 13:55:27 +0100509static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
510 struct ieee80211_supported_band *sband,
511 u32 *rates)
512{
513 int i, j, count;
514 *rates = 0;
515 count = 0;
516 for (i = 0; i < supp_rates_len; i++) {
517 int rate = (supp_rates[i] & 0x7F) * 5;
518
519 for (j = 0; j < sband->n_bitrates; j++)
520 if (sband->bitrates[j].bitrate == rate) {
521 *rates |= BIT(j);
522 count++;
523 break;
524 }
525 }
526
527 return count;
528}
529
530static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200531 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100532 struct ieee80211_supported_band *sband,
533 struct ieee80211_channel *channel,
534 enum ieee80211_smps_mode smps)
535{
Johannes Berg66e67e42012-01-20 13:55:27 +0100536 u8 *pos;
537 u32 flags = channel->flags;
538 u16 cap;
539 struct ieee80211_sta_ht_cap ht_cap;
540
541 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
542
Johannes Berg66e67e42012-01-20 13:55:27 +0100543 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
544 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
545
Johannes Berg66e67e42012-01-20 13:55:27 +0100546 /* determine capability flags */
547 cap = ht_cap.cap;
548
Johannes Berg9dde6422012-05-16 23:43:19 +0200549 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100550 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
551 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
552 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
553 cap &= ~IEEE80211_HT_CAP_SGI_40;
554 }
555 break;
556 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
557 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
558 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
559 cap &= ~IEEE80211_HT_CAP_SGI_40;
560 }
561 break;
562 }
563
Johannes Berg24398e32012-03-28 10:58:36 +0200564 /*
565 * If 40 MHz was disabled associate as though we weren't
566 * capable of 40 MHz -- some broken APs will never fall
567 * back to trying to transmit in 20 MHz.
568 */
569 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
570 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
571 cap &= ~IEEE80211_HT_CAP_SGI_40;
572 }
573
Johannes Berg66e67e42012-01-20 13:55:27 +0100574 /* set SM PS mode properly */
575 cap &= ~IEEE80211_HT_CAP_SM_PS;
576 switch (smps) {
577 case IEEE80211_SMPS_AUTOMATIC:
578 case IEEE80211_SMPS_NUM_MODES:
579 WARN_ON(1);
580 case IEEE80211_SMPS_OFF:
581 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
582 IEEE80211_HT_CAP_SM_PS_SHIFT;
583 break;
584 case IEEE80211_SMPS_STATIC:
585 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
586 IEEE80211_HT_CAP_SM_PS_SHIFT;
587 break;
588 case IEEE80211_SMPS_DYNAMIC:
589 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
590 IEEE80211_HT_CAP_SM_PS_SHIFT;
591 break;
592 }
593
594 /* reserve and fill IE */
595 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
596 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
597}
598
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
600 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100601 struct ieee80211_supported_band *sband,
602 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000603{
604 u8 *pos;
605 u32 cap;
606 struct ieee80211_sta_vht_cap vht_cap;
607
608 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
609
610 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100611 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000612
613 /* determine capability flags */
614 cap = vht_cap.cap;
615
Johannes Bergf2d9d272012-11-22 14:11:39 +0100616 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
617 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
618 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
619 }
620
621 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
622 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
623 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
624 }
625
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100626 /*
627 * Some APs apparently get confused if our capabilities are better
628 * than theirs, so restrict what we advertise in the assoc request.
629 */
630 if (!(ap_vht_cap->vht_cap_info &
631 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
632 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
633
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000634 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000635 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000636 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
637}
638
Johannes Berg66e67e42012-01-20 13:55:27 +0100639static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
640{
641 struct ieee80211_local *local = sdata->local;
642 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
643 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
644 struct sk_buff *skb;
645 struct ieee80211_mgmt *mgmt;
646 u8 *pos, qos_info;
647 size_t offset = 0, noffset;
648 int i, count, rates_len, supp_rates_len;
649 u16 capab;
650 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200651 struct ieee80211_chanctx_conf *chanctx_conf;
652 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100653 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100654
655 lockdep_assert_held(&ifmgd->mtx);
656
Johannes Berg55de9082012-07-26 17:24:39 +0200657 rcu_read_lock();
658 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
659 if (WARN_ON(!chanctx_conf)) {
660 rcu_read_unlock();
661 return;
662 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100663 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200664 rcu_read_unlock();
665 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100666
667 if (assoc_data->supp_rates_len) {
668 /*
669 * Get all rates supported by the device and the AP as
670 * some APs don't like getting a superset of their rates
671 * in the association request (e.g. D-Link DAP 1353 in
672 * b-only mode)...
673 */
674 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
675 assoc_data->supp_rates_len,
676 sband, &rates);
677 } else {
678 /*
679 * In case AP not provide any supported rates information
680 * before association, we send information element(s) with
681 * all rates that we support.
682 */
683 rates = ~0;
684 rates_len = sband->n_bitrates;
685 }
686
687 skb = alloc_skb(local->hw.extra_tx_headroom +
688 sizeof(*mgmt) + /* bit too much but doesn't matter */
689 2 + assoc_data->ssid_len + /* SSID */
690 4 + rates_len + /* (extended) rates */
691 4 + /* power capability */
692 2 + 2 * sband->n_channels + /* supported channels */
693 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000694 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100695 assoc_data->ie_len + /* extra IEs */
696 9, /* WMM */
697 GFP_KERNEL);
698 if (!skb)
699 return;
700
701 skb_reserve(skb, local->hw.extra_tx_headroom);
702
703 capab = WLAN_CAPABILITY_ESS;
704
705 if (sband->band == IEEE80211_BAND_2GHZ) {
706 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
707 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
708 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
709 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
710 }
711
712 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
713 capab |= WLAN_CAPABILITY_PRIVACY;
714
715 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
716 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
717 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
718
719 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
720 memset(mgmt, 0, 24);
721 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
722 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
723 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
724
725 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
726 skb_put(skb, 10);
727 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
728 IEEE80211_STYPE_REASSOC_REQ);
729 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
730 mgmt->u.reassoc_req.listen_interval =
731 cpu_to_le16(local->hw.conf.listen_interval);
732 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
733 ETH_ALEN);
734 } else {
735 skb_put(skb, 4);
736 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
737 IEEE80211_STYPE_ASSOC_REQ);
738 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
739 mgmt->u.assoc_req.listen_interval =
740 cpu_to_le16(local->hw.conf.listen_interval);
741 }
742
743 /* SSID */
744 pos = skb_put(skb, 2 + assoc_data->ssid_len);
745 *pos++ = WLAN_EID_SSID;
746 *pos++ = assoc_data->ssid_len;
747 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
748
749 /* add all rates which were marked to be used above */
750 supp_rates_len = rates_len;
751 if (supp_rates_len > 8)
752 supp_rates_len = 8;
753
754 pos = skb_put(skb, supp_rates_len + 2);
755 *pos++ = WLAN_EID_SUPP_RATES;
756 *pos++ = supp_rates_len;
757
758 count = 0;
759 for (i = 0; i < sband->n_bitrates; i++) {
760 if (BIT(i) & rates) {
761 int rate = sband->bitrates[i].bitrate;
762 *pos++ = (u8) (rate / 5);
763 if (++count == 8)
764 break;
765 }
766 }
767
768 if (rates_len > count) {
769 pos = skb_put(skb, rates_len - count + 2);
770 *pos++ = WLAN_EID_EXT_SUPP_RATES;
771 *pos++ = rates_len - count;
772
773 for (i++; i < sband->n_bitrates; i++) {
774 if (BIT(i) & rates) {
775 int rate = sband->bitrates[i].bitrate;
776 *pos++ = (u8) (rate / 5);
777 }
778 }
779 }
780
781 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
782 /* 1. power capabilities */
783 pos = skb_put(skb, 4);
784 *pos++ = WLAN_EID_PWR_CAPABILITY;
785 *pos++ = 2;
786 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200787 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100788
789 /* 2. supported channels */
790 /* TODO: get this in reg domain format */
791 pos = skb_put(skb, 2 * sband->n_channels + 2);
792 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
793 *pos++ = 2 * sband->n_channels;
794 for (i = 0; i < sband->n_channels; i++) {
795 *pos++ = ieee80211_frequency_to_channel(
796 sband->channels[i].center_freq);
797 *pos++ = 1; /* one channel in the subband*/
798 }
799 }
800
801 /* if present, add any custom IEs that go before HT */
802 if (assoc_data->ie_len && assoc_data->ie) {
803 static const u8 before_ht[] = {
804 WLAN_EID_SSID,
805 WLAN_EID_SUPP_RATES,
806 WLAN_EID_EXT_SUPP_RATES,
807 WLAN_EID_PWR_CAPABILITY,
808 WLAN_EID_SUPPORTED_CHANNELS,
809 WLAN_EID_RSN,
810 WLAN_EID_QOS_CAPA,
811 WLAN_EID_RRM_ENABLED_CAPABILITIES,
812 WLAN_EID_MOBILITY_DOMAIN,
813 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
814 };
815 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
816 before_ht, ARRAY_SIZE(before_ht),
817 offset);
818 pos = skb_put(skb, noffset - offset);
819 memcpy(pos, assoc_data->ie + offset, noffset - offset);
820 offset = noffset;
821 }
822
Johannes Bergf2d9d272012-11-22 14:11:39 +0100823 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
824 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
825 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
826
Johannes Berga8243b72012-11-22 14:32:09 +0100827 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200828 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200829 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100830
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000831 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100832 ieee80211_add_vht_ie(sdata, skb, sband,
833 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000834
Johannes Berg66e67e42012-01-20 13:55:27 +0100835 /* if present, add any custom non-vendor IEs that go after HT */
836 if (assoc_data->ie_len && assoc_data->ie) {
837 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
838 assoc_data->ie_len,
839 offset);
840 pos = skb_put(skb, noffset - offset);
841 memcpy(pos, assoc_data->ie + offset, noffset - offset);
842 offset = noffset;
843 }
844
Johannes Berg76f03032012-03-08 15:02:05 +0100845 if (assoc_data->wmm) {
846 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200847 qos_info = ifmgd->uapsd_queues;
848 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100849 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
850 } else {
851 qos_info = 0;
852 }
853
854 pos = skb_put(skb, 9);
855 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
856 *pos++ = 7; /* len */
857 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
858 *pos++ = 0x50;
859 *pos++ = 0xf2;
860 *pos++ = 2; /* WME */
861 *pos++ = 0; /* WME info */
862 *pos++ = 1; /* WME ver */
863 *pos++ = qos_info;
864 }
865
866 /* add any remaining custom (i.e. vendor specific here) IEs */
867 if (assoc_data->ie_len && assoc_data->ie) {
868 noffset = assoc_data->ie_len;
869 pos = skb_put(skb, noffset - offset);
870 memcpy(pos, assoc_data->ie + offset, noffset - offset);
871 }
872
Johannes Berga1845fc2012-06-27 13:18:36 +0200873 drv_mgd_prepare_tx(local, sdata);
874
Johannes Berg66e67e42012-01-20 13:55:27 +0100875 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100876 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
877 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
878 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100879 ieee80211_tx_skb(sdata, skb);
880}
881
Kalle Valo572e0012009-02-10 17:09:31 +0200882void ieee80211_send_pspoll(struct ieee80211_local *local,
883 struct ieee80211_sub_if_data *sdata)
884{
Kalle Valo572e0012009-02-10 17:09:31 +0200885 struct ieee80211_pspoll *pspoll;
886 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200887
Kalle Valod8cd1892010-01-05 20:16:26 +0200888 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
889 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200890 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200891
Kalle Valod8cd1892010-01-05 20:16:26 +0200892 pspoll = (struct ieee80211_pspoll *) skb->data;
893 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200894
Johannes Berg62ae67b2009-11-18 18:42:05 +0100895 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
896 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200897}
898
Johannes Berg965beda2009-04-16 13:17:24 +0200899void ieee80211_send_nullfunc(struct ieee80211_local *local,
900 struct ieee80211_sub_if_data *sdata,
901 int powersave)
902{
903 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200904 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530905 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200906
Kalle Valod8cd1892010-01-05 20:16:26 +0200907 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
908 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200909 return;
910
Kalle Valod8cd1892010-01-05 20:16:26 +0200911 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200912 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200913 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200914
Seth Forshee6c17b772013-02-11 11:21:07 -0600915 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
916 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530917 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
918 IEEE80211_STA_CONNECTION_POLL))
919 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
920
Johannes Berg62ae67b2009-11-18 18:42:05 +0100921 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200922}
923
Felix Fietkaud5242152010-01-08 18:06:26 +0100924static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
925 struct ieee80211_sub_if_data *sdata)
926{
927 struct sk_buff *skb;
928 struct ieee80211_hdr *nullfunc;
929 __le16 fc;
930
931 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
932 return;
933
934 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700935 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100936 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700937
Felix Fietkaud5242152010-01-08 18:06:26 +0100938 skb_reserve(skb, local->hw.extra_tx_headroom);
939
940 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
941 memset(nullfunc, 0, 30);
942 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
943 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
944 nullfunc->frame_control = fc;
945 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
946 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
947 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
948 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
949
950 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
951 ieee80211_tx_skb(sdata, skb);
952}
953
Johannes Bergcc32abd2009-05-15 11:52:31 +0200954/* spectrum management related things */
955static void ieee80211_chswitch_work(struct work_struct *work)
956{
957 struct ieee80211_sub_if_data *sdata =
958 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100959 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200960 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
961
Johannes Berg9607e6b2009-12-23 13:15:31 +0100962 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200963 return;
964
Johannes Berg77fdaa12009-07-07 03:45:17 +0200965 mutex_lock(&ifmgd->mtx);
966 if (!ifmgd->associated)
967 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200968
Johannes Berg85220d72013-03-25 18:29:27 +0100969 local->_oper_chandef = local->csa_chandef;
Karl Beldan675a0b02013-03-25 16:26:57 +0100970
971 if (!local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +0200972 /* call "hw_config" only if doing sw channel switch */
Karl Beldan675a0b02013-03-25 16:26:57 +0100973 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300974 } else {
975 /* update the device channel directly */
Karl Beldan675a0b02013-03-25 16:26:57 +0100976 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200977 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200978
Johannes Berg77fdaa12009-07-07 03:45:17 +0200979 /* XXX: shouldn't really modify cfg80211-owned data! */
Karl Beldan675a0b02013-03-25 16:26:57 +0100980 ifmgd->associated->channel = local->_oper_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200981
Johannes Berg57eebdf2012-08-01 15:50:46 +0200982 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +0100983 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100984 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200985 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200986 out:
987 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
988 mutex_unlock(&ifmgd->mtx);
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 Berg36709462013-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;
1024 struct ieee80211_bss *bss;
Johannes Berg55de9082012-07-26 17:24:39 +02001025 struct ieee80211_chanctx *chanctx;
Johannes Bergb4f286a12013-03-26 14:13:58 +01001026 enum ieee80211_band new_band;
1027 int new_freq;
1028 u8 new_chan_no;
1029 u8 count;
1030 u8 mode;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001031 struct ieee80211_channel *new_chan;
Johannes Berg85220d72013-03-25 18:29:27 +01001032 struct cfg80211_chan_def new_chandef = {};
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001033 struct cfg80211_chan_def new_vht_chandef = {};
1034 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1035 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
Johannes Berg36709462013-05-03 09:35:35 +02001036 const struct ieee80211_ht_operation *ht_oper;
Johannes Berg85220d72013-03-25 18:29:27 +01001037 int secondary_channel_offset = -1;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001038
Johannes Berg77fdaa12009-07-07 03:45:17 +02001039 ASSERT_MGD_MTX(ifmgd);
1040
Johannes Berg37799e52013-03-26 14:02:26 +01001041 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001042 return;
1043
Johannes Bergb4f286a12013-03-26 14:13:58 +01001044 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001045 return;
1046
Johannes Berg37799e52013-03-26 14:02:26 +01001047 /* disregard subsequent announcements if we are already processing */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001048 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1049 return;
1050
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001051 sec_chan_offs = elems->sec_chan_offs;
1052 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
Johannes Berg36709462013-05-03 09:35:35 +02001053 ht_oper = elems->ht_operation;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001054
1055 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1056 IEEE80211_STA_DISABLE_40MHZ)) {
1057 sec_chan_offs = NULL;
1058 wide_bw_chansw_ie = NULL;
Johannes Berg36709462013-05-03 09:35:35 +02001059 /* only used for bandwidth here */
1060 ht_oper = NULL;
Johannes Berg85220d72013-03-25 18:29:27 +01001061 }
1062
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001063 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1064 wide_bw_chansw_ie = NULL;
Johannes Berg85220d72013-03-25 18:29:27 +01001065
Johannes Bergb4f286a12013-03-26 14:13:58 +01001066 if (elems->ext_chansw_ie) {
1067 if (!ieee80211_operating_class_to_band(
1068 elems->ext_chansw_ie->new_operating_class,
1069 &new_band)) {
1070 sdata_info(sdata,
1071 "cannot understand ECSA IE operating class %d, disconnecting\n",
1072 elems->ext_chansw_ie->new_operating_class);
1073 ieee80211_queue_work(&local->hw,
1074 &ifmgd->csa_connection_drop_work);
1075 }
1076 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1077 count = elems->ext_chansw_ie->count;
1078 mode = elems->ext_chansw_ie->mode;
1079 } else if (elems->ch_switch_ie) {
1080 new_band = cbss->channel->band;
1081 new_chan_no = elems->ch_switch_ie->new_ch_num;
1082 count = elems->ch_switch_ie->count;
1083 mode = elems->ch_switch_ie->mode;
1084 } else {
1085 /* nothing here we understand */
Johannes Berg37799e52013-03-26 14:02:26 +01001086 return;
Johannes Bergb4f286a12013-03-26 14:13:58 +01001087 }
Johannes Berg37799e52013-03-26 14:02:26 +01001088
1089 bss = (void *)cbss->priv;
1090
Johannes Bergb4f286a12013-03-26 14:13:58 +01001091 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001092 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1093 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001094 sdata_info(sdata,
1095 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1096 ifmgd->associated->bssid, new_freq);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001097 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001098 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001099 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001100 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001101
Johannes Berg36709462013-05-03 09:35:35 +02001102 if (!beacon && sec_chan_offs) {
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001103 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
Johannes Berg36709462013-05-03 09:35:35 +02001104 } else if (beacon && ht_oper) {
1105 secondary_channel_offset =
1106 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001107 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Berg36709462013-05-03 09:35:35 +02001108 /*
1109 * If it's not a beacon, HT is enabled and the IE not present,
1110 * it's 20 MHz, 802.11-2012 8.5.2.6:
1111 * This element [the Secondary Channel Offset Element] is
1112 * present when switching to a 40 MHz channel. It may be
1113 * present when switching to a 20 MHz channel (in which
1114 * case the secondary channel offset is set to SCN).
1115 */
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001116 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1117 }
1118
Johannes Berg85220d72013-03-25 18:29:27 +01001119 switch (secondary_channel_offset) {
1120 default:
1121 /* secondary_channel_offset was present but is invalid */
1122 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001123 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001124 NL80211_CHAN_HT20);
1125 break;
1126 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001127 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001128 NL80211_CHAN_HT40PLUS);
1129 break;
1130 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001131 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001132 NL80211_CHAN_HT40MINUS);
1133 break;
1134 case -1:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001135 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001136 NL80211_CHAN_NO_HT);
1137 break;
1138 }
1139
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001140 if (wide_bw_chansw_ie) {
1141 new_vht_chandef.chan = new_chan;
1142 new_vht_chandef.center_freq1 =
1143 ieee80211_channel_to_frequency(
1144 wide_bw_chansw_ie->new_center_freq_seg0,
1145 new_band);
1146
1147 switch (wide_bw_chansw_ie->new_channel_width) {
1148 default:
1149 /* hmmm, ignore VHT and use HT if present */
1150 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1151 new_vht_chandef.chan = NULL;
1152 break;
1153 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1154 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1155 break;
1156 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1157 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1158 break;
1159 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1160 /* field is otherwise reserved */
1161 new_vht_chandef.center_freq2 =
1162 ieee80211_channel_to_frequency(
1163 wide_bw_chansw_ie->new_center_freq_seg1,
1164 new_band);
1165 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1166 break;
1167 }
1168 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1169 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1170 chandef_downgrade(&new_vht_chandef);
1171 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1172 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1173 chandef_downgrade(&new_vht_chandef);
1174 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1175 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1176 chandef_downgrade(&new_vht_chandef);
1177 }
1178
1179 /* if VHT data is there validate & use it */
1180 if (new_vht_chandef.chan) {
1181 if (!cfg80211_chandef_compatible(&new_vht_chandef,
1182 &new_chandef)) {
1183 sdata_info(sdata,
1184 "AP %pM CSA has inconsistent channel data, disconnecting\n",
1185 ifmgd->associated->bssid);
1186 ieee80211_queue_work(&local->hw,
1187 &ifmgd->csa_connection_drop_work);
1188 return;
1189 }
1190 new_chandef = new_vht_chandef;
1191 }
1192
Johannes Berg85220d72013-03-25 18:29:27 +01001193 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1194 IEEE80211_CHAN_DISABLED)) {
1195 sdata_info(sdata,
1196 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1197 ifmgd->associated->bssid, new_freq,
1198 new_chandef.width, new_chandef.center_freq1,
1199 new_chandef.center_freq2);
1200 ieee80211_queue_work(&local->hw,
1201 &ifmgd->csa_connection_drop_work);
1202 return;
1203 }
1204
Johannes Berg57eebdf2012-08-01 15:50:46 +02001205 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1206
Johannes Bergb4f286a12013-03-26 14:13:58 +01001207 if (local->use_chanctx) {
Johannes Berg55de9082012-07-26 17:24:39 +02001208 sdata_info(sdata,
1209 "not handling channel switch with channel contexts\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001210 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001211 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001212 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001213 }
1214
Johannes Bergb4f286a12013-03-26 14:13:58 +01001215 mutex_lock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001216 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
Johannes Bergb4f286a12013-03-26 14:13:58 +01001217 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001218 return;
1219 }
1220 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1221 struct ieee80211_chanctx, conf);
1222 if (chanctx->refcount > 1) {
1223 sdata_info(sdata,
1224 "channel switch with multiple interfaces on the same channel, disconnecting\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001225 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001226 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001227 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001228 return;
1229 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001230 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001231
Johannes Berg85220d72013-03-25 18:29:27 +01001232 local->csa_chandef = new_chandef;
Johannes Berg55de9082012-07-26 17:24:39 +02001233
Johannes Bergb4f286a12013-03-26 14:13:58 +01001234 if (mode)
1235 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001236 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001237 IEEE80211_QUEUE_STOP_REASON_CSA);
1238
Johannes Bergb4f286a12013-03-26 14:13:58 +01001239 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001240 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001241 struct ieee80211_channel_switch ch_switch = {
1242 .timestamp = timestamp,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001243 .block_tx = mode,
Johannes Berg85220d72013-03-25 18:29:27 +01001244 .chandef = new_chandef,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001245 .count = count,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001246 };
1247
Johannes Bergb4f286a12013-03-26 14:13:58 +01001248 drv_channel_switch(local, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001249 return;
1250 }
1251
1252 /* channel switch handled in software */
Johannes Bergb4f286a12013-03-26 14:13:58 +01001253 if (count <= 1)
1254 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001255 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001256 mod_timer(&ifmgd->chswitch_timer,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001257 TU_TO_EXP_TIME(count * cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001258}
1259
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001260static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1261 struct ieee80211_channel *channel,
1262 const u8 *country_ie, u8 country_ie_len,
1263 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001264{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001265 struct ieee80211_country_ie_triplet *triplet;
1266 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1267 int i, chan_pwr, chan_increment, new_ap_level;
1268 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001269
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001270 /* Invalid IE */
1271 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001272 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001273
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001274 triplet = (void *)(country_ie + 3);
1275 country_ie_len -= 3;
1276
1277 switch (channel->band) {
1278 default:
1279 WARN_ON_ONCE(1);
1280 /* fall through */
1281 case IEEE80211_BAND_2GHZ:
1282 case IEEE80211_BAND_60GHZ:
1283 chan_increment = 1;
1284 break;
1285 case IEEE80211_BAND_5GHZ:
1286 chan_increment = 4;
1287 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001288 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001289
1290 /* find channel */
1291 while (country_ie_len >= 3) {
1292 u8 first_channel = triplet->chans.first_channel;
1293
1294 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1295 goto next;
1296
1297 for (i = 0; i < triplet->chans.num_channels; i++) {
1298 if (first_channel + i * chan_increment == chan) {
1299 have_chan_pwr = true;
1300 chan_pwr = triplet->chans.max_power;
1301 break;
1302 }
1303 }
1304 if (have_chan_pwr)
1305 break;
1306
1307 next:
1308 triplet++;
1309 country_ie_len -= 3;
1310 }
1311
1312 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001313 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001314
1315 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1316
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001317 if (sdata->ap_power_level == new_ap_level)
1318 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001319
1320 sdata_info(sdata,
1321 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1322 new_ap_level, chan_pwr, *pwr_constr_elem,
1323 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001324 sdata->ap_power_level = new_ap_level;
1325 if (__ieee80211_recalc_txpower(sdata))
1326 return BSS_CHANGED_TXPOWER;
1327 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001328}
1329
Johannes Berg965beda2009-04-16 13:17:24 +02001330/* powersave */
1331static void ieee80211_enable_ps(struct ieee80211_local *local,
1332 struct ieee80211_sub_if_data *sdata)
1333{
1334 struct ieee80211_conf *conf = &local->hw.conf;
1335
Johannes Bergd5edaed2009-04-22 23:02:51 +02001336 /*
1337 * If we are scanning right now then the parameters will
1338 * take effect when scan finishes.
1339 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001340 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001341 return;
1342
Johannes Berg965beda2009-04-16 13:17:24 +02001343 if (conf->dynamic_ps_timeout > 0 &&
1344 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1345 mod_timer(&local->dynamic_ps_timer, jiffies +
1346 msecs_to_jiffies(conf->dynamic_ps_timeout));
1347 } else {
1348 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1349 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301350
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001351 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1352 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1353 return;
1354
1355 conf->flags |= IEEE80211_CONF_PS;
1356 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001357 }
1358}
1359
1360static void ieee80211_change_ps(struct ieee80211_local *local)
1361{
1362 struct ieee80211_conf *conf = &local->hw.conf;
1363
1364 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001365 ieee80211_enable_ps(local, local->ps_sdata);
1366 } else if (conf->flags & IEEE80211_CONF_PS) {
1367 conf->flags &= ~IEEE80211_CONF_PS;
1368 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1369 del_timer_sync(&local->dynamic_ps_timer);
1370 cancel_work_sync(&local->dynamic_ps_enable_work);
1371 }
1372}
1373
Jason Young808118c2011-03-10 16:43:19 -08001374static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1375{
1376 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1377 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001378 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001379
1380 if (!mgd->powersave)
1381 return false;
1382
Johannes Berg05cb9102011-10-28 11:59:47 +02001383 if (mgd->broken_ap)
1384 return false;
1385
Jason Young808118c2011-03-10 16:43:19 -08001386 if (!mgd->associated)
1387 return false;
1388
Jason Young808118c2011-03-10 16:43:19 -08001389 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1390 IEEE80211_STA_CONNECTION_POLL))
1391 return false;
1392
1393 rcu_read_lock();
1394 sta = sta_info_get(sdata, mgd->bssid);
1395 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001396 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001397 rcu_read_unlock();
1398
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001399 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001400}
1401
Johannes Berg965beda2009-04-16 13:17:24 +02001402/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001403void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001404{
1405 struct ieee80211_sub_if_data *sdata, *found = NULL;
1406 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001407 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001408
1409 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1410 local->ps_sdata = NULL;
1411 return;
1412 }
1413
1414 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001415 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001416 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301417 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1418 /* If an AP vif is found, then disable PS
1419 * by setting the count to zero thereby setting
1420 * ps_sdata to NULL.
1421 */
1422 count = 0;
1423 break;
1424 }
Johannes Berg965beda2009-04-16 13:17:24 +02001425 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1426 continue;
1427 found = sdata;
1428 count++;
1429 }
1430
Jason Young808118c2011-03-10 16:43:19 -08001431 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001432 s32 beaconint_us;
1433
1434 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001435 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001436
1437 beaconint_us = ieee80211_tu_to_usec(
1438 found->vif.bss_conf.beacon_int);
1439
Juuso Oikarinenff616382010-06-09 09:51:52 +03001440 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001441 if (timeout < 0) {
1442 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001443 * Go to full PSM if the user configures a very low
1444 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001445 * The 2000 second value is there for compatibility
1446 * until the PM_QOS_NETWORK_LATENCY is configured
1447 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001448 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001449 if (latency > (1900 * USEC_PER_MSEC) &&
1450 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001451 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001452 else
1453 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001454 }
Johannes Berg09b85562013-02-06 23:07:41 +01001455 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001456
Johannes Berg04fe2032009-04-22 18:44:37 +02001457 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001458 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001459 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001460 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001461 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001462
1463 /* If the TIM IE is invalid, pretend the value is 1 */
1464 if (!dtimper)
1465 dtimper = 1;
1466 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001467 maxslp = min_t(int, dtimper,
1468 latency / beaconint_us);
1469
Johannes Berg9ccebe62009-04-23 10:32:36 +02001470 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001471 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001472 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001473 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001474 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001475 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001476 }
Johannes Berg965beda2009-04-16 13:17:24 +02001477
1478 ieee80211_change_ps(local);
1479}
1480
Eliad Pellerab095872012-07-27 12:33:22 +03001481void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1482{
1483 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1484
1485 if (sdata->vif.bss_conf.ps != ps_allowed) {
1486 sdata->vif.bss_conf.ps = ps_allowed;
1487 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1488 }
1489}
1490
Johannes Berg965beda2009-04-16 13:17:24 +02001491void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1492{
1493 struct ieee80211_local *local =
1494 container_of(work, struct ieee80211_local,
1495 dynamic_ps_disable_work);
1496
1497 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1498 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1499 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1500 }
1501
1502 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001503 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001504 IEEE80211_QUEUE_STOP_REASON_PS);
1505}
1506
1507void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1508{
1509 struct ieee80211_local *local =
1510 container_of(work, struct ieee80211_local,
1511 dynamic_ps_enable_work);
1512 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001513 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301514 unsigned long flags;
1515 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001516
1517 /* can only happen when PS was just disabled anyway */
1518 if (!sdata)
1519 return;
1520
Christian Lamparter5e340692011-06-30 21:08:43 +02001521 ifmgd = &sdata->u.mgd;
1522
Johannes Berg965beda2009-04-16 13:17:24 +02001523 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1524 return;
1525
Johannes Berg09b85562013-02-06 23:07:41 +01001526 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001527 /* don't enter PS if TX frames are pending */
1528 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301529 mod_timer(&local->dynamic_ps_timer, jiffies +
1530 msecs_to_jiffies(
1531 local->hw.conf.dynamic_ps_timeout));
1532 return;
1533 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001534
1535 /*
1536 * transmission can be stopped by others which leads to
1537 * dynamic_ps_timer expiry. Postpone the ps timer if it
1538 * is not the actual idle state.
1539 */
1540 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1541 for (q = 0; q < local->hw.queues; q++) {
1542 if (local->queue_stop_reasons[q]) {
1543 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1544 flags);
1545 mod_timer(&local->dynamic_ps_timer, jiffies +
1546 msecs_to_jiffies(
1547 local->hw.conf.dynamic_ps_timeout));
1548 return;
1549 }
1550 }
1551 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301552 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301553
Vivek Natarajan375177b2010-02-09 14:50:28 +05301554 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301555 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001556 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301557 mod_timer(&local->dynamic_ps_timer, jiffies +
1558 msecs_to_jiffies(
1559 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001560 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301561 ieee80211_send_nullfunc(local, sdata, 1);
1562 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001563 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301564 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301565 }
1566
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001567 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1568 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301569 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1570 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1571 local->hw.conf.flags |= IEEE80211_CONF_PS;
1572 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1573 }
Johannes Berg965beda2009-04-16 13:17:24 +02001574}
1575
1576void ieee80211_dynamic_ps_timer(unsigned long data)
1577{
1578 struct ieee80211_local *local = (void *) data;
1579
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001580 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001581 return;
1582
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001583 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001584}
1585
Simon Wunderlich164eb022013-02-08 18:16:20 +01001586void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1587{
1588 struct delayed_work *delayed_work =
1589 container_of(work, struct delayed_work, work);
1590 struct ieee80211_sub_if_data *sdata =
1591 container_of(delayed_work, struct ieee80211_sub_if_data,
1592 dfs_cac_timer_work);
1593
1594 ieee80211_vif_release_channel(sdata);
1595
1596 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1597}
1598
Johannes Berg60f8b392008-09-08 17:44:22 +02001599/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001600static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001601 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001602 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001603{
Jiri Bencf0706e82007-05-05 11:45:53 -07001604 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001605 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001606 size_t left;
1607 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001608 const u8 *pos;
1609 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001610
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001611 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001612 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001613
Johannes Berg32c50572012-03-28 11:04:29 +02001614 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001615 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001616
1617 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001618 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001619
Jiri Bencf0706e82007-05-05 11:45:53 -07001620 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001621 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001622
1623 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001624 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001625
Jiri Bencf0706e82007-05-05 11:45:53 -07001626 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001627 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001628 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001629 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001630
1631 pos = wmm_param + 8;
1632 left = wmm_param_len - 8;
1633
1634 memset(&params, 0, sizeof(params));
1635
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001636 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001637 for (; left >= 4; left -= 4, pos += 4) {
1638 int aci = (pos[0] >> 5) & 0x03;
1639 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001640 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001641 int queue;
1642
1643 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001644 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001645 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001646 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001647 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001648 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1649 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001650 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001651 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001652 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001653 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001654 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001655 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1656 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001657 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001658 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001659 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001660 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001661 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001662 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1663 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001664 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001665 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001666 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001667 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001668 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001669 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001670 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1671 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001672 break;
1673 }
1674
1675 params.aifs = pos[0] & 0x0f;
1676 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1677 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001678 params.txop = get_unaligned_le16(pos + 2);
Alexander Bondar908f8d02013-04-07 09:53:30 +03001679 params.acm = acm;
Kalle Valoab133152010-01-12 10:42:31 +02001680 params.uapsd = uapsd;
1681
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001682 mlme_dbg(sdata,
1683 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1684 queue, aci, acm,
1685 params.aifs, params.cw_min, params.cw_max,
1686 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001687 sdata->tx_conf[queue] = params;
1688 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001689 sdata_err(sdata,
1690 "failed to set TX queue parameters for queue %d\n",
1691 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001692 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001693
1694 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001695 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001696 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001697}
1698
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001699static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1700{
1701 lockdep_assert_held(&sdata->local->mtx);
1702
1703 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1704 IEEE80211_STA_BEACON_POLL);
1705 ieee80211_run_deferred_scan(sdata->local);
1706}
1707
1708static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1709{
1710 mutex_lock(&sdata->local->mtx);
1711 __ieee80211_stop_poll(sdata);
1712 mutex_unlock(&sdata->local->mtx);
1713}
1714
Johannes Berg7a5158e2008-10-08 10:59:33 +02001715static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1716 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001717{
Johannes Bergbda39332008-10-11 01:51:51 +02001718 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001719 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001720 bool use_protection;
1721 bool use_short_preamble;
1722 bool use_short_slot;
1723
1724 if (erp_valid) {
1725 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1726 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1727 } else {
1728 use_protection = false;
1729 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1730 }
1731
1732 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001733 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001734 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001735
Johannes Berg471b3ef2007-12-28 14:32:58 +01001736 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001737 bss_conf->use_cts_prot = use_protection;
1738 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001739 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001740
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001741 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001742 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001743 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001744 }
Daniel Draked9430a32007-07-27 15:43:24 +02001745
Johannes Berg7a5158e2008-10-08 10:59:33 +02001746 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001747 bss_conf->use_short_slot = use_short_slot;
1748 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001749 }
1750
1751 return changed;
1752}
1753
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001754static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001755 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001756 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001757{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001758 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001759 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001760 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001761
Johannes Bergae5eb022008-10-14 16:58:37 +02001762 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001763 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001764 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001765
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001766 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001767 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001768
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001769 sdata->u.mgd.associated = cbss;
1770 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001771
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001772 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1773
Johannes Berg488dd7b2012-10-29 20:08:01 +01001774 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001775 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001776
Johannes Berg9caf0362012-11-29 01:25:20 +01001777 rcu_read_lock();
1778 ies = rcu_dereference(cbss->ies);
1779 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001780 int ret;
1781
1782 ret = cfg80211_get_p2p_attr(
1783 ies->data, ies->len,
1784 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001785 (u8 *) &bss_conf->p2p_noa_attr,
1786 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001787 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001788 sdata->u.mgd.p2p_noa_index =
1789 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001790 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001791 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001792 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001793 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001794 }
1795
Johannes Bergb291ba12009-07-10 15:29:03 +02001796 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001797 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001798
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001799 ieee80211_led_assoc(local, 1);
1800
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001801 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001802 /*
1803 * If the AP is buggy we may get here with no DTIM period
1804 * known, so assume it's 1 which is the only safe assumption
1805 * in that case, although if the TIM IE is broken powersave
1806 * probably just won't work at all.
1807 */
1808 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001809 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001810 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001811 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001812 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001813
Juuso Oikarinen68542962010-06-09 13:43:26 +03001814 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001815
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001816 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001817 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001818 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001819 bss_info_changed |= BSS_CHANGED_CQM;
1820
Juuso Oikarinen68542962010-06-09 13:43:26 +03001821 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001822 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001823 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001824
Johannes Bergae5eb022008-10-14 16:58:37 +02001825 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001826
Johannes Berg056508d2009-07-30 21:43:55 +02001827 mutex_lock(&local->iflist_mtx);
1828 ieee80211_recalc_ps(local, -1);
1829 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001830
Johannes Berg04ecd252012-09-11 14:34:12 +02001831 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001832 ieee80211_recalc_ps_vif(sdata);
1833
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001834 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001835}
1836
Jouni Malinene69e95d2010-03-29 23:29:31 -07001837static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001838 u16 stype, u16 reason, bool tx,
1839 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001840{
Johannes Berg46900292009-02-15 12:44:28 +01001841 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001842 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001843 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001844
Johannes Berg77fdaa12009-07-07 03:45:17 +02001845 ASSERT_MGD_MTX(ifmgd);
1846
Johannes Berg37ad3882012-02-24 13:50:54 +01001847 if (WARN_ON_ONCE(tx && !frame_buf))
1848 return;
1849
Johannes Bergb291ba12009-07-10 15:29:03 +02001850 if (WARN_ON(!ifmgd->associated))
1851 return;
1852
David Spinadel79543d82012-06-12 09:59:45 +03001853 ieee80211_stop_poll(sdata);
1854
Johannes Berg77fdaa12009-07-07 03:45:17 +02001855 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001856 netif_carrier_off(sdata->dev);
1857
Eliad Peller88bc40e2012-07-12 17:35:33 +03001858 /*
1859 * if we want to get out of ps before disassoc (why?) we have
1860 * to do it before sending disassoc, as otherwise the null-packet
1861 * won't be valid.
1862 */
1863 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1864 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1865 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1866 }
1867 local->ps_sdata = NULL;
1868
Eliad Pellerab095872012-07-27 12:33:22 +03001869 /* disable per-vif ps */
1870 ieee80211_recalc_ps_vif(sdata);
1871
Eliad Pellerf8239812012-06-27 14:18:22 +03001872 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1873 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001874 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001875
Johannes Berg37ad3882012-02-24 13:50:54 +01001876 /* deauthenticate/disassociate now */
1877 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001878 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1879 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001880
1881 /* flush out frame */
1882 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001883 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001884
Eliad Peller88a9e312012-06-01 11:14:03 +03001885 /* clear bssid only after building the needed mgmt frames */
1886 memset(ifmgd->bssid, 0, ETH_ALEN);
1887
Johannes Berg37ad3882012-02-24 13:50:54 +01001888 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001889 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001890
1891 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001892 changed |= ieee80211_reset_erp_info(sdata);
1893
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001894 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001895 changed |= BSS_CHANGED_ASSOC;
1896 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001897
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001898 ifmgd->p2p_noa_index = -1;
1899 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1900 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001901
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001902 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001903 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1904 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001905 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1906 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001907
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001908 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301909
Kalle Valo520eb822008-12-18 23:35:27 +02001910 del_timer_sync(&local->dynamic_ps_timer);
1911 cancel_work_sync(&local->dynamic_ps_enable_work);
1912
Juuso Oikarinen68542962010-06-09 13:43:26 +03001913 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001914 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001915 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001916
Johannes Berg3abead52012-03-02 15:56:59 +01001917 sdata->vif.bss_conf.qos = false;
1918 changed |= BSS_CHANGED_QOS;
1919
Johannes Berg0aaffa92010-05-05 15:28:27 +02001920 /* The BSSID (not really interesting) and HT changed */
1921 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001922 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001923
Johannes Berg3abead52012-03-02 15:56:59 +01001924 /* disassociated - set to defaults now */
1925 ieee80211_set_wmm_default(sdata, false);
1926
Johannes Bergb9dcf712010-08-27 12:35:54 +02001927 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1928 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1929 del_timer_sync(&sdata->u.mgd.timer);
1930 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001931
Johannes Berg826262c2012-12-10 16:38:14 +02001932 sdata->vif.bss_conf.dtim_period = 0;
1933
Johannes Berg028e8da02012-11-26 11:57:41 +01001934 ifmgd->flags = 0;
1935 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001936}
Jiri Bencf0706e82007-05-05 11:45:53 -07001937
Kalle Valo3cf335d2009-03-22 21:57:06 +02001938void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1939 struct ieee80211_hdr *hdr)
1940{
1941 /*
1942 * We can postpone the mgd.timer whenever receiving unicast frames
1943 * from AP because we know that the connection is working both ways
1944 * at that time. But multicast frames (and hence also beacons) must
1945 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001946 * data idle periods for sending the periodic probe request to the
1947 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001948 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001949 if (is_multicast_ether_addr(hdr->addr1))
1950 return;
1951
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001952 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001953}
Jiri Bencf0706e82007-05-05 11:45:53 -07001954
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001955static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1956{
1957 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001958 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001959
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001960 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001961 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001962 IEEE80211_STA_CONNECTION_POLL))) {
1963 mutex_unlock(&local->mtx);
1964 return;
1965 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001966
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001967 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001968
1969 mutex_lock(&local->iflist_mtx);
1970 ieee80211_recalc_ps(local, -1);
1971 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001972
1973 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001974 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001975
1976 /*
1977 * We've received a probe response, but are not sure whether
1978 * we have or will be receiving any beacons or data, so let's
1979 * schedule the timers again, just in case.
1980 */
1981 ieee80211_sta_reset_beacon_monitor(sdata);
1982
1983 mod_timer(&ifmgd->conn_mon_timer,
1984 round_jiffies_up(jiffies +
1985 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001986out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001987 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001988}
1989
1990void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001991 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001992{
Felix Fietkau75706d02010-12-02 21:01:07 +01001993 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001994 return;
1995
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001996 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1997 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001998 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001999 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002000 else
2001 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002002 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002003 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002004 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002005
2006 if (ack)
2007 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002008}
2009
Maxim Levitskya43abf22009-07-31 18:54:12 +03002010static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2011{
2012 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2013 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002014 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002015 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002016
2017 /*
2018 * Try sending broadcast probe requests for the last three
2019 * probe requests after the first ones failed since some
2020 * buggy APs only support broadcast probe requests.
2021 */
2022 if (ifmgd->probe_send_count >= unicast_limit)
2023 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002024
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002025 /*
2026 * When the hardware reports an accurate Tx ACK status, it's
2027 * better to send a nullfunc frame instead of a probe request,
2028 * as it will kick us off the AP quickly if we aren't associated
2029 * anymore. The timeout will be reset if the frame is ACKed by
2030 * the AP.
2031 */
Soumik Das992e68b2012-05-20 15:31:13 +05302032 ifmgd->probe_send_count++;
2033
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002034 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2035 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002036 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002037 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002038 int ssid_len;
2039
Johannes Berg9caf0362012-11-29 01:25:20 +01002040 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002041 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002042 if (WARN_ON_ONCE(ssid == NULL))
2043 ssid_len = 0;
2044 else
2045 ssid_len = ssid[1];
2046
2047 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002048 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02002049 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002050 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002051 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002052
Ben Greear180205b2011-02-04 15:30:24 -08002053 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002054 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05302055 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01002056 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002057}
2058
Johannes Bergb291ba12009-07-10 15:29:03 +02002059static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2060 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002061{
Kalle Valo04de8382009-03-22 21:57:35 +02002062 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002063 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002064
Johannes Berg9607e6b2009-12-23 13:15:31 +01002065 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002066 return;
2067
Johannes Berg77fdaa12009-07-07 03:45:17 +02002068 mutex_lock(&ifmgd->mtx);
2069
2070 if (!ifmgd->associated)
2071 goto out;
2072
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002073 mutex_lock(&sdata->local->mtx);
2074
2075 if (sdata->local->tmp_channel || sdata->local->scanning) {
2076 mutex_unlock(&sdata->local->mtx);
2077 goto out;
2078 }
2079
Ben Greeara13fbe52013-03-25 11:19:35 -07002080 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002081 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002082 "detected beacon loss from AP (missed %d beacons) - probing\n",
2083 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002084
Ben Greeara13fbe52013-03-25 11:19:35 -07002085 ieee80211_cqm_rssi_notify(&sdata->vif,
2086 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
2087 GFP_KERNEL);
2088 }
Kalle Valo04de8382009-03-22 21:57:35 +02002089
Johannes Bergb291ba12009-07-10 15:29:03 +02002090 /*
2091 * The driver/our work has already reported this event or the
2092 * connection monitoring has kicked in and we have already sent
2093 * a probe request. Or maybe the AP died and the driver keeps
2094 * reporting until we disassociate...
2095 *
2096 * In either case we have to ignore the current call to this
2097 * function (except for setting the correct probe reason bit)
2098 * because otherwise we would reset the timer every time and
2099 * never check whether we received a probe response!
2100 */
2101 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2102 IEEE80211_STA_CONNECTION_POLL))
2103 already = true;
2104
2105 if (beacon)
2106 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2107 else
2108 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2109
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002110 mutex_unlock(&sdata->local->mtx);
2111
Johannes Bergb291ba12009-07-10 15:29:03 +02002112 if (already)
2113 goto out;
2114
Johannes Berg4e751842009-06-10 15:16:52 +02002115 mutex_lock(&sdata->local->iflist_mtx);
2116 ieee80211_recalc_ps(sdata->local, -1);
2117 mutex_unlock(&sdata->local->iflist_mtx);
2118
Maxim Levitskya43abf22009-07-31 18:54:12 +03002119 ifmgd->probe_send_count = 0;
2120 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002121 out:
2122 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002123}
2124
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002125struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2126 struct ieee80211_vif *vif)
2127{
2128 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002130 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002131 struct sk_buff *skb;
2132 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002133 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002134
2135 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2136 return NULL;
2137
2138 ASSERT_MGD_MTX(ifmgd);
2139
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002140 if (ifmgd->associated)
2141 cbss = ifmgd->associated;
2142 else if (ifmgd->auth_data)
2143 cbss = ifmgd->auth_data->bss;
2144 else if (ifmgd->assoc_data)
2145 cbss = ifmgd->assoc_data->bss;
2146 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002147 return NULL;
2148
Johannes Berg9caf0362012-11-29 01:25:20 +01002149 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002150 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002151 if (WARN_ON_ONCE(ssid == NULL))
2152 ssid_len = 0;
2153 else
2154 ssid_len = ssid[1];
2155
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002156 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002157 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002158 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002159 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002160 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002161
2162 return skb;
2163}
2164EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2165
Johannes Bergeef9e542013-01-29 13:09:34 +01002166static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002167{
2168 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002169 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002170
2171 mutex_lock(&ifmgd->mtx);
2172 if (!ifmgd->associated) {
2173 mutex_unlock(&ifmgd->mtx);
2174 return;
2175 }
2176
Johannes Berg37ad3882012-02-24 13:50:54 +01002177 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2178 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002179 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002180 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002181 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002182 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002183 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002184 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002185
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002186 /*
2187 * must be outside lock due to cfg80211,
2188 * but that's not a problem.
2189 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002190 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002191}
2192
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002193static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002194{
2195 struct ieee80211_sub_if_data *sdata =
2196 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002197 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002198 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2199 struct sta_info *sta;
2200
2201 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302202 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002203 sta = sta_info_get(sdata, ifmgd->bssid);
2204 if (sta)
2205 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302206 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002207 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002208
Johannes Berg682bd382013-01-29 13:13:50 +01002209 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002210 sdata_info(sdata, "Connection to AP %pM lost\n",
2211 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002212 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002213 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002214 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002215 }
2216}
2217
2218static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2219{
2220 struct ieee80211_sub_if_data *sdata =
2221 container_of(work, struct ieee80211_sub_if_data,
2222 u.mgd.csa_connection_drop_work);
2223
Johannes Bergeef9e542013-01-29 13:09:34 +01002224 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002225}
2226
Kalle Valo04de8382009-03-22 21:57:35 +02002227void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2228{
2229 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002230 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002231
Johannes Bergb5878a22010-04-07 16:48:40 +02002232 trace_api_beacon_loss(sdata);
2233
Johannes Berg682bd382013-01-29 13:13:50 +01002234 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002235 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002236}
2237EXPORT_SYMBOL(ieee80211_beacon_loss);
2238
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002239void ieee80211_connection_loss(struct ieee80211_vif *vif)
2240{
2241 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2242 struct ieee80211_hw *hw = &sdata->local->hw;
2243
Johannes Bergb5878a22010-04-07 16:48:40 +02002244 trace_api_connection_loss(sdata);
2245
Johannes Berg682bd382013-01-29 13:13:50 +01002246 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002247 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2248}
2249EXPORT_SYMBOL(ieee80211_connection_loss);
2250
2251
Johannes Berg66e67e42012-01-20 13:55:27 +01002252static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2253 bool assoc)
2254{
2255 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2256
2257 lockdep_assert_held(&sdata->u.mgd.mtx);
2258
Johannes Berg66e67e42012-01-20 13:55:27 +01002259 if (!assoc) {
2260 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2261
2262 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2263 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002264 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002265 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002266 }
2267
Johannes Berg5b112d32013-02-01 01:49:58 +01002268 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002269 kfree(auth_data);
2270 sdata->u.mgd.auth_data = NULL;
2271}
2272
2273static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2274 struct ieee80211_mgmt *mgmt, size_t len)
2275{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002276 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002277 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2278 u8 *pos;
2279 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002280 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002281
2282 pos = mgmt->u.auth.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002283 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002284 if (!elems.challenge)
2285 return;
2286 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002287 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002288 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2289 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2290 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002291 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002292 elems.challenge - 2, elems.challenge_len + 2,
2293 auth_data->bss->bssid, auth_data->bss->bssid,
2294 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002295 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002296}
2297
2298static enum rx_mgmt_action __must_check
2299ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2300 struct ieee80211_mgmt *mgmt, size_t len)
2301{
2302 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2303 u8 bssid[ETH_ALEN];
2304 u16 auth_alg, auth_transaction, status_code;
2305 struct sta_info *sta;
2306
2307 lockdep_assert_held(&ifmgd->mtx);
2308
2309 if (len < 24 + 6)
2310 return RX_MGMT_NONE;
2311
2312 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2313 return RX_MGMT_NONE;
2314
2315 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2316
Joe Perchesb203ca32012-05-08 18:56:52 +00002317 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002318 return RX_MGMT_NONE;
2319
2320 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2321 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2322 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2323
2324 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002325 auth_transaction != ifmgd->auth_data->expected_transaction) {
2326 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2327 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2328 auth_transaction,
2329 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002330 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002331 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002332
2333 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002334 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2335 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002336 ieee80211_destroy_auth_data(sdata, false);
2337 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002338 }
2339
2340 switch (ifmgd->auth_data->algorithm) {
2341 case WLAN_AUTH_OPEN:
2342 case WLAN_AUTH_LEAP:
2343 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002344 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002345 break;
2346 case WLAN_AUTH_SHARED_KEY:
2347 if (ifmgd->auth_data->expected_transaction != 4) {
2348 ieee80211_auth_challenge(sdata, mgmt, len);
2349 /* need another frame */
2350 return RX_MGMT_NONE;
2351 }
2352 break;
2353 default:
2354 WARN_ONCE(1, "invalid auth alg %d",
2355 ifmgd->auth_data->algorithm);
2356 return RX_MGMT_NONE;
2357 }
2358
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002359 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002360 ifmgd->auth_data->done = true;
2361 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002362 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002363 run_again(ifmgd, ifmgd->auth_data->timeout);
2364
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002365 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2366 ifmgd->auth_data->expected_transaction != 2) {
2367 /*
2368 * Report auth frame to user space for processing since another
2369 * round of Authentication frames is still needed.
2370 */
2371 return RX_MGMT_CFG80211_RX_AUTH;
2372 }
2373
Johannes Berg66e67e42012-01-20 13:55:27 +01002374 /* move station state to auth */
2375 mutex_lock(&sdata->local->sta_mtx);
2376 sta = sta_info_get(sdata, bssid);
2377 if (!sta) {
2378 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2379 goto out_err;
2380 }
2381 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002382 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002383 goto out_err;
2384 }
2385 mutex_unlock(&sdata->local->sta_mtx);
2386
2387 return RX_MGMT_CFG80211_RX_AUTH;
2388 out_err:
2389 mutex_unlock(&sdata->local->sta_mtx);
2390 /* ignore frame -- wait for timeout */
2391 return RX_MGMT_NONE;
2392}
2393
2394
Johannes Berg77fdaa12009-07-07 03:45:17 +02002395static enum rx_mgmt_action __must_check
2396ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002397 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002398{
Johannes Berg46900292009-02-15 12:44:28 +01002399 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002400 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002401 u16 reason_code;
2402
Johannes Berg66e67e42012-01-20 13:55:27 +01002403 lockdep_assert_held(&ifmgd->mtx);
2404
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002405 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002406 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002407
Johannes Berg66e67e42012-01-20 13:55:27 +01002408 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002409 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002410 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002411
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002412 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002413
2414 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2415
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002416 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2417 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002418
Johannes Berg37ad3882012-02-24 13:50:54 +01002419 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2420
Johannes Berg77fdaa12009-07-07 03:45:17 +02002421 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002422}
2423
2424
Johannes Berg77fdaa12009-07-07 03:45:17 +02002425static enum rx_mgmt_action __must_check
2426ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2427 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002428{
Johannes Berg46900292009-02-15 12:44:28 +01002429 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002430 u16 reason_code;
2431
Johannes Berg66e67e42012-01-20 13:55:27 +01002432 lockdep_assert_held(&ifmgd->mtx);
2433
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002434 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002435 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002436
Johannes Berg66e67e42012-01-20 13:55:27 +01002437 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002438 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002439 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002440
2441 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2442
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002443 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2444 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002445
Johannes Berg37ad3882012-02-24 13:50:54 +01002446 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2447
Johannes Berg77fdaa12009-07-07 03:45:17 +02002448 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002449}
2450
Christian Lamparterc74d0842011-10-15 00:14:49 +02002451static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2452 u8 *supp_rates, unsigned int supp_rates_len,
2453 u32 *rates, u32 *basic_rates,
2454 bool *have_higher_than_11mbit,
2455 int *min_rate, int *min_rate_index)
2456{
2457 int i, j;
2458
2459 for (i = 0; i < supp_rates_len; i++) {
2460 int rate = (supp_rates[i] & 0x7f) * 5;
2461 bool is_basic = !!(supp_rates[i] & 0x80);
2462
2463 if (rate > 110)
2464 *have_higher_than_11mbit = true;
2465
2466 /*
2467 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2468 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2469 *
2470 * Note: Even through the membership selector and the basic
2471 * rate flag share the same bit, they are not exactly
2472 * the same.
2473 */
2474 if (!!(supp_rates[i] & 0x80) &&
2475 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2476 continue;
2477
2478 for (j = 0; j < sband->n_bitrates; j++) {
2479 if (sband->bitrates[j].bitrate == rate) {
2480 *rates |= BIT(j);
2481 if (is_basic)
2482 *basic_rates |= BIT(j);
2483 if (rate < *min_rate) {
2484 *min_rate = rate;
2485 *min_rate_index = j;
2486 }
2487 break;
2488 }
2489 }
2490 }
2491}
Jiri Bencf0706e82007-05-05 11:45:53 -07002492
Johannes Berg66e67e42012-01-20 13:55:27 +01002493static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2494 bool assoc)
2495{
2496 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2497
2498 lockdep_assert_held(&sdata->u.mgd.mtx);
2499
Johannes Berg66e67e42012-01-20 13:55:27 +01002500 if (!assoc) {
2501 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2502
2503 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2504 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002505 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002506 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002507 }
2508
2509 kfree(assoc_data);
2510 sdata->u.mgd.assoc_data = NULL;
2511}
2512
2513static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2514 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002515 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002516{
Johannes Berg46900292009-02-15 12:44:28 +01002517 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002518 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002519 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002520 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002521 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002522 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002523 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002524 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02002525 const struct cfg80211_bss_ies *bss_ies = NULL;
2526 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Bergae5eb022008-10-14 16:58:37 +02002527 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002528 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02002529 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002530
Johannes Bergaf6b6372009-12-23 13:15:35 +01002531 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002532
Jiri Bencf0706e82007-05-05 11:45:53 -07002533 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002534 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002535
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002536 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002537 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2538 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002539 aid &= ~(BIT(15) | BIT(14));
2540
Johannes Berg05cb9102011-10-28 11:59:47 +02002541 ifmgd->broken_ap = false;
2542
2543 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002544 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2545 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002546 aid = 0;
2547 ifmgd->broken_ap = true;
2548 }
2549
Johannes Bergaf6b6372009-12-23 13:15:35 +01002550 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002551 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002552
Jiri Bencf0706e82007-05-05 11:45:53 -07002553 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002554 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002555 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002556 }
2557
Johannes Berg46900292009-02-15 12:44:28 +01002558 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002559
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002560 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02002561 * Some APs are erroneously not including some information in their
2562 * (re)association response frames. Try to recover by using the data
2563 * from the beacon or probe response. This seems to afflict mobile
2564 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2565 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2566 */
2567 if ((assoc_data->wmm && !elems.wmm_param) ||
2568 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2569 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2570 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2571 (!elems.vht_cap_elem || !elems.vht_operation))) {
2572 const struct cfg80211_bss_ies *ies;
2573 struct ieee802_11_elems bss_elems;
2574
2575 rcu_read_lock();
2576 ies = rcu_dereference(cbss->ies);
2577 if (ies)
2578 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2579 GFP_ATOMIC);
2580 rcu_read_unlock();
2581 if (!bss_ies)
2582 return false;
2583
2584 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2585 false, &bss_elems);
2586 if (assoc_data->wmm &&
2587 !elems.wmm_param && bss_elems.wmm_param) {
2588 elems.wmm_param = bss_elems.wmm_param;
2589 sdata_info(sdata,
2590 "AP bug: WMM param missing from AssocResp\n");
2591 }
2592
2593 /*
2594 * Also check if we requested HT/VHT, otherwise the AP doesn't
2595 * have to include the IEs in the (re)association response.
2596 */
2597 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2598 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2599 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2600 sdata_info(sdata,
2601 "AP bug: HT capability missing from AssocResp\n");
2602 }
2603 if (!elems.ht_operation && bss_elems.ht_operation &&
2604 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2605 elems.ht_operation = bss_elems.ht_operation;
2606 sdata_info(sdata,
2607 "AP bug: HT operation missing from AssocResp\n");
2608 }
2609 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2610 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2611 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2612 sdata_info(sdata,
2613 "AP bug: VHT capa missing from AssocResp\n");
2614 }
2615 if (!elems.vht_operation && bss_elems.vht_operation &&
2616 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2617 elems.vht_operation = bss_elems.vht_operation;
2618 sdata_info(sdata,
2619 "AP bug: VHT operation missing from AssocResp\n");
2620 }
2621 }
2622
2623 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002624 * We previously checked these in the beacon/probe response, so
2625 * they should be present here. This is just a safety net.
2626 */
2627 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2628 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2629 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002630 "HT AP is missing WMM params or HT capability/operation\n");
2631 ret = false;
2632 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002633 }
2634
2635 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2636 (!elems.vht_cap_elem || !elems.vht_operation)) {
2637 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002638 "VHT AP is missing VHT capability/operation\n");
2639 ret = false;
2640 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002641 }
2642
Guy Eilam2a33bee2011-08-17 15:18:15 +03002643 mutex_lock(&sdata->local->sta_mtx);
2644 /*
2645 * station info was already allocated and inserted before
2646 * the association and should be available to us
2647 */
Johannes Berg7852e362012-01-20 13:55:24 +01002648 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002649 if (WARN_ON(!sta)) {
2650 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002651 ret = false;
2652 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07002653 }
2654
Johannes Berg55de9082012-07-26 17:24:39 +02002655 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002656
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002657 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002658 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002659 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002660 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002661
Mahesh Palivela818255e2012-10-10 11:33:04 +00002662 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2663 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002664 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002665
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002666 /*
2667 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2668 * in their association response, so ignore that data for our own
2669 * configuration. If it changed since the last beacon, we'll get the
2670 * next beacon and update then.
2671 */
Johannes Berg11289582013-02-07 17:36:12 +01002672
Johannes Bergbee7f582013-02-07 22:24:55 +01002673 /*
2674 * If an operating mode notification IE is present, override the
2675 * NSS calculation (that would be done in rate_control_rate_init())
2676 * and use the # of streams from that element.
2677 */
2678 if (elems.opmode_notif &&
2679 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2680 u8 nss;
2681
2682 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2683 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2684 nss += 1;
2685 sta->sta.rx_nss = nss;
2686 }
2687
Johannes Berg4b7679a2008-09-18 18:14:18 +02002688 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002689
Johannes Berg46900292009-02-15 12:44:28 +01002690 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002691 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002692
Johannes Bergddf4ac52008-10-22 11:41:38 +02002693 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002694 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002695
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002696 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002697 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2698 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2699 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002700 sdata_info(sdata,
2701 "failed to move station %pM to desired state\n",
2702 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002703 WARN_ON(__sta_info_destroy(sta));
2704 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002705 ret = false;
2706 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01002707 }
2708
Johannes Berg7852e362012-01-20 13:55:24 +01002709 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002710
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002711 /*
2712 * Always handle WMM once after association regardless
2713 * of the first value the AP uses. Setting -1 here has
2714 * that effect because the AP values is an unsigned
2715 * 4-bit value.
2716 */
2717 ifmgd->wmm_last_param_set = -1;
2718
Johannes Bergddf4ac52008-10-22 11:41:38 +02002719 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002720 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002721 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002722 else
Johannes Berg3abead52012-03-02 15:56:59 +01002723 ieee80211_set_wmm_default(sdata, false);
2724 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002725
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002726 /* set AID and assoc capability,
2727 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002728 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002729 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002730 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002731
Kalle Valo15b7b062009-03-22 21:57:14 +02002732 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002733 * If we're using 4-addr mode, let the AP know that we're
2734 * doing so, so that it can create the STA VLAN on its side
2735 */
2736 if (ifmgd->use_4addr)
2737 ieee80211_send_4addr_nullfunc(local, sdata);
2738
2739 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002740 * Start timer to probe the connection to the AP now.
2741 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002742 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002743 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002744 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002745
Johannes Berg35d865a2013-05-28 10:54:03 +02002746 ret = true;
2747 out:
2748 kfree(bss_ies);
2749 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002750}
2751
Johannes Berg66e67e42012-01-20 13:55:27 +01002752static enum rx_mgmt_action __must_check
2753ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2754 struct ieee80211_mgmt *mgmt, size_t len,
2755 struct cfg80211_bss **bss)
2756{
2757 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2758 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2759 u16 capab_info, status_code, aid;
2760 struct ieee802_11_elems elems;
2761 u8 *pos;
2762 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002763
Johannes Berg66e67e42012-01-20 13:55:27 +01002764 lockdep_assert_held(&ifmgd->mtx);
2765
2766 if (!assoc_data)
2767 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002768 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002769 return RX_MGMT_NONE;
2770
2771 /*
2772 * AssocResp and ReassocResp have identical structure, so process both
2773 * of them in this function.
2774 */
2775
2776 if (len < 24 + 6)
2777 return RX_MGMT_NONE;
2778
2779 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2780 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2781 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2782 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2783
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002784 sdata_info(sdata,
2785 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2786 reassoc ? "Rea" : "A", mgmt->sa,
2787 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002788
2789 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002790 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002791
2792 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002793 elems.timeout_int &&
2794 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002795 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002796 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002797 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002798 sdata_info(sdata,
2799 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2800 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002801 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002802 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002803 if (ms > IEEE80211_ASSOC_TIMEOUT)
2804 run_again(ifmgd, assoc_data->timeout);
2805 return RX_MGMT_NONE;
2806 }
2807
2808 *bss = assoc_data->bss;
2809
2810 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002811 sdata_info(sdata, "%pM denied association (code=%d)\n",
2812 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002813 ieee80211_destroy_assoc_data(sdata, false);
2814 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002815 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2816 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002817 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002818 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002819 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2820 }
John W. Linville635d9992012-07-09 16:34:34 -04002821 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002822
2823 /*
2824 * destroy assoc_data afterwards, as otherwise an idle
2825 * recalc after assoc_data is NULL but before associated
2826 * is set can cause the interface to go idle
2827 */
2828 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002829 }
2830
2831 return RX_MGMT_CFG80211_RX_ASSOC;
2832}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002833
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002834static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002835 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002836 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002837 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002838{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002839 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002840 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002841 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002842 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002843 bool need_ps = false;
2844
Johannes Bergb4f286a12013-03-26 14:13:58 +01002845 lockdep_assert_held(&sdata->u.mgd.mtx);
2846
Johannes Berg826262c2012-12-10 16:38:14 +02002847 if ((sdata->u.mgd.associated &&
2848 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2849 (sdata->u.mgd.assoc_data &&
2850 ether_addr_equal(mgmt->bssid,
2851 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002852 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002853 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2854
2855 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002856 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002857 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2858 }
Johannes Berg56007a02010-01-26 14:19:52 +01002859 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002860
Johannes Berg1cd8e882013-03-27 14:30:12 +01002861 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002862 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2863 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002864 else
2865 freq = rx_status->freq;
2866
2867 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2868
2869 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2870 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002871
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002872 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002873 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002874 if (bss)
2875 ieee80211_rx_bss_put(local, bss);
2876
Johannes Berg37799e52013-03-26 14:02:26 +01002877 if (!sdata->u.mgd.associated ||
2878 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002879 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002880
Johannes Berg56007a02010-01-26 14:19:52 +01002881 if (need_ps) {
2882 mutex_lock(&local->iflist_mtx);
2883 ieee80211_recalc_ps(local, -1);
2884 mutex_unlock(&local->iflist_mtx);
2885 }
2886
Johannes Berg36709462013-05-03 09:35:35 +02002887 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2888 elems, true);
Johannes Bergb4f286a12013-03-26 14:13:58 +01002889
Jiri Bencf0706e82007-05-05 11:45:53 -07002890}
2891
2892
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002893static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002894 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002895{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002896 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002897 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002898 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2899 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002900 struct ieee802_11_elems elems;
2901
Kalle Valo15b7b062009-03-22 21:57:14 +02002902 ifmgd = &sdata->u.mgd;
2903
Johannes Berg77fdaa12009-07-07 03:45:17 +02002904 ASSERT_MGD_MTX(ifmgd);
2905
Joe Perchesb203ca32012-05-08 18:56:52 +00002906 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002907 return; /* ignore ProbeResp to foreign address */
2908
Ester Kummerae6a44e2008-06-27 18:54:48 +03002909 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2910 if (baselen > len)
2911 return;
2912
2913 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002914 false, &elems);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002915
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002916 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002917
Johannes Berg77fdaa12009-07-07 03:45:17 +02002918 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002919 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002920 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002921
2922 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002923 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002924 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002925 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002926 ifmgd->auth_data->tries = 0;
2927 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002928 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002929 run_again(ifmgd, ifmgd->auth_data->timeout);
2930 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002931}
2932
Johannes Bergd91f36d2009-04-16 13:17:26 +02002933/*
2934 * This is the canonical list of information elements we care about,
2935 * the filter code also gives us all changes to the Microsoft OUI
2936 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2937 *
2938 * We implement beacon filtering in software since that means we can
2939 * avoid processing the frame here and in cfg80211, and userspace
2940 * will not be able to tell whether the hardware supports it or not.
2941 *
2942 * XXX: This list needs to be dynamic -- userspace needs to be able to
2943 * add items it requires. It also needs to be able to tell us to
2944 * look out for other vendor IEs.
2945 */
2946static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002947 (1ULL << WLAN_EID_COUNTRY) |
2948 (1ULL << WLAN_EID_ERP_INFO) |
2949 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2950 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2951 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002952 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002953
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002954static enum rx_mgmt_action
2955ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2956 struct ieee80211_mgmt *mgmt, size_t len,
2957 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002958{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002959 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002960 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002961 size_t baselen;
2962 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002963 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002964 struct ieee80211_chanctx_conf *chanctx_conf;
2965 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002966 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002967 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002968 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002969 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002970 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002971 u8 *bssid;
2972
Johannes Berg66e67e42012-01-20 13:55:27 +01002973 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002974
Ester Kummerae6a44e2008-06-27 18:54:48 +03002975 /* Process beacon from the current BSS */
2976 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2977 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002978 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002979
Johannes Berg55de9082012-07-26 17:24:39 +02002980 rcu_read_lock();
2981 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2982 if (!chanctx_conf) {
2983 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002984 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002985 }
2986
Johannes Berg4bf88532012-11-09 11:39:59 +01002987 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002988 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002989 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002990 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002991 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002992 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002993
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002994 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002995 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002996 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002997 len - baselen, false, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002998
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002999 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01003000 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003001 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01003002 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3003 sdata->vif.bss_conf.sync_tsf =
3004 le64_to_cpu(mgmt->u.beacon.timestamp);
3005 sdata->vif.bss_conf.sync_device_ts =
3006 rx_status->device_timestamp;
3007 if (elems.tim)
3008 sdata->vif.bss_conf.sync_dtim_count =
3009 elems.tim->dtim_count;
3010 else
3011 sdata->vif.bss_conf.sync_dtim_count = 0;
3012 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003013 /* continue assoc process */
3014 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01003015 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003016 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003017 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01003018 }
3019
3020 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00003021 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003022 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003023 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003024
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003025 /* Track average RSSI from the Beacon frames of the current AP */
3026 ifmgd->last_beacon_signal = rx_status->signal;
3027 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3028 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03003029 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003030 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003031 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003032 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003033 } else {
3034 ifmgd->ave_beacon_signal =
3035 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
3036 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
3037 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03003038 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003039 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003040
3041 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3042 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3043 int sig = ifmgd->ave_beacon_signal;
3044 int last_sig = ifmgd->last_ave_beacon_signal;
3045
3046 /*
3047 * if signal crosses either of the boundaries, invoke callback
3048 * with appropriate parameters
3049 */
3050 if (sig > ifmgd->rssi_max_thold &&
3051 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3052 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02003053 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003054 } else if (sig < ifmgd->rssi_min_thold &&
3055 (last_sig >= ifmgd->rssi_max_thold ||
3056 last_sig == 0)) {
3057 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02003058 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003059 }
3060 }
3061
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003062 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003063 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003064 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003065 int sig = ifmgd->ave_beacon_signal / 16;
3066 int last_event = ifmgd->last_cqm_event_signal;
3067 int thold = bss_conf->cqm_rssi_thold;
3068 int hyst = bss_conf->cqm_rssi_hyst;
3069 if (sig < thold &&
3070 (last_event == 0 || sig < last_event - hyst)) {
3071 ifmgd->last_cqm_event_signal = sig;
3072 ieee80211_cqm_rssi_notify(
3073 &sdata->vif,
3074 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3075 GFP_KERNEL);
3076 } else if (sig > thold &&
3077 (last_event == 0 || sig > last_event + hyst)) {
3078 ifmgd->last_cqm_event_signal = sig;
3079 ieee80211_cqm_rssi_notify(
3080 &sdata->vif,
3081 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3082 GFP_KERNEL);
3083 }
3084 }
3085
Johannes Bergb291ba12009-07-10 15:29:03 +02003086 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003087 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01003088 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003089 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02003090 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003091 ieee80211_run_deferred_scan(local);
3092 mutex_unlock(&local->mtx);
3093
Johannes Berg4e751842009-06-10 15:16:52 +02003094 mutex_lock(&local->iflist_mtx);
3095 ieee80211_recalc_ps(local, -1);
3096 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03003097 }
3098
Johannes Bergb291ba12009-07-10 15:29:03 +02003099 /*
3100 * Push the beacon loss detection into the future since
3101 * we are processing a beacon from the AP just now.
3102 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003103 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003104
Johannes Bergd91f36d2009-04-16 13:17:26 +02003105 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3106 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003107 len - baselen, false, &elems,
Johannes Bergd91f36d2009-04-16 13:17:26 +02003108 care_about_ies, ncrc);
3109
Vivek Natarajan25c9c872009-03-02 20:20:30 +05303110 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02003111 bool directed_tim = ieee80211_check_tim(elems.tim,
3112 elems.tim_len,
3113 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02003114 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02003115 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01003116 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3117 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3118 ieee80211_hw_config(local,
3119 IEEE80211_CONF_CHANGE_PS);
3120 }
Kalle Valo572e0012009-02-10 17:09:31 +02003121 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05303122 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02003123 local->pspolling = true;
3124
3125 /*
3126 * Here is assumed that the driver will be
3127 * able to send ps-poll frame and receive a
3128 * response even though power save mode is
3129 * enabled, but some drivers might require
3130 * to disable power save here. This needs
3131 * to be investigated.
3132 */
3133 ieee80211_send_pspoll(local, sdata);
3134 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08003135 }
3136 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003137
Johannes Berg488dd7b2012-10-29 20:08:01 +01003138 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003139 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01003140 int ret;
3141
3142 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3143 len - baselen,
3144 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01003145 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003146 if (ret >= 2) {
3147 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3148 /* valid noa_attr and index changed */
3149 sdata->u.mgd.p2p_noa_index = noa.index;
3150 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3151 changed |= BSS_CHANGED_P2P_PS;
3152 /*
3153 * make sure we update all information, the CRC
3154 * mechanism doesn't look at P2P attributes.
3155 */
3156 ifmgd->beacon_crc_valid = false;
3157 }
3158 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3159 /* noa_attr not found and we had valid noa_attr before */
3160 sdata->u.mgd.p2p_noa_index = -1;
3161 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01003162 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01003163 ifmgd->beacon_crc_valid = false;
3164 }
3165 }
3166
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003167 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003168 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03003169 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003170 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03003171
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02003172 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02003173
3174 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3175 elems.wmm_param_len))
3176 changed |= BSS_CHANGED_QOS;
3177
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003178 /*
3179 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01003180 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003181 */
3182 if (!bss_conf->dtim_period) {
3183 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3184 if (elems.tim)
3185 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3186 else
3187 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003188
3189 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3190 sdata->vif.bss_conf.sync_tsf =
3191 le64_to_cpu(mgmt->u.beacon.timestamp);
3192 sdata->vif.bss_conf.sync_device_ts =
3193 rx_status->device_timestamp;
3194 if (elems.tim)
3195 sdata->vif.bss_conf.sync_dtim_count =
3196 elems.tim->dtim_count;
3197 else
3198 sdata->vif.bss_conf.sync_dtim_count = 0;
3199 }
3200
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003201 changed |= BSS_CHANGED_DTIM_PERIOD;
3202 }
3203
Johannes Berg1946bed2013-03-27 14:31:53 +01003204 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003205 erp_valid = true;
3206 erp_value = elems.erp_info[0];
3207 } else {
3208 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003209 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003210 changed |= ieee80211_handle_bss_capability(sdata,
3211 le16_to_cpu(mgmt->u.beacon.capab_info),
3212 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003213
Johannes Berg11289582013-02-07 17:36:12 +01003214 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003215 sta = sta_info_get(sdata, bssid);
3216
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003217 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3218 elems.vht_operation, bssid, &changed)) {
3219 mutex_unlock(&local->sta_mtx);
3220 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3221 WLAN_REASON_DEAUTH_LEAVING,
3222 true, deauth_buf);
3223 return RX_MGMT_CFG80211_TX_DEAUTH;
3224 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003225
3226 if (sta && elems.opmode_notif)
3227 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3228 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003229 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003230
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003231 if (elems.country_elem && elems.pwr_constr_elem &&
3232 mgmt->u.probe_resp.capab_info &
3233 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003234 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3235 elems.country_elem,
3236 elems.country_elem_len,
3237 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003238
Johannes Berg471b3ef2007-12-28 14:32:58 +01003239 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003240
3241 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003242}
3243
Johannes Berg1fa57d02010-06-10 10:21:32 +02003244void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3245 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003246{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003247 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003248 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003249 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003250 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003251 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003252 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003253 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003254 struct ieee802_11_elems elems;
3255 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07003256
Jiri Bencf0706e82007-05-05 11:45:53 -07003257 rx_status = (struct ieee80211_rx_status *) skb->cb;
3258 mgmt = (struct ieee80211_mgmt *) skb->data;
3259 fc = le16_to_cpu(mgmt->frame_control);
3260
Johannes Berg77fdaa12009-07-07 03:45:17 +02003261 mutex_lock(&ifmgd->mtx);
3262
Johannes Berg66e67e42012-01-20 13:55:27 +01003263 switch (fc & IEEE80211_FCTL_STYPE) {
3264 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003265 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3266 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003267 break;
3268 case IEEE80211_STYPE_PROBE_RESP:
3269 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3270 break;
3271 case IEEE80211_STYPE_AUTH:
3272 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3273 break;
3274 case IEEE80211_STYPE_DEAUTH:
3275 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3276 break;
3277 case IEEE80211_STYPE_DISASSOC:
3278 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3279 break;
3280 case IEEE80211_STYPE_ASSOC_RESP:
3281 case IEEE80211_STYPE_REASSOC_RESP:
3282 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3283 break;
3284 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01003285 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003286 ies_len = skb->len -
3287 offsetof(struct ieee80211_mgmt,
3288 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01003289
3290 if (ies_len < 0)
3291 break;
3292
3293 ieee802_11_parse_elems(
3294 mgmt->u.action.u.chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003295 ies_len, true, &elems);
Johannes Berg37799e52013-03-26 14:02:26 +01003296
3297 if (elems.parse_error)
3298 break;
3299
Johannes Berg66e67e42012-01-20 13:55:27 +01003300 ieee80211_sta_process_chanswitch(sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01003301 rx_status->mactime,
Johannes Berg36709462013-05-03 09:35:35 +02003302 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003303 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3304 ies_len = skb->len -
3305 offsetof(struct ieee80211_mgmt,
3306 u.action.u.ext_chan_switch.variable);
3307
3308 if (ies_len < 0)
3309 break;
3310
3311 ieee802_11_parse_elems(
3312 mgmt->u.action.u.ext_chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003313 ies_len, true, &elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003314
3315 if (elems.parse_error)
3316 break;
3317
3318 /* for the handling code pretend this was also an IE */
3319 elems.ext_chansw_ie =
3320 &mgmt->u.action.u.ext_chan_switch.data;
3321
3322 ieee80211_sta_process_chanswitch(sdata,
3323 rx_status->mactime,
Johannes Berg36709462013-05-03 09:35:35 +02003324 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003325 }
Johannes Berg37799e52013-03-26 14:02:26 +01003326 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003327 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003328 mutex_unlock(&ifmgd->mtx);
3329
Johannes Berg66e67e42012-01-20 13:55:27 +01003330 switch (rma) {
3331 case RX_MGMT_NONE:
3332 /* no action */
3333 break;
3334 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003335 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003336 break;
3337 case RX_MGMT_CFG80211_DISASSOC:
3338 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3339 break;
3340 case RX_MGMT_CFG80211_RX_AUTH:
3341 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3342 break;
3343 case RX_MGMT_CFG80211_RX_ASSOC:
3344 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3345 break;
3346 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3347 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3348 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003349 case RX_MGMT_CFG80211_TX_DEAUTH:
3350 cfg80211_send_deauth(sdata->dev, deauth_buf,
3351 sizeof(deauth_buf));
3352 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003353 default:
3354 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003355 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003356}
3357
Johannes Berg9c6bd792008-09-11 00:01:52 +02003358static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003359{
3360 struct ieee80211_sub_if_data *sdata =
3361 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003362
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003363 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003364}
3365
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003366static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003367 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003368{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003369 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003370 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003371
Johannes Berg37ad3882012-02-24 13:50:54 +01003372 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003373 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003374 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003375
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003376 /*
3377 * must be outside lock due to cfg80211,
3378 * but that's not a problem.
3379 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003380 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003381
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003382 mutex_lock(&ifmgd->mtx);
3383}
3384
Johannes Berg66e67e42012-01-20 13:55:27 +01003385static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3386{
3387 struct ieee80211_local *local = sdata->local;
3388 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3389 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003390 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003391
3392 lockdep_assert_held(&ifmgd->mtx);
3393
3394 if (WARN_ON_ONCE(!auth_data))
3395 return -EINVAL;
3396
Johannes Berg66e67e42012-01-20 13:55:27 +01003397 auth_data->tries++;
3398
3399 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003400 sdata_info(sdata, "authentication with %pM timed out\n",
3401 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003402
3403 /*
3404 * Most likely AP is not in the range so remove the
3405 * bss struct for that AP.
3406 */
3407 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3408
3409 return -ETIMEDOUT;
3410 }
3411
Johannes Berga1845fc2012-06-27 13:18:36 +02003412 drv_mgd_prepare_tx(local, sdata);
3413
Johannes Berg66e67e42012-01-20 13:55:27 +01003414 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003415 u16 trans = 1;
3416 u16 status = 0;
3417
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003418 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3419 auth_data->bss->bssid, auth_data->tries,
3420 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003421
3422 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003423
3424 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3425 trans = auth_data->sae_trans;
3426 status = auth_data->sae_status;
3427 auth_data->expected_transaction = trans;
3428 }
3429
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003430 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3431 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3432 IEEE80211_TX_INTFL_MLME_CONN_TX;
3433
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003434 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3435 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003436 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003437 auth_data->bss->bssid, NULL, 0, 0,
3438 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003439 } else {
3440 const u8 *ssidie;
3441
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003442 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3443 auth_data->bss->bssid, auth_data->tries,
3444 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003445
Johannes Berg9caf0362012-11-29 01:25:20 +01003446 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003447 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003448 if (!ssidie) {
3449 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003450 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003451 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003452 /*
3453 * Direct probe is sent to broadcast address as some APs
3454 * will not answer to direct packet in unassociated state.
3455 */
3456 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003457 NULL, 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02003458 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003459 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003460 }
3461
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003462 if (tx_flags == 0) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01003463 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003464 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003465 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003466 } else {
3467 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003468 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003469
3470 return 0;
3471}
3472
3473static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3474{
3475 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3476 struct ieee80211_local *local = sdata->local;
3477
3478 lockdep_assert_held(&sdata->u.mgd.mtx);
3479
Johannes Berg66e67e42012-01-20 13:55:27 +01003480 assoc_data->tries++;
3481 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003482 sdata_info(sdata, "association with %pM timed out\n",
3483 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003484
3485 /*
3486 * Most likely AP is not in the range so remove the
3487 * bss struct for that AP.
3488 */
3489 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3490
3491 return -ETIMEDOUT;
3492 }
3493
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003494 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3495 assoc_data->bss->bssid, assoc_data->tries,
3496 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003497 ieee80211_send_assoc(sdata);
3498
Johannes Berg1672c0e32013-01-29 15:02:27 +01003499 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3500 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003501 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003502 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003503 } else {
3504 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003505 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003506
3507 return 0;
3508}
3509
Johannes Berg1672c0e32013-01-29 15:02:27 +01003510void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3511 __le16 fc, bool acked)
3512{
3513 struct ieee80211_local *local = sdata->local;
3514
3515 sdata->u.mgd.status_fc = fc;
3516 sdata->u.mgd.status_acked = acked;
3517 sdata->u.mgd.status_received = true;
3518
3519 ieee80211_queue_work(&local->hw, &sdata->work);
3520}
3521
Johannes Berg1fa57d02010-06-10 10:21:32 +02003522void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003523{
Johannes Berg60f8b392008-09-08 17:44:22 +02003524 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003525 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003526
Johannes Berg77fdaa12009-07-07 03:45:17 +02003527 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003528
Johannes Berg1672c0e32013-01-29 15:02:27 +01003529 if (ifmgd->status_received) {
3530 __le16 fc = ifmgd->status_fc;
3531 bool status_acked = ifmgd->status_acked;
3532
3533 ifmgd->status_received = false;
3534 if (ifmgd->auth_data &&
3535 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3536 if (status_acked) {
3537 ifmgd->auth_data->timeout =
3538 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3539 run_again(ifmgd, ifmgd->auth_data->timeout);
3540 } else {
3541 ifmgd->auth_data->timeout = jiffies - 1;
3542 }
Johannes Berg89afe612013-02-13 15:39:57 +01003543 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003544 } else if (ifmgd->assoc_data &&
3545 (ieee80211_is_assoc_req(fc) ||
3546 ieee80211_is_reassoc_req(fc))) {
3547 if (status_acked) {
3548 ifmgd->assoc_data->timeout =
3549 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3550 run_again(ifmgd, ifmgd->assoc_data->timeout);
3551 } else {
3552 ifmgd->assoc_data->timeout = jiffies - 1;
3553 }
Johannes Berg89afe612013-02-13 15:39:57 +01003554 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003555 }
3556 }
3557
Johannes Berg89afe612013-02-13 15:39:57 +01003558 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003559 time_after(jiffies, ifmgd->auth_data->timeout)) {
3560 if (ifmgd->auth_data->done) {
3561 /*
3562 * ok ... we waited for assoc but userspace didn't,
3563 * so let's just kill the auth data
3564 */
3565 ieee80211_destroy_auth_data(sdata, false);
3566 } else if (ieee80211_probe_auth(sdata)) {
3567 u8 bssid[ETH_ALEN];
3568
3569 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3570
3571 ieee80211_destroy_auth_data(sdata, false);
3572
3573 mutex_unlock(&ifmgd->mtx);
3574 cfg80211_send_auth_timeout(sdata->dev, bssid);
3575 mutex_lock(&ifmgd->mtx);
3576 }
Johannes Berg89afe612013-02-13 15:39:57 +01003577 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003578 run_again(ifmgd, ifmgd->auth_data->timeout);
3579
Johannes Berg89afe612013-02-13 15:39:57 +01003580 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003581 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003582 if ((ifmgd->assoc_data->need_beacon &&
3583 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003584 ieee80211_do_assoc(sdata)) {
3585 u8 bssid[ETH_ALEN];
3586
3587 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3588
3589 ieee80211_destroy_assoc_data(sdata, false);
3590
3591 mutex_unlock(&ifmgd->mtx);
3592 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3593 mutex_lock(&ifmgd->mtx);
3594 }
Johannes Berg89afe612013-02-13 15:39:57 +01003595 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003596 run_again(ifmgd, ifmgd->assoc_data->timeout);
3597
Johannes Bergb291ba12009-07-10 15:29:03 +02003598 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3599 IEEE80211_STA_CONNECTION_POLL) &&
3600 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003601 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003602 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003603
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003604 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003605
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003606 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003607 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003608 else
Ben Greear180205b2011-02-04 15:30:24 -08003609 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003610
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003611 /* ACK received for nullfunc probing frame */
3612 if (!ifmgd->probe_send_count)
3613 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003614 else if (ifmgd->nullfunc_failed) {
3615 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003616 mlme_dbg(sdata,
3617 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3618 bssid, ifmgd->probe_send_count,
3619 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003620 ieee80211_mgd_probe_ap_send(sdata);
3621 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003622 mlme_dbg(sdata,
3623 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3624 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003625 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003626 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3627 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003628 }
3629 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003630 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003631 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003632 mlme_dbg(sdata,
3633 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3634 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003635 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003636 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003637 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003638 mlme_dbg(sdata,
3639 "No probe response from AP %pM after %dms, try %d/%i\n",
3640 bssid, probe_wait_ms,
3641 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003642 ieee80211_mgd_probe_ap_send(sdata);
3643 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003644 /*
3645 * We actually lost the connection ... or did we?
3646 * Let's make sure!
3647 */
Ben Greearb38afa82010-10-07 16:12:06 -07003648 wiphy_debug(local->hw.wiphy,
3649 "%s: No probe response from AP %pM"
3650 " after %dms, disconnecting.\n",
3651 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003652 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003653
Johannes Berg95acac62011-07-12 12:30:59 +02003654 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003655 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003656 }
3657 }
3658
Johannes Berg77fdaa12009-07-07 03:45:17 +02003659 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003660}
Johannes Berg0a51b272008-09-08 17:44:25 +02003661
Johannes Bergb291ba12009-07-10 15:29:03 +02003662static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3663{
3664 struct ieee80211_sub_if_data *sdata =
3665 (struct ieee80211_sub_if_data *) data;
3666 struct ieee80211_local *local = sdata->local;
3667
3668 if (local->quiescing)
3669 return;
3670
Johannes Berg682bd382013-01-29 13:13:50 +01003671 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003672 ieee80211_queue_work(&sdata->local->hw,
3673 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003674}
3675
3676static void ieee80211_sta_conn_mon_timer(unsigned long data)
3677{
3678 struct ieee80211_sub_if_data *sdata =
3679 (struct ieee80211_sub_if_data *) data;
3680 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3681 struct ieee80211_local *local = sdata->local;
3682
3683 if (local->quiescing)
3684 return;
3685
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003686 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003687}
3688
3689static void ieee80211_sta_monitor_work(struct work_struct *work)
3690{
3691 struct ieee80211_sub_if_data *sdata =
3692 container_of(work, struct ieee80211_sub_if_data,
3693 u.mgd.monitor_work);
3694
3695 ieee80211_mgd_probe_ap(sdata, false);
3696}
3697
Johannes Berga1678f82008-09-11 00:01:47 +02003698static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3699{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003700 u32 flags;
3701
Kalle Vaload935682009-04-19 08:47:19 +03003702 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003703 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003704
Johannes Bergb291ba12009-07-10 15:29:03 +02003705 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003706 flags = sdata->local->hw.flags;
3707 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3708 ieee80211_queue_work(&sdata->local->hw,
3709 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003710 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003711 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003712 }
Johannes Berga1678f82008-09-11 00:01:47 +02003713}
3714
Johannes Bergb8360ab2013-04-29 14:57:44 +02003715#ifdef CONFIG_PM
3716void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3717{
3718 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3719
3720 mutex_lock(&ifmgd->mtx);
3721 if (!ifmgd->associated) {
3722 mutex_unlock(&ifmgd->mtx);
3723 return;
3724 }
3725
3726 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3727 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3728 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3729 ieee80211_sta_connection_lost(sdata,
3730 ifmgd->associated->bssid,
3731 WLAN_REASON_UNSPECIFIED,
3732 true);
3733 mutex_unlock(&ifmgd->mtx);
3734 return;
3735 }
3736 mutex_unlock(&ifmgd->mtx);
3737}
3738#endif
3739
Johannes Berg9c6bd792008-09-11 00:01:52 +02003740/* interface setup */
3741void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3742{
Johannes Berg46900292009-02-15 12:44:28 +01003743 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003744
Johannes Berg46900292009-02-15 12:44:28 +01003745 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003746 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003747 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003748 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3749 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003750 INIT_WORK(&ifmgd->csa_connection_drop_work,
3751 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003752 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003753 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003754 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003755 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3756 (unsigned long) sdata);
3757 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3758 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003759 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303760 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003761
Johannes Bergab1faea2009-07-01 21:41:17 +02003762 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303763 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003764 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3765 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003766 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003767
Johannes Berg77fdaa12009-07-07 03:45:17 +02003768 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003769
3770 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3771 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3772 else
3773 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003774}
3775
3776/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003777void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3778{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003779 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003780
3781 /* Restart STA timers */
3782 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07003783 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3784 if (ieee80211_sdata_running(sdata))
3785 ieee80211_restart_sta_timer(sdata);
3786 }
Johannes Berga1678f82008-09-11 00:01:47 +02003787 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003788}
Johannes Berg10f644a2009-04-16 13:17:25 +02003789
3790int ieee80211_max_network_latency(struct notifier_block *nb,
3791 unsigned long data, void *dummy)
3792{
3793 s32 latency_usec = (s32) data;
3794 struct ieee80211_local *local =
3795 container_of(nb, struct ieee80211_local,
3796 network_latency_notifier);
3797
3798 mutex_lock(&local->iflist_mtx);
3799 ieee80211_recalc_ps(local, latency_usec);
3800 mutex_unlock(&local->iflist_mtx);
3801
3802 return 0;
3803}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003804
Johannes Bergf2d9d272012-11-22 14:11:39 +01003805static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3806 struct cfg80211_bss *cbss)
3807{
3808 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3809 const u8 *ht_cap_ie, *vht_cap_ie;
3810 const struct ieee80211_ht_cap *ht_cap;
3811 const struct ieee80211_vht_cap *vht_cap;
3812 u8 chains = 1;
3813
3814 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3815 return chains;
3816
Johannes Berg9caf0362012-11-29 01:25:20 +01003817 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003818 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3819 ht_cap = (void *)(ht_cap_ie + 2);
3820 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3821 /*
3822 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3823 * "Tx Unequal Modulation Supported" fields.
3824 */
3825 }
3826
3827 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3828 return chains;
3829
Johannes Berg9caf0362012-11-29 01:25:20 +01003830 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003831 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3832 u8 nss;
3833 u16 tx_mcs_map;
3834
3835 vht_cap = (void *)(vht_cap_ie + 2);
3836 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3837 for (nss = 8; nss > 0; nss--) {
3838 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3839 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3840 break;
3841 }
3842 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3843 chains = max(chains, nss);
3844 }
3845
3846 return chains;
3847}
3848
Johannes Bergb17166a2012-07-27 11:41:27 +02003849static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3850 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003851{
3852 struct ieee80211_local *local = sdata->local;
3853 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003854 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003855 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003856 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003857 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003858 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003859
Johannes Berg24398e32012-03-28 10:58:36 +02003860 sband = local->hw.wiphy->bands[cbss->channel->band];
3861
Johannes Bergf2d9d272012-11-22 14:11:39 +01003862 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3863 IEEE80211_STA_DISABLE_80P80MHZ |
3864 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003865
Johannes Berg9caf0362012-11-29 01:25:20 +01003866 rcu_read_lock();
3867
Johannes Bergf2d9d272012-11-22 14:11:39 +01003868 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3869 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003870 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003871
Johannes Berg9caf0362012-11-29 01:25:20 +01003872 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003873 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3874 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003875
3876 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3877 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3878 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3879 ht_oper = NULL;
3880 }
Johannes Berg24398e32012-03-28 10:58:36 +02003881 }
3882
Johannes Bergf2d9d272012-11-22 14:11:39 +01003883 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3884 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003885 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003886
Johannes Berg9caf0362012-11-29 01:25:20 +01003887 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3888 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003889 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3890 vht_oper = (void *)(vht_oper_ie + 2);
3891 if (vht_oper && !ht_oper) {
3892 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003893 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003894 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003895 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3896 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003897 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003898
3899 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3900 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3901 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3902 vht_oper = NULL;
3903 }
Johannes Berg24398e32012-03-28 10:58:36 +02003904 }
3905
Johannes Bergf2d9d272012-11-22 14:11:39 +01003906 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3907 cbss->channel,
3908 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003909 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003910
Johannes Bergf2d9d272012-11-22 14:11:39 +01003911 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3912 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003913
Johannes Berg9caf0362012-11-29 01:25:20 +01003914 rcu_read_unlock();
3915
Johannes Berg04ecd252012-09-11 14:34:12 +02003916 /* will change later if needed */
3917 sdata->smps_mode = IEEE80211_SMPS_OFF;
3918
Johannes Bergf2d9d272012-11-22 14:11:39 +01003919 /*
3920 * If this fails (possibly due to channel context sharing
3921 * on incompatible channels, e.g. 80+80 and 160 sharing the
3922 * same control channel) try to use a smaller bandwidth.
3923 */
3924 ret = ieee80211_vif_use_channel(sdata, &chandef,
3925 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003926 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003927 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003928 ret = ieee80211_vif_use_channel(sdata, &chandef,
3929 IEEE80211_CHANCTX_SHARED);
3930 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003931 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003932}
3933
3934static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3935 struct cfg80211_bss *cbss, bool assoc)
3936{
3937 struct ieee80211_local *local = sdata->local;
3938 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3939 struct ieee80211_bss *bss = (void *)cbss->priv;
3940 struct sta_info *new_sta = NULL;
3941 bool have_sta = false;
3942 int err;
3943
3944 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3945 return -EINVAL;
3946
3947 if (assoc) {
3948 rcu_read_lock();
3949 have_sta = sta_info_get(sdata, cbss->bssid);
3950 rcu_read_unlock();
3951 }
3952
3953 if (!have_sta) {
3954 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3955 if (!new_sta)
3956 return -ENOMEM;
3957 }
3958
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003959 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003960 u32 rates = 0, basic_rates = 0;
3961 bool have_higher_than_11mbit;
3962 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003963 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003964 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003965
3966 sband = local->hw.wiphy->bands[cbss->channel->band];
3967
3968 err = ieee80211_prep_channel(sdata, cbss);
3969 if (err) {
3970 sta_info_free(local, new_sta);
3971 return err;
3972 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003973
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003974 ieee80211_get_rates(sband, bss->supp_rates,
3975 bss->supp_rates_len,
3976 &rates, &basic_rates,
3977 &have_higher_than_11mbit,
3978 &min_rate, &min_rate_index);
3979
3980 /*
3981 * This used to be a workaround for basic rates missing
3982 * in the association response frame. Now that we no
3983 * longer use the basic rates from there, it probably
3984 * doesn't happen any more, but keep the workaround so
3985 * in case some *other* APs are buggy in different ways
3986 * we can connect -- with a warning.
3987 */
3988 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003989 sdata_info(sdata,
3990 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003991 basic_rates = BIT(min_rate_index);
3992 }
3993
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003994 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003995 sdata->vif.bss_conf.basic_rates = basic_rates;
3996
3997 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003998 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003999 have_higher_than_11mbit)
4000 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4001 else
4002 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4003
4004 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4005
Johannes Berg8c358bc2012-05-22 22:13:05 +02004006 /* set timing information */
4007 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01004008 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01004009 ies = rcu_dereference(cbss->beacon_ies);
4010 if (ies) {
4011 const u8 *tim_ie;
4012
4013 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4014 sdata->vif.bss_conf.sync_device_ts =
4015 bss->device_ts_beacon;
4016 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4017 ies->data, ies->len);
4018 if (tim_ie && tim_ie[1] >= 2)
4019 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
4020 else
4021 sdata->vif.bss_conf.sync_dtim_count = 0;
4022 } else if (!(local->hw.flags &
4023 IEEE80211_HW_TIMING_BEACON_ONLY)) {
4024 ies = rcu_dereference(cbss->proberesp_ies);
4025 /* must be non-NULL since beacon IEs were NULL */
4026 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4027 sdata->vif.bss_conf.sync_device_ts =
4028 bss->device_ts_presp;
4029 sdata->vif.bss_conf.sync_dtim_count = 0;
4030 } else {
4031 sdata->vif.bss_conf.sync_tsf = 0;
4032 sdata->vif.bss_conf.sync_device_ts = 0;
4033 sdata->vif.bss_conf.sync_dtim_count = 0;
4034 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01004035 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02004036
4037 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01004038 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02004039 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
4040 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01004041
4042 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02004043 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01004044
Johannes Berg13e0c8e2012-07-27 10:43:16 +02004045 err = sta_info_insert(new_sta);
4046 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01004047 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004048 sdata_info(sdata,
4049 "failed to insert STA entry for the AP (error %d)\n",
4050 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01004051 return err;
4052 }
4053 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00004054 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01004055
4056 return 0;
4057}
4058
Johannes Berg77fdaa12009-07-07 03:45:17 +02004059/* config hooks */
4060int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4061 struct cfg80211_auth_request *req)
4062{
Johannes Berg66e67e42012-01-20 13:55:27 +01004063 struct ieee80211_local *local = sdata->local;
4064 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4065 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004066 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01004067 int err;
4068
4069 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02004070
4071 switch (req->auth_type) {
4072 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4073 auth_alg = WLAN_AUTH_OPEN;
4074 break;
4075 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01004076 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02004077 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004078 auth_alg = WLAN_AUTH_SHARED_KEY;
4079 break;
4080 case NL80211_AUTHTYPE_FT:
4081 auth_alg = WLAN_AUTH_FT;
4082 break;
4083 case NL80211_AUTHTYPE_NETWORK_EAP:
4084 auth_alg = WLAN_AUTH_LEAP;
4085 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004086 case NL80211_AUTHTYPE_SAE:
4087 auth_alg = WLAN_AUTH_SAE;
4088 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004089 default:
4090 return -EOPNOTSUPP;
4091 }
4092
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004093 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
4094 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01004095 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004096 return -ENOMEM;
4097
Johannes Berg66e67e42012-01-20 13:55:27 +01004098 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004099
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004100 if (req->sae_data_len >= 4) {
4101 __le16 *pos = (__le16 *) req->sae_data;
4102 auth_data->sae_trans = le16_to_cpu(pos[0]);
4103 auth_data->sae_status = le16_to_cpu(pos[1]);
4104 memcpy(auth_data->data, req->sae_data + 4,
4105 req->sae_data_len - 4);
4106 auth_data->data_len += req->sae_data_len - 4;
4107 }
4108
Johannes Berg77fdaa12009-07-07 03:45:17 +02004109 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004110 memcpy(&auth_data->data[auth_data->data_len],
4111 req->ie, req->ie_len);
4112 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004113 }
4114
Johannes Bergfffd0932009-07-08 14:22:54 +02004115 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004116 auth_data->key_len = req->key_len;
4117 auth_data->key_idx = req->key_idx;
4118 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02004119 }
4120
Johannes Berg66e67e42012-01-20 13:55:27 +01004121 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004122
Johannes Berg66e67e42012-01-20 13:55:27 +01004123 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01004124
Johannes Berg66e67e42012-01-20 13:55:27 +01004125 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004126
Johannes Berg66e67e42012-01-20 13:55:27 +01004127 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4128 ifmgd->assoc_data) {
4129 err = -EBUSY;
4130 goto err_free;
4131 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004132
Johannes Berg66e67e42012-01-20 13:55:27 +01004133 if (ifmgd->auth_data)
4134 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03004135
Johannes Berg66e67e42012-01-20 13:55:27 +01004136 /* prep auth_data so we don't go into idle on disassoc */
4137 ifmgd->auth_data = auth_data;
4138
Johannes Berg7b119dc2013-04-10 21:38:36 +02004139 if (ifmgd->associated) {
4140 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4141
4142 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4143 WLAN_REASON_UNSPECIFIED,
4144 false, frame_buf);
4145
4146 __cfg80211_send_deauth(sdata->dev, frame_buf,
4147 sizeof(frame_buf));
4148 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004149
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004150 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004151
Johannes Berga1cf7752012-03-08 15:02:07 +01004152 err = ieee80211_prep_connection(sdata, req->bss, false);
4153 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01004154 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004155
Johannes Berg66e67e42012-01-20 13:55:27 +01004156 err = ieee80211_probe_auth(sdata);
4157 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004158 sta_info_destroy_addr(sdata, req->bss->bssid);
4159 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004160 }
4161
Johannes Berg66e67e42012-01-20 13:55:27 +01004162 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01004163 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01004164 err = 0;
4165 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02004166
Johannes Berg66e67e42012-01-20 13:55:27 +01004167 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004168 memset(ifmgd->bssid, 0, ETH_ALEN);
4169 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004170 ifmgd->auth_data = NULL;
4171 err_free:
4172 kfree(auth_data);
4173 out_unlock:
4174 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02004175
Johannes Berg66e67e42012-01-20 13:55:27 +01004176 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004177}
4178
Johannes Berg77fdaa12009-07-07 03:45:17 +02004179int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4180 struct cfg80211_assoc_request *req)
4181{
Johannes Berg66e67e42012-01-20 13:55:27 +01004182 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004184 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004185 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004186 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004187 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004188 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004189 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004190
Johannes Berg66e67e42012-01-20 13:55:27 +01004191 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4192 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004193 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004194
Johannes Berg9caf0362012-11-29 01:25:20 +01004195 rcu_read_lock();
4196 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4197 if (!ssidie) {
4198 rcu_read_unlock();
4199 kfree(assoc_data);
4200 return -EINVAL;
4201 }
4202 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4203 assoc_data->ssid_len = ssidie[1];
4204 rcu_read_unlock();
4205
Johannes Berg66e67e42012-01-20 13:55:27 +01004206 mutex_lock(&ifmgd->mtx);
4207
Johannes Berg7b119dc2013-04-10 21:38:36 +02004208 if (ifmgd->associated) {
4209 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4210
4211 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4212 WLAN_REASON_UNSPECIFIED,
4213 false, frame_buf);
4214
4215 __cfg80211_send_deauth(sdata->dev, frame_buf,
4216 sizeof(frame_buf));
4217 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004218
4219 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4220 err = -EBUSY;
4221 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004222 }
4223
Johannes Berg66e67e42012-01-20 13:55:27 +01004224 if (ifmgd->assoc_data) {
4225 err = -EBUSY;
4226 goto err_free;
4227 }
4228
4229 if (ifmgd->auth_data) {
4230 bool match;
4231
4232 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004233 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004234 ieee80211_destroy_auth_data(sdata, match);
4235 }
4236
4237 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02004238
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004239 ifmgd->beacon_crc_valid = false;
4240
Johannes Bergde5036a2012-03-08 15:02:03 +01004241 /*
4242 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4243 * We still associate in non-HT mode (11a/b/g) if any one of these
4244 * ciphers is configured as pairwise.
4245 * We can set this to true for non-11n hardware, that'll be checked
4246 * separately along with the peer capabilities.
4247 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004248 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004249 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4250 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004251 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004252 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004253 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004254 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004255 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004256 }
4257 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004258
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004259 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004260 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004261 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4262 }
Ben Greearef96a8422011-11-18 11:32:00 -08004263
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004264 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4265 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4266
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004267 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4268 sband = local->hw.wiphy->bands[req->bss->channel->band];
4269 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004270 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004271 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004272 if (!bss->wmm_used)
4273 netdev_info(sdata->dev,
4274 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004275 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004276
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004277 /* disable VHT if we don't support it or the AP doesn't use WMM */
4278 if (!sband->vht_cap.vht_supported ||
4279 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4280 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004281 if (!bss->wmm_used)
4282 netdev_info(sdata->dev,
4283 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004284 }
4285
Ben Greearef96a8422011-11-18 11:32:00 -08004286 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4287 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4288 sizeof(ifmgd->ht_capa_mask));
4289
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004290 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4291 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4292 sizeof(ifmgd->vht_capa_mask));
4293
Johannes Berg77fdaa12009-07-07 03:45:17 +02004294 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004295 memcpy(assoc_data->ie, req->ie, req->ie_len);
4296 assoc_data->ie_len = req->ie_len;
4297 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004298
Johannes Berg66e67e42012-01-20 13:55:27 +01004299 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004300
Johannes Bergaf6b6372009-12-23 13:15:35 +01004301 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4302 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004303 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004304 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004305 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004306 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004307 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004308
Johannes Berg66e67e42012-01-20 13:55:27 +01004309 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004310 assoc_data->wmm = bss->wmm_used &&
4311 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004312 assoc_data->supp_rates = bss->supp_rates;
4313 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004314
Johannes Berg9caf0362012-11-29 01:25:20 +01004315 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004316 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4317 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4318 assoc_data->ap_ht_param =
4319 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4320 else
Johannes Berga8243b72012-11-22 14:32:09 +01004321 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004322 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4323 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4324 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4325 sizeof(struct ieee80211_vht_cap));
4326 else
4327 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004328 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004329
Kalle Valoab133152010-01-12 10:42:31 +02004330 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004331 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4332 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004333 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004334 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4335 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004336 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004337 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4338 }
4339
Johannes Berg77fdaa12009-07-07 03:45:17 +02004340 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004341 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004342
4343 if (req->use_mfp) {
4344 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4345 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4346 } else {
4347 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4348 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4349 }
4350
4351 if (req->crypto.control_port)
4352 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4353 else
4354 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4355
Johannes Berga621fa42010-08-27 14:26:54 +03004356 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4357 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4358
Johannes Berg66e67e42012-01-20 13:55:27 +01004359 /* kick off associate process */
4360
4361 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004362 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004363
Johannes Berga1cf7752012-03-08 15:02:07 +01004364 err = ieee80211_prep_connection(sdata, req->bss, true);
4365 if (err)
4366 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004367
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004368 rcu_read_lock();
4369 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004370
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004371 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4372 !beacon_ies) {
4373 /*
4374 * Wait up to one beacon interval ...
4375 * should this be more if we miss one?
4376 */
4377 sdata_info(sdata, "waiting for beacon from %pM\n",
4378 ifmgd->bssid);
4379 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004380 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004381 assoc_data->need_beacon = true;
4382 } else if (beacon_ies) {
4383 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4384 beacon_ies->data,
4385 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004386 u8 dtim_count = 0;
4387
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004388 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4389 const struct ieee80211_tim_ie *tim;
4390 tim = (void *)(tim_ie + 2);
4391 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004392 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004393 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004394 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004395 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004396 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004397
4398 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4399 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4400 sdata->vif.bss_conf.sync_device_ts =
4401 bss->device_ts_beacon;
4402 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4403 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004404 } else {
4405 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004406 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004407 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004408 rcu_read_unlock();
4409
Johannes Berg66e67e42012-01-20 13:55:27 +01004410 run_again(ifmgd, assoc_data->timeout);
4411
Paul Stewartfcff4f12012-02-23 17:59:53 -08004412 if (bss->corrupt_data) {
4413 char *corrupt_type = "data";
4414 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4415 if (bss->corrupt_data &
4416 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4417 corrupt_type = "beacon and probe response";
4418 else
4419 corrupt_type = "beacon";
4420 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4421 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004422 sdata_info(sdata, "associating with AP with corrupt %s\n",
4423 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004424 }
4425
Johannes Berg66e67e42012-01-20 13:55:27 +01004426 err = 0;
4427 goto out;
4428 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004429 memset(ifmgd->bssid, 0, ETH_ALEN);
4430 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004431 ifmgd->assoc_data = NULL;
4432 err_free:
4433 kfree(assoc_data);
4434 out:
4435 mutex_unlock(&ifmgd->mtx);
4436
4437 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004438}
4439
4440int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004441 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004442{
4443 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004444 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004445 bool tx = !req->local_state_change;
Johannes Bergde3d43a2013-05-14 09:39:02 +02004446 bool report_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004447
Johannes Berg77fdaa12009-07-07 03:45:17 +02004448 mutex_lock(&ifmgd->mtx);
4449
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004450 sdata_info(sdata,
4451 "deauthenticating from %pM by local choice (reason=%d)\n",
4452 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004453
Johannes Berg86552012012-10-29 09:46:31 +01004454 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004455 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004456 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4457 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004458 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004459 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004460 ieee80211_destroy_auth_data(sdata, false);
4461 mutex_unlock(&ifmgd->mtx);
4462
Johannes Bergde3d43a2013-05-14 09:39:02 +02004463 report_frame = true;
Johannes Berg86552012012-10-29 09:46:31 +01004464 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004465 }
4466
Johannes Berg86552012012-10-29 09:46:31 +01004467 if (ifmgd->associated &&
4468 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4469 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4470 req->reason_code, tx, frame_buf);
Johannes Bergde3d43a2013-05-14 09:39:02 +02004471 report_frame = true;
Johannes Berg86552012012-10-29 09:46:31 +01004472 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004473 mutex_unlock(&ifmgd->mtx);
4474
Johannes Berg86552012012-10-29 09:46:31 +01004475 out:
Johannes Bergde3d43a2013-05-14 09:39:02 +02004476 if (report_frame)
Johannes Berg86552012012-10-29 09:46:31 +01004477 __cfg80211_send_deauth(sdata->dev, frame_buf,
4478 IEEE80211_DEAUTH_FRAME_LEN);
4479
Johannes Berg77fdaa12009-07-07 03:45:17 +02004480 return 0;
4481}
4482
4483int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004484 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004485{
4486 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004487 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004488 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004489
Johannes Berg77fdaa12009-07-07 03:45:17 +02004490 mutex_lock(&ifmgd->mtx);
4491
Johannes Berg8d8b2612009-07-25 11:58:36 +02004492 /*
4493 * cfg80211 should catch this ... but it's racy since
4494 * we can receive a disassoc frame, process it, hand it
4495 * to cfg80211 while that's in a locked section already
4496 * trying to tell us that the user wants to disconnect.
4497 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004498 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004499 mutex_unlock(&ifmgd->mtx);
4500 return -ENOLINK;
4501 }
4502
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004503 sdata_info(sdata,
4504 "disassociating from %pM by local choice (reason=%d)\n",
4505 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004506
Jouni Malinene69e95d2010-03-29 23:29:31 -07004507 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004508 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4509 req->reason_code, !req->local_state_change,
4510 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004511 mutex_unlock(&ifmgd->mtx);
4512
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004513 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4514 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004515
Johannes Berg77fdaa12009-07-07 03:45:17 +02004516 return 0;
4517}
Jouni Malinen026331c2010-02-15 12:53:10 +02004518
Eliad Pellerafa762f2012-04-23 14:45:15 +03004519void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004520{
4521 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4522
Ben Greear49921852013-02-20 09:41:09 -08004523 /*
4524 * Make sure some work items will not run after this,
4525 * they will not do anything but might not have been
4526 * cancelled when disconnecting.
4527 */
4528 cancel_work_sync(&ifmgd->monitor_work);
4529 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4530 cancel_work_sync(&ifmgd->request_smps_work);
4531 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4532 cancel_work_sync(&ifmgd->chswitch_work);
4533
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004534 mutex_lock(&ifmgd->mtx);
4535 if (ifmgd->assoc_data)
4536 ieee80211_destroy_assoc_data(sdata, false);
4537 if (ifmgd->auth_data)
4538 ieee80211_destroy_auth_data(sdata, false);
4539 del_timer_sync(&ifmgd->timer);
4540 mutex_unlock(&ifmgd->mtx);
4541}
4542
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004543void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4544 enum nl80211_cqm_rssi_threshold_event rssi_event,
4545 gfp_t gfp)
4546{
4547 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4548
Johannes Bergb5878a22010-04-07 16:48:40 +02004549 trace_api_cqm_rssi_notify(sdata, rssi_event);
4550
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004551 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4552}
4553EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);