blob: 7a8cd789e487598d0396925ae2566fd38fce9be0 [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:
360 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
361 IEEE80211_CHAN_DISABLED)) {
362 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
363 ret = IEEE80211_STA_DISABLE_HT |
364 IEEE80211_STA_DISABLE_VHT;
365 goto out;
366 }
367
368 ret |= chandef_downgrade(chandef);
369 }
370
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100371 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100372 sdata_info(sdata,
373 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
374
375 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
376 return ret;
377}
378
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100379static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
380 struct sta_info *sta,
381 const struct ieee80211_ht_operation *ht_oper,
382 const struct ieee80211_vht_operation *vht_oper,
383 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200384{
385 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100386 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200387 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200388 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100389 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200390 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100391 u32 flags;
392 enum ieee80211_sta_rx_bandwidth new_sta_bw;
393 int ret;
394
395 /* if HT was/is disabled, don't track any bandwidth changes */
396 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
397 return 0;
398
399 /* don't check VHT if we associated as non-VHT station */
400 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
401 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200402
Johannes Berg11289582013-02-07 17:36:12 +0100403 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100404 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100405
Johannes Bergf2d93302013-02-08 13:15:55 +0100406 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200407 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200408
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100409 /* calculate new channel (type) based on HT/VHT operation IEs */
410 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
411 vht_oper, &chandef, false);
412
413 /*
414 * Downgrade the new channel if we associated with restricted
415 * capabilities. For example, if we associated as a 20 MHz STA
416 * to a 40 MHz AP (due to regulatory, capabilities or config
417 * reasons) then switching to a 40 MHz channel now won't do us
418 * any good -- we couldn't use it with the AP.
419 */
420 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
421 chandef.width == NL80211_CHAN_WIDTH_80P80)
422 flags |= chandef_downgrade(&chandef);
423 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
424 chandef.width == NL80211_CHAN_WIDTH_160)
425 flags |= chandef_downgrade(&chandef);
426 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
427 chandef.width > NL80211_CHAN_WIDTH_20)
428 flags |= chandef_downgrade(&chandef);
429
430 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
431 return 0;
432
433 sdata_info(sdata,
434 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
435 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
436 chandef.center_freq1, chandef.center_freq2);
437
438 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
439 IEEE80211_STA_DISABLE_VHT |
440 IEEE80211_STA_DISABLE_40MHZ |
441 IEEE80211_STA_DISABLE_80P80MHZ |
442 IEEE80211_STA_DISABLE_160MHZ)) ||
443 !cfg80211_chandef_valid(&chandef)) {
444 sdata_info(sdata,
445 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
446 ifmgd->bssid);
447 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200448 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200449
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100450 switch (chandef.width) {
451 case NL80211_CHAN_WIDTH_20_NOHT:
452 case NL80211_CHAN_WIDTH_20:
453 new_sta_bw = IEEE80211_STA_RX_BW_20;
454 break;
455 case NL80211_CHAN_WIDTH_40:
456 new_sta_bw = IEEE80211_STA_RX_BW_40;
457 break;
458 case NL80211_CHAN_WIDTH_80:
459 new_sta_bw = IEEE80211_STA_RX_BW_80;
460 break;
461 case NL80211_CHAN_WIDTH_80P80:
462 case NL80211_CHAN_WIDTH_160:
463 new_sta_bw = IEEE80211_STA_RX_BW_160;
464 break;
465 default:
466 return -EINVAL;
467 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200468
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100469 if (new_sta_bw > sta->cur_max_bandwidth)
470 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200471
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100472 if (new_sta_bw < sta->sta.bandwidth) {
473 sta->sta.bandwidth = new_sta_bw;
474 rate_control_rate_update(local, sband, sta,
475 IEEE80211_RC_BW_CHANGED);
476 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530477
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100478 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
479 if (ret) {
480 sdata_info(sdata,
481 "AP %pM changed bandwidth to incompatible one - disconnect\n",
482 ifmgd->bssid);
483 return ret;
484 }
485
486 if (new_sta_bw > sta->sta.bandwidth) {
487 sta->sta.bandwidth = new_sta_bw;
488 rate_control_rate_update(local, sband, sta,
489 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200490 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200491
Johannes Berg074d46d2012-03-15 19:45:16 +0100492 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200493
494 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100495 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
496 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200497 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200498 }
499
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100500 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200501}
502
Johannes Berg9c6bd792008-09-11 00:01:52 +0200503/* frame sending functions */
504
Johannes Berg66e67e42012-01-20 13:55:27 +0100505static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
506 struct ieee80211_supported_band *sband,
507 u32 *rates)
508{
509 int i, j, count;
510 *rates = 0;
511 count = 0;
512 for (i = 0; i < supp_rates_len; i++) {
513 int rate = (supp_rates[i] & 0x7F) * 5;
514
515 for (j = 0; j < sband->n_bitrates; j++)
516 if (sband->bitrates[j].bitrate == rate) {
517 *rates |= BIT(j);
518 count++;
519 break;
520 }
521 }
522
523 return count;
524}
525
526static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200527 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100528 struct ieee80211_supported_band *sband,
529 struct ieee80211_channel *channel,
530 enum ieee80211_smps_mode smps)
531{
Johannes Berg66e67e42012-01-20 13:55:27 +0100532 u8 *pos;
533 u32 flags = channel->flags;
534 u16 cap;
535 struct ieee80211_sta_ht_cap ht_cap;
536
537 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
538
Johannes Berg66e67e42012-01-20 13:55:27 +0100539 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
540 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
541
Johannes Berg66e67e42012-01-20 13:55:27 +0100542 /* determine capability flags */
543 cap = ht_cap.cap;
544
Johannes Berg9dde6422012-05-16 23:43:19 +0200545 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100546 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
547 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
548 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
549 cap &= ~IEEE80211_HT_CAP_SGI_40;
550 }
551 break;
552 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
553 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
554 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
555 cap &= ~IEEE80211_HT_CAP_SGI_40;
556 }
557 break;
558 }
559
Johannes Berg24398e32012-03-28 10:58:36 +0200560 /*
561 * If 40 MHz was disabled associate as though we weren't
562 * capable of 40 MHz -- some broken APs will never fall
563 * back to trying to transmit in 20 MHz.
564 */
565 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
566 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
567 cap &= ~IEEE80211_HT_CAP_SGI_40;
568 }
569
Johannes Berg66e67e42012-01-20 13:55:27 +0100570 /* set SM PS mode properly */
571 cap &= ~IEEE80211_HT_CAP_SM_PS;
572 switch (smps) {
573 case IEEE80211_SMPS_AUTOMATIC:
574 case IEEE80211_SMPS_NUM_MODES:
575 WARN_ON(1);
576 case IEEE80211_SMPS_OFF:
577 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
578 IEEE80211_HT_CAP_SM_PS_SHIFT;
579 break;
580 case IEEE80211_SMPS_STATIC:
581 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
582 IEEE80211_HT_CAP_SM_PS_SHIFT;
583 break;
584 case IEEE80211_SMPS_DYNAMIC:
585 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
586 IEEE80211_HT_CAP_SM_PS_SHIFT;
587 break;
588 }
589
590 /* reserve and fill IE */
591 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
592 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
593}
594
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000595static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
596 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100597 struct ieee80211_supported_band *sband,
598 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599{
600 u8 *pos;
601 u32 cap;
602 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100603 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000604
605 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
606
607 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
608
609 /* determine capability flags */
610 cap = vht_cap.cap;
611
Johannes Bergf2d9d272012-11-22 14:11:39 +0100612 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
613 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
614 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
615 }
616
617 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
618 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
619 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
620 }
621
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100622 /*
623 * Some APs apparently get confused if our capabilities are better
624 * than theirs, so restrict what we advertise in the assoc request.
625 */
626 if (!(ap_vht_cap->vht_cap_info &
627 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
628 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
629
630 if (!(ap_vht_cap->vht_cap_info &
631 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
632 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
633 IEEE80211_VHT_CAP_RXSTBC_3 |
634 IEEE80211_VHT_CAP_RXSTBC_4);
635
636 for (i = 0; i < 8; i++) {
637 int shift = i * 2;
638 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
639 u16 ap_mcs, our_mcs;
640
641 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
642 mask) >> shift;
643 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
644 mask) >> shift;
645
646 switch (ap_mcs) {
647 default:
648 if (our_mcs <= ap_mcs)
649 break;
650 /* fall through */
651 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
652 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
653 vht_cap.vht_mcs.rx_mcs_map |=
654 cpu_to_le16(ap_mcs << shift);
655 }
656 }
657
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000658 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000659 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000660 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
661}
662
Johannes Berg66e67e42012-01-20 13:55:27 +0100663static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
664{
665 struct ieee80211_local *local = sdata->local;
666 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
667 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
668 struct sk_buff *skb;
669 struct ieee80211_mgmt *mgmt;
670 u8 *pos, qos_info;
671 size_t offset = 0, noffset;
672 int i, count, rates_len, supp_rates_len;
673 u16 capab;
674 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200675 struct ieee80211_chanctx_conf *chanctx_conf;
676 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100677 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100678
679 lockdep_assert_held(&ifmgd->mtx);
680
Johannes Berg55de9082012-07-26 17:24:39 +0200681 rcu_read_lock();
682 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
683 if (WARN_ON(!chanctx_conf)) {
684 rcu_read_unlock();
685 return;
686 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100687 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200688 rcu_read_unlock();
689 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100690
691 if (assoc_data->supp_rates_len) {
692 /*
693 * Get all rates supported by the device and the AP as
694 * some APs don't like getting a superset of their rates
695 * in the association request (e.g. D-Link DAP 1353 in
696 * b-only mode)...
697 */
698 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
699 assoc_data->supp_rates_len,
700 sband, &rates);
701 } else {
702 /*
703 * In case AP not provide any supported rates information
704 * before association, we send information element(s) with
705 * all rates that we support.
706 */
707 rates = ~0;
708 rates_len = sband->n_bitrates;
709 }
710
711 skb = alloc_skb(local->hw.extra_tx_headroom +
712 sizeof(*mgmt) + /* bit too much but doesn't matter */
713 2 + assoc_data->ssid_len + /* SSID */
714 4 + rates_len + /* (extended) rates */
715 4 + /* power capability */
716 2 + 2 * sband->n_channels + /* supported channels */
717 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000718 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100719 assoc_data->ie_len + /* extra IEs */
720 9, /* WMM */
721 GFP_KERNEL);
722 if (!skb)
723 return;
724
725 skb_reserve(skb, local->hw.extra_tx_headroom);
726
727 capab = WLAN_CAPABILITY_ESS;
728
729 if (sband->band == IEEE80211_BAND_2GHZ) {
730 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
731 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
732 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
733 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
734 }
735
736 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
737 capab |= WLAN_CAPABILITY_PRIVACY;
738
739 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
740 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
741 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
742
743 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
744 memset(mgmt, 0, 24);
745 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
746 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
747 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
748
749 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
750 skb_put(skb, 10);
751 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
752 IEEE80211_STYPE_REASSOC_REQ);
753 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
754 mgmt->u.reassoc_req.listen_interval =
755 cpu_to_le16(local->hw.conf.listen_interval);
756 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
757 ETH_ALEN);
758 } else {
759 skb_put(skb, 4);
760 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
761 IEEE80211_STYPE_ASSOC_REQ);
762 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
763 mgmt->u.assoc_req.listen_interval =
764 cpu_to_le16(local->hw.conf.listen_interval);
765 }
766
767 /* SSID */
768 pos = skb_put(skb, 2 + assoc_data->ssid_len);
769 *pos++ = WLAN_EID_SSID;
770 *pos++ = assoc_data->ssid_len;
771 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
772
773 /* add all rates which were marked to be used above */
774 supp_rates_len = rates_len;
775 if (supp_rates_len > 8)
776 supp_rates_len = 8;
777
778 pos = skb_put(skb, supp_rates_len + 2);
779 *pos++ = WLAN_EID_SUPP_RATES;
780 *pos++ = supp_rates_len;
781
782 count = 0;
783 for (i = 0; i < sband->n_bitrates; i++) {
784 if (BIT(i) & rates) {
785 int rate = sband->bitrates[i].bitrate;
786 *pos++ = (u8) (rate / 5);
787 if (++count == 8)
788 break;
789 }
790 }
791
792 if (rates_len > count) {
793 pos = skb_put(skb, rates_len - count + 2);
794 *pos++ = WLAN_EID_EXT_SUPP_RATES;
795 *pos++ = rates_len - count;
796
797 for (i++; i < sband->n_bitrates; i++) {
798 if (BIT(i) & rates) {
799 int rate = sband->bitrates[i].bitrate;
800 *pos++ = (u8) (rate / 5);
801 }
802 }
803 }
804
805 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
806 /* 1. power capabilities */
807 pos = skb_put(skb, 4);
808 *pos++ = WLAN_EID_PWR_CAPABILITY;
809 *pos++ = 2;
810 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200811 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100812
813 /* 2. supported channels */
814 /* TODO: get this in reg domain format */
815 pos = skb_put(skb, 2 * sband->n_channels + 2);
816 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
817 *pos++ = 2 * sband->n_channels;
818 for (i = 0; i < sband->n_channels; i++) {
819 *pos++ = ieee80211_frequency_to_channel(
820 sband->channels[i].center_freq);
821 *pos++ = 1; /* one channel in the subband*/
822 }
823 }
824
825 /* if present, add any custom IEs that go before HT */
826 if (assoc_data->ie_len && assoc_data->ie) {
827 static const u8 before_ht[] = {
828 WLAN_EID_SSID,
829 WLAN_EID_SUPP_RATES,
830 WLAN_EID_EXT_SUPP_RATES,
831 WLAN_EID_PWR_CAPABILITY,
832 WLAN_EID_SUPPORTED_CHANNELS,
833 WLAN_EID_RSN,
834 WLAN_EID_QOS_CAPA,
835 WLAN_EID_RRM_ENABLED_CAPABILITIES,
836 WLAN_EID_MOBILITY_DOMAIN,
837 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
838 };
839 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
840 before_ht, ARRAY_SIZE(before_ht),
841 offset);
842 pos = skb_put(skb, noffset - offset);
843 memcpy(pos, assoc_data->ie + offset, noffset - offset);
844 offset = noffset;
845 }
846
Johannes Bergf2d9d272012-11-22 14:11:39 +0100847 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
848 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
849 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
850
Johannes Berga8243b72012-11-22 14:32:09 +0100851 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200852 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200853 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100854
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000855 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100856 ieee80211_add_vht_ie(sdata, skb, sband,
857 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000858
Johannes Berg66e67e42012-01-20 13:55:27 +0100859 /* if present, add any custom non-vendor IEs that go after HT */
860 if (assoc_data->ie_len && assoc_data->ie) {
861 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
862 assoc_data->ie_len,
863 offset);
864 pos = skb_put(skb, noffset - offset);
865 memcpy(pos, assoc_data->ie + offset, noffset - offset);
866 offset = noffset;
867 }
868
Johannes Berg76f03032012-03-08 15:02:05 +0100869 if (assoc_data->wmm) {
870 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200871 qos_info = ifmgd->uapsd_queues;
872 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100873 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
874 } else {
875 qos_info = 0;
876 }
877
878 pos = skb_put(skb, 9);
879 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
880 *pos++ = 7; /* len */
881 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
882 *pos++ = 0x50;
883 *pos++ = 0xf2;
884 *pos++ = 2; /* WME */
885 *pos++ = 0; /* WME info */
886 *pos++ = 1; /* WME ver */
887 *pos++ = qos_info;
888 }
889
890 /* add any remaining custom (i.e. vendor specific here) IEs */
891 if (assoc_data->ie_len && assoc_data->ie) {
892 noffset = assoc_data->ie_len;
893 pos = skb_put(skb, noffset - offset);
894 memcpy(pos, assoc_data->ie + offset, noffset - offset);
895 }
896
Johannes Berga1845fc2012-06-27 13:18:36 +0200897 drv_mgd_prepare_tx(local, sdata);
898
Johannes Berg66e67e42012-01-20 13:55:27 +0100899 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100900 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
901 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
902 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100903 ieee80211_tx_skb(sdata, skb);
904}
905
Kalle Valo572e0012009-02-10 17:09:31 +0200906void ieee80211_send_pspoll(struct ieee80211_local *local,
907 struct ieee80211_sub_if_data *sdata)
908{
Kalle Valo572e0012009-02-10 17:09:31 +0200909 struct ieee80211_pspoll *pspoll;
910 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200911
Kalle Valod8cd1892010-01-05 20:16:26 +0200912 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
913 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200914 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200915
Kalle Valod8cd1892010-01-05 20:16:26 +0200916 pspoll = (struct ieee80211_pspoll *) skb->data;
917 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200918
Johannes Berg62ae67b2009-11-18 18:42:05 +0100919 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
920 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200921}
922
Johannes Berg965beda2009-04-16 13:17:24 +0200923void ieee80211_send_nullfunc(struct ieee80211_local *local,
924 struct ieee80211_sub_if_data *sdata,
925 int powersave)
926{
927 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200928 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530929 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200930
Kalle Valod8cd1892010-01-05 20:16:26 +0200931 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
932 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200933 return;
934
Kalle Valod8cd1892010-01-05 20:16:26 +0200935 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200936 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200937 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200938
Seth Forshee6c17b772013-02-11 11:21:07 -0600939 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
940 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530941 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
942 IEEE80211_STA_CONNECTION_POLL))
943 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
944
Johannes Berg62ae67b2009-11-18 18:42:05 +0100945 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200946}
947
Felix Fietkaud5242152010-01-08 18:06:26 +0100948static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
949 struct ieee80211_sub_if_data *sdata)
950{
951 struct sk_buff *skb;
952 struct ieee80211_hdr *nullfunc;
953 __le16 fc;
954
955 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
956 return;
957
958 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700959 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100960 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700961
Felix Fietkaud5242152010-01-08 18:06:26 +0100962 skb_reserve(skb, local->hw.extra_tx_headroom);
963
964 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
965 memset(nullfunc, 0, 30);
966 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
967 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
968 nullfunc->frame_control = fc;
969 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
970 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
971 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
972 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
973
974 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
975 ieee80211_tx_skb(sdata, skb);
976}
977
Johannes Bergcc32abd2009-05-15 11:52:31 +0200978/* spectrum management related things */
979static void ieee80211_chswitch_work(struct work_struct *work)
980{
981 struct ieee80211_sub_if_data *sdata =
982 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200983 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
984
Johannes Berg9607e6b2009-12-23 13:15:31 +0100985 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200986 return;
987
Johannes Berg77fdaa12009-07-07 03:45:17 +0200988 mutex_lock(&ifmgd->mtx);
989 if (!ifmgd->associated)
990 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200991
Johannes Berg55de9082012-07-26 17:24:39 +0200992 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200993 if (!sdata->local->ops->channel_switch) {
994 /* call "hw_config" only if doing sw channel switch */
995 ieee80211_hw_config(sdata->local,
996 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300997 } else {
998 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200999 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001000 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001001
Johannes Berg77fdaa12009-07-07 03:45:17 +02001002 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +02001003 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001004
Johannes Berg57eebdf2012-08-01 15:50:46 +02001005 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001006 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1007 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001008 out:
1009 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1010 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001011}
1012
Johannes Berg5ce6e432010-05-11 16:20:57 +02001013void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1014{
Johannes Berg55de9082012-07-26 17:24:39 +02001015 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1016 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001017
1018 trace_api_chswitch_done(sdata, success);
1019 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001020 sdata_info(sdata,
1021 "driver channel switch failed, disconnecting\n");
1022 ieee80211_queue_work(&sdata->local->hw,
1023 &ifmgd->csa_connection_drop_work);
1024 } else {
1025 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001026 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001027}
1028EXPORT_SYMBOL(ieee80211_chswitch_done);
1029
Johannes Bergcc32abd2009-05-15 11:52:31 +02001030static void ieee80211_chswitch_timer(unsigned long data)
1031{
1032 struct ieee80211_sub_if_data *sdata =
1033 (struct ieee80211_sub_if_data *) data;
1034 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1035
Johannes Berg5bb644a2009-05-17 11:40:42 +02001036 if (sdata->local->quiescing) {
1037 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
1038 return;
1039 }
1040
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001041 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001042}
1043
Johannes Berg4a3cb702013-02-12 16:43:19 +01001044void
1045ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1046 const struct ieee80211_channel_sw_ie *sw_elem,
1047 struct ieee80211_bss *bss, u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001048{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001049 struct cfg80211_bss *cbss =
1050 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001051 struct ieee80211_channel *new_ch;
1052 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001053 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1054 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +02001055 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001056
Johannes Berg77fdaa12009-07-07 03:45:17 +02001057 ASSERT_MGD_MTX(ifmgd);
1058
1059 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001060 return;
1061
Helmut Schaafbe9c422009-07-23 12:14:04 +02001062 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001063 return;
1064
1065 /* Disregard subsequent beacons if we are already running a timer
1066 processing a CSA */
1067
1068 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1069 return;
1070
1071 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001072 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1073 sdata_info(sdata,
1074 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1075 ifmgd->associated->bssid, new_freq);
1076 ieee80211_queue_work(&sdata->local->hw,
1077 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001078 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001079 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001080
Johannes Berg57eebdf2012-08-01 15:50:46 +02001081 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1082
Johannes Berg55de9082012-07-26 17:24:39 +02001083 if (sdata->local->use_chanctx) {
1084 sdata_info(sdata,
1085 "not handling channel switch with channel contexts\n");
1086 ieee80211_queue_work(&sdata->local->hw,
1087 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001088 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001089 }
1090
1091 mutex_lock(&sdata->local->chanctx_mtx);
1092 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1093 mutex_unlock(&sdata->local->chanctx_mtx);
1094 return;
1095 }
1096 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1097 struct ieee80211_chanctx, conf);
1098 if (chanctx->refcount > 1) {
1099 sdata_info(sdata,
1100 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1101 ieee80211_queue_work(&sdata->local->hw,
1102 &ifmgd->csa_connection_drop_work);
1103 mutex_unlock(&sdata->local->chanctx_mtx);
1104 return;
1105 }
1106 mutex_unlock(&sdata->local->chanctx_mtx);
1107
1108 sdata->local->csa_channel = new_ch;
1109
Johannes Berg57eebdf2012-08-01 15:50:46 +02001110 if (sw_elem->mode)
1111 ieee80211_stop_queues_by_reason(&sdata->local->hw,
1112 IEEE80211_QUEUE_STOP_REASON_CSA);
1113
Johannes Berg5ce6e432010-05-11 16:20:57 +02001114 if (sdata->local->ops->channel_switch) {
1115 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001116 struct ieee80211_channel_switch ch_switch = {
1117 .timestamp = timestamp,
1118 .block_tx = sw_elem->mode,
1119 .channel = new_ch,
1120 .count = sw_elem->count,
1121 };
1122
Johannes Berg5ce6e432010-05-11 16:20:57 +02001123 drv_channel_switch(sdata->local, &ch_switch);
1124 return;
1125 }
1126
1127 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001128 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001129 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001130 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001131 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +02001132 TU_TO_EXP_TIME(sw_elem->count *
1133 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001134}
1135
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001136static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1137 struct ieee80211_channel *channel,
1138 const u8 *country_ie, u8 country_ie_len,
1139 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001140{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001141 struct ieee80211_country_ie_triplet *triplet;
1142 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1143 int i, chan_pwr, chan_increment, new_ap_level;
1144 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001145
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001146 /* Invalid IE */
1147 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001148 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001149
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001150 triplet = (void *)(country_ie + 3);
1151 country_ie_len -= 3;
1152
1153 switch (channel->band) {
1154 default:
1155 WARN_ON_ONCE(1);
1156 /* fall through */
1157 case IEEE80211_BAND_2GHZ:
1158 case IEEE80211_BAND_60GHZ:
1159 chan_increment = 1;
1160 break;
1161 case IEEE80211_BAND_5GHZ:
1162 chan_increment = 4;
1163 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001164 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001165
1166 /* find channel */
1167 while (country_ie_len >= 3) {
1168 u8 first_channel = triplet->chans.first_channel;
1169
1170 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1171 goto next;
1172
1173 for (i = 0; i < triplet->chans.num_channels; i++) {
1174 if (first_channel + i * chan_increment == chan) {
1175 have_chan_pwr = true;
1176 chan_pwr = triplet->chans.max_power;
1177 break;
1178 }
1179 }
1180 if (have_chan_pwr)
1181 break;
1182
1183 next:
1184 triplet++;
1185 country_ie_len -= 3;
1186 }
1187
1188 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001189 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001190
1191 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1192
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001193 if (sdata->ap_power_level == new_ap_level)
1194 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001195
1196 sdata_info(sdata,
1197 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1198 new_ap_level, chan_pwr, *pwr_constr_elem,
1199 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001200 sdata->ap_power_level = new_ap_level;
1201 if (__ieee80211_recalc_txpower(sdata))
1202 return BSS_CHANGED_TXPOWER;
1203 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001204}
1205
Johannes Berg965beda2009-04-16 13:17:24 +02001206/* powersave */
1207static void ieee80211_enable_ps(struct ieee80211_local *local,
1208 struct ieee80211_sub_if_data *sdata)
1209{
1210 struct ieee80211_conf *conf = &local->hw.conf;
1211
Johannes Bergd5edaed2009-04-22 23:02:51 +02001212 /*
1213 * If we are scanning right now then the parameters will
1214 * take effect when scan finishes.
1215 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001216 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001217 return;
1218
Johannes Berg965beda2009-04-16 13:17:24 +02001219 if (conf->dynamic_ps_timeout > 0 &&
1220 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1221 mod_timer(&local->dynamic_ps_timer, jiffies +
1222 msecs_to_jiffies(conf->dynamic_ps_timeout));
1223 } else {
1224 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1225 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301226
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001227 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1228 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1229 return;
1230
1231 conf->flags |= IEEE80211_CONF_PS;
1232 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001233 }
1234}
1235
1236static void ieee80211_change_ps(struct ieee80211_local *local)
1237{
1238 struct ieee80211_conf *conf = &local->hw.conf;
1239
1240 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001241 ieee80211_enable_ps(local, local->ps_sdata);
1242 } else if (conf->flags & IEEE80211_CONF_PS) {
1243 conf->flags &= ~IEEE80211_CONF_PS;
1244 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1245 del_timer_sync(&local->dynamic_ps_timer);
1246 cancel_work_sync(&local->dynamic_ps_enable_work);
1247 }
1248}
1249
Jason Young808118c2011-03-10 16:43:19 -08001250static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1251{
1252 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1253 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001254 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001255
1256 if (!mgd->powersave)
1257 return false;
1258
Johannes Berg05cb9102011-10-28 11:59:47 +02001259 if (mgd->broken_ap)
1260 return false;
1261
Jason Young808118c2011-03-10 16:43:19 -08001262 if (!mgd->associated)
1263 return false;
1264
Jason Young808118c2011-03-10 16:43:19 -08001265 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1266 IEEE80211_STA_CONNECTION_POLL))
1267 return false;
1268
1269 rcu_read_lock();
1270 sta = sta_info_get(sdata, mgd->bssid);
1271 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001272 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001273 rcu_read_unlock();
1274
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001275 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001276}
1277
Johannes Berg965beda2009-04-16 13:17:24 +02001278/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001279void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001280{
1281 struct ieee80211_sub_if_data *sdata, *found = NULL;
1282 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001283 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001284
1285 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1286 local->ps_sdata = NULL;
1287 return;
1288 }
1289
1290 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001291 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001292 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301293 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1294 /* If an AP vif is found, then disable PS
1295 * by setting the count to zero thereby setting
1296 * ps_sdata to NULL.
1297 */
1298 count = 0;
1299 break;
1300 }
Johannes Berg965beda2009-04-16 13:17:24 +02001301 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1302 continue;
1303 found = sdata;
1304 count++;
1305 }
1306
Jason Young808118c2011-03-10 16:43:19 -08001307 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001308 s32 beaconint_us;
1309
1310 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001311 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001312
1313 beaconint_us = ieee80211_tu_to_usec(
1314 found->vif.bss_conf.beacon_int);
1315
Juuso Oikarinenff616382010-06-09 09:51:52 +03001316 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001317 if (timeout < 0) {
1318 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001319 * Go to full PSM if the user configures a very low
1320 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001321 * The 2000 second value is there for compatibility
1322 * until the PM_QOS_NETWORK_LATENCY is configured
1323 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001324 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001325 if (latency > (1900 * USEC_PER_MSEC) &&
1326 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001327 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001328 else
1329 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001330 }
Johannes Berg09b85562013-02-06 23:07:41 +01001331 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001332
Johannes Berg04fe2032009-04-22 18:44:37 +02001333 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001334 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001335 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001336 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001337 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001338
1339 /* If the TIM IE is invalid, pretend the value is 1 */
1340 if (!dtimper)
1341 dtimper = 1;
1342 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001343 maxslp = min_t(int, dtimper,
1344 latency / beaconint_us);
1345
Johannes Berg9ccebe62009-04-23 10:32:36 +02001346 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001347 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001348 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001349 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001350 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001351 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001352 }
Johannes Berg965beda2009-04-16 13:17:24 +02001353
1354 ieee80211_change_ps(local);
1355}
1356
Eliad Pellerab095872012-07-27 12:33:22 +03001357void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1358{
1359 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1360
1361 if (sdata->vif.bss_conf.ps != ps_allowed) {
1362 sdata->vif.bss_conf.ps = ps_allowed;
1363 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1364 }
1365}
1366
Johannes Berg965beda2009-04-16 13:17:24 +02001367void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1368{
1369 struct ieee80211_local *local =
1370 container_of(work, struct ieee80211_local,
1371 dynamic_ps_disable_work);
1372
1373 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1374 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1375 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1376 }
1377
1378 ieee80211_wake_queues_by_reason(&local->hw,
1379 IEEE80211_QUEUE_STOP_REASON_PS);
1380}
1381
1382void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1383{
1384 struct ieee80211_local *local =
1385 container_of(work, struct ieee80211_local,
1386 dynamic_ps_enable_work);
1387 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001388 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301389 unsigned long flags;
1390 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001391
1392 /* can only happen when PS was just disabled anyway */
1393 if (!sdata)
1394 return;
1395
Christian Lamparter5e340692011-06-30 21:08:43 +02001396 ifmgd = &sdata->u.mgd;
1397
Johannes Berg965beda2009-04-16 13:17:24 +02001398 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1399 return;
1400
Johannes Berg09b85562013-02-06 23:07:41 +01001401 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001402 /* don't enter PS if TX frames are pending */
1403 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301404 mod_timer(&local->dynamic_ps_timer, jiffies +
1405 msecs_to_jiffies(
1406 local->hw.conf.dynamic_ps_timeout));
1407 return;
1408 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001409
1410 /*
1411 * transmission can be stopped by others which leads to
1412 * dynamic_ps_timer expiry. Postpone the ps timer if it
1413 * is not the actual idle state.
1414 */
1415 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1416 for (q = 0; q < local->hw.queues; q++) {
1417 if (local->queue_stop_reasons[q]) {
1418 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1419 flags);
1420 mod_timer(&local->dynamic_ps_timer, jiffies +
1421 msecs_to_jiffies(
1422 local->hw.conf.dynamic_ps_timeout));
1423 return;
1424 }
1425 }
1426 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301427 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301428
Vivek Natarajan375177b2010-02-09 14:50:28 +05301429 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301430 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301431 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001432
Vivek Natarajane8306f92011-04-06 11:41:10 +05301433 if (drv_tx_frames_pending(local))
1434 mod_timer(&local->dynamic_ps_timer, jiffies +
1435 msecs_to_jiffies(
1436 local->hw.conf.dynamic_ps_timeout));
1437 else {
1438 ieee80211_send_nullfunc(local, sdata, 1);
1439 /* Flush to get the tx status of nullfunc frame */
1440 drv_flush(local, false);
1441 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301442 }
1443
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001444 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1445 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301446 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1447 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1448 local->hw.conf.flags |= IEEE80211_CONF_PS;
1449 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1450 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301451
Arik Nemtsov77b70232011-06-26 12:06:54 +03001452 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1453 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001454}
1455
1456void ieee80211_dynamic_ps_timer(unsigned long data)
1457{
1458 struct ieee80211_local *local = (void *) data;
1459
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001460 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001461 return;
1462
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001463 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001464}
1465
Simon Wunderlich164eb022013-02-08 18:16:20 +01001466void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1467{
1468 struct delayed_work *delayed_work =
1469 container_of(work, struct delayed_work, work);
1470 struct ieee80211_sub_if_data *sdata =
1471 container_of(delayed_work, struct ieee80211_sub_if_data,
1472 dfs_cac_timer_work);
1473
1474 ieee80211_vif_release_channel(sdata);
1475
1476 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1477}
1478
Johannes Berg60f8b392008-09-08 17:44:22 +02001479/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001480static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001481 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001482 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001483{
Jiri Bencf0706e82007-05-05 11:45:53 -07001484 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001485 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001486 size_t left;
1487 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001488 const u8 *pos;
1489 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001490
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001491 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001492 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001493
Johannes Berg32c50572012-03-28 11:04:29 +02001494 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001495 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001496
1497 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001498 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001499
Jiri Bencf0706e82007-05-05 11:45:53 -07001500 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001501 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001502
1503 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001504 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001505
Jiri Bencf0706e82007-05-05 11:45:53 -07001506 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001507 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001508 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001509 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001510
1511 pos = wmm_param + 8;
1512 left = wmm_param_len - 8;
1513
1514 memset(&params, 0, sizeof(params));
1515
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001516 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001517 for (; left >= 4; left -= 4, pos += 4) {
1518 int aci = (pos[0] >> 5) & 0x03;
1519 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001520 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001521 int queue;
1522
1523 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001524 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001525 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001526 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001527 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001528 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1529 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001530 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001531 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001532 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001533 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001534 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001535 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1536 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001537 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001538 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001539 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001540 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001541 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001542 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1543 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001544 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001545 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001546 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001547 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001548 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001549 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001550 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1551 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001552 break;
1553 }
1554
1555 params.aifs = pos[0] & 0x0f;
1556 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1557 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001558 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001559 params.uapsd = uapsd;
1560
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001561 mlme_dbg(sdata,
1562 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1563 queue, aci, acm,
1564 params.aifs, params.cw_min, params.cw_max,
1565 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001566 sdata->tx_conf[queue] = params;
1567 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001568 sdata_err(sdata,
1569 "failed to set TX queue parameters for queue %d\n",
1570 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001571 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001572
1573 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001574 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001575 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001576}
1577
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001578static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1579{
1580 lockdep_assert_held(&sdata->local->mtx);
1581
1582 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1583 IEEE80211_STA_BEACON_POLL);
1584 ieee80211_run_deferred_scan(sdata->local);
1585}
1586
1587static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1588{
1589 mutex_lock(&sdata->local->mtx);
1590 __ieee80211_stop_poll(sdata);
1591 mutex_unlock(&sdata->local->mtx);
1592}
1593
Johannes Berg7a5158e2008-10-08 10:59:33 +02001594static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1595 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001596{
Johannes Bergbda39332008-10-11 01:51:51 +02001597 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001598 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001599 bool use_protection;
1600 bool use_short_preamble;
1601 bool use_short_slot;
1602
1603 if (erp_valid) {
1604 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1605 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1606 } else {
1607 use_protection = false;
1608 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1609 }
1610
1611 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001612 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001613 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001614
Johannes Berg471b3ef2007-12-28 14:32:58 +01001615 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001616 bss_conf->use_cts_prot = use_protection;
1617 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001618 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001619
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001620 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001621 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001622 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001623 }
Daniel Draked9430a32007-07-27 15:43:24 +02001624
Johannes Berg7a5158e2008-10-08 10:59:33 +02001625 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001626 bss_conf->use_short_slot = use_short_slot;
1627 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001628 }
1629
1630 return changed;
1631}
1632
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001633static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001634 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001635 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001636{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001637 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001638 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001639 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001640
Johannes Bergae5eb022008-10-14 16:58:37 +02001641 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001642 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001643 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001644
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001645 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1646 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1647
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001648 sdata->u.mgd.associated = cbss;
1649 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001650
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001651 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1652
Johannes Berg488dd7b2012-10-29 20:08:01 +01001653 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001654 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001655
Johannes Berg9caf0362012-11-29 01:25:20 +01001656 rcu_read_lock();
1657 ies = rcu_dereference(cbss->ies);
1658 if (ies) {
1659 u8 noa[2];
1660 int ret;
1661
1662 ret = cfg80211_get_p2p_attr(
1663 ies->data, ies->len,
1664 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1665 noa, sizeof(noa));
1666 if (ret >= 2) {
1667 bss_conf->p2p_oppps = noa[1] & 0x80;
1668 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1669 bss_info_changed |= BSS_CHANGED_P2P_PS;
1670 sdata->u.mgd.p2p_noa_index = noa[0];
1671 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001672 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001673 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001674 }
1675
Johannes Bergb291ba12009-07-10 15:29:03 +02001676 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001677 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001678
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001679 ieee80211_led_assoc(local, 1);
1680
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001681 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001682 /*
1683 * If the AP is buggy we may get here with no DTIM period
1684 * known, so assume it's 1 which is the only safe assumption
1685 * in that case, although if the TIM IE is broken powersave
1686 * probably just won't work at all.
1687 */
1688 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001689 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001690 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001691 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001692 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001693
Juuso Oikarinen68542962010-06-09 13:43:26 +03001694 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001695
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001696 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001697 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001698 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001699 bss_info_changed |= BSS_CHANGED_CQM;
1700
Juuso Oikarinen68542962010-06-09 13:43:26 +03001701 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001702 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001703 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001704
Johannes Bergae5eb022008-10-14 16:58:37 +02001705 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001706
Johannes Berg056508d2009-07-30 21:43:55 +02001707 mutex_lock(&local->iflist_mtx);
1708 ieee80211_recalc_ps(local, -1);
1709 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001710
Johannes Berg04ecd252012-09-11 14:34:12 +02001711 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001712 ieee80211_recalc_ps_vif(sdata);
1713
John W. Linville8a5b33f2010-01-06 15:39:39 -05001714 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001715 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001716}
1717
Jouni Malinene69e95d2010-03-29 23:29:31 -07001718static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001719 u16 stype, u16 reason, bool tx,
1720 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001721{
Johannes Berg46900292009-02-15 12:44:28 +01001722 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001723 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001724 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001725
Johannes Berg77fdaa12009-07-07 03:45:17 +02001726 ASSERT_MGD_MTX(ifmgd);
1727
Johannes Berg37ad3882012-02-24 13:50:54 +01001728 if (WARN_ON_ONCE(tx && !frame_buf))
1729 return;
1730
Johannes Bergb291ba12009-07-10 15:29:03 +02001731 if (WARN_ON(!ifmgd->associated))
1732 return;
1733
David Spinadel79543d82012-06-12 09:59:45 +03001734 ieee80211_stop_poll(sdata);
1735
Johannes Berg77fdaa12009-07-07 03:45:17 +02001736 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001737
1738 /*
1739 * we need to commit the associated = NULL change because the
1740 * scan code uses that to determine whether this iface should
1741 * go to/wake up from powersave or not -- and could otherwise
1742 * wake the queues erroneously.
1743 */
1744 smp_mb();
1745
1746 /*
1747 * Thus, we can only afterwards stop the queues -- to account
1748 * for the case where another CPU is finishing a scan at this
1749 * time -- we don't want the scan code to enable queues.
1750 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001751
John W. Linville8a5b33f2010-01-06 15:39:39 -05001752 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001753 netif_carrier_off(sdata->dev);
1754
Eliad Peller88bc40e2012-07-12 17:35:33 +03001755 /*
1756 * if we want to get out of ps before disassoc (why?) we have
1757 * to do it before sending disassoc, as otherwise the null-packet
1758 * won't be valid.
1759 */
1760 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1761 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1762 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1763 }
1764 local->ps_sdata = NULL;
1765
Eliad Pellerab095872012-07-27 12:33:22 +03001766 /* disable per-vif ps */
1767 ieee80211_recalc_ps_vif(sdata);
1768
Eliad Pellerf8239812012-06-27 14:18:22 +03001769 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1770 if (tx)
1771 drv_flush(local, false);
1772
Johannes Berg37ad3882012-02-24 13:50:54 +01001773 /* deauthenticate/disassociate now */
1774 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001775 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1776 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001777
1778 /* flush out frame */
1779 if (tx)
1780 drv_flush(local, false);
1781
Eliad Peller88a9e312012-06-01 11:14:03 +03001782 /* clear bssid only after building the needed mgmt frames */
1783 memset(ifmgd->bssid, 0, ETH_ALEN);
1784
Johannes Berg37ad3882012-02-24 13:50:54 +01001785 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001786 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001787
1788 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001789 changed |= ieee80211_reset_erp_info(sdata);
1790
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001791 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001792 changed |= BSS_CHANGED_ASSOC;
1793 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001794
Johannes Berg488dd7b2012-10-29 20:08:01 +01001795 sdata->vif.bss_conf.p2p_ctwindow = 0;
1796 sdata->vif.bss_conf.p2p_oppps = false;
1797
Johannes Berg413ad502009-05-08 21:21:06 +02001798 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001799 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1800 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001801
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001802 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301803
Kalle Valo520eb822008-12-18 23:35:27 +02001804 del_timer_sync(&local->dynamic_ps_timer);
1805 cancel_work_sync(&local->dynamic_ps_enable_work);
1806
Juuso Oikarinen68542962010-06-09 13:43:26 +03001807 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001808 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001809 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001810
Johannes Berg3abead52012-03-02 15:56:59 +01001811 sdata->vif.bss_conf.qos = false;
1812 changed |= BSS_CHANGED_QOS;
1813
Johannes Berg0aaffa92010-05-05 15:28:27 +02001814 /* The BSSID (not really interesting) and HT changed */
1815 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001816 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001817
Johannes Berg3abead52012-03-02 15:56:59 +01001818 /* disassociated - set to defaults now */
1819 ieee80211_set_wmm_default(sdata, false);
1820
Johannes Bergb9dcf712010-08-27 12:35:54 +02001821 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1822 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1823 del_timer_sync(&sdata->u.mgd.timer);
1824 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001825
1826 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001827
Johannes Berg826262c2012-12-10 16:38:14 +02001828 sdata->vif.bss_conf.dtim_period = 0;
1829
Johannes Berg028e8da02012-11-26 11:57:41 +01001830 ifmgd->flags = 0;
1831 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001832}
Jiri Bencf0706e82007-05-05 11:45:53 -07001833
Kalle Valo3cf335d2009-03-22 21:57:06 +02001834void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1835 struct ieee80211_hdr *hdr)
1836{
1837 /*
1838 * We can postpone the mgd.timer whenever receiving unicast frames
1839 * from AP because we know that the connection is working both ways
1840 * at that time. But multicast frames (and hence also beacons) must
1841 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001842 * data idle periods for sending the periodic probe request to the
1843 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001844 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001845 if (is_multicast_ether_addr(hdr->addr1))
1846 return;
1847
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001848 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001849}
Jiri Bencf0706e82007-05-05 11:45:53 -07001850
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001851static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1852{
1853 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001854 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001855
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001856 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001857 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001858 IEEE80211_STA_CONNECTION_POLL))) {
1859 mutex_unlock(&local->mtx);
1860 return;
1861 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001862
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001863 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001864
1865 mutex_lock(&local->iflist_mtx);
1866 ieee80211_recalc_ps(local, -1);
1867 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001868
1869 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001870 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001871
1872 /*
1873 * We've received a probe response, but are not sure whether
1874 * we have or will be receiving any beacons or data, so let's
1875 * schedule the timers again, just in case.
1876 */
1877 ieee80211_sta_reset_beacon_monitor(sdata);
1878
1879 mod_timer(&ifmgd->conn_mon_timer,
1880 round_jiffies_up(jiffies +
1881 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001882out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001883 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001884}
1885
1886void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001887 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001888{
Felix Fietkau75706d02010-12-02 21:01:07 +01001889 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001890 return;
1891
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001892 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1893 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001894 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001895 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001896 else
1897 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001898 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001899 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001900 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001901
1902 if (ack)
1903 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001904}
1905
Maxim Levitskya43abf22009-07-31 18:54:12 +03001906static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1907{
1908 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1909 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001910 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001911 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001912
1913 /*
1914 * Try sending broadcast probe requests for the last three
1915 * probe requests after the first ones failed since some
1916 * buggy APs only support broadcast probe requests.
1917 */
1918 if (ifmgd->probe_send_count >= unicast_limit)
1919 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001920
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001921 /*
1922 * When the hardware reports an accurate Tx ACK status, it's
1923 * better to send a nullfunc frame instead of a probe request,
1924 * as it will kick us off the AP quickly if we aren't associated
1925 * anymore. The timeout will be reset if the frame is ACKed by
1926 * the AP.
1927 */
Soumik Das992e68b2012-05-20 15:31:13 +05301928 ifmgd->probe_send_count++;
1929
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001930 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1931 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001932 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001933 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001934 int ssid_len;
1935
Johannes Berg9caf0362012-11-29 01:25:20 +01001936 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001937 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001938 if (WARN_ON_ONCE(ssid == NULL))
1939 ssid_len = 0;
1940 else
1941 ssid_len = ssid[1];
1942
1943 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001944 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001945 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001946 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001947 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001948
Ben Greear180205b2011-02-04 15:30:24 -08001949 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001950 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301951 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1952 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001953}
1954
Johannes Bergb291ba12009-07-10 15:29:03 +02001955static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1956 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001957{
Kalle Valo04de8382009-03-22 21:57:35 +02001958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001959 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001960
Johannes Berg9607e6b2009-12-23 13:15:31 +01001961 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001962 return;
1963
Johannes Berg77fdaa12009-07-07 03:45:17 +02001964 mutex_lock(&ifmgd->mtx);
1965
1966 if (!ifmgd->associated)
1967 goto out;
1968
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001969 mutex_lock(&sdata->local->mtx);
1970
1971 if (sdata->local->tmp_channel || sdata->local->scanning) {
1972 mutex_unlock(&sdata->local->mtx);
1973 goto out;
1974 }
1975
Joe Perchese87cc472012-05-13 21:56:26 +00001976 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001977 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001978 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001979
Holger Schurig6efb71b2012-05-15 15:38:59 +02001980 ieee80211_cqm_rssi_notify(&sdata->vif,
1981 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001982
Johannes Bergb291ba12009-07-10 15:29:03 +02001983 /*
1984 * The driver/our work has already reported this event or the
1985 * connection monitoring has kicked in and we have already sent
1986 * a probe request. Or maybe the AP died and the driver keeps
1987 * reporting until we disassociate...
1988 *
1989 * In either case we have to ignore the current call to this
1990 * function (except for setting the correct probe reason bit)
1991 * because otherwise we would reset the timer every time and
1992 * never check whether we received a probe response!
1993 */
1994 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1995 IEEE80211_STA_CONNECTION_POLL))
1996 already = true;
1997
1998 if (beacon)
1999 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2000 else
2001 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2002
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002003 mutex_unlock(&sdata->local->mtx);
2004
Johannes Bergb291ba12009-07-10 15:29:03 +02002005 if (already)
2006 goto out;
2007
Johannes Berg4e751842009-06-10 15:16:52 +02002008 mutex_lock(&sdata->local->iflist_mtx);
2009 ieee80211_recalc_ps(sdata->local, -1);
2010 mutex_unlock(&sdata->local->iflist_mtx);
2011
Maxim Levitskya43abf22009-07-31 18:54:12 +03002012 ifmgd->probe_send_count = 0;
2013 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002014 out:
2015 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002016}
2017
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002018struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2019 struct ieee80211_vif *vif)
2020{
2021 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2022 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002023 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002024 struct sk_buff *skb;
2025 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002026 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002027
2028 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2029 return NULL;
2030
2031 ASSERT_MGD_MTX(ifmgd);
2032
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002033 if (ifmgd->associated)
2034 cbss = ifmgd->associated;
2035 else if (ifmgd->auth_data)
2036 cbss = ifmgd->auth_data->bss;
2037 else if (ifmgd->assoc_data)
2038 cbss = ifmgd->assoc_data->bss;
2039 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002040 return NULL;
2041
Johannes Berg9caf0362012-11-29 01:25:20 +01002042 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002043 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002044 if (WARN_ON_ONCE(ssid == NULL))
2045 ssid_len = 0;
2046 else
2047 ssid_len = ssid[1];
2048
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002049 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002050 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002051 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002052 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002053 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002054
2055 return skb;
2056}
2057EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2058
Johannes Bergeef9e542013-01-29 13:09:34 +01002059static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002060{
2061 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002062 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002063
2064 mutex_lock(&ifmgd->mtx);
2065 if (!ifmgd->associated) {
2066 mutex_unlock(&ifmgd->mtx);
2067 return;
2068 }
2069
Johannes Berg37ad3882012-02-24 13:50:54 +01002070 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2071 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002072 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002073 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002074 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2075 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002076 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002077
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002078 /*
2079 * must be outside lock due to cfg80211,
2080 * but that's not a problem.
2081 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002082 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002083}
2084
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002085static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002086{
2087 struct ieee80211_sub_if_data *sdata =
2088 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002089 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002090 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2091 struct sta_info *sta;
2092
2093 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302094 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002095 sta = sta_info_get(sdata, ifmgd->bssid);
2096 if (sta)
2097 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302098 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002099 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002100
Johannes Berg682bd382013-01-29 13:13:50 +01002101 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002102 sdata_info(sdata, "Connection to AP %pM lost\n",
2103 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002104 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002105 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002106 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002107 }
2108}
2109
2110static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2111{
2112 struct ieee80211_sub_if_data *sdata =
2113 container_of(work, struct ieee80211_sub_if_data,
2114 u.mgd.csa_connection_drop_work);
2115
Johannes Bergeef9e542013-01-29 13:09:34 +01002116 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002117}
2118
Kalle Valo04de8382009-03-22 21:57:35 +02002119void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2120{
2121 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002122 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002123
Johannes Bergb5878a22010-04-07 16:48:40 +02002124 trace_api_beacon_loss(sdata);
2125
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002126 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002127 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002128 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002129}
2130EXPORT_SYMBOL(ieee80211_beacon_loss);
2131
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002132void ieee80211_connection_loss(struct ieee80211_vif *vif)
2133{
2134 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2135 struct ieee80211_hw *hw = &sdata->local->hw;
2136
Johannes Bergb5878a22010-04-07 16:48:40 +02002137 trace_api_connection_loss(sdata);
2138
Johannes Berg682bd382013-01-29 13:13:50 +01002139 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002140 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2141}
2142EXPORT_SYMBOL(ieee80211_connection_loss);
2143
2144
Johannes Berg66e67e42012-01-20 13:55:27 +01002145static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2146 bool assoc)
2147{
2148 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2149
2150 lockdep_assert_held(&sdata->u.mgd.mtx);
2151
Johannes Berg66e67e42012-01-20 13:55:27 +01002152 if (!assoc) {
2153 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2154
2155 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2156 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002157 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002158 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002159 }
2160
Johannes Berg5b112d32013-02-01 01:49:58 +01002161 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002162 kfree(auth_data);
2163 sdata->u.mgd.auth_data = NULL;
2164}
2165
2166static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2167 struct ieee80211_mgmt *mgmt, size_t len)
2168{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002169 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002170 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2171 u8 *pos;
2172 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002173 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002174
2175 pos = mgmt->u.auth.variable;
2176 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2177 if (!elems.challenge)
2178 return;
2179 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002180 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002181 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2182 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2183 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002184 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002185 elems.challenge - 2, elems.challenge_len + 2,
2186 auth_data->bss->bssid, auth_data->bss->bssid,
2187 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002188 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002189}
2190
2191static enum rx_mgmt_action __must_check
2192ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2193 struct ieee80211_mgmt *mgmt, size_t len)
2194{
2195 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2196 u8 bssid[ETH_ALEN];
2197 u16 auth_alg, auth_transaction, status_code;
2198 struct sta_info *sta;
2199
2200 lockdep_assert_held(&ifmgd->mtx);
2201
2202 if (len < 24 + 6)
2203 return RX_MGMT_NONE;
2204
2205 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2206 return RX_MGMT_NONE;
2207
2208 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2209
Joe Perchesb203ca32012-05-08 18:56:52 +00002210 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002211 return RX_MGMT_NONE;
2212
2213 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2214 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2215 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2216
2217 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002218 auth_transaction != ifmgd->auth_data->expected_transaction) {
2219 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2220 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2221 auth_transaction,
2222 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002223 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002224 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002225
2226 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002227 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2228 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002229 ieee80211_destroy_auth_data(sdata, false);
2230 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002231 }
2232
2233 switch (ifmgd->auth_data->algorithm) {
2234 case WLAN_AUTH_OPEN:
2235 case WLAN_AUTH_LEAP:
2236 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002237 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002238 break;
2239 case WLAN_AUTH_SHARED_KEY:
2240 if (ifmgd->auth_data->expected_transaction != 4) {
2241 ieee80211_auth_challenge(sdata, mgmt, len);
2242 /* need another frame */
2243 return RX_MGMT_NONE;
2244 }
2245 break;
2246 default:
2247 WARN_ONCE(1, "invalid auth alg %d",
2248 ifmgd->auth_data->algorithm);
2249 return RX_MGMT_NONE;
2250 }
2251
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002252 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002253 ifmgd->auth_data->done = true;
2254 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002255 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002256 run_again(ifmgd, ifmgd->auth_data->timeout);
2257
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002258 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2259 ifmgd->auth_data->expected_transaction != 2) {
2260 /*
2261 * Report auth frame to user space for processing since another
2262 * round of Authentication frames is still needed.
2263 */
2264 return RX_MGMT_CFG80211_RX_AUTH;
2265 }
2266
Johannes Berg66e67e42012-01-20 13:55:27 +01002267 /* move station state to auth */
2268 mutex_lock(&sdata->local->sta_mtx);
2269 sta = sta_info_get(sdata, bssid);
2270 if (!sta) {
2271 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2272 goto out_err;
2273 }
2274 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002275 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002276 goto out_err;
2277 }
2278 mutex_unlock(&sdata->local->sta_mtx);
2279
2280 return RX_MGMT_CFG80211_RX_AUTH;
2281 out_err:
2282 mutex_unlock(&sdata->local->sta_mtx);
2283 /* ignore frame -- wait for timeout */
2284 return RX_MGMT_NONE;
2285}
2286
2287
Johannes Berg77fdaa12009-07-07 03:45:17 +02002288static enum rx_mgmt_action __must_check
2289ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002290 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002291{
Johannes Berg46900292009-02-15 12:44:28 +01002292 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002293 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002294 u16 reason_code;
2295
Johannes Berg66e67e42012-01-20 13:55:27 +01002296 lockdep_assert_held(&ifmgd->mtx);
2297
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002298 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002299 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002300
Johannes Berg66e67e42012-01-20 13:55:27 +01002301 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002302 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002303 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002304
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002305 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002306
2307 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2308
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002309 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2310 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002311
Johannes Berg37ad3882012-02-24 13:50:54 +01002312 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2313
Johannes Berg77fdaa12009-07-07 03:45:17 +02002314 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002315}
2316
2317
Johannes Berg77fdaa12009-07-07 03:45:17 +02002318static enum rx_mgmt_action __must_check
2319ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2320 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002321{
Johannes Berg46900292009-02-15 12:44:28 +01002322 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002323 u16 reason_code;
2324
Johannes Berg66e67e42012-01-20 13:55:27 +01002325 lockdep_assert_held(&ifmgd->mtx);
2326
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002327 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002328 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002329
Johannes Berg66e67e42012-01-20 13:55:27 +01002330 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002331 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002332 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002333
2334 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2335
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002336 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2337 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002338
Johannes Berg37ad3882012-02-24 13:50:54 +01002339 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2340
Johannes Berg77fdaa12009-07-07 03:45:17 +02002341 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002342}
2343
Christian Lamparterc74d0842011-10-15 00:14:49 +02002344static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2345 u8 *supp_rates, unsigned int supp_rates_len,
2346 u32 *rates, u32 *basic_rates,
2347 bool *have_higher_than_11mbit,
2348 int *min_rate, int *min_rate_index)
2349{
2350 int i, j;
2351
2352 for (i = 0; i < supp_rates_len; i++) {
2353 int rate = (supp_rates[i] & 0x7f) * 5;
2354 bool is_basic = !!(supp_rates[i] & 0x80);
2355
2356 if (rate > 110)
2357 *have_higher_than_11mbit = true;
2358
2359 /*
2360 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2361 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2362 *
2363 * Note: Even through the membership selector and the basic
2364 * rate flag share the same bit, they are not exactly
2365 * the same.
2366 */
2367 if (!!(supp_rates[i] & 0x80) &&
2368 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2369 continue;
2370
2371 for (j = 0; j < sband->n_bitrates; j++) {
2372 if (sband->bitrates[j].bitrate == rate) {
2373 *rates |= BIT(j);
2374 if (is_basic)
2375 *basic_rates |= BIT(j);
2376 if (rate < *min_rate) {
2377 *min_rate = rate;
2378 *min_rate_index = j;
2379 }
2380 break;
2381 }
2382 }
2383 }
2384}
Jiri Bencf0706e82007-05-05 11:45:53 -07002385
Johannes Berg66e67e42012-01-20 13:55:27 +01002386static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2387 bool assoc)
2388{
2389 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2390
2391 lockdep_assert_held(&sdata->u.mgd.mtx);
2392
Johannes Berg66e67e42012-01-20 13:55:27 +01002393 if (!assoc) {
2394 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2395
2396 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2397 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002398 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002399 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002400 }
2401
2402 kfree(assoc_data);
2403 sdata->u.mgd.assoc_data = NULL;
2404}
2405
2406static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2407 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002408 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002409{
Johannes Berg46900292009-02-15 12:44:28 +01002410 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002411 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002412 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002413 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002414 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002415 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002416 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002417 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002418 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002419 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002420
Johannes Bergaf6b6372009-12-23 13:15:35 +01002421 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002422
Jiri Bencf0706e82007-05-05 11:45:53 -07002423 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002424 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002425
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002426 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002427 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2428 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002429 aid &= ~(BIT(15) | BIT(14));
2430
Johannes Berg05cb9102011-10-28 11:59:47 +02002431 ifmgd->broken_ap = false;
2432
2433 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002434 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2435 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002436 aid = 0;
2437 ifmgd->broken_ap = true;
2438 }
2439
Johannes Bergaf6b6372009-12-23 13:15:35 +01002440 pos = mgmt->u.assoc_resp.variable;
2441 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2442
Jiri Bencf0706e82007-05-05 11:45:53 -07002443 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002444 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002445 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002446 }
2447
Johannes Berg46900292009-02-15 12:44:28 +01002448 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002449
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002450 /*
2451 * We previously checked these in the beacon/probe response, so
2452 * they should be present here. This is just a safety net.
2453 */
2454 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2455 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2456 sdata_info(sdata,
2457 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2458 return false;
2459 }
2460
2461 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2462 (!elems.vht_cap_elem || !elems.vht_operation)) {
2463 sdata_info(sdata,
2464 "VHT AP is missing VHT capability/operation in AssocResp\n");
2465 return false;
2466 }
2467
Guy Eilam2a33bee2011-08-17 15:18:15 +03002468 mutex_lock(&sdata->local->sta_mtx);
2469 /*
2470 * station info was already allocated and inserted before
2471 * the association and should be available to us
2472 */
Johannes Berg7852e362012-01-20 13:55:24 +01002473 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002474 if (WARN_ON(!sta)) {
2475 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002476 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002477 }
2478
Johannes Berg55de9082012-07-26 17:24:39 +02002479 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002480
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002481 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002482 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002483 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002484 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002485
Mahesh Palivela818255e2012-10-10 11:33:04 +00002486 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2487 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002488 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002489
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002490 /*
2491 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2492 * in their association response, so ignore that data for our own
2493 * configuration. If it changed since the last beacon, we'll get the
2494 * next beacon and update then.
2495 */
Johannes Berg11289582013-02-07 17:36:12 +01002496
Johannes Bergbee7f582013-02-07 22:24:55 +01002497 /*
2498 * If an operating mode notification IE is present, override the
2499 * NSS calculation (that would be done in rate_control_rate_init())
2500 * and use the # of streams from that element.
2501 */
2502 if (elems.opmode_notif &&
2503 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2504 u8 nss;
2505
2506 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2507 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2508 nss += 1;
2509 sta->sta.rx_nss = nss;
2510 }
2511
Johannes Berg4b7679a2008-09-18 18:14:18 +02002512 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002513
Johannes Berg46900292009-02-15 12:44:28 +01002514 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002515 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002516
Johannes Bergddf4ac52008-10-22 11:41:38 +02002517 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002518 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002519
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002520 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002521 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2522 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2523 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002524 sdata_info(sdata,
2525 "failed to move station %pM to desired state\n",
2526 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002527 WARN_ON(__sta_info_destroy(sta));
2528 mutex_unlock(&sdata->local->sta_mtx);
2529 return false;
2530 }
2531
Johannes Berg7852e362012-01-20 13:55:24 +01002532 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002533
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002534 /*
2535 * Always handle WMM once after association regardless
2536 * of the first value the AP uses. Setting -1 here has
2537 * that effect because the AP values is an unsigned
2538 * 4-bit value.
2539 */
2540 ifmgd->wmm_last_param_set = -1;
2541
Johannes Bergddf4ac52008-10-22 11:41:38 +02002542 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002543 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002544 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002545 else
Johannes Berg3abead52012-03-02 15:56:59 +01002546 ieee80211_set_wmm_default(sdata, false);
2547 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002548
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002549 /* set AID and assoc capability,
2550 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002551 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002552 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002553 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002554
Kalle Valo15b7b062009-03-22 21:57:14 +02002555 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002556 * If we're using 4-addr mode, let the AP know that we're
2557 * doing so, so that it can create the STA VLAN on its side
2558 */
2559 if (ifmgd->use_4addr)
2560 ieee80211_send_4addr_nullfunc(local, sdata);
2561
2562 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002563 * Start timer to probe the connection to the AP now.
2564 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002565 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002566 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002567 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002568
Johannes Bergaf6b6372009-12-23 13:15:35 +01002569 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002570}
2571
Johannes Berg66e67e42012-01-20 13:55:27 +01002572static enum rx_mgmt_action __must_check
2573ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2574 struct ieee80211_mgmt *mgmt, size_t len,
2575 struct cfg80211_bss **bss)
2576{
2577 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2578 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2579 u16 capab_info, status_code, aid;
2580 struct ieee802_11_elems elems;
2581 u8 *pos;
2582 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002583
Johannes Berg66e67e42012-01-20 13:55:27 +01002584 lockdep_assert_held(&ifmgd->mtx);
2585
2586 if (!assoc_data)
2587 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002588 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002589 return RX_MGMT_NONE;
2590
2591 /*
2592 * AssocResp and ReassocResp have identical structure, so process both
2593 * of them in this function.
2594 */
2595
2596 if (len < 24 + 6)
2597 return RX_MGMT_NONE;
2598
2599 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2600 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2601 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2602 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2603
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002604 sdata_info(sdata,
2605 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2606 reassoc ? "Rea" : "A", mgmt->sa,
2607 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002608
2609 pos = mgmt->u.assoc_resp.variable;
2610 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2611
2612 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2613 elems.timeout_int && elems.timeout_int_len == 5 &&
2614 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2615 u32 tu, ms;
2616 tu = get_unaligned_le32(elems.timeout_int + 1);
2617 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002618 sdata_info(sdata,
2619 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2620 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002621 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002622 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002623 if (ms > IEEE80211_ASSOC_TIMEOUT)
2624 run_again(ifmgd, assoc_data->timeout);
2625 return RX_MGMT_NONE;
2626 }
2627
2628 *bss = assoc_data->bss;
2629
2630 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002631 sdata_info(sdata, "%pM denied association (code=%d)\n",
2632 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002633 ieee80211_destroy_assoc_data(sdata, false);
2634 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002635 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2636 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002637 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002638 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002639 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2640 }
John W. Linville635d9992012-07-09 16:34:34 -04002641 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002642
2643 /*
2644 * destroy assoc_data afterwards, as otherwise an idle
2645 * recalc after assoc_data is NULL but before associated
2646 * is set can cause the interface to go idle
2647 */
2648 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002649 }
2650
2651 return RX_MGMT_CFG80211_RX_ASSOC;
2652}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002653
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002654static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002655 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002656 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002657 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002658{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002659 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002660 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002661 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002662 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002663 bool need_ps = false;
2664
Johannes Berg826262c2012-12-10 16:38:14 +02002665 if ((sdata->u.mgd.associated &&
2666 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2667 (sdata->u.mgd.assoc_data &&
2668 ether_addr_equal(mgmt->bssid,
2669 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002670 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002671 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2672
2673 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002674 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002675 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2676 }
Johannes Berg56007a02010-01-26 14:19:52 +01002677 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002678
Johannes Berg5bda6172008-09-08 11:05:10 +02002679 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002680 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2681 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002682 else
2683 freq = rx_status->freq;
2684
2685 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2686
2687 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2688 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002689
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002690 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002691 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002692 if (bss)
2693 ieee80211_rx_bss_put(local, bss);
2694
2695 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002696 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002697
Johannes Berg56007a02010-01-26 14:19:52 +01002698 if (need_ps) {
2699 mutex_lock(&local->iflist_mtx);
2700 ieee80211_recalc_ps(local, -1);
2701 mutex_unlock(&local->iflist_mtx);
2702 }
2703
Johannes Berg5bc14202012-08-01 16:13:02 +02002704 if (elems->ch_switch_ie &&
2705 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2706 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002707 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002708}
2709
2710
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002711static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002712 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002713{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002714 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002715 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002716 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2717 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002718 struct ieee802_11_elems elems;
2719
Kalle Valo15b7b062009-03-22 21:57:14 +02002720 ifmgd = &sdata->u.mgd;
2721
Johannes Berg77fdaa12009-07-07 03:45:17 +02002722 ASSERT_MGD_MTX(ifmgd);
2723
Joe Perchesb203ca32012-05-08 18:56:52 +00002724 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002725 return; /* ignore ProbeResp to foreign address */
2726
Ester Kummerae6a44e2008-06-27 18:54:48 +03002727 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2728 if (baselen > len)
2729 return;
2730
2731 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2732 &elems);
2733
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002734 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002735
Johannes Berg77fdaa12009-07-07 03:45:17 +02002736 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002737 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002738 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002739
2740 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002741 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002742 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002743 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002744 ifmgd->auth_data->tries = 0;
2745 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002746 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002747 run_again(ifmgd, ifmgd->auth_data->timeout);
2748 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002749}
2750
Johannes Bergd91f36d2009-04-16 13:17:26 +02002751/*
2752 * This is the canonical list of information elements we care about,
2753 * the filter code also gives us all changes to the Microsoft OUI
2754 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2755 *
2756 * We implement beacon filtering in software since that means we can
2757 * avoid processing the frame here and in cfg80211, and userspace
2758 * will not be able to tell whether the hardware supports it or not.
2759 *
2760 * XXX: This list needs to be dynamic -- userspace needs to be able to
2761 * add items it requires. It also needs to be able to tell us to
2762 * look out for other vendor IEs.
2763 */
2764static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002765 (1ULL << WLAN_EID_COUNTRY) |
2766 (1ULL << WLAN_EID_ERP_INFO) |
2767 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2768 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2769 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002770 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002771
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002772static enum rx_mgmt_action
2773ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2774 struct ieee80211_mgmt *mgmt, size_t len,
2775 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002776{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002777 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002778 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002779 size_t baselen;
2780 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002781 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002782 struct ieee80211_chanctx_conf *chanctx_conf;
2783 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002784 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002785 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002786 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002787 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002788 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002789 u8 *bssid;
2790
Johannes Berg66e67e42012-01-20 13:55:27 +01002791 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002792
Ester Kummerae6a44e2008-06-27 18:54:48 +03002793 /* Process beacon from the current BSS */
2794 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2795 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002796 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002797
Johannes Berg55de9082012-07-26 17:24:39 +02002798 rcu_read_lock();
2799 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2800 if (!chanctx_conf) {
2801 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002802 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002803 }
2804
Johannes Berg4bf88532012-11-09 11:39:59 +01002805 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002806 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002807 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002808 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002809 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002810 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002811
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002812 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002813 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002814 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2815 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002816
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002817 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002818 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002819 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002820 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2821 sdata->vif.bss_conf.sync_tsf =
2822 le64_to_cpu(mgmt->u.beacon.timestamp);
2823 sdata->vif.bss_conf.sync_device_ts =
2824 rx_status->device_timestamp;
2825 if (elems.tim)
2826 sdata->vif.bss_conf.sync_dtim_count =
2827 elems.tim->dtim_count;
2828 else
2829 sdata->vif.bss_conf.sync_dtim_count = 0;
2830 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002831 /* continue assoc process */
2832 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002833 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002834 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002835 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002836 }
2837
2838 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002839 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002840 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002841 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002842
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002843 /* Track average RSSI from the Beacon frames of the current AP */
2844 ifmgd->last_beacon_signal = rx_status->signal;
2845 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2846 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002847 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002848 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002849 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002850 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002851 } else {
2852 ifmgd->ave_beacon_signal =
2853 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2854 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2855 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002856 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002857 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002858
2859 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2860 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2861 int sig = ifmgd->ave_beacon_signal;
2862 int last_sig = ifmgd->last_ave_beacon_signal;
2863
2864 /*
2865 * if signal crosses either of the boundaries, invoke callback
2866 * with appropriate parameters
2867 */
2868 if (sig > ifmgd->rssi_max_thold &&
2869 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2870 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002871 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002872 } else if (sig < ifmgd->rssi_min_thold &&
2873 (last_sig >= ifmgd->rssi_max_thold ||
2874 last_sig == 0)) {
2875 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002876 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002877 }
2878 }
2879
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002880 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002881 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002882 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002883 int sig = ifmgd->ave_beacon_signal / 16;
2884 int last_event = ifmgd->last_cqm_event_signal;
2885 int thold = bss_conf->cqm_rssi_thold;
2886 int hyst = bss_conf->cqm_rssi_hyst;
2887 if (sig < thold &&
2888 (last_event == 0 || sig < last_event - hyst)) {
2889 ifmgd->last_cqm_event_signal = sig;
2890 ieee80211_cqm_rssi_notify(
2891 &sdata->vif,
2892 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2893 GFP_KERNEL);
2894 } else 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_HIGH,
2900 GFP_KERNEL);
2901 }
2902 }
2903
Johannes Bergb291ba12009-07-10 15:29:03 +02002904 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002905 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002906 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002907 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002908 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002909 ieee80211_run_deferred_scan(local);
2910 mutex_unlock(&local->mtx);
2911
Johannes Berg4e751842009-06-10 15:16:52 +02002912 mutex_lock(&local->iflist_mtx);
2913 ieee80211_recalc_ps(local, -1);
2914 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002915 }
2916
Johannes Bergb291ba12009-07-10 15:29:03 +02002917 /*
2918 * Push the beacon loss detection into the future since
2919 * we are processing a beacon from the AP just now.
2920 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002921 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002922
Johannes Bergd91f36d2009-04-16 13:17:26 +02002923 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2924 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2925 len - baselen, &elems,
2926 care_about_ies, ncrc);
2927
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302928 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002929 bool directed_tim = ieee80211_check_tim(elems.tim,
2930 elems.tim_len,
2931 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002932 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002933 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002934 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2935 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2936 ieee80211_hw_config(local,
2937 IEEE80211_CONF_CHANGE_PS);
2938 }
Kalle Valo572e0012009-02-10 17:09:31 +02002939 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302940 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002941 local->pspolling = true;
2942
2943 /*
2944 * Here is assumed that the driver will be
2945 * able to send ps-poll frame and receive a
2946 * response even though power save mode is
2947 * enabled, but some drivers might require
2948 * to disable power save here. This needs
2949 * to be investigated.
2950 */
2951 ieee80211_send_pspoll(local, sdata);
2952 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002953 }
2954 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002955
Johannes Berg488dd7b2012-10-29 20:08:01 +01002956 if (sdata->vif.p2p) {
2957 u8 noa[2];
2958 int ret;
2959
2960 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2961 len - baselen,
2962 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2963 noa, sizeof(noa));
2964 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2965 bss_conf->p2p_oppps = noa[1] & 0x80;
2966 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2967 changed |= BSS_CHANGED_P2P_PS;
2968 sdata->u.mgd.p2p_noa_index = noa[0];
2969 /*
2970 * make sure we update all information, the CRC
2971 * mechanism doesn't look at P2P attributes.
2972 */
2973 ifmgd->beacon_crc_valid = false;
2974 }
2975 }
2976
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002977 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002978 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03002979 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002980 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002981
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002982 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002983
2984 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2985 elems.wmm_param_len))
2986 changed |= BSS_CHANGED_QOS;
2987
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002988 /*
2989 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002990 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002991 */
2992 if (!bss_conf->dtim_period) {
2993 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2994 if (elems.tim)
2995 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2996 else
2997 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01002998
2999 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3000 sdata->vif.bss_conf.sync_tsf =
3001 le64_to_cpu(mgmt->u.beacon.timestamp);
3002 sdata->vif.bss_conf.sync_device_ts =
3003 rx_status->device_timestamp;
3004 if (elems.tim)
3005 sdata->vif.bss_conf.sync_dtim_count =
3006 elems.tim->dtim_count;
3007 else
3008 sdata->vif.bss_conf.sync_dtim_count = 0;
3009 }
3010
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003011 changed |= BSS_CHANGED_DTIM_PERIOD;
3012 }
3013
Johannes Berg7a5158e2008-10-08 10:59:33 +02003014 if (elems.erp_info && elems.erp_info_len >= 1) {
3015 erp_valid = true;
3016 erp_value = elems.erp_info[0];
3017 } else {
3018 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003019 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003020 changed |= ieee80211_handle_bss_capability(sdata,
3021 le16_to_cpu(mgmt->u.beacon.capab_info),
3022 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003023
Johannes Berg11289582013-02-07 17:36:12 +01003024 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003025 sta = sta_info_get(sdata, bssid);
3026
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003027 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3028 elems.vht_operation, bssid, &changed)) {
3029 mutex_unlock(&local->sta_mtx);
3030 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3031 WLAN_REASON_DEAUTH_LEAVING,
3032 true, deauth_buf);
3033 return RX_MGMT_CFG80211_TX_DEAUTH;
3034 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003035
3036 if (sta && elems.opmode_notif)
3037 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3038 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003039 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003040
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003041 if (elems.country_elem && elems.pwr_constr_elem &&
3042 mgmt->u.probe_resp.capab_info &
3043 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003044 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3045 elems.country_elem,
3046 elems.country_elem_len,
3047 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003048
Johannes Berg471b3ef2007-12-28 14:32:58 +01003049 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003050
3051 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003052}
3053
Johannes Berg1fa57d02010-06-10 10:21:32 +02003054void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3055 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003056{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003057 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003058 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003059 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003060 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003061 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003062 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003063 u16 fc;
3064
Jiri Bencf0706e82007-05-05 11:45:53 -07003065 rx_status = (struct ieee80211_rx_status *) skb->cb;
3066 mgmt = (struct ieee80211_mgmt *) skb->data;
3067 fc = le16_to_cpu(mgmt->frame_control);
3068
Johannes Berg77fdaa12009-07-07 03:45:17 +02003069 mutex_lock(&ifmgd->mtx);
3070
Johannes Berg66e67e42012-01-20 13:55:27 +01003071 switch (fc & IEEE80211_FCTL_STYPE) {
3072 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003073 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3074 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003075 break;
3076 case IEEE80211_STYPE_PROBE_RESP:
3077 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3078 break;
3079 case IEEE80211_STYPE_AUTH:
3080 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3081 break;
3082 case IEEE80211_STYPE_DEAUTH:
3083 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3084 break;
3085 case IEEE80211_STYPE_DISASSOC:
3086 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3087 break;
3088 case IEEE80211_STYPE_ASSOC_RESP:
3089 case IEEE80211_STYPE_REASSOC_RESP:
3090 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3091 break;
3092 case IEEE80211_STYPE_ACTION:
3093 switch (mgmt->u.action.category) {
3094 case WLAN_CATEGORY_SPECTRUM_MGMT:
3095 ieee80211_sta_process_chanswitch(sdata,
3096 &mgmt->u.action.u.chan_switch.sw_elem,
3097 (void *)ifmgd->associated->priv,
3098 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003099 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003100 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003101 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003102 mutex_unlock(&ifmgd->mtx);
3103
Johannes Berg66e67e42012-01-20 13:55:27 +01003104 switch (rma) {
3105 case RX_MGMT_NONE:
3106 /* no action */
3107 break;
3108 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003109 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003110 break;
3111 case RX_MGMT_CFG80211_DISASSOC:
3112 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3113 break;
3114 case RX_MGMT_CFG80211_RX_AUTH:
3115 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3116 break;
3117 case RX_MGMT_CFG80211_RX_ASSOC:
3118 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3119 break;
3120 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3121 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3122 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003123 case RX_MGMT_CFG80211_TX_DEAUTH:
3124 cfg80211_send_deauth(sdata->dev, deauth_buf,
3125 sizeof(deauth_buf));
3126 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003127 default:
3128 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003129 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003130}
3131
Johannes Berg9c6bd792008-09-11 00:01:52 +02003132static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003133{
3134 struct ieee80211_sub_if_data *sdata =
3135 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01003136 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003137 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003138
Johannes Berg5bb644a2009-05-17 11:40:42 +02003139 if (local->quiescing) {
3140 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3141 return;
3142 }
3143
Johannes Berg64592c82010-06-10 10:21:31 +02003144 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003145}
3146
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003147static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003148 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003149{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003150 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003151 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003152
Johannes Berg37ad3882012-02-24 13:50:54 +01003153 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003154 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003155 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003156
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003157 /*
3158 * must be outside lock due to cfg80211,
3159 * but that's not a problem.
3160 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003161 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003162
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003163 mutex_lock(&ifmgd->mtx);
3164}
3165
Johannes Berg66e67e42012-01-20 13:55:27 +01003166static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3167{
3168 struct ieee80211_local *local = sdata->local;
3169 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3170 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003171 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003172
3173 lockdep_assert_held(&ifmgd->mtx);
3174
3175 if (WARN_ON_ONCE(!auth_data))
3176 return -EINVAL;
3177
Johannes Berg1672c0e32013-01-29 15:02:27 +01003178 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3179 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3180 IEEE80211_TX_INTFL_MLME_CONN_TX;
3181
Johannes Berg66e67e42012-01-20 13:55:27 +01003182 auth_data->tries++;
3183
3184 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003185 sdata_info(sdata, "authentication with %pM timed out\n",
3186 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003187
3188 /*
3189 * Most likely AP is not in the range so remove the
3190 * bss struct for that AP.
3191 */
3192 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3193
3194 return -ETIMEDOUT;
3195 }
3196
Johannes Berga1845fc2012-06-27 13:18:36 +02003197 drv_mgd_prepare_tx(local, sdata);
3198
Johannes Berg66e67e42012-01-20 13:55:27 +01003199 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003200 u16 trans = 1;
3201 u16 status = 0;
3202
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003203 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3204 auth_data->bss->bssid, auth_data->tries,
3205 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003206
3207 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003208
3209 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3210 trans = auth_data->sae_trans;
3211 status = auth_data->sae_status;
3212 auth_data->expected_transaction = trans;
3213 }
3214
3215 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3216 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003217 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003218 auth_data->bss->bssid, NULL, 0, 0,
3219 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003220 } else {
3221 const u8 *ssidie;
3222
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003223 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3224 auth_data->bss->bssid, auth_data->tries,
3225 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003226
Johannes Berg9caf0362012-11-29 01:25:20 +01003227 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003228 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003229 if (!ssidie) {
3230 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003231 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003232 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003233 /*
3234 * Direct probe is sent to broadcast address as some APs
3235 * will not answer to direct packet in unassociated state.
3236 */
3237 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01003238 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003239 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003240 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003241 }
3242
Johannes Berg1672c0e32013-01-29 15:02:27 +01003243 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3244 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003245 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003246 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003247 } else {
3248 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003249 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003250
3251 return 0;
3252}
3253
3254static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3255{
3256 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3257 struct ieee80211_local *local = sdata->local;
3258
3259 lockdep_assert_held(&sdata->u.mgd.mtx);
3260
Johannes Berg66e67e42012-01-20 13:55:27 +01003261 assoc_data->tries++;
3262 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003263 sdata_info(sdata, "association with %pM timed out\n",
3264 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003265
3266 /*
3267 * Most likely AP is not in the range so remove the
3268 * bss struct for that AP.
3269 */
3270 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3271
3272 return -ETIMEDOUT;
3273 }
3274
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003275 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3276 assoc_data->bss->bssid, assoc_data->tries,
3277 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003278 ieee80211_send_assoc(sdata);
3279
Johannes Berg1672c0e32013-01-29 15:02:27 +01003280 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3281 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003282 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003283 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003284 } else {
3285 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003286 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003287
3288 return 0;
3289}
3290
Johannes Berg1672c0e32013-01-29 15:02:27 +01003291void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3292 __le16 fc, bool acked)
3293{
3294 struct ieee80211_local *local = sdata->local;
3295
3296 sdata->u.mgd.status_fc = fc;
3297 sdata->u.mgd.status_acked = acked;
3298 sdata->u.mgd.status_received = true;
3299
3300 ieee80211_queue_work(&local->hw, &sdata->work);
3301}
3302
Johannes Berg1fa57d02010-06-10 10:21:32 +02003303void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003304{
Johannes Berg60f8b392008-09-08 17:44:22 +02003305 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003306 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003307
Johannes Berg77fdaa12009-07-07 03:45:17 +02003308 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003309
Johannes Berg1672c0e32013-01-29 15:02:27 +01003310 if (ifmgd->status_received) {
3311 __le16 fc = ifmgd->status_fc;
3312 bool status_acked = ifmgd->status_acked;
3313
3314 ifmgd->status_received = false;
3315 if (ifmgd->auth_data &&
3316 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3317 if (status_acked) {
3318 ifmgd->auth_data->timeout =
3319 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3320 run_again(ifmgd, ifmgd->auth_data->timeout);
3321 } else {
3322 ifmgd->auth_data->timeout = jiffies - 1;
3323 }
Johannes Berg89afe612013-02-13 15:39:57 +01003324 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003325 } else if (ifmgd->assoc_data &&
3326 (ieee80211_is_assoc_req(fc) ||
3327 ieee80211_is_reassoc_req(fc))) {
3328 if (status_acked) {
3329 ifmgd->assoc_data->timeout =
3330 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3331 run_again(ifmgd, ifmgd->assoc_data->timeout);
3332 } else {
3333 ifmgd->assoc_data->timeout = jiffies - 1;
3334 }
Johannes Berg89afe612013-02-13 15:39:57 +01003335 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003336 }
3337 }
3338
Johannes Berg89afe612013-02-13 15:39:57 +01003339 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003340 time_after(jiffies, ifmgd->auth_data->timeout)) {
3341 if (ifmgd->auth_data->done) {
3342 /*
3343 * ok ... we waited for assoc but userspace didn't,
3344 * so let's just kill the auth data
3345 */
3346 ieee80211_destroy_auth_data(sdata, false);
3347 } else if (ieee80211_probe_auth(sdata)) {
3348 u8 bssid[ETH_ALEN];
3349
3350 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3351
3352 ieee80211_destroy_auth_data(sdata, false);
3353
3354 mutex_unlock(&ifmgd->mtx);
3355 cfg80211_send_auth_timeout(sdata->dev, bssid);
3356 mutex_lock(&ifmgd->mtx);
3357 }
Johannes Berg89afe612013-02-13 15:39:57 +01003358 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003359 run_again(ifmgd, ifmgd->auth_data->timeout);
3360
Johannes Berg89afe612013-02-13 15:39:57 +01003361 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003362 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003363 if ((ifmgd->assoc_data->need_beacon &&
3364 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003365 ieee80211_do_assoc(sdata)) {
3366 u8 bssid[ETH_ALEN];
3367
3368 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3369
3370 ieee80211_destroy_assoc_data(sdata, false);
3371
3372 mutex_unlock(&ifmgd->mtx);
3373 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3374 mutex_lock(&ifmgd->mtx);
3375 }
Johannes Berg89afe612013-02-13 15:39:57 +01003376 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003377 run_again(ifmgd, ifmgd->assoc_data->timeout);
3378
Johannes Bergb291ba12009-07-10 15:29:03 +02003379 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3380 IEEE80211_STA_CONNECTION_POLL) &&
3381 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003382 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003383 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003384
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003385 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003386
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003387 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003388 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003389 else
Ben Greear180205b2011-02-04 15:30:24 -08003390 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003391
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003392 /* ACK received for nullfunc probing frame */
3393 if (!ifmgd->probe_send_count)
3394 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003395 else if (ifmgd->nullfunc_failed) {
3396 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003397 mlme_dbg(sdata,
3398 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3399 bssid, ifmgd->probe_send_count,
3400 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003401 ieee80211_mgd_probe_ap_send(sdata);
3402 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003403 mlme_dbg(sdata,
3404 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3405 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003406 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003407 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3408 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003409 }
3410 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003411 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003412 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003413 mlme_dbg(sdata,
3414 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3415 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003416 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003417 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003418 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003419 mlme_dbg(sdata,
3420 "No probe response from AP %pM after %dms, try %d/%i\n",
3421 bssid, probe_wait_ms,
3422 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003423 ieee80211_mgd_probe_ap_send(sdata);
3424 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003425 /*
3426 * We actually lost the connection ... or did we?
3427 * Let's make sure!
3428 */
Ben Greearb38afa82010-10-07 16:12:06 -07003429 wiphy_debug(local->hw.wiphy,
3430 "%s: No probe response from AP %pM"
3431 " after %dms, disconnecting.\n",
3432 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003433 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003434
Johannes Berg95acac62011-07-12 12:30:59 +02003435 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003436 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003437 }
3438 }
3439
Johannes Berg77fdaa12009-07-07 03:45:17 +02003440 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003441}
Johannes Berg0a51b272008-09-08 17:44:25 +02003442
Johannes Bergb291ba12009-07-10 15:29:03 +02003443static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3444{
3445 struct ieee80211_sub_if_data *sdata =
3446 (struct ieee80211_sub_if_data *) data;
3447 struct ieee80211_local *local = sdata->local;
3448
3449 if (local->quiescing)
3450 return;
3451
Johannes Berg682bd382013-01-29 13:13:50 +01003452 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003453 ieee80211_queue_work(&sdata->local->hw,
3454 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003455}
3456
3457static void ieee80211_sta_conn_mon_timer(unsigned long data)
3458{
3459 struct ieee80211_sub_if_data *sdata =
3460 (struct ieee80211_sub_if_data *) data;
3461 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3462 struct ieee80211_local *local = sdata->local;
3463
3464 if (local->quiescing)
3465 return;
3466
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003467 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003468}
3469
3470static void ieee80211_sta_monitor_work(struct work_struct *work)
3471{
3472 struct ieee80211_sub_if_data *sdata =
3473 container_of(work, struct ieee80211_sub_if_data,
3474 u.mgd.monitor_work);
3475
3476 ieee80211_mgd_probe_ap(sdata, false);
3477}
3478
Johannes Berga1678f82008-09-11 00:01:47 +02003479static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3480{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003481 u32 flags;
3482
Kalle Vaload935682009-04-19 08:47:19 +03003483 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003484 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003485
Johannes Bergb291ba12009-07-10 15:29:03 +02003486 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003487 flags = sdata->local->hw.flags;
3488 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3489 ieee80211_queue_work(&sdata->local->hw,
3490 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003491 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003492 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003493 }
Johannes Berga1678f82008-09-11 00:01:47 +02003494}
3495
Johannes Berg5bb644a2009-05-17 11:40:42 +02003496#ifdef CONFIG_PM
3497void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3498{
3499 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3500
3501 /*
3502 * we need to use atomic bitops for the running bits
3503 * only because both timers might fire at the same
3504 * time -- the code here is properly synchronised.
3505 */
3506
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003507 cancel_work_sync(&ifmgd->request_smps_work);
3508
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003509 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003510 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003511 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003512 if (del_timer_sync(&ifmgd->timer))
3513 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3514
3515 cancel_work_sync(&ifmgd->chswitch_work);
3516 if (del_timer_sync(&ifmgd->chswitch_timer))
3517 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003518
Johannes Bergb291ba12009-07-10 15:29:03 +02003519 /* these will just be re-established on connection */
3520 del_timer_sync(&ifmgd->conn_mon_timer);
3521 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003522}
3523
3524void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3525{
3526 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3527
Johannes Berg782d2672013-01-18 21:21:31 +01003528 mutex_lock(&ifmgd->mtx);
3529 if (!ifmgd->associated) {
3530 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303531 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003532 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303533
Johannes Berg95acac62011-07-12 12:30:59 +02003534 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3535 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003536 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3537 ieee80211_sta_connection_lost(sdata,
3538 ifmgd->associated->bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003539 WLAN_REASON_UNSPECIFIED,
3540 true);
Johannes Berg95acac62011-07-12 12:30:59 +02003541 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003542 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003543 }
Johannes Berg782d2672013-01-18 21:21:31 +01003544 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003545
Johannes Berg5bb644a2009-05-17 11:40:42 +02003546 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3547 add_timer(&ifmgd->timer);
3548 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3549 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003550 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003551
3552 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003553 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003554 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003555}
3556#endif
3557
Johannes Berg9c6bd792008-09-11 00:01:52 +02003558/* interface setup */
3559void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3560{
Johannes Berg46900292009-02-15 12:44:28 +01003561 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003562
Johannes Berg46900292009-02-15 12:44:28 +01003563 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003564 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003565 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003566 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3567 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003568 INIT_WORK(&ifmgd->csa_connection_drop_work,
3569 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003570 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003571 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003572 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003573 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3574 (unsigned long) sdata);
3575 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3576 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003577 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303578 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003579
Johannes Bergab1faea2009-07-01 21:41:17 +02003580 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303581 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003582 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3583 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003584
Johannes Berg77fdaa12009-07-07 03:45:17 +02003585 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003586
3587 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3588 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3589 else
3590 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003591}
3592
3593/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003594void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3595{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003596 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003597
3598 /* Restart STA timers */
3599 rcu_read_lock();
3600 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3601 ieee80211_restart_sta_timer(sdata);
3602 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003603}
Johannes Berg10f644a2009-04-16 13:17:25 +02003604
3605int ieee80211_max_network_latency(struct notifier_block *nb,
3606 unsigned long data, void *dummy)
3607{
3608 s32 latency_usec = (s32) data;
3609 struct ieee80211_local *local =
3610 container_of(nb, struct ieee80211_local,
3611 network_latency_notifier);
3612
3613 mutex_lock(&local->iflist_mtx);
3614 ieee80211_recalc_ps(local, latency_usec);
3615 mutex_unlock(&local->iflist_mtx);
3616
3617 return 0;
3618}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003619
Johannes Bergf2d9d272012-11-22 14:11:39 +01003620static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3621 struct cfg80211_bss *cbss)
3622{
3623 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3624 const u8 *ht_cap_ie, *vht_cap_ie;
3625 const struct ieee80211_ht_cap *ht_cap;
3626 const struct ieee80211_vht_cap *vht_cap;
3627 u8 chains = 1;
3628
3629 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3630 return chains;
3631
Johannes Berg9caf0362012-11-29 01:25:20 +01003632 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003633 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3634 ht_cap = (void *)(ht_cap_ie + 2);
3635 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3636 /*
3637 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3638 * "Tx Unequal Modulation Supported" fields.
3639 */
3640 }
3641
3642 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3643 return chains;
3644
Johannes Berg9caf0362012-11-29 01:25:20 +01003645 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003646 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3647 u8 nss;
3648 u16 tx_mcs_map;
3649
3650 vht_cap = (void *)(vht_cap_ie + 2);
3651 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3652 for (nss = 8; nss > 0; nss--) {
3653 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3654 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3655 break;
3656 }
3657 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3658 chains = max(chains, nss);
3659 }
3660
3661 return chains;
3662}
3663
Johannes Bergb17166a2012-07-27 11:41:27 +02003664static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3665 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003666{
3667 struct ieee80211_local *local = sdata->local;
3668 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003669 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003670 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003671 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003672 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003673 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003674
Johannes Berg24398e32012-03-28 10:58:36 +02003675 sband = local->hw.wiphy->bands[cbss->channel->band];
3676
Johannes Bergf2d9d272012-11-22 14:11:39 +01003677 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3678 IEEE80211_STA_DISABLE_80P80MHZ |
3679 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003680
Johannes Berg9caf0362012-11-29 01:25:20 +01003681 rcu_read_lock();
3682
Johannes Bergf2d9d272012-11-22 14:11:39 +01003683 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3684 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003685 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003686
Johannes Berg9caf0362012-11-29 01:25:20 +01003687 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003688 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3689 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003690
3691 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3692 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3693 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3694 ht_oper = NULL;
3695 }
Johannes Berg24398e32012-03-28 10:58:36 +02003696 }
3697
Johannes Bergf2d9d272012-11-22 14:11:39 +01003698 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3699 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003700 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003701
Johannes Berg9caf0362012-11-29 01:25:20 +01003702 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3703 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003704 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3705 vht_oper = (void *)(vht_oper_ie + 2);
3706 if (vht_oper && !ht_oper) {
3707 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003708 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003709 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003710 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3711 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003712 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003713
3714 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3715 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3716 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3717 vht_oper = NULL;
3718 }
Johannes Berg24398e32012-03-28 10:58:36 +02003719 }
3720
Johannes Bergf2d9d272012-11-22 14:11:39 +01003721 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3722 cbss->channel,
3723 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003724 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003725
Johannes Bergf2d9d272012-11-22 14:11:39 +01003726 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3727 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003728
Johannes Berg9caf0362012-11-29 01:25:20 +01003729 rcu_read_unlock();
3730
Johannes Berg04ecd252012-09-11 14:34:12 +02003731 /* will change later if needed */
3732 sdata->smps_mode = IEEE80211_SMPS_OFF;
3733
Johannes Bergf2d9d272012-11-22 14:11:39 +01003734 /*
3735 * If this fails (possibly due to channel context sharing
3736 * on incompatible channels, e.g. 80+80 and 160 sharing the
3737 * same control channel) try to use a smaller bandwidth.
3738 */
3739 ret = ieee80211_vif_use_channel(sdata, &chandef,
3740 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003741 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003742 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003743 ret = ieee80211_vif_use_channel(sdata, &chandef,
3744 IEEE80211_CHANCTX_SHARED);
3745 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003746 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003747}
3748
3749static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3750 struct cfg80211_bss *cbss, bool assoc)
3751{
3752 struct ieee80211_local *local = sdata->local;
3753 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3754 struct ieee80211_bss *bss = (void *)cbss->priv;
3755 struct sta_info *new_sta = NULL;
3756 bool have_sta = false;
3757 int err;
3758
3759 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3760 return -EINVAL;
3761
3762 if (assoc) {
3763 rcu_read_lock();
3764 have_sta = sta_info_get(sdata, cbss->bssid);
3765 rcu_read_unlock();
3766 }
3767
3768 if (!have_sta) {
3769 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3770 if (!new_sta)
3771 return -ENOMEM;
3772 }
3773
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003774 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003775 u32 rates = 0, basic_rates = 0;
3776 bool have_higher_than_11mbit;
3777 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003778 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003779 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003780
3781 sband = local->hw.wiphy->bands[cbss->channel->band];
3782
3783 err = ieee80211_prep_channel(sdata, cbss);
3784 if (err) {
3785 sta_info_free(local, new_sta);
3786 return err;
3787 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003788
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003789 ieee80211_get_rates(sband, bss->supp_rates,
3790 bss->supp_rates_len,
3791 &rates, &basic_rates,
3792 &have_higher_than_11mbit,
3793 &min_rate, &min_rate_index);
3794
3795 /*
3796 * This used to be a workaround for basic rates missing
3797 * in the association response frame. Now that we no
3798 * longer use the basic rates from there, it probably
3799 * doesn't happen any more, but keep the workaround so
3800 * in case some *other* APs are buggy in different ways
3801 * we can connect -- with a warning.
3802 */
3803 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003804 sdata_info(sdata,
3805 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003806 basic_rates = BIT(min_rate_index);
3807 }
3808
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003809 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003810 sdata->vif.bss_conf.basic_rates = basic_rates;
3811
3812 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003813 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003814 have_higher_than_11mbit)
3815 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3816 else
3817 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3818
3819 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3820
Johannes Berg8c358bc2012-05-22 22:13:05 +02003821 /* set timing information */
3822 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003823 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003824 ies = rcu_dereference(cbss->beacon_ies);
3825 if (ies) {
3826 const u8 *tim_ie;
3827
3828 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3829 sdata->vif.bss_conf.sync_device_ts =
3830 bss->device_ts_beacon;
3831 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3832 ies->data, ies->len);
3833 if (tim_ie && tim_ie[1] >= 2)
3834 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3835 else
3836 sdata->vif.bss_conf.sync_dtim_count = 0;
3837 } else if (!(local->hw.flags &
3838 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3839 ies = rcu_dereference(cbss->proberesp_ies);
3840 /* must be non-NULL since beacon IEs were NULL */
3841 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3842 sdata->vif.bss_conf.sync_device_ts =
3843 bss->device_ts_presp;
3844 sdata->vif.bss_conf.sync_dtim_count = 0;
3845 } else {
3846 sdata->vif.bss_conf.sync_tsf = 0;
3847 sdata->vif.bss_conf.sync_device_ts = 0;
3848 sdata->vif.bss_conf.sync_dtim_count = 0;
3849 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003850 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003851
3852 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003853 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003854 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3855 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003856
3857 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003858 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003859
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003860 err = sta_info_insert(new_sta);
3861 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003862 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003863 sdata_info(sdata,
3864 "failed to insert STA entry for the AP (error %d)\n",
3865 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003866 return err;
3867 }
3868 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003869 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003870
3871 return 0;
3872}
3873
Johannes Berg77fdaa12009-07-07 03:45:17 +02003874/* config hooks */
3875int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3876 struct cfg80211_auth_request *req)
3877{
Johannes Berg66e67e42012-01-20 13:55:27 +01003878 struct ieee80211_local *local = sdata->local;
3879 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3880 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003881 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003882 int err;
3883
3884 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003885
3886 switch (req->auth_type) {
3887 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3888 auth_alg = WLAN_AUTH_OPEN;
3889 break;
3890 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003891 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003892 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003893 auth_alg = WLAN_AUTH_SHARED_KEY;
3894 break;
3895 case NL80211_AUTHTYPE_FT:
3896 auth_alg = WLAN_AUTH_FT;
3897 break;
3898 case NL80211_AUTHTYPE_NETWORK_EAP:
3899 auth_alg = WLAN_AUTH_LEAP;
3900 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003901 case NL80211_AUTHTYPE_SAE:
3902 auth_alg = WLAN_AUTH_SAE;
3903 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003904 default:
3905 return -EOPNOTSUPP;
3906 }
3907
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003908 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3909 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003910 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003911 return -ENOMEM;
3912
Johannes Berg66e67e42012-01-20 13:55:27 +01003913 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003914
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003915 if (req->sae_data_len >= 4) {
3916 __le16 *pos = (__le16 *) req->sae_data;
3917 auth_data->sae_trans = le16_to_cpu(pos[0]);
3918 auth_data->sae_status = le16_to_cpu(pos[1]);
3919 memcpy(auth_data->data, req->sae_data + 4,
3920 req->sae_data_len - 4);
3921 auth_data->data_len += req->sae_data_len - 4;
3922 }
3923
Johannes Berg77fdaa12009-07-07 03:45:17 +02003924 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003925 memcpy(&auth_data->data[auth_data->data_len],
3926 req->ie, req->ie_len);
3927 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003928 }
3929
Johannes Bergfffd0932009-07-08 14:22:54 +02003930 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003931 auth_data->key_len = req->key_len;
3932 auth_data->key_idx = req->key_idx;
3933 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003934 }
3935
Johannes Berg66e67e42012-01-20 13:55:27 +01003936 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003937
Johannes Berg66e67e42012-01-20 13:55:27 +01003938 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003939
Johannes Berg66e67e42012-01-20 13:55:27 +01003940 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003941
Johannes Berg66e67e42012-01-20 13:55:27 +01003942 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3943 ifmgd->assoc_data) {
3944 err = -EBUSY;
3945 goto err_free;
3946 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003947
Johannes Berg66e67e42012-01-20 13:55:27 +01003948 if (ifmgd->auth_data)
3949 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003950
Johannes Berg66e67e42012-01-20 13:55:27 +01003951 /* prep auth_data so we don't go into idle on disassoc */
3952 ifmgd->auth_data = auth_data;
3953
3954 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003955 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003956
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003957 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003958
Johannes Berga1cf7752012-03-08 15:02:07 +01003959 err = ieee80211_prep_connection(sdata, req->bss, false);
3960 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003961 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003962
Johannes Berg66e67e42012-01-20 13:55:27 +01003963 err = ieee80211_probe_auth(sdata);
3964 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003965 sta_info_destroy_addr(sdata, req->bss->bssid);
3966 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003967 }
3968
Johannes Berg66e67e42012-01-20 13:55:27 +01003969 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003970 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003971 err = 0;
3972 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003973
Johannes Berg66e67e42012-01-20 13:55:27 +01003974 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003975 memset(ifmgd->bssid, 0, ETH_ALEN);
3976 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003977 ifmgd->auth_data = NULL;
3978 err_free:
3979 kfree(auth_data);
3980 out_unlock:
3981 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003982
Johannes Berg66e67e42012-01-20 13:55:27 +01003983 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003984}
3985
Johannes Berg77fdaa12009-07-07 03:45:17 +02003986int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3987 struct cfg80211_assoc_request *req)
3988{
Johannes Berg66e67e42012-01-20 13:55:27 +01003989 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003990 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003991 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003992 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003993 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003994 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003995 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003996 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003997
Johannes Berg66e67e42012-01-20 13:55:27 +01003998 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3999 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004000 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004001
Johannes Berg9caf0362012-11-29 01:25:20 +01004002 rcu_read_lock();
4003 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4004 if (!ssidie) {
4005 rcu_read_unlock();
4006 kfree(assoc_data);
4007 return -EINVAL;
4008 }
4009 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4010 assoc_data->ssid_len = ssidie[1];
4011 rcu_read_unlock();
4012
Johannes Berg66e67e42012-01-20 13:55:27 +01004013 mutex_lock(&ifmgd->mtx);
4014
4015 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01004016 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01004017
4018 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4019 err = -EBUSY;
4020 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004021 }
4022
Johannes Berg66e67e42012-01-20 13:55:27 +01004023 if (ifmgd->assoc_data) {
4024 err = -EBUSY;
4025 goto err_free;
4026 }
4027
4028 if (ifmgd->auth_data) {
4029 bool match;
4030
4031 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004032 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004033 ieee80211_destroy_auth_data(sdata, match);
4034 }
4035
4036 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02004037
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004038 ifmgd->beacon_crc_valid = false;
4039
Johannes Bergde5036a2012-03-08 15:02:03 +01004040 /*
4041 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4042 * We still associate in non-HT mode (11a/b/g) if any one of these
4043 * ciphers is configured as pairwise.
4044 * We can set this to true for non-11n hardware, that'll be checked
4045 * separately along with the peer capabilities.
4046 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004047 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004048 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4049 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004050 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004051 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004052 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004053 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004054 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004055 }
4056 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004057
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004058 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004059 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004060 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4061 }
Ben Greearef96a8422011-11-18 11:32:00 -08004062
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004063 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4064 sband = local->hw.wiphy->bands[req->bss->channel->band];
4065 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004066 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004067 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004068 if (!bss->wmm_used)
4069 netdev_info(sdata->dev,
4070 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004071 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004072
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004073 /* disable VHT if we don't support it or the AP doesn't use WMM */
4074 if (!sband->vht_cap.vht_supported ||
4075 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4076 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004077 if (!bss->wmm_used)
4078 netdev_info(sdata->dev,
4079 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004080 }
4081
Ben Greearef96a8422011-11-18 11:32:00 -08004082 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4083 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4084 sizeof(ifmgd->ht_capa_mask));
4085
Johannes Berg77fdaa12009-07-07 03:45:17 +02004086 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004087 memcpy(assoc_data->ie, req->ie, req->ie_len);
4088 assoc_data->ie_len = req->ie_len;
4089 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004090
Johannes Berg66e67e42012-01-20 13:55:27 +01004091 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004092
Johannes Bergaf6b6372009-12-23 13:15:35 +01004093 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4094 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004095 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004096 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004097 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004098 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004099 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004100
Johannes Berg66e67e42012-01-20 13:55:27 +01004101 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004102 assoc_data->wmm = bss->wmm_used &&
4103 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004104 assoc_data->supp_rates = bss->supp_rates;
4105 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004106
Johannes Berg9caf0362012-11-29 01:25:20 +01004107 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004108 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4109 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4110 assoc_data->ap_ht_param =
4111 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4112 else
Johannes Berga8243b72012-11-22 14:32:09 +01004113 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004114 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4115 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4116 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4117 sizeof(struct ieee80211_vht_cap));
4118 else
4119 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004120 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004121
Kalle Valoab133152010-01-12 10:42:31 +02004122 if (bss->wmm_used && bss->uapsd_supported &&
4123 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01004124 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004125 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4126 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004127 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004128 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4129 }
4130
Johannes Berg77fdaa12009-07-07 03:45:17 +02004131 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004132 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004133
4134 if (req->use_mfp) {
4135 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4136 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4137 } else {
4138 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4139 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4140 }
4141
4142 if (req->crypto.control_port)
4143 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4144 else
4145 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4146
Johannes Berga621fa42010-08-27 14:26:54 +03004147 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4148 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4149
Johannes Berg66e67e42012-01-20 13:55:27 +01004150 /* kick off associate process */
4151
4152 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004153 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004154
Johannes Berga1cf7752012-03-08 15:02:07 +01004155 err = ieee80211_prep_connection(sdata, req->bss, true);
4156 if (err)
4157 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004158
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004159 rcu_read_lock();
4160 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004161
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004162 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4163 !beacon_ies) {
4164 /*
4165 * Wait up to one beacon interval ...
4166 * should this be more if we miss one?
4167 */
4168 sdata_info(sdata, "waiting for beacon from %pM\n",
4169 ifmgd->bssid);
4170 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004171 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004172 assoc_data->need_beacon = true;
4173 } else if (beacon_ies) {
4174 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4175 beacon_ies->data,
4176 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004177 u8 dtim_count = 0;
4178
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004179 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4180 const struct ieee80211_tim_ie *tim;
4181 tim = (void *)(tim_ie + 2);
4182 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004183 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004184 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004185 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004186 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004187 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004188
4189 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4190 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4191 sdata->vif.bss_conf.sync_device_ts =
4192 bss->device_ts_beacon;
4193 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4194 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004195 } else {
4196 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004197 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004198 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004199 rcu_read_unlock();
4200
Johannes Berg66e67e42012-01-20 13:55:27 +01004201 run_again(ifmgd, assoc_data->timeout);
4202
Paul Stewartfcff4f12012-02-23 17:59:53 -08004203 if (bss->corrupt_data) {
4204 char *corrupt_type = "data";
4205 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4206 if (bss->corrupt_data &
4207 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4208 corrupt_type = "beacon and probe response";
4209 else
4210 corrupt_type = "beacon";
4211 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4212 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004213 sdata_info(sdata, "associating with AP with corrupt %s\n",
4214 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004215 }
4216
Johannes Berg66e67e42012-01-20 13:55:27 +01004217 err = 0;
4218 goto out;
4219 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004220 memset(ifmgd->bssid, 0, ETH_ALEN);
4221 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004222 ifmgd->assoc_data = NULL;
4223 err_free:
4224 kfree(assoc_data);
4225 out:
4226 mutex_unlock(&ifmgd->mtx);
4227
4228 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004229}
4230
4231int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004232 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004233{
4234 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004235 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004236 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004237 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004238
Johannes Berg77fdaa12009-07-07 03:45:17 +02004239 mutex_lock(&ifmgd->mtx);
4240
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004241 sdata_info(sdata,
4242 "deauthenticating from %pM by local choice (reason=%d)\n",
4243 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004244
Johannes Berg86552012012-10-29 09:46:31 +01004245 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004246 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004247 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4248 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004249 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004250 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004251 ieee80211_destroy_auth_data(sdata, false);
4252 mutex_unlock(&ifmgd->mtx);
4253
4254 sent_frame = tx;
4255 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004256 }
4257
Johannes Berg86552012012-10-29 09:46:31 +01004258 if (ifmgd->associated &&
4259 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4260 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4261 req->reason_code, tx, frame_buf);
4262 sent_frame = tx;
4263 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004264 mutex_unlock(&ifmgd->mtx);
4265
Johannes Berg86552012012-10-29 09:46:31 +01004266 out:
Johannes Berg86552012012-10-29 09:46:31 +01004267 if (sent_frame)
4268 __cfg80211_send_deauth(sdata->dev, frame_buf,
4269 IEEE80211_DEAUTH_FRAME_LEN);
4270
Johannes Berg77fdaa12009-07-07 03:45:17 +02004271 return 0;
4272}
4273
4274int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004275 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004276{
4277 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004278 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004279 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004280
Johannes Berg77fdaa12009-07-07 03:45:17 +02004281 mutex_lock(&ifmgd->mtx);
4282
Johannes Berg8d8b2612009-07-25 11:58:36 +02004283 /*
4284 * cfg80211 should catch this ... but it's racy since
4285 * we can receive a disassoc frame, process it, hand it
4286 * to cfg80211 while that's in a locked section already
4287 * trying to tell us that the user wants to disconnect.
4288 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004289 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004290 mutex_unlock(&ifmgd->mtx);
4291 return -ENOLINK;
4292 }
4293
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004294 sdata_info(sdata,
4295 "disassociating from %pM by local choice (reason=%d)\n",
4296 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004297
Jouni Malinene69e95d2010-03-29 23:29:31 -07004298 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004299 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4300 req->reason_code, !req->local_state_change,
4301 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004302 mutex_unlock(&ifmgd->mtx);
4303
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004304 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4305 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004306
Johannes Berg77fdaa12009-07-07 03:45:17 +02004307 return 0;
4308}
Jouni Malinen026331c2010-02-15 12:53:10 +02004309
Eliad Pellerafa762f2012-04-23 14:45:15 +03004310void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004311{
4312 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4313
4314 mutex_lock(&ifmgd->mtx);
4315 if (ifmgd->assoc_data)
4316 ieee80211_destroy_assoc_data(sdata, false);
4317 if (ifmgd->auth_data)
4318 ieee80211_destroy_auth_data(sdata, false);
4319 del_timer_sync(&ifmgd->timer);
4320 mutex_unlock(&ifmgd->mtx);
4321}
4322
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004323void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4324 enum nl80211_cqm_rssi_threshold_event rssi_event,
4325 gfp_t gfp)
4326{
4327 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4328
Johannes Bergb5878a22010-04-07 16:48:40 +02004329 trace_api_cqm_rssi_notify(sdata, rssi_event);
4330
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004331 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4332}
4333EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);