blob: 4d383a93ea734c3128ed59d0e3e00668c6f4636f [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 Berg77fdaa12009-07-07 03:45:17 +020090/*
91 * All cfg80211 functions have to be called outside a locked
92 * section so that they can acquire a lock themselves... This
93 * is much simpler than queuing up things in cfg80211, but we
94 * do need some indirection for that here.
95 */
96enum rx_mgmt_action {
97 /* no action required */
98 RX_MGMT_NONE,
99
Johannes Berg77fdaa12009-07-07 03:45:17 +0200100 /* caller must call cfg80211_send_deauth() */
101 RX_MGMT_CFG80211_DEAUTH,
102
103 /* caller must call cfg80211_send_disassoc() */
104 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100105
106 /* caller must call cfg80211_send_rx_auth() */
107 RX_MGMT_CFG80211_RX_AUTH,
108
109 /* caller must call cfg80211_send_rx_assoc() */
110 RX_MGMT_CFG80211_RX_ASSOC,
111
112 /* caller must call cfg80211_send_assoc_timeout() */
113 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100114
115 /* used when a processed beacon causes a deauth */
116 RX_MGMT_CFG80211_TX_DEAUTH,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200117};
118
Johannes Berg5484e232008-09-08 17:44:27 +0200119/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200122 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123}
124
Johannes Bergca386f32009-07-10 02:39:48 +0200125/*
126 * We can have multiple work items (and connection probing)
127 * scheduling this timer, but we need to take care to only
128 * reschedule it when it should fire _earlier_ than it was
129 * asked for before, or if it's not pending right now. This
130 * function ensures that. Note that it then is required to
131 * run this function for all timeouts after the first one
132 * has happened -- the work that runs from this timer will
133 * do that.
134 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100135static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200136{
137 ASSERT_MGD_MTX(ifmgd);
138
139 if (!timer_pending(&ifmgd->timer) ||
140 time_before(timeout, ifmgd->timer.expires))
141 mod_timer(&ifmgd->timer, timeout);
142}
143
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400144void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145{
Johannes Bergc1288b12012-01-19 09:29:57 +0100146 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200147 return;
148
Johannes Berg7eeff742012-07-18 10:27:27 +0200149 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
150 return;
151
Johannes Bergb291ba12009-07-10 15:29:03 +0200152 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100153 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200154}
155
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400156void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
157{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400158 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
159
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100160 if (unlikely(!sdata->u.mgd.associated))
161 return;
162
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400163 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
164 return;
165
166 mod_timer(&sdata->u.mgd.conn_mon_timer,
167 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400168
169 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400170}
171
Johannes Berg5484e232008-09-08 17:44:27 +0200172static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200173{
Johannes Berg5484e232008-09-08 17:44:27 +0200174 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200175}
176
Johannes Berg6565ec92013-02-08 14:52:32 +0100177static u32 chandef_downgrade(struct cfg80211_chan_def *c)
178{
179 u32 ret;
180 int tmp;
181
182 switch (c->width) {
183 case NL80211_CHAN_WIDTH_20:
184 c->width = NL80211_CHAN_WIDTH_20_NOHT;
185 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
186 break;
187 case NL80211_CHAN_WIDTH_40:
188 c->width = NL80211_CHAN_WIDTH_20;
189 c->center_freq1 = c->chan->center_freq;
190 ret = IEEE80211_STA_DISABLE_40MHZ |
191 IEEE80211_STA_DISABLE_VHT;
192 break;
193 case NL80211_CHAN_WIDTH_80:
194 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
195 /* n_P40 */
196 tmp /= 2;
197 /* freq_P40 */
198 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
199 c->width = NL80211_CHAN_WIDTH_40;
200 ret = IEEE80211_STA_DISABLE_VHT;
201 break;
202 case NL80211_CHAN_WIDTH_80P80:
203 c->center_freq2 = 0;
204 c->width = NL80211_CHAN_WIDTH_80;
205 ret = IEEE80211_STA_DISABLE_80P80MHZ |
206 IEEE80211_STA_DISABLE_160MHZ;
207 break;
208 case NL80211_CHAN_WIDTH_160:
209 /* n_P20 */
210 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
211 /* n_P80 */
212 tmp /= 4;
213 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
214 c->width = NL80211_CHAN_WIDTH_80;
215 ret = IEEE80211_STA_DISABLE_80P80MHZ |
216 IEEE80211_STA_DISABLE_160MHZ;
217 break;
218 default:
219 case NL80211_CHAN_WIDTH_20_NOHT:
220 WARN_ON_ONCE(1);
221 c->width = NL80211_CHAN_WIDTH_20_NOHT;
222 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
223 break;
224 }
225
226 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
227
228 return ret;
229}
230
231static u32
232ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
233 struct ieee80211_supported_band *sband,
234 struct ieee80211_channel *channel,
235 const struct ieee80211_ht_operation *ht_oper,
236 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100237 struct cfg80211_chan_def *chandef, bool verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100238{
239 struct cfg80211_chan_def vht_chandef;
240 u32 ht_cfreq, ret;
241
242 chandef->chan = channel;
243 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
244 chandef->center_freq1 = channel->center_freq;
245 chandef->center_freq2 = 0;
246
247 if (!ht_oper || !sband->ht_cap.ht_supported) {
248 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
249 goto out;
250 }
251
252 chandef->width = NL80211_CHAN_WIDTH_20;
253
254 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
255 channel->band);
256 /* check that channel matches the right operating channel */
257 if (channel->center_freq != ht_cfreq) {
258 /*
259 * It's possible that some APs are confused here;
260 * Netgear WNDR3700 sometimes reports 4 higher than
261 * the actual channel in association responses, but
262 * since we look at probe response/beacon data here
263 * it should be OK.
264 */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100265 if (verbose)
266 sdata_info(sdata,
267 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
268 channel->center_freq, ht_cfreq,
269 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100270 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
271 goto out;
272 }
273
274 /* check 40 MHz support, if we have it */
275 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
276 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
277 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
278 chandef->width = NL80211_CHAN_WIDTH_40;
279 chandef->center_freq1 += 10;
280 break;
281 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
282 chandef->width = NL80211_CHAN_WIDTH_40;
283 chandef->center_freq1 -= 10;
284 break;
285 }
286 } else {
287 /* 40 MHz (and 80 MHz) must be supported for VHT */
288 ret = IEEE80211_STA_DISABLE_VHT;
289 goto out;
290 }
291
292 if (!vht_oper || !sband->vht_cap.vht_supported) {
293 ret = IEEE80211_STA_DISABLE_VHT;
294 goto out;
295 }
296
297 vht_chandef.chan = channel;
298 vht_chandef.center_freq1 =
299 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
300 channel->band);
301 vht_chandef.center_freq2 = 0;
302
303 if (vht_oper->center_freq_seg2_idx)
304 vht_chandef.center_freq2 =
305 ieee80211_channel_to_frequency(
306 vht_oper->center_freq_seg2_idx,
307 channel->band);
308
309 switch (vht_oper->chan_width) {
310 case IEEE80211_VHT_CHANWIDTH_USE_HT:
311 vht_chandef.width = chandef->width;
312 break;
313 case IEEE80211_VHT_CHANWIDTH_80MHZ:
314 vht_chandef.width = NL80211_CHAN_WIDTH_80;
315 break;
316 case IEEE80211_VHT_CHANWIDTH_160MHZ:
317 vht_chandef.width = NL80211_CHAN_WIDTH_160;
318 break;
319 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
320 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
321 break;
322 default:
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100323 if (verbose)
324 sdata_info(sdata,
325 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
326 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100327 ret = IEEE80211_STA_DISABLE_VHT;
328 goto out;
329 }
330
331 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100332 if (verbose)
333 sdata_info(sdata,
334 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100335 ret = IEEE80211_STA_DISABLE_VHT;
336 goto out;
337 }
338
339 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
340 ret = 0;
341 goto out;
342 }
343
344 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100345 if (verbose)
346 sdata_info(sdata,
347 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100348 ret = IEEE80211_STA_DISABLE_VHT;
349 goto out;
350 }
351
352 *chandef = vht_chandef;
353
354 ret = 0;
355
356out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100357 /* don't print the message below for VHT mismatch if VHT is disabled */
358 if (ret & IEEE80211_STA_DISABLE_VHT)
359 vht_chandef = *chandef;
360
Johannes Berg6565ec92013-02-08 14:52:32 +0100361 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
362 IEEE80211_CHAN_DISABLED)) {
363 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
364 ret = IEEE80211_STA_DISABLE_HT |
365 IEEE80211_STA_DISABLE_VHT;
366 goto out;
367 }
368
369 ret |= chandef_downgrade(chandef);
370 }
371
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100372 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100373 sdata_info(sdata,
374 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
375
376 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
377 return ret;
378}
379
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100380static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
381 struct sta_info *sta,
382 const struct ieee80211_ht_operation *ht_oper,
383 const struct ieee80211_vht_operation *vht_oper,
384 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200385{
386 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100387 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200388 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200389 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100390 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200391 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100392 u32 flags;
393 enum ieee80211_sta_rx_bandwidth new_sta_bw;
394 int ret;
395
396 /* if HT was/is disabled, don't track any bandwidth changes */
397 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
398 return 0;
399
400 /* don't check VHT if we associated as non-VHT station */
401 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
402 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200403
Johannes Berg11289582013-02-07 17:36:12 +0100404 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100405 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100406
Johannes Bergf2d93302013-02-08 13:15:55 +0100407 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200408 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200409
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100410 /* calculate new channel (type) based on HT/VHT operation IEs */
411 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
412 vht_oper, &chandef, false);
413
414 /*
415 * Downgrade the new channel if we associated with restricted
416 * capabilities. For example, if we associated as a 20 MHz STA
417 * to a 40 MHz AP (due to regulatory, capabilities or config
418 * reasons) then switching to a 40 MHz channel now won't do us
419 * any good -- we couldn't use it with the AP.
420 */
421 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
422 chandef.width == NL80211_CHAN_WIDTH_80P80)
423 flags |= chandef_downgrade(&chandef);
424 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
425 chandef.width == NL80211_CHAN_WIDTH_160)
426 flags |= chandef_downgrade(&chandef);
427 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
428 chandef.width > NL80211_CHAN_WIDTH_20)
429 flags |= chandef_downgrade(&chandef);
430
431 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
432 return 0;
433
434 sdata_info(sdata,
435 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
436 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
437 chandef.center_freq1, chandef.center_freq2);
438
439 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
440 IEEE80211_STA_DISABLE_VHT |
441 IEEE80211_STA_DISABLE_40MHZ |
442 IEEE80211_STA_DISABLE_80P80MHZ |
443 IEEE80211_STA_DISABLE_160MHZ)) ||
444 !cfg80211_chandef_valid(&chandef)) {
445 sdata_info(sdata,
446 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
447 ifmgd->bssid);
448 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200449 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200450
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100451 switch (chandef.width) {
452 case NL80211_CHAN_WIDTH_20_NOHT:
453 case NL80211_CHAN_WIDTH_20:
454 new_sta_bw = IEEE80211_STA_RX_BW_20;
455 break;
456 case NL80211_CHAN_WIDTH_40:
457 new_sta_bw = IEEE80211_STA_RX_BW_40;
458 break;
459 case NL80211_CHAN_WIDTH_80:
460 new_sta_bw = IEEE80211_STA_RX_BW_80;
461 break;
462 case NL80211_CHAN_WIDTH_80P80:
463 case NL80211_CHAN_WIDTH_160:
464 new_sta_bw = IEEE80211_STA_RX_BW_160;
465 break;
466 default:
467 return -EINVAL;
468 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200469
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100470 if (new_sta_bw > sta->cur_max_bandwidth)
471 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200472
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100473 if (new_sta_bw < sta->sta.bandwidth) {
474 sta->sta.bandwidth = new_sta_bw;
475 rate_control_rate_update(local, sband, sta,
476 IEEE80211_RC_BW_CHANGED);
477 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530478
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100479 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
480 if (ret) {
481 sdata_info(sdata,
482 "AP %pM changed bandwidth to incompatible one - disconnect\n",
483 ifmgd->bssid);
484 return ret;
485 }
486
487 if (new_sta_bw > sta->sta.bandwidth) {
488 sta->sta.bandwidth = new_sta_bw;
489 rate_control_rate_update(local, sband, sta,
490 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200491 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200492
Johannes Berg074d46d2012-03-15 19:45:16 +0100493 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200494
495 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100496 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
497 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200498 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200499 }
500
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100501 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200502}
503
Johannes Berg9c6bd792008-09-11 00:01:52 +0200504/* frame sending functions */
505
Johannes Berg66e67e42012-01-20 13:55:27 +0100506static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
507 struct ieee80211_supported_band *sband,
508 u32 *rates)
509{
510 int i, j, count;
511 *rates = 0;
512 count = 0;
513 for (i = 0; i < supp_rates_len; i++) {
514 int rate = (supp_rates[i] & 0x7F) * 5;
515
516 for (j = 0; j < sband->n_bitrates; j++)
517 if (sband->bitrates[j].bitrate == rate) {
518 *rates |= BIT(j);
519 count++;
520 break;
521 }
522 }
523
524 return count;
525}
526
527static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200528 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100529 struct ieee80211_supported_band *sband,
530 struct ieee80211_channel *channel,
531 enum ieee80211_smps_mode smps)
532{
Johannes Berg66e67e42012-01-20 13:55:27 +0100533 u8 *pos;
534 u32 flags = channel->flags;
535 u16 cap;
536 struct ieee80211_sta_ht_cap ht_cap;
537
538 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
539
Johannes Berg66e67e42012-01-20 13:55:27 +0100540 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
541 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
542
Johannes Berg66e67e42012-01-20 13:55:27 +0100543 /* determine capability flags */
544 cap = ht_cap.cap;
545
Johannes Berg9dde6422012-05-16 23:43:19 +0200546 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100547 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
548 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
549 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
550 cap &= ~IEEE80211_HT_CAP_SGI_40;
551 }
552 break;
553 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
554 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
555 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
556 cap &= ~IEEE80211_HT_CAP_SGI_40;
557 }
558 break;
559 }
560
Johannes Berg24398e32012-03-28 10:58:36 +0200561 /*
562 * If 40 MHz was disabled associate as though we weren't
563 * capable of 40 MHz -- some broken APs will never fall
564 * back to trying to transmit in 20 MHz.
565 */
566 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
567 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
568 cap &= ~IEEE80211_HT_CAP_SGI_40;
569 }
570
Johannes Berg66e67e42012-01-20 13:55:27 +0100571 /* set SM PS mode properly */
572 cap &= ~IEEE80211_HT_CAP_SM_PS;
573 switch (smps) {
574 case IEEE80211_SMPS_AUTOMATIC:
575 case IEEE80211_SMPS_NUM_MODES:
576 WARN_ON(1);
577 case IEEE80211_SMPS_OFF:
578 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
579 IEEE80211_HT_CAP_SM_PS_SHIFT;
580 break;
581 case IEEE80211_SMPS_STATIC:
582 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
583 IEEE80211_HT_CAP_SM_PS_SHIFT;
584 break;
585 case IEEE80211_SMPS_DYNAMIC:
586 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
587 IEEE80211_HT_CAP_SM_PS_SHIFT;
588 break;
589 }
590
591 /* reserve and fill IE */
592 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
593 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
594}
595
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000596static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
597 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100598 struct ieee80211_supported_band *sband,
599 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000600{
601 u8 *pos;
602 u32 cap;
603 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100604 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000605
606 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
607
608 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100609 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000610
611 /* determine capability flags */
612 cap = vht_cap.cap;
613
Johannes Bergf2d9d272012-11-22 14:11:39 +0100614 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
615 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
616 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
617 }
618
619 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
620 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
621 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
622 }
623
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100624 /*
625 * Some APs apparently get confused if our capabilities are better
626 * than theirs, so restrict what we advertise in the assoc request.
627 */
628 if (!(ap_vht_cap->vht_cap_info &
629 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
630 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
631
632 if (!(ap_vht_cap->vht_cap_info &
633 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
634 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
635 IEEE80211_VHT_CAP_RXSTBC_3 |
636 IEEE80211_VHT_CAP_RXSTBC_4);
637
638 for (i = 0; i < 8; i++) {
639 int shift = i * 2;
640 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
641 u16 ap_mcs, our_mcs;
642
643 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
644 mask) >> shift;
645 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
646 mask) >> shift;
647
Johannes Berg24af7172013-03-01 17:33:18 +0100648 if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
649 continue;
650
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100651 switch (ap_mcs) {
652 default:
653 if (our_mcs <= ap_mcs)
654 break;
655 /* fall through */
656 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
657 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
658 vht_cap.vht_mcs.rx_mcs_map |=
659 cpu_to_le16(ap_mcs << shift);
660 }
661 }
662
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000663 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000664 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000665 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
666}
667
Johannes Berg66e67e42012-01-20 13:55:27 +0100668static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
669{
670 struct ieee80211_local *local = sdata->local;
671 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
672 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
673 struct sk_buff *skb;
674 struct ieee80211_mgmt *mgmt;
675 u8 *pos, qos_info;
676 size_t offset = 0, noffset;
677 int i, count, rates_len, supp_rates_len;
678 u16 capab;
679 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200680 struct ieee80211_chanctx_conf *chanctx_conf;
681 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100682 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100683
684 lockdep_assert_held(&ifmgd->mtx);
685
Johannes Berg55de9082012-07-26 17:24:39 +0200686 rcu_read_lock();
687 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
688 if (WARN_ON(!chanctx_conf)) {
689 rcu_read_unlock();
690 return;
691 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100692 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200693 rcu_read_unlock();
694 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100695
696 if (assoc_data->supp_rates_len) {
697 /*
698 * Get all rates supported by the device and the AP as
699 * some APs don't like getting a superset of their rates
700 * in the association request (e.g. D-Link DAP 1353 in
701 * b-only mode)...
702 */
703 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
704 assoc_data->supp_rates_len,
705 sband, &rates);
706 } else {
707 /*
708 * In case AP not provide any supported rates information
709 * before association, we send information element(s) with
710 * all rates that we support.
711 */
712 rates = ~0;
713 rates_len = sband->n_bitrates;
714 }
715
716 skb = alloc_skb(local->hw.extra_tx_headroom +
717 sizeof(*mgmt) + /* bit too much but doesn't matter */
718 2 + assoc_data->ssid_len + /* SSID */
719 4 + rates_len + /* (extended) rates */
720 4 + /* power capability */
721 2 + 2 * sband->n_channels + /* supported channels */
722 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000723 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100724 assoc_data->ie_len + /* extra IEs */
725 9, /* WMM */
726 GFP_KERNEL);
727 if (!skb)
728 return;
729
730 skb_reserve(skb, local->hw.extra_tx_headroom);
731
732 capab = WLAN_CAPABILITY_ESS;
733
734 if (sband->band == IEEE80211_BAND_2GHZ) {
735 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
736 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
737 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
738 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
739 }
740
741 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
742 capab |= WLAN_CAPABILITY_PRIVACY;
743
744 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
745 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
746 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
747
748 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
749 memset(mgmt, 0, 24);
750 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
751 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
752 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
753
754 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
755 skb_put(skb, 10);
756 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
757 IEEE80211_STYPE_REASSOC_REQ);
758 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
759 mgmt->u.reassoc_req.listen_interval =
760 cpu_to_le16(local->hw.conf.listen_interval);
761 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
762 ETH_ALEN);
763 } else {
764 skb_put(skb, 4);
765 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
766 IEEE80211_STYPE_ASSOC_REQ);
767 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
768 mgmt->u.assoc_req.listen_interval =
769 cpu_to_le16(local->hw.conf.listen_interval);
770 }
771
772 /* SSID */
773 pos = skb_put(skb, 2 + assoc_data->ssid_len);
774 *pos++ = WLAN_EID_SSID;
775 *pos++ = assoc_data->ssid_len;
776 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
777
778 /* add all rates which were marked to be used above */
779 supp_rates_len = rates_len;
780 if (supp_rates_len > 8)
781 supp_rates_len = 8;
782
783 pos = skb_put(skb, supp_rates_len + 2);
784 *pos++ = WLAN_EID_SUPP_RATES;
785 *pos++ = supp_rates_len;
786
787 count = 0;
788 for (i = 0; i < sband->n_bitrates; i++) {
789 if (BIT(i) & rates) {
790 int rate = sband->bitrates[i].bitrate;
791 *pos++ = (u8) (rate / 5);
792 if (++count == 8)
793 break;
794 }
795 }
796
797 if (rates_len > count) {
798 pos = skb_put(skb, rates_len - count + 2);
799 *pos++ = WLAN_EID_EXT_SUPP_RATES;
800 *pos++ = rates_len - count;
801
802 for (i++; i < sband->n_bitrates; i++) {
803 if (BIT(i) & rates) {
804 int rate = sband->bitrates[i].bitrate;
805 *pos++ = (u8) (rate / 5);
806 }
807 }
808 }
809
810 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
811 /* 1. power capabilities */
812 pos = skb_put(skb, 4);
813 *pos++ = WLAN_EID_PWR_CAPABILITY;
814 *pos++ = 2;
815 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200816 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100817
818 /* 2. supported channels */
819 /* TODO: get this in reg domain format */
820 pos = skb_put(skb, 2 * sband->n_channels + 2);
821 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
822 *pos++ = 2 * sband->n_channels;
823 for (i = 0; i < sband->n_channels; i++) {
824 *pos++ = ieee80211_frequency_to_channel(
825 sband->channels[i].center_freq);
826 *pos++ = 1; /* one channel in the subband*/
827 }
828 }
829
830 /* if present, add any custom IEs that go before HT */
831 if (assoc_data->ie_len && assoc_data->ie) {
832 static const u8 before_ht[] = {
833 WLAN_EID_SSID,
834 WLAN_EID_SUPP_RATES,
835 WLAN_EID_EXT_SUPP_RATES,
836 WLAN_EID_PWR_CAPABILITY,
837 WLAN_EID_SUPPORTED_CHANNELS,
838 WLAN_EID_RSN,
839 WLAN_EID_QOS_CAPA,
840 WLAN_EID_RRM_ENABLED_CAPABILITIES,
841 WLAN_EID_MOBILITY_DOMAIN,
842 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
843 };
844 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
845 before_ht, ARRAY_SIZE(before_ht),
846 offset);
847 pos = skb_put(skb, noffset - offset);
848 memcpy(pos, assoc_data->ie + offset, noffset - offset);
849 offset = noffset;
850 }
851
Johannes Bergf2d9d272012-11-22 14:11:39 +0100852 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
853 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
854 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
855
Johannes Berga8243b72012-11-22 14:32:09 +0100856 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200857 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200858 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100859
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000860 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100861 ieee80211_add_vht_ie(sdata, skb, sband,
862 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000863
Johannes Berg66e67e42012-01-20 13:55:27 +0100864 /* if present, add any custom non-vendor IEs that go after HT */
865 if (assoc_data->ie_len && assoc_data->ie) {
866 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
867 assoc_data->ie_len,
868 offset);
869 pos = skb_put(skb, noffset - offset);
870 memcpy(pos, assoc_data->ie + offset, noffset - offset);
871 offset = noffset;
872 }
873
Johannes Berg76f03032012-03-08 15:02:05 +0100874 if (assoc_data->wmm) {
875 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200876 qos_info = ifmgd->uapsd_queues;
877 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100878 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
879 } else {
880 qos_info = 0;
881 }
882
883 pos = skb_put(skb, 9);
884 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
885 *pos++ = 7; /* len */
886 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
887 *pos++ = 0x50;
888 *pos++ = 0xf2;
889 *pos++ = 2; /* WME */
890 *pos++ = 0; /* WME info */
891 *pos++ = 1; /* WME ver */
892 *pos++ = qos_info;
893 }
894
895 /* add any remaining custom (i.e. vendor specific here) IEs */
896 if (assoc_data->ie_len && assoc_data->ie) {
897 noffset = assoc_data->ie_len;
898 pos = skb_put(skb, noffset - offset);
899 memcpy(pos, assoc_data->ie + offset, noffset - offset);
900 }
901
Johannes Berga1845fc2012-06-27 13:18:36 +0200902 drv_mgd_prepare_tx(local, sdata);
903
Johannes Berg66e67e42012-01-20 13:55:27 +0100904 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100905 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
906 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
907 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100908 ieee80211_tx_skb(sdata, skb);
909}
910
Kalle Valo572e0012009-02-10 17:09:31 +0200911void ieee80211_send_pspoll(struct ieee80211_local *local,
912 struct ieee80211_sub_if_data *sdata)
913{
Kalle Valo572e0012009-02-10 17:09:31 +0200914 struct ieee80211_pspoll *pspoll;
915 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200916
Kalle Valod8cd1892010-01-05 20:16:26 +0200917 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
918 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200919 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200920
Kalle Valod8cd1892010-01-05 20:16:26 +0200921 pspoll = (struct ieee80211_pspoll *) skb->data;
922 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200923
Johannes Berg62ae67b2009-11-18 18:42:05 +0100924 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
925 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200926}
927
Johannes Berg965beda2009-04-16 13:17:24 +0200928void ieee80211_send_nullfunc(struct ieee80211_local *local,
929 struct ieee80211_sub_if_data *sdata,
930 int powersave)
931{
932 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200933 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530934 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200935
Kalle Valod8cd1892010-01-05 20:16:26 +0200936 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
937 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200938 return;
939
Kalle Valod8cd1892010-01-05 20:16:26 +0200940 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200941 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200942 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200943
Seth Forshee6c17b772013-02-11 11:21:07 -0600944 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
945 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530946 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
947 IEEE80211_STA_CONNECTION_POLL))
948 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
949
Johannes Berg62ae67b2009-11-18 18:42:05 +0100950 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200951}
952
Felix Fietkaud5242152010-01-08 18:06:26 +0100953static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
954 struct ieee80211_sub_if_data *sdata)
955{
956 struct sk_buff *skb;
957 struct ieee80211_hdr *nullfunc;
958 __le16 fc;
959
960 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
961 return;
962
963 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700964 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100965 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700966
Felix Fietkaud5242152010-01-08 18:06:26 +0100967 skb_reserve(skb, local->hw.extra_tx_headroom);
968
969 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
970 memset(nullfunc, 0, 30);
971 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
972 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
973 nullfunc->frame_control = fc;
974 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
975 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
976 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
977 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
978
979 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
980 ieee80211_tx_skb(sdata, skb);
981}
982
Johannes Bergcc32abd2009-05-15 11:52:31 +0200983/* spectrum management related things */
984static void ieee80211_chswitch_work(struct work_struct *work)
985{
986 struct ieee80211_sub_if_data *sdata =
987 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200988 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
989
Johannes Berg9607e6b2009-12-23 13:15:31 +0100990 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200991 return;
992
Johannes Berg77fdaa12009-07-07 03:45:17 +0200993 mutex_lock(&ifmgd->mtx);
994 if (!ifmgd->associated)
995 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200996
Johannes Berg55de9082012-07-26 17:24:39 +0200997 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200998 if (!sdata->local->ops->channel_switch) {
999 /* call "hw_config" only if doing sw channel switch */
1000 ieee80211_hw_config(sdata->local,
1001 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +03001002 } else {
1003 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +02001004 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001005 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001006
Johannes Berg77fdaa12009-07-07 03:45:17 +02001007 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +02001008 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001009
Johannes Berg57eebdf2012-08-01 15:50:46 +02001010 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001011 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001012 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +02001013 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001014 out:
1015 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1016 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001017}
1018
Johannes Berg5ce6e432010-05-11 16:20:57 +02001019void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1020{
Johannes Berg55de9082012-07-26 17:24:39 +02001021 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1022 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001023
1024 trace_api_chswitch_done(sdata, success);
1025 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001026 sdata_info(sdata,
1027 "driver channel switch failed, disconnecting\n");
1028 ieee80211_queue_work(&sdata->local->hw,
1029 &ifmgd->csa_connection_drop_work);
1030 } else {
1031 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001032 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001033}
1034EXPORT_SYMBOL(ieee80211_chswitch_done);
1035
Johannes Bergcc32abd2009-05-15 11:52:31 +02001036static void ieee80211_chswitch_timer(unsigned long data)
1037{
1038 struct ieee80211_sub_if_data *sdata =
1039 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001040
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001041 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001042}
1043
Johannes Berg4a3cb702013-02-12 16:43:19 +01001044void
1045ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1046 const struct ieee80211_channel_sw_ie *sw_elem,
1047 struct ieee80211_bss *bss, u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001048{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001049 struct cfg80211_bss *cbss =
1050 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001051 struct ieee80211_channel *new_ch;
1052 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001053 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1054 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +02001055 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001056
Johannes Berg77fdaa12009-07-07 03:45:17 +02001057 ASSERT_MGD_MTX(ifmgd);
1058
1059 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001060 return;
1061
Helmut Schaafbe9c422009-07-23 12:14:04 +02001062 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001063 return;
1064
1065 /* Disregard subsequent beacons if we are already running a timer
1066 processing a CSA */
1067
1068 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1069 return;
1070
1071 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001072 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1073 sdata_info(sdata,
1074 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1075 ifmgd->associated->bssid, new_freq);
1076 ieee80211_queue_work(&sdata->local->hw,
1077 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001078 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001079 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001080
Johannes Berg57eebdf2012-08-01 15:50:46 +02001081 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1082
Johannes Berg55de9082012-07-26 17:24:39 +02001083 if (sdata->local->use_chanctx) {
1084 sdata_info(sdata,
1085 "not handling channel switch with channel contexts\n");
1086 ieee80211_queue_work(&sdata->local->hw,
1087 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001088 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001089 }
1090
1091 mutex_lock(&sdata->local->chanctx_mtx);
1092 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1093 mutex_unlock(&sdata->local->chanctx_mtx);
1094 return;
1095 }
1096 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1097 struct ieee80211_chanctx, conf);
1098 if (chanctx->refcount > 1) {
1099 sdata_info(sdata,
1100 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1101 ieee80211_queue_work(&sdata->local->hw,
1102 &ifmgd->csa_connection_drop_work);
1103 mutex_unlock(&sdata->local->chanctx_mtx);
1104 return;
1105 }
1106 mutex_unlock(&sdata->local->chanctx_mtx);
1107
1108 sdata->local->csa_channel = new_ch;
1109
Johannes Berg57eebdf2012-08-01 15:50:46 +02001110 if (sw_elem->mode)
1111 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001112 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001113 IEEE80211_QUEUE_STOP_REASON_CSA);
1114
Johannes Berg5ce6e432010-05-11 16:20:57 +02001115 if (sdata->local->ops->channel_switch) {
1116 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001117 struct ieee80211_channel_switch ch_switch = {
1118 .timestamp = timestamp,
1119 .block_tx = sw_elem->mode,
1120 .channel = new_ch,
1121 .count = sw_elem->count,
1122 };
1123
Johannes Berg5ce6e432010-05-11 16:20:57 +02001124 drv_channel_switch(sdata->local, &ch_switch);
1125 return;
1126 }
1127
1128 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001129 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001130 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001131 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001132 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +02001133 TU_TO_EXP_TIME(sw_elem->count *
1134 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001135}
1136
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001137static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1138 struct ieee80211_channel *channel,
1139 const u8 *country_ie, u8 country_ie_len,
1140 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001141{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001142 struct ieee80211_country_ie_triplet *triplet;
1143 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1144 int i, chan_pwr, chan_increment, new_ap_level;
1145 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001146
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001147 /* Invalid IE */
1148 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001149 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001150
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001151 triplet = (void *)(country_ie + 3);
1152 country_ie_len -= 3;
1153
1154 switch (channel->band) {
1155 default:
1156 WARN_ON_ONCE(1);
1157 /* fall through */
1158 case IEEE80211_BAND_2GHZ:
1159 case IEEE80211_BAND_60GHZ:
1160 chan_increment = 1;
1161 break;
1162 case IEEE80211_BAND_5GHZ:
1163 chan_increment = 4;
1164 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001165 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001166
1167 /* find channel */
1168 while (country_ie_len >= 3) {
1169 u8 first_channel = triplet->chans.first_channel;
1170
1171 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1172 goto next;
1173
1174 for (i = 0; i < triplet->chans.num_channels; i++) {
1175 if (first_channel + i * chan_increment == chan) {
1176 have_chan_pwr = true;
1177 chan_pwr = triplet->chans.max_power;
1178 break;
1179 }
1180 }
1181 if (have_chan_pwr)
1182 break;
1183
1184 next:
1185 triplet++;
1186 country_ie_len -= 3;
1187 }
1188
1189 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001190 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001191
1192 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1193
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001194 if (sdata->ap_power_level == new_ap_level)
1195 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001196
1197 sdata_info(sdata,
1198 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1199 new_ap_level, chan_pwr, *pwr_constr_elem,
1200 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001201 sdata->ap_power_level = new_ap_level;
1202 if (__ieee80211_recalc_txpower(sdata))
1203 return BSS_CHANGED_TXPOWER;
1204 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001205}
1206
Johannes Berg965beda2009-04-16 13:17:24 +02001207/* powersave */
1208static void ieee80211_enable_ps(struct ieee80211_local *local,
1209 struct ieee80211_sub_if_data *sdata)
1210{
1211 struct ieee80211_conf *conf = &local->hw.conf;
1212
Johannes Bergd5edaed2009-04-22 23:02:51 +02001213 /*
1214 * If we are scanning right now then the parameters will
1215 * take effect when scan finishes.
1216 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001217 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001218 return;
1219
Johannes Berg965beda2009-04-16 13:17:24 +02001220 if (conf->dynamic_ps_timeout > 0 &&
1221 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1222 mod_timer(&local->dynamic_ps_timer, jiffies +
1223 msecs_to_jiffies(conf->dynamic_ps_timeout));
1224 } else {
1225 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1226 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301227
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001228 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1229 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1230 return;
1231
1232 conf->flags |= IEEE80211_CONF_PS;
1233 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001234 }
1235}
1236
1237static void ieee80211_change_ps(struct ieee80211_local *local)
1238{
1239 struct ieee80211_conf *conf = &local->hw.conf;
1240
1241 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001242 ieee80211_enable_ps(local, local->ps_sdata);
1243 } else if (conf->flags & IEEE80211_CONF_PS) {
1244 conf->flags &= ~IEEE80211_CONF_PS;
1245 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1246 del_timer_sync(&local->dynamic_ps_timer);
1247 cancel_work_sync(&local->dynamic_ps_enable_work);
1248 }
1249}
1250
Jason Young808118c2011-03-10 16:43:19 -08001251static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1252{
1253 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1254 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001255 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001256
1257 if (!mgd->powersave)
1258 return false;
1259
Johannes Berg05cb9102011-10-28 11:59:47 +02001260 if (mgd->broken_ap)
1261 return false;
1262
Jason Young808118c2011-03-10 16:43:19 -08001263 if (!mgd->associated)
1264 return false;
1265
Jason Young808118c2011-03-10 16:43:19 -08001266 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1267 IEEE80211_STA_CONNECTION_POLL))
1268 return false;
1269
1270 rcu_read_lock();
1271 sta = sta_info_get(sdata, mgd->bssid);
1272 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001273 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001274 rcu_read_unlock();
1275
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001276 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001277}
1278
Johannes Berg965beda2009-04-16 13:17:24 +02001279/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001280void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001281{
1282 struct ieee80211_sub_if_data *sdata, *found = NULL;
1283 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001284 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001285
1286 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1287 local->ps_sdata = NULL;
1288 return;
1289 }
1290
1291 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001292 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001293 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301294 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1295 /* If an AP vif is found, then disable PS
1296 * by setting the count to zero thereby setting
1297 * ps_sdata to NULL.
1298 */
1299 count = 0;
1300 break;
1301 }
Johannes Berg965beda2009-04-16 13:17:24 +02001302 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1303 continue;
1304 found = sdata;
1305 count++;
1306 }
1307
Jason Young808118c2011-03-10 16:43:19 -08001308 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001309 s32 beaconint_us;
1310
1311 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001312 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001313
1314 beaconint_us = ieee80211_tu_to_usec(
1315 found->vif.bss_conf.beacon_int);
1316
Juuso Oikarinenff616382010-06-09 09:51:52 +03001317 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001318 if (timeout < 0) {
1319 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001320 * Go to full PSM if the user configures a very low
1321 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001322 * The 2000 second value is there for compatibility
1323 * until the PM_QOS_NETWORK_LATENCY is configured
1324 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001325 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001326 if (latency > (1900 * USEC_PER_MSEC) &&
1327 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001328 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001329 else
1330 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001331 }
Johannes Berg09b85562013-02-06 23:07:41 +01001332 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001333
Johannes Berg04fe2032009-04-22 18:44:37 +02001334 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001335 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001336 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001337 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001338 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001339
1340 /* If the TIM IE is invalid, pretend the value is 1 */
1341 if (!dtimper)
1342 dtimper = 1;
1343 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001344 maxslp = min_t(int, dtimper,
1345 latency / beaconint_us);
1346
Johannes Berg9ccebe62009-04-23 10:32:36 +02001347 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001348 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001349 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001350 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001351 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001352 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001353 }
Johannes Berg965beda2009-04-16 13:17:24 +02001354
1355 ieee80211_change_ps(local);
1356}
1357
Eliad Pellerab095872012-07-27 12:33:22 +03001358void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1359{
1360 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1361
1362 if (sdata->vif.bss_conf.ps != ps_allowed) {
1363 sdata->vif.bss_conf.ps = ps_allowed;
1364 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1365 }
1366}
1367
Johannes Berg965beda2009-04-16 13:17:24 +02001368void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1369{
1370 struct ieee80211_local *local =
1371 container_of(work, struct ieee80211_local,
1372 dynamic_ps_disable_work);
1373
1374 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1375 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1376 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1377 }
1378
1379 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001380 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001381 IEEE80211_QUEUE_STOP_REASON_PS);
1382}
1383
1384void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1385{
1386 struct ieee80211_local *local =
1387 container_of(work, struct ieee80211_local,
1388 dynamic_ps_enable_work);
1389 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001390 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301391 unsigned long flags;
1392 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001393
1394 /* can only happen when PS was just disabled anyway */
1395 if (!sdata)
1396 return;
1397
Christian Lamparter5e340692011-06-30 21:08:43 +02001398 ifmgd = &sdata->u.mgd;
1399
Johannes Berg965beda2009-04-16 13:17:24 +02001400 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1401 return;
1402
Johannes Berg09b85562013-02-06 23:07:41 +01001403 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001404 /* don't enter PS if TX frames are pending */
1405 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301406 mod_timer(&local->dynamic_ps_timer, jiffies +
1407 msecs_to_jiffies(
1408 local->hw.conf.dynamic_ps_timeout));
1409 return;
1410 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001411
1412 /*
1413 * transmission can be stopped by others which leads to
1414 * dynamic_ps_timer expiry. Postpone the ps timer if it
1415 * is not the actual idle state.
1416 */
1417 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1418 for (q = 0; q < local->hw.queues; q++) {
1419 if (local->queue_stop_reasons[q]) {
1420 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1421 flags);
1422 mod_timer(&local->dynamic_ps_timer, jiffies +
1423 msecs_to_jiffies(
1424 local->hw.conf.dynamic_ps_timeout));
1425 return;
1426 }
1427 }
1428 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301429 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301430
Vivek Natarajan375177b2010-02-09 14:50:28 +05301431 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301432 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301433 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001434
Vivek Natarajane8306f92011-04-06 11:41:10 +05301435 if (drv_tx_frames_pending(local))
1436 mod_timer(&local->dynamic_ps_timer, jiffies +
1437 msecs_to_jiffies(
1438 local->hw.conf.dynamic_ps_timeout));
1439 else {
1440 ieee80211_send_nullfunc(local, sdata, 1);
1441 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001442 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301443 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301444 }
1445
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001446 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1447 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301448 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1449 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1450 local->hw.conf.flags |= IEEE80211_CONF_PS;
1451 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1452 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301453
Arik Nemtsov77b70232011-06-26 12:06:54 +03001454 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1455 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001456}
1457
1458void ieee80211_dynamic_ps_timer(unsigned long data)
1459{
1460 struct ieee80211_local *local = (void *) data;
1461
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001462 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001463 return;
1464
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001465 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001466}
1467
Simon Wunderlich164eb022013-02-08 18:16:20 +01001468void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1469{
1470 struct delayed_work *delayed_work =
1471 container_of(work, struct delayed_work, work);
1472 struct ieee80211_sub_if_data *sdata =
1473 container_of(delayed_work, struct ieee80211_sub_if_data,
1474 dfs_cac_timer_work);
1475
1476 ieee80211_vif_release_channel(sdata);
1477
1478 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1479}
1480
Johannes Berg60f8b392008-09-08 17:44:22 +02001481/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001482static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001483 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001484 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001485{
Jiri Bencf0706e82007-05-05 11:45:53 -07001486 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001487 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001488 size_t left;
1489 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001490 const u8 *pos;
1491 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001492
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001493 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001494 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001495
Johannes Berg32c50572012-03-28 11:04:29 +02001496 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001497 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001498
1499 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001500 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001501
Jiri Bencf0706e82007-05-05 11:45:53 -07001502 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001503 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001504
1505 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001506 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001507
Jiri Bencf0706e82007-05-05 11:45:53 -07001508 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001509 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001510 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001511 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001512
1513 pos = wmm_param + 8;
1514 left = wmm_param_len - 8;
1515
1516 memset(&params, 0, sizeof(params));
1517
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001518 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001519 for (; left >= 4; left -= 4, pos += 4) {
1520 int aci = (pos[0] >> 5) & 0x03;
1521 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001522 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001523 int queue;
1524
1525 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001526 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001527 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001528 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001529 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001530 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1531 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001532 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001533 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001534 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001535 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001536 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001537 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1538 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001539 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001540 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001541 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001542 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001543 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001544 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1545 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001546 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001547 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001548 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001549 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001550 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001551 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001552 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1553 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001554 break;
1555 }
1556
1557 params.aifs = pos[0] & 0x0f;
1558 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1559 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001560 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001561 params.uapsd = uapsd;
1562
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001563 mlme_dbg(sdata,
1564 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1565 queue, aci, acm,
1566 params.aifs, params.cw_min, params.cw_max,
1567 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001568 sdata->tx_conf[queue] = params;
1569 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001570 sdata_err(sdata,
1571 "failed to set TX queue parameters for queue %d\n",
1572 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001573 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001574
1575 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001576 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001577 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001578}
1579
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001580static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1581{
1582 lockdep_assert_held(&sdata->local->mtx);
1583
1584 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1585 IEEE80211_STA_BEACON_POLL);
1586 ieee80211_run_deferred_scan(sdata->local);
1587}
1588
1589static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1590{
1591 mutex_lock(&sdata->local->mtx);
1592 __ieee80211_stop_poll(sdata);
1593 mutex_unlock(&sdata->local->mtx);
1594}
1595
Johannes Berg7a5158e2008-10-08 10:59:33 +02001596static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1597 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001598{
Johannes Bergbda39332008-10-11 01:51:51 +02001599 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001600 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001601 bool use_protection;
1602 bool use_short_preamble;
1603 bool use_short_slot;
1604
1605 if (erp_valid) {
1606 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1607 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1608 } else {
1609 use_protection = false;
1610 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1611 }
1612
1613 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001614 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001615 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001616
Johannes Berg471b3ef2007-12-28 14:32:58 +01001617 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001618 bss_conf->use_cts_prot = use_protection;
1619 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001620 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001621
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001622 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001623 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001624 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001625 }
Daniel Draked9430a32007-07-27 15:43:24 +02001626
Johannes Berg7a5158e2008-10-08 10:59:33 +02001627 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001628 bss_conf->use_short_slot = use_short_slot;
1629 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001630 }
1631
1632 return changed;
1633}
1634
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001635static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001636 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001637 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001638{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001639 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001640 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001641 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001642
Johannes Bergae5eb022008-10-14 16:58:37 +02001643 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001644 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001645 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001646
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001647 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1648 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1649
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001650 sdata->u.mgd.associated = cbss;
1651 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001652
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001653 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1654
Johannes Berg488dd7b2012-10-29 20:08:01 +01001655 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001656 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001657
Johannes Berg9caf0362012-11-29 01:25:20 +01001658 rcu_read_lock();
1659 ies = rcu_dereference(cbss->ies);
1660 if (ies) {
1661 u8 noa[2];
1662 int ret;
1663
1664 ret = cfg80211_get_p2p_attr(
1665 ies->data, ies->len,
1666 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1667 noa, sizeof(noa));
1668 if (ret >= 2) {
1669 bss_conf->p2p_oppps = noa[1] & 0x80;
1670 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1671 bss_info_changed |= BSS_CHANGED_P2P_PS;
1672 sdata->u.mgd.p2p_noa_index = noa[0];
1673 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001674 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001675 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001676 }
1677
Johannes Bergb291ba12009-07-10 15:29:03 +02001678 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001679 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001680
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001681 ieee80211_led_assoc(local, 1);
1682
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001683 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001684 /*
1685 * If the AP is buggy we may get here with no DTIM period
1686 * known, so assume it's 1 which is the only safe assumption
1687 * in that case, although if the TIM IE is broken powersave
1688 * probably just won't work at all.
1689 */
1690 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001691 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001692 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001693 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001694 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001695
Juuso Oikarinen68542962010-06-09 13:43:26 +03001696 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001697
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001698 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001699 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001700 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001701 bss_info_changed |= BSS_CHANGED_CQM;
1702
Juuso Oikarinen68542962010-06-09 13:43:26 +03001703 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001704 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001705 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001706
Johannes Bergae5eb022008-10-14 16:58:37 +02001707 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001708
Johannes Berg056508d2009-07-30 21:43:55 +02001709 mutex_lock(&local->iflist_mtx);
1710 ieee80211_recalc_ps(local, -1);
1711 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001712
Johannes Berg04ecd252012-09-11 14:34:12 +02001713 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001714 ieee80211_recalc_ps_vif(sdata);
1715
John W. Linville8a5b33f2010-01-06 15:39:39 -05001716 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001717 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001718}
1719
Jouni Malinene69e95d2010-03-29 23:29:31 -07001720static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001721 u16 stype, u16 reason, bool tx,
1722 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001723{
Johannes Berg46900292009-02-15 12:44:28 +01001724 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001725 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001726 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001727
Johannes Berg77fdaa12009-07-07 03:45:17 +02001728 ASSERT_MGD_MTX(ifmgd);
1729
Johannes Berg37ad3882012-02-24 13:50:54 +01001730 if (WARN_ON_ONCE(tx && !frame_buf))
1731 return;
1732
Johannes Bergb291ba12009-07-10 15:29:03 +02001733 if (WARN_ON(!ifmgd->associated))
1734 return;
1735
David Spinadel79543d82012-06-12 09:59:45 +03001736 ieee80211_stop_poll(sdata);
1737
Johannes Berg77fdaa12009-07-07 03:45:17 +02001738 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001739
1740 /*
1741 * we need to commit the associated = NULL change because the
1742 * scan code uses that to determine whether this iface should
1743 * go to/wake up from powersave or not -- and could otherwise
1744 * wake the queues erroneously.
1745 */
1746 smp_mb();
1747
1748 /*
1749 * Thus, we can only afterwards stop the queues -- to account
1750 * for the case where another CPU is finishing a scan at this
1751 * time -- we don't want the scan code to enable queues.
1752 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001753
John W. Linville8a5b33f2010-01-06 15:39:39 -05001754 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001755 netif_carrier_off(sdata->dev);
1756
Eliad Peller88bc40e2012-07-12 17:35:33 +03001757 /*
1758 * if we want to get out of ps before disassoc (why?) we have
1759 * to do it before sending disassoc, as otherwise the null-packet
1760 * won't be valid.
1761 */
1762 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1763 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1764 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1765 }
1766 local->ps_sdata = NULL;
1767
Eliad Pellerab095872012-07-27 12:33:22 +03001768 /* disable per-vif ps */
1769 ieee80211_recalc_ps_vif(sdata);
1770
Eliad Pellerf8239812012-06-27 14:18:22 +03001771 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1772 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001773 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001774
Johannes Berg37ad3882012-02-24 13:50:54 +01001775 /* deauthenticate/disassociate now */
1776 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001777 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1778 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001779
1780 /* flush out frame */
1781 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001782 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001783
Eliad Peller88a9e312012-06-01 11:14:03 +03001784 /* clear bssid only after building the needed mgmt frames */
1785 memset(ifmgd->bssid, 0, ETH_ALEN);
1786
Johannes Berg37ad3882012-02-24 13:50:54 +01001787 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001788 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001789
1790 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001791 changed |= ieee80211_reset_erp_info(sdata);
1792
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001793 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001794 changed |= BSS_CHANGED_ASSOC;
1795 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001796
Johannes Berg488dd7b2012-10-29 20:08:01 +01001797 sdata->vif.bss_conf.p2p_ctwindow = 0;
1798 sdata->vif.bss_conf.p2p_oppps = false;
1799
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001800 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001801 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1802 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001803 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1804 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001805
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001806 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301807
Kalle Valo520eb822008-12-18 23:35:27 +02001808 del_timer_sync(&local->dynamic_ps_timer);
1809 cancel_work_sync(&local->dynamic_ps_enable_work);
1810
Juuso Oikarinen68542962010-06-09 13:43:26 +03001811 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001812 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001813 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001814
Johannes Berg3abead52012-03-02 15:56:59 +01001815 sdata->vif.bss_conf.qos = false;
1816 changed |= BSS_CHANGED_QOS;
1817
Johannes Berg0aaffa92010-05-05 15:28:27 +02001818 /* The BSSID (not really interesting) and HT changed */
1819 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001820 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001821
Johannes Berg3abead52012-03-02 15:56:59 +01001822 /* disassociated - set to defaults now */
1823 ieee80211_set_wmm_default(sdata, false);
1824
Johannes Bergb9dcf712010-08-27 12:35:54 +02001825 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1826 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1827 del_timer_sync(&sdata->u.mgd.timer);
1828 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001829
Johannes Berg826262c2012-12-10 16:38:14 +02001830 sdata->vif.bss_conf.dtim_period = 0;
1831
Johannes Berg028e8da02012-11-26 11:57:41 +01001832 ifmgd->flags = 0;
1833 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001834}
Jiri Bencf0706e82007-05-05 11:45:53 -07001835
Kalle Valo3cf335d2009-03-22 21:57:06 +02001836void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1837 struct ieee80211_hdr *hdr)
1838{
1839 /*
1840 * We can postpone the mgd.timer whenever receiving unicast frames
1841 * from AP because we know that the connection is working both ways
1842 * at that time. But multicast frames (and hence also beacons) must
1843 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001844 * data idle periods for sending the periodic probe request to the
1845 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001846 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001847 if (is_multicast_ether_addr(hdr->addr1))
1848 return;
1849
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001850 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001851}
Jiri Bencf0706e82007-05-05 11:45:53 -07001852
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001853static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1854{
1855 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001856 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001857
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001858 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001859 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001860 IEEE80211_STA_CONNECTION_POLL))) {
1861 mutex_unlock(&local->mtx);
1862 return;
1863 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001864
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001865 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001866
1867 mutex_lock(&local->iflist_mtx);
1868 ieee80211_recalc_ps(local, -1);
1869 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001870
1871 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001872 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001873
1874 /*
1875 * We've received a probe response, but are not sure whether
1876 * we have or will be receiving any beacons or data, so let's
1877 * schedule the timers again, just in case.
1878 */
1879 ieee80211_sta_reset_beacon_monitor(sdata);
1880
1881 mod_timer(&ifmgd->conn_mon_timer,
1882 round_jiffies_up(jiffies +
1883 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001884out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001885 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001886}
1887
1888void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001889 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001890{
Felix Fietkau75706d02010-12-02 21:01:07 +01001891 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001892 return;
1893
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001894 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1895 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001896 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001897 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001898 else
1899 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001900 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001901 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001902 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001903
1904 if (ack)
1905 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001906}
1907
Maxim Levitskya43abf22009-07-31 18:54:12 +03001908static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1909{
1910 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1911 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001912 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001913 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001914
1915 /*
1916 * Try sending broadcast probe requests for the last three
1917 * probe requests after the first ones failed since some
1918 * buggy APs only support broadcast probe requests.
1919 */
1920 if (ifmgd->probe_send_count >= unicast_limit)
1921 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001922
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001923 /*
1924 * When the hardware reports an accurate Tx ACK status, it's
1925 * better to send a nullfunc frame instead of a probe request,
1926 * as it will kick us off the AP quickly if we aren't associated
1927 * anymore. The timeout will be reset if the frame is ACKed by
1928 * the AP.
1929 */
Soumik Das992e68b2012-05-20 15:31:13 +05301930 ifmgd->probe_send_count++;
1931
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001932 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1933 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001934 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001935 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001936 int ssid_len;
1937
Johannes Berg9caf0362012-11-29 01:25:20 +01001938 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001939 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001940 if (WARN_ON_ONCE(ssid == NULL))
1941 ssid_len = 0;
1942 else
1943 ssid_len = ssid[1];
1944
1945 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001946 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001947 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001948 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001949 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001950
Ben Greear180205b2011-02-04 15:30:24 -08001951 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001952 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301953 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01001954 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001955}
1956
Johannes Bergb291ba12009-07-10 15:29:03 +02001957static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1958 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001959{
Kalle Valo04de8382009-03-22 21:57:35 +02001960 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001961 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001962
Johannes Berg9607e6b2009-12-23 13:15:31 +01001963 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001964 return;
1965
Johannes Berg77fdaa12009-07-07 03:45:17 +02001966 mutex_lock(&ifmgd->mtx);
1967
1968 if (!ifmgd->associated)
1969 goto out;
1970
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001971 mutex_lock(&sdata->local->mtx);
1972
1973 if (sdata->local->tmp_channel || sdata->local->scanning) {
1974 mutex_unlock(&sdata->local->mtx);
1975 goto out;
1976 }
1977
Joe Perchese87cc472012-05-13 21:56:26 +00001978 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001979 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001980 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001981
Holger Schurig6efb71b2012-05-15 15:38:59 +02001982 ieee80211_cqm_rssi_notify(&sdata->vif,
1983 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001984
Johannes Bergb291ba12009-07-10 15:29:03 +02001985 /*
1986 * The driver/our work has already reported this event or the
1987 * connection monitoring has kicked in and we have already sent
1988 * a probe request. Or maybe the AP died and the driver keeps
1989 * reporting until we disassociate...
1990 *
1991 * In either case we have to ignore the current call to this
1992 * function (except for setting the correct probe reason bit)
1993 * because otherwise we would reset the timer every time and
1994 * never check whether we received a probe response!
1995 */
1996 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1997 IEEE80211_STA_CONNECTION_POLL))
1998 already = true;
1999
2000 if (beacon)
2001 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2002 else
2003 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2004
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002005 mutex_unlock(&sdata->local->mtx);
2006
Johannes Bergb291ba12009-07-10 15:29:03 +02002007 if (already)
2008 goto out;
2009
Johannes Berg4e751842009-06-10 15:16:52 +02002010 mutex_lock(&sdata->local->iflist_mtx);
2011 ieee80211_recalc_ps(sdata->local, -1);
2012 mutex_unlock(&sdata->local->iflist_mtx);
2013
Maxim Levitskya43abf22009-07-31 18:54:12 +03002014 ifmgd->probe_send_count = 0;
2015 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002016 out:
2017 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002018}
2019
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002020struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2021 struct ieee80211_vif *vif)
2022{
2023 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2024 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002025 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002026 struct sk_buff *skb;
2027 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002028 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002029
2030 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2031 return NULL;
2032
2033 ASSERT_MGD_MTX(ifmgd);
2034
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002035 if (ifmgd->associated)
2036 cbss = ifmgd->associated;
2037 else if (ifmgd->auth_data)
2038 cbss = ifmgd->auth_data->bss;
2039 else if (ifmgd->assoc_data)
2040 cbss = ifmgd->assoc_data->bss;
2041 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002042 return NULL;
2043
Johannes Berg9caf0362012-11-29 01:25:20 +01002044 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002045 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002046 if (WARN_ON_ONCE(ssid == NULL))
2047 ssid_len = 0;
2048 else
2049 ssid_len = ssid[1];
2050
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002051 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002052 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002053 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002054 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002055 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002056
2057 return skb;
2058}
2059EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2060
Johannes Bergeef9e542013-01-29 13:09:34 +01002061static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002062{
2063 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002064 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002065
2066 mutex_lock(&ifmgd->mtx);
2067 if (!ifmgd->associated) {
2068 mutex_unlock(&ifmgd->mtx);
2069 return;
2070 }
2071
Johannes Berg37ad3882012-02-24 13:50:54 +01002072 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2073 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002074 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002075 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002076 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002077 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002078 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002079 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002080
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002081 /*
2082 * must be outside lock due to cfg80211,
2083 * but that's not a problem.
2084 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002085 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002086}
2087
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002088static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002089{
2090 struct ieee80211_sub_if_data *sdata =
2091 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002092 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002093 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2094 struct sta_info *sta;
2095
2096 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302097 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002098 sta = sta_info_get(sdata, ifmgd->bssid);
2099 if (sta)
2100 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302101 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002102 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002103
Johannes Berg682bd382013-01-29 13:13:50 +01002104 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002105 sdata_info(sdata, "Connection to AP %pM lost\n",
2106 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002107 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002108 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002109 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002110 }
2111}
2112
2113static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2114{
2115 struct ieee80211_sub_if_data *sdata =
2116 container_of(work, struct ieee80211_sub_if_data,
2117 u.mgd.csa_connection_drop_work);
2118
Johannes Bergeef9e542013-01-29 13:09:34 +01002119 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002120}
2121
Kalle Valo04de8382009-03-22 21:57:35 +02002122void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2123{
2124 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002125 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002126
Johannes Bergb5878a22010-04-07 16:48:40 +02002127 trace_api_beacon_loss(sdata);
2128
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002129 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002130 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002131 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002132}
2133EXPORT_SYMBOL(ieee80211_beacon_loss);
2134
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002135void ieee80211_connection_loss(struct ieee80211_vif *vif)
2136{
2137 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2138 struct ieee80211_hw *hw = &sdata->local->hw;
2139
Johannes Bergb5878a22010-04-07 16:48:40 +02002140 trace_api_connection_loss(sdata);
2141
Johannes Berg682bd382013-01-29 13:13:50 +01002142 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002143 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2144}
2145EXPORT_SYMBOL(ieee80211_connection_loss);
2146
2147
Johannes Berg66e67e42012-01-20 13:55:27 +01002148static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2149 bool assoc)
2150{
2151 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2152
2153 lockdep_assert_held(&sdata->u.mgd.mtx);
2154
Johannes Berg66e67e42012-01-20 13:55:27 +01002155 if (!assoc) {
2156 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2157
2158 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2159 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002160 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002161 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002162 }
2163
Johannes Berg5b112d32013-02-01 01:49:58 +01002164 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002165 kfree(auth_data);
2166 sdata->u.mgd.auth_data = NULL;
2167}
2168
2169static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2170 struct ieee80211_mgmt *mgmt, size_t len)
2171{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002172 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002173 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2174 u8 *pos;
2175 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002176 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002177
2178 pos = mgmt->u.auth.variable;
2179 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2180 if (!elems.challenge)
2181 return;
2182 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002183 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002184 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2185 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2186 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002187 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002188 elems.challenge - 2, elems.challenge_len + 2,
2189 auth_data->bss->bssid, auth_data->bss->bssid,
2190 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002191 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002192}
2193
2194static enum rx_mgmt_action __must_check
2195ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2196 struct ieee80211_mgmt *mgmt, size_t len)
2197{
2198 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2199 u8 bssid[ETH_ALEN];
2200 u16 auth_alg, auth_transaction, status_code;
2201 struct sta_info *sta;
2202
2203 lockdep_assert_held(&ifmgd->mtx);
2204
2205 if (len < 24 + 6)
2206 return RX_MGMT_NONE;
2207
2208 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2209 return RX_MGMT_NONE;
2210
2211 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2212
Joe Perchesb203ca32012-05-08 18:56:52 +00002213 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002214 return RX_MGMT_NONE;
2215
2216 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2217 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2218 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2219
2220 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002221 auth_transaction != ifmgd->auth_data->expected_transaction) {
2222 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2223 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2224 auth_transaction,
2225 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002226 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002227 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002228
2229 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002230 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2231 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002232 ieee80211_destroy_auth_data(sdata, false);
2233 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002234 }
2235
2236 switch (ifmgd->auth_data->algorithm) {
2237 case WLAN_AUTH_OPEN:
2238 case WLAN_AUTH_LEAP:
2239 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002240 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002241 break;
2242 case WLAN_AUTH_SHARED_KEY:
2243 if (ifmgd->auth_data->expected_transaction != 4) {
2244 ieee80211_auth_challenge(sdata, mgmt, len);
2245 /* need another frame */
2246 return RX_MGMT_NONE;
2247 }
2248 break;
2249 default:
2250 WARN_ONCE(1, "invalid auth alg %d",
2251 ifmgd->auth_data->algorithm);
2252 return RX_MGMT_NONE;
2253 }
2254
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002255 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002256 ifmgd->auth_data->done = true;
2257 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002258 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002259 run_again(ifmgd, ifmgd->auth_data->timeout);
2260
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002261 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2262 ifmgd->auth_data->expected_transaction != 2) {
2263 /*
2264 * Report auth frame to user space for processing since another
2265 * round of Authentication frames is still needed.
2266 */
2267 return RX_MGMT_CFG80211_RX_AUTH;
2268 }
2269
Johannes Berg66e67e42012-01-20 13:55:27 +01002270 /* move station state to auth */
2271 mutex_lock(&sdata->local->sta_mtx);
2272 sta = sta_info_get(sdata, bssid);
2273 if (!sta) {
2274 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2275 goto out_err;
2276 }
2277 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002278 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002279 goto out_err;
2280 }
2281 mutex_unlock(&sdata->local->sta_mtx);
2282
2283 return RX_MGMT_CFG80211_RX_AUTH;
2284 out_err:
2285 mutex_unlock(&sdata->local->sta_mtx);
2286 /* ignore frame -- wait for timeout */
2287 return RX_MGMT_NONE;
2288}
2289
2290
Johannes Berg77fdaa12009-07-07 03:45:17 +02002291static enum rx_mgmt_action __must_check
2292ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002293 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002294{
Johannes Berg46900292009-02-15 12:44:28 +01002295 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002296 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002297 u16 reason_code;
2298
Johannes Berg66e67e42012-01-20 13:55:27 +01002299 lockdep_assert_held(&ifmgd->mtx);
2300
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002301 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002302 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002303
Johannes Berg66e67e42012-01-20 13:55:27 +01002304 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002305 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002306 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002307
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002308 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002309
2310 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2311
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002312 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2313 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002314
Johannes Berg37ad3882012-02-24 13:50:54 +01002315 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2316
Johannes Berg77fdaa12009-07-07 03:45:17 +02002317 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002318}
2319
2320
Johannes Berg77fdaa12009-07-07 03:45:17 +02002321static enum rx_mgmt_action __must_check
2322ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2323 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002324{
Johannes Berg46900292009-02-15 12:44:28 +01002325 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002326 u16 reason_code;
2327
Johannes Berg66e67e42012-01-20 13:55:27 +01002328 lockdep_assert_held(&ifmgd->mtx);
2329
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002330 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002331 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002332
Johannes Berg66e67e42012-01-20 13:55:27 +01002333 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002334 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002335 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002336
2337 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2338
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002339 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2340 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002341
Johannes Berg37ad3882012-02-24 13:50:54 +01002342 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2343
Johannes Berg77fdaa12009-07-07 03:45:17 +02002344 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002345}
2346
Christian Lamparterc74d0842011-10-15 00:14:49 +02002347static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2348 u8 *supp_rates, unsigned int supp_rates_len,
2349 u32 *rates, u32 *basic_rates,
2350 bool *have_higher_than_11mbit,
2351 int *min_rate, int *min_rate_index)
2352{
2353 int i, j;
2354
2355 for (i = 0; i < supp_rates_len; i++) {
2356 int rate = (supp_rates[i] & 0x7f) * 5;
2357 bool is_basic = !!(supp_rates[i] & 0x80);
2358
2359 if (rate > 110)
2360 *have_higher_than_11mbit = true;
2361
2362 /*
2363 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2364 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2365 *
2366 * Note: Even through the membership selector and the basic
2367 * rate flag share the same bit, they are not exactly
2368 * the same.
2369 */
2370 if (!!(supp_rates[i] & 0x80) &&
2371 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2372 continue;
2373
2374 for (j = 0; j < sband->n_bitrates; j++) {
2375 if (sband->bitrates[j].bitrate == rate) {
2376 *rates |= BIT(j);
2377 if (is_basic)
2378 *basic_rates |= BIT(j);
2379 if (rate < *min_rate) {
2380 *min_rate = rate;
2381 *min_rate_index = j;
2382 }
2383 break;
2384 }
2385 }
2386 }
2387}
Jiri Bencf0706e82007-05-05 11:45:53 -07002388
Johannes Berg66e67e42012-01-20 13:55:27 +01002389static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2390 bool assoc)
2391{
2392 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2393
2394 lockdep_assert_held(&sdata->u.mgd.mtx);
2395
Johannes Berg66e67e42012-01-20 13:55:27 +01002396 if (!assoc) {
2397 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2398
2399 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2400 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002401 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002402 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002403 }
2404
2405 kfree(assoc_data);
2406 sdata->u.mgd.assoc_data = NULL;
2407}
2408
2409static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2410 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002411 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002412{
Johannes Berg46900292009-02-15 12:44:28 +01002413 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002414 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002415 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002416 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002417 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002418 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002419 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002420 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002421 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002422 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002423
Johannes Bergaf6b6372009-12-23 13:15:35 +01002424 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002425
Jiri Bencf0706e82007-05-05 11:45:53 -07002426 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002427 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002428
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002429 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002430 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2431 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002432 aid &= ~(BIT(15) | BIT(14));
2433
Johannes Berg05cb9102011-10-28 11:59:47 +02002434 ifmgd->broken_ap = false;
2435
2436 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002437 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2438 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002439 aid = 0;
2440 ifmgd->broken_ap = true;
2441 }
2442
Johannes Bergaf6b6372009-12-23 13:15:35 +01002443 pos = mgmt->u.assoc_resp.variable;
2444 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2445
Jiri Bencf0706e82007-05-05 11:45:53 -07002446 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002447 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002448 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002449 }
2450
Johannes Berg46900292009-02-15 12:44:28 +01002451 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002452
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002453 /*
2454 * We previously checked these in the beacon/probe response, so
2455 * they should be present here. This is just a safety net.
2456 */
2457 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2458 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2459 sdata_info(sdata,
2460 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2461 return false;
2462 }
2463
2464 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2465 (!elems.vht_cap_elem || !elems.vht_operation)) {
2466 sdata_info(sdata,
2467 "VHT AP is missing VHT capability/operation in AssocResp\n");
2468 return false;
2469 }
2470
Guy Eilam2a33bee2011-08-17 15:18:15 +03002471 mutex_lock(&sdata->local->sta_mtx);
2472 /*
2473 * station info was already allocated and inserted before
2474 * the association and should be available to us
2475 */
Johannes Berg7852e362012-01-20 13:55:24 +01002476 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002477 if (WARN_ON(!sta)) {
2478 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002479 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002480 }
2481
Johannes Berg55de9082012-07-26 17:24:39 +02002482 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002483
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002484 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002485 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002486 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002487 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002488
Mahesh Palivela818255e2012-10-10 11:33:04 +00002489 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2490 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002491 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002492
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002493 /*
2494 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2495 * in their association response, so ignore that data for our own
2496 * configuration. If it changed since the last beacon, we'll get the
2497 * next beacon and update then.
2498 */
Johannes Berg11289582013-02-07 17:36:12 +01002499
Johannes Bergbee7f582013-02-07 22:24:55 +01002500 /*
2501 * If an operating mode notification IE is present, override the
2502 * NSS calculation (that would be done in rate_control_rate_init())
2503 * and use the # of streams from that element.
2504 */
2505 if (elems.opmode_notif &&
2506 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2507 u8 nss;
2508
2509 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2510 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2511 nss += 1;
2512 sta->sta.rx_nss = nss;
2513 }
2514
Johannes Berg4b7679a2008-09-18 18:14:18 +02002515 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002516
Johannes Berg46900292009-02-15 12:44:28 +01002517 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002518 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002519
Johannes Bergddf4ac52008-10-22 11:41:38 +02002520 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002521 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002522
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002523 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002524 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2525 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2526 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002527 sdata_info(sdata,
2528 "failed to move station %pM to desired state\n",
2529 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002530 WARN_ON(__sta_info_destroy(sta));
2531 mutex_unlock(&sdata->local->sta_mtx);
2532 return false;
2533 }
2534
Johannes Berg7852e362012-01-20 13:55:24 +01002535 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002536
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002537 /*
2538 * Always handle WMM once after association regardless
2539 * of the first value the AP uses. Setting -1 here has
2540 * that effect because the AP values is an unsigned
2541 * 4-bit value.
2542 */
2543 ifmgd->wmm_last_param_set = -1;
2544
Johannes Bergddf4ac52008-10-22 11:41:38 +02002545 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002546 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002547 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002548 else
Johannes Berg3abead52012-03-02 15:56:59 +01002549 ieee80211_set_wmm_default(sdata, false);
2550 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002551
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002552 /* set AID and assoc capability,
2553 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002554 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002555 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002556 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002557
Kalle Valo15b7b062009-03-22 21:57:14 +02002558 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002559 * If we're using 4-addr mode, let the AP know that we're
2560 * doing so, so that it can create the STA VLAN on its side
2561 */
2562 if (ifmgd->use_4addr)
2563 ieee80211_send_4addr_nullfunc(local, sdata);
2564
2565 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002566 * Start timer to probe the connection to the AP now.
2567 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002568 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002569 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002570 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002571
Johannes Bergaf6b6372009-12-23 13:15:35 +01002572 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002573}
2574
Johannes Berg66e67e42012-01-20 13:55:27 +01002575static enum rx_mgmt_action __must_check
2576ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2577 struct ieee80211_mgmt *mgmt, size_t len,
2578 struct cfg80211_bss **bss)
2579{
2580 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2581 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2582 u16 capab_info, status_code, aid;
2583 struct ieee802_11_elems elems;
2584 u8 *pos;
2585 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002586
Johannes Berg66e67e42012-01-20 13:55:27 +01002587 lockdep_assert_held(&ifmgd->mtx);
2588
2589 if (!assoc_data)
2590 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002591 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002592 return RX_MGMT_NONE;
2593
2594 /*
2595 * AssocResp and ReassocResp have identical structure, so process both
2596 * of them in this function.
2597 */
2598
2599 if (len < 24 + 6)
2600 return RX_MGMT_NONE;
2601
2602 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2603 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2604 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2605 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2606
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002607 sdata_info(sdata,
2608 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2609 reassoc ? "Rea" : "A", mgmt->sa,
2610 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002611
2612 pos = mgmt->u.assoc_resp.variable;
2613 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2614
2615 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2616 elems.timeout_int && elems.timeout_int_len == 5 &&
2617 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2618 u32 tu, ms;
2619 tu = get_unaligned_le32(elems.timeout_int + 1);
2620 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002621 sdata_info(sdata,
2622 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2623 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002624 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002625 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002626 if (ms > IEEE80211_ASSOC_TIMEOUT)
2627 run_again(ifmgd, assoc_data->timeout);
2628 return RX_MGMT_NONE;
2629 }
2630
2631 *bss = assoc_data->bss;
2632
2633 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002634 sdata_info(sdata, "%pM denied association (code=%d)\n",
2635 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002636 ieee80211_destroy_assoc_data(sdata, false);
2637 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002638 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2639 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002640 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002641 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002642 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2643 }
John W. Linville635d9992012-07-09 16:34:34 -04002644 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002645
2646 /*
2647 * destroy assoc_data afterwards, as otherwise an idle
2648 * recalc after assoc_data is NULL but before associated
2649 * is set can cause the interface to go idle
2650 */
2651 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002652 }
2653
2654 return RX_MGMT_CFG80211_RX_ASSOC;
2655}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002656
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002657static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002658 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002659 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002660 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002661{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002662 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002663 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002664 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002665 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002666 bool need_ps = false;
2667
Johannes Berg826262c2012-12-10 16:38:14 +02002668 if ((sdata->u.mgd.associated &&
2669 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2670 (sdata->u.mgd.assoc_data &&
2671 ether_addr_equal(mgmt->bssid,
2672 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002673 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002674 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2675
2676 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002677 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002678 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2679 }
Johannes Berg56007a02010-01-26 14:19:52 +01002680 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002681
Johannes Berg5bda6172008-09-08 11:05:10 +02002682 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002683 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2684 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002685 else
2686 freq = rx_status->freq;
2687
2688 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2689
2690 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2691 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002692
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002693 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002694 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002695 if (bss)
2696 ieee80211_rx_bss_put(local, bss);
2697
2698 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002699 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002700
Johannes Berg56007a02010-01-26 14:19:52 +01002701 if (need_ps) {
2702 mutex_lock(&local->iflist_mtx);
2703 ieee80211_recalc_ps(local, -1);
2704 mutex_unlock(&local->iflist_mtx);
2705 }
2706
Johannes Berg5bc14202012-08-01 16:13:02 +02002707 if (elems->ch_switch_ie &&
2708 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2709 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002710 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002711}
2712
2713
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002714static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002715 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002716{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002717 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002718 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002719 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2720 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002721 struct ieee802_11_elems elems;
2722
Kalle Valo15b7b062009-03-22 21:57:14 +02002723 ifmgd = &sdata->u.mgd;
2724
Johannes Berg77fdaa12009-07-07 03:45:17 +02002725 ASSERT_MGD_MTX(ifmgd);
2726
Joe Perchesb203ca32012-05-08 18:56:52 +00002727 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002728 return; /* ignore ProbeResp to foreign address */
2729
Ester Kummerae6a44e2008-06-27 18:54:48 +03002730 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2731 if (baselen > len)
2732 return;
2733
2734 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2735 &elems);
2736
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002737 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002738
Johannes Berg77fdaa12009-07-07 03:45:17 +02002739 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002740 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002741 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002742
2743 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002744 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002745 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002746 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002747 ifmgd->auth_data->tries = 0;
2748 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002749 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002750 run_again(ifmgd, ifmgd->auth_data->timeout);
2751 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002752}
2753
Johannes Bergd91f36d2009-04-16 13:17:26 +02002754/*
2755 * This is the canonical list of information elements we care about,
2756 * the filter code also gives us all changes to the Microsoft OUI
2757 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2758 *
2759 * We implement beacon filtering in software since that means we can
2760 * avoid processing the frame here and in cfg80211, and userspace
2761 * will not be able to tell whether the hardware supports it or not.
2762 *
2763 * XXX: This list needs to be dynamic -- userspace needs to be able to
2764 * add items it requires. It also needs to be able to tell us to
2765 * look out for other vendor IEs.
2766 */
2767static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002768 (1ULL << WLAN_EID_COUNTRY) |
2769 (1ULL << WLAN_EID_ERP_INFO) |
2770 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2771 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2772 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002773 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002774
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002775static enum rx_mgmt_action
2776ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2777 struct ieee80211_mgmt *mgmt, size_t len,
2778 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002779{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002780 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002781 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002782 size_t baselen;
2783 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002784 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002785 struct ieee80211_chanctx_conf *chanctx_conf;
2786 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002787 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002788 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002789 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002790 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002791 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002792 u8 *bssid;
2793
Johannes Berg66e67e42012-01-20 13:55:27 +01002794 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002795
Ester Kummerae6a44e2008-06-27 18:54:48 +03002796 /* Process beacon from the current BSS */
2797 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2798 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002799 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002800
Johannes Berg55de9082012-07-26 17:24:39 +02002801 rcu_read_lock();
2802 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2803 if (!chanctx_conf) {
2804 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002805 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002806 }
2807
Johannes Berg4bf88532012-11-09 11:39:59 +01002808 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002809 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002810 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002811 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002812 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002813 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002814
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002815 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002816 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002817 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2818 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002819
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002820 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002821 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002822 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002823 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2824 sdata->vif.bss_conf.sync_tsf =
2825 le64_to_cpu(mgmt->u.beacon.timestamp);
2826 sdata->vif.bss_conf.sync_device_ts =
2827 rx_status->device_timestamp;
2828 if (elems.tim)
2829 sdata->vif.bss_conf.sync_dtim_count =
2830 elems.tim->dtim_count;
2831 else
2832 sdata->vif.bss_conf.sync_dtim_count = 0;
2833 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002834 /* continue assoc process */
2835 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002836 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002837 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002838 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002839 }
2840
2841 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002842 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002843 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002844 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002845
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002846 /* Track average RSSI from the Beacon frames of the current AP */
2847 ifmgd->last_beacon_signal = rx_status->signal;
2848 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2849 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002850 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002851 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002852 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002853 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002854 } else {
2855 ifmgd->ave_beacon_signal =
2856 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2857 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2858 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002859 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002860 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002861
2862 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2863 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2864 int sig = ifmgd->ave_beacon_signal;
2865 int last_sig = ifmgd->last_ave_beacon_signal;
2866
2867 /*
2868 * if signal crosses either of the boundaries, invoke callback
2869 * with appropriate parameters
2870 */
2871 if (sig > ifmgd->rssi_max_thold &&
2872 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2873 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002874 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002875 } else if (sig < ifmgd->rssi_min_thold &&
2876 (last_sig >= ifmgd->rssi_max_thold ||
2877 last_sig == 0)) {
2878 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002879 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002880 }
2881 }
2882
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002883 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002884 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002885 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002886 int sig = ifmgd->ave_beacon_signal / 16;
2887 int last_event = ifmgd->last_cqm_event_signal;
2888 int thold = bss_conf->cqm_rssi_thold;
2889 int hyst = bss_conf->cqm_rssi_hyst;
2890 if (sig < thold &&
2891 (last_event == 0 || sig < last_event - hyst)) {
2892 ifmgd->last_cqm_event_signal = sig;
2893 ieee80211_cqm_rssi_notify(
2894 &sdata->vif,
2895 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2896 GFP_KERNEL);
2897 } else 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_HIGH,
2903 GFP_KERNEL);
2904 }
2905 }
2906
Johannes Bergb291ba12009-07-10 15:29:03 +02002907 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002908 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002909 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002910 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002911 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002912 ieee80211_run_deferred_scan(local);
2913 mutex_unlock(&local->mtx);
2914
Johannes Berg4e751842009-06-10 15:16:52 +02002915 mutex_lock(&local->iflist_mtx);
2916 ieee80211_recalc_ps(local, -1);
2917 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002918 }
2919
Johannes Bergb291ba12009-07-10 15:29:03 +02002920 /*
2921 * Push the beacon loss detection into the future since
2922 * we are processing a beacon from the AP just now.
2923 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002924 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002925
Johannes Bergd91f36d2009-04-16 13:17:26 +02002926 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2927 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2928 len - baselen, &elems,
2929 care_about_ies, ncrc);
2930
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302931 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002932 bool directed_tim = ieee80211_check_tim(elems.tim,
2933 elems.tim_len,
2934 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002935 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002936 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002937 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2938 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2939 ieee80211_hw_config(local,
2940 IEEE80211_CONF_CHANGE_PS);
2941 }
Kalle Valo572e0012009-02-10 17:09:31 +02002942 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302943 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002944 local->pspolling = true;
2945
2946 /*
2947 * Here is assumed that the driver will be
2948 * able to send ps-poll frame and receive a
2949 * response even though power save mode is
2950 * enabled, but some drivers might require
2951 * to disable power save here. This needs
2952 * to be investigated.
2953 */
2954 ieee80211_send_pspoll(local, sdata);
2955 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002956 }
2957 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002958
Johannes Berg488dd7b2012-10-29 20:08:01 +01002959 if (sdata->vif.p2p) {
2960 u8 noa[2];
2961 int ret;
2962
2963 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2964 len - baselen,
2965 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2966 noa, sizeof(noa));
2967 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2968 bss_conf->p2p_oppps = noa[1] & 0x80;
2969 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2970 changed |= BSS_CHANGED_P2P_PS;
2971 sdata->u.mgd.p2p_noa_index = noa[0];
2972 /*
2973 * make sure we update all information, the CRC
2974 * mechanism doesn't look at P2P attributes.
2975 */
2976 ifmgd->beacon_crc_valid = false;
2977 }
2978 }
2979
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002980 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002981 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03002982 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002983 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002984
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002985 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002986
2987 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2988 elems.wmm_param_len))
2989 changed |= BSS_CHANGED_QOS;
2990
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002991 /*
2992 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002993 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002994 */
2995 if (!bss_conf->dtim_period) {
2996 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2997 if (elems.tim)
2998 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2999 else
3000 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003001
3002 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3003 sdata->vif.bss_conf.sync_tsf =
3004 le64_to_cpu(mgmt->u.beacon.timestamp);
3005 sdata->vif.bss_conf.sync_device_ts =
3006 rx_status->device_timestamp;
3007 if (elems.tim)
3008 sdata->vif.bss_conf.sync_dtim_count =
3009 elems.tim->dtim_count;
3010 else
3011 sdata->vif.bss_conf.sync_dtim_count = 0;
3012 }
3013
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003014 changed |= BSS_CHANGED_DTIM_PERIOD;
3015 }
3016
Johannes Berg7a5158e2008-10-08 10:59:33 +02003017 if (elems.erp_info && elems.erp_info_len >= 1) {
3018 erp_valid = true;
3019 erp_value = elems.erp_info[0];
3020 } else {
3021 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003022 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003023 changed |= ieee80211_handle_bss_capability(sdata,
3024 le16_to_cpu(mgmt->u.beacon.capab_info),
3025 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003026
Johannes Berg11289582013-02-07 17:36:12 +01003027 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003028 sta = sta_info_get(sdata, bssid);
3029
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003030 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3031 elems.vht_operation, bssid, &changed)) {
3032 mutex_unlock(&local->sta_mtx);
3033 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3034 WLAN_REASON_DEAUTH_LEAVING,
3035 true, deauth_buf);
3036 return RX_MGMT_CFG80211_TX_DEAUTH;
3037 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003038
3039 if (sta && elems.opmode_notif)
3040 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3041 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003042 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003043
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003044 if (elems.country_elem && elems.pwr_constr_elem &&
3045 mgmt->u.probe_resp.capab_info &
3046 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003047 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3048 elems.country_elem,
3049 elems.country_elem_len,
3050 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003051
Johannes Berg471b3ef2007-12-28 14:32:58 +01003052 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003053
3054 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003055}
3056
Johannes Berg1fa57d02010-06-10 10:21:32 +02003057void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3058 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003059{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003060 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003061 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003062 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003063 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003064 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003065 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003066 u16 fc;
3067
Jiri Bencf0706e82007-05-05 11:45:53 -07003068 rx_status = (struct ieee80211_rx_status *) skb->cb;
3069 mgmt = (struct ieee80211_mgmt *) skb->data;
3070 fc = le16_to_cpu(mgmt->frame_control);
3071
Johannes Berg77fdaa12009-07-07 03:45:17 +02003072 mutex_lock(&ifmgd->mtx);
3073
Johannes Berg66e67e42012-01-20 13:55:27 +01003074 switch (fc & IEEE80211_FCTL_STYPE) {
3075 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003076 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3077 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003078 break;
3079 case IEEE80211_STYPE_PROBE_RESP:
3080 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3081 break;
3082 case IEEE80211_STYPE_AUTH:
3083 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3084 break;
3085 case IEEE80211_STYPE_DEAUTH:
3086 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3087 break;
3088 case IEEE80211_STYPE_DISASSOC:
3089 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3090 break;
3091 case IEEE80211_STYPE_ASSOC_RESP:
3092 case IEEE80211_STYPE_REASSOC_RESP:
3093 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3094 break;
3095 case IEEE80211_STYPE_ACTION:
3096 switch (mgmt->u.action.category) {
3097 case WLAN_CATEGORY_SPECTRUM_MGMT:
3098 ieee80211_sta_process_chanswitch(sdata,
3099 &mgmt->u.action.u.chan_switch.sw_elem,
3100 (void *)ifmgd->associated->priv,
3101 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003102 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003103 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003104 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003105 mutex_unlock(&ifmgd->mtx);
3106
Johannes Berg66e67e42012-01-20 13:55:27 +01003107 switch (rma) {
3108 case RX_MGMT_NONE:
3109 /* no action */
3110 break;
3111 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003112 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003113 break;
3114 case RX_MGMT_CFG80211_DISASSOC:
3115 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3116 break;
3117 case RX_MGMT_CFG80211_RX_AUTH:
3118 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3119 break;
3120 case RX_MGMT_CFG80211_RX_ASSOC:
3121 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3122 break;
3123 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3124 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3125 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003126 case RX_MGMT_CFG80211_TX_DEAUTH:
3127 cfg80211_send_deauth(sdata->dev, deauth_buf,
3128 sizeof(deauth_buf));
3129 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003130 default:
3131 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003132 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003133}
3134
Johannes Berg9c6bd792008-09-11 00:01:52 +02003135static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003136{
3137 struct ieee80211_sub_if_data *sdata =
3138 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003139
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003140 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003141}
3142
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003143static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003144 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003145{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003146 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003147 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003148
Johannes Berg37ad3882012-02-24 13:50:54 +01003149 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003150 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003151 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003152
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003153 /*
3154 * must be outside lock due to cfg80211,
3155 * but that's not a problem.
3156 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003157 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003158
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003159 mutex_lock(&ifmgd->mtx);
3160}
3161
Johannes Berg66e67e42012-01-20 13:55:27 +01003162static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3163{
3164 struct ieee80211_local *local = sdata->local;
3165 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3166 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003167 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003168
3169 lockdep_assert_held(&ifmgd->mtx);
3170
3171 if (WARN_ON_ONCE(!auth_data))
3172 return -EINVAL;
3173
Johannes Berg1672c0e32013-01-29 15:02:27 +01003174 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3175 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3176 IEEE80211_TX_INTFL_MLME_CONN_TX;
3177
Johannes Berg66e67e42012-01-20 13:55:27 +01003178 auth_data->tries++;
3179
3180 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003181 sdata_info(sdata, "authentication with %pM timed out\n",
3182 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003183
3184 /*
3185 * Most likely AP is not in the range so remove the
3186 * bss struct for that AP.
3187 */
3188 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3189
3190 return -ETIMEDOUT;
3191 }
3192
Johannes Berga1845fc2012-06-27 13:18:36 +02003193 drv_mgd_prepare_tx(local, sdata);
3194
Johannes Berg66e67e42012-01-20 13:55:27 +01003195 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003196 u16 trans = 1;
3197 u16 status = 0;
3198
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003199 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3200 auth_data->bss->bssid, auth_data->tries,
3201 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003202
3203 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003204
3205 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3206 trans = auth_data->sae_trans;
3207 status = auth_data->sae_status;
3208 auth_data->expected_transaction = trans;
3209 }
3210
3211 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3212 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003213 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003214 auth_data->bss->bssid, NULL, 0, 0,
3215 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003216 } else {
3217 const u8 *ssidie;
3218
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003219 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3220 auth_data->bss->bssid, auth_data->tries,
3221 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003222
Johannes Berg9caf0362012-11-29 01:25:20 +01003223 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003224 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003225 if (!ssidie) {
3226 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003227 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003228 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003229 /*
3230 * Direct probe is sent to broadcast address as some APs
3231 * will not answer to direct packet in unassociated state.
3232 */
3233 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01003234 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003235 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003236 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003237 }
3238
Johannes Berg1672c0e32013-01-29 15:02:27 +01003239 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3240 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003241 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003242 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003243 } else {
3244 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003245 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003246
3247 return 0;
3248}
3249
3250static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3251{
3252 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3253 struct ieee80211_local *local = sdata->local;
3254
3255 lockdep_assert_held(&sdata->u.mgd.mtx);
3256
Johannes Berg66e67e42012-01-20 13:55:27 +01003257 assoc_data->tries++;
3258 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003259 sdata_info(sdata, "association with %pM timed out\n",
3260 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003261
3262 /*
3263 * Most likely AP is not in the range so remove the
3264 * bss struct for that AP.
3265 */
3266 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3267
3268 return -ETIMEDOUT;
3269 }
3270
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003271 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3272 assoc_data->bss->bssid, assoc_data->tries,
3273 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003274 ieee80211_send_assoc(sdata);
3275
Johannes Berg1672c0e32013-01-29 15:02:27 +01003276 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3277 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003278 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003279 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003280 } else {
3281 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003282 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003283
3284 return 0;
3285}
3286
Johannes Berg1672c0e32013-01-29 15:02:27 +01003287void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3288 __le16 fc, bool acked)
3289{
3290 struct ieee80211_local *local = sdata->local;
3291
3292 sdata->u.mgd.status_fc = fc;
3293 sdata->u.mgd.status_acked = acked;
3294 sdata->u.mgd.status_received = true;
3295
3296 ieee80211_queue_work(&local->hw, &sdata->work);
3297}
3298
Johannes Berg1fa57d02010-06-10 10:21:32 +02003299void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003300{
Johannes Berg60f8b392008-09-08 17:44:22 +02003301 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003302 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003303
Johannes Berg77fdaa12009-07-07 03:45:17 +02003304 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003305
Johannes Berg1672c0e32013-01-29 15:02:27 +01003306 if (ifmgd->status_received) {
3307 __le16 fc = ifmgd->status_fc;
3308 bool status_acked = ifmgd->status_acked;
3309
3310 ifmgd->status_received = false;
3311 if (ifmgd->auth_data &&
3312 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3313 if (status_acked) {
3314 ifmgd->auth_data->timeout =
3315 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3316 run_again(ifmgd, ifmgd->auth_data->timeout);
3317 } else {
3318 ifmgd->auth_data->timeout = jiffies - 1;
3319 }
Johannes Berg89afe612013-02-13 15:39:57 +01003320 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003321 } else if (ifmgd->assoc_data &&
3322 (ieee80211_is_assoc_req(fc) ||
3323 ieee80211_is_reassoc_req(fc))) {
3324 if (status_acked) {
3325 ifmgd->assoc_data->timeout =
3326 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3327 run_again(ifmgd, ifmgd->assoc_data->timeout);
3328 } else {
3329 ifmgd->assoc_data->timeout = jiffies - 1;
3330 }
Johannes Berg89afe612013-02-13 15:39:57 +01003331 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003332 }
3333 }
3334
Johannes Berg89afe612013-02-13 15:39:57 +01003335 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003336 time_after(jiffies, ifmgd->auth_data->timeout)) {
3337 if (ifmgd->auth_data->done) {
3338 /*
3339 * ok ... we waited for assoc but userspace didn't,
3340 * so let's just kill the auth data
3341 */
3342 ieee80211_destroy_auth_data(sdata, false);
3343 } else if (ieee80211_probe_auth(sdata)) {
3344 u8 bssid[ETH_ALEN];
3345
3346 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3347
3348 ieee80211_destroy_auth_data(sdata, false);
3349
3350 mutex_unlock(&ifmgd->mtx);
3351 cfg80211_send_auth_timeout(sdata->dev, bssid);
3352 mutex_lock(&ifmgd->mtx);
3353 }
Johannes Berg89afe612013-02-13 15:39:57 +01003354 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003355 run_again(ifmgd, ifmgd->auth_data->timeout);
3356
Johannes Berg89afe612013-02-13 15:39:57 +01003357 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003358 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003359 if ((ifmgd->assoc_data->need_beacon &&
3360 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003361 ieee80211_do_assoc(sdata)) {
3362 u8 bssid[ETH_ALEN];
3363
3364 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3365
3366 ieee80211_destroy_assoc_data(sdata, false);
3367
3368 mutex_unlock(&ifmgd->mtx);
3369 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3370 mutex_lock(&ifmgd->mtx);
3371 }
Johannes Berg89afe612013-02-13 15:39:57 +01003372 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003373 run_again(ifmgd, ifmgd->assoc_data->timeout);
3374
Johannes Bergb291ba12009-07-10 15:29:03 +02003375 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3376 IEEE80211_STA_CONNECTION_POLL) &&
3377 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003378 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003379 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003380
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003381 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003382
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003383 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003384 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003385 else
Ben Greear180205b2011-02-04 15:30:24 -08003386 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003387
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003388 /* ACK received for nullfunc probing frame */
3389 if (!ifmgd->probe_send_count)
3390 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003391 else if (ifmgd->nullfunc_failed) {
3392 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003393 mlme_dbg(sdata,
3394 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3395 bssid, ifmgd->probe_send_count,
3396 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003397 ieee80211_mgd_probe_ap_send(sdata);
3398 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003399 mlme_dbg(sdata,
3400 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3401 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003402 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003403 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3404 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003405 }
3406 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003407 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003408 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003409 mlme_dbg(sdata,
3410 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3411 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003412 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003413 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003414 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003415 mlme_dbg(sdata,
3416 "No probe response from AP %pM after %dms, try %d/%i\n",
3417 bssid, probe_wait_ms,
3418 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003419 ieee80211_mgd_probe_ap_send(sdata);
3420 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003421 /*
3422 * We actually lost the connection ... or did we?
3423 * Let's make sure!
3424 */
Ben Greearb38afa82010-10-07 16:12:06 -07003425 wiphy_debug(local->hw.wiphy,
3426 "%s: No probe response from AP %pM"
3427 " after %dms, disconnecting.\n",
3428 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003429 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003430
Johannes Berg95acac62011-07-12 12:30:59 +02003431 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003432 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003433 }
3434 }
3435
Johannes Berg77fdaa12009-07-07 03:45:17 +02003436 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003437}
Johannes Berg0a51b272008-09-08 17:44:25 +02003438
Johannes Bergb291ba12009-07-10 15:29:03 +02003439static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3440{
3441 struct ieee80211_sub_if_data *sdata =
3442 (struct ieee80211_sub_if_data *) data;
3443 struct ieee80211_local *local = sdata->local;
3444
3445 if (local->quiescing)
3446 return;
3447
Johannes Berg682bd382013-01-29 13:13:50 +01003448 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003449 ieee80211_queue_work(&sdata->local->hw,
3450 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003451}
3452
3453static void ieee80211_sta_conn_mon_timer(unsigned long data)
3454{
3455 struct ieee80211_sub_if_data *sdata =
3456 (struct ieee80211_sub_if_data *) data;
3457 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3458 struct ieee80211_local *local = sdata->local;
3459
3460 if (local->quiescing)
3461 return;
3462
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003463 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003464}
3465
3466static void ieee80211_sta_monitor_work(struct work_struct *work)
3467{
3468 struct ieee80211_sub_if_data *sdata =
3469 container_of(work, struct ieee80211_sub_if_data,
3470 u.mgd.monitor_work);
3471
3472 ieee80211_mgd_probe_ap(sdata, false);
3473}
3474
Johannes Berga1678f82008-09-11 00:01:47 +02003475static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3476{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003477 u32 flags;
3478
Kalle Vaload935682009-04-19 08:47:19 +03003479 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003480 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003481
Johannes Bergb291ba12009-07-10 15:29:03 +02003482 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003483 flags = sdata->local->hw.flags;
3484 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3485 ieee80211_queue_work(&sdata->local->hw,
3486 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003487 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003488 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003489 }
Johannes Berga1678f82008-09-11 00:01:47 +02003490}
3491
Johannes Berg9c6bd792008-09-11 00:01:52 +02003492/* interface setup */
3493void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3494{
Johannes Berg46900292009-02-15 12:44:28 +01003495 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003496
Johannes Berg46900292009-02-15 12:44:28 +01003497 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003498 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003499 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003500 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3501 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003502 INIT_WORK(&ifmgd->csa_connection_drop_work,
3503 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003504 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003505 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003506 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003507 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3508 (unsigned long) sdata);
3509 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3510 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003511 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303512 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003513
Johannes Bergab1faea2009-07-01 21:41:17 +02003514 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303515 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003516 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3517 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003518
Johannes Berg77fdaa12009-07-07 03:45:17 +02003519 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003520
3521 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3522 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3523 else
3524 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003525}
3526
3527/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003528void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3529{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003530 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003531
3532 /* Restart STA timers */
3533 rcu_read_lock();
3534 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3535 ieee80211_restart_sta_timer(sdata);
3536 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003537}
Johannes Berg10f644a2009-04-16 13:17:25 +02003538
3539int ieee80211_max_network_latency(struct notifier_block *nb,
3540 unsigned long data, void *dummy)
3541{
3542 s32 latency_usec = (s32) data;
3543 struct ieee80211_local *local =
3544 container_of(nb, struct ieee80211_local,
3545 network_latency_notifier);
3546
3547 mutex_lock(&local->iflist_mtx);
3548 ieee80211_recalc_ps(local, latency_usec);
3549 mutex_unlock(&local->iflist_mtx);
3550
3551 return 0;
3552}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003553
Johannes Bergf2d9d272012-11-22 14:11:39 +01003554static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3555 struct cfg80211_bss *cbss)
3556{
3557 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3558 const u8 *ht_cap_ie, *vht_cap_ie;
3559 const struct ieee80211_ht_cap *ht_cap;
3560 const struct ieee80211_vht_cap *vht_cap;
3561 u8 chains = 1;
3562
3563 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3564 return chains;
3565
Johannes Berg9caf0362012-11-29 01:25:20 +01003566 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003567 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3568 ht_cap = (void *)(ht_cap_ie + 2);
3569 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3570 /*
3571 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3572 * "Tx Unequal Modulation Supported" fields.
3573 */
3574 }
3575
3576 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3577 return chains;
3578
Johannes Berg9caf0362012-11-29 01:25:20 +01003579 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003580 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3581 u8 nss;
3582 u16 tx_mcs_map;
3583
3584 vht_cap = (void *)(vht_cap_ie + 2);
3585 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3586 for (nss = 8; nss > 0; nss--) {
3587 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3588 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3589 break;
3590 }
3591 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3592 chains = max(chains, nss);
3593 }
3594
3595 return chains;
3596}
3597
Johannes Bergb17166a2012-07-27 11:41:27 +02003598static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3599 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003600{
3601 struct ieee80211_local *local = sdata->local;
3602 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003603 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003604 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003605 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003606 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003607 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003608
Johannes Berg24398e32012-03-28 10:58:36 +02003609 sband = local->hw.wiphy->bands[cbss->channel->band];
3610
Johannes Bergf2d9d272012-11-22 14:11:39 +01003611 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3612 IEEE80211_STA_DISABLE_80P80MHZ |
3613 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003614
Johannes Berg9caf0362012-11-29 01:25:20 +01003615 rcu_read_lock();
3616
Johannes Bergf2d9d272012-11-22 14:11:39 +01003617 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3618 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003619 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003620
Johannes Berg9caf0362012-11-29 01:25:20 +01003621 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003622 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3623 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003624
3625 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3626 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3627 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3628 ht_oper = NULL;
3629 }
Johannes Berg24398e32012-03-28 10:58:36 +02003630 }
3631
Johannes Bergf2d9d272012-11-22 14:11:39 +01003632 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3633 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003634 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003635
Johannes Berg9caf0362012-11-29 01:25:20 +01003636 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3637 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003638 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3639 vht_oper = (void *)(vht_oper_ie + 2);
3640 if (vht_oper && !ht_oper) {
3641 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003642 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003643 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003644 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3645 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003646 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003647
3648 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3649 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3650 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3651 vht_oper = NULL;
3652 }
Johannes Berg24398e32012-03-28 10:58:36 +02003653 }
3654
Johannes Bergf2d9d272012-11-22 14:11:39 +01003655 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3656 cbss->channel,
3657 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003658 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003659
Johannes Bergf2d9d272012-11-22 14:11:39 +01003660 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3661 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003662
Johannes Berg9caf0362012-11-29 01:25:20 +01003663 rcu_read_unlock();
3664
Johannes Berg04ecd252012-09-11 14:34:12 +02003665 /* will change later if needed */
3666 sdata->smps_mode = IEEE80211_SMPS_OFF;
3667
Johannes Bergf2d9d272012-11-22 14:11:39 +01003668 /*
3669 * If this fails (possibly due to channel context sharing
3670 * on incompatible channels, e.g. 80+80 and 160 sharing the
3671 * same control channel) try to use a smaller bandwidth.
3672 */
3673 ret = ieee80211_vif_use_channel(sdata, &chandef,
3674 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003675 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003676 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003677 ret = ieee80211_vif_use_channel(sdata, &chandef,
3678 IEEE80211_CHANCTX_SHARED);
3679 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003680 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003681}
3682
3683static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3684 struct cfg80211_bss *cbss, bool assoc)
3685{
3686 struct ieee80211_local *local = sdata->local;
3687 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3688 struct ieee80211_bss *bss = (void *)cbss->priv;
3689 struct sta_info *new_sta = NULL;
3690 bool have_sta = false;
3691 int err;
3692
3693 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3694 return -EINVAL;
3695
3696 if (assoc) {
3697 rcu_read_lock();
3698 have_sta = sta_info_get(sdata, cbss->bssid);
3699 rcu_read_unlock();
3700 }
3701
3702 if (!have_sta) {
3703 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3704 if (!new_sta)
3705 return -ENOMEM;
3706 }
3707
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003708 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003709 u32 rates = 0, basic_rates = 0;
3710 bool have_higher_than_11mbit;
3711 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003712 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003713 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003714
3715 sband = local->hw.wiphy->bands[cbss->channel->band];
3716
3717 err = ieee80211_prep_channel(sdata, cbss);
3718 if (err) {
3719 sta_info_free(local, new_sta);
3720 return err;
3721 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003722
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003723 ieee80211_get_rates(sband, bss->supp_rates,
3724 bss->supp_rates_len,
3725 &rates, &basic_rates,
3726 &have_higher_than_11mbit,
3727 &min_rate, &min_rate_index);
3728
3729 /*
3730 * This used to be a workaround for basic rates missing
3731 * in the association response frame. Now that we no
3732 * longer use the basic rates from there, it probably
3733 * doesn't happen any more, but keep the workaround so
3734 * in case some *other* APs are buggy in different ways
3735 * we can connect -- with a warning.
3736 */
3737 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003738 sdata_info(sdata,
3739 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003740 basic_rates = BIT(min_rate_index);
3741 }
3742
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003743 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003744 sdata->vif.bss_conf.basic_rates = basic_rates;
3745
3746 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003747 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003748 have_higher_than_11mbit)
3749 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3750 else
3751 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3752
3753 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3754
Johannes Berg8c358bc2012-05-22 22:13:05 +02003755 /* set timing information */
3756 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003757 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003758 ies = rcu_dereference(cbss->beacon_ies);
3759 if (ies) {
3760 const u8 *tim_ie;
3761
3762 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3763 sdata->vif.bss_conf.sync_device_ts =
3764 bss->device_ts_beacon;
3765 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3766 ies->data, ies->len);
3767 if (tim_ie && tim_ie[1] >= 2)
3768 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3769 else
3770 sdata->vif.bss_conf.sync_dtim_count = 0;
3771 } else if (!(local->hw.flags &
3772 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3773 ies = rcu_dereference(cbss->proberesp_ies);
3774 /* must be non-NULL since beacon IEs were NULL */
3775 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3776 sdata->vif.bss_conf.sync_device_ts =
3777 bss->device_ts_presp;
3778 sdata->vif.bss_conf.sync_dtim_count = 0;
3779 } else {
3780 sdata->vif.bss_conf.sync_tsf = 0;
3781 sdata->vif.bss_conf.sync_device_ts = 0;
3782 sdata->vif.bss_conf.sync_dtim_count = 0;
3783 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003784 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003785
3786 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003787 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003788 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3789 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003790
3791 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003792 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003793
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003794 err = sta_info_insert(new_sta);
3795 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003796 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003797 sdata_info(sdata,
3798 "failed to insert STA entry for the AP (error %d)\n",
3799 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003800 return err;
3801 }
3802 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003803 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003804
3805 return 0;
3806}
3807
Johannes Berg77fdaa12009-07-07 03:45:17 +02003808/* config hooks */
3809int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3810 struct cfg80211_auth_request *req)
3811{
Johannes Berg66e67e42012-01-20 13:55:27 +01003812 struct ieee80211_local *local = sdata->local;
3813 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3814 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003815 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003816 int err;
3817
3818 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003819
3820 switch (req->auth_type) {
3821 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3822 auth_alg = WLAN_AUTH_OPEN;
3823 break;
3824 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003825 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003826 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003827 auth_alg = WLAN_AUTH_SHARED_KEY;
3828 break;
3829 case NL80211_AUTHTYPE_FT:
3830 auth_alg = WLAN_AUTH_FT;
3831 break;
3832 case NL80211_AUTHTYPE_NETWORK_EAP:
3833 auth_alg = WLAN_AUTH_LEAP;
3834 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003835 case NL80211_AUTHTYPE_SAE:
3836 auth_alg = WLAN_AUTH_SAE;
3837 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003838 default:
3839 return -EOPNOTSUPP;
3840 }
3841
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003842 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3843 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003844 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003845 return -ENOMEM;
3846
Johannes Berg66e67e42012-01-20 13:55:27 +01003847 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003848
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003849 if (req->sae_data_len >= 4) {
3850 __le16 *pos = (__le16 *) req->sae_data;
3851 auth_data->sae_trans = le16_to_cpu(pos[0]);
3852 auth_data->sae_status = le16_to_cpu(pos[1]);
3853 memcpy(auth_data->data, req->sae_data + 4,
3854 req->sae_data_len - 4);
3855 auth_data->data_len += req->sae_data_len - 4;
3856 }
3857
Johannes Berg77fdaa12009-07-07 03:45:17 +02003858 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003859 memcpy(&auth_data->data[auth_data->data_len],
3860 req->ie, req->ie_len);
3861 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003862 }
3863
Johannes Bergfffd0932009-07-08 14:22:54 +02003864 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003865 auth_data->key_len = req->key_len;
3866 auth_data->key_idx = req->key_idx;
3867 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003868 }
3869
Johannes Berg66e67e42012-01-20 13:55:27 +01003870 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003871
Johannes Berg66e67e42012-01-20 13:55:27 +01003872 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003873
Johannes Berg66e67e42012-01-20 13:55:27 +01003874 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003875
Johannes Berg66e67e42012-01-20 13:55:27 +01003876 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3877 ifmgd->assoc_data) {
3878 err = -EBUSY;
3879 goto err_free;
3880 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003881
Johannes Berg66e67e42012-01-20 13:55:27 +01003882 if (ifmgd->auth_data)
3883 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003884
Johannes Berg66e67e42012-01-20 13:55:27 +01003885 /* prep auth_data so we don't go into idle on disassoc */
3886 ifmgd->auth_data = auth_data;
3887
3888 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003889 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003890
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003891 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003892
Johannes Berga1cf7752012-03-08 15:02:07 +01003893 err = ieee80211_prep_connection(sdata, req->bss, false);
3894 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003895 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003896
Johannes Berg66e67e42012-01-20 13:55:27 +01003897 err = ieee80211_probe_auth(sdata);
3898 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003899 sta_info_destroy_addr(sdata, req->bss->bssid);
3900 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003901 }
3902
Johannes Berg66e67e42012-01-20 13:55:27 +01003903 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003904 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003905 err = 0;
3906 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003907
Johannes Berg66e67e42012-01-20 13:55:27 +01003908 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003909 memset(ifmgd->bssid, 0, ETH_ALEN);
3910 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003911 ifmgd->auth_data = NULL;
3912 err_free:
3913 kfree(auth_data);
3914 out_unlock:
3915 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003916
Johannes Berg66e67e42012-01-20 13:55:27 +01003917 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003918}
3919
Johannes Berg77fdaa12009-07-07 03:45:17 +02003920int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3921 struct cfg80211_assoc_request *req)
3922{
Johannes Berg66e67e42012-01-20 13:55:27 +01003923 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003924 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003925 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003926 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003927 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003928 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003929 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003930 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003931
Johannes Berg66e67e42012-01-20 13:55:27 +01003932 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3933 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003934 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003935
Johannes Berg9caf0362012-11-29 01:25:20 +01003936 rcu_read_lock();
3937 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3938 if (!ssidie) {
3939 rcu_read_unlock();
3940 kfree(assoc_data);
3941 return -EINVAL;
3942 }
3943 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3944 assoc_data->ssid_len = ssidie[1];
3945 rcu_read_unlock();
3946
Johannes Berg66e67e42012-01-20 13:55:27 +01003947 mutex_lock(&ifmgd->mtx);
3948
3949 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003950 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003951
3952 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3953 err = -EBUSY;
3954 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003955 }
3956
Johannes Berg66e67e42012-01-20 13:55:27 +01003957 if (ifmgd->assoc_data) {
3958 err = -EBUSY;
3959 goto err_free;
3960 }
3961
3962 if (ifmgd->auth_data) {
3963 bool match;
3964
3965 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003966 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003967 ieee80211_destroy_auth_data(sdata, match);
3968 }
3969
3970 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003971
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003972 ifmgd->beacon_crc_valid = false;
3973
Johannes Bergde5036a2012-03-08 15:02:03 +01003974 /*
3975 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3976 * We still associate in non-HT mode (11a/b/g) if any one of these
3977 * ciphers is configured as pairwise.
3978 * We can set this to true for non-11n hardware, that'll be checked
3979 * separately along with the peer capabilities.
3980 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003981 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003982 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3983 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003984 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003985 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003986 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003987 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003988 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003989 }
3990 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003991
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003992 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003993 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003994 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3995 }
Ben Greearef96a8422011-11-18 11:32:00 -08003996
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01003997 if (req->flags & ASSOC_REQ_DISABLE_VHT)
3998 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3999
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004000 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4001 sband = local->hw.wiphy->bands[req->bss->channel->band];
4002 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004003 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004004 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004005 if (!bss->wmm_used)
4006 netdev_info(sdata->dev,
4007 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004008 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004009
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004010 /* disable VHT if we don't support it or the AP doesn't use WMM */
4011 if (!sband->vht_cap.vht_supported ||
4012 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4013 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004014 if (!bss->wmm_used)
4015 netdev_info(sdata->dev,
4016 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004017 }
4018
Ben Greearef96a8422011-11-18 11:32:00 -08004019 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4020 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4021 sizeof(ifmgd->ht_capa_mask));
4022
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004023 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4024 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4025 sizeof(ifmgd->vht_capa_mask));
4026
Johannes Berg77fdaa12009-07-07 03:45:17 +02004027 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004028 memcpy(assoc_data->ie, req->ie, req->ie_len);
4029 assoc_data->ie_len = req->ie_len;
4030 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004031
Johannes Berg66e67e42012-01-20 13:55:27 +01004032 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004033
Johannes Bergaf6b6372009-12-23 13:15:35 +01004034 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4035 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004036 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004037 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004038 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004039 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004040 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004041
Johannes Berg66e67e42012-01-20 13:55:27 +01004042 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004043 assoc_data->wmm = bss->wmm_used &&
4044 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004045 assoc_data->supp_rates = bss->supp_rates;
4046 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004047
Johannes Berg9caf0362012-11-29 01:25:20 +01004048 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004049 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4050 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4051 assoc_data->ap_ht_param =
4052 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4053 else
Johannes Berga8243b72012-11-22 14:32:09 +01004054 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004055 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4056 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4057 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4058 sizeof(struct ieee80211_vht_cap));
4059 else
4060 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004061 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004062
Kalle Valoab133152010-01-12 10:42:31 +02004063 if (bss->wmm_used && bss->uapsd_supported &&
4064 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01004065 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004066 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4067 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004068 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004069 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4070 }
4071
Johannes Berg77fdaa12009-07-07 03:45:17 +02004072 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004073 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004074
4075 if (req->use_mfp) {
4076 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4077 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4078 } else {
4079 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4080 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4081 }
4082
4083 if (req->crypto.control_port)
4084 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4085 else
4086 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4087
Johannes Berga621fa42010-08-27 14:26:54 +03004088 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4089 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4090
Johannes Berg66e67e42012-01-20 13:55:27 +01004091 /* kick off associate process */
4092
4093 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004094 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004095
Johannes Berga1cf7752012-03-08 15:02:07 +01004096 err = ieee80211_prep_connection(sdata, req->bss, true);
4097 if (err)
4098 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004099
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004100 rcu_read_lock();
4101 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004102
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004103 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4104 !beacon_ies) {
4105 /*
4106 * Wait up to one beacon interval ...
4107 * should this be more if we miss one?
4108 */
4109 sdata_info(sdata, "waiting for beacon from %pM\n",
4110 ifmgd->bssid);
4111 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004112 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004113 assoc_data->need_beacon = true;
4114 } else if (beacon_ies) {
4115 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4116 beacon_ies->data,
4117 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004118 u8 dtim_count = 0;
4119
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004120 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4121 const struct ieee80211_tim_ie *tim;
4122 tim = (void *)(tim_ie + 2);
4123 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004124 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004125 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004126 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004127 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004128 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004129
4130 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4131 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4132 sdata->vif.bss_conf.sync_device_ts =
4133 bss->device_ts_beacon;
4134 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4135 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004136 } else {
4137 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004138 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004139 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004140 rcu_read_unlock();
4141
Johannes Berg66e67e42012-01-20 13:55:27 +01004142 run_again(ifmgd, assoc_data->timeout);
4143
Paul Stewartfcff4f12012-02-23 17:59:53 -08004144 if (bss->corrupt_data) {
4145 char *corrupt_type = "data";
4146 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4147 if (bss->corrupt_data &
4148 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4149 corrupt_type = "beacon and probe response";
4150 else
4151 corrupt_type = "beacon";
4152 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4153 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004154 sdata_info(sdata, "associating with AP with corrupt %s\n",
4155 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004156 }
4157
Johannes Berg66e67e42012-01-20 13:55:27 +01004158 err = 0;
4159 goto out;
4160 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004161 memset(ifmgd->bssid, 0, ETH_ALEN);
4162 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004163 ifmgd->assoc_data = NULL;
4164 err_free:
4165 kfree(assoc_data);
4166 out:
4167 mutex_unlock(&ifmgd->mtx);
4168
4169 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004170}
4171
4172int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004173 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004174{
4175 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004176 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004177 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004178 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004179
Johannes Berg77fdaa12009-07-07 03:45:17 +02004180 mutex_lock(&ifmgd->mtx);
4181
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004182 sdata_info(sdata,
4183 "deauthenticating from %pM by local choice (reason=%d)\n",
4184 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004185
Johannes Berg86552012012-10-29 09:46:31 +01004186 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004187 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004188 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4189 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004190 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004191 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004192 ieee80211_destroy_auth_data(sdata, false);
4193 mutex_unlock(&ifmgd->mtx);
4194
4195 sent_frame = tx;
4196 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004197 }
4198
Johannes Berg86552012012-10-29 09:46:31 +01004199 if (ifmgd->associated &&
4200 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4201 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4202 req->reason_code, tx, frame_buf);
4203 sent_frame = tx;
4204 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004205 mutex_unlock(&ifmgd->mtx);
4206
Johannes Berg86552012012-10-29 09:46:31 +01004207 out:
Johannes Berg86552012012-10-29 09:46:31 +01004208 if (sent_frame)
4209 __cfg80211_send_deauth(sdata->dev, frame_buf,
4210 IEEE80211_DEAUTH_FRAME_LEN);
4211
Johannes Berg77fdaa12009-07-07 03:45:17 +02004212 return 0;
4213}
4214
4215int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004216 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004217{
4218 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004219 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004220 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004221
Johannes Berg77fdaa12009-07-07 03:45:17 +02004222 mutex_lock(&ifmgd->mtx);
4223
Johannes Berg8d8b2612009-07-25 11:58:36 +02004224 /*
4225 * cfg80211 should catch this ... but it's racy since
4226 * we can receive a disassoc frame, process it, hand it
4227 * to cfg80211 while that's in a locked section already
4228 * trying to tell us that the user wants to disconnect.
4229 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004230 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004231 mutex_unlock(&ifmgd->mtx);
4232 return -ENOLINK;
4233 }
4234
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004235 sdata_info(sdata,
4236 "disassociating from %pM by local choice (reason=%d)\n",
4237 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004238
Jouni Malinene69e95d2010-03-29 23:29:31 -07004239 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004240 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4241 req->reason_code, !req->local_state_change,
4242 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004243 mutex_unlock(&ifmgd->mtx);
4244
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004245 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4246 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004247
Johannes Berg77fdaa12009-07-07 03:45:17 +02004248 return 0;
4249}
Jouni Malinen026331c2010-02-15 12:53:10 +02004250
Eliad Pellerafa762f2012-04-23 14:45:15 +03004251void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004252{
4253 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4254
Ben Greear49921852013-02-20 09:41:09 -08004255 /*
4256 * Make sure some work items will not run after this,
4257 * they will not do anything but might not have been
4258 * cancelled when disconnecting.
4259 */
4260 cancel_work_sync(&ifmgd->monitor_work);
4261 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4262 cancel_work_sync(&ifmgd->request_smps_work);
4263 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4264 cancel_work_sync(&ifmgd->chswitch_work);
4265
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004266 mutex_lock(&ifmgd->mtx);
4267 if (ifmgd->assoc_data)
4268 ieee80211_destroy_assoc_data(sdata, false);
4269 if (ifmgd->auth_data)
4270 ieee80211_destroy_auth_data(sdata, false);
4271 del_timer_sync(&ifmgd->timer);
4272 mutex_unlock(&ifmgd->mtx);
4273}
4274
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004275void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4276 enum nl80211_cqm_rssi_threshold_event rssi_event,
4277 gfp_t gfp)
4278{
4279 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4280
Johannes Bergb5878a22010-04-07 16:48:40 +02004281 trace_api_cqm_rssi_notify(sdata, rssi_event);
4282
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004283 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4284}
4285EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);