blob: 141577412d8407fc8b18ad354a34ad8de105f154 [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 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010059#define IEEE80211_BEACON_LOSS_COUNT 7
60
Johannes Bergb291ba12009-07-10 15:29:03 +020061/*
62 * Time the connection can be idle before we probe
63 * it to see if we can still talk to the AP.
64 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030065#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time we wait for a probe response after sending
68 * a probe request because of beacon loss or for
69 * checking the connection still works.
70 */
Ben Greear180205b2011-02-04 15:30:24 -080071static int probe_wait_ms = 500;
72module_param(probe_wait_ms, int, 0644);
73MODULE_PARM_DESC(probe_wait_ms,
74 "Maximum time(ms) to wait for probe response"
75 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jouni Malinen17e4ec12010-03-29 23:28:30 -070077/*
78 * Weight given to the latest Beacon frame when calculating average signal
79 * strength for Beacon frames received in the current BSS. This must be
80 * between 1 and 15.
81 */
82#define IEEE80211_SIGNAL_AVE_WEIGHT 3
83
Jouni Malinen391a2002010-08-27 22:22:00 +030084/*
85 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg5bb644a2009-05-17 11:40:42 +020090#define TMR_RUNNING_TIMER 0
91#define TMR_RUNNING_CHANSW 1
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;
292 goto out;
293 }
294
295 if (!vht_oper || !sband->vht_cap.vht_supported) {
296 ret = IEEE80211_STA_DISABLE_VHT;
297 goto out;
298 }
299
300 vht_chandef.chan = channel;
301 vht_chandef.center_freq1 =
302 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
303 channel->band);
304 vht_chandef.center_freq2 = 0;
305
306 if (vht_oper->center_freq_seg2_idx)
307 vht_chandef.center_freq2 =
308 ieee80211_channel_to_frequency(
309 vht_oper->center_freq_seg2_idx,
310 channel->band);
311
312 switch (vht_oper->chan_width) {
313 case IEEE80211_VHT_CHANWIDTH_USE_HT:
314 vht_chandef.width = chandef->width;
315 break;
316 case IEEE80211_VHT_CHANWIDTH_80MHZ:
317 vht_chandef.width = NL80211_CHAN_WIDTH_80;
318 break;
319 case IEEE80211_VHT_CHANWIDTH_160MHZ:
320 vht_chandef.width = NL80211_CHAN_WIDTH_160;
321 break;
322 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
323 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
324 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;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100607 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000608
609 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
610
611 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
612
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
634 if (!(ap_vht_cap->vht_cap_info &
635 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
636 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
637 IEEE80211_VHT_CAP_RXSTBC_3 |
638 IEEE80211_VHT_CAP_RXSTBC_4);
639
640 for (i = 0; i < 8; i++) {
641 int shift = i * 2;
642 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
643 u16 ap_mcs, our_mcs;
644
645 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
646 mask) >> shift;
647 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
648 mask) >> shift;
649
Johannes Berg24af7172013-03-01 17:33:18 +0100650 if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
651 continue;
652
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100653 switch (ap_mcs) {
654 default:
655 if (our_mcs <= ap_mcs)
656 break;
657 /* fall through */
658 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
659 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
660 vht_cap.vht_mcs.rx_mcs_map |=
661 cpu_to_le16(ap_mcs << shift);
662 }
663 }
664
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000665 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000666 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000667 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
668}
669
Johannes Berg66e67e42012-01-20 13:55:27 +0100670static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
671{
672 struct ieee80211_local *local = sdata->local;
673 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
674 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
675 struct sk_buff *skb;
676 struct ieee80211_mgmt *mgmt;
677 u8 *pos, qos_info;
678 size_t offset = 0, noffset;
679 int i, count, rates_len, supp_rates_len;
680 u16 capab;
681 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200682 struct ieee80211_chanctx_conf *chanctx_conf;
683 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100684 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100685
686 lockdep_assert_held(&ifmgd->mtx);
687
Johannes Berg55de9082012-07-26 17:24:39 +0200688 rcu_read_lock();
689 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
690 if (WARN_ON(!chanctx_conf)) {
691 rcu_read_unlock();
692 return;
693 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100694 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200695 rcu_read_unlock();
696 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100697
698 if (assoc_data->supp_rates_len) {
699 /*
700 * Get all rates supported by the device and the AP as
701 * some APs don't like getting a superset of their rates
702 * in the association request (e.g. D-Link DAP 1353 in
703 * b-only mode)...
704 */
705 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
706 assoc_data->supp_rates_len,
707 sband, &rates);
708 } else {
709 /*
710 * In case AP not provide any supported rates information
711 * before association, we send information element(s) with
712 * all rates that we support.
713 */
714 rates = ~0;
715 rates_len = sband->n_bitrates;
716 }
717
718 skb = alloc_skb(local->hw.extra_tx_headroom +
719 sizeof(*mgmt) + /* bit too much but doesn't matter */
720 2 + assoc_data->ssid_len + /* SSID */
721 4 + rates_len + /* (extended) rates */
722 4 + /* power capability */
723 2 + 2 * sband->n_channels + /* supported channels */
724 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000725 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100726 assoc_data->ie_len + /* extra IEs */
727 9, /* WMM */
728 GFP_KERNEL);
729 if (!skb)
730 return;
731
732 skb_reserve(skb, local->hw.extra_tx_headroom);
733
734 capab = WLAN_CAPABILITY_ESS;
735
736 if (sband->band == IEEE80211_BAND_2GHZ) {
737 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
738 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
739 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
740 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
741 }
742
743 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
744 capab |= WLAN_CAPABILITY_PRIVACY;
745
746 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
747 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
748 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
749
750 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
751 memset(mgmt, 0, 24);
752 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
753 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
754 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
755
756 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
757 skb_put(skb, 10);
758 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
759 IEEE80211_STYPE_REASSOC_REQ);
760 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
761 mgmt->u.reassoc_req.listen_interval =
762 cpu_to_le16(local->hw.conf.listen_interval);
763 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
764 ETH_ALEN);
765 } else {
766 skb_put(skb, 4);
767 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
768 IEEE80211_STYPE_ASSOC_REQ);
769 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
770 mgmt->u.assoc_req.listen_interval =
771 cpu_to_le16(local->hw.conf.listen_interval);
772 }
773
774 /* SSID */
775 pos = skb_put(skb, 2 + assoc_data->ssid_len);
776 *pos++ = WLAN_EID_SSID;
777 *pos++ = assoc_data->ssid_len;
778 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
779
780 /* add all rates which were marked to be used above */
781 supp_rates_len = rates_len;
782 if (supp_rates_len > 8)
783 supp_rates_len = 8;
784
785 pos = skb_put(skb, supp_rates_len + 2);
786 *pos++ = WLAN_EID_SUPP_RATES;
787 *pos++ = supp_rates_len;
788
789 count = 0;
790 for (i = 0; i < sband->n_bitrates; i++) {
791 if (BIT(i) & rates) {
792 int rate = sband->bitrates[i].bitrate;
793 *pos++ = (u8) (rate / 5);
794 if (++count == 8)
795 break;
796 }
797 }
798
799 if (rates_len > count) {
800 pos = skb_put(skb, rates_len - count + 2);
801 *pos++ = WLAN_EID_EXT_SUPP_RATES;
802 *pos++ = rates_len - count;
803
804 for (i++; i < sband->n_bitrates; i++) {
805 if (BIT(i) & rates) {
806 int rate = sband->bitrates[i].bitrate;
807 *pos++ = (u8) (rate / 5);
808 }
809 }
810 }
811
812 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
813 /* 1. power capabilities */
814 pos = skb_put(skb, 4);
815 *pos++ = WLAN_EID_PWR_CAPABILITY;
816 *pos++ = 2;
817 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200818 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100819
820 /* 2. supported channels */
821 /* TODO: get this in reg domain format */
822 pos = skb_put(skb, 2 * sband->n_channels + 2);
823 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
824 *pos++ = 2 * sband->n_channels;
825 for (i = 0; i < sband->n_channels; i++) {
826 *pos++ = ieee80211_frequency_to_channel(
827 sband->channels[i].center_freq);
828 *pos++ = 1; /* one channel in the subband*/
829 }
830 }
831
832 /* if present, add any custom IEs that go before HT */
833 if (assoc_data->ie_len && assoc_data->ie) {
834 static const u8 before_ht[] = {
835 WLAN_EID_SSID,
836 WLAN_EID_SUPP_RATES,
837 WLAN_EID_EXT_SUPP_RATES,
838 WLAN_EID_PWR_CAPABILITY,
839 WLAN_EID_SUPPORTED_CHANNELS,
840 WLAN_EID_RSN,
841 WLAN_EID_QOS_CAPA,
842 WLAN_EID_RRM_ENABLED_CAPABILITIES,
843 WLAN_EID_MOBILITY_DOMAIN,
844 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
845 };
846 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
847 before_ht, ARRAY_SIZE(before_ht),
848 offset);
849 pos = skb_put(skb, noffset - offset);
850 memcpy(pos, assoc_data->ie + offset, noffset - offset);
851 offset = noffset;
852 }
853
Johannes Bergf2d9d272012-11-22 14:11:39 +0100854 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
855 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
856 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
857
Johannes Berga8243b72012-11-22 14:32:09 +0100858 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200859 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200860 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100861
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000862 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100863 ieee80211_add_vht_ie(sdata, skb, sband,
864 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000865
Johannes Berg66e67e42012-01-20 13:55:27 +0100866 /* if present, add any custom non-vendor IEs that go after HT */
867 if (assoc_data->ie_len && assoc_data->ie) {
868 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
869 assoc_data->ie_len,
870 offset);
871 pos = skb_put(skb, noffset - offset);
872 memcpy(pos, assoc_data->ie + offset, noffset - offset);
873 offset = noffset;
874 }
875
Johannes Berg76f03032012-03-08 15:02:05 +0100876 if (assoc_data->wmm) {
877 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200878 qos_info = ifmgd->uapsd_queues;
879 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100880 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
881 } else {
882 qos_info = 0;
883 }
884
885 pos = skb_put(skb, 9);
886 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
887 *pos++ = 7; /* len */
888 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
889 *pos++ = 0x50;
890 *pos++ = 0xf2;
891 *pos++ = 2; /* WME */
892 *pos++ = 0; /* WME info */
893 *pos++ = 1; /* WME ver */
894 *pos++ = qos_info;
895 }
896
897 /* add any remaining custom (i.e. vendor specific here) IEs */
898 if (assoc_data->ie_len && assoc_data->ie) {
899 noffset = assoc_data->ie_len;
900 pos = skb_put(skb, noffset - offset);
901 memcpy(pos, assoc_data->ie + offset, noffset - offset);
902 }
903
Johannes Berga1845fc2012-06-27 13:18:36 +0200904 drv_mgd_prepare_tx(local, sdata);
905
Johannes Berg66e67e42012-01-20 13:55:27 +0100906 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100907 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
908 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
909 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100910 ieee80211_tx_skb(sdata, skb);
911}
912
Kalle Valo572e0012009-02-10 17:09:31 +0200913void ieee80211_send_pspoll(struct ieee80211_local *local,
914 struct ieee80211_sub_if_data *sdata)
915{
Kalle Valo572e0012009-02-10 17:09:31 +0200916 struct ieee80211_pspoll *pspoll;
917 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200918
Kalle Valod8cd1892010-01-05 20:16:26 +0200919 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
920 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200921 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200922
Kalle Valod8cd1892010-01-05 20:16:26 +0200923 pspoll = (struct ieee80211_pspoll *) skb->data;
924 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200925
Johannes Berg62ae67b2009-11-18 18:42:05 +0100926 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
927 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200928}
929
Johannes Berg965beda2009-04-16 13:17:24 +0200930void ieee80211_send_nullfunc(struct ieee80211_local *local,
931 struct ieee80211_sub_if_data *sdata,
932 int powersave)
933{
934 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200935 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530936 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200937
Kalle Valod8cd1892010-01-05 20:16:26 +0200938 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
939 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200940 return;
941
Kalle Valod8cd1892010-01-05 20:16:26 +0200942 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200943 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200944 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200945
Seth Forshee6c17b772013-02-11 11:21:07 -0600946 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
947 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530948 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
949 IEEE80211_STA_CONNECTION_POLL))
950 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
951
Johannes Berg62ae67b2009-11-18 18:42:05 +0100952 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200953}
954
Felix Fietkaud5242152010-01-08 18:06:26 +0100955static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
956 struct ieee80211_sub_if_data *sdata)
957{
958 struct sk_buff *skb;
959 struct ieee80211_hdr *nullfunc;
960 __le16 fc;
961
962 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
963 return;
964
965 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700966 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100967 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700968
Felix Fietkaud5242152010-01-08 18:06:26 +0100969 skb_reserve(skb, local->hw.extra_tx_headroom);
970
971 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
972 memset(nullfunc, 0, 30);
973 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
974 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
975 nullfunc->frame_control = fc;
976 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
977 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
978 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
979 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
980
981 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
982 ieee80211_tx_skb(sdata, skb);
983}
984
Johannes Bergcc32abd2009-05-15 11:52:31 +0200985/* spectrum management related things */
986static void ieee80211_chswitch_work(struct work_struct *work)
987{
988 struct ieee80211_sub_if_data *sdata =
989 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200990 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
991
Johannes Berg9607e6b2009-12-23 13:15:31 +0100992 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200993 return;
994
Johannes Berg77fdaa12009-07-07 03:45:17 +0200995 mutex_lock(&ifmgd->mtx);
996 if (!ifmgd->associated)
997 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200998
Johannes Berg55de9082012-07-26 17:24:39 +0200999 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001000 if (!sdata->local->ops->channel_switch) {
1001 /* call "hw_config" only if doing sw channel switch */
1002 ieee80211_hw_config(sdata->local,
1003 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +03001004 } else {
1005 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +02001006 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001007 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001008
Johannes Berg77fdaa12009-07-07 03:45:17 +02001009 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +02001010 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001011
Johannes Berg57eebdf2012-08-01 15:50:46 +02001012 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001013 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1014 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001015 out:
1016 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1017 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001018}
1019
Johannes Berg5ce6e432010-05-11 16:20:57 +02001020void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1021{
Johannes Berg55de9082012-07-26 17:24:39 +02001022 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1023 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001024
1025 trace_api_chswitch_done(sdata, success);
1026 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001027 sdata_info(sdata,
1028 "driver channel switch failed, disconnecting\n");
1029 ieee80211_queue_work(&sdata->local->hw,
1030 &ifmgd->csa_connection_drop_work);
1031 } else {
1032 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001033 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001034}
1035EXPORT_SYMBOL(ieee80211_chswitch_done);
1036
Johannes Bergcc32abd2009-05-15 11:52:31 +02001037static void ieee80211_chswitch_timer(unsigned long data)
1038{
1039 struct ieee80211_sub_if_data *sdata =
1040 (struct ieee80211_sub_if_data *) data;
1041 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1042
Johannes Berg5bb644a2009-05-17 11:40:42 +02001043 if (sdata->local->quiescing) {
1044 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
1045 return;
1046 }
1047
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001048 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001049}
1050
Johannes Berg4a3cb702013-02-12 16:43:19 +01001051void
1052ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1053 const struct ieee80211_channel_sw_ie *sw_elem,
1054 struct ieee80211_bss *bss, u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001055{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001056 struct cfg80211_bss *cbss =
1057 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001058 struct ieee80211_channel *new_ch;
1059 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001060 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1061 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +02001062 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001063
Johannes Berg77fdaa12009-07-07 03:45:17 +02001064 ASSERT_MGD_MTX(ifmgd);
1065
1066 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001067 return;
1068
Helmut Schaafbe9c422009-07-23 12:14:04 +02001069 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001070 return;
1071
1072 /* Disregard subsequent beacons if we are already running a timer
1073 processing a CSA */
1074
1075 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1076 return;
1077
1078 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001079 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1080 sdata_info(sdata,
1081 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1082 ifmgd->associated->bssid, new_freq);
1083 ieee80211_queue_work(&sdata->local->hw,
1084 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001085 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001086 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001087
Johannes Berg57eebdf2012-08-01 15:50:46 +02001088 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1089
Johannes Berg55de9082012-07-26 17:24:39 +02001090 if (sdata->local->use_chanctx) {
1091 sdata_info(sdata,
1092 "not handling channel switch with channel contexts\n");
1093 ieee80211_queue_work(&sdata->local->hw,
1094 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001095 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001096 }
1097
1098 mutex_lock(&sdata->local->chanctx_mtx);
1099 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1100 mutex_unlock(&sdata->local->chanctx_mtx);
1101 return;
1102 }
1103 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1104 struct ieee80211_chanctx, conf);
1105 if (chanctx->refcount > 1) {
1106 sdata_info(sdata,
1107 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1108 ieee80211_queue_work(&sdata->local->hw,
1109 &ifmgd->csa_connection_drop_work);
1110 mutex_unlock(&sdata->local->chanctx_mtx);
1111 return;
1112 }
1113 mutex_unlock(&sdata->local->chanctx_mtx);
1114
1115 sdata->local->csa_channel = new_ch;
1116
Johannes Berg57eebdf2012-08-01 15:50:46 +02001117 if (sw_elem->mode)
1118 ieee80211_stop_queues_by_reason(&sdata->local->hw,
1119 IEEE80211_QUEUE_STOP_REASON_CSA);
1120
Johannes Berg5ce6e432010-05-11 16:20:57 +02001121 if (sdata->local->ops->channel_switch) {
1122 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001123 struct ieee80211_channel_switch ch_switch = {
1124 .timestamp = timestamp,
1125 .block_tx = sw_elem->mode,
1126 .channel = new_ch,
1127 .count = sw_elem->count,
1128 };
1129
Johannes Berg5ce6e432010-05-11 16:20:57 +02001130 drv_channel_switch(sdata->local, &ch_switch);
1131 return;
1132 }
1133
1134 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001135 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001136 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001137 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001138 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +02001139 TU_TO_EXP_TIME(sw_elem->count *
1140 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001141}
1142
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001143static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1144 struct ieee80211_channel *channel,
1145 const u8 *country_ie, u8 country_ie_len,
1146 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001147{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001148 struct ieee80211_country_ie_triplet *triplet;
1149 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1150 int i, chan_pwr, chan_increment, new_ap_level;
1151 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001152
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001153 /* Invalid IE */
1154 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001155 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001156
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001157 triplet = (void *)(country_ie + 3);
1158 country_ie_len -= 3;
1159
1160 switch (channel->band) {
1161 default:
1162 WARN_ON_ONCE(1);
1163 /* fall through */
1164 case IEEE80211_BAND_2GHZ:
1165 case IEEE80211_BAND_60GHZ:
1166 chan_increment = 1;
1167 break;
1168 case IEEE80211_BAND_5GHZ:
1169 chan_increment = 4;
1170 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001171 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001172
1173 /* find channel */
1174 while (country_ie_len >= 3) {
1175 u8 first_channel = triplet->chans.first_channel;
1176
1177 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1178 goto next;
1179
1180 for (i = 0; i < triplet->chans.num_channels; i++) {
1181 if (first_channel + i * chan_increment == chan) {
1182 have_chan_pwr = true;
1183 chan_pwr = triplet->chans.max_power;
1184 break;
1185 }
1186 }
1187 if (have_chan_pwr)
1188 break;
1189
1190 next:
1191 triplet++;
1192 country_ie_len -= 3;
1193 }
1194
1195 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001196 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001197
1198 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1199
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001200 if (sdata->ap_power_level == new_ap_level)
1201 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001202
1203 sdata_info(sdata,
1204 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1205 new_ap_level, chan_pwr, *pwr_constr_elem,
1206 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001207 sdata->ap_power_level = new_ap_level;
1208 if (__ieee80211_recalc_txpower(sdata))
1209 return BSS_CHANGED_TXPOWER;
1210 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001211}
1212
Johannes Berg965beda2009-04-16 13:17:24 +02001213/* powersave */
1214static void ieee80211_enable_ps(struct ieee80211_local *local,
1215 struct ieee80211_sub_if_data *sdata)
1216{
1217 struct ieee80211_conf *conf = &local->hw.conf;
1218
Johannes Bergd5edaed2009-04-22 23:02:51 +02001219 /*
1220 * If we are scanning right now then the parameters will
1221 * take effect when scan finishes.
1222 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001223 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001224 return;
1225
Johannes Berg965beda2009-04-16 13:17:24 +02001226 if (conf->dynamic_ps_timeout > 0 &&
1227 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1228 mod_timer(&local->dynamic_ps_timer, jiffies +
1229 msecs_to_jiffies(conf->dynamic_ps_timeout));
1230 } else {
1231 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1232 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301233
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001234 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1235 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1236 return;
1237
1238 conf->flags |= IEEE80211_CONF_PS;
1239 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001240 }
1241}
1242
1243static void ieee80211_change_ps(struct ieee80211_local *local)
1244{
1245 struct ieee80211_conf *conf = &local->hw.conf;
1246
1247 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001248 ieee80211_enable_ps(local, local->ps_sdata);
1249 } else if (conf->flags & IEEE80211_CONF_PS) {
1250 conf->flags &= ~IEEE80211_CONF_PS;
1251 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1252 del_timer_sync(&local->dynamic_ps_timer);
1253 cancel_work_sync(&local->dynamic_ps_enable_work);
1254 }
1255}
1256
Jason Young808118c2011-03-10 16:43:19 -08001257static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1258{
1259 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1260 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001261 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001262
1263 if (!mgd->powersave)
1264 return false;
1265
Johannes Berg05cb9102011-10-28 11:59:47 +02001266 if (mgd->broken_ap)
1267 return false;
1268
Jason Young808118c2011-03-10 16:43:19 -08001269 if (!mgd->associated)
1270 return false;
1271
Jason Young808118c2011-03-10 16:43:19 -08001272 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1273 IEEE80211_STA_CONNECTION_POLL))
1274 return false;
1275
1276 rcu_read_lock();
1277 sta = sta_info_get(sdata, mgd->bssid);
1278 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001279 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001280 rcu_read_unlock();
1281
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001282 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001283}
1284
Johannes Berg965beda2009-04-16 13:17:24 +02001285/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001286void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001287{
1288 struct ieee80211_sub_if_data *sdata, *found = NULL;
1289 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001290 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001291
1292 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1293 local->ps_sdata = NULL;
1294 return;
1295 }
1296
1297 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001298 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001299 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301300 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1301 /* If an AP vif is found, then disable PS
1302 * by setting the count to zero thereby setting
1303 * ps_sdata to NULL.
1304 */
1305 count = 0;
1306 break;
1307 }
Johannes Berg965beda2009-04-16 13:17:24 +02001308 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1309 continue;
1310 found = sdata;
1311 count++;
1312 }
1313
Jason Young808118c2011-03-10 16:43:19 -08001314 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001315 s32 beaconint_us;
1316
1317 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001318 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001319
1320 beaconint_us = ieee80211_tu_to_usec(
1321 found->vif.bss_conf.beacon_int);
1322
Juuso Oikarinenff616382010-06-09 09:51:52 +03001323 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001324 if (timeout < 0) {
1325 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001326 * Go to full PSM if the user configures a very low
1327 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001328 * The 2000 second value is there for compatibility
1329 * until the PM_QOS_NETWORK_LATENCY is configured
1330 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001331 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001332 if (latency > (1900 * USEC_PER_MSEC) &&
1333 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001334 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001335 else
1336 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001337 }
Johannes Berg09b85562013-02-06 23:07:41 +01001338 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001339
Johannes Berg04fe2032009-04-22 18:44:37 +02001340 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001341 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001342 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001343 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001344 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001345
1346 /* If the TIM IE is invalid, pretend the value is 1 */
1347 if (!dtimper)
1348 dtimper = 1;
1349 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001350 maxslp = min_t(int, dtimper,
1351 latency / beaconint_us);
1352
Johannes Berg9ccebe62009-04-23 10:32:36 +02001353 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001354 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001355 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001356 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001357 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001358 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001359 }
Johannes Berg965beda2009-04-16 13:17:24 +02001360
1361 ieee80211_change_ps(local);
1362}
1363
Eliad Pellerab095872012-07-27 12:33:22 +03001364void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1365{
1366 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1367
1368 if (sdata->vif.bss_conf.ps != ps_allowed) {
1369 sdata->vif.bss_conf.ps = ps_allowed;
1370 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1371 }
1372}
1373
Johannes Berg965beda2009-04-16 13:17:24 +02001374void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1375{
1376 struct ieee80211_local *local =
1377 container_of(work, struct ieee80211_local,
1378 dynamic_ps_disable_work);
1379
1380 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1381 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1382 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1383 }
1384
1385 ieee80211_wake_queues_by_reason(&local->hw,
1386 IEEE80211_QUEUE_STOP_REASON_PS);
1387}
1388
1389void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1390{
1391 struct ieee80211_local *local =
1392 container_of(work, struct ieee80211_local,
1393 dynamic_ps_enable_work);
1394 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001395 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301396 unsigned long flags;
1397 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001398
1399 /* can only happen when PS was just disabled anyway */
1400 if (!sdata)
1401 return;
1402
Christian Lamparter5e340692011-06-30 21:08:43 +02001403 ifmgd = &sdata->u.mgd;
1404
Johannes Berg965beda2009-04-16 13:17:24 +02001405 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1406 return;
1407
Johannes Berg09b85562013-02-06 23:07:41 +01001408 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001409 /* don't enter PS if TX frames are pending */
1410 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301411 mod_timer(&local->dynamic_ps_timer, jiffies +
1412 msecs_to_jiffies(
1413 local->hw.conf.dynamic_ps_timeout));
1414 return;
1415 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001416
1417 /*
1418 * transmission can be stopped by others which leads to
1419 * dynamic_ps_timer expiry. Postpone the ps timer if it
1420 * is not the actual idle state.
1421 */
1422 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1423 for (q = 0; q < local->hw.queues; q++) {
1424 if (local->queue_stop_reasons[q]) {
1425 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1426 flags);
1427 mod_timer(&local->dynamic_ps_timer, jiffies +
1428 msecs_to_jiffies(
1429 local->hw.conf.dynamic_ps_timeout));
1430 return;
1431 }
1432 }
1433 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301434 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301435
Vivek Natarajan375177b2010-02-09 14:50:28 +05301436 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301437 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301438 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001439
Vivek Natarajane8306f92011-04-06 11:41:10 +05301440 if (drv_tx_frames_pending(local))
1441 mod_timer(&local->dynamic_ps_timer, jiffies +
1442 msecs_to_jiffies(
1443 local->hw.conf.dynamic_ps_timeout));
1444 else {
1445 ieee80211_send_nullfunc(local, sdata, 1);
1446 /* Flush to get the tx status of nullfunc frame */
1447 drv_flush(local, false);
1448 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301449 }
1450
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001451 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1452 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301453 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1454 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1455 local->hw.conf.flags |= IEEE80211_CONF_PS;
1456 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1457 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301458
Arik Nemtsov77b70232011-06-26 12:06:54 +03001459 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1460 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001461}
1462
1463void ieee80211_dynamic_ps_timer(unsigned long data)
1464{
1465 struct ieee80211_local *local = (void *) data;
1466
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001467 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001468 return;
1469
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001470 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001471}
1472
Simon Wunderlich164eb022013-02-08 18:16:20 +01001473void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1474{
1475 struct delayed_work *delayed_work =
1476 container_of(work, struct delayed_work, work);
1477 struct ieee80211_sub_if_data *sdata =
1478 container_of(delayed_work, struct ieee80211_sub_if_data,
1479 dfs_cac_timer_work);
1480
1481 ieee80211_vif_release_channel(sdata);
1482
1483 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1484}
1485
Johannes Berg60f8b392008-09-08 17:44:22 +02001486/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001487static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001488 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001489 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001490{
Jiri Bencf0706e82007-05-05 11:45:53 -07001491 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001492 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001493 size_t left;
1494 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001495 const u8 *pos;
1496 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001497
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001498 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001499 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001500
Johannes Berg32c50572012-03-28 11:04:29 +02001501 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001502 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001503
1504 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001505 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001506
Jiri Bencf0706e82007-05-05 11:45:53 -07001507 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001508 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001509
1510 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001511 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001512
Jiri Bencf0706e82007-05-05 11:45:53 -07001513 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001514 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001515 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001516 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001517
1518 pos = wmm_param + 8;
1519 left = wmm_param_len - 8;
1520
1521 memset(&params, 0, sizeof(params));
1522
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001523 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001524 for (; left >= 4; left -= 4, pos += 4) {
1525 int aci = (pos[0] >> 5) & 0x03;
1526 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001527 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001528 int queue;
1529
1530 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001531 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001532 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001533 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001534 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001535 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1536 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001537 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001538 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001539 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001540 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001541 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001542 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1543 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001544 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001545 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001546 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001547 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001548 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001549 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1550 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001551 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001552 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001553 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001554 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001555 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001556 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001557 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1558 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001559 break;
1560 }
1561
1562 params.aifs = pos[0] & 0x0f;
1563 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1564 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001565 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001566 params.uapsd = uapsd;
1567
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001568 mlme_dbg(sdata,
1569 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1570 queue, aci, acm,
1571 params.aifs, params.cw_min, params.cw_max,
1572 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001573 sdata->tx_conf[queue] = params;
1574 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001575 sdata_err(sdata,
1576 "failed to set TX queue parameters for queue %d\n",
1577 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001578 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001579
1580 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001581 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001582 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001583}
1584
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001585static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1586{
1587 lockdep_assert_held(&sdata->local->mtx);
1588
1589 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1590 IEEE80211_STA_BEACON_POLL);
1591 ieee80211_run_deferred_scan(sdata->local);
1592}
1593
1594static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1595{
1596 mutex_lock(&sdata->local->mtx);
1597 __ieee80211_stop_poll(sdata);
1598 mutex_unlock(&sdata->local->mtx);
1599}
1600
Johannes Berg7a5158e2008-10-08 10:59:33 +02001601static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1602 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001603{
Johannes Bergbda39332008-10-11 01:51:51 +02001604 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001605 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001606 bool use_protection;
1607 bool use_short_preamble;
1608 bool use_short_slot;
1609
1610 if (erp_valid) {
1611 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1612 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1613 } else {
1614 use_protection = false;
1615 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1616 }
1617
1618 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001619 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001620 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001621
Johannes Berg471b3ef2007-12-28 14:32:58 +01001622 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001623 bss_conf->use_cts_prot = use_protection;
1624 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001625 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001626
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001627 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001628 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001629 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001630 }
Daniel Draked9430a32007-07-27 15:43:24 +02001631
Johannes Berg7a5158e2008-10-08 10:59:33 +02001632 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001633 bss_conf->use_short_slot = use_short_slot;
1634 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001635 }
1636
1637 return changed;
1638}
1639
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001640static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001641 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001642 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001643{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001644 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001645 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001646 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001647
Johannes Bergae5eb022008-10-14 16:58:37 +02001648 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001649 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001650 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001651
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001652 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1653 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1654
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001655 sdata->u.mgd.associated = cbss;
1656 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001657
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001658 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1659
Johannes Berg488dd7b2012-10-29 20:08:01 +01001660 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001661 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001662
Johannes Berg9caf0362012-11-29 01:25:20 +01001663 rcu_read_lock();
1664 ies = rcu_dereference(cbss->ies);
1665 if (ies) {
1666 u8 noa[2];
1667 int ret;
1668
1669 ret = cfg80211_get_p2p_attr(
1670 ies->data, ies->len,
1671 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1672 noa, sizeof(noa));
1673 if (ret >= 2) {
1674 bss_conf->p2p_oppps = noa[1] & 0x80;
1675 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1676 bss_info_changed |= BSS_CHANGED_P2P_PS;
1677 sdata->u.mgd.p2p_noa_index = noa[0];
1678 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001679 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001680 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001681 }
1682
Johannes Bergb291ba12009-07-10 15:29:03 +02001683 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001684 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001685
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001686 ieee80211_led_assoc(local, 1);
1687
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001688 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001689 /*
1690 * If the AP is buggy we may get here with no DTIM period
1691 * known, so assume it's 1 which is the only safe assumption
1692 * in that case, although if the TIM IE is broken powersave
1693 * probably just won't work at all.
1694 */
1695 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001696 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001697 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001698 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001699 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001700
Juuso Oikarinen68542962010-06-09 13:43:26 +03001701 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001702
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001703 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001704 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001705 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001706 bss_info_changed |= BSS_CHANGED_CQM;
1707
Juuso Oikarinen68542962010-06-09 13:43:26 +03001708 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001709 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001710 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001711
Johannes Bergae5eb022008-10-14 16:58:37 +02001712 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001713
Johannes Berg056508d2009-07-30 21:43:55 +02001714 mutex_lock(&local->iflist_mtx);
1715 ieee80211_recalc_ps(local, -1);
1716 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001717
Johannes Berg04ecd252012-09-11 14:34:12 +02001718 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001719 ieee80211_recalc_ps_vif(sdata);
1720
John W. Linville8a5b33f2010-01-06 15:39:39 -05001721 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001722 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001723}
1724
Jouni Malinene69e95d2010-03-29 23:29:31 -07001725static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001726 u16 stype, u16 reason, bool tx,
1727 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001728{
Johannes Berg46900292009-02-15 12:44:28 +01001729 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001730 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001731 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001732
Johannes Berg77fdaa12009-07-07 03:45:17 +02001733 ASSERT_MGD_MTX(ifmgd);
1734
Johannes Berg37ad3882012-02-24 13:50:54 +01001735 if (WARN_ON_ONCE(tx && !frame_buf))
1736 return;
1737
Johannes Bergb291ba12009-07-10 15:29:03 +02001738 if (WARN_ON(!ifmgd->associated))
1739 return;
1740
David Spinadel79543d82012-06-12 09:59:45 +03001741 ieee80211_stop_poll(sdata);
1742
Johannes Berg77fdaa12009-07-07 03:45:17 +02001743 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001744
1745 /*
1746 * we need to commit the associated = NULL change because the
1747 * scan code uses that to determine whether this iface should
1748 * go to/wake up from powersave or not -- and could otherwise
1749 * wake the queues erroneously.
1750 */
1751 smp_mb();
1752
1753 /*
1754 * Thus, we can only afterwards stop the queues -- to account
1755 * for the case where another CPU is finishing a scan at this
1756 * time -- we don't want the scan code to enable queues.
1757 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001758
John W. Linville8a5b33f2010-01-06 15:39:39 -05001759 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001760 netif_carrier_off(sdata->dev);
1761
Eliad Peller88bc40e2012-07-12 17:35:33 +03001762 /*
1763 * if we want to get out of ps before disassoc (why?) we have
1764 * to do it before sending disassoc, as otherwise the null-packet
1765 * won't be valid.
1766 */
1767 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1768 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1769 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1770 }
1771 local->ps_sdata = NULL;
1772
Eliad Pellerab095872012-07-27 12:33:22 +03001773 /* disable per-vif ps */
1774 ieee80211_recalc_ps_vif(sdata);
1775
Eliad Pellerf8239812012-06-27 14:18:22 +03001776 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1777 if (tx)
1778 drv_flush(local, false);
1779
Johannes Berg37ad3882012-02-24 13:50:54 +01001780 /* deauthenticate/disassociate now */
1781 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001782 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1783 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001784
1785 /* flush out frame */
1786 if (tx)
1787 drv_flush(local, false);
1788
Eliad Peller88a9e312012-06-01 11:14:03 +03001789 /* clear bssid only after building the needed mgmt frames */
1790 memset(ifmgd->bssid, 0, ETH_ALEN);
1791
Johannes Berg37ad3882012-02-24 13:50:54 +01001792 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001793 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001794
1795 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001796 changed |= ieee80211_reset_erp_info(sdata);
1797
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001798 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001799 changed |= BSS_CHANGED_ASSOC;
1800 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001801
Johannes Berg488dd7b2012-10-29 20:08:01 +01001802 sdata->vif.bss_conf.p2p_ctwindow = 0;
1803 sdata->vif.bss_conf.p2p_oppps = false;
1804
Johannes Berg413ad502009-05-08 21:21:06 +02001805 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001806 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1807 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001808
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001809 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301810
Kalle Valo520eb822008-12-18 23:35:27 +02001811 del_timer_sync(&local->dynamic_ps_timer);
1812 cancel_work_sync(&local->dynamic_ps_enable_work);
1813
Juuso Oikarinen68542962010-06-09 13:43:26 +03001814 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001815 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001816 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001817
Johannes Berg3abead52012-03-02 15:56:59 +01001818 sdata->vif.bss_conf.qos = false;
1819 changed |= BSS_CHANGED_QOS;
1820
Johannes Berg0aaffa92010-05-05 15:28:27 +02001821 /* The BSSID (not really interesting) and HT changed */
1822 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001823 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001824
Johannes Berg3abead52012-03-02 15:56:59 +01001825 /* disassociated - set to defaults now */
1826 ieee80211_set_wmm_default(sdata, false);
1827
Johannes Bergb9dcf712010-08-27 12:35:54 +02001828 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1829 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1830 del_timer_sync(&sdata->u.mgd.timer);
1831 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001832
1833 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001834
Johannes Berg826262c2012-12-10 16:38:14 +02001835 sdata->vif.bss_conf.dtim_period = 0;
1836
Johannes Berg028e8da02012-11-26 11:57:41 +01001837 ifmgd->flags = 0;
1838 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001839}
Jiri Bencf0706e82007-05-05 11:45:53 -07001840
Kalle Valo3cf335d2009-03-22 21:57:06 +02001841void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1842 struct ieee80211_hdr *hdr)
1843{
1844 /*
1845 * We can postpone the mgd.timer whenever receiving unicast frames
1846 * from AP because we know that the connection is working both ways
1847 * at that time. But multicast frames (and hence also beacons) must
1848 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001849 * data idle periods for sending the periodic probe request to the
1850 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001851 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001852 if (is_multicast_ether_addr(hdr->addr1))
1853 return;
1854
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001855 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001856}
Jiri Bencf0706e82007-05-05 11:45:53 -07001857
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001858static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1859{
1860 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001861 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001862
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001863 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001864 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001865 IEEE80211_STA_CONNECTION_POLL))) {
1866 mutex_unlock(&local->mtx);
1867 return;
1868 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001869
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001870 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001871
1872 mutex_lock(&local->iflist_mtx);
1873 ieee80211_recalc_ps(local, -1);
1874 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001875
1876 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001877 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001878
1879 /*
1880 * We've received a probe response, but are not sure whether
1881 * we have or will be receiving any beacons or data, so let's
1882 * schedule the timers again, just in case.
1883 */
1884 ieee80211_sta_reset_beacon_monitor(sdata);
1885
1886 mod_timer(&ifmgd->conn_mon_timer,
1887 round_jiffies_up(jiffies +
1888 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001889out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001890 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001891}
1892
1893void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001894 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001895{
Felix Fietkau75706d02010-12-02 21:01:07 +01001896 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001897 return;
1898
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001899 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1900 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001901 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001902 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001903 else
1904 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001905 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001906 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001907 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001908
1909 if (ack)
1910 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001911}
1912
Maxim Levitskya43abf22009-07-31 18:54:12 +03001913static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1914{
1915 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1916 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001917 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001918 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001919
1920 /*
1921 * Try sending broadcast probe requests for the last three
1922 * probe requests after the first ones failed since some
1923 * buggy APs only support broadcast probe requests.
1924 */
1925 if (ifmgd->probe_send_count >= unicast_limit)
1926 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001927
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001928 /*
1929 * When the hardware reports an accurate Tx ACK status, it's
1930 * better to send a nullfunc frame instead of a probe request,
1931 * as it will kick us off the AP quickly if we aren't associated
1932 * anymore. The timeout will be reset if the frame is ACKed by
1933 * the AP.
1934 */
Soumik Das992e68b2012-05-20 15:31:13 +05301935 ifmgd->probe_send_count++;
1936
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001937 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1938 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001939 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001940 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001941 int ssid_len;
1942
Johannes Berg9caf0362012-11-29 01:25:20 +01001943 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001944 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001945 if (WARN_ON_ONCE(ssid == NULL))
1946 ssid_len = 0;
1947 else
1948 ssid_len = ssid[1];
1949
1950 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001951 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001952 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001953 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001954 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001955
Ben Greear180205b2011-02-04 15:30:24 -08001956 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001957 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301958 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1959 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001960}
1961
Johannes Bergb291ba12009-07-10 15:29:03 +02001962static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1963 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001964{
Kalle Valo04de8382009-03-22 21:57:35 +02001965 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001966 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001967
Johannes Berg9607e6b2009-12-23 13:15:31 +01001968 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001969 return;
1970
Johannes Berg77fdaa12009-07-07 03:45:17 +02001971 mutex_lock(&ifmgd->mtx);
1972
1973 if (!ifmgd->associated)
1974 goto out;
1975
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001976 mutex_lock(&sdata->local->mtx);
1977
1978 if (sdata->local->tmp_channel || sdata->local->scanning) {
1979 mutex_unlock(&sdata->local->mtx);
1980 goto out;
1981 }
1982
Joe Perchese87cc472012-05-13 21:56:26 +00001983 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001984 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001985 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001986
Holger Schurig6efb71b2012-05-15 15:38:59 +02001987 ieee80211_cqm_rssi_notify(&sdata->vif,
1988 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001989
Johannes Bergb291ba12009-07-10 15:29:03 +02001990 /*
1991 * The driver/our work has already reported this event or the
1992 * connection monitoring has kicked in and we have already sent
1993 * a probe request. Or maybe the AP died and the driver keeps
1994 * reporting until we disassociate...
1995 *
1996 * In either case we have to ignore the current call to this
1997 * function (except for setting the correct probe reason bit)
1998 * because otherwise we would reset the timer every time and
1999 * never check whether we received a probe response!
2000 */
2001 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2002 IEEE80211_STA_CONNECTION_POLL))
2003 already = true;
2004
2005 if (beacon)
2006 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2007 else
2008 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2009
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002010 mutex_unlock(&sdata->local->mtx);
2011
Johannes Bergb291ba12009-07-10 15:29:03 +02002012 if (already)
2013 goto out;
2014
Johannes Berg4e751842009-06-10 15:16:52 +02002015 mutex_lock(&sdata->local->iflist_mtx);
2016 ieee80211_recalc_ps(sdata->local, -1);
2017 mutex_unlock(&sdata->local->iflist_mtx);
2018
Maxim Levitskya43abf22009-07-31 18:54:12 +03002019 ifmgd->probe_send_count = 0;
2020 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002021 out:
2022 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002023}
2024
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002025struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2026 struct ieee80211_vif *vif)
2027{
2028 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2029 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002030 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002031 struct sk_buff *skb;
2032 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002033 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002034
2035 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2036 return NULL;
2037
2038 ASSERT_MGD_MTX(ifmgd);
2039
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002040 if (ifmgd->associated)
2041 cbss = ifmgd->associated;
2042 else if (ifmgd->auth_data)
2043 cbss = ifmgd->auth_data->bss;
2044 else if (ifmgd->assoc_data)
2045 cbss = ifmgd->assoc_data->bss;
2046 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002047 return NULL;
2048
Johannes Berg9caf0362012-11-29 01:25:20 +01002049 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002050 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002051 if (WARN_ON_ONCE(ssid == NULL))
2052 ssid_len = 0;
2053 else
2054 ssid_len = ssid[1];
2055
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002056 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002057 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002058 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002059 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002060 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002061
2062 return skb;
2063}
2064EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2065
Johannes Bergeef9e542013-01-29 13:09:34 +01002066static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002067{
2068 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002069 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002070
2071 mutex_lock(&ifmgd->mtx);
2072 if (!ifmgd->associated) {
2073 mutex_unlock(&ifmgd->mtx);
2074 return;
2075 }
2076
Johannes Berg37ad3882012-02-24 13:50:54 +01002077 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2078 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002079 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002080 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002081 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2082 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002083 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002084
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002085 /*
2086 * must be outside lock due to cfg80211,
2087 * but that's not a problem.
2088 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002089 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002090}
2091
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002092static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002093{
2094 struct ieee80211_sub_if_data *sdata =
2095 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002096 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002097 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2098 struct sta_info *sta;
2099
2100 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302101 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002102 sta = sta_info_get(sdata, ifmgd->bssid);
2103 if (sta)
2104 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302105 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002106 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002107
Johannes Berg682bd382013-01-29 13:13:50 +01002108 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002109 sdata_info(sdata, "Connection to AP %pM lost\n",
2110 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002111 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002112 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002113 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002114 }
2115}
2116
2117static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2118{
2119 struct ieee80211_sub_if_data *sdata =
2120 container_of(work, struct ieee80211_sub_if_data,
2121 u.mgd.csa_connection_drop_work);
2122
Johannes Bergeef9e542013-01-29 13:09:34 +01002123 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002124}
2125
Kalle Valo04de8382009-03-22 21:57:35 +02002126void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2127{
2128 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002129 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002130
Johannes Bergb5878a22010-04-07 16:48:40 +02002131 trace_api_beacon_loss(sdata);
2132
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002133 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002134 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002135 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002136}
2137EXPORT_SYMBOL(ieee80211_beacon_loss);
2138
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002139void ieee80211_connection_loss(struct ieee80211_vif *vif)
2140{
2141 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2142 struct ieee80211_hw *hw = &sdata->local->hw;
2143
Johannes Bergb5878a22010-04-07 16:48:40 +02002144 trace_api_connection_loss(sdata);
2145
Johannes Berg682bd382013-01-29 13:13:50 +01002146 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002147 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2148}
2149EXPORT_SYMBOL(ieee80211_connection_loss);
2150
2151
Johannes Berg66e67e42012-01-20 13:55:27 +01002152static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2153 bool assoc)
2154{
2155 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2156
2157 lockdep_assert_held(&sdata->u.mgd.mtx);
2158
Johannes Berg66e67e42012-01-20 13:55:27 +01002159 if (!assoc) {
2160 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2161
2162 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2163 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002164 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002165 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002166 }
2167
Johannes Berg5b112d32013-02-01 01:49:58 +01002168 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002169 kfree(auth_data);
2170 sdata->u.mgd.auth_data = NULL;
2171}
2172
2173static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2174 struct ieee80211_mgmt *mgmt, size_t len)
2175{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002176 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002177 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2178 u8 *pos;
2179 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002180 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002181
2182 pos = mgmt->u.auth.variable;
2183 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2184 if (!elems.challenge)
2185 return;
2186 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002187 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002188 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2189 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2190 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002191 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002192 elems.challenge - 2, elems.challenge_len + 2,
2193 auth_data->bss->bssid, auth_data->bss->bssid,
2194 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002195 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002196}
2197
2198static enum rx_mgmt_action __must_check
2199ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2200 struct ieee80211_mgmt *mgmt, size_t len)
2201{
2202 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2203 u8 bssid[ETH_ALEN];
2204 u16 auth_alg, auth_transaction, status_code;
2205 struct sta_info *sta;
2206
2207 lockdep_assert_held(&ifmgd->mtx);
2208
2209 if (len < 24 + 6)
2210 return RX_MGMT_NONE;
2211
2212 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2213 return RX_MGMT_NONE;
2214
2215 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2216
Joe Perchesb203ca32012-05-08 18:56:52 +00002217 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002218 return RX_MGMT_NONE;
2219
2220 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2221 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2222 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2223
2224 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002225 auth_transaction != ifmgd->auth_data->expected_transaction) {
2226 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2227 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2228 auth_transaction,
2229 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002230 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002231 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002232
2233 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002234 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2235 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002236 ieee80211_destroy_auth_data(sdata, false);
2237 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002238 }
2239
2240 switch (ifmgd->auth_data->algorithm) {
2241 case WLAN_AUTH_OPEN:
2242 case WLAN_AUTH_LEAP:
2243 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002244 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002245 break;
2246 case WLAN_AUTH_SHARED_KEY:
2247 if (ifmgd->auth_data->expected_transaction != 4) {
2248 ieee80211_auth_challenge(sdata, mgmt, len);
2249 /* need another frame */
2250 return RX_MGMT_NONE;
2251 }
2252 break;
2253 default:
2254 WARN_ONCE(1, "invalid auth alg %d",
2255 ifmgd->auth_data->algorithm);
2256 return RX_MGMT_NONE;
2257 }
2258
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002259 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002260 ifmgd->auth_data->done = true;
2261 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002262 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002263 run_again(ifmgd, ifmgd->auth_data->timeout);
2264
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002265 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2266 ifmgd->auth_data->expected_transaction != 2) {
2267 /*
2268 * Report auth frame to user space for processing since another
2269 * round of Authentication frames is still needed.
2270 */
2271 return RX_MGMT_CFG80211_RX_AUTH;
2272 }
2273
Johannes Berg66e67e42012-01-20 13:55:27 +01002274 /* move station state to auth */
2275 mutex_lock(&sdata->local->sta_mtx);
2276 sta = sta_info_get(sdata, bssid);
2277 if (!sta) {
2278 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2279 goto out_err;
2280 }
2281 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002282 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002283 goto out_err;
2284 }
2285 mutex_unlock(&sdata->local->sta_mtx);
2286
2287 return RX_MGMT_CFG80211_RX_AUTH;
2288 out_err:
2289 mutex_unlock(&sdata->local->sta_mtx);
2290 /* ignore frame -- wait for timeout */
2291 return RX_MGMT_NONE;
2292}
2293
2294
Johannes Berg77fdaa12009-07-07 03:45:17 +02002295static enum rx_mgmt_action __must_check
2296ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002297 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002298{
Johannes Berg46900292009-02-15 12:44:28 +01002299 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002300 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002301 u16 reason_code;
2302
Johannes Berg66e67e42012-01-20 13:55:27 +01002303 lockdep_assert_held(&ifmgd->mtx);
2304
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002305 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002306 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002307
Johannes Berg66e67e42012-01-20 13:55:27 +01002308 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002309 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002310 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002311
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002312 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002313
2314 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2315
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002316 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2317 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002318
Johannes Berg37ad3882012-02-24 13:50:54 +01002319 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2320
Johannes Berg77fdaa12009-07-07 03:45:17 +02002321 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002322}
2323
2324
Johannes Berg77fdaa12009-07-07 03:45:17 +02002325static enum rx_mgmt_action __must_check
2326ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2327 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002328{
Johannes Berg46900292009-02-15 12:44:28 +01002329 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002330 u16 reason_code;
2331
Johannes Berg66e67e42012-01-20 13:55:27 +01002332 lockdep_assert_held(&ifmgd->mtx);
2333
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002334 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002335 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002336
Johannes Berg66e67e42012-01-20 13:55:27 +01002337 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002338 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002339 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002340
2341 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2342
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002343 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2344 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002345
Johannes Berg37ad3882012-02-24 13:50:54 +01002346 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2347
Johannes Berg77fdaa12009-07-07 03:45:17 +02002348 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002349}
2350
Christian Lamparterc74d0842011-10-15 00:14:49 +02002351static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2352 u8 *supp_rates, unsigned int supp_rates_len,
2353 u32 *rates, u32 *basic_rates,
2354 bool *have_higher_than_11mbit,
2355 int *min_rate, int *min_rate_index)
2356{
2357 int i, j;
2358
2359 for (i = 0; i < supp_rates_len; i++) {
2360 int rate = (supp_rates[i] & 0x7f) * 5;
2361 bool is_basic = !!(supp_rates[i] & 0x80);
2362
2363 if (rate > 110)
2364 *have_higher_than_11mbit = true;
2365
2366 /*
2367 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2368 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2369 *
2370 * Note: Even through the membership selector and the basic
2371 * rate flag share the same bit, they are not exactly
2372 * the same.
2373 */
2374 if (!!(supp_rates[i] & 0x80) &&
2375 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2376 continue;
2377
2378 for (j = 0; j < sband->n_bitrates; j++) {
2379 if (sband->bitrates[j].bitrate == rate) {
2380 *rates |= BIT(j);
2381 if (is_basic)
2382 *basic_rates |= BIT(j);
2383 if (rate < *min_rate) {
2384 *min_rate = rate;
2385 *min_rate_index = j;
2386 }
2387 break;
2388 }
2389 }
2390 }
2391}
Jiri Bencf0706e82007-05-05 11:45:53 -07002392
Johannes Berg66e67e42012-01-20 13:55:27 +01002393static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2394 bool assoc)
2395{
2396 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2397
2398 lockdep_assert_held(&sdata->u.mgd.mtx);
2399
Johannes Berg66e67e42012-01-20 13:55:27 +01002400 if (!assoc) {
2401 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2402
2403 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2404 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002405 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002406 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002407 }
2408
2409 kfree(assoc_data);
2410 sdata->u.mgd.assoc_data = NULL;
2411}
2412
2413static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2414 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002415 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002416{
Johannes Berg46900292009-02-15 12:44:28 +01002417 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002418 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002419 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002420 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002421 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002422 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002423 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002424 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002425 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002426 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002427
Johannes Bergaf6b6372009-12-23 13:15:35 +01002428 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002429
Jiri Bencf0706e82007-05-05 11:45:53 -07002430 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002431 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002432
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002433 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002434 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2435 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002436 aid &= ~(BIT(15) | BIT(14));
2437
Johannes Berg05cb9102011-10-28 11:59:47 +02002438 ifmgd->broken_ap = false;
2439
2440 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002441 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2442 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002443 aid = 0;
2444 ifmgd->broken_ap = true;
2445 }
2446
Johannes Bergaf6b6372009-12-23 13:15:35 +01002447 pos = mgmt->u.assoc_resp.variable;
2448 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2449
Jiri Bencf0706e82007-05-05 11:45:53 -07002450 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002451 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002452 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002453 }
2454
Johannes Berg46900292009-02-15 12:44:28 +01002455 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002456
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002457 /*
2458 * We previously checked these in the beacon/probe response, so
2459 * they should be present here. This is just a safety net.
2460 */
2461 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2462 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2463 sdata_info(sdata,
2464 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2465 return false;
2466 }
2467
2468 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2469 (!elems.vht_cap_elem || !elems.vht_operation)) {
2470 sdata_info(sdata,
2471 "VHT AP is missing VHT capability/operation in AssocResp\n");
2472 return false;
2473 }
2474
Guy Eilam2a33bee2011-08-17 15:18:15 +03002475 mutex_lock(&sdata->local->sta_mtx);
2476 /*
2477 * station info was already allocated and inserted before
2478 * the association and should be available to us
2479 */
Johannes Berg7852e362012-01-20 13:55:24 +01002480 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002481 if (WARN_ON(!sta)) {
2482 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002483 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002484 }
2485
Johannes Berg55de9082012-07-26 17:24:39 +02002486 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002487
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002488 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002489 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002490 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002491 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002492
Mahesh Palivela818255e2012-10-10 11:33:04 +00002493 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2494 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002495 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002496
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002497 /*
2498 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2499 * in their association response, so ignore that data for our own
2500 * configuration. If it changed since the last beacon, we'll get the
2501 * next beacon and update then.
2502 */
Johannes Berg11289582013-02-07 17:36:12 +01002503
Johannes Bergbee7f582013-02-07 22:24:55 +01002504 /*
2505 * If an operating mode notification IE is present, override the
2506 * NSS calculation (that would be done in rate_control_rate_init())
2507 * and use the # of streams from that element.
2508 */
2509 if (elems.opmode_notif &&
2510 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2511 u8 nss;
2512
2513 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2514 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2515 nss += 1;
2516 sta->sta.rx_nss = nss;
2517 }
2518
Johannes Berg4b7679a2008-09-18 18:14:18 +02002519 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002520
Johannes Berg46900292009-02-15 12:44:28 +01002521 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002522 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002523
Johannes Bergddf4ac52008-10-22 11:41:38 +02002524 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002525 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002526
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002527 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002528 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2529 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2530 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002531 sdata_info(sdata,
2532 "failed to move station %pM to desired state\n",
2533 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002534 WARN_ON(__sta_info_destroy(sta));
2535 mutex_unlock(&sdata->local->sta_mtx);
2536 return false;
2537 }
2538
Johannes Berg7852e362012-01-20 13:55:24 +01002539 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002540
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002541 /*
2542 * Always handle WMM once after association regardless
2543 * of the first value the AP uses. Setting -1 here has
2544 * that effect because the AP values is an unsigned
2545 * 4-bit value.
2546 */
2547 ifmgd->wmm_last_param_set = -1;
2548
Johannes Bergddf4ac52008-10-22 11:41:38 +02002549 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002550 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002551 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002552 else
Johannes Berg3abead52012-03-02 15:56:59 +01002553 ieee80211_set_wmm_default(sdata, false);
2554 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002555
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002556 /* set AID and assoc capability,
2557 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002558 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002559 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002560 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002561
Kalle Valo15b7b062009-03-22 21:57:14 +02002562 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002563 * If we're using 4-addr mode, let the AP know that we're
2564 * doing so, so that it can create the STA VLAN on its side
2565 */
2566 if (ifmgd->use_4addr)
2567 ieee80211_send_4addr_nullfunc(local, sdata);
2568
2569 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002570 * Start timer to probe the connection to the AP now.
2571 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002572 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002573 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002574 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002575
Johannes Bergaf6b6372009-12-23 13:15:35 +01002576 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002577}
2578
Johannes Berg66e67e42012-01-20 13:55:27 +01002579static enum rx_mgmt_action __must_check
2580ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2581 struct ieee80211_mgmt *mgmt, size_t len,
2582 struct cfg80211_bss **bss)
2583{
2584 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2585 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2586 u16 capab_info, status_code, aid;
2587 struct ieee802_11_elems elems;
2588 u8 *pos;
2589 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002590
Johannes Berg66e67e42012-01-20 13:55:27 +01002591 lockdep_assert_held(&ifmgd->mtx);
2592
2593 if (!assoc_data)
2594 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002595 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002596 return RX_MGMT_NONE;
2597
2598 /*
2599 * AssocResp and ReassocResp have identical structure, so process both
2600 * of them in this function.
2601 */
2602
2603 if (len < 24 + 6)
2604 return RX_MGMT_NONE;
2605
2606 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2607 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2608 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2609 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2610
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002611 sdata_info(sdata,
2612 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2613 reassoc ? "Rea" : "A", mgmt->sa,
2614 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002615
2616 pos = mgmt->u.assoc_resp.variable;
2617 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2618
2619 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2620 elems.timeout_int && elems.timeout_int_len == 5 &&
2621 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2622 u32 tu, ms;
2623 tu = get_unaligned_le32(elems.timeout_int + 1);
2624 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002625 sdata_info(sdata,
2626 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2627 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002628 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002629 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002630 if (ms > IEEE80211_ASSOC_TIMEOUT)
2631 run_again(ifmgd, assoc_data->timeout);
2632 return RX_MGMT_NONE;
2633 }
2634
2635 *bss = assoc_data->bss;
2636
2637 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002638 sdata_info(sdata, "%pM denied association (code=%d)\n",
2639 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002640 ieee80211_destroy_assoc_data(sdata, false);
2641 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002642 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2643 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002644 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002645 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002646 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2647 }
John W. Linville635d9992012-07-09 16:34:34 -04002648 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002649
2650 /*
2651 * destroy assoc_data afterwards, as otherwise an idle
2652 * recalc after assoc_data is NULL but before associated
2653 * is set can cause the interface to go idle
2654 */
2655 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002656 }
2657
2658 return RX_MGMT_CFG80211_RX_ASSOC;
2659}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002660
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002661static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002662 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002663 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002664 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002665{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002666 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002667 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002668 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002669 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002670 bool need_ps = false;
2671
Johannes Berg826262c2012-12-10 16:38:14 +02002672 if ((sdata->u.mgd.associated &&
2673 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2674 (sdata->u.mgd.assoc_data &&
2675 ether_addr_equal(mgmt->bssid,
2676 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002677 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002678 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2679
2680 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002681 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002682 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2683 }
Johannes Berg56007a02010-01-26 14:19:52 +01002684 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002685
Johannes Berg5bda6172008-09-08 11:05:10 +02002686 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002687 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2688 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002689 else
2690 freq = rx_status->freq;
2691
2692 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2693
2694 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2695 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002696
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002697 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002698 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002699 if (bss)
2700 ieee80211_rx_bss_put(local, bss);
2701
2702 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002703 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002704
Johannes Berg56007a02010-01-26 14:19:52 +01002705 if (need_ps) {
2706 mutex_lock(&local->iflist_mtx);
2707 ieee80211_recalc_ps(local, -1);
2708 mutex_unlock(&local->iflist_mtx);
2709 }
2710
Johannes Berg5bc14202012-08-01 16:13:02 +02002711 if (elems->ch_switch_ie &&
2712 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2713 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002714 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002715}
2716
2717
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002718static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002719 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002720{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002721 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002722 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002723 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2724 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002725 struct ieee802_11_elems elems;
2726
Kalle Valo15b7b062009-03-22 21:57:14 +02002727 ifmgd = &sdata->u.mgd;
2728
Johannes Berg77fdaa12009-07-07 03:45:17 +02002729 ASSERT_MGD_MTX(ifmgd);
2730
Joe Perchesb203ca32012-05-08 18:56:52 +00002731 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002732 return; /* ignore ProbeResp to foreign address */
2733
Ester Kummerae6a44e2008-06-27 18:54:48 +03002734 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2735 if (baselen > len)
2736 return;
2737
2738 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2739 &elems);
2740
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002741 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002742
Johannes Berg77fdaa12009-07-07 03:45:17 +02002743 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002744 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002745 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002746
2747 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002748 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002749 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002750 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002751 ifmgd->auth_data->tries = 0;
2752 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002753 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002754 run_again(ifmgd, ifmgd->auth_data->timeout);
2755 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002756}
2757
Johannes Bergd91f36d2009-04-16 13:17:26 +02002758/*
2759 * This is the canonical list of information elements we care about,
2760 * the filter code also gives us all changes to the Microsoft OUI
2761 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2762 *
2763 * We implement beacon filtering in software since that means we can
2764 * avoid processing the frame here and in cfg80211, and userspace
2765 * will not be able to tell whether the hardware supports it or not.
2766 *
2767 * XXX: This list needs to be dynamic -- userspace needs to be able to
2768 * add items it requires. It also needs to be able to tell us to
2769 * look out for other vendor IEs.
2770 */
2771static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002772 (1ULL << WLAN_EID_COUNTRY) |
2773 (1ULL << WLAN_EID_ERP_INFO) |
2774 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2775 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2776 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002777 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002778
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002779static enum rx_mgmt_action
2780ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2781 struct ieee80211_mgmt *mgmt, size_t len,
2782 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002783{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002784 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002785 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002786 size_t baselen;
2787 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002788 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002789 struct ieee80211_chanctx_conf *chanctx_conf;
2790 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002791 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002792 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002793 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002794 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002795 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002796 u8 *bssid;
2797
Johannes Berg66e67e42012-01-20 13:55:27 +01002798 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002799
Ester Kummerae6a44e2008-06-27 18:54:48 +03002800 /* Process beacon from the current BSS */
2801 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2802 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002803 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002804
Johannes Berg55de9082012-07-26 17:24:39 +02002805 rcu_read_lock();
2806 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2807 if (!chanctx_conf) {
2808 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002809 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002810 }
2811
Johannes Berg4bf88532012-11-09 11:39:59 +01002812 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002813 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002814 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002815 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002816 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002817 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002818
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002819 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002820 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002821 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2822 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002823
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002824 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002825 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002826 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002827 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2828 sdata->vif.bss_conf.sync_tsf =
2829 le64_to_cpu(mgmt->u.beacon.timestamp);
2830 sdata->vif.bss_conf.sync_device_ts =
2831 rx_status->device_timestamp;
2832 if (elems.tim)
2833 sdata->vif.bss_conf.sync_dtim_count =
2834 elems.tim->dtim_count;
2835 else
2836 sdata->vif.bss_conf.sync_dtim_count = 0;
2837 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002838 /* continue assoc process */
2839 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002840 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002841 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002842 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002843 }
2844
2845 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002846 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002847 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002848 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002849
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002850 /* Track average RSSI from the Beacon frames of the current AP */
2851 ifmgd->last_beacon_signal = rx_status->signal;
2852 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2853 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002854 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002855 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002856 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002857 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002858 } else {
2859 ifmgd->ave_beacon_signal =
2860 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2861 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2862 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002863 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002864 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002865
2866 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2867 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2868 int sig = ifmgd->ave_beacon_signal;
2869 int last_sig = ifmgd->last_ave_beacon_signal;
2870
2871 /*
2872 * if signal crosses either of the boundaries, invoke callback
2873 * with appropriate parameters
2874 */
2875 if (sig > ifmgd->rssi_max_thold &&
2876 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2877 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002878 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002879 } else if (sig < ifmgd->rssi_min_thold &&
2880 (last_sig >= ifmgd->rssi_max_thold ||
2881 last_sig == 0)) {
2882 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002883 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002884 }
2885 }
2886
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002887 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002888 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002889 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002890 int sig = ifmgd->ave_beacon_signal / 16;
2891 int last_event = ifmgd->last_cqm_event_signal;
2892 int thold = bss_conf->cqm_rssi_thold;
2893 int hyst = bss_conf->cqm_rssi_hyst;
2894 if (sig < thold &&
2895 (last_event == 0 || sig < last_event - hyst)) {
2896 ifmgd->last_cqm_event_signal = sig;
2897 ieee80211_cqm_rssi_notify(
2898 &sdata->vif,
2899 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2900 GFP_KERNEL);
2901 } else if (sig > thold &&
2902 (last_event == 0 || sig > last_event + hyst)) {
2903 ifmgd->last_cqm_event_signal = sig;
2904 ieee80211_cqm_rssi_notify(
2905 &sdata->vif,
2906 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2907 GFP_KERNEL);
2908 }
2909 }
2910
Johannes Bergb291ba12009-07-10 15:29:03 +02002911 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002912 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002913 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002914 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002915 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002916 ieee80211_run_deferred_scan(local);
2917 mutex_unlock(&local->mtx);
2918
Johannes Berg4e751842009-06-10 15:16:52 +02002919 mutex_lock(&local->iflist_mtx);
2920 ieee80211_recalc_ps(local, -1);
2921 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002922 }
2923
Johannes Bergb291ba12009-07-10 15:29:03 +02002924 /*
2925 * Push the beacon loss detection into the future since
2926 * we are processing a beacon from the AP just now.
2927 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002928 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002929
Johannes Bergd91f36d2009-04-16 13:17:26 +02002930 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2931 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2932 len - baselen, &elems,
2933 care_about_ies, ncrc);
2934
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302935 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002936 bool directed_tim = ieee80211_check_tim(elems.tim,
2937 elems.tim_len,
2938 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002939 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002940 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002941 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2942 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2943 ieee80211_hw_config(local,
2944 IEEE80211_CONF_CHANGE_PS);
2945 }
Kalle Valo572e0012009-02-10 17:09:31 +02002946 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302947 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002948 local->pspolling = true;
2949
2950 /*
2951 * Here is assumed that the driver will be
2952 * able to send ps-poll frame and receive a
2953 * response even though power save mode is
2954 * enabled, but some drivers might require
2955 * to disable power save here. This needs
2956 * to be investigated.
2957 */
2958 ieee80211_send_pspoll(local, sdata);
2959 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002960 }
2961 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002962
Johannes Berg488dd7b2012-10-29 20:08:01 +01002963 if (sdata->vif.p2p) {
2964 u8 noa[2];
2965 int ret;
2966
2967 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2968 len - baselen,
2969 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2970 noa, sizeof(noa));
2971 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2972 bss_conf->p2p_oppps = noa[1] & 0x80;
2973 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2974 changed |= BSS_CHANGED_P2P_PS;
2975 sdata->u.mgd.p2p_noa_index = noa[0];
2976 /*
2977 * make sure we update all information, the CRC
2978 * mechanism doesn't look at P2P attributes.
2979 */
2980 ifmgd->beacon_crc_valid = false;
2981 }
2982 }
2983
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002984 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002985 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03002986 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002987 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002988
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002989 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002990
2991 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2992 elems.wmm_param_len))
2993 changed |= BSS_CHANGED_QOS;
2994
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002995 /*
2996 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002997 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002998 */
2999 if (!bss_conf->dtim_period) {
3000 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3001 if (elems.tim)
3002 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3003 else
3004 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003005
3006 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3007 sdata->vif.bss_conf.sync_tsf =
3008 le64_to_cpu(mgmt->u.beacon.timestamp);
3009 sdata->vif.bss_conf.sync_device_ts =
3010 rx_status->device_timestamp;
3011 if (elems.tim)
3012 sdata->vif.bss_conf.sync_dtim_count =
3013 elems.tim->dtim_count;
3014 else
3015 sdata->vif.bss_conf.sync_dtim_count = 0;
3016 }
3017
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003018 changed |= BSS_CHANGED_DTIM_PERIOD;
3019 }
3020
Johannes Berg7a5158e2008-10-08 10:59:33 +02003021 if (elems.erp_info && elems.erp_info_len >= 1) {
3022 erp_valid = true;
3023 erp_value = elems.erp_info[0];
3024 } else {
3025 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003026 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003027 changed |= ieee80211_handle_bss_capability(sdata,
3028 le16_to_cpu(mgmt->u.beacon.capab_info),
3029 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003030
Johannes Berg11289582013-02-07 17:36:12 +01003031 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003032 sta = sta_info_get(sdata, bssid);
3033
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003034 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3035 elems.vht_operation, bssid, &changed)) {
3036 mutex_unlock(&local->sta_mtx);
3037 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3038 WLAN_REASON_DEAUTH_LEAVING,
3039 true, deauth_buf);
3040 return RX_MGMT_CFG80211_TX_DEAUTH;
3041 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003042
3043 if (sta && elems.opmode_notif)
3044 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3045 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003046 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003047
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003048 if (elems.country_elem && elems.pwr_constr_elem &&
3049 mgmt->u.probe_resp.capab_info &
3050 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003051 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3052 elems.country_elem,
3053 elems.country_elem_len,
3054 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003055
Johannes Berg471b3ef2007-12-28 14:32:58 +01003056 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003057
3058 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003059}
3060
Johannes Berg1fa57d02010-06-10 10:21:32 +02003061void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3062 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003063{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003064 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003065 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003066 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003067 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003068 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003069 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003070 u16 fc;
3071
Jiri Bencf0706e82007-05-05 11:45:53 -07003072 rx_status = (struct ieee80211_rx_status *) skb->cb;
3073 mgmt = (struct ieee80211_mgmt *) skb->data;
3074 fc = le16_to_cpu(mgmt->frame_control);
3075
Johannes Berg77fdaa12009-07-07 03:45:17 +02003076 mutex_lock(&ifmgd->mtx);
3077
Johannes Berg66e67e42012-01-20 13:55:27 +01003078 switch (fc & IEEE80211_FCTL_STYPE) {
3079 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003080 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3081 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003082 break;
3083 case IEEE80211_STYPE_PROBE_RESP:
3084 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3085 break;
3086 case IEEE80211_STYPE_AUTH:
3087 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3088 break;
3089 case IEEE80211_STYPE_DEAUTH:
3090 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3091 break;
3092 case IEEE80211_STYPE_DISASSOC:
3093 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3094 break;
3095 case IEEE80211_STYPE_ASSOC_RESP:
3096 case IEEE80211_STYPE_REASSOC_RESP:
3097 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3098 break;
3099 case IEEE80211_STYPE_ACTION:
3100 switch (mgmt->u.action.category) {
3101 case WLAN_CATEGORY_SPECTRUM_MGMT:
3102 ieee80211_sta_process_chanswitch(sdata,
3103 &mgmt->u.action.u.chan_switch.sw_elem,
3104 (void *)ifmgd->associated->priv,
3105 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003106 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003107 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003108 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003109 mutex_unlock(&ifmgd->mtx);
3110
Johannes Berg66e67e42012-01-20 13:55:27 +01003111 switch (rma) {
3112 case RX_MGMT_NONE:
3113 /* no action */
3114 break;
3115 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003116 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003117 break;
3118 case RX_MGMT_CFG80211_DISASSOC:
3119 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3120 break;
3121 case RX_MGMT_CFG80211_RX_AUTH:
3122 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3123 break;
3124 case RX_MGMT_CFG80211_RX_ASSOC:
3125 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3126 break;
3127 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3128 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3129 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003130 case RX_MGMT_CFG80211_TX_DEAUTH:
3131 cfg80211_send_deauth(sdata->dev, deauth_buf,
3132 sizeof(deauth_buf));
3133 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003134 default:
3135 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003136 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003137}
3138
Johannes Berg9c6bd792008-09-11 00:01:52 +02003139static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003140{
3141 struct ieee80211_sub_if_data *sdata =
3142 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01003143 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003144 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003145
Johannes Berg5bb644a2009-05-17 11:40:42 +02003146 if (local->quiescing) {
3147 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3148 return;
3149 }
3150
Johannes Berg64592c82010-06-10 10:21:31 +02003151 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003152}
3153
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003154static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003155 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003156{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003157 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003158 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003159
Johannes Berg37ad3882012-02-24 13:50:54 +01003160 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003161 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003162 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003163
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003164 /*
3165 * must be outside lock due to cfg80211,
3166 * but that's not a problem.
3167 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003168 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003169
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003170 mutex_lock(&ifmgd->mtx);
3171}
3172
Johannes Berg66e67e42012-01-20 13:55:27 +01003173static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3174{
3175 struct ieee80211_local *local = sdata->local;
3176 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3177 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003178 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003179
3180 lockdep_assert_held(&ifmgd->mtx);
3181
3182 if (WARN_ON_ONCE(!auth_data))
3183 return -EINVAL;
3184
Johannes Berg1672c0e32013-01-29 15:02:27 +01003185 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3186 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3187 IEEE80211_TX_INTFL_MLME_CONN_TX;
3188
Johannes Berg66e67e42012-01-20 13:55:27 +01003189 auth_data->tries++;
3190
3191 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003192 sdata_info(sdata, "authentication with %pM timed out\n",
3193 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003194
3195 /*
3196 * Most likely AP is not in the range so remove the
3197 * bss struct for that AP.
3198 */
3199 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3200
3201 return -ETIMEDOUT;
3202 }
3203
Johannes Berga1845fc2012-06-27 13:18:36 +02003204 drv_mgd_prepare_tx(local, sdata);
3205
Johannes Berg66e67e42012-01-20 13:55:27 +01003206 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003207 u16 trans = 1;
3208 u16 status = 0;
3209
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003210 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3211 auth_data->bss->bssid, auth_data->tries,
3212 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003213
3214 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003215
3216 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3217 trans = auth_data->sae_trans;
3218 status = auth_data->sae_status;
3219 auth_data->expected_transaction = trans;
3220 }
3221
3222 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3223 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003224 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003225 auth_data->bss->bssid, NULL, 0, 0,
3226 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003227 } else {
3228 const u8 *ssidie;
3229
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003230 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3231 auth_data->bss->bssid, auth_data->tries,
3232 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003233
Johannes Berg9caf0362012-11-29 01:25:20 +01003234 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003235 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003236 if (!ssidie) {
3237 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003238 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003239 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003240 /*
3241 * Direct probe is sent to broadcast address as some APs
3242 * will not answer to direct packet in unassociated state.
3243 */
3244 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01003245 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003246 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003247 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003248 }
3249
Johannes Berg1672c0e32013-01-29 15:02:27 +01003250 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3251 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003252 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003253 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003254 } else {
3255 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003256 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003257
3258 return 0;
3259}
3260
3261static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3262{
3263 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3264 struct ieee80211_local *local = sdata->local;
3265
3266 lockdep_assert_held(&sdata->u.mgd.mtx);
3267
Johannes Berg66e67e42012-01-20 13:55:27 +01003268 assoc_data->tries++;
3269 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003270 sdata_info(sdata, "association with %pM timed out\n",
3271 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003272
3273 /*
3274 * Most likely AP is not in the range so remove the
3275 * bss struct for that AP.
3276 */
3277 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3278
3279 return -ETIMEDOUT;
3280 }
3281
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003282 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3283 assoc_data->bss->bssid, assoc_data->tries,
3284 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003285 ieee80211_send_assoc(sdata);
3286
Johannes Berg1672c0e32013-01-29 15:02:27 +01003287 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3288 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003289 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003290 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003291 } else {
3292 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003293 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003294
3295 return 0;
3296}
3297
Johannes Berg1672c0e32013-01-29 15:02:27 +01003298void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3299 __le16 fc, bool acked)
3300{
3301 struct ieee80211_local *local = sdata->local;
3302
3303 sdata->u.mgd.status_fc = fc;
3304 sdata->u.mgd.status_acked = acked;
3305 sdata->u.mgd.status_received = true;
3306
3307 ieee80211_queue_work(&local->hw, &sdata->work);
3308}
3309
Johannes Berg1fa57d02010-06-10 10:21:32 +02003310void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003311{
Johannes Berg60f8b392008-09-08 17:44:22 +02003312 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003314
Johannes Berg77fdaa12009-07-07 03:45:17 +02003315 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003316
Johannes Berg1672c0e32013-01-29 15:02:27 +01003317 if (ifmgd->status_received) {
3318 __le16 fc = ifmgd->status_fc;
3319 bool status_acked = ifmgd->status_acked;
3320
3321 ifmgd->status_received = false;
3322 if (ifmgd->auth_data &&
3323 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3324 if (status_acked) {
3325 ifmgd->auth_data->timeout =
3326 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3327 run_again(ifmgd, ifmgd->auth_data->timeout);
3328 } else {
3329 ifmgd->auth_data->timeout = jiffies - 1;
3330 }
Johannes Berg89afe612013-02-13 15:39:57 +01003331 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003332 } else if (ifmgd->assoc_data &&
3333 (ieee80211_is_assoc_req(fc) ||
3334 ieee80211_is_reassoc_req(fc))) {
3335 if (status_acked) {
3336 ifmgd->assoc_data->timeout =
3337 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3338 run_again(ifmgd, ifmgd->assoc_data->timeout);
3339 } else {
3340 ifmgd->assoc_data->timeout = jiffies - 1;
3341 }
Johannes Berg89afe612013-02-13 15:39:57 +01003342 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003343 }
3344 }
3345
Johannes Berg89afe612013-02-13 15:39:57 +01003346 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003347 time_after(jiffies, ifmgd->auth_data->timeout)) {
3348 if (ifmgd->auth_data->done) {
3349 /*
3350 * ok ... we waited for assoc but userspace didn't,
3351 * so let's just kill the auth data
3352 */
3353 ieee80211_destroy_auth_data(sdata, false);
3354 } else if (ieee80211_probe_auth(sdata)) {
3355 u8 bssid[ETH_ALEN];
3356
3357 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3358
3359 ieee80211_destroy_auth_data(sdata, false);
3360
3361 mutex_unlock(&ifmgd->mtx);
3362 cfg80211_send_auth_timeout(sdata->dev, bssid);
3363 mutex_lock(&ifmgd->mtx);
3364 }
Johannes Berg89afe612013-02-13 15:39:57 +01003365 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003366 run_again(ifmgd, ifmgd->auth_data->timeout);
3367
Johannes Berg89afe612013-02-13 15:39:57 +01003368 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003369 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003370 if ((ifmgd->assoc_data->need_beacon &&
3371 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003372 ieee80211_do_assoc(sdata)) {
3373 u8 bssid[ETH_ALEN];
3374
3375 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3376
3377 ieee80211_destroy_assoc_data(sdata, false);
3378
3379 mutex_unlock(&ifmgd->mtx);
3380 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3381 mutex_lock(&ifmgd->mtx);
3382 }
Johannes Berg89afe612013-02-13 15:39:57 +01003383 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003384 run_again(ifmgd, ifmgd->assoc_data->timeout);
3385
Johannes Bergb291ba12009-07-10 15:29:03 +02003386 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3387 IEEE80211_STA_CONNECTION_POLL) &&
3388 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003389 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003390 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003391
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003392 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003393
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003394 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003395 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003396 else
Ben Greear180205b2011-02-04 15:30:24 -08003397 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003398
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003399 /* ACK received for nullfunc probing frame */
3400 if (!ifmgd->probe_send_count)
3401 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003402 else if (ifmgd->nullfunc_failed) {
3403 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003404 mlme_dbg(sdata,
3405 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3406 bssid, ifmgd->probe_send_count,
3407 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003408 ieee80211_mgd_probe_ap_send(sdata);
3409 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003410 mlme_dbg(sdata,
3411 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3412 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003413 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003414 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3415 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003416 }
3417 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003418 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003419 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003420 mlme_dbg(sdata,
3421 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3422 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003423 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003424 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003425 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003426 mlme_dbg(sdata,
3427 "No probe response from AP %pM after %dms, try %d/%i\n",
3428 bssid, probe_wait_ms,
3429 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003430 ieee80211_mgd_probe_ap_send(sdata);
3431 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003432 /*
3433 * We actually lost the connection ... or did we?
3434 * Let's make sure!
3435 */
Ben Greearb38afa82010-10-07 16:12:06 -07003436 wiphy_debug(local->hw.wiphy,
3437 "%s: No probe response from AP %pM"
3438 " after %dms, disconnecting.\n",
3439 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003440 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003441
Johannes Berg95acac62011-07-12 12:30:59 +02003442 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003443 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003444 }
3445 }
3446
Johannes Berg77fdaa12009-07-07 03:45:17 +02003447 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003448}
Johannes Berg0a51b272008-09-08 17:44:25 +02003449
Johannes Bergb291ba12009-07-10 15:29:03 +02003450static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3451{
3452 struct ieee80211_sub_if_data *sdata =
3453 (struct ieee80211_sub_if_data *) data;
3454 struct ieee80211_local *local = sdata->local;
3455
3456 if (local->quiescing)
3457 return;
3458
Johannes Berg682bd382013-01-29 13:13:50 +01003459 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003460 ieee80211_queue_work(&sdata->local->hw,
3461 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003462}
3463
3464static void ieee80211_sta_conn_mon_timer(unsigned long data)
3465{
3466 struct ieee80211_sub_if_data *sdata =
3467 (struct ieee80211_sub_if_data *) data;
3468 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3469 struct ieee80211_local *local = sdata->local;
3470
3471 if (local->quiescing)
3472 return;
3473
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003474 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003475}
3476
3477static void ieee80211_sta_monitor_work(struct work_struct *work)
3478{
3479 struct ieee80211_sub_if_data *sdata =
3480 container_of(work, struct ieee80211_sub_if_data,
3481 u.mgd.monitor_work);
3482
3483 ieee80211_mgd_probe_ap(sdata, false);
3484}
3485
Johannes Berga1678f82008-09-11 00:01:47 +02003486static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3487{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003488 u32 flags;
3489
Kalle Vaload935682009-04-19 08:47:19 +03003490 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003491 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003492
Johannes Bergb291ba12009-07-10 15:29:03 +02003493 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003494 flags = sdata->local->hw.flags;
3495 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3496 ieee80211_queue_work(&sdata->local->hw,
3497 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003498 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003499 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003500 }
Johannes Berga1678f82008-09-11 00:01:47 +02003501}
3502
Johannes Berg5bb644a2009-05-17 11:40:42 +02003503#ifdef CONFIG_PM
3504void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3505{
3506 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3507
3508 /*
Ben Greear9b5bd5a2013-02-20 09:41:08 -08003509 * Stop timers before deleting work items, as timers
3510 * could race and re-add the work-items. They will be
3511 * re-established on connection.
3512 */
3513 del_timer_sync(&ifmgd->conn_mon_timer);
3514 del_timer_sync(&ifmgd->bcn_mon_timer);
3515
3516 /*
Johannes Berg5bb644a2009-05-17 11:40:42 +02003517 * we need to use atomic bitops for the running bits
3518 * only because both timers might fire at the same
3519 * time -- the code here is properly synchronised.
3520 */
3521
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003522 cancel_work_sync(&ifmgd->request_smps_work);
3523
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003524 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003525 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003526 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003527 if (del_timer_sync(&ifmgd->timer))
3528 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3529
Johannes Berg5bb644a2009-05-17 11:40:42 +02003530 if (del_timer_sync(&ifmgd->chswitch_timer))
3531 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Ben Greear9b5bd5a2013-02-20 09:41:08 -08003532 cancel_work_sync(&ifmgd->chswitch_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003533}
3534
3535void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3536{
3537 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3538
Johannes Berg782d2672013-01-18 21:21:31 +01003539 mutex_lock(&ifmgd->mtx);
3540 if (!ifmgd->associated) {
3541 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303542 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003543 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303544
Johannes Berg95acac62011-07-12 12:30:59 +02003545 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3546 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003547 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3548 ieee80211_sta_connection_lost(sdata,
3549 ifmgd->associated->bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003550 WLAN_REASON_UNSPECIFIED,
3551 true);
Johannes Berg95acac62011-07-12 12:30:59 +02003552 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003553 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003554 }
Johannes Berg782d2672013-01-18 21:21:31 +01003555 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003556
Johannes Berg5bb644a2009-05-17 11:40:42 +02003557 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3558 add_timer(&ifmgd->timer);
3559 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3560 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003561 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003562
3563 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003564 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003565 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003566}
3567#endif
3568
Johannes Berg9c6bd792008-09-11 00:01:52 +02003569/* interface setup */
3570void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3571{
Johannes Berg46900292009-02-15 12:44:28 +01003572 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003573
Johannes Berg46900292009-02-15 12:44:28 +01003574 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003575 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003576 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003577 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3578 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003579 INIT_WORK(&ifmgd->csa_connection_drop_work,
3580 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003581 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003582 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003583 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003584 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3585 (unsigned long) sdata);
3586 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3587 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003588 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303589 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003590
Johannes Bergab1faea2009-07-01 21:41:17 +02003591 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303592 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003593 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3594 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003595
Johannes Berg77fdaa12009-07-07 03:45:17 +02003596 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003597
3598 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3599 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3600 else
3601 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003602}
3603
3604/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003605void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3606{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003607 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003608
3609 /* Restart STA timers */
3610 rcu_read_lock();
3611 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3612 ieee80211_restart_sta_timer(sdata);
3613 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003614}
Johannes Berg10f644a2009-04-16 13:17:25 +02003615
3616int ieee80211_max_network_latency(struct notifier_block *nb,
3617 unsigned long data, void *dummy)
3618{
3619 s32 latency_usec = (s32) data;
3620 struct ieee80211_local *local =
3621 container_of(nb, struct ieee80211_local,
3622 network_latency_notifier);
3623
3624 mutex_lock(&local->iflist_mtx);
3625 ieee80211_recalc_ps(local, latency_usec);
3626 mutex_unlock(&local->iflist_mtx);
3627
3628 return 0;
3629}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003630
Johannes Bergf2d9d272012-11-22 14:11:39 +01003631static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3632 struct cfg80211_bss *cbss)
3633{
3634 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3635 const u8 *ht_cap_ie, *vht_cap_ie;
3636 const struct ieee80211_ht_cap *ht_cap;
3637 const struct ieee80211_vht_cap *vht_cap;
3638 u8 chains = 1;
3639
3640 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3641 return chains;
3642
Johannes Berg9caf0362012-11-29 01:25:20 +01003643 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003644 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3645 ht_cap = (void *)(ht_cap_ie + 2);
3646 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3647 /*
3648 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3649 * "Tx Unequal Modulation Supported" fields.
3650 */
3651 }
3652
3653 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3654 return chains;
3655
Johannes Berg9caf0362012-11-29 01:25:20 +01003656 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003657 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3658 u8 nss;
3659 u16 tx_mcs_map;
3660
3661 vht_cap = (void *)(vht_cap_ie + 2);
3662 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3663 for (nss = 8; nss > 0; nss--) {
3664 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3665 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3666 break;
3667 }
3668 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3669 chains = max(chains, nss);
3670 }
3671
3672 return chains;
3673}
3674
Johannes Bergb17166a2012-07-27 11:41:27 +02003675static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3676 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003677{
3678 struct ieee80211_local *local = sdata->local;
3679 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003680 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003681 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003682 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003683 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003684 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003685
Johannes Berg24398e32012-03-28 10:58:36 +02003686 sband = local->hw.wiphy->bands[cbss->channel->band];
3687
Johannes Bergf2d9d272012-11-22 14:11:39 +01003688 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3689 IEEE80211_STA_DISABLE_80P80MHZ |
3690 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003691
Johannes Berg9caf0362012-11-29 01:25:20 +01003692 rcu_read_lock();
3693
Johannes Bergf2d9d272012-11-22 14:11:39 +01003694 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3695 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003696 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003697
Johannes Berg9caf0362012-11-29 01:25:20 +01003698 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003699 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3700 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003701
3702 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3703 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3704 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3705 ht_oper = NULL;
3706 }
Johannes Berg24398e32012-03-28 10:58:36 +02003707 }
3708
Johannes Bergf2d9d272012-11-22 14:11:39 +01003709 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3710 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003711 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003712
Johannes Berg9caf0362012-11-29 01:25:20 +01003713 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3714 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003715 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3716 vht_oper = (void *)(vht_oper_ie + 2);
3717 if (vht_oper && !ht_oper) {
3718 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003719 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003720 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003721 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3722 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003723 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003724
3725 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3726 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3727 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3728 vht_oper = NULL;
3729 }
Johannes Berg24398e32012-03-28 10:58:36 +02003730 }
3731
Johannes Bergf2d9d272012-11-22 14:11:39 +01003732 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3733 cbss->channel,
3734 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003735 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003736
Johannes Bergf2d9d272012-11-22 14:11:39 +01003737 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3738 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003739
Johannes Berg9caf0362012-11-29 01:25:20 +01003740 rcu_read_unlock();
3741
Johannes Berg04ecd252012-09-11 14:34:12 +02003742 /* will change later if needed */
3743 sdata->smps_mode = IEEE80211_SMPS_OFF;
3744
Johannes Bergf2d9d272012-11-22 14:11:39 +01003745 /*
3746 * If this fails (possibly due to channel context sharing
3747 * on incompatible channels, e.g. 80+80 and 160 sharing the
3748 * same control channel) try to use a smaller bandwidth.
3749 */
3750 ret = ieee80211_vif_use_channel(sdata, &chandef,
3751 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003752 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003753 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003754 ret = ieee80211_vif_use_channel(sdata, &chandef,
3755 IEEE80211_CHANCTX_SHARED);
3756 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003757 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003758}
3759
3760static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3761 struct cfg80211_bss *cbss, bool assoc)
3762{
3763 struct ieee80211_local *local = sdata->local;
3764 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3765 struct ieee80211_bss *bss = (void *)cbss->priv;
3766 struct sta_info *new_sta = NULL;
3767 bool have_sta = false;
3768 int err;
3769
3770 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3771 return -EINVAL;
3772
3773 if (assoc) {
3774 rcu_read_lock();
3775 have_sta = sta_info_get(sdata, cbss->bssid);
3776 rcu_read_unlock();
3777 }
3778
3779 if (!have_sta) {
3780 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3781 if (!new_sta)
3782 return -ENOMEM;
3783 }
3784
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003785 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003786 u32 rates = 0, basic_rates = 0;
3787 bool have_higher_than_11mbit;
3788 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003789 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003790 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003791
3792 sband = local->hw.wiphy->bands[cbss->channel->band];
3793
3794 err = ieee80211_prep_channel(sdata, cbss);
3795 if (err) {
3796 sta_info_free(local, new_sta);
3797 return err;
3798 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003799
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003800 ieee80211_get_rates(sband, bss->supp_rates,
3801 bss->supp_rates_len,
3802 &rates, &basic_rates,
3803 &have_higher_than_11mbit,
3804 &min_rate, &min_rate_index);
3805
3806 /*
3807 * This used to be a workaround for basic rates missing
3808 * in the association response frame. Now that we no
3809 * longer use the basic rates from there, it probably
3810 * doesn't happen any more, but keep the workaround so
3811 * in case some *other* APs are buggy in different ways
3812 * we can connect -- with a warning.
3813 */
3814 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003815 sdata_info(sdata,
3816 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003817 basic_rates = BIT(min_rate_index);
3818 }
3819
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003820 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003821 sdata->vif.bss_conf.basic_rates = basic_rates;
3822
3823 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003824 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003825 have_higher_than_11mbit)
3826 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3827 else
3828 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3829
3830 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3831
Johannes Berg8c358bc2012-05-22 22:13:05 +02003832 /* set timing information */
3833 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003834 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003835 ies = rcu_dereference(cbss->beacon_ies);
3836 if (ies) {
3837 const u8 *tim_ie;
3838
3839 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3840 sdata->vif.bss_conf.sync_device_ts =
3841 bss->device_ts_beacon;
3842 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3843 ies->data, ies->len);
3844 if (tim_ie && tim_ie[1] >= 2)
3845 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3846 else
3847 sdata->vif.bss_conf.sync_dtim_count = 0;
3848 } else if (!(local->hw.flags &
3849 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3850 ies = rcu_dereference(cbss->proberesp_ies);
3851 /* must be non-NULL since beacon IEs were NULL */
3852 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3853 sdata->vif.bss_conf.sync_device_ts =
3854 bss->device_ts_presp;
3855 sdata->vif.bss_conf.sync_dtim_count = 0;
3856 } else {
3857 sdata->vif.bss_conf.sync_tsf = 0;
3858 sdata->vif.bss_conf.sync_device_ts = 0;
3859 sdata->vif.bss_conf.sync_dtim_count = 0;
3860 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003861 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003862
3863 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003864 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003865 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3866 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003867
3868 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003869 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003870
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003871 err = sta_info_insert(new_sta);
3872 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003873 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003874 sdata_info(sdata,
3875 "failed to insert STA entry for the AP (error %d)\n",
3876 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003877 return err;
3878 }
3879 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003880 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003881
3882 return 0;
3883}
3884
Johannes Berg77fdaa12009-07-07 03:45:17 +02003885/* config hooks */
3886int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3887 struct cfg80211_auth_request *req)
3888{
Johannes Berg66e67e42012-01-20 13:55:27 +01003889 struct ieee80211_local *local = sdata->local;
3890 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3891 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003892 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003893 int err;
3894
3895 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003896
3897 switch (req->auth_type) {
3898 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3899 auth_alg = WLAN_AUTH_OPEN;
3900 break;
3901 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003902 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003903 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003904 auth_alg = WLAN_AUTH_SHARED_KEY;
3905 break;
3906 case NL80211_AUTHTYPE_FT:
3907 auth_alg = WLAN_AUTH_FT;
3908 break;
3909 case NL80211_AUTHTYPE_NETWORK_EAP:
3910 auth_alg = WLAN_AUTH_LEAP;
3911 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003912 case NL80211_AUTHTYPE_SAE:
3913 auth_alg = WLAN_AUTH_SAE;
3914 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003915 default:
3916 return -EOPNOTSUPP;
3917 }
3918
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003919 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3920 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003921 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003922 return -ENOMEM;
3923
Johannes Berg66e67e42012-01-20 13:55:27 +01003924 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003925
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003926 if (req->sae_data_len >= 4) {
3927 __le16 *pos = (__le16 *) req->sae_data;
3928 auth_data->sae_trans = le16_to_cpu(pos[0]);
3929 auth_data->sae_status = le16_to_cpu(pos[1]);
3930 memcpy(auth_data->data, req->sae_data + 4,
3931 req->sae_data_len - 4);
3932 auth_data->data_len += req->sae_data_len - 4;
3933 }
3934
Johannes Berg77fdaa12009-07-07 03:45:17 +02003935 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003936 memcpy(&auth_data->data[auth_data->data_len],
3937 req->ie, req->ie_len);
3938 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003939 }
3940
Johannes Bergfffd0932009-07-08 14:22:54 +02003941 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003942 auth_data->key_len = req->key_len;
3943 auth_data->key_idx = req->key_idx;
3944 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003945 }
3946
Johannes Berg66e67e42012-01-20 13:55:27 +01003947 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003948
Johannes Berg66e67e42012-01-20 13:55:27 +01003949 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003950
Johannes Berg66e67e42012-01-20 13:55:27 +01003951 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003952
Johannes Berg66e67e42012-01-20 13:55:27 +01003953 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3954 ifmgd->assoc_data) {
3955 err = -EBUSY;
3956 goto err_free;
3957 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003958
Johannes Berg66e67e42012-01-20 13:55:27 +01003959 if (ifmgd->auth_data)
3960 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003961
Johannes Berg66e67e42012-01-20 13:55:27 +01003962 /* prep auth_data so we don't go into idle on disassoc */
3963 ifmgd->auth_data = auth_data;
3964
3965 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003966 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003967
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003968 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003969
Johannes Berga1cf7752012-03-08 15:02:07 +01003970 err = ieee80211_prep_connection(sdata, req->bss, false);
3971 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003972 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003973
Johannes Berg66e67e42012-01-20 13:55:27 +01003974 err = ieee80211_probe_auth(sdata);
3975 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003976 sta_info_destroy_addr(sdata, req->bss->bssid);
3977 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003978 }
3979
Johannes Berg66e67e42012-01-20 13:55:27 +01003980 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003981 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003982 err = 0;
3983 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003984
Johannes Berg66e67e42012-01-20 13:55:27 +01003985 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003986 memset(ifmgd->bssid, 0, ETH_ALEN);
3987 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003988 ifmgd->auth_data = NULL;
3989 err_free:
3990 kfree(auth_data);
3991 out_unlock:
3992 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003993
Johannes Berg66e67e42012-01-20 13:55:27 +01003994 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003995}
3996
Johannes Berg77fdaa12009-07-07 03:45:17 +02003997int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3998 struct cfg80211_assoc_request *req)
3999{
Johannes Berg66e67e42012-01-20 13:55:27 +01004000 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004001 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004002 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004003 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004004 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004005 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004006 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004007 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004008
Johannes Berg66e67e42012-01-20 13:55:27 +01004009 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4010 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004011 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004012
Johannes Berg9caf0362012-11-29 01:25:20 +01004013 rcu_read_lock();
4014 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4015 if (!ssidie) {
4016 rcu_read_unlock();
4017 kfree(assoc_data);
4018 return -EINVAL;
4019 }
4020 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4021 assoc_data->ssid_len = ssidie[1];
4022 rcu_read_unlock();
4023
Johannes Berg66e67e42012-01-20 13:55:27 +01004024 mutex_lock(&ifmgd->mtx);
4025
4026 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01004027 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01004028
4029 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4030 err = -EBUSY;
4031 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004032 }
4033
Johannes Berg66e67e42012-01-20 13:55:27 +01004034 if (ifmgd->assoc_data) {
4035 err = -EBUSY;
4036 goto err_free;
4037 }
4038
4039 if (ifmgd->auth_data) {
4040 bool match;
4041
4042 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004043 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004044 ieee80211_destroy_auth_data(sdata, match);
4045 }
4046
4047 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02004048
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004049 ifmgd->beacon_crc_valid = false;
4050
Johannes Bergde5036a2012-03-08 15:02:03 +01004051 /*
4052 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4053 * We still associate in non-HT mode (11a/b/g) if any one of these
4054 * ciphers is configured as pairwise.
4055 * We can set this to true for non-11n hardware, that'll be checked
4056 * separately along with the peer capabilities.
4057 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004058 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004059 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4060 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004061 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004062 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004063 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004064 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004065 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004066 }
4067 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004068
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004069 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004070 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004071 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4072 }
Ben Greearef96a8422011-11-18 11:32:00 -08004073
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004074 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4075 sband = local->hw.wiphy->bands[req->bss->channel->band];
4076 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004077 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004078 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004079 if (!bss->wmm_used)
4080 netdev_info(sdata->dev,
4081 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004082 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004083
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004084 /* disable VHT if we don't support it or the AP doesn't use WMM */
4085 if (!sband->vht_cap.vht_supported ||
4086 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4087 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004088 if (!bss->wmm_used)
4089 netdev_info(sdata->dev,
4090 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004091 }
4092
Ben Greearef96a8422011-11-18 11:32:00 -08004093 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4094 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4095 sizeof(ifmgd->ht_capa_mask));
4096
Johannes Berg77fdaa12009-07-07 03:45:17 +02004097 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004098 memcpy(assoc_data->ie, req->ie, req->ie_len);
4099 assoc_data->ie_len = req->ie_len;
4100 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004101
Johannes Berg66e67e42012-01-20 13:55:27 +01004102 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004103
Johannes Bergaf6b6372009-12-23 13:15:35 +01004104 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4105 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004106 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004107 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004108 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004109 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004110 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004111
Johannes Berg66e67e42012-01-20 13:55:27 +01004112 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004113 assoc_data->wmm = bss->wmm_used &&
4114 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004115 assoc_data->supp_rates = bss->supp_rates;
4116 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004117
Johannes Berg9caf0362012-11-29 01:25:20 +01004118 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004119 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4120 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4121 assoc_data->ap_ht_param =
4122 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4123 else
Johannes Berga8243b72012-11-22 14:32:09 +01004124 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004125 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4126 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4127 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4128 sizeof(struct ieee80211_vht_cap));
4129 else
4130 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004131 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004132
Kalle Valoab133152010-01-12 10:42:31 +02004133 if (bss->wmm_used && bss->uapsd_supported &&
4134 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01004135 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004136 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4137 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004138 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004139 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4140 }
4141
Johannes Berg77fdaa12009-07-07 03:45:17 +02004142 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004143 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004144
4145 if (req->use_mfp) {
4146 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4147 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4148 } else {
4149 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4150 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4151 }
4152
4153 if (req->crypto.control_port)
4154 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4155 else
4156 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4157
Johannes Berga621fa42010-08-27 14:26:54 +03004158 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4159 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4160
Johannes Berg66e67e42012-01-20 13:55:27 +01004161 /* kick off associate process */
4162
4163 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004164 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004165
Johannes Berga1cf7752012-03-08 15:02:07 +01004166 err = ieee80211_prep_connection(sdata, req->bss, true);
4167 if (err)
4168 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004169
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004170 rcu_read_lock();
4171 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004172
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004173 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4174 !beacon_ies) {
4175 /*
4176 * Wait up to one beacon interval ...
4177 * should this be more if we miss one?
4178 */
4179 sdata_info(sdata, "waiting for beacon from %pM\n",
4180 ifmgd->bssid);
4181 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004182 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004183 assoc_data->need_beacon = true;
4184 } else if (beacon_ies) {
4185 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4186 beacon_ies->data,
4187 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004188 u8 dtim_count = 0;
4189
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004190 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4191 const struct ieee80211_tim_ie *tim;
4192 tim = (void *)(tim_ie + 2);
4193 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004194 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004195 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004196 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004197 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004198 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004199
4200 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4201 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4202 sdata->vif.bss_conf.sync_device_ts =
4203 bss->device_ts_beacon;
4204 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4205 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004206 } else {
4207 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004208 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004209 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004210 rcu_read_unlock();
4211
Johannes Berg66e67e42012-01-20 13:55:27 +01004212 run_again(ifmgd, assoc_data->timeout);
4213
Paul Stewartfcff4f12012-02-23 17:59:53 -08004214 if (bss->corrupt_data) {
4215 char *corrupt_type = "data";
4216 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4217 if (bss->corrupt_data &
4218 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4219 corrupt_type = "beacon and probe response";
4220 else
4221 corrupt_type = "beacon";
4222 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4223 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004224 sdata_info(sdata, "associating with AP with corrupt %s\n",
4225 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004226 }
4227
Johannes Berg66e67e42012-01-20 13:55:27 +01004228 err = 0;
4229 goto out;
4230 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004231 memset(ifmgd->bssid, 0, ETH_ALEN);
4232 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004233 ifmgd->assoc_data = NULL;
4234 err_free:
4235 kfree(assoc_data);
4236 out:
4237 mutex_unlock(&ifmgd->mtx);
4238
4239 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004240}
4241
4242int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004243 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004244{
4245 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004246 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004247 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004248 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004249
Johannes Berg77fdaa12009-07-07 03:45:17 +02004250 mutex_lock(&ifmgd->mtx);
4251
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004252 sdata_info(sdata,
4253 "deauthenticating from %pM by local choice (reason=%d)\n",
4254 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004255
Johannes Berg86552012012-10-29 09:46:31 +01004256 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004257 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004258 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4259 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004260 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004261 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004262 ieee80211_destroy_auth_data(sdata, false);
4263 mutex_unlock(&ifmgd->mtx);
4264
4265 sent_frame = tx;
4266 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004267 }
4268
Johannes Berg86552012012-10-29 09:46:31 +01004269 if (ifmgd->associated &&
4270 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4271 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4272 req->reason_code, tx, frame_buf);
4273 sent_frame = tx;
4274 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004275 mutex_unlock(&ifmgd->mtx);
4276
Johannes Berg86552012012-10-29 09:46:31 +01004277 out:
Johannes Berg86552012012-10-29 09:46:31 +01004278 if (sent_frame)
4279 __cfg80211_send_deauth(sdata->dev, frame_buf,
4280 IEEE80211_DEAUTH_FRAME_LEN);
4281
Johannes Berg77fdaa12009-07-07 03:45:17 +02004282 return 0;
4283}
4284
4285int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004286 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004287{
4288 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004289 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004290 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004291
Johannes Berg77fdaa12009-07-07 03:45:17 +02004292 mutex_lock(&ifmgd->mtx);
4293
Johannes Berg8d8b2612009-07-25 11:58:36 +02004294 /*
4295 * cfg80211 should catch this ... but it's racy since
4296 * we can receive a disassoc frame, process it, hand it
4297 * to cfg80211 while that's in a locked section already
4298 * trying to tell us that the user wants to disconnect.
4299 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004300 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004301 mutex_unlock(&ifmgd->mtx);
4302 return -ENOLINK;
4303 }
4304
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004305 sdata_info(sdata,
4306 "disassociating from %pM by local choice (reason=%d)\n",
4307 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004308
Jouni Malinene69e95d2010-03-29 23:29:31 -07004309 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004310 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4311 req->reason_code, !req->local_state_change,
4312 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004313 mutex_unlock(&ifmgd->mtx);
4314
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004315 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4316 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004317
Johannes Berg77fdaa12009-07-07 03:45:17 +02004318 return 0;
4319}
Jouni Malinen026331c2010-02-15 12:53:10 +02004320
Eliad Pellerafa762f2012-04-23 14:45:15 +03004321void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004322{
4323 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4324
Ben Greear49921852013-02-20 09:41:09 -08004325 /*
4326 * Make sure some work items will not run after this,
4327 * they will not do anything but might not have been
4328 * cancelled when disconnecting.
4329 */
4330 cancel_work_sync(&ifmgd->monitor_work);
4331 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4332 cancel_work_sync(&ifmgd->request_smps_work);
4333 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4334 cancel_work_sync(&ifmgd->chswitch_work);
4335
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004336 mutex_lock(&ifmgd->mtx);
4337 if (ifmgd->assoc_data)
4338 ieee80211_destroy_assoc_data(sdata, false);
4339 if (ifmgd->auth_data)
4340 ieee80211_destroy_auth_data(sdata, false);
4341 del_timer_sync(&ifmgd->timer);
4342 mutex_unlock(&ifmgd->mtx);
4343}
4344
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004345void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4346 enum nl80211_cqm_rssi_threshold_event rssi_event,
4347 gfp_t gfp)
4348{
4349 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4350
Johannes Bergb5878a22010-04-07 16:48:40 +02004351 trace_api_cqm_rssi_notify(sdata, rssi_event);
4352
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004353 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4354}
4355EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);