blob: 9784622cd3d14ed73590eea456e2d1d99bd23652 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
35#define IEEE80211_AUTH_MAX_TRIES 3
36#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
39#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static int max_nullfunc_tries = 2;
42module_param(max_nullfunc_tries, int, 0644);
43MODULE_PARM_DESC(max_nullfunc_tries,
44 "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46static int max_probe_tries = 5;
47module_param(max_probe_tries, int, 0644);
48MODULE_PARM_DESC(max_probe_tries,
49 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * Beacon loss timeout is calculated as N frames times the
53 * advertised beacon interval. This may need to be somewhat
54 * higher than what hardware might detect to account for
55 * delays in the host processing frames. But since we also
56 * probe on beacon miss before declaring the connection lost
57 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010059#define IEEE80211_BEACON_LOSS_COUNT 7
60
Johannes Bergb291ba12009-07-10 15:29:03 +020061/*
62 * Time the connection can be idle before we probe
63 * it to see if we can still talk to the AP.
64 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030065#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time we wait for a probe response after sending
68 * a probe request because of beacon loss or for
69 * checking the connection still works.
70 */
Ben Greear180205b2011-02-04 15:30:24 -080071static int probe_wait_ms = 500;
72module_param(probe_wait_ms, int, 0644);
73MODULE_PARM_DESC(probe_wait_ms,
74 "Maximum time(ms) to wait for probe response"
75 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jouni Malinen17e4ec12010-03-29 23:28:30 -070077/*
78 * Weight given to the latest Beacon frame when calculating average signal
79 * strength for Beacon frames received in the current BSS. This must be
80 * between 1 and 15.
81 */
82#define IEEE80211_SIGNAL_AVE_WEIGHT 3
83
Jouni Malinen391a2002010-08-27 22:22:00 +030084/*
85 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg5bb644a2009-05-17 11:40:42 +020090#define TMR_RUNNING_TIMER 0
91#define TMR_RUNNING_CHANSW 1
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100117
118 /* used when a processed beacon causes a deauth */
119 RX_MGMT_CFG80211_TX_DEAUTH,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120};
121
Johannes Berg5484e232008-09-08 17:44:27 +0200122/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
124{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200125 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200126}
127
Johannes Bergca386f32009-07-10 02:39:48 +0200128/*
129 * We can have multiple work items (and connection probing)
130 * scheduling this timer, but we need to take care to only
131 * reschedule it when it should fire _earlier_ than it was
132 * asked for before, or if it's not pending right now. This
133 * function ensures that. Note that it then is required to
134 * run this function for all timeouts after the first one
135 * has happened -- the work that runs from this timer will
136 * do that.
137 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100138static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200139{
140 ASSERT_MGD_MTX(ifmgd);
141
142 if (!timer_pending(&ifmgd->timer) ||
143 time_before(timeout, ifmgd->timer.expires))
144 mod_timer(&ifmgd->timer, timeout);
145}
146
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400147void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200148{
Johannes Bergc1288b12012-01-19 09:29:57 +0100149 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 return;
151
Johannes Berg7eeff742012-07-18 10:27:27 +0200152 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
153 return;
154
Johannes Bergb291ba12009-07-10 15:29:03 +0200155 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100156 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200157}
158
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400159void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
160{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400161 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
162
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100163 if (unlikely(!sdata->u.mgd.associated))
164 return;
165
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400166 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
167 return;
168
169 mod_timer(&sdata->u.mgd.conn_mon_timer,
170 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400171
172 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400173}
174
Johannes Berg5484e232008-09-08 17:44:27 +0200175static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200176{
Johannes Berg5484e232008-09-08 17:44:27 +0200177 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200178}
179
Johannes Berg6565ec92013-02-08 14:52:32 +0100180static u32 chandef_downgrade(struct cfg80211_chan_def *c)
181{
182 u32 ret;
183 int tmp;
184
185 switch (c->width) {
186 case NL80211_CHAN_WIDTH_20:
187 c->width = NL80211_CHAN_WIDTH_20_NOHT;
188 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
189 break;
190 case NL80211_CHAN_WIDTH_40:
191 c->width = NL80211_CHAN_WIDTH_20;
192 c->center_freq1 = c->chan->center_freq;
193 ret = IEEE80211_STA_DISABLE_40MHZ |
194 IEEE80211_STA_DISABLE_VHT;
195 break;
196 case NL80211_CHAN_WIDTH_80:
197 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
198 /* n_P40 */
199 tmp /= 2;
200 /* freq_P40 */
201 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
202 c->width = NL80211_CHAN_WIDTH_40;
203 ret = IEEE80211_STA_DISABLE_VHT;
204 break;
205 case NL80211_CHAN_WIDTH_80P80:
206 c->center_freq2 = 0;
207 c->width = NL80211_CHAN_WIDTH_80;
208 ret = IEEE80211_STA_DISABLE_80P80MHZ |
209 IEEE80211_STA_DISABLE_160MHZ;
210 break;
211 case NL80211_CHAN_WIDTH_160:
212 /* n_P20 */
213 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
214 /* n_P80 */
215 tmp /= 4;
216 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
217 c->width = NL80211_CHAN_WIDTH_80;
218 ret = IEEE80211_STA_DISABLE_80P80MHZ |
219 IEEE80211_STA_DISABLE_160MHZ;
220 break;
221 default:
222 case NL80211_CHAN_WIDTH_20_NOHT:
223 WARN_ON_ONCE(1);
224 c->width = NL80211_CHAN_WIDTH_20_NOHT;
225 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
226 break;
227 }
228
229 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
230
231 return ret;
232}
233
234static u32
235ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
236 struct ieee80211_supported_band *sband,
237 struct ieee80211_channel *channel,
238 const struct ieee80211_ht_operation *ht_oper,
239 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100240 struct cfg80211_chan_def *chandef, bool verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100241{
242 struct cfg80211_chan_def vht_chandef;
243 u32 ht_cfreq, ret;
244
245 chandef->chan = channel;
246 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
247 chandef->center_freq1 = channel->center_freq;
248 chandef->center_freq2 = 0;
249
250 if (!ht_oper || !sband->ht_cap.ht_supported) {
251 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
252 goto out;
253 }
254
255 chandef->width = NL80211_CHAN_WIDTH_20;
256
257 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
258 channel->band);
259 /* check that channel matches the right operating channel */
260 if (channel->center_freq != ht_cfreq) {
261 /*
262 * It's possible that some APs are confused here;
263 * Netgear WNDR3700 sometimes reports 4 higher than
264 * the actual channel in association responses, but
265 * since we look at probe response/beacon data here
266 * it should be OK.
267 */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100268 if (verbose)
269 sdata_info(sdata,
270 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
271 channel->center_freq, ht_cfreq,
272 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100273 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
274 goto out;
275 }
276
277 /* check 40 MHz support, if we have it */
278 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
279 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
280 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
281 chandef->width = NL80211_CHAN_WIDTH_40;
282 chandef->center_freq1 += 10;
283 break;
284 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
285 chandef->width = NL80211_CHAN_WIDTH_40;
286 chandef->center_freq1 -= 10;
287 break;
288 }
289 } else {
290 /* 40 MHz (and 80 MHz) must be supported for VHT */
291 ret = IEEE80211_STA_DISABLE_VHT;
292 goto out;
293 }
294
295 if (!vht_oper || !sband->vht_cap.vht_supported) {
296 ret = IEEE80211_STA_DISABLE_VHT;
297 goto out;
298 }
299
300 vht_chandef.chan = channel;
301 vht_chandef.center_freq1 =
302 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
303 channel->band);
304 vht_chandef.center_freq2 = 0;
305
306 if (vht_oper->center_freq_seg2_idx)
307 vht_chandef.center_freq2 =
308 ieee80211_channel_to_frequency(
309 vht_oper->center_freq_seg2_idx,
310 channel->band);
311
312 switch (vht_oper->chan_width) {
313 case IEEE80211_VHT_CHANWIDTH_USE_HT:
314 vht_chandef.width = chandef->width;
315 break;
316 case IEEE80211_VHT_CHANWIDTH_80MHZ:
317 vht_chandef.width = NL80211_CHAN_WIDTH_80;
318 break;
319 case IEEE80211_VHT_CHANWIDTH_160MHZ:
320 vht_chandef.width = NL80211_CHAN_WIDTH_160;
321 break;
322 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
323 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
324 break;
325 default:
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100326 if (verbose)
327 sdata_info(sdata,
328 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
329 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100330 ret = IEEE80211_STA_DISABLE_VHT;
331 goto out;
332 }
333
334 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100335 if (verbose)
336 sdata_info(sdata,
337 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100338 ret = IEEE80211_STA_DISABLE_VHT;
339 goto out;
340 }
341
342 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
343 ret = 0;
344 goto out;
345 }
346
347 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100348 if (verbose)
349 sdata_info(sdata,
350 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100351 ret = IEEE80211_STA_DISABLE_VHT;
352 goto out;
353 }
354
355 *chandef = vht_chandef;
356
357 ret = 0;
358
359out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100360 /* don't print the message below for VHT mismatch if VHT is disabled */
361 if (ret & IEEE80211_STA_DISABLE_VHT)
362 vht_chandef = *chandef;
363
Johannes Berg6565ec92013-02-08 14:52:32 +0100364 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
365 IEEE80211_CHAN_DISABLED)) {
366 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
367 ret = IEEE80211_STA_DISABLE_HT |
368 IEEE80211_STA_DISABLE_VHT;
369 goto out;
370 }
371
372 ret |= chandef_downgrade(chandef);
373 }
374
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100375 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100376 sdata_info(sdata,
377 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
378
379 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
380 return ret;
381}
382
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100383static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
384 struct sta_info *sta,
385 const struct ieee80211_ht_operation *ht_oper,
386 const struct ieee80211_vht_operation *vht_oper,
387 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200388{
389 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100390 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200391 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200392 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100393 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200394 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100395 u32 flags;
396 enum ieee80211_sta_rx_bandwidth new_sta_bw;
397 int ret;
398
399 /* if HT was/is disabled, don't track any bandwidth changes */
400 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
401 return 0;
402
403 /* don't check VHT if we associated as non-VHT station */
404 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
405 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200406
Johannes Berg11289582013-02-07 17:36:12 +0100407 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100408 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100409
Johannes Bergf2d93302013-02-08 13:15:55 +0100410 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200411 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200412
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100413 /* calculate new channel (type) based on HT/VHT operation IEs */
414 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
415 vht_oper, &chandef, false);
416
417 /*
418 * Downgrade the new channel if we associated with restricted
419 * capabilities. For example, if we associated as a 20 MHz STA
420 * to a 40 MHz AP (due to regulatory, capabilities or config
421 * reasons) then switching to a 40 MHz channel now won't do us
422 * any good -- we couldn't use it with the AP.
423 */
424 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
425 chandef.width == NL80211_CHAN_WIDTH_80P80)
426 flags |= chandef_downgrade(&chandef);
427 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
428 chandef.width == NL80211_CHAN_WIDTH_160)
429 flags |= chandef_downgrade(&chandef);
430 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
431 chandef.width > NL80211_CHAN_WIDTH_20)
432 flags |= chandef_downgrade(&chandef);
433
434 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
435 return 0;
436
437 sdata_info(sdata,
438 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
439 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
440 chandef.center_freq1, chandef.center_freq2);
441
442 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
443 IEEE80211_STA_DISABLE_VHT |
444 IEEE80211_STA_DISABLE_40MHZ |
445 IEEE80211_STA_DISABLE_80P80MHZ |
446 IEEE80211_STA_DISABLE_160MHZ)) ||
447 !cfg80211_chandef_valid(&chandef)) {
448 sdata_info(sdata,
449 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
450 ifmgd->bssid);
451 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200452 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200453
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100454 switch (chandef.width) {
455 case NL80211_CHAN_WIDTH_20_NOHT:
456 case NL80211_CHAN_WIDTH_20:
457 new_sta_bw = IEEE80211_STA_RX_BW_20;
458 break;
459 case NL80211_CHAN_WIDTH_40:
460 new_sta_bw = IEEE80211_STA_RX_BW_40;
461 break;
462 case NL80211_CHAN_WIDTH_80:
463 new_sta_bw = IEEE80211_STA_RX_BW_80;
464 break;
465 case NL80211_CHAN_WIDTH_80P80:
466 case NL80211_CHAN_WIDTH_160:
467 new_sta_bw = IEEE80211_STA_RX_BW_160;
468 break;
469 default:
470 return -EINVAL;
471 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200472
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100473 if (new_sta_bw > sta->cur_max_bandwidth)
474 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200475
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100476 if (new_sta_bw < sta->sta.bandwidth) {
477 sta->sta.bandwidth = new_sta_bw;
478 rate_control_rate_update(local, sband, sta,
479 IEEE80211_RC_BW_CHANGED);
480 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530481
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100482 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
483 if (ret) {
484 sdata_info(sdata,
485 "AP %pM changed bandwidth to incompatible one - disconnect\n",
486 ifmgd->bssid);
487 return ret;
488 }
489
490 if (new_sta_bw > sta->sta.bandwidth) {
491 sta->sta.bandwidth = new_sta_bw;
492 rate_control_rate_update(local, sband, sta,
493 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200494 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200495
Johannes Berg074d46d2012-03-15 19:45:16 +0100496 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200497
498 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100499 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
500 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200501 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200502 }
503
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100504 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200505}
506
Johannes Berg9c6bd792008-09-11 00:01:52 +0200507/* frame sending functions */
508
Johannes Berg66e67e42012-01-20 13:55:27 +0100509static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
510 struct ieee80211_supported_band *sband,
511 u32 *rates)
512{
513 int i, j, count;
514 *rates = 0;
515 count = 0;
516 for (i = 0; i < supp_rates_len; i++) {
517 int rate = (supp_rates[i] & 0x7F) * 5;
518
519 for (j = 0; j < sband->n_bitrates; j++)
520 if (sband->bitrates[j].bitrate == rate) {
521 *rates |= BIT(j);
522 count++;
523 break;
524 }
525 }
526
527 return count;
528}
529
530static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200531 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100532 struct ieee80211_supported_band *sband,
533 struct ieee80211_channel *channel,
534 enum ieee80211_smps_mode smps)
535{
Johannes Berg66e67e42012-01-20 13:55:27 +0100536 u8 *pos;
537 u32 flags = channel->flags;
538 u16 cap;
539 struct ieee80211_sta_ht_cap ht_cap;
540
541 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
542
Johannes Berg66e67e42012-01-20 13:55:27 +0100543 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
544 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
545
Johannes Berg66e67e42012-01-20 13:55:27 +0100546 /* determine capability flags */
547 cap = ht_cap.cap;
548
Johannes Berg9dde6422012-05-16 23:43:19 +0200549 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100550 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
551 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
552 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
553 cap &= ~IEEE80211_HT_CAP_SGI_40;
554 }
555 break;
556 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
557 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
558 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
559 cap &= ~IEEE80211_HT_CAP_SGI_40;
560 }
561 break;
562 }
563
Johannes Berg24398e32012-03-28 10:58:36 +0200564 /*
565 * If 40 MHz was disabled associate as though we weren't
566 * capable of 40 MHz -- some broken APs will never fall
567 * back to trying to transmit in 20 MHz.
568 */
569 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
570 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
571 cap &= ~IEEE80211_HT_CAP_SGI_40;
572 }
573
Johannes Berg66e67e42012-01-20 13:55:27 +0100574 /* set SM PS mode properly */
575 cap &= ~IEEE80211_HT_CAP_SM_PS;
576 switch (smps) {
577 case IEEE80211_SMPS_AUTOMATIC:
578 case IEEE80211_SMPS_NUM_MODES:
579 WARN_ON(1);
580 case IEEE80211_SMPS_OFF:
581 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
582 IEEE80211_HT_CAP_SM_PS_SHIFT;
583 break;
584 case IEEE80211_SMPS_STATIC:
585 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
586 IEEE80211_HT_CAP_SM_PS_SHIFT;
587 break;
588 case IEEE80211_SMPS_DYNAMIC:
589 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
590 IEEE80211_HT_CAP_SM_PS_SHIFT;
591 break;
592 }
593
594 /* reserve and fill IE */
595 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
596 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
597}
598
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
600 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100601 struct ieee80211_supported_band *sband,
602 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000603{
604 u8 *pos;
605 u32 cap;
606 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100607 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000608
609 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
610
611 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100612 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000613
614 /* determine capability flags */
615 cap = vht_cap.cap;
616
Johannes Bergf2d9d272012-11-22 14:11:39 +0100617 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
618 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
619 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
620 }
621
622 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
623 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
624 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
625 }
626
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100627 /*
628 * Some APs apparently get confused if our capabilities are better
629 * than theirs, so restrict what we advertise in the assoc request.
630 */
631 if (!(ap_vht_cap->vht_cap_info &
632 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
633 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
634
635 if (!(ap_vht_cap->vht_cap_info &
636 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
637 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
638 IEEE80211_VHT_CAP_RXSTBC_3 |
639 IEEE80211_VHT_CAP_RXSTBC_4);
640
641 for (i = 0; i < 8; i++) {
642 int shift = i * 2;
643 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
644 u16 ap_mcs, our_mcs;
645
646 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
647 mask) >> shift;
648 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
649 mask) >> shift;
650
Johannes Berg24af7172013-03-01 17:33:18 +0100651 if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
652 continue;
653
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100654 switch (ap_mcs) {
655 default:
656 if (our_mcs <= ap_mcs)
657 break;
658 /* fall through */
659 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
660 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
661 vht_cap.vht_mcs.rx_mcs_map |=
662 cpu_to_le16(ap_mcs << shift);
663 }
664 }
665
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000666 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000667 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000668 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
669}
670
Johannes Berg66e67e42012-01-20 13:55:27 +0100671static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
672{
673 struct ieee80211_local *local = sdata->local;
674 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
675 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
676 struct sk_buff *skb;
677 struct ieee80211_mgmt *mgmt;
678 u8 *pos, qos_info;
679 size_t offset = 0, noffset;
680 int i, count, rates_len, supp_rates_len;
681 u16 capab;
682 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200683 struct ieee80211_chanctx_conf *chanctx_conf;
684 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100685 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100686
687 lockdep_assert_held(&ifmgd->mtx);
688
Johannes Berg55de9082012-07-26 17:24:39 +0200689 rcu_read_lock();
690 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
691 if (WARN_ON(!chanctx_conf)) {
692 rcu_read_unlock();
693 return;
694 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100695 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200696 rcu_read_unlock();
697 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100698
699 if (assoc_data->supp_rates_len) {
700 /*
701 * Get all rates supported by the device and the AP as
702 * some APs don't like getting a superset of their rates
703 * in the association request (e.g. D-Link DAP 1353 in
704 * b-only mode)...
705 */
706 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
707 assoc_data->supp_rates_len,
708 sband, &rates);
709 } else {
710 /*
711 * In case AP not provide any supported rates information
712 * before association, we send information element(s) with
713 * all rates that we support.
714 */
715 rates = ~0;
716 rates_len = sband->n_bitrates;
717 }
718
719 skb = alloc_skb(local->hw.extra_tx_headroom +
720 sizeof(*mgmt) + /* bit too much but doesn't matter */
721 2 + assoc_data->ssid_len + /* SSID */
722 4 + rates_len + /* (extended) rates */
723 4 + /* power capability */
724 2 + 2 * sband->n_channels + /* supported channels */
725 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000726 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100727 assoc_data->ie_len + /* extra IEs */
728 9, /* WMM */
729 GFP_KERNEL);
730 if (!skb)
731 return;
732
733 skb_reserve(skb, local->hw.extra_tx_headroom);
734
735 capab = WLAN_CAPABILITY_ESS;
736
737 if (sband->band == IEEE80211_BAND_2GHZ) {
738 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
739 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
740 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
741 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
742 }
743
744 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
745 capab |= WLAN_CAPABILITY_PRIVACY;
746
747 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
748 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
749 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
750
751 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
752 memset(mgmt, 0, 24);
753 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
754 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
755 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
756
757 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
758 skb_put(skb, 10);
759 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
760 IEEE80211_STYPE_REASSOC_REQ);
761 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
762 mgmt->u.reassoc_req.listen_interval =
763 cpu_to_le16(local->hw.conf.listen_interval);
764 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
765 ETH_ALEN);
766 } else {
767 skb_put(skb, 4);
768 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
769 IEEE80211_STYPE_ASSOC_REQ);
770 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
771 mgmt->u.assoc_req.listen_interval =
772 cpu_to_le16(local->hw.conf.listen_interval);
773 }
774
775 /* SSID */
776 pos = skb_put(skb, 2 + assoc_data->ssid_len);
777 *pos++ = WLAN_EID_SSID;
778 *pos++ = assoc_data->ssid_len;
779 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
780
781 /* add all rates which were marked to be used above */
782 supp_rates_len = rates_len;
783 if (supp_rates_len > 8)
784 supp_rates_len = 8;
785
786 pos = skb_put(skb, supp_rates_len + 2);
787 *pos++ = WLAN_EID_SUPP_RATES;
788 *pos++ = supp_rates_len;
789
790 count = 0;
791 for (i = 0; i < sband->n_bitrates; i++) {
792 if (BIT(i) & rates) {
793 int rate = sband->bitrates[i].bitrate;
794 *pos++ = (u8) (rate / 5);
795 if (++count == 8)
796 break;
797 }
798 }
799
800 if (rates_len > count) {
801 pos = skb_put(skb, rates_len - count + 2);
802 *pos++ = WLAN_EID_EXT_SUPP_RATES;
803 *pos++ = rates_len - count;
804
805 for (i++; i < sband->n_bitrates; i++) {
806 if (BIT(i) & rates) {
807 int rate = sband->bitrates[i].bitrate;
808 *pos++ = (u8) (rate / 5);
809 }
810 }
811 }
812
813 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
814 /* 1. power capabilities */
815 pos = skb_put(skb, 4);
816 *pos++ = WLAN_EID_PWR_CAPABILITY;
817 *pos++ = 2;
818 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200819 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100820
821 /* 2. supported channels */
822 /* TODO: get this in reg domain format */
823 pos = skb_put(skb, 2 * sband->n_channels + 2);
824 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
825 *pos++ = 2 * sband->n_channels;
826 for (i = 0; i < sband->n_channels; i++) {
827 *pos++ = ieee80211_frequency_to_channel(
828 sband->channels[i].center_freq);
829 *pos++ = 1; /* one channel in the subband*/
830 }
831 }
832
833 /* if present, add any custom IEs that go before HT */
834 if (assoc_data->ie_len && assoc_data->ie) {
835 static const u8 before_ht[] = {
836 WLAN_EID_SSID,
837 WLAN_EID_SUPP_RATES,
838 WLAN_EID_EXT_SUPP_RATES,
839 WLAN_EID_PWR_CAPABILITY,
840 WLAN_EID_SUPPORTED_CHANNELS,
841 WLAN_EID_RSN,
842 WLAN_EID_QOS_CAPA,
843 WLAN_EID_RRM_ENABLED_CAPABILITIES,
844 WLAN_EID_MOBILITY_DOMAIN,
845 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
846 };
847 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
848 before_ht, ARRAY_SIZE(before_ht),
849 offset);
850 pos = skb_put(skb, noffset - offset);
851 memcpy(pos, assoc_data->ie + offset, noffset - offset);
852 offset = noffset;
853 }
854
Johannes Bergf2d9d272012-11-22 14:11:39 +0100855 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
856 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
857 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
858
Johannes Berga8243b72012-11-22 14:32:09 +0100859 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200860 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200861 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100862
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000863 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100864 ieee80211_add_vht_ie(sdata, skb, sband,
865 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000866
Johannes Berg66e67e42012-01-20 13:55:27 +0100867 /* if present, add any custom non-vendor IEs that go after HT */
868 if (assoc_data->ie_len && assoc_data->ie) {
869 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
870 assoc_data->ie_len,
871 offset);
872 pos = skb_put(skb, noffset - offset);
873 memcpy(pos, assoc_data->ie + offset, noffset - offset);
874 offset = noffset;
875 }
876
Johannes Berg76f03032012-03-08 15:02:05 +0100877 if (assoc_data->wmm) {
878 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200879 qos_info = ifmgd->uapsd_queues;
880 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100881 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
882 } else {
883 qos_info = 0;
884 }
885
886 pos = skb_put(skb, 9);
887 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
888 *pos++ = 7; /* len */
889 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
890 *pos++ = 0x50;
891 *pos++ = 0xf2;
892 *pos++ = 2; /* WME */
893 *pos++ = 0; /* WME info */
894 *pos++ = 1; /* WME ver */
895 *pos++ = qos_info;
896 }
897
898 /* add any remaining custom (i.e. vendor specific here) IEs */
899 if (assoc_data->ie_len && assoc_data->ie) {
900 noffset = assoc_data->ie_len;
901 pos = skb_put(skb, noffset - offset);
902 memcpy(pos, assoc_data->ie + offset, noffset - offset);
903 }
904
Johannes Berga1845fc2012-06-27 13:18:36 +0200905 drv_mgd_prepare_tx(local, sdata);
906
Johannes Berg66e67e42012-01-20 13:55:27 +0100907 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100908 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
909 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
910 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100911 ieee80211_tx_skb(sdata, skb);
912}
913
Kalle Valo572e0012009-02-10 17:09:31 +0200914void ieee80211_send_pspoll(struct ieee80211_local *local,
915 struct ieee80211_sub_if_data *sdata)
916{
Kalle Valo572e0012009-02-10 17:09:31 +0200917 struct ieee80211_pspoll *pspoll;
918 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200919
Kalle Valod8cd1892010-01-05 20:16:26 +0200920 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
921 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200922 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200923
Kalle Valod8cd1892010-01-05 20:16:26 +0200924 pspoll = (struct ieee80211_pspoll *) skb->data;
925 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200926
Johannes Berg62ae67b2009-11-18 18:42:05 +0100927 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
928 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200929}
930
Johannes Berg965beda2009-04-16 13:17:24 +0200931void ieee80211_send_nullfunc(struct ieee80211_local *local,
932 struct ieee80211_sub_if_data *sdata,
933 int powersave)
934{
935 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200936 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530937 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200938
Kalle Valod8cd1892010-01-05 20:16:26 +0200939 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
940 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200941 return;
942
Kalle Valod8cd1892010-01-05 20:16:26 +0200943 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200944 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200945 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200946
Seth Forshee6c17b772013-02-11 11:21:07 -0600947 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
948 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530949 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
950 IEEE80211_STA_CONNECTION_POLL))
951 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
952
Johannes Berg62ae67b2009-11-18 18:42:05 +0100953 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200954}
955
Felix Fietkaud5242152010-01-08 18:06:26 +0100956static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
957 struct ieee80211_sub_if_data *sdata)
958{
959 struct sk_buff *skb;
960 struct ieee80211_hdr *nullfunc;
961 __le16 fc;
962
963 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
964 return;
965
966 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700967 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100968 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700969
Felix Fietkaud5242152010-01-08 18:06:26 +0100970 skb_reserve(skb, local->hw.extra_tx_headroom);
971
972 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
973 memset(nullfunc, 0, 30);
974 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
975 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
976 nullfunc->frame_control = fc;
977 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
978 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
979 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
980 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
981
982 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
983 ieee80211_tx_skb(sdata, skb);
984}
985
Johannes Bergcc32abd2009-05-15 11:52:31 +0200986/* spectrum management related things */
987static void ieee80211_chswitch_work(struct work_struct *work)
988{
989 struct ieee80211_sub_if_data *sdata =
990 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200991 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
992
Johannes Berg9607e6b2009-12-23 13:15:31 +0100993 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200994 return;
995
Johannes Berg77fdaa12009-07-07 03:45:17 +0200996 mutex_lock(&ifmgd->mtx);
997 if (!ifmgd->associated)
998 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200999
Johannes Berg55de9082012-07-26 17:24:39 +02001000 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001001 if (!sdata->local->ops->channel_switch) {
1002 /* call "hw_config" only if doing sw channel switch */
1003 ieee80211_hw_config(sdata->local,
1004 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +03001005 } else {
1006 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +02001007 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001008 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001009
Johannes Berg77fdaa12009-07-07 03:45:17 +02001010 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +02001011 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001012
Johannes Berg57eebdf2012-08-01 15:50:46 +02001013 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001014 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1015 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001016 out:
1017 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1018 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001019}
1020
Johannes Berg5ce6e432010-05-11 16:20:57 +02001021void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1022{
Johannes Berg55de9082012-07-26 17:24:39 +02001023 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1024 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001025
1026 trace_api_chswitch_done(sdata, success);
1027 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001028 sdata_info(sdata,
1029 "driver channel switch failed, disconnecting\n");
1030 ieee80211_queue_work(&sdata->local->hw,
1031 &ifmgd->csa_connection_drop_work);
1032 } else {
1033 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001034 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001035}
1036EXPORT_SYMBOL(ieee80211_chswitch_done);
1037
Johannes Bergcc32abd2009-05-15 11:52:31 +02001038static void ieee80211_chswitch_timer(unsigned long data)
1039{
1040 struct ieee80211_sub_if_data *sdata =
1041 (struct ieee80211_sub_if_data *) data;
1042 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1043
Johannes Berg5bb644a2009-05-17 11:40:42 +02001044 if (sdata->local->quiescing) {
1045 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
1046 return;
1047 }
1048
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001049 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001050}
1051
Johannes Berg4a3cb702013-02-12 16:43:19 +01001052void
1053ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1054 const struct ieee80211_channel_sw_ie *sw_elem,
1055 struct ieee80211_bss *bss, u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001056{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001057 struct cfg80211_bss *cbss =
1058 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001059 struct ieee80211_channel *new_ch;
1060 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001061 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1062 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +02001063 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001064
Johannes Berg77fdaa12009-07-07 03:45:17 +02001065 ASSERT_MGD_MTX(ifmgd);
1066
1067 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001068 return;
1069
Helmut Schaafbe9c422009-07-23 12:14:04 +02001070 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001071 return;
1072
1073 /* Disregard subsequent beacons if we are already running a timer
1074 processing a CSA */
1075
1076 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1077 return;
1078
1079 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001080 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1081 sdata_info(sdata,
1082 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1083 ifmgd->associated->bssid, new_freq);
1084 ieee80211_queue_work(&sdata->local->hw,
1085 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001086 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001087 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001088
Johannes Berg57eebdf2012-08-01 15:50:46 +02001089 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1090
Johannes Berg55de9082012-07-26 17:24:39 +02001091 if (sdata->local->use_chanctx) {
1092 sdata_info(sdata,
1093 "not handling channel switch with channel contexts\n");
1094 ieee80211_queue_work(&sdata->local->hw,
1095 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001096 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001097 }
1098
1099 mutex_lock(&sdata->local->chanctx_mtx);
1100 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1101 mutex_unlock(&sdata->local->chanctx_mtx);
1102 return;
1103 }
1104 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1105 struct ieee80211_chanctx, conf);
1106 if (chanctx->refcount > 1) {
1107 sdata_info(sdata,
1108 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1109 ieee80211_queue_work(&sdata->local->hw,
1110 &ifmgd->csa_connection_drop_work);
1111 mutex_unlock(&sdata->local->chanctx_mtx);
1112 return;
1113 }
1114 mutex_unlock(&sdata->local->chanctx_mtx);
1115
1116 sdata->local->csa_channel = new_ch;
1117
Johannes Berg57eebdf2012-08-01 15:50:46 +02001118 if (sw_elem->mode)
1119 ieee80211_stop_queues_by_reason(&sdata->local->hw,
1120 IEEE80211_QUEUE_STOP_REASON_CSA);
1121
Johannes Berg5ce6e432010-05-11 16:20:57 +02001122 if (sdata->local->ops->channel_switch) {
1123 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001124 struct ieee80211_channel_switch ch_switch = {
1125 .timestamp = timestamp,
1126 .block_tx = sw_elem->mode,
1127 .channel = new_ch,
1128 .count = sw_elem->count,
1129 };
1130
Johannes Berg5ce6e432010-05-11 16:20:57 +02001131 drv_channel_switch(sdata->local, &ch_switch);
1132 return;
1133 }
1134
1135 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001136 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001137 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001138 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001139 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +02001140 TU_TO_EXP_TIME(sw_elem->count *
1141 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001142}
1143
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001144static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1145 struct ieee80211_channel *channel,
1146 const u8 *country_ie, u8 country_ie_len,
1147 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001148{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001149 struct ieee80211_country_ie_triplet *triplet;
1150 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1151 int i, chan_pwr, chan_increment, new_ap_level;
1152 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001153
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001154 /* Invalid IE */
1155 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001156 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001157
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001158 triplet = (void *)(country_ie + 3);
1159 country_ie_len -= 3;
1160
1161 switch (channel->band) {
1162 default:
1163 WARN_ON_ONCE(1);
1164 /* fall through */
1165 case IEEE80211_BAND_2GHZ:
1166 case IEEE80211_BAND_60GHZ:
1167 chan_increment = 1;
1168 break;
1169 case IEEE80211_BAND_5GHZ:
1170 chan_increment = 4;
1171 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001172 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001173
1174 /* find channel */
1175 while (country_ie_len >= 3) {
1176 u8 first_channel = triplet->chans.first_channel;
1177
1178 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1179 goto next;
1180
1181 for (i = 0; i < triplet->chans.num_channels; i++) {
1182 if (first_channel + i * chan_increment == chan) {
1183 have_chan_pwr = true;
1184 chan_pwr = triplet->chans.max_power;
1185 break;
1186 }
1187 }
1188 if (have_chan_pwr)
1189 break;
1190
1191 next:
1192 triplet++;
1193 country_ie_len -= 3;
1194 }
1195
1196 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001197 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001198
1199 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1200
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001201 if (sdata->ap_power_level == new_ap_level)
1202 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001203
1204 sdata_info(sdata,
1205 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1206 new_ap_level, chan_pwr, *pwr_constr_elem,
1207 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001208 sdata->ap_power_level = new_ap_level;
1209 if (__ieee80211_recalc_txpower(sdata))
1210 return BSS_CHANGED_TXPOWER;
1211 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001212}
1213
Johannes Berg965beda2009-04-16 13:17:24 +02001214/* powersave */
1215static void ieee80211_enable_ps(struct ieee80211_local *local,
1216 struct ieee80211_sub_if_data *sdata)
1217{
1218 struct ieee80211_conf *conf = &local->hw.conf;
1219
Johannes Bergd5edaed2009-04-22 23:02:51 +02001220 /*
1221 * If we are scanning right now then the parameters will
1222 * take effect when scan finishes.
1223 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001224 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001225 return;
1226
Johannes Berg965beda2009-04-16 13:17:24 +02001227 if (conf->dynamic_ps_timeout > 0 &&
1228 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1229 mod_timer(&local->dynamic_ps_timer, jiffies +
1230 msecs_to_jiffies(conf->dynamic_ps_timeout));
1231 } else {
1232 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1233 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301234
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001235 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1236 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1237 return;
1238
1239 conf->flags |= IEEE80211_CONF_PS;
1240 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001241 }
1242}
1243
1244static void ieee80211_change_ps(struct ieee80211_local *local)
1245{
1246 struct ieee80211_conf *conf = &local->hw.conf;
1247
1248 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001249 ieee80211_enable_ps(local, local->ps_sdata);
1250 } else if (conf->flags & IEEE80211_CONF_PS) {
1251 conf->flags &= ~IEEE80211_CONF_PS;
1252 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1253 del_timer_sync(&local->dynamic_ps_timer);
1254 cancel_work_sync(&local->dynamic_ps_enable_work);
1255 }
1256}
1257
Jason Young808118c2011-03-10 16:43:19 -08001258static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1259{
1260 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1261 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001262 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001263
1264 if (!mgd->powersave)
1265 return false;
1266
Johannes Berg05cb9102011-10-28 11:59:47 +02001267 if (mgd->broken_ap)
1268 return false;
1269
Jason Young808118c2011-03-10 16:43:19 -08001270 if (!mgd->associated)
1271 return false;
1272
Jason Young808118c2011-03-10 16:43:19 -08001273 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1274 IEEE80211_STA_CONNECTION_POLL))
1275 return false;
1276
1277 rcu_read_lock();
1278 sta = sta_info_get(sdata, mgd->bssid);
1279 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001280 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001281 rcu_read_unlock();
1282
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001283 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001284}
1285
Johannes Berg965beda2009-04-16 13:17:24 +02001286/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001287void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001288{
1289 struct ieee80211_sub_if_data *sdata, *found = NULL;
1290 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001291 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001292
1293 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1294 local->ps_sdata = NULL;
1295 return;
1296 }
1297
1298 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001299 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001300 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301301 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1302 /* If an AP vif is found, then disable PS
1303 * by setting the count to zero thereby setting
1304 * ps_sdata to NULL.
1305 */
1306 count = 0;
1307 break;
1308 }
Johannes Berg965beda2009-04-16 13:17:24 +02001309 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1310 continue;
1311 found = sdata;
1312 count++;
1313 }
1314
Jason Young808118c2011-03-10 16:43:19 -08001315 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001316 s32 beaconint_us;
1317
1318 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001319 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001320
1321 beaconint_us = ieee80211_tu_to_usec(
1322 found->vif.bss_conf.beacon_int);
1323
Juuso Oikarinenff616382010-06-09 09:51:52 +03001324 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001325 if (timeout < 0) {
1326 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001327 * Go to full PSM if the user configures a very low
1328 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001329 * The 2000 second value is there for compatibility
1330 * until the PM_QOS_NETWORK_LATENCY is configured
1331 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001332 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001333 if (latency > (1900 * USEC_PER_MSEC) &&
1334 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001335 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001336 else
1337 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001338 }
Johannes Berg09b85562013-02-06 23:07:41 +01001339 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001340
Johannes Berg04fe2032009-04-22 18:44:37 +02001341 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001342 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001343 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001344 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001345 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001346
1347 /* If the TIM IE is invalid, pretend the value is 1 */
1348 if (!dtimper)
1349 dtimper = 1;
1350 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001351 maxslp = min_t(int, dtimper,
1352 latency / beaconint_us);
1353
Johannes Berg9ccebe62009-04-23 10:32:36 +02001354 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001355 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001356 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001357 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001358 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001359 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001360 }
Johannes Berg965beda2009-04-16 13:17:24 +02001361
1362 ieee80211_change_ps(local);
1363}
1364
Eliad Pellerab095872012-07-27 12:33:22 +03001365void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1366{
1367 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1368
1369 if (sdata->vif.bss_conf.ps != ps_allowed) {
1370 sdata->vif.bss_conf.ps = ps_allowed;
1371 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1372 }
1373}
1374
Johannes Berg965beda2009-04-16 13:17:24 +02001375void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1376{
1377 struct ieee80211_local *local =
1378 container_of(work, struct ieee80211_local,
1379 dynamic_ps_disable_work);
1380
1381 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1382 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1383 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1384 }
1385
1386 ieee80211_wake_queues_by_reason(&local->hw,
1387 IEEE80211_QUEUE_STOP_REASON_PS);
1388}
1389
1390void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1391{
1392 struct ieee80211_local *local =
1393 container_of(work, struct ieee80211_local,
1394 dynamic_ps_enable_work);
1395 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001396 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301397 unsigned long flags;
1398 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001399
1400 /* can only happen when PS was just disabled anyway */
1401 if (!sdata)
1402 return;
1403
Christian Lamparter5e340692011-06-30 21:08:43 +02001404 ifmgd = &sdata->u.mgd;
1405
Johannes Berg965beda2009-04-16 13:17:24 +02001406 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1407 return;
1408
Johannes Berg09b85562013-02-06 23:07:41 +01001409 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001410 /* don't enter PS if TX frames are pending */
1411 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301412 mod_timer(&local->dynamic_ps_timer, jiffies +
1413 msecs_to_jiffies(
1414 local->hw.conf.dynamic_ps_timeout));
1415 return;
1416 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001417
1418 /*
1419 * transmission can be stopped by others which leads to
1420 * dynamic_ps_timer expiry. Postpone the ps timer if it
1421 * is not the actual idle state.
1422 */
1423 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1424 for (q = 0; q < local->hw.queues; q++) {
1425 if (local->queue_stop_reasons[q]) {
1426 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1427 flags);
1428 mod_timer(&local->dynamic_ps_timer, jiffies +
1429 msecs_to_jiffies(
1430 local->hw.conf.dynamic_ps_timeout));
1431 return;
1432 }
1433 }
1434 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301435 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301436
Vivek Natarajan375177b2010-02-09 14:50:28 +05301437 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301438 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301439 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001440
Vivek Natarajane8306f92011-04-06 11:41:10 +05301441 if (drv_tx_frames_pending(local))
1442 mod_timer(&local->dynamic_ps_timer, jiffies +
1443 msecs_to_jiffies(
1444 local->hw.conf.dynamic_ps_timeout));
1445 else {
1446 ieee80211_send_nullfunc(local, sdata, 1);
1447 /* Flush to get the tx status of nullfunc frame */
1448 drv_flush(local, false);
1449 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301450 }
1451
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001452 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1453 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301454 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1455 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1456 local->hw.conf.flags |= IEEE80211_CONF_PS;
1457 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1458 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301459
Arik Nemtsov77b70232011-06-26 12:06:54 +03001460 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1461 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001462}
1463
1464void ieee80211_dynamic_ps_timer(unsigned long data)
1465{
1466 struct ieee80211_local *local = (void *) data;
1467
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001468 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001469 return;
1470
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001471 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001472}
1473
Simon Wunderlich164eb022013-02-08 18:16:20 +01001474void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1475{
1476 struct delayed_work *delayed_work =
1477 container_of(work, struct delayed_work, work);
1478 struct ieee80211_sub_if_data *sdata =
1479 container_of(delayed_work, struct ieee80211_sub_if_data,
1480 dfs_cac_timer_work);
1481
1482 ieee80211_vif_release_channel(sdata);
1483
1484 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1485}
1486
Johannes Berg60f8b392008-09-08 17:44:22 +02001487/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001488static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001489 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001490 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001491{
Jiri Bencf0706e82007-05-05 11:45:53 -07001492 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001493 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001494 size_t left;
1495 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001496 const u8 *pos;
1497 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001498
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001499 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001500 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001501
Johannes Berg32c50572012-03-28 11:04:29 +02001502 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001503 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001504
1505 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001506 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001507
Jiri Bencf0706e82007-05-05 11:45:53 -07001508 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001509 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001510
1511 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001512 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001513
Jiri Bencf0706e82007-05-05 11:45:53 -07001514 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001515 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001516 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001517 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001518
1519 pos = wmm_param + 8;
1520 left = wmm_param_len - 8;
1521
1522 memset(&params, 0, sizeof(params));
1523
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001524 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001525 for (; left >= 4; left -= 4, pos += 4) {
1526 int aci = (pos[0] >> 5) & 0x03;
1527 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001528 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001529 int queue;
1530
1531 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001532 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001533 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001534 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001535 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001536 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1537 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001538 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001539 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001540 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001541 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001542 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001543 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1544 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001545 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001546 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001547 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001548 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001549 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001550 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1551 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001552 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001553 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001554 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001555 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001556 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001557 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001558 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1559 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001560 break;
1561 }
1562
1563 params.aifs = pos[0] & 0x0f;
1564 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1565 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001566 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001567 params.uapsd = uapsd;
1568
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001569 mlme_dbg(sdata,
1570 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1571 queue, aci, acm,
1572 params.aifs, params.cw_min, params.cw_max,
1573 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001574 sdata->tx_conf[queue] = params;
1575 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001576 sdata_err(sdata,
1577 "failed to set TX queue parameters for queue %d\n",
1578 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001579 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001580
1581 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001582 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001583 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001584}
1585
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001586static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1587{
1588 lockdep_assert_held(&sdata->local->mtx);
1589
1590 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1591 IEEE80211_STA_BEACON_POLL);
1592 ieee80211_run_deferred_scan(sdata->local);
1593}
1594
1595static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1596{
1597 mutex_lock(&sdata->local->mtx);
1598 __ieee80211_stop_poll(sdata);
1599 mutex_unlock(&sdata->local->mtx);
1600}
1601
Johannes Berg7a5158e2008-10-08 10:59:33 +02001602static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1603 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001604{
Johannes Bergbda39332008-10-11 01:51:51 +02001605 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001606 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001607 bool use_protection;
1608 bool use_short_preamble;
1609 bool use_short_slot;
1610
1611 if (erp_valid) {
1612 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1613 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1614 } else {
1615 use_protection = false;
1616 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1617 }
1618
1619 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001620 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001621 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001622
Johannes Berg471b3ef2007-12-28 14:32:58 +01001623 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001624 bss_conf->use_cts_prot = use_protection;
1625 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001626 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001627
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001628 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001629 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001630 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001631 }
Daniel Draked9430a32007-07-27 15:43:24 +02001632
Johannes Berg7a5158e2008-10-08 10:59:33 +02001633 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001634 bss_conf->use_short_slot = use_short_slot;
1635 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001636 }
1637
1638 return changed;
1639}
1640
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001641static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001642 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001643 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001644{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001645 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001646 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001647 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001648
Johannes Bergae5eb022008-10-14 16:58:37 +02001649 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001650 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001651 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001652
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001653 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1654 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1655
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001656 sdata->u.mgd.associated = cbss;
1657 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001658
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001659 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1660
Johannes Berg488dd7b2012-10-29 20:08:01 +01001661 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001662 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001663
Johannes Berg9caf0362012-11-29 01:25:20 +01001664 rcu_read_lock();
1665 ies = rcu_dereference(cbss->ies);
1666 if (ies) {
1667 u8 noa[2];
1668 int ret;
1669
1670 ret = cfg80211_get_p2p_attr(
1671 ies->data, ies->len,
1672 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1673 noa, sizeof(noa));
1674 if (ret >= 2) {
1675 bss_conf->p2p_oppps = noa[1] & 0x80;
1676 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1677 bss_info_changed |= BSS_CHANGED_P2P_PS;
1678 sdata->u.mgd.p2p_noa_index = noa[0];
1679 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001680 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001681 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001682 }
1683
Johannes Bergb291ba12009-07-10 15:29:03 +02001684 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001685 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001686
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001687 ieee80211_led_assoc(local, 1);
1688
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001689 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001690 /*
1691 * If the AP is buggy we may get here with no DTIM period
1692 * known, so assume it's 1 which is the only safe assumption
1693 * in that case, although if the TIM IE is broken powersave
1694 * probably just won't work at all.
1695 */
1696 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001697 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001698 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001699 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001700 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001701
Juuso Oikarinen68542962010-06-09 13:43:26 +03001702 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001703
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001704 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001705 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001706 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001707 bss_info_changed |= BSS_CHANGED_CQM;
1708
Juuso Oikarinen68542962010-06-09 13:43:26 +03001709 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001710 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001711 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001712
Johannes Bergae5eb022008-10-14 16:58:37 +02001713 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001714
Johannes Berg056508d2009-07-30 21:43:55 +02001715 mutex_lock(&local->iflist_mtx);
1716 ieee80211_recalc_ps(local, -1);
1717 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001718
Johannes Berg04ecd252012-09-11 14:34:12 +02001719 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001720 ieee80211_recalc_ps_vif(sdata);
1721
John W. Linville8a5b33f2010-01-06 15:39:39 -05001722 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001723 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001724}
1725
Jouni Malinene69e95d2010-03-29 23:29:31 -07001726static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001727 u16 stype, u16 reason, bool tx,
1728 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001729{
Johannes Berg46900292009-02-15 12:44:28 +01001730 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001731 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001732 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001733
Johannes Berg77fdaa12009-07-07 03:45:17 +02001734 ASSERT_MGD_MTX(ifmgd);
1735
Johannes Berg37ad3882012-02-24 13:50:54 +01001736 if (WARN_ON_ONCE(tx && !frame_buf))
1737 return;
1738
Johannes Bergb291ba12009-07-10 15:29:03 +02001739 if (WARN_ON(!ifmgd->associated))
1740 return;
1741
David Spinadel79543d82012-06-12 09:59:45 +03001742 ieee80211_stop_poll(sdata);
1743
Johannes Berg77fdaa12009-07-07 03:45:17 +02001744 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001745
1746 /*
1747 * we need to commit the associated = NULL change because the
1748 * scan code uses that to determine whether this iface should
1749 * go to/wake up from powersave or not -- and could otherwise
1750 * wake the queues erroneously.
1751 */
1752 smp_mb();
1753
1754 /*
1755 * Thus, we can only afterwards stop the queues -- to account
1756 * for the case where another CPU is finishing a scan at this
1757 * time -- we don't want the scan code to enable queues.
1758 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001759
John W. Linville8a5b33f2010-01-06 15:39:39 -05001760 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001761 netif_carrier_off(sdata->dev);
1762
Eliad Peller88bc40e2012-07-12 17:35:33 +03001763 /*
1764 * if we want to get out of ps before disassoc (why?) we have
1765 * to do it before sending disassoc, as otherwise the null-packet
1766 * won't be valid.
1767 */
1768 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1769 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1770 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1771 }
1772 local->ps_sdata = NULL;
1773
Eliad Pellerab095872012-07-27 12:33:22 +03001774 /* disable per-vif ps */
1775 ieee80211_recalc_ps_vif(sdata);
1776
Eliad Pellerf8239812012-06-27 14:18:22 +03001777 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1778 if (tx)
1779 drv_flush(local, false);
1780
Johannes Berg37ad3882012-02-24 13:50:54 +01001781 /* deauthenticate/disassociate now */
1782 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001783 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1784 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001785
1786 /* flush out frame */
1787 if (tx)
1788 drv_flush(local, false);
1789
Eliad Peller88a9e312012-06-01 11:14:03 +03001790 /* clear bssid only after building the needed mgmt frames */
1791 memset(ifmgd->bssid, 0, ETH_ALEN);
1792
Johannes Berg37ad3882012-02-24 13:50:54 +01001793 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001794 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001795
1796 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001797 changed |= ieee80211_reset_erp_info(sdata);
1798
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001799 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001800 changed |= BSS_CHANGED_ASSOC;
1801 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001802
Johannes Berg488dd7b2012-10-29 20:08:01 +01001803 sdata->vif.bss_conf.p2p_ctwindow = 0;
1804 sdata->vif.bss_conf.p2p_oppps = false;
1805
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001806 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001807 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1808 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001809 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1810 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001811
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001812 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301813
Kalle Valo520eb822008-12-18 23:35:27 +02001814 del_timer_sync(&local->dynamic_ps_timer);
1815 cancel_work_sync(&local->dynamic_ps_enable_work);
1816
Juuso Oikarinen68542962010-06-09 13:43:26 +03001817 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001818 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001819 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001820
Johannes Berg3abead52012-03-02 15:56:59 +01001821 sdata->vif.bss_conf.qos = false;
1822 changed |= BSS_CHANGED_QOS;
1823
Johannes Berg0aaffa92010-05-05 15:28:27 +02001824 /* The BSSID (not really interesting) and HT changed */
1825 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001826 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001827
Johannes Berg3abead52012-03-02 15:56:59 +01001828 /* disassociated - set to defaults now */
1829 ieee80211_set_wmm_default(sdata, false);
1830
Johannes Bergb9dcf712010-08-27 12:35:54 +02001831 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1832 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1833 del_timer_sync(&sdata->u.mgd.timer);
1834 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001835
1836 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001837
Johannes Berg826262c2012-12-10 16:38:14 +02001838 sdata->vif.bss_conf.dtim_period = 0;
1839
Johannes Berg028e8da02012-11-26 11:57:41 +01001840 ifmgd->flags = 0;
1841 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001842}
Jiri Bencf0706e82007-05-05 11:45:53 -07001843
Kalle Valo3cf335d2009-03-22 21:57:06 +02001844void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1845 struct ieee80211_hdr *hdr)
1846{
1847 /*
1848 * We can postpone the mgd.timer whenever receiving unicast frames
1849 * from AP because we know that the connection is working both ways
1850 * at that time. But multicast frames (and hence also beacons) must
1851 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001852 * data idle periods for sending the periodic probe request to the
1853 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001854 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001855 if (is_multicast_ether_addr(hdr->addr1))
1856 return;
1857
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001858 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001859}
Jiri Bencf0706e82007-05-05 11:45:53 -07001860
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001861static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1862{
1863 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001864 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001865
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001866 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001867 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001868 IEEE80211_STA_CONNECTION_POLL))) {
1869 mutex_unlock(&local->mtx);
1870 return;
1871 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001872
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001873 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001874
1875 mutex_lock(&local->iflist_mtx);
1876 ieee80211_recalc_ps(local, -1);
1877 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001878
1879 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001880 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001881
1882 /*
1883 * We've received a probe response, but are not sure whether
1884 * we have or will be receiving any beacons or data, so let's
1885 * schedule the timers again, just in case.
1886 */
1887 ieee80211_sta_reset_beacon_monitor(sdata);
1888
1889 mod_timer(&ifmgd->conn_mon_timer,
1890 round_jiffies_up(jiffies +
1891 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001892out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001893 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001894}
1895
1896void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001897 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001898{
Felix Fietkau75706d02010-12-02 21:01:07 +01001899 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001900 return;
1901
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001902 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1903 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001904 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001905 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001906 else
1907 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001908 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001909 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001910 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001911
1912 if (ack)
1913 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001914}
1915
Maxim Levitskya43abf22009-07-31 18:54:12 +03001916static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1917{
1918 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1919 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001920 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001921 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001922
1923 /*
1924 * Try sending broadcast probe requests for the last three
1925 * probe requests after the first ones failed since some
1926 * buggy APs only support broadcast probe requests.
1927 */
1928 if (ifmgd->probe_send_count >= unicast_limit)
1929 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001930
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001931 /*
1932 * When the hardware reports an accurate Tx ACK status, it's
1933 * better to send a nullfunc frame instead of a probe request,
1934 * as it will kick us off the AP quickly if we aren't associated
1935 * anymore. The timeout will be reset if the frame is ACKed by
1936 * the AP.
1937 */
Soumik Das992e68b2012-05-20 15:31:13 +05301938 ifmgd->probe_send_count++;
1939
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001940 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1941 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001942 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001943 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001944 int ssid_len;
1945
Johannes Berg9caf0362012-11-29 01:25:20 +01001946 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001947 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001948 if (WARN_ON_ONCE(ssid == NULL))
1949 ssid_len = 0;
1950 else
1951 ssid_len = ssid[1];
1952
1953 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001954 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001955 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001956 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001957 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001958
Ben Greear180205b2011-02-04 15:30:24 -08001959 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001960 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301961 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1962 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001963}
1964
Johannes Bergb291ba12009-07-10 15:29:03 +02001965static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1966 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001967{
Kalle Valo04de8382009-03-22 21:57:35 +02001968 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001969 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001970
Johannes Berg9607e6b2009-12-23 13:15:31 +01001971 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001972 return;
1973
Johannes Berg77fdaa12009-07-07 03:45:17 +02001974 mutex_lock(&ifmgd->mtx);
1975
1976 if (!ifmgd->associated)
1977 goto out;
1978
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001979 mutex_lock(&sdata->local->mtx);
1980
1981 if (sdata->local->tmp_channel || sdata->local->scanning) {
1982 mutex_unlock(&sdata->local->mtx);
1983 goto out;
1984 }
1985
Joe Perchese87cc472012-05-13 21:56:26 +00001986 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001987 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001988 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001989
Holger Schurig6efb71b2012-05-15 15:38:59 +02001990 ieee80211_cqm_rssi_notify(&sdata->vif,
1991 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001992
Johannes Bergb291ba12009-07-10 15:29:03 +02001993 /*
1994 * The driver/our work has already reported this event or the
1995 * connection monitoring has kicked in and we have already sent
1996 * a probe request. Or maybe the AP died and the driver keeps
1997 * reporting until we disassociate...
1998 *
1999 * In either case we have to ignore the current call to this
2000 * function (except for setting the correct probe reason bit)
2001 * because otherwise we would reset the timer every time and
2002 * never check whether we received a probe response!
2003 */
2004 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2005 IEEE80211_STA_CONNECTION_POLL))
2006 already = true;
2007
2008 if (beacon)
2009 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2010 else
2011 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2012
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002013 mutex_unlock(&sdata->local->mtx);
2014
Johannes Bergb291ba12009-07-10 15:29:03 +02002015 if (already)
2016 goto out;
2017
Johannes Berg4e751842009-06-10 15:16:52 +02002018 mutex_lock(&sdata->local->iflist_mtx);
2019 ieee80211_recalc_ps(sdata->local, -1);
2020 mutex_unlock(&sdata->local->iflist_mtx);
2021
Maxim Levitskya43abf22009-07-31 18:54:12 +03002022 ifmgd->probe_send_count = 0;
2023 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002024 out:
2025 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002026}
2027
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002028struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2029 struct ieee80211_vif *vif)
2030{
2031 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2032 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002033 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002034 struct sk_buff *skb;
2035 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002036 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002037
2038 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2039 return NULL;
2040
2041 ASSERT_MGD_MTX(ifmgd);
2042
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002043 if (ifmgd->associated)
2044 cbss = ifmgd->associated;
2045 else if (ifmgd->auth_data)
2046 cbss = ifmgd->auth_data->bss;
2047 else if (ifmgd->assoc_data)
2048 cbss = ifmgd->assoc_data->bss;
2049 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002050 return NULL;
2051
Johannes Berg9caf0362012-11-29 01:25:20 +01002052 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002053 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002054 if (WARN_ON_ONCE(ssid == NULL))
2055 ssid_len = 0;
2056 else
2057 ssid_len = ssid[1];
2058
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002059 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002060 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002061 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002062 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002063 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002064
2065 return skb;
2066}
2067EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2068
Johannes Bergeef9e542013-01-29 13:09:34 +01002069static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002070{
2071 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002072 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002073
2074 mutex_lock(&ifmgd->mtx);
2075 if (!ifmgd->associated) {
2076 mutex_unlock(&ifmgd->mtx);
2077 return;
2078 }
2079
Johannes Berg37ad3882012-02-24 13:50:54 +01002080 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2081 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002082 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002083 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002084 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2085 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002086 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002087
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002088 /*
2089 * must be outside lock due to cfg80211,
2090 * but that's not a problem.
2091 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002092 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002093}
2094
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002095static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002096{
2097 struct ieee80211_sub_if_data *sdata =
2098 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002099 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002100 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2101 struct sta_info *sta;
2102
2103 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302104 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002105 sta = sta_info_get(sdata, ifmgd->bssid);
2106 if (sta)
2107 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302108 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002109 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002110
Johannes Berg682bd382013-01-29 13:13:50 +01002111 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002112 sdata_info(sdata, "Connection to AP %pM lost\n",
2113 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002114 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002115 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002116 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002117 }
2118}
2119
2120static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2121{
2122 struct ieee80211_sub_if_data *sdata =
2123 container_of(work, struct ieee80211_sub_if_data,
2124 u.mgd.csa_connection_drop_work);
2125
Johannes Bergeef9e542013-01-29 13:09:34 +01002126 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002127}
2128
Kalle Valo04de8382009-03-22 21:57:35 +02002129void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2130{
2131 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002132 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002133
Johannes Bergb5878a22010-04-07 16:48:40 +02002134 trace_api_beacon_loss(sdata);
2135
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002136 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002137 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002138 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002139}
2140EXPORT_SYMBOL(ieee80211_beacon_loss);
2141
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002142void ieee80211_connection_loss(struct ieee80211_vif *vif)
2143{
2144 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2145 struct ieee80211_hw *hw = &sdata->local->hw;
2146
Johannes Bergb5878a22010-04-07 16:48:40 +02002147 trace_api_connection_loss(sdata);
2148
Johannes Berg682bd382013-01-29 13:13:50 +01002149 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002150 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2151}
2152EXPORT_SYMBOL(ieee80211_connection_loss);
2153
2154
Johannes Berg66e67e42012-01-20 13:55:27 +01002155static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2156 bool assoc)
2157{
2158 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2159
2160 lockdep_assert_held(&sdata->u.mgd.mtx);
2161
Johannes Berg66e67e42012-01-20 13:55:27 +01002162 if (!assoc) {
2163 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2164
2165 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2166 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002167 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002168 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002169 }
2170
Johannes Berg5b112d32013-02-01 01:49:58 +01002171 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002172 kfree(auth_data);
2173 sdata->u.mgd.auth_data = NULL;
2174}
2175
2176static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2177 struct ieee80211_mgmt *mgmt, size_t len)
2178{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002179 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002180 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2181 u8 *pos;
2182 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002183 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002184
2185 pos = mgmt->u.auth.variable;
2186 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2187 if (!elems.challenge)
2188 return;
2189 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002190 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002191 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2192 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2193 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002194 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002195 elems.challenge - 2, elems.challenge_len + 2,
2196 auth_data->bss->bssid, auth_data->bss->bssid,
2197 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002198 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002199}
2200
2201static enum rx_mgmt_action __must_check
2202ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2203 struct ieee80211_mgmt *mgmt, size_t len)
2204{
2205 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2206 u8 bssid[ETH_ALEN];
2207 u16 auth_alg, auth_transaction, status_code;
2208 struct sta_info *sta;
2209
2210 lockdep_assert_held(&ifmgd->mtx);
2211
2212 if (len < 24 + 6)
2213 return RX_MGMT_NONE;
2214
2215 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2216 return RX_MGMT_NONE;
2217
2218 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2219
Joe Perchesb203ca32012-05-08 18:56:52 +00002220 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002221 return RX_MGMT_NONE;
2222
2223 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2224 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2225 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2226
2227 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002228 auth_transaction != ifmgd->auth_data->expected_transaction) {
2229 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2230 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2231 auth_transaction,
2232 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002233 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002234 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002235
2236 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002237 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2238 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002239 ieee80211_destroy_auth_data(sdata, false);
2240 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002241 }
2242
2243 switch (ifmgd->auth_data->algorithm) {
2244 case WLAN_AUTH_OPEN:
2245 case WLAN_AUTH_LEAP:
2246 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002247 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002248 break;
2249 case WLAN_AUTH_SHARED_KEY:
2250 if (ifmgd->auth_data->expected_transaction != 4) {
2251 ieee80211_auth_challenge(sdata, mgmt, len);
2252 /* need another frame */
2253 return RX_MGMT_NONE;
2254 }
2255 break;
2256 default:
2257 WARN_ONCE(1, "invalid auth alg %d",
2258 ifmgd->auth_data->algorithm);
2259 return RX_MGMT_NONE;
2260 }
2261
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002262 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002263 ifmgd->auth_data->done = true;
2264 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002265 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002266 run_again(ifmgd, ifmgd->auth_data->timeout);
2267
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002268 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2269 ifmgd->auth_data->expected_transaction != 2) {
2270 /*
2271 * Report auth frame to user space for processing since another
2272 * round of Authentication frames is still needed.
2273 */
2274 return RX_MGMT_CFG80211_RX_AUTH;
2275 }
2276
Johannes Berg66e67e42012-01-20 13:55:27 +01002277 /* move station state to auth */
2278 mutex_lock(&sdata->local->sta_mtx);
2279 sta = sta_info_get(sdata, bssid);
2280 if (!sta) {
2281 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2282 goto out_err;
2283 }
2284 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002285 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002286 goto out_err;
2287 }
2288 mutex_unlock(&sdata->local->sta_mtx);
2289
2290 return RX_MGMT_CFG80211_RX_AUTH;
2291 out_err:
2292 mutex_unlock(&sdata->local->sta_mtx);
2293 /* ignore frame -- wait for timeout */
2294 return RX_MGMT_NONE;
2295}
2296
2297
Johannes Berg77fdaa12009-07-07 03:45:17 +02002298static enum rx_mgmt_action __must_check
2299ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002300 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002301{
Johannes Berg46900292009-02-15 12:44:28 +01002302 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002303 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002304 u16 reason_code;
2305
Johannes Berg66e67e42012-01-20 13:55:27 +01002306 lockdep_assert_held(&ifmgd->mtx);
2307
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002308 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002309 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002310
Johannes Berg66e67e42012-01-20 13:55:27 +01002311 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002312 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002313 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002314
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002315 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002316
2317 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2318
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002319 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2320 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002321
Johannes Berg37ad3882012-02-24 13:50:54 +01002322 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2323
Johannes Berg77fdaa12009-07-07 03:45:17 +02002324 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002325}
2326
2327
Johannes Berg77fdaa12009-07-07 03:45:17 +02002328static enum rx_mgmt_action __must_check
2329ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2330 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002331{
Johannes Berg46900292009-02-15 12:44:28 +01002332 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002333 u16 reason_code;
2334
Johannes Berg66e67e42012-01-20 13:55:27 +01002335 lockdep_assert_held(&ifmgd->mtx);
2336
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002337 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002338 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002339
Johannes Berg66e67e42012-01-20 13:55:27 +01002340 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002341 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002342 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002343
2344 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2345
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002346 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2347 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002348
Johannes Berg37ad3882012-02-24 13:50:54 +01002349 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2350
Johannes Berg77fdaa12009-07-07 03:45:17 +02002351 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002352}
2353
Christian Lamparterc74d0842011-10-15 00:14:49 +02002354static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2355 u8 *supp_rates, unsigned int supp_rates_len,
2356 u32 *rates, u32 *basic_rates,
2357 bool *have_higher_than_11mbit,
2358 int *min_rate, int *min_rate_index)
2359{
2360 int i, j;
2361
2362 for (i = 0; i < supp_rates_len; i++) {
2363 int rate = (supp_rates[i] & 0x7f) * 5;
2364 bool is_basic = !!(supp_rates[i] & 0x80);
2365
2366 if (rate > 110)
2367 *have_higher_than_11mbit = true;
2368
2369 /*
2370 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2371 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2372 *
2373 * Note: Even through the membership selector and the basic
2374 * rate flag share the same bit, they are not exactly
2375 * the same.
2376 */
2377 if (!!(supp_rates[i] & 0x80) &&
2378 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2379 continue;
2380
2381 for (j = 0; j < sband->n_bitrates; j++) {
2382 if (sband->bitrates[j].bitrate == rate) {
2383 *rates |= BIT(j);
2384 if (is_basic)
2385 *basic_rates |= BIT(j);
2386 if (rate < *min_rate) {
2387 *min_rate = rate;
2388 *min_rate_index = j;
2389 }
2390 break;
2391 }
2392 }
2393 }
2394}
Jiri Bencf0706e82007-05-05 11:45:53 -07002395
Johannes Berg66e67e42012-01-20 13:55:27 +01002396static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2397 bool assoc)
2398{
2399 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2400
2401 lockdep_assert_held(&sdata->u.mgd.mtx);
2402
Johannes Berg66e67e42012-01-20 13:55:27 +01002403 if (!assoc) {
2404 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2405
2406 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2407 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002408 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002409 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002410 }
2411
2412 kfree(assoc_data);
2413 sdata->u.mgd.assoc_data = NULL;
2414}
2415
2416static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2417 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002418 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002419{
Johannes Berg46900292009-02-15 12:44:28 +01002420 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002421 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002422 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002423 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002424 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002425 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002426 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002427 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002428 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002429 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002430
Johannes Bergaf6b6372009-12-23 13:15:35 +01002431 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002432
Jiri Bencf0706e82007-05-05 11:45:53 -07002433 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002434 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002435
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002436 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002437 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2438 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002439 aid &= ~(BIT(15) | BIT(14));
2440
Johannes Berg05cb9102011-10-28 11:59:47 +02002441 ifmgd->broken_ap = false;
2442
2443 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002444 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2445 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002446 aid = 0;
2447 ifmgd->broken_ap = true;
2448 }
2449
Johannes Bergaf6b6372009-12-23 13:15:35 +01002450 pos = mgmt->u.assoc_resp.variable;
2451 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2452
Jiri Bencf0706e82007-05-05 11:45:53 -07002453 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002454 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002455 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002456 }
2457
Johannes Berg46900292009-02-15 12:44:28 +01002458 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002459
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002460 /*
2461 * We previously checked these in the beacon/probe response, so
2462 * they should be present here. This is just a safety net.
2463 */
2464 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2465 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2466 sdata_info(sdata,
2467 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2468 return false;
2469 }
2470
2471 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2472 (!elems.vht_cap_elem || !elems.vht_operation)) {
2473 sdata_info(sdata,
2474 "VHT AP is missing VHT capability/operation in AssocResp\n");
2475 return false;
2476 }
2477
Guy Eilam2a33bee2011-08-17 15:18:15 +03002478 mutex_lock(&sdata->local->sta_mtx);
2479 /*
2480 * station info was already allocated and inserted before
2481 * the association and should be available to us
2482 */
Johannes Berg7852e362012-01-20 13:55:24 +01002483 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002484 if (WARN_ON(!sta)) {
2485 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002486 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002487 }
2488
Johannes Berg55de9082012-07-26 17:24:39 +02002489 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002490
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002491 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002492 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002493 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002494 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002495
Mahesh Palivela818255e2012-10-10 11:33:04 +00002496 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2497 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002498 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002499
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002500 /*
2501 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2502 * in their association response, so ignore that data for our own
2503 * configuration. If it changed since the last beacon, we'll get the
2504 * next beacon and update then.
2505 */
Johannes Berg11289582013-02-07 17:36:12 +01002506
Johannes Bergbee7f582013-02-07 22:24:55 +01002507 /*
2508 * If an operating mode notification IE is present, override the
2509 * NSS calculation (that would be done in rate_control_rate_init())
2510 * and use the # of streams from that element.
2511 */
2512 if (elems.opmode_notif &&
2513 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2514 u8 nss;
2515
2516 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2517 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2518 nss += 1;
2519 sta->sta.rx_nss = nss;
2520 }
2521
Johannes Berg4b7679a2008-09-18 18:14:18 +02002522 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002523
Johannes Berg46900292009-02-15 12:44:28 +01002524 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002525 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002526
Johannes Bergddf4ac52008-10-22 11:41:38 +02002527 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002528 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002529
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002530 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002531 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2532 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2533 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002534 sdata_info(sdata,
2535 "failed to move station %pM to desired state\n",
2536 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002537 WARN_ON(__sta_info_destroy(sta));
2538 mutex_unlock(&sdata->local->sta_mtx);
2539 return false;
2540 }
2541
Johannes Berg7852e362012-01-20 13:55:24 +01002542 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002543
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002544 /*
2545 * Always handle WMM once after association regardless
2546 * of the first value the AP uses. Setting -1 here has
2547 * that effect because the AP values is an unsigned
2548 * 4-bit value.
2549 */
2550 ifmgd->wmm_last_param_set = -1;
2551
Johannes Bergddf4ac52008-10-22 11:41:38 +02002552 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002553 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002554 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002555 else
Johannes Berg3abead52012-03-02 15:56:59 +01002556 ieee80211_set_wmm_default(sdata, false);
2557 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002558
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002559 /* set AID and assoc capability,
2560 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002561 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002562 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002563 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002564
Kalle Valo15b7b062009-03-22 21:57:14 +02002565 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002566 * If we're using 4-addr mode, let the AP know that we're
2567 * doing so, so that it can create the STA VLAN on its side
2568 */
2569 if (ifmgd->use_4addr)
2570 ieee80211_send_4addr_nullfunc(local, sdata);
2571
2572 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002573 * Start timer to probe the connection to the AP now.
2574 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002575 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002576 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002577 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002578
Johannes Bergaf6b6372009-12-23 13:15:35 +01002579 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002580}
2581
Johannes Berg66e67e42012-01-20 13:55:27 +01002582static enum rx_mgmt_action __must_check
2583ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2584 struct ieee80211_mgmt *mgmt, size_t len,
2585 struct cfg80211_bss **bss)
2586{
2587 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2588 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2589 u16 capab_info, status_code, aid;
2590 struct ieee802_11_elems elems;
2591 u8 *pos;
2592 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002593
Johannes Berg66e67e42012-01-20 13:55:27 +01002594 lockdep_assert_held(&ifmgd->mtx);
2595
2596 if (!assoc_data)
2597 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002598 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002599 return RX_MGMT_NONE;
2600
2601 /*
2602 * AssocResp and ReassocResp have identical structure, so process both
2603 * of them in this function.
2604 */
2605
2606 if (len < 24 + 6)
2607 return RX_MGMT_NONE;
2608
2609 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2610 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2611 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2612 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2613
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002614 sdata_info(sdata,
2615 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2616 reassoc ? "Rea" : "A", mgmt->sa,
2617 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002618
2619 pos = mgmt->u.assoc_resp.variable;
2620 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2621
2622 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2623 elems.timeout_int && elems.timeout_int_len == 5 &&
2624 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2625 u32 tu, ms;
2626 tu = get_unaligned_le32(elems.timeout_int + 1);
2627 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002628 sdata_info(sdata,
2629 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2630 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002631 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002632 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002633 if (ms > IEEE80211_ASSOC_TIMEOUT)
2634 run_again(ifmgd, assoc_data->timeout);
2635 return RX_MGMT_NONE;
2636 }
2637
2638 *bss = assoc_data->bss;
2639
2640 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002641 sdata_info(sdata, "%pM denied association (code=%d)\n",
2642 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002643 ieee80211_destroy_assoc_data(sdata, false);
2644 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002645 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2646 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002647 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002648 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002649 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2650 }
John W. Linville635d9992012-07-09 16:34:34 -04002651 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002652
2653 /*
2654 * destroy assoc_data afterwards, as otherwise an idle
2655 * recalc after assoc_data is NULL but before associated
2656 * is set can cause the interface to go idle
2657 */
2658 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002659 }
2660
2661 return RX_MGMT_CFG80211_RX_ASSOC;
2662}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002663
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002664static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002665 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002666 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002667 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002668{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002669 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002670 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002671 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002672 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002673 bool need_ps = false;
2674
Johannes Berg826262c2012-12-10 16:38:14 +02002675 if ((sdata->u.mgd.associated &&
2676 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2677 (sdata->u.mgd.assoc_data &&
2678 ether_addr_equal(mgmt->bssid,
2679 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002680 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002681 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2682
2683 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002684 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002685 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2686 }
Johannes Berg56007a02010-01-26 14:19:52 +01002687 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002688
Johannes Berg5bda6172008-09-08 11:05:10 +02002689 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002690 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2691 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002692 else
2693 freq = rx_status->freq;
2694
2695 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2696
2697 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2698 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002699
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002700 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002701 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002702 if (bss)
2703 ieee80211_rx_bss_put(local, bss);
2704
2705 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002706 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002707
Johannes Berg56007a02010-01-26 14:19:52 +01002708 if (need_ps) {
2709 mutex_lock(&local->iflist_mtx);
2710 ieee80211_recalc_ps(local, -1);
2711 mutex_unlock(&local->iflist_mtx);
2712 }
2713
Johannes Berg5bc14202012-08-01 16:13:02 +02002714 if (elems->ch_switch_ie &&
2715 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2716 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002717 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002718}
2719
2720
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002721static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002722 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002723{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002724 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002725 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002726 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2727 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002728 struct ieee802_11_elems elems;
2729
Kalle Valo15b7b062009-03-22 21:57:14 +02002730 ifmgd = &sdata->u.mgd;
2731
Johannes Berg77fdaa12009-07-07 03:45:17 +02002732 ASSERT_MGD_MTX(ifmgd);
2733
Joe Perchesb203ca32012-05-08 18:56:52 +00002734 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002735 return; /* ignore ProbeResp to foreign address */
2736
Ester Kummerae6a44e2008-06-27 18:54:48 +03002737 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2738 if (baselen > len)
2739 return;
2740
2741 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2742 &elems);
2743
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002744 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002745
Johannes Berg77fdaa12009-07-07 03:45:17 +02002746 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002747 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002748 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002749
2750 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002751 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002752 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002753 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002754 ifmgd->auth_data->tries = 0;
2755 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002756 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002757 run_again(ifmgd, ifmgd->auth_data->timeout);
2758 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002759}
2760
Johannes Bergd91f36d2009-04-16 13:17:26 +02002761/*
2762 * This is the canonical list of information elements we care about,
2763 * the filter code also gives us all changes to the Microsoft OUI
2764 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2765 *
2766 * We implement beacon filtering in software since that means we can
2767 * avoid processing the frame here and in cfg80211, and userspace
2768 * will not be able to tell whether the hardware supports it or not.
2769 *
2770 * XXX: This list needs to be dynamic -- userspace needs to be able to
2771 * add items it requires. It also needs to be able to tell us to
2772 * look out for other vendor IEs.
2773 */
2774static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002775 (1ULL << WLAN_EID_COUNTRY) |
2776 (1ULL << WLAN_EID_ERP_INFO) |
2777 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2778 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2779 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002780 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002781
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002782static enum rx_mgmt_action
2783ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2784 struct ieee80211_mgmt *mgmt, size_t len,
2785 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002786{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002787 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002788 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002789 size_t baselen;
2790 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002791 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002792 struct ieee80211_chanctx_conf *chanctx_conf;
2793 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002794 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002795 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002796 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002797 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002798 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002799 u8 *bssid;
2800
Johannes Berg66e67e42012-01-20 13:55:27 +01002801 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002802
Ester Kummerae6a44e2008-06-27 18:54:48 +03002803 /* Process beacon from the current BSS */
2804 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2805 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002806 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002807
Johannes Berg55de9082012-07-26 17:24:39 +02002808 rcu_read_lock();
2809 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2810 if (!chanctx_conf) {
2811 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002812 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002813 }
2814
Johannes Berg4bf88532012-11-09 11:39:59 +01002815 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002816 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002817 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002818 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002819 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002820 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002821
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002822 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002823 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002824 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2825 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002826
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002827 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002828 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002829 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002830 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2831 sdata->vif.bss_conf.sync_tsf =
2832 le64_to_cpu(mgmt->u.beacon.timestamp);
2833 sdata->vif.bss_conf.sync_device_ts =
2834 rx_status->device_timestamp;
2835 if (elems.tim)
2836 sdata->vif.bss_conf.sync_dtim_count =
2837 elems.tim->dtim_count;
2838 else
2839 sdata->vif.bss_conf.sync_dtim_count = 0;
2840 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002841 /* continue assoc process */
2842 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002843 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002844 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002845 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002846 }
2847
2848 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002849 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002850 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002851 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002852
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002853 /* Track average RSSI from the Beacon frames of the current AP */
2854 ifmgd->last_beacon_signal = rx_status->signal;
2855 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2856 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002857 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002858 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002859 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002860 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002861 } else {
2862 ifmgd->ave_beacon_signal =
2863 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2864 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2865 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002866 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002867 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002868
2869 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2870 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2871 int sig = ifmgd->ave_beacon_signal;
2872 int last_sig = ifmgd->last_ave_beacon_signal;
2873
2874 /*
2875 * if signal crosses either of the boundaries, invoke callback
2876 * with appropriate parameters
2877 */
2878 if (sig > ifmgd->rssi_max_thold &&
2879 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2880 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002881 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002882 } else if (sig < ifmgd->rssi_min_thold &&
2883 (last_sig >= ifmgd->rssi_max_thold ||
2884 last_sig == 0)) {
2885 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002886 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002887 }
2888 }
2889
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002890 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002891 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002892 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002893 int sig = ifmgd->ave_beacon_signal / 16;
2894 int last_event = ifmgd->last_cqm_event_signal;
2895 int thold = bss_conf->cqm_rssi_thold;
2896 int hyst = bss_conf->cqm_rssi_hyst;
2897 if (sig < thold &&
2898 (last_event == 0 || sig < last_event - hyst)) {
2899 ifmgd->last_cqm_event_signal = sig;
2900 ieee80211_cqm_rssi_notify(
2901 &sdata->vif,
2902 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2903 GFP_KERNEL);
2904 } else if (sig > thold &&
2905 (last_event == 0 || sig > last_event + hyst)) {
2906 ifmgd->last_cqm_event_signal = sig;
2907 ieee80211_cqm_rssi_notify(
2908 &sdata->vif,
2909 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2910 GFP_KERNEL);
2911 }
2912 }
2913
Johannes Bergb291ba12009-07-10 15:29:03 +02002914 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002915 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002916 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002917 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002918 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002919 ieee80211_run_deferred_scan(local);
2920 mutex_unlock(&local->mtx);
2921
Johannes Berg4e751842009-06-10 15:16:52 +02002922 mutex_lock(&local->iflist_mtx);
2923 ieee80211_recalc_ps(local, -1);
2924 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002925 }
2926
Johannes Bergb291ba12009-07-10 15:29:03 +02002927 /*
2928 * Push the beacon loss detection into the future since
2929 * we are processing a beacon from the AP just now.
2930 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002931 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002932
Johannes Bergd91f36d2009-04-16 13:17:26 +02002933 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2934 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2935 len - baselen, &elems,
2936 care_about_ies, ncrc);
2937
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302938 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002939 bool directed_tim = ieee80211_check_tim(elems.tim,
2940 elems.tim_len,
2941 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002942 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002943 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002944 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2945 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2946 ieee80211_hw_config(local,
2947 IEEE80211_CONF_CHANGE_PS);
2948 }
Kalle Valo572e0012009-02-10 17:09:31 +02002949 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302950 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002951 local->pspolling = true;
2952
2953 /*
2954 * Here is assumed that the driver will be
2955 * able to send ps-poll frame and receive a
2956 * response even though power save mode is
2957 * enabled, but some drivers might require
2958 * to disable power save here. This needs
2959 * to be investigated.
2960 */
2961 ieee80211_send_pspoll(local, sdata);
2962 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002963 }
2964 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002965
Johannes Berg488dd7b2012-10-29 20:08:01 +01002966 if (sdata->vif.p2p) {
2967 u8 noa[2];
2968 int ret;
2969
2970 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2971 len - baselen,
2972 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2973 noa, sizeof(noa));
2974 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2975 bss_conf->p2p_oppps = noa[1] & 0x80;
2976 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2977 changed |= BSS_CHANGED_P2P_PS;
2978 sdata->u.mgd.p2p_noa_index = noa[0];
2979 /*
2980 * make sure we update all information, the CRC
2981 * mechanism doesn't look at P2P attributes.
2982 */
2983 ifmgd->beacon_crc_valid = false;
2984 }
2985 }
2986
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002987 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002988 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03002989 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002990 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002991
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002992 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002993
2994 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2995 elems.wmm_param_len))
2996 changed |= BSS_CHANGED_QOS;
2997
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002998 /*
2999 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01003000 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003001 */
3002 if (!bss_conf->dtim_period) {
3003 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3004 if (elems.tim)
3005 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3006 else
3007 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003008
3009 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3010 sdata->vif.bss_conf.sync_tsf =
3011 le64_to_cpu(mgmt->u.beacon.timestamp);
3012 sdata->vif.bss_conf.sync_device_ts =
3013 rx_status->device_timestamp;
3014 if (elems.tim)
3015 sdata->vif.bss_conf.sync_dtim_count =
3016 elems.tim->dtim_count;
3017 else
3018 sdata->vif.bss_conf.sync_dtim_count = 0;
3019 }
3020
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003021 changed |= BSS_CHANGED_DTIM_PERIOD;
3022 }
3023
Johannes Berg7a5158e2008-10-08 10:59:33 +02003024 if (elems.erp_info && elems.erp_info_len >= 1) {
3025 erp_valid = true;
3026 erp_value = elems.erp_info[0];
3027 } else {
3028 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003029 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003030 changed |= ieee80211_handle_bss_capability(sdata,
3031 le16_to_cpu(mgmt->u.beacon.capab_info),
3032 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003033
Johannes Berg11289582013-02-07 17:36:12 +01003034 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003035 sta = sta_info_get(sdata, bssid);
3036
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003037 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3038 elems.vht_operation, bssid, &changed)) {
3039 mutex_unlock(&local->sta_mtx);
3040 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3041 WLAN_REASON_DEAUTH_LEAVING,
3042 true, deauth_buf);
3043 return RX_MGMT_CFG80211_TX_DEAUTH;
3044 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003045
3046 if (sta && elems.opmode_notif)
3047 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3048 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003049 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003050
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003051 if (elems.country_elem && elems.pwr_constr_elem &&
3052 mgmt->u.probe_resp.capab_info &
3053 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003054 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3055 elems.country_elem,
3056 elems.country_elem_len,
3057 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003058
Johannes Berg471b3ef2007-12-28 14:32:58 +01003059 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003060
3061 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003062}
3063
Johannes Berg1fa57d02010-06-10 10:21:32 +02003064void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3065 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003066{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003067 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003068 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003069 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003070 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003071 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003072 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003073 u16 fc;
3074
Jiri Bencf0706e82007-05-05 11:45:53 -07003075 rx_status = (struct ieee80211_rx_status *) skb->cb;
3076 mgmt = (struct ieee80211_mgmt *) skb->data;
3077 fc = le16_to_cpu(mgmt->frame_control);
3078
Johannes Berg77fdaa12009-07-07 03:45:17 +02003079 mutex_lock(&ifmgd->mtx);
3080
Johannes Berg66e67e42012-01-20 13:55:27 +01003081 switch (fc & IEEE80211_FCTL_STYPE) {
3082 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003083 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3084 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003085 break;
3086 case IEEE80211_STYPE_PROBE_RESP:
3087 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3088 break;
3089 case IEEE80211_STYPE_AUTH:
3090 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3091 break;
3092 case IEEE80211_STYPE_DEAUTH:
3093 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3094 break;
3095 case IEEE80211_STYPE_DISASSOC:
3096 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3097 break;
3098 case IEEE80211_STYPE_ASSOC_RESP:
3099 case IEEE80211_STYPE_REASSOC_RESP:
3100 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3101 break;
3102 case IEEE80211_STYPE_ACTION:
3103 switch (mgmt->u.action.category) {
3104 case WLAN_CATEGORY_SPECTRUM_MGMT:
3105 ieee80211_sta_process_chanswitch(sdata,
3106 &mgmt->u.action.u.chan_switch.sw_elem,
3107 (void *)ifmgd->associated->priv,
3108 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003109 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003110 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003111 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003112 mutex_unlock(&ifmgd->mtx);
3113
Johannes Berg66e67e42012-01-20 13:55:27 +01003114 switch (rma) {
3115 case RX_MGMT_NONE:
3116 /* no action */
3117 break;
3118 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003119 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003120 break;
3121 case RX_MGMT_CFG80211_DISASSOC:
3122 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3123 break;
3124 case RX_MGMT_CFG80211_RX_AUTH:
3125 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3126 break;
3127 case RX_MGMT_CFG80211_RX_ASSOC:
3128 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3129 break;
3130 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3131 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3132 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003133 case RX_MGMT_CFG80211_TX_DEAUTH:
3134 cfg80211_send_deauth(sdata->dev, deauth_buf,
3135 sizeof(deauth_buf));
3136 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003137 default:
3138 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003139 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003140}
3141
Johannes Berg9c6bd792008-09-11 00:01:52 +02003142static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003143{
3144 struct ieee80211_sub_if_data *sdata =
3145 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01003146 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003147 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003148
Johannes Berg5bb644a2009-05-17 11:40:42 +02003149 if (local->quiescing) {
3150 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3151 return;
3152 }
3153
Johannes Berg64592c82010-06-10 10:21:31 +02003154 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003155}
3156
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003157static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003158 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003159{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003160 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003161 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003162
Johannes Berg37ad3882012-02-24 13:50:54 +01003163 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003164 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003165 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003166
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003167 /*
3168 * must be outside lock due to cfg80211,
3169 * but that's not a problem.
3170 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003171 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003172
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003173 mutex_lock(&ifmgd->mtx);
3174}
3175
Johannes Berg66e67e42012-01-20 13:55:27 +01003176static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3177{
3178 struct ieee80211_local *local = sdata->local;
3179 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3180 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003181 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003182
3183 lockdep_assert_held(&ifmgd->mtx);
3184
3185 if (WARN_ON_ONCE(!auth_data))
3186 return -EINVAL;
3187
Johannes Berg1672c0e32013-01-29 15:02:27 +01003188 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3189 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3190 IEEE80211_TX_INTFL_MLME_CONN_TX;
3191
Johannes Berg66e67e42012-01-20 13:55:27 +01003192 auth_data->tries++;
3193
3194 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003195 sdata_info(sdata, "authentication with %pM timed out\n",
3196 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003197
3198 /*
3199 * Most likely AP is not in the range so remove the
3200 * bss struct for that AP.
3201 */
3202 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3203
3204 return -ETIMEDOUT;
3205 }
3206
Johannes Berga1845fc2012-06-27 13:18:36 +02003207 drv_mgd_prepare_tx(local, sdata);
3208
Johannes Berg66e67e42012-01-20 13:55:27 +01003209 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003210 u16 trans = 1;
3211 u16 status = 0;
3212
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003213 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3214 auth_data->bss->bssid, auth_data->tries,
3215 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003216
3217 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003218
3219 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3220 trans = auth_data->sae_trans;
3221 status = auth_data->sae_status;
3222 auth_data->expected_transaction = trans;
3223 }
3224
3225 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3226 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003227 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003228 auth_data->bss->bssid, NULL, 0, 0,
3229 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003230 } else {
3231 const u8 *ssidie;
3232
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003233 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3234 auth_data->bss->bssid, auth_data->tries,
3235 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003236
Johannes Berg9caf0362012-11-29 01:25:20 +01003237 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003238 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003239 if (!ssidie) {
3240 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003241 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003242 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003243 /*
3244 * Direct probe is sent to broadcast address as some APs
3245 * will not answer to direct packet in unassociated state.
3246 */
3247 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01003248 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003249 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003250 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003251 }
3252
Johannes Berg1672c0e32013-01-29 15:02:27 +01003253 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3254 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003255 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003256 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003257 } else {
3258 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003259 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003260
3261 return 0;
3262}
3263
3264static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3265{
3266 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3267 struct ieee80211_local *local = sdata->local;
3268
3269 lockdep_assert_held(&sdata->u.mgd.mtx);
3270
Johannes Berg66e67e42012-01-20 13:55:27 +01003271 assoc_data->tries++;
3272 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003273 sdata_info(sdata, "association with %pM timed out\n",
3274 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003275
3276 /*
3277 * Most likely AP is not in the range so remove the
3278 * bss struct for that AP.
3279 */
3280 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3281
3282 return -ETIMEDOUT;
3283 }
3284
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003285 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3286 assoc_data->bss->bssid, assoc_data->tries,
3287 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003288 ieee80211_send_assoc(sdata);
3289
Johannes Berg1672c0e32013-01-29 15:02:27 +01003290 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3291 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003292 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003293 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003294 } else {
3295 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003296 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003297
3298 return 0;
3299}
3300
Johannes Berg1672c0e32013-01-29 15:02:27 +01003301void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3302 __le16 fc, bool acked)
3303{
3304 struct ieee80211_local *local = sdata->local;
3305
3306 sdata->u.mgd.status_fc = fc;
3307 sdata->u.mgd.status_acked = acked;
3308 sdata->u.mgd.status_received = true;
3309
3310 ieee80211_queue_work(&local->hw, &sdata->work);
3311}
3312
Johannes Berg1fa57d02010-06-10 10:21:32 +02003313void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003314{
Johannes Berg60f8b392008-09-08 17:44:22 +02003315 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003316 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003317
Johannes Berg77fdaa12009-07-07 03:45:17 +02003318 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003319
Johannes Berg1672c0e32013-01-29 15:02:27 +01003320 if (ifmgd->status_received) {
3321 __le16 fc = ifmgd->status_fc;
3322 bool status_acked = ifmgd->status_acked;
3323
3324 ifmgd->status_received = false;
3325 if (ifmgd->auth_data &&
3326 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3327 if (status_acked) {
3328 ifmgd->auth_data->timeout =
3329 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3330 run_again(ifmgd, ifmgd->auth_data->timeout);
3331 } else {
3332 ifmgd->auth_data->timeout = jiffies - 1;
3333 }
Johannes Berg89afe612013-02-13 15:39:57 +01003334 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003335 } else if (ifmgd->assoc_data &&
3336 (ieee80211_is_assoc_req(fc) ||
3337 ieee80211_is_reassoc_req(fc))) {
3338 if (status_acked) {
3339 ifmgd->assoc_data->timeout =
3340 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3341 run_again(ifmgd, ifmgd->assoc_data->timeout);
3342 } else {
3343 ifmgd->assoc_data->timeout = jiffies - 1;
3344 }
Johannes Berg89afe612013-02-13 15:39:57 +01003345 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003346 }
3347 }
3348
Johannes Berg89afe612013-02-13 15:39:57 +01003349 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003350 time_after(jiffies, ifmgd->auth_data->timeout)) {
3351 if (ifmgd->auth_data->done) {
3352 /*
3353 * ok ... we waited for assoc but userspace didn't,
3354 * so let's just kill the auth data
3355 */
3356 ieee80211_destroy_auth_data(sdata, false);
3357 } else if (ieee80211_probe_auth(sdata)) {
3358 u8 bssid[ETH_ALEN];
3359
3360 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3361
3362 ieee80211_destroy_auth_data(sdata, false);
3363
3364 mutex_unlock(&ifmgd->mtx);
3365 cfg80211_send_auth_timeout(sdata->dev, bssid);
3366 mutex_lock(&ifmgd->mtx);
3367 }
Johannes Berg89afe612013-02-13 15:39:57 +01003368 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003369 run_again(ifmgd, ifmgd->auth_data->timeout);
3370
Johannes Berg89afe612013-02-13 15:39:57 +01003371 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003372 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003373 if ((ifmgd->assoc_data->need_beacon &&
3374 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003375 ieee80211_do_assoc(sdata)) {
3376 u8 bssid[ETH_ALEN];
3377
3378 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3379
3380 ieee80211_destroy_assoc_data(sdata, false);
3381
3382 mutex_unlock(&ifmgd->mtx);
3383 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3384 mutex_lock(&ifmgd->mtx);
3385 }
Johannes Berg89afe612013-02-13 15:39:57 +01003386 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003387 run_again(ifmgd, ifmgd->assoc_data->timeout);
3388
Johannes Bergb291ba12009-07-10 15:29:03 +02003389 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3390 IEEE80211_STA_CONNECTION_POLL) &&
3391 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003392 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003393 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003394
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003395 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003396
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003397 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003398 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003399 else
Ben Greear180205b2011-02-04 15:30:24 -08003400 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003401
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003402 /* ACK received for nullfunc probing frame */
3403 if (!ifmgd->probe_send_count)
3404 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003405 else if (ifmgd->nullfunc_failed) {
3406 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003407 mlme_dbg(sdata,
3408 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3409 bssid, ifmgd->probe_send_count,
3410 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003411 ieee80211_mgd_probe_ap_send(sdata);
3412 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003413 mlme_dbg(sdata,
3414 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3415 bssid);
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,
3418 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003419 }
3420 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003421 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003422 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003423 mlme_dbg(sdata,
3424 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3425 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003426 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003427 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003428 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003429 mlme_dbg(sdata,
3430 "No probe response from AP %pM after %dms, try %d/%i\n",
3431 bssid, probe_wait_ms,
3432 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003433 ieee80211_mgd_probe_ap_send(sdata);
3434 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003435 /*
3436 * We actually lost the connection ... or did we?
3437 * Let's make sure!
3438 */
Ben Greearb38afa82010-10-07 16:12:06 -07003439 wiphy_debug(local->hw.wiphy,
3440 "%s: No probe response from AP %pM"
3441 " after %dms, disconnecting.\n",
3442 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003443 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003444
Johannes Berg95acac62011-07-12 12:30:59 +02003445 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003446 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003447 }
3448 }
3449
Johannes Berg77fdaa12009-07-07 03:45:17 +02003450 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003451}
Johannes Berg0a51b272008-09-08 17:44:25 +02003452
Johannes Bergb291ba12009-07-10 15:29:03 +02003453static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3454{
3455 struct ieee80211_sub_if_data *sdata =
3456 (struct ieee80211_sub_if_data *) data;
3457 struct ieee80211_local *local = sdata->local;
3458
3459 if (local->quiescing)
3460 return;
3461
Johannes Berg682bd382013-01-29 13:13:50 +01003462 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003463 ieee80211_queue_work(&sdata->local->hw,
3464 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003465}
3466
3467static void ieee80211_sta_conn_mon_timer(unsigned long data)
3468{
3469 struct ieee80211_sub_if_data *sdata =
3470 (struct ieee80211_sub_if_data *) data;
3471 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3472 struct ieee80211_local *local = sdata->local;
3473
3474 if (local->quiescing)
3475 return;
3476
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003477 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003478}
3479
3480static void ieee80211_sta_monitor_work(struct work_struct *work)
3481{
3482 struct ieee80211_sub_if_data *sdata =
3483 container_of(work, struct ieee80211_sub_if_data,
3484 u.mgd.monitor_work);
3485
3486 ieee80211_mgd_probe_ap(sdata, false);
3487}
3488
Johannes Berga1678f82008-09-11 00:01:47 +02003489static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3490{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003491 u32 flags;
3492
Kalle Vaload935682009-04-19 08:47:19 +03003493 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003494 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003495
Johannes Bergb291ba12009-07-10 15:29:03 +02003496 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003497 flags = sdata->local->hw.flags;
3498 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3499 ieee80211_queue_work(&sdata->local->hw,
3500 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003501 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003502 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003503 }
Johannes Berga1678f82008-09-11 00:01:47 +02003504}
3505
Johannes Berg5bb644a2009-05-17 11:40:42 +02003506#ifdef CONFIG_PM
3507void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3508{
3509 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3510
3511 /*
Ben Greear9b5bd5a2013-02-20 09:41:08 -08003512 * Stop timers before deleting work items, as timers
3513 * could race and re-add the work-items. They will be
3514 * re-established on connection.
3515 */
3516 del_timer_sync(&ifmgd->conn_mon_timer);
3517 del_timer_sync(&ifmgd->bcn_mon_timer);
3518
3519 /*
Johannes Berg5bb644a2009-05-17 11:40:42 +02003520 * we need to use atomic bitops for the running bits
3521 * only because both timers might fire at the same
3522 * time -- the code here is properly synchronised.
3523 */
3524
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003525 cancel_work_sync(&ifmgd->request_smps_work);
3526
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003527 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003528 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003529 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003530 if (del_timer_sync(&ifmgd->timer))
3531 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3532
Johannes Berg5bb644a2009-05-17 11:40:42 +02003533 if (del_timer_sync(&ifmgd->chswitch_timer))
3534 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Ben Greear9b5bd5a2013-02-20 09:41:08 -08003535 cancel_work_sync(&ifmgd->chswitch_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003536}
3537
3538void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3539{
3540 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3541
Johannes Berg782d2672013-01-18 21:21:31 +01003542 mutex_lock(&ifmgd->mtx);
3543 if (!ifmgd->associated) {
3544 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303545 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003546 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303547
Johannes Berg95acac62011-07-12 12:30:59 +02003548 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3549 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003550 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3551 ieee80211_sta_connection_lost(sdata,
3552 ifmgd->associated->bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003553 WLAN_REASON_UNSPECIFIED,
3554 true);
Johannes Berg95acac62011-07-12 12:30:59 +02003555 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003556 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003557 }
Johannes Berg782d2672013-01-18 21:21:31 +01003558 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003559
Johannes Berg5bb644a2009-05-17 11:40:42 +02003560 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3561 add_timer(&ifmgd->timer);
3562 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3563 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003564 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003565
3566 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003567 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003568 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003569}
3570#endif
3571
Johannes Berg9c6bd792008-09-11 00:01:52 +02003572/* interface setup */
3573void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3574{
Johannes Berg46900292009-02-15 12:44:28 +01003575 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003576
Johannes Berg46900292009-02-15 12:44:28 +01003577 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003578 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003579 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003580 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3581 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003582 INIT_WORK(&ifmgd->csa_connection_drop_work,
3583 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003584 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003585 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003586 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003587 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3588 (unsigned long) sdata);
3589 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3590 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003591 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303592 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003593
Johannes Bergab1faea2009-07-01 21:41:17 +02003594 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303595 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003596 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3597 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003598
Johannes Berg77fdaa12009-07-07 03:45:17 +02003599 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003600
3601 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3602 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3603 else
3604 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003605}
3606
3607/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003608void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3609{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003610 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003611
3612 /* Restart STA timers */
3613 rcu_read_lock();
3614 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3615 ieee80211_restart_sta_timer(sdata);
3616 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003617}
Johannes Berg10f644a2009-04-16 13:17:25 +02003618
3619int ieee80211_max_network_latency(struct notifier_block *nb,
3620 unsigned long data, void *dummy)
3621{
3622 s32 latency_usec = (s32) data;
3623 struct ieee80211_local *local =
3624 container_of(nb, struct ieee80211_local,
3625 network_latency_notifier);
3626
3627 mutex_lock(&local->iflist_mtx);
3628 ieee80211_recalc_ps(local, latency_usec);
3629 mutex_unlock(&local->iflist_mtx);
3630
3631 return 0;
3632}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003633
Johannes Bergf2d9d272012-11-22 14:11:39 +01003634static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3635 struct cfg80211_bss *cbss)
3636{
3637 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3638 const u8 *ht_cap_ie, *vht_cap_ie;
3639 const struct ieee80211_ht_cap *ht_cap;
3640 const struct ieee80211_vht_cap *vht_cap;
3641 u8 chains = 1;
3642
3643 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3644 return chains;
3645
Johannes Berg9caf0362012-11-29 01:25:20 +01003646 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003647 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3648 ht_cap = (void *)(ht_cap_ie + 2);
3649 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3650 /*
3651 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3652 * "Tx Unequal Modulation Supported" fields.
3653 */
3654 }
3655
3656 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3657 return chains;
3658
Johannes Berg9caf0362012-11-29 01:25:20 +01003659 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003660 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3661 u8 nss;
3662 u16 tx_mcs_map;
3663
3664 vht_cap = (void *)(vht_cap_ie + 2);
3665 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3666 for (nss = 8; nss > 0; nss--) {
3667 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3668 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3669 break;
3670 }
3671 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3672 chains = max(chains, nss);
3673 }
3674
3675 return chains;
3676}
3677
Johannes Bergb17166a2012-07-27 11:41:27 +02003678static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3679 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003680{
3681 struct ieee80211_local *local = sdata->local;
3682 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003683 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003684 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003685 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003686 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003687 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003688
Johannes Berg24398e32012-03-28 10:58:36 +02003689 sband = local->hw.wiphy->bands[cbss->channel->band];
3690
Johannes Bergf2d9d272012-11-22 14:11:39 +01003691 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3692 IEEE80211_STA_DISABLE_80P80MHZ |
3693 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003694
Johannes Berg9caf0362012-11-29 01:25:20 +01003695 rcu_read_lock();
3696
Johannes Bergf2d9d272012-11-22 14:11:39 +01003697 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3698 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003699 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003700
Johannes Berg9caf0362012-11-29 01:25:20 +01003701 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003702 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3703 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003704
3705 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3706 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3707 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3708 ht_oper = NULL;
3709 }
Johannes Berg24398e32012-03-28 10:58:36 +02003710 }
3711
Johannes Bergf2d9d272012-11-22 14:11:39 +01003712 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3713 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003714 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003715
Johannes Berg9caf0362012-11-29 01:25:20 +01003716 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3717 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003718 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3719 vht_oper = (void *)(vht_oper_ie + 2);
3720 if (vht_oper && !ht_oper) {
3721 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003722 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003723 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003724 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3725 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003726 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003727
3728 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3729 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3730 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3731 vht_oper = NULL;
3732 }
Johannes Berg24398e32012-03-28 10:58:36 +02003733 }
3734
Johannes Bergf2d9d272012-11-22 14:11:39 +01003735 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3736 cbss->channel,
3737 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003738 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003739
Johannes Bergf2d9d272012-11-22 14:11:39 +01003740 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3741 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003742
Johannes Berg9caf0362012-11-29 01:25:20 +01003743 rcu_read_unlock();
3744
Johannes Berg04ecd252012-09-11 14:34:12 +02003745 /* will change later if needed */
3746 sdata->smps_mode = IEEE80211_SMPS_OFF;
3747
Johannes Bergf2d9d272012-11-22 14:11:39 +01003748 /*
3749 * If this fails (possibly due to channel context sharing
3750 * on incompatible channels, e.g. 80+80 and 160 sharing the
3751 * same control channel) try to use a smaller bandwidth.
3752 */
3753 ret = ieee80211_vif_use_channel(sdata, &chandef,
3754 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003755 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003756 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003757 ret = ieee80211_vif_use_channel(sdata, &chandef,
3758 IEEE80211_CHANCTX_SHARED);
3759 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003760 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003761}
3762
3763static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3764 struct cfg80211_bss *cbss, bool assoc)
3765{
3766 struct ieee80211_local *local = sdata->local;
3767 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3768 struct ieee80211_bss *bss = (void *)cbss->priv;
3769 struct sta_info *new_sta = NULL;
3770 bool have_sta = false;
3771 int err;
3772
3773 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3774 return -EINVAL;
3775
3776 if (assoc) {
3777 rcu_read_lock();
3778 have_sta = sta_info_get(sdata, cbss->bssid);
3779 rcu_read_unlock();
3780 }
3781
3782 if (!have_sta) {
3783 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3784 if (!new_sta)
3785 return -ENOMEM;
3786 }
3787
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003788 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003789 u32 rates = 0, basic_rates = 0;
3790 bool have_higher_than_11mbit;
3791 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003792 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003793 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003794
3795 sband = local->hw.wiphy->bands[cbss->channel->band];
3796
3797 err = ieee80211_prep_channel(sdata, cbss);
3798 if (err) {
3799 sta_info_free(local, new_sta);
3800 return err;
3801 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003802
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003803 ieee80211_get_rates(sband, bss->supp_rates,
3804 bss->supp_rates_len,
3805 &rates, &basic_rates,
3806 &have_higher_than_11mbit,
3807 &min_rate, &min_rate_index);
3808
3809 /*
3810 * This used to be a workaround for basic rates missing
3811 * in the association response frame. Now that we no
3812 * longer use the basic rates from there, it probably
3813 * doesn't happen any more, but keep the workaround so
3814 * in case some *other* APs are buggy in different ways
3815 * we can connect -- with a warning.
3816 */
3817 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003818 sdata_info(sdata,
3819 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003820 basic_rates = BIT(min_rate_index);
3821 }
3822
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003823 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003824 sdata->vif.bss_conf.basic_rates = basic_rates;
3825
3826 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003827 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003828 have_higher_than_11mbit)
3829 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3830 else
3831 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3832
3833 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3834
Johannes Berg8c358bc2012-05-22 22:13:05 +02003835 /* set timing information */
3836 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003837 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003838 ies = rcu_dereference(cbss->beacon_ies);
3839 if (ies) {
3840 const u8 *tim_ie;
3841
3842 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3843 sdata->vif.bss_conf.sync_device_ts =
3844 bss->device_ts_beacon;
3845 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3846 ies->data, ies->len);
3847 if (tim_ie && tim_ie[1] >= 2)
3848 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3849 else
3850 sdata->vif.bss_conf.sync_dtim_count = 0;
3851 } else if (!(local->hw.flags &
3852 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3853 ies = rcu_dereference(cbss->proberesp_ies);
3854 /* must be non-NULL since beacon IEs were NULL */
3855 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3856 sdata->vif.bss_conf.sync_device_ts =
3857 bss->device_ts_presp;
3858 sdata->vif.bss_conf.sync_dtim_count = 0;
3859 } else {
3860 sdata->vif.bss_conf.sync_tsf = 0;
3861 sdata->vif.bss_conf.sync_device_ts = 0;
3862 sdata->vif.bss_conf.sync_dtim_count = 0;
3863 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003864 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003865
3866 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003867 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003868 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3869 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003870
3871 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003872 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003873
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003874 err = sta_info_insert(new_sta);
3875 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003876 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003877 sdata_info(sdata,
3878 "failed to insert STA entry for the AP (error %d)\n",
3879 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003880 return err;
3881 }
3882 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003883 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003884
3885 return 0;
3886}
3887
Johannes Berg77fdaa12009-07-07 03:45:17 +02003888/* config hooks */
3889int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3890 struct cfg80211_auth_request *req)
3891{
Johannes Berg66e67e42012-01-20 13:55:27 +01003892 struct ieee80211_local *local = sdata->local;
3893 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3894 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003895 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003896 int err;
3897
3898 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003899
3900 switch (req->auth_type) {
3901 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3902 auth_alg = WLAN_AUTH_OPEN;
3903 break;
3904 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003905 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003906 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003907 auth_alg = WLAN_AUTH_SHARED_KEY;
3908 break;
3909 case NL80211_AUTHTYPE_FT:
3910 auth_alg = WLAN_AUTH_FT;
3911 break;
3912 case NL80211_AUTHTYPE_NETWORK_EAP:
3913 auth_alg = WLAN_AUTH_LEAP;
3914 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003915 case NL80211_AUTHTYPE_SAE:
3916 auth_alg = WLAN_AUTH_SAE;
3917 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003918 default:
3919 return -EOPNOTSUPP;
3920 }
3921
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003922 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3923 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003924 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003925 return -ENOMEM;
3926
Johannes Berg66e67e42012-01-20 13:55:27 +01003927 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003928
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003929 if (req->sae_data_len >= 4) {
3930 __le16 *pos = (__le16 *) req->sae_data;
3931 auth_data->sae_trans = le16_to_cpu(pos[0]);
3932 auth_data->sae_status = le16_to_cpu(pos[1]);
3933 memcpy(auth_data->data, req->sae_data + 4,
3934 req->sae_data_len - 4);
3935 auth_data->data_len += req->sae_data_len - 4;
3936 }
3937
Johannes Berg77fdaa12009-07-07 03:45:17 +02003938 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003939 memcpy(&auth_data->data[auth_data->data_len],
3940 req->ie, req->ie_len);
3941 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003942 }
3943
Johannes Bergfffd0932009-07-08 14:22:54 +02003944 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003945 auth_data->key_len = req->key_len;
3946 auth_data->key_idx = req->key_idx;
3947 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003948 }
3949
Johannes Berg66e67e42012-01-20 13:55:27 +01003950 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003951
Johannes Berg66e67e42012-01-20 13:55:27 +01003952 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003953
Johannes Berg66e67e42012-01-20 13:55:27 +01003954 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003955
Johannes Berg66e67e42012-01-20 13:55:27 +01003956 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3957 ifmgd->assoc_data) {
3958 err = -EBUSY;
3959 goto err_free;
3960 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003961
Johannes Berg66e67e42012-01-20 13:55:27 +01003962 if (ifmgd->auth_data)
3963 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003964
Johannes Berg66e67e42012-01-20 13:55:27 +01003965 /* prep auth_data so we don't go into idle on disassoc */
3966 ifmgd->auth_data = auth_data;
3967
3968 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003969 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003970
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003971 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003972
Johannes Berga1cf7752012-03-08 15:02:07 +01003973 err = ieee80211_prep_connection(sdata, req->bss, false);
3974 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003975 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003976
Johannes Berg66e67e42012-01-20 13:55:27 +01003977 err = ieee80211_probe_auth(sdata);
3978 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003979 sta_info_destroy_addr(sdata, req->bss->bssid);
3980 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003981 }
3982
Johannes Berg66e67e42012-01-20 13:55:27 +01003983 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003984 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003985 err = 0;
3986 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003987
Johannes Berg66e67e42012-01-20 13:55:27 +01003988 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003989 memset(ifmgd->bssid, 0, ETH_ALEN);
3990 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003991 ifmgd->auth_data = NULL;
3992 err_free:
3993 kfree(auth_data);
3994 out_unlock:
3995 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003996
Johannes Berg66e67e42012-01-20 13:55:27 +01003997 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003998}
3999
Johannes Berg77fdaa12009-07-07 03:45:17 +02004000int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4001 struct cfg80211_assoc_request *req)
4002{
Johannes Berg66e67e42012-01-20 13:55:27 +01004003 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004004 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004005 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004006 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004007 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004008 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004009 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004010 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004011
Johannes Berg66e67e42012-01-20 13:55:27 +01004012 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4013 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004014 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004015
Johannes Berg9caf0362012-11-29 01:25:20 +01004016 rcu_read_lock();
4017 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4018 if (!ssidie) {
4019 rcu_read_unlock();
4020 kfree(assoc_data);
4021 return -EINVAL;
4022 }
4023 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4024 assoc_data->ssid_len = ssidie[1];
4025 rcu_read_unlock();
4026
Johannes Berg66e67e42012-01-20 13:55:27 +01004027 mutex_lock(&ifmgd->mtx);
4028
4029 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01004030 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01004031
4032 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4033 err = -EBUSY;
4034 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004035 }
4036
Johannes Berg66e67e42012-01-20 13:55:27 +01004037 if (ifmgd->assoc_data) {
4038 err = -EBUSY;
4039 goto err_free;
4040 }
4041
4042 if (ifmgd->auth_data) {
4043 bool match;
4044
4045 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004046 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004047 ieee80211_destroy_auth_data(sdata, match);
4048 }
4049
4050 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02004051
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004052 ifmgd->beacon_crc_valid = false;
4053
Johannes Bergde5036a2012-03-08 15:02:03 +01004054 /*
4055 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4056 * We still associate in non-HT mode (11a/b/g) if any one of these
4057 * ciphers is configured as pairwise.
4058 * We can set this to true for non-11n hardware, that'll be checked
4059 * separately along with the peer capabilities.
4060 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004061 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004062 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4063 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004064 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004065 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004066 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004067 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004068 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004069 }
4070 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004071
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004072 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004073 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004074 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4075 }
Ben Greearef96a8422011-11-18 11:32:00 -08004076
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004077 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4078 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4079
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004080 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4081 sband = local->hw.wiphy->bands[req->bss->channel->band];
4082 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004083 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004084 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004085 if (!bss->wmm_used)
4086 netdev_info(sdata->dev,
4087 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004088 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004089
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004090 /* disable VHT if we don't support it or the AP doesn't use WMM */
4091 if (!sband->vht_cap.vht_supported ||
4092 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4093 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004094 if (!bss->wmm_used)
4095 netdev_info(sdata->dev,
4096 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004097 }
4098
Ben Greearef96a8422011-11-18 11:32:00 -08004099 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4100 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4101 sizeof(ifmgd->ht_capa_mask));
4102
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004103 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4104 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4105 sizeof(ifmgd->vht_capa_mask));
4106
Johannes Berg77fdaa12009-07-07 03:45:17 +02004107 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004108 memcpy(assoc_data->ie, req->ie, req->ie_len);
4109 assoc_data->ie_len = req->ie_len;
4110 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004111
Johannes Berg66e67e42012-01-20 13:55:27 +01004112 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004113
Johannes Bergaf6b6372009-12-23 13:15:35 +01004114 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4115 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004116 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004117 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004118 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004119 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004120 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004121
Johannes Berg66e67e42012-01-20 13:55:27 +01004122 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004123 assoc_data->wmm = bss->wmm_used &&
4124 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004125 assoc_data->supp_rates = bss->supp_rates;
4126 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004127
Johannes Berg9caf0362012-11-29 01:25:20 +01004128 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004129 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4130 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4131 assoc_data->ap_ht_param =
4132 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4133 else
Johannes Berga8243b72012-11-22 14:32:09 +01004134 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004135 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4136 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4137 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4138 sizeof(struct ieee80211_vht_cap));
4139 else
4140 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004141 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004142
Kalle Valoab133152010-01-12 10:42:31 +02004143 if (bss->wmm_used && bss->uapsd_supported &&
4144 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01004145 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004146 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4147 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004148 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004149 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4150 }
4151
Johannes Berg77fdaa12009-07-07 03:45:17 +02004152 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004153 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004154
4155 if (req->use_mfp) {
4156 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4157 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4158 } else {
4159 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4160 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4161 }
4162
4163 if (req->crypto.control_port)
4164 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4165 else
4166 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4167
Johannes Berga621fa42010-08-27 14:26:54 +03004168 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4169 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4170
Johannes Berg66e67e42012-01-20 13:55:27 +01004171 /* kick off associate process */
4172
4173 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004174 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004175
Johannes Berga1cf7752012-03-08 15:02:07 +01004176 err = ieee80211_prep_connection(sdata, req->bss, true);
4177 if (err)
4178 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004179
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004180 rcu_read_lock();
4181 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004182
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004183 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4184 !beacon_ies) {
4185 /*
4186 * Wait up to one beacon interval ...
4187 * should this be more if we miss one?
4188 */
4189 sdata_info(sdata, "waiting for beacon from %pM\n",
4190 ifmgd->bssid);
4191 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004192 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004193 assoc_data->need_beacon = true;
4194 } else if (beacon_ies) {
4195 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4196 beacon_ies->data,
4197 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004198 u8 dtim_count = 0;
4199
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004200 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4201 const struct ieee80211_tim_ie *tim;
4202 tim = (void *)(tim_ie + 2);
4203 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004204 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004205 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004206 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004207 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004208 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004209
4210 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4211 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4212 sdata->vif.bss_conf.sync_device_ts =
4213 bss->device_ts_beacon;
4214 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4215 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004216 } else {
4217 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004218 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004219 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004220 rcu_read_unlock();
4221
Johannes Berg66e67e42012-01-20 13:55:27 +01004222 run_again(ifmgd, assoc_data->timeout);
4223
Paul Stewartfcff4f12012-02-23 17:59:53 -08004224 if (bss->corrupt_data) {
4225 char *corrupt_type = "data";
4226 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4227 if (bss->corrupt_data &
4228 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4229 corrupt_type = "beacon and probe response";
4230 else
4231 corrupt_type = "beacon";
4232 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4233 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004234 sdata_info(sdata, "associating with AP with corrupt %s\n",
4235 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004236 }
4237
Johannes Berg66e67e42012-01-20 13:55:27 +01004238 err = 0;
4239 goto out;
4240 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004241 memset(ifmgd->bssid, 0, ETH_ALEN);
4242 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004243 ifmgd->assoc_data = NULL;
4244 err_free:
4245 kfree(assoc_data);
4246 out:
4247 mutex_unlock(&ifmgd->mtx);
4248
4249 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004250}
4251
4252int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004253 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004254{
4255 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004256 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004257 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004258 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004259
Johannes Berg77fdaa12009-07-07 03:45:17 +02004260 mutex_lock(&ifmgd->mtx);
4261
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004262 sdata_info(sdata,
4263 "deauthenticating from %pM by local choice (reason=%d)\n",
4264 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004265
Johannes Berg86552012012-10-29 09:46:31 +01004266 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004267 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004268 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4269 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004270 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004271 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004272 ieee80211_destroy_auth_data(sdata, false);
4273 mutex_unlock(&ifmgd->mtx);
4274
4275 sent_frame = tx;
4276 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004277 }
4278
Johannes Berg86552012012-10-29 09:46:31 +01004279 if (ifmgd->associated &&
4280 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4281 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4282 req->reason_code, tx, frame_buf);
4283 sent_frame = tx;
4284 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004285 mutex_unlock(&ifmgd->mtx);
4286
Johannes Berg86552012012-10-29 09:46:31 +01004287 out:
Johannes Berg86552012012-10-29 09:46:31 +01004288 if (sent_frame)
4289 __cfg80211_send_deauth(sdata->dev, frame_buf,
4290 IEEE80211_DEAUTH_FRAME_LEN);
4291
Johannes Berg77fdaa12009-07-07 03:45:17 +02004292 return 0;
4293}
4294
4295int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004296 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004297{
4298 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004299 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004300 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004301
Johannes Berg77fdaa12009-07-07 03:45:17 +02004302 mutex_lock(&ifmgd->mtx);
4303
Johannes Berg8d8b2612009-07-25 11:58:36 +02004304 /*
4305 * cfg80211 should catch this ... but it's racy since
4306 * we can receive a disassoc frame, process it, hand it
4307 * to cfg80211 while that's in a locked section already
4308 * trying to tell us that the user wants to disconnect.
4309 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004310 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004311 mutex_unlock(&ifmgd->mtx);
4312 return -ENOLINK;
4313 }
4314
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004315 sdata_info(sdata,
4316 "disassociating from %pM by local choice (reason=%d)\n",
4317 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004318
Jouni Malinene69e95d2010-03-29 23:29:31 -07004319 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004320 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4321 req->reason_code, !req->local_state_change,
4322 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004323 mutex_unlock(&ifmgd->mtx);
4324
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004325 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4326 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004327
Johannes Berg77fdaa12009-07-07 03:45:17 +02004328 return 0;
4329}
Jouni Malinen026331c2010-02-15 12:53:10 +02004330
Eliad Pellerafa762f2012-04-23 14:45:15 +03004331void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004332{
4333 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4334
Ben Greear49921852013-02-20 09:41:09 -08004335 /*
4336 * Make sure some work items will not run after this,
4337 * they will not do anything but might not have been
4338 * cancelled when disconnecting.
4339 */
4340 cancel_work_sync(&ifmgd->monitor_work);
4341 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4342 cancel_work_sync(&ifmgd->request_smps_work);
4343 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4344 cancel_work_sync(&ifmgd->chswitch_work);
4345
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004346 mutex_lock(&ifmgd->mtx);
4347 if (ifmgd->assoc_data)
4348 ieee80211_destroy_assoc_data(sdata, false);
4349 if (ifmgd->auth_data)
4350 ieee80211_destroy_auth_data(sdata, false);
4351 del_timer_sync(&ifmgd->timer);
4352 mutex_unlock(&ifmgd->mtx);
4353}
4354
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004355void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4356 enum nl80211_cqm_rssi_threshold_event rssi_event,
4357 gfp_t gfp)
4358{
4359 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4360
Johannes Bergb5878a22010-04-07 16:48:40 +02004361 trace_api_cqm_rssi_notify(sdata, rssi_event);
4362
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004363 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4364}
4365EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);