blob: 79647eaecc8d9b422f22d0fd8eb1126a7ce37908 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
35#define IEEE80211_AUTH_MAX_TRIES 3
36#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
39#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static int max_nullfunc_tries = 2;
42module_param(max_nullfunc_tries, int, 0644);
43MODULE_PARM_DESC(max_nullfunc_tries,
44 "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46static int max_probe_tries = 5;
47module_param(max_probe_tries, int, 0644);
48MODULE_PARM_DESC(max_probe_tries,
49 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * Beacon loss timeout is calculated as N frames times the
53 * advertised beacon interval. This may need to be somewhat
54 * higher than what hardware might detect to account for
55 * delays in the host processing frames. But since we also
56 * probe on beacon miss before declaring the connection lost
57 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Ben Greear59c1ec22013-03-19 14:19:56 -070059static int beacon_loss_count = 7;
60module_param(beacon_loss_count, int, 0644);
61MODULE_PARM_DESC(beacon_loss_count,
62 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010063
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time the connection can be idle before we probe
66 * it to see if we can still talk to the AP.
67 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030068#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020069/*
70 * Time we wait for a probe response after sending
71 * a probe request because of beacon loss or for
72 * checking the connection still works.
73 */
Ben Greear180205b2011-02-04 15:30:24 -080074static int probe_wait_ms = 500;
75module_param(probe_wait_ms, int, 0644);
76MODULE_PARM_DESC(probe_wait_ms,
77 "Maximum time(ms) to wait for probe response"
78 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070079
Jouni Malinen17e4ec12010-03-29 23:28:30 -070080/*
81 * Weight given to the latest Beacon frame when calculating average signal
82 * strength for Beacon frames received in the current BSS. This must be
83 * between 1 and 15.
84 */
85#define IEEE80211_SIGNAL_AVE_WEIGHT 3
86
Jouni Malinen391a2002010-08-27 22:22:00 +030087/*
88 * How many Beacon frames need to have been used in average signal strength
89 * before starting to indicate signal change events.
90 */
91#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100117
118 /* used when a processed beacon causes a deauth */
119 RX_MGMT_CFG80211_TX_DEAUTH,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120};
121
Johannes Berg5484e232008-09-08 17:44:27 +0200122/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
124{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200125 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200126}
127
Johannes Bergca386f32009-07-10 02:39:48 +0200128/*
129 * We can have multiple work items (and connection probing)
130 * scheduling this timer, but we need to take care to only
131 * reschedule it when it should fire _earlier_ than it was
132 * asked for before, or if it's not pending right now. This
133 * function ensures that. Note that it then is required to
134 * run this function for all timeouts after the first one
135 * has happened -- the work that runs from this timer will
136 * do that.
137 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100138static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200139{
140 ASSERT_MGD_MTX(ifmgd);
141
142 if (!timer_pending(&ifmgd->timer) ||
143 time_before(timeout, ifmgd->timer.expires))
144 mod_timer(&ifmgd->timer, timeout);
145}
146
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400147void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200148{
Johannes Bergc1288b12012-01-19 09:29:57 +0100149 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 return;
151
Johannes Berg7eeff742012-07-18 10:27:27 +0200152 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
153 return;
154
Johannes Bergb291ba12009-07-10 15:29:03 +0200155 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100156 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200157}
158
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400159void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
160{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400161 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
162
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100163 if (unlikely(!sdata->u.mgd.associated))
164 return;
165
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400166 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
167 return;
168
169 mod_timer(&sdata->u.mgd.conn_mon_timer,
170 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400171
172 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400173}
174
Johannes Berg5484e232008-09-08 17:44:27 +0200175static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200176{
Johannes Berg5484e232008-09-08 17:44:27 +0200177 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200178}
179
Johannes Berg6565ec92013-02-08 14:52:32 +0100180static u32 chandef_downgrade(struct cfg80211_chan_def *c)
181{
182 u32 ret;
183 int tmp;
184
185 switch (c->width) {
186 case NL80211_CHAN_WIDTH_20:
187 c->width = NL80211_CHAN_WIDTH_20_NOHT;
188 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
189 break;
190 case NL80211_CHAN_WIDTH_40:
191 c->width = NL80211_CHAN_WIDTH_20;
192 c->center_freq1 = c->chan->center_freq;
193 ret = IEEE80211_STA_DISABLE_40MHZ |
194 IEEE80211_STA_DISABLE_VHT;
195 break;
196 case NL80211_CHAN_WIDTH_80:
197 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
198 /* n_P40 */
199 tmp /= 2;
200 /* freq_P40 */
201 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
202 c->width = NL80211_CHAN_WIDTH_40;
203 ret = IEEE80211_STA_DISABLE_VHT;
204 break;
205 case NL80211_CHAN_WIDTH_80P80:
206 c->center_freq2 = 0;
207 c->width = NL80211_CHAN_WIDTH_80;
208 ret = IEEE80211_STA_DISABLE_80P80MHZ |
209 IEEE80211_STA_DISABLE_160MHZ;
210 break;
211 case NL80211_CHAN_WIDTH_160:
212 /* n_P20 */
213 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
214 /* n_P80 */
215 tmp /= 4;
216 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
217 c->width = NL80211_CHAN_WIDTH_80;
218 ret = IEEE80211_STA_DISABLE_80P80MHZ |
219 IEEE80211_STA_DISABLE_160MHZ;
220 break;
221 default:
222 case NL80211_CHAN_WIDTH_20_NOHT:
223 WARN_ON_ONCE(1);
224 c->width = NL80211_CHAN_WIDTH_20_NOHT;
225 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
226 break;
227 }
228
229 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
230
231 return ret;
232}
233
234static u32
235ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
236 struct ieee80211_supported_band *sband,
237 struct ieee80211_channel *channel,
238 const struct ieee80211_ht_operation *ht_oper,
239 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100240 struct cfg80211_chan_def *chandef, bool verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100241{
242 struct cfg80211_chan_def vht_chandef;
243 u32 ht_cfreq, ret;
244
245 chandef->chan = channel;
246 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
247 chandef->center_freq1 = channel->center_freq;
248 chandef->center_freq2 = 0;
249
250 if (!ht_oper || !sband->ht_cap.ht_supported) {
251 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
252 goto out;
253 }
254
255 chandef->width = NL80211_CHAN_WIDTH_20;
256
257 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
258 channel->band);
259 /* check that channel matches the right operating channel */
260 if (channel->center_freq != ht_cfreq) {
261 /*
262 * It's possible that some APs are confused here;
263 * Netgear WNDR3700 sometimes reports 4 higher than
264 * the actual channel in association responses, but
265 * since we look at probe response/beacon data here
266 * it should be OK.
267 */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100268 if (verbose)
269 sdata_info(sdata,
270 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
271 channel->center_freq, ht_cfreq,
272 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100273 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
274 goto out;
275 }
276
277 /* check 40 MHz support, if we have it */
278 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
279 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
280 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
281 chandef->width = NL80211_CHAN_WIDTH_40;
282 chandef->center_freq1 += 10;
283 break;
284 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
285 chandef->width = NL80211_CHAN_WIDTH_40;
286 chandef->center_freq1 -= 10;
287 break;
288 }
289 } else {
290 /* 40 MHz (and 80 MHz) must be supported for VHT */
291 ret = IEEE80211_STA_DISABLE_VHT;
292 goto out;
293 }
294
295 if (!vht_oper || !sband->vht_cap.vht_supported) {
296 ret = IEEE80211_STA_DISABLE_VHT;
297 goto out;
298 }
299
300 vht_chandef.chan = channel;
301 vht_chandef.center_freq1 =
302 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
303 channel->band);
304 vht_chandef.center_freq2 = 0;
305
306 if (vht_oper->center_freq_seg2_idx)
307 vht_chandef.center_freq2 =
308 ieee80211_channel_to_frequency(
309 vht_oper->center_freq_seg2_idx,
310 channel->band);
311
312 switch (vht_oper->chan_width) {
313 case IEEE80211_VHT_CHANWIDTH_USE_HT:
314 vht_chandef.width = chandef->width;
315 break;
316 case IEEE80211_VHT_CHANWIDTH_80MHZ:
317 vht_chandef.width = NL80211_CHAN_WIDTH_80;
318 break;
319 case IEEE80211_VHT_CHANWIDTH_160MHZ:
320 vht_chandef.width = NL80211_CHAN_WIDTH_160;
321 break;
322 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
323 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
324 break;
325 default:
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100326 if (verbose)
327 sdata_info(sdata,
328 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
329 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100330 ret = IEEE80211_STA_DISABLE_VHT;
331 goto out;
332 }
333
334 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100335 if (verbose)
336 sdata_info(sdata,
337 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100338 ret = IEEE80211_STA_DISABLE_VHT;
339 goto out;
340 }
341
342 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
343 ret = 0;
344 goto out;
345 }
346
347 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100348 if (verbose)
349 sdata_info(sdata,
350 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100351 ret = IEEE80211_STA_DISABLE_VHT;
352 goto out;
353 }
354
355 *chandef = vht_chandef;
356
357 ret = 0;
358
359out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100360 /* don't print the message below for VHT mismatch if VHT is disabled */
361 if (ret & IEEE80211_STA_DISABLE_VHT)
362 vht_chandef = *chandef;
363
Johannes Berg6565ec92013-02-08 14:52:32 +0100364 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
365 IEEE80211_CHAN_DISABLED)) {
366 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
367 ret = IEEE80211_STA_DISABLE_HT |
368 IEEE80211_STA_DISABLE_VHT;
369 goto out;
370 }
371
372 ret |= chandef_downgrade(chandef);
373 }
374
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100375 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100376 sdata_info(sdata,
377 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
378
379 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
380 return ret;
381}
382
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100383static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
384 struct sta_info *sta,
385 const struct ieee80211_ht_operation *ht_oper,
386 const struct ieee80211_vht_operation *vht_oper,
387 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200388{
389 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100390 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200391 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200392 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100393 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200394 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100395 u32 flags;
396 enum ieee80211_sta_rx_bandwidth new_sta_bw;
397 int ret;
398
399 /* if HT was/is disabled, don't track any bandwidth changes */
400 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
401 return 0;
402
403 /* don't check VHT if we associated as non-VHT station */
404 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
405 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200406
Johannes Berg11289582013-02-07 17:36:12 +0100407 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100408 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100409
Johannes Bergf2d93302013-02-08 13:15:55 +0100410 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200411 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200412
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100413 /* calculate new channel (type) based on HT/VHT operation IEs */
414 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
415 vht_oper, &chandef, false);
416
417 /*
418 * Downgrade the new channel if we associated with restricted
419 * capabilities. For example, if we associated as a 20 MHz STA
420 * to a 40 MHz AP (due to regulatory, capabilities or config
421 * reasons) then switching to a 40 MHz channel now won't do us
422 * any good -- we couldn't use it with the AP.
423 */
424 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
425 chandef.width == NL80211_CHAN_WIDTH_80P80)
426 flags |= chandef_downgrade(&chandef);
427 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
428 chandef.width == NL80211_CHAN_WIDTH_160)
429 flags |= chandef_downgrade(&chandef);
430 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
431 chandef.width > NL80211_CHAN_WIDTH_20)
432 flags |= chandef_downgrade(&chandef);
433
434 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
435 return 0;
436
437 sdata_info(sdata,
438 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
439 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
440 chandef.center_freq1, chandef.center_freq2);
441
442 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
443 IEEE80211_STA_DISABLE_VHT |
444 IEEE80211_STA_DISABLE_40MHZ |
445 IEEE80211_STA_DISABLE_80P80MHZ |
446 IEEE80211_STA_DISABLE_160MHZ)) ||
447 !cfg80211_chandef_valid(&chandef)) {
448 sdata_info(sdata,
449 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
450 ifmgd->bssid);
451 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200452 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200453
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100454 switch (chandef.width) {
455 case NL80211_CHAN_WIDTH_20_NOHT:
456 case NL80211_CHAN_WIDTH_20:
457 new_sta_bw = IEEE80211_STA_RX_BW_20;
458 break;
459 case NL80211_CHAN_WIDTH_40:
460 new_sta_bw = IEEE80211_STA_RX_BW_40;
461 break;
462 case NL80211_CHAN_WIDTH_80:
463 new_sta_bw = IEEE80211_STA_RX_BW_80;
464 break;
465 case NL80211_CHAN_WIDTH_80P80:
466 case NL80211_CHAN_WIDTH_160:
467 new_sta_bw = IEEE80211_STA_RX_BW_160;
468 break;
469 default:
470 return -EINVAL;
471 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200472
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100473 if (new_sta_bw > sta->cur_max_bandwidth)
474 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200475
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100476 if (new_sta_bw < sta->sta.bandwidth) {
477 sta->sta.bandwidth = new_sta_bw;
478 rate_control_rate_update(local, sband, sta,
479 IEEE80211_RC_BW_CHANGED);
480 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530481
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100482 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
483 if (ret) {
484 sdata_info(sdata,
485 "AP %pM changed bandwidth to incompatible one - disconnect\n",
486 ifmgd->bssid);
487 return ret;
488 }
489
490 if (new_sta_bw > sta->sta.bandwidth) {
491 sta->sta.bandwidth = new_sta_bw;
492 rate_control_rate_update(local, sband, sta,
493 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200494 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200495
Johannes Berg074d46d2012-03-15 19:45:16 +0100496 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200497
498 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100499 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
500 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200501 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200502 }
503
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100504 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200505}
506
Johannes Berg9c6bd792008-09-11 00:01:52 +0200507/* frame sending functions */
508
Johannes Berg66e67e42012-01-20 13:55:27 +0100509static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
510 struct ieee80211_supported_band *sband,
511 u32 *rates)
512{
513 int i, j, count;
514 *rates = 0;
515 count = 0;
516 for (i = 0; i < supp_rates_len; i++) {
517 int rate = (supp_rates[i] & 0x7F) * 5;
518
519 for (j = 0; j < sband->n_bitrates; j++)
520 if (sband->bitrates[j].bitrate == rate) {
521 *rates |= BIT(j);
522 count++;
523 break;
524 }
525 }
526
527 return count;
528}
529
530static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200531 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100532 struct ieee80211_supported_band *sband,
533 struct ieee80211_channel *channel,
534 enum ieee80211_smps_mode smps)
535{
Johannes Berg66e67e42012-01-20 13:55:27 +0100536 u8 *pos;
537 u32 flags = channel->flags;
538 u16 cap;
539 struct ieee80211_sta_ht_cap ht_cap;
540
541 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
542
Johannes Berg66e67e42012-01-20 13:55:27 +0100543 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
544 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
545
Johannes Berg66e67e42012-01-20 13:55:27 +0100546 /* determine capability flags */
547 cap = ht_cap.cap;
548
Johannes Berg9dde6422012-05-16 23:43:19 +0200549 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100550 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
551 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
552 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
553 cap &= ~IEEE80211_HT_CAP_SGI_40;
554 }
555 break;
556 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
557 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
558 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
559 cap &= ~IEEE80211_HT_CAP_SGI_40;
560 }
561 break;
562 }
563
Johannes Berg24398e32012-03-28 10:58:36 +0200564 /*
565 * If 40 MHz was disabled associate as though we weren't
566 * capable of 40 MHz -- some broken APs will never fall
567 * back to trying to transmit in 20 MHz.
568 */
569 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
570 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
571 cap &= ~IEEE80211_HT_CAP_SGI_40;
572 }
573
Johannes Berg66e67e42012-01-20 13:55:27 +0100574 /* set SM PS mode properly */
575 cap &= ~IEEE80211_HT_CAP_SM_PS;
576 switch (smps) {
577 case IEEE80211_SMPS_AUTOMATIC:
578 case IEEE80211_SMPS_NUM_MODES:
579 WARN_ON(1);
580 case IEEE80211_SMPS_OFF:
581 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
582 IEEE80211_HT_CAP_SM_PS_SHIFT;
583 break;
584 case IEEE80211_SMPS_STATIC:
585 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
586 IEEE80211_HT_CAP_SM_PS_SHIFT;
587 break;
588 case IEEE80211_SMPS_DYNAMIC:
589 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
590 IEEE80211_HT_CAP_SM_PS_SHIFT;
591 break;
592 }
593
594 /* reserve and fill IE */
595 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
596 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
597}
598
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
600 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100601 struct ieee80211_supported_band *sband,
602 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000603{
604 u8 *pos;
605 u32 cap;
606 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100607 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000608
609 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
610
611 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100612 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000613
614 /* determine capability flags */
615 cap = vht_cap.cap;
616
Johannes Bergf2d9d272012-11-22 14:11:39 +0100617 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
618 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
619 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
620 }
621
622 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
623 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
624 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
625 }
626
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100627 /*
628 * Some APs apparently get confused if our capabilities are better
629 * than theirs, so restrict what we advertise in the assoc request.
630 */
631 if (!(ap_vht_cap->vht_cap_info &
632 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
633 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
634
635 if (!(ap_vht_cap->vht_cap_info &
636 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
637 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
638 IEEE80211_VHT_CAP_RXSTBC_3 |
639 IEEE80211_VHT_CAP_RXSTBC_4);
640
641 for (i = 0; i < 8; i++) {
642 int shift = i * 2;
643 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
644 u16 ap_mcs, our_mcs;
645
646 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
647 mask) >> shift;
648 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
649 mask) >> shift;
650
Johannes Berg24af7172013-03-01 17:33:18 +0100651 if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
652 continue;
653
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100654 switch (ap_mcs) {
655 default:
656 if (our_mcs <= ap_mcs)
657 break;
658 /* fall through */
659 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
660 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
661 vht_cap.vht_mcs.rx_mcs_map |=
662 cpu_to_le16(ap_mcs << shift);
663 }
664 }
665
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000666 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000667 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000668 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
669}
670
Johannes Berg66e67e42012-01-20 13:55:27 +0100671static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
672{
673 struct ieee80211_local *local = sdata->local;
674 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
675 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
676 struct sk_buff *skb;
677 struct ieee80211_mgmt *mgmt;
678 u8 *pos, qos_info;
679 size_t offset = 0, noffset;
680 int i, count, rates_len, supp_rates_len;
681 u16 capab;
682 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200683 struct ieee80211_chanctx_conf *chanctx_conf;
684 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100685 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100686
687 lockdep_assert_held(&ifmgd->mtx);
688
Johannes Berg55de9082012-07-26 17:24:39 +0200689 rcu_read_lock();
690 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
691 if (WARN_ON(!chanctx_conf)) {
692 rcu_read_unlock();
693 return;
694 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100695 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200696 rcu_read_unlock();
697 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100698
699 if (assoc_data->supp_rates_len) {
700 /*
701 * Get all rates supported by the device and the AP as
702 * some APs don't like getting a superset of their rates
703 * in the association request (e.g. D-Link DAP 1353 in
704 * b-only mode)...
705 */
706 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
707 assoc_data->supp_rates_len,
708 sband, &rates);
709 } else {
710 /*
711 * In case AP not provide any supported rates information
712 * before association, we send information element(s) with
713 * all rates that we support.
714 */
715 rates = ~0;
716 rates_len = sband->n_bitrates;
717 }
718
719 skb = alloc_skb(local->hw.extra_tx_headroom +
720 sizeof(*mgmt) + /* bit too much but doesn't matter */
721 2 + assoc_data->ssid_len + /* SSID */
722 4 + rates_len + /* (extended) rates */
723 4 + /* power capability */
724 2 + 2 * sband->n_channels + /* supported channels */
725 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000726 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100727 assoc_data->ie_len + /* extra IEs */
728 9, /* WMM */
729 GFP_KERNEL);
730 if (!skb)
731 return;
732
733 skb_reserve(skb, local->hw.extra_tx_headroom);
734
735 capab = WLAN_CAPABILITY_ESS;
736
737 if (sband->band == IEEE80211_BAND_2GHZ) {
738 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
739 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
740 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
741 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
742 }
743
744 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
745 capab |= WLAN_CAPABILITY_PRIVACY;
746
747 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
748 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
749 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
750
751 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
752 memset(mgmt, 0, 24);
753 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
754 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
755 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
756
757 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
758 skb_put(skb, 10);
759 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
760 IEEE80211_STYPE_REASSOC_REQ);
761 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
762 mgmt->u.reassoc_req.listen_interval =
763 cpu_to_le16(local->hw.conf.listen_interval);
764 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
765 ETH_ALEN);
766 } else {
767 skb_put(skb, 4);
768 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
769 IEEE80211_STYPE_ASSOC_REQ);
770 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
771 mgmt->u.assoc_req.listen_interval =
772 cpu_to_le16(local->hw.conf.listen_interval);
773 }
774
775 /* SSID */
776 pos = skb_put(skb, 2 + assoc_data->ssid_len);
777 *pos++ = WLAN_EID_SSID;
778 *pos++ = assoc_data->ssid_len;
779 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
780
781 /* add all rates which were marked to be used above */
782 supp_rates_len = rates_len;
783 if (supp_rates_len > 8)
784 supp_rates_len = 8;
785
786 pos = skb_put(skb, supp_rates_len + 2);
787 *pos++ = WLAN_EID_SUPP_RATES;
788 *pos++ = supp_rates_len;
789
790 count = 0;
791 for (i = 0; i < sband->n_bitrates; i++) {
792 if (BIT(i) & rates) {
793 int rate = sband->bitrates[i].bitrate;
794 *pos++ = (u8) (rate / 5);
795 if (++count == 8)
796 break;
797 }
798 }
799
800 if (rates_len > count) {
801 pos = skb_put(skb, rates_len - count + 2);
802 *pos++ = WLAN_EID_EXT_SUPP_RATES;
803 *pos++ = rates_len - count;
804
805 for (i++; i < sband->n_bitrates; i++) {
806 if (BIT(i) & rates) {
807 int rate = sband->bitrates[i].bitrate;
808 *pos++ = (u8) (rate / 5);
809 }
810 }
811 }
812
813 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
814 /* 1. power capabilities */
815 pos = skb_put(skb, 4);
816 *pos++ = WLAN_EID_PWR_CAPABILITY;
817 *pos++ = 2;
818 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200819 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100820
821 /* 2. supported channels */
822 /* TODO: get this in reg domain format */
823 pos = skb_put(skb, 2 * sband->n_channels + 2);
824 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
825 *pos++ = 2 * sband->n_channels;
826 for (i = 0; i < sband->n_channels; i++) {
827 *pos++ = ieee80211_frequency_to_channel(
828 sband->channels[i].center_freq);
829 *pos++ = 1; /* one channel in the subband*/
830 }
831 }
832
833 /* if present, add any custom IEs that go before HT */
834 if (assoc_data->ie_len && assoc_data->ie) {
835 static const u8 before_ht[] = {
836 WLAN_EID_SSID,
837 WLAN_EID_SUPP_RATES,
838 WLAN_EID_EXT_SUPP_RATES,
839 WLAN_EID_PWR_CAPABILITY,
840 WLAN_EID_SUPPORTED_CHANNELS,
841 WLAN_EID_RSN,
842 WLAN_EID_QOS_CAPA,
843 WLAN_EID_RRM_ENABLED_CAPABILITIES,
844 WLAN_EID_MOBILITY_DOMAIN,
845 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
846 };
847 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
848 before_ht, ARRAY_SIZE(before_ht),
849 offset);
850 pos = skb_put(skb, noffset - offset);
851 memcpy(pos, assoc_data->ie + offset, noffset - offset);
852 offset = noffset;
853 }
854
Johannes Bergf2d9d272012-11-22 14:11:39 +0100855 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
856 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
857 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
858
Johannes Berga8243b72012-11-22 14:32:09 +0100859 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200860 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200861 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100862
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000863 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100864 ieee80211_add_vht_ie(sdata, skb, sband,
865 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000866
Johannes Berg66e67e42012-01-20 13:55:27 +0100867 /* if present, add any custom non-vendor IEs that go after HT */
868 if (assoc_data->ie_len && assoc_data->ie) {
869 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
870 assoc_data->ie_len,
871 offset);
872 pos = skb_put(skb, noffset - offset);
873 memcpy(pos, assoc_data->ie + offset, noffset - offset);
874 offset = noffset;
875 }
876
Johannes Berg76f03032012-03-08 15:02:05 +0100877 if (assoc_data->wmm) {
878 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200879 qos_info = ifmgd->uapsd_queues;
880 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100881 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
882 } else {
883 qos_info = 0;
884 }
885
886 pos = skb_put(skb, 9);
887 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
888 *pos++ = 7; /* len */
889 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
890 *pos++ = 0x50;
891 *pos++ = 0xf2;
892 *pos++ = 2; /* WME */
893 *pos++ = 0; /* WME info */
894 *pos++ = 1; /* WME ver */
895 *pos++ = qos_info;
896 }
897
898 /* add any remaining custom (i.e. vendor specific here) IEs */
899 if (assoc_data->ie_len && assoc_data->ie) {
900 noffset = assoc_data->ie_len;
901 pos = skb_put(skb, noffset - offset);
902 memcpy(pos, assoc_data->ie + offset, noffset - offset);
903 }
904
Johannes Berga1845fc2012-06-27 13:18:36 +0200905 drv_mgd_prepare_tx(local, sdata);
906
Johannes Berg66e67e42012-01-20 13:55:27 +0100907 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100908 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
909 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
910 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100911 ieee80211_tx_skb(sdata, skb);
912}
913
Kalle Valo572e0012009-02-10 17:09:31 +0200914void ieee80211_send_pspoll(struct ieee80211_local *local,
915 struct ieee80211_sub_if_data *sdata)
916{
Kalle Valo572e0012009-02-10 17:09:31 +0200917 struct ieee80211_pspoll *pspoll;
918 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200919
Kalle Valod8cd1892010-01-05 20:16:26 +0200920 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
921 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200922 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200923
Kalle Valod8cd1892010-01-05 20:16:26 +0200924 pspoll = (struct ieee80211_pspoll *) skb->data;
925 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200926
Johannes Berg62ae67b2009-11-18 18:42:05 +0100927 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
928 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200929}
930
Johannes Berg965beda2009-04-16 13:17:24 +0200931void ieee80211_send_nullfunc(struct ieee80211_local *local,
932 struct ieee80211_sub_if_data *sdata,
933 int powersave)
934{
935 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200936 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530937 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200938
Kalle Valod8cd1892010-01-05 20:16:26 +0200939 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
940 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200941 return;
942
Kalle Valod8cd1892010-01-05 20:16:26 +0200943 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200944 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200945 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200946
Seth Forshee6c17b772013-02-11 11:21:07 -0600947 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
948 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530949 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
950 IEEE80211_STA_CONNECTION_POLL))
951 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
952
Johannes Berg62ae67b2009-11-18 18:42:05 +0100953 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200954}
955
Felix Fietkaud5242152010-01-08 18:06:26 +0100956static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
957 struct ieee80211_sub_if_data *sdata)
958{
959 struct sk_buff *skb;
960 struct ieee80211_hdr *nullfunc;
961 __le16 fc;
962
963 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
964 return;
965
966 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700967 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100968 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700969
Felix Fietkaud5242152010-01-08 18:06:26 +0100970 skb_reserve(skb, local->hw.extra_tx_headroom);
971
972 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
973 memset(nullfunc, 0, 30);
974 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
975 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
976 nullfunc->frame_control = fc;
977 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
978 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
979 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
980 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
981
982 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
983 ieee80211_tx_skb(sdata, skb);
984}
985
Johannes Bergcc32abd2009-05-15 11:52:31 +0200986/* spectrum management related things */
987static void ieee80211_chswitch_work(struct work_struct *work)
988{
989 struct ieee80211_sub_if_data *sdata =
990 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100991 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200992 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
993
Johannes Berg9607e6b662009-12-23 13:15:31 +0100994 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200995 return;
996
Johannes Berg77fdaa12009-07-07 03:45:17 +0200997 mutex_lock(&ifmgd->mtx);
998 if (!ifmgd->associated)
999 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001000
Karl Beldan675a0b02013-03-25 16:26:57 +01001001 /*
1002 * FIXME: Here we are downgrading to NL80211_CHAN_WIDTH_20_NOHT
1003 * and don't adjust our ht/vht settings
1004 * This is wrong - we should behave according to the CSA params
1005 */
1006 local->_oper_chandef.chan = local->csa_channel;
1007 local->_oper_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
1008 local->_oper_chandef.center_freq1 =
1009 local->_oper_chandef.chan->center_freq;
1010 local->_oper_chandef.center_freq2 = 0;
1011
1012 if (!local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001013 /* call "hw_config" only if doing sw channel switch */
Karl Beldan675a0b02013-03-25 16:26:57 +01001014 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +03001015 } else {
1016 /* update the device channel directly */
Karl Beldan675a0b02013-03-25 16:26:57 +01001017 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001018 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001019
Johannes Berg77fdaa12009-07-07 03:45:17 +02001020 /* XXX: shouldn't really modify cfg80211-owned data! */
Karl Beldan675a0b02013-03-25 16:26:57 +01001021 ifmgd->associated->channel = local->_oper_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001022
Johannes Berg57eebdf2012-08-01 15:50:46 +02001023 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +01001024 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001025 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +02001026 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001027 out:
1028 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1029 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001030}
1031
Johannes Berg5ce6e432010-05-11 16:20:57 +02001032void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1033{
Johannes Berg55de9082012-07-26 17:24:39 +02001034 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1035 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001036
1037 trace_api_chswitch_done(sdata, success);
1038 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001039 sdata_info(sdata,
1040 "driver channel switch failed, disconnecting\n");
1041 ieee80211_queue_work(&sdata->local->hw,
1042 &ifmgd->csa_connection_drop_work);
1043 } else {
1044 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001045 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001046}
1047EXPORT_SYMBOL(ieee80211_chswitch_done);
1048
Johannes Bergcc32abd2009-05-15 11:52:31 +02001049static void ieee80211_chswitch_timer(unsigned long data)
1050{
1051 struct ieee80211_sub_if_data *sdata =
1052 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001053
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001054 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001055}
1056
Johannes Berg4a3cb702013-02-12 16:43:19 +01001057void
1058ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1059 const struct ieee80211_channel_sw_ie *sw_elem,
1060 struct ieee80211_bss *bss, u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001061{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001062 struct cfg80211_bss *cbss =
1063 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001064 struct ieee80211_channel *new_ch;
1065 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001066 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1067 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +02001068 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001069
Johannes Berg77fdaa12009-07-07 03:45:17 +02001070 ASSERT_MGD_MTX(ifmgd);
1071
1072 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001073 return;
1074
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001075 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001076 return;
1077
1078 /* Disregard subsequent beacons if we are already running a timer
1079 processing a CSA */
1080
1081 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1082 return;
1083
1084 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001085 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1086 sdata_info(sdata,
1087 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1088 ifmgd->associated->bssid, new_freq);
1089 ieee80211_queue_work(&sdata->local->hw,
1090 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001091 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001092 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001093
Johannes Berg57eebdf2012-08-01 15:50:46 +02001094 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1095
Johannes Berg55de9082012-07-26 17:24:39 +02001096 if (sdata->local->use_chanctx) {
1097 sdata_info(sdata,
1098 "not handling channel switch with channel contexts\n");
1099 ieee80211_queue_work(&sdata->local->hw,
1100 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001101 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001102 }
1103
1104 mutex_lock(&sdata->local->chanctx_mtx);
1105 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1106 mutex_unlock(&sdata->local->chanctx_mtx);
1107 return;
1108 }
1109 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1110 struct ieee80211_chanctx, conf);
1111 if (chanctx->refcount > 1) {
1112 sdata_info(sdata,
1113 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1114 ieee80211_queue_work(&sdata->local->hw,
1115 &ifmgd->csa_connection_drop_work);
1116 mutex_unlock(&sdata->local->chanctx_mtx);
1117 return;
1118 }
1119 mutex_unlock(&sdata->local->chanctx_mtx);
1120
1121 sdata->local->csa_channel = new_ch;
1122
Johannes Berg57eebdf2012-08-01 15:50:46 +02001123 if (sw_elem->mode)
1124 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001125 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001126 IEEE80211_QUEUE_STOP_REASON_CSA);
1127
Johannes Berg5ce6e432010-05-11 16:20:57 +02001128 if (sdata->local->ops->channel_switch) {
1129 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001130 struct ieee80211_channel_switch ch_switch = {
1131 .timestamp = timestamp,
1132 .block_tx = sw_elem->mode,
1133 .channel = new_ch,
1134 .count = sw_elem->count,
1135 };
1136
Johannes Berg5ce6e432010-05-11 16:20:57 +02001137 drv_channel_switch(sdata->local, &ch_switch);
1138 return;
1139 }
1140
1141 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001142 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001143 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001144 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001145 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +02001146 TU_TO_EXP_TIME(sw_elem->count *
1147 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001148}
1149
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001150static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1151 struct ieee80211_channel *channel,
1152 const u8 *country_ie, u8 country_ie_len,
1153 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001154{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001155 struct ieee80211_country_ie_triplet *triplet;
1156 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1157 int i, chan_pwr, chan_increment, new_ap_level;
1158 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001159
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001160 /* Invalid IE */
1161 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001162 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001163
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001164 triplet = (void *)(country_ie + 3);
1165 country_ie_len -= 3;
1166
1167 switch (channel->band) {
1168 default:
1169 WARN_ON_ONCE(1);
1170 /* fall through */
1171 case IEEE80211_BAND_2GHZ:
1172 case IEEE80211_BAND_60GHZ:
1173 chan_increment = 1;
1174 break;
1175 case IEEE80211_BAND_5GHZ:
1176 chan_increment = 4;
1177 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001178 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001179
1180 /* find channel */
1181 while (country_ie_len >= 3) {
1182 u8 first_channel = triplet->chans.first_channel;
1183
1184 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1185 goto next;
1186
1187 for (i = 0; i < triplet->chans.num_channels; i++) {
1188 if (first_channel + i * chan_increment == chan) {
1189 have_chan_pwr = true;
1190 chan_pwr = triplet->chans.max_power;
1191 break;
1192 }
1193 }
1194 if (have_chan_pwr)
1195 break;
1196
1197 next:
1198 triplet++;
1199 country_ie_len -= 3;
1200 }
1201
1202 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001203 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001204
1205 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1206
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001207 if (sdata->ap_power_level == new_ap_level)
1208 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001209
1210 sdata_info(sdata,
1211 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1212 new_ap_level, chan_pwr, *pwr_constr_elem,
1213 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001214 sdata->ap_power_level = new_ap_level;
1215 if (__ieee80211_recalc_txpower(sdata))
1216 return BSS_CHANGED_TXPOWER;
1217 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001218}
1219
Johannes Berg965beda2009-04-16 13:17:24 +02001220/* powersave */
1221static void ieee80211_enable_ps(struct ieee80211_local *local,
1222 struct ieee80211_sub_if_data *sdata)
1223{
1224 struct ieee80211_conf *conf = &local->hw.conf;
1225
Johannes Bergd5edaed2009-04-22 23:02:51 +02001226 /*
1227 * If we are scanning right now then the parameters will
1228 * take effect when scan finishes.
1229 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001230 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001231 return;
1232
Johannes Berg965beda2009-04-16 13:17:24 +02001233 if (conf->dynamic_ps_timeout > 0 &&
1234 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1235 mod_timer(&local->dynamic_ps_timer, jiffies +
1236 msecs_to_jiffies(conf->dynamic_ps_timeout));
1237 } else {
1238 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1239 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301240
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001241 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1242 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1243 return;
1244
1245 conf->flags |= IEEE80211_CONF_PS;
1246 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001247 }
1248}
1249
1250static void ieee80211_change_ps(struct ieee80211_local *local)
1251{
1252 struct ieee80211_conf *conf = &local->hw.conf;
1253
1254 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001255 ieee80211_enable_ps(local, local->ps_sdata);
1256 } else if (conf->flags & IEEE80211_CONF_PS) {
1257 conf->flags &= ~IEEE80211_CONF_PS;
1258 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1259 del_timer_sync(&local->dynamic_ps_timer);
1260 cancel_work_sync(&local->dynamic_ps_enable_work);
1261 }
1262}
1263
Jason Young808118c2011-03-10 16:43:19 -08001264static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1265{
1266 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1267 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001268 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001269
1270 if (!mgd->powersave)
1271 return false;
1272
Johannes Berg05cb9102011-10-28 11:59:47 +02001273 if (mgd->broken_ap)
1274 return false;
1275
Jason Young808118c2011-03-10 16:43:19 -08001276 if (!mgd->associated)
1277 return false;
1278
Jason Young808118c2011-03-10 16:43:19 -08001279 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1280 IEEE80211_STA_CONNECTION_POLL))
1281 return false;
1282
1283 rcu_read_lock();
1284 sta = sta_info_get(sdata, mgd->bssid);
1285 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001286 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001287 rcu_read_unlock();
1288
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001289 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001290}
1291
Johannes Berg965beda2009-04-16 13:17:24 +02001292/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001293void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001294{
1295 struct ieee80211_sub_if_data *sdata, *found = NULL;
1296 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001297 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001298
1299 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1300 local->ps_sdata = NULL;
1301 return;
1302 }
1303
1304 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001305 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001306 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301307 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1308 /* If an AP vif is found, then disable PS
1309 * by setting the count to zero thereby setting
1310 * ps_sdata to NULL.
1311 */
1312 count = 0;
1313 break;
1314 }
Johannes Berg965beda2009-04-16 13:17:24 +02001315 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1316 continue;
1317 found = sdata;
1318 count++;
1319 }
1320
Jason Young808118c2011-03-10 16:43:19 -08001321 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001322 s32 beaconint_us;
1323
1324 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001325 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001326
1327 beaconint_us = ieee80211_tu_to_usec(
1328 found->vif.bss_conf.beacon_int);
1329
Juuso Oikarinenff616382010-06-09 09:51:52 +03001330 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001331 if (timeout < 0) {
1332 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001333 * Go to full PSM if the user configures a very low
1334 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001335 * The 2000 second value is there for compatibility
1336 * until the PM_QOS_NETWORK_LATENCY is configured
1337 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001338 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001339 if (latency > (1900 * USEC_PER_MSEC) &&
1340 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001341 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001342 else
1343 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001344 }
Johannes Berg09b85562013-02-06 23:07:41 +01001345 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001346
Johannes Berg04fe2032009-04-22 18:44:37 +02001347 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001348 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001349 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001350 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001351 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001352
1353 /* If the TIM IE is invalid, pretend the value is 1 */
1354 if (!dtimper)
1355 dtimper = 1;
1356 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001357 maxslp = min_t(int, dtimper,
1358 latency / beaconint_us);
1359
Johannes Berg9ccebe62009-04-23 10:32:36 +02001360 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001361 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001362 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001363 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001364 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001365 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001366 }
Johannes Berg965beda2009-04-16 13:17:24 +02001367
1368 ieee80211_change_ps(local);
1369}
1370
Eliad Pellerab095872012-07-27 12:33:22 +03001371void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1372{
1373 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1374
1375 if (sdata->vif.bss_conf.ps != ps_allowed) {
1376 sdata->vif.bss_conf.ps = ps_allowed;
1377 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1378 }
1379}
1380
Johannes Berg965beda2009-04-16 13:17:24 +02001381void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1382{
1383 struct ieee80211_local *local =
1384 container_of(work, struct ieee80211_local,
1385 dynamic_ps_disable_work);
1386
1387 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1388 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1389 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1390 }
1391
1392 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001393 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001394 IEEE80211_QUEUE_STOP_REASON_PS);
1395}
1396
1397void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1398{
1399 struct ieee80211_local *local =
1400 container_of(work, struct ieee80211_local,
1401 dynamic_ps_enable_work);
1402 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001403 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301404 unsigned long flags;
1405 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001406
1407 /* can only happen when PS was just disabled anyway */
1408 if (!sdata)
1409 return;
1410
Christian Lamparter5e340692011-06-30 21:08:43 +02001411 ifmgd = &sdata->u.mgd;
1412
Johannes Berg965beda2009-04-16 13:17:24 +02001413 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1414 return;
1415
Johannes Berg09b85562013-02-06 23:07:41 +01001416 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001417 /* don't enter PS if TX frames are pending */
1418 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301419 mod_timer(&local->dynamic_ps_timer, jiffies +
1420 msecs_to_jiffies(
1421 local->hw.conf.dynamic_ps_timeout));
1422 return;
1423 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001424
1425 /*
1426 * transmission can be stopped by others which leads to
1427 * dynamic_ps_timer expiry. Postpone the ps timer if it
1428 * is not the actual idle state.
1429 */
1430 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1431 for (q = 0; q < local->hw.queues; q++) {
1432 if (local->queue_stop_reasons[q]) {
1433 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1434 flags);
1435 mod_timer(&local->dynamic_ps_timer, jiffies +
1436 msecs_to_jiffies(
1437 local->hw.conf.dynamic_ps_timeout));
1438 return;
1439 }
1440 }
1441 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301442 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301443
Vivek Natarajan375177b2010-02-09 14:50:28 +05301444 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301445 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001446 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301447 mod_timer(&local->dynamic_ps_timer, jiffies +
1448 msecs_to_jiffies(
1449 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001450 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301451 ieee80211_send_nullfunc(local, sdata, 1);
1452 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001453 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301454 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301455 }
1456
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001457 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1458 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301459 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1460 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1461 local->hw.conf.flags |= IEEE80211_CONF_PS;
1462 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1463 }
Johannes Berg965beda2009-04-16 13:17:24 +02001464}
1465
1466void ieee80211_dynamic_ps_timer(unsigned long data)
1467{
1468 struct ieee80211_local *local = (void *) data;
1469
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001470 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001471 return;
1472
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001473 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001474}
1475
Simon Wunderlich164eb022013-02-08 18:16:20 +01001476void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1477{
1478 struct delayed_work *delayed_work =
1479 container_of(work, struct delayed_work, work);
1480 struct ieee80211_sub_if_data *sdata =
1481 container_of(delayed_work, struct ieee80211_sub_if_data,
1482 dfs_cac_timer_work);
1483
1484 ieee80211_vif_release_channel(sdata);
1485
1486 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1487}
1488
Johannes Berg60f8b392008-09-08 17:44:22 +02001489/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001490static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001491 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001492 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001493{
Jiri Bencf0706e82007-05-05 11:45:53 -07001494 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001495 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001496 size_t left;
1497 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001498 const u8 *pos;
1499 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001500
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001501 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001502 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001503
Johannes Berg32c50572012-03-28 11:04:29 +02001504 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001505 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001506
1507 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001508 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001509
Jiri Bencf0706e82007-05-05 11:45:53 -07001510 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001511 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001512
1513 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001514 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001515
Jiri Bencf0706e82007-05-05 11:45:53 -07001516 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001517 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001518 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001519 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001520
1521 pos = wmm_param + 8;
1522 left = wmm_param_len - 8;
1523
1524 memset(&params, 0, sizeof(params));
1525
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001526 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001527 for (; left >= 4; left -= 4, pos += 4) {
1528 int aci = (pos[0] >> 5) & 0x03;
1529 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001530 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001531 int queue;
1532
1533 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001534 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001535 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001536 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001537 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001538 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1539 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001540 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001541 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001542 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001543 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001544 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001545 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1546 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001547 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001548 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001549 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001550 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001551 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001552 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1553 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001554 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001555 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001556 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001557 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001558 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001559 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001560 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1561 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001562 break;
1563 }
1564
1565 params.aifs = pos[0] & 0x0f;
1566 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1567 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001568 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001569 params.uapsd = uapsd;
1570
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001571 mlme_dbg(sdata,
1572 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1573 queue, aci, acm,
1574 params.aifs, params.cw_min, params.cw_max,
1575 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001576 sdata->tx_conf[queue] = params;
1577 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001578 sdata_err(sdata,
1579 "failed to set TX queue parameters for queue %d\n",
1580 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001581 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001582
1583 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001584 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001585 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001586}
1587
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001588static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1589{
1590 lockdep_assert_held(&sdata->local->mtx);
1591
1592 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1593 IEEE80211_STA_BEACON_POLL);
1594 ieee80211_run_deferred_scan(sdata->local);
1595}
1596
1597static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1598{
1599 mutex_lock(&sdata->local->mtx);
1600 __ieee80211_stop_poll(sdata);
1601 mutex_unlock(&sdata->local->mtx);
1602}
1603
Johannes Berg7a5158e2008-10-08 10:59:33 +02001604static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1605 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001606{
Johannes Bergbda39332008-10-11 01:51:51 +02001607 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001608 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001609 bool use_protection;
1610 bool use_short_preamble;
1611 bool use_short_slot;
1612
1613 if (erp_valid) {
1614 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1615 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1616 } else {
1617 use_protection = false;
1618 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1619 }
1620
1621 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001622 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001623 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001624
Johannes Berg471b3ef2007-12-28 14:32:58 +01001625 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001626 bss_conf->use_cts_prot = use_protection;
1627 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001628 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001629
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001630 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001631 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001632 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001633 }
Daniel Draked9430a32007-07-27 15:43:24 +02001634
Johannes Berg7a5158e2008-10-08 10:59:33 +02001635 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001636 bss_conf->use_short_slot = use_short_slot;
1637 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001638 }
1639
1640 return changed;
1641}
1642
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001643static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001644 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001645 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001646{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001647 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001648 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001649 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001650
Johannes Bergae5eb022008-10-14 16:58:37 +02001651 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001652 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001653 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001654
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001655 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001656 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001657
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001658 sdata->u.mgd.associated = cbss;
1659 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001660
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001661 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1662
Johannes Berg488dd7b2012-10-29 20:08:01 +01001663 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001664 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001665
Johannes Berg9caf0362012-11-29 01:25:20 +01001666 rcu_read_lock();
1667 ies = rcu_dereference(cbss->ies);
1668 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001669 int ret;
1670
1671 ret = cfg80211_get_p2p_attr(
1672 ies->data, ies->len,
1673 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001674 (u8 *) &bss_conf->p2p_noa_attr,
1675 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001676 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001677 sdata->u.mgd.p2p_noa_index =
1678 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001679 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001680 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001681 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001682 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001683 }
1684
Johannes Bergb291ba12009-07-10 15:29:03 +02001685 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001686 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001687
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001688 ieee80211_led_assoc(local, 1);
1689
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001690 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001691 /*
1692 * If the AP is buggy we may get here with no DTIM period
1693 * known, so assume it's 1 which is the only safe assumption
1694 * in that case, although if the TIM IE is broken powersave
1695 * probably just won't work at all.
1696 */
1697 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001698 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001699 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001700 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001701 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001702
Juuso Oikarinen68542962010-06-09 13:43:26 +03001703 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001704
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001705 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001706 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001707 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001708 bss_info_changed |= BSS_CHANGED_CQM;
1709
Juuso Oikarinen68542962010-06-09 13:43:26 +03001710 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001711 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001712 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001713
Johannes Bergae5eb022008-10-14 16:58:37 +02001714 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001715
Johannes Berg056508d2009-07-30 21:43:55 +02001716 mutex_lock(&local->iflist_mtx);
1717 ieee80211_recalc_ps(local, -1);
1718 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001719
Johannes Berg04ecd252012-09-11 14:34:12 +02001720 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001721 ieee80211_recalc_ps_vif(sdata);
1722
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001723 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001724}
1725
Jouni Malinene69e95d2010-03-29 23:29:31 -07001726static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001727 u16 stype, u16 reason, bool tx,
1728 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001729{
Johannes Berg46900292009-02-15 12:44:28 +01001730 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001731 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001732 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001733
Johannes Berg77fdaa12009-07-07 03:45:17 +02001734 ASSERT_MGD_MTX(ifmgd);
1735
Johannes Berg37ad3882012-02-24 13:50:54 +01001736 if (WARN_ON_ONCE(tx && !frame_buf))
1737 return;
1738
Johannes Bergb291ba12009-07-10 15:29:03 +02001739 if (WARN_ON(!ifmgd->associated))
1740 return;
1741
David Spinadel79543d82012-06-12 09:59:45 +03001742 ieee80211_stop_poll(sdata);
1743
Johannes Berg77fdaa12009-07-07 03:45:17 +02001744 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001745 netif_carrier_off(sdata->dev);
1746
Eliad Peller88bc40e2012-07-12 17:35:33 +03001747 /*
1748 * if we want to get out of ps before disassoc (why?) we have
1749 * to do it before sending disassoc, as otherwise the null-packet
1750 * won't be valid.
1751 */
1752 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1753 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1754 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1755 }
1756 local->ps_sdata = NULL;
1757
Eliad Pellerab095872012-07-27 12:33:22 +03001758 /* disable per-vif ps */
1759 ieee80211_recalc_ps_vif(sdata);
1760
Eliad Pellerf8239812012-06-27 14:18:22 +03001761 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1762 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001763 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001764
Johannes Berg37ad3882012-02-24 13:50:54 +01001765 /* deauthenticate/disassociate now */
1766 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001767 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1768 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001769
1770 /* flush out frame */
1771 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001772 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001773
Eliad Peller88a9e312012-06-01 11:14:03 +03001774 /* clear bssid only after building the needed mgmt frames */
1775 memset(ifmgd->bssid, 0, ETH_ALEN);
1776
Johannes Berg37ad3882012-02-24 13:50:54 +01001777 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001778 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001779
1780 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001781 changed |= ieee80211_reset_erp_info(sdata);
1782
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001783 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001784 changed |= BSS_CHANGED_ASSOC;
1785 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001786
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001787 ifmgd->p2p_noa_index = -1;
1788 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1789 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001790
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001791 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001792 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1793 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001794 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1795 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001796
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001797 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301798
Kalle Valo520eb822008-12-18 23:35:27 +02001799 del_timer_sync(&local->dynamic_ps_timer);
1800 cancel_work_sync(&local->dynamic_ps_enable_work);
1801
Juuso Oikarinen68542962010-06-09 13:43:26 +03001802 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001803 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001804 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001805
Johannes Berg3abead52012-03-02 15:56:59 +01001806 sdata->vif.bss_conf.qos = false;
1807 changed |= BSS_CHANGED_QOS;
1808
Johannes Berg0aaffa92010-05-05 15:28:27 +02001809 /* The BSSID (not really interesting) and HT changed */
1810 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001811 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001812
Johannes Berg3abead52012-03-02 15:56:59 +01001813 /* disassociated - set to defaults now */
1814 ieee80211_set_wmm_default(sdata, false);
1815
Johannes Bergb9dcf712010-08-27 12:35:54 +02001816 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1817 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1818 del_timer_sync(&sdata->u.mgd.timer);
1819 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001820
Johannes Berg826262c2012-12-10 16:38:14 +02001821 sdata->vif.bss_conf.dtim_period = 0;
1822
Johannes Berg028e8da02012-11-26 11:57:41 +01001823 ifmgd->flags = 0;
1824 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001825}
Jiri Bencf0706e82007-05-05 11:45:53 -07001826
Kalle Valo3cf335d52009-03-22 21:57:06 +02001827void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1828 struct ieee80211_hdr *hdr)
1829{
1830 /*
1831 * We can postpone the mgd.timer whenever receiving unicast frames
1832 * from AP because we know that the connection is working both ways
1833 * at that time. But multicast frames (and hence also beacons) must
1834 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001835 * data idle periods for sending the periodic probe request to the
1836 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02001837 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001838 if (is_multicast_ether_addr(hdr->addr1))
1839 return;
1840
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001841 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02001842}
Jiri Bencf0706e82007-05-05 11:45:53 -07001843
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001844static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1845{
1846 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001847 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001848
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001849 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001850 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001851 IEEE80211_STA_CONNECTION_POLL))) {
1852 mutex_unlock(&local->mtx);
1853 return;
1854 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001855
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001856 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001857
1858 mutex_lock(&local->iflist_mtx);
1859 ieee80211_recalc_ps(local, -1);
1860 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001861
1862 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001863 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001864
1865 /*
1866 * We've received a probe response, but are not sure whether
1867 * we have or will be receiving any beacons or data, so let's
1868 * schedule the timers again, just in case.
1869 */
1870 ieee80211_sta_reset_beacon_monitor(sdata);
1871
1872 mod_timer(&ifmgd->conn_mon_timer,
1873 round_jiffies_up(jiffies +
1874 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001875out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001876 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001877}
1878
1879void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001880 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001881{
Felix Fietkau75706d02010-12-02 21:01:07 +01001882 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001883 return;
1884
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001885 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1886 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001887 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001888 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001889 else
1890 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001891 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001892 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001893 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001894
1895 if (ack)
1896 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001897}
1898
Maxim Levitskya43abf22009-07-31 18:54:12 +03001899static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1900{
1901 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1902 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001903 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001904 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001905
1906 /*
1907 * Try sending broadcast probe requests for the last three
1908 * probe requests after the first ones failed since some
1909 * buggy APs only support broadcast probe requests.
1910 */
1911 if (ifmgd->probe_send_count >= unicast_limit)
1912 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001913
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001914 /*
1915 * When the hardware reports an accurate Tx ACK status, it's
1916 * better to send a nullfunc frame instead of a probe request,
1917 * as it will kick us off the AP quickly if we aren't associated
1918 * anymore. The timeout will be reset if the frame is ACKed by
1919 * the AP.
1920 */
Soumik Das992e68b2012-05-20 15:31:13 +05301921 ifmgd->probe_send_count++;
1922
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001923 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1924 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001925 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001926 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001927 int ssid_len;
1928
Johannes Berg9caf0362012-11-29 01:25:20 +01001929 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001930 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001931 if (WARN_ON_ONCE(ssid == NULL))
1932 ssid_len = 0;
1933 else
1934 ssid_len = ssid[1];
1935
1936 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001937 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001938 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001939 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001940 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001941
Ben Greear180205b2011-02-04 15:30:24 -08001942 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001943 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301944 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01001945 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001946}
1947
Johannes Bergb291ba12009-07-10 15:29:03 +02001948static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1949 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001950{
Kalle Valo04de8382009-03-22 21:57:35 +02001951 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001952 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001953
Johannes Berg9607e6b662009-12-23 13:15:31 +01001954 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001955 return;
1956
Johannes Berg77fdaa12009-07-07 03:45:17 +02001957 mutex_lock(&ifmgd->mtx);
1958
1959 if (!ifmgd->associated)
1960 goto out;
1961
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001962 mutex_lock(&sdata->local->mtx);
1963
1964 if (sdata->local->tmp_channel || sdata->local->scanning) {
1965 mutex_unlock(&sdata->local->mtx);
1966 goto out;
1967 }
1968
Ben Greeara13fbe52013-03-25 11:19:35 -07001969 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001970 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07001971 "detected beacon loss from AP (missed %d beacons) - probing\n",
1972 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001973
Ben Greeara13fbe52013-03-25 11:19:35 -07001974 ieee80211_cqm_rssi_notify(&sdata->vif,
1975 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
1976 GFP_KERNEL);
1977 }
Kalle Valo04de8382009-03-22 21:57:35 +02001978
Johannes Bergb291ba12009-07-10 15:29:03 +02001979 /*
1980 * The driver/our work has already reported this event or the
1981 * connection monitoring has kicked in and we have already sent
1982 * a probe request. Or maybe the AP died and the driver keeps
1983 * reporting until we disassociate...
1984 *
1985 * In either case we have to ignore the current call to this
1986 * function (except for setting the correct probe reason bit)
1987 * because otherwise we would reset the timer every time and
1988 * never check whether we received a probe response!
1989 */
1990 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1991 IEEE80211_STA_CONNECTION_POLL))
1992 already = true;
1993
1994 if (beacon)
1995 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1996 else
1997 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1998
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001999 mutex_unlock(&sdata->local->mtx);
2000
Johannes Bergb291ba12009-07-10 15:29:03 +02002001 if (already)
2002 goto out;
2003
Johannes Berg4e751842009-06-10 15:16:52 +02002004 mutex_lock(&sdata->local->iflist_mtx);
2005 ieee80211_recalc_ps(sdata->local, -1);
2006 mutex_unlock(&sdata->local->iflist_mtx);
2007
Maxim Levitskya43abf22009-07-31 18:54:12 +03002008 ifmgd->probe_send_count = 0;
2009 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002010 out:
2011 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002012}
2013
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002014struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2015 struct ieee80211_vif *vif)
2016{
2017 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2018 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002019 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002020 struct sk_buff *skb;
2021 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002022 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002023
2024 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2025 return NULL;
2026
2027 ASSERT_MGD_MTX(ifmgd);
2028
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002029 if (ifmgd->associated)
2030 cbss = ifmgd->associated;
2031 else if (ifmgd->auth_data)
2032 cbss = ifmgd->auth_data->bss;
2033 else if (ifmgd->assoc_data)
2034 cbss = ifmgd->assoc_data->bss;
2035 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002036 return NULL;
2037
Johannes Berg9caf0362012-11-29 01:25:20 +01002038 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002039 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002040 if (WARN_ON_ONCE(ssid == NULL))
2041 ssid_len = 0;
2042 else
2043 ssid_len = ssid[1];
2044
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002045 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002046 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002047 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002048 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002049 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002050
2051 return skb;
2052}
2053EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2054
Johannes Bergeef9e542013-01-29 13:09:34 +01002055static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002056{
2057 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002058 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002059
2060 mutex_lock(&ifmgd->mtx);
2061 if (!ifmgd->associated) {
2062 mutex_unlock(&ifmgd->mtx);
2063 return;
2064 }
2065
Johannes Berg37ad3882012-02-24 13:50:54 +01002066 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2067 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002068 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002069 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002070 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002071 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002072 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002073 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002074
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002075 /*
2076 * must be outside lock due to cfg80211,
2077 * but that's not a problem.
2078 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002079 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002080}
2081
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002082static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002083{
2084 struct ieee80211_sub_if_data *sdata =
2085 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002086 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002087 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2088 struct sta_info *sta;
2089
2090 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302091 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002092 sta = sta_info_get(sdata, ifmgd->bssid);
2093 if (sta)
2094 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302095 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002096 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002097
Johannes Berg682bd382013-01-29 13:13:50 +01002098 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002099 sdata_info(sdata, "Connection to AP %pM lost\n",
2100 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002101 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002102 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002103 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002104 }
2105}
2106
2107static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2108{
2109 struct ieee80211_sub_if_data *sdata =
2110 container_of(work, struct ieee80211_sub_if_data,
2111 u.mgd.csa_connection_drop_work);
2112
Johannes Bergeef9e542013-01-29 13:09:34 +01002113 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002114}
2115
Kalle Valo04de8382009-03-22 21:57:35 +02002116void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2117{
2118 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002119 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002120
Johannes Bergb5878a22010-04-07 16:48:40 +02002121 trace_api_beacon_loss(sdata);
2122
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002123 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002124 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002125 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002126}
2127EXPORT_SYMBOL(ieee80211_beacon_loss);
2128
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002129void ieee80211_connection_loss(struct ieee80211_vif *vif)
2130{
2131 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2132 struct ieee80211_hw *hw = &sdata->local->hw;
2133
Johannes Bergb5878a22010-04-07 16:48:40 +02002134 trace_api_connection_loss(sdata);
2135
Johannes Berg682bd382013-01-29 13:13:50 +01002136 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002137 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2138}
2139EXPORT_SYMBOL(ieee80211_connection_loss);
2140
2141
Johannes Berg66e67e42012-01-20 13:55:27 +01002142static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2143 bool assoc)
2144{
2145 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2146
2147 lockdep_assert_held(&sdata->u.mgd.mtx);
2148
Johannes Berg66e67e42012-01-20 13:55:27 +01002149 if (!assoc) {
2150 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2151
2152 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2153 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002154 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002155 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002156 }
2157
Johannes Berg5b112d32013-02-01 01:49:58 +01002158 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002159 kfree(auth_data);
2160 sdata->u.mgd.auth_data = NULL;
2161}
2162
2163static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2164 struct ieee80211_mgmt *mgmt, size_t len)
2165{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002166 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002167 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2168 u8 *pos;
2169 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002170 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002171
2172 pos = mgmt->u.auth.variable;
2173 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2174 if (!elems.challenge)
2175 return;
2176 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002177 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002178 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2179 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2180 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002181 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002182 elems.challenge - 2, elems.challenge_len + 2,
2183 auth_data->bss->bssid, auth_data->bss->bssid,
2184 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002185 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002186}
2187
2188static enum rx_mgmt_action __must_check
2189ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2190 struct ieee80211_mgmt *mgmt, size_t len)
2191{
2192 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2193 u8 bssid[ETH_ALEN];
2194 u16 auth_alg, auth_transaction, status_code;
2195 struct sta_info *sta;
2196
2197 lockdep_assert_held(&ifmgd->mtx);
2198
2199 if (len < 24 + 6)
2200 return RX_MGMT_NONE;
2201
2202 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2203 return RX_MGMT_NONE;
2204
2205 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2206
Joe Perchesb203ca32012-05-08 18:56:52 +00002207 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002208 return RX_MGMT_NONE;
2209
2210 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2211 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2212 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2213
2214 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002215 auth_transaction != ifmgd->auth_data->expected_transaction) {
2216 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2217 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2218 auth_transaction,
2219 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002220 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002221 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002222
2223 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002224 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2225 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002226 ieee80211_destroy_auth_data(sdata, false);
2227 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002228 }
2229
2230 switch (ifmgd->auth_data->algorithm) {
2231 case WLAN_AUTH_OPEN:
2232 case WLAN_AUTH_LEAP:
2233 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002234 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002235 break;
2236 case WLAN_AUTH_SHARED_KEY:
2237 if (ifmgd->auth_data->expected_transaction != 4) {
2238 ieee80211_auth_challenge(sdata, mgmt, len);
2239 /* need another frame */
2240 return RX_MGMT_NONE;
2241 }
2242 break;
2243 default:
2244 WARN_ONCE(1, "invalid auth alg %d",
2245 ifmgd->auth_data->algorithm);
2246 return RX_MGMT_NONE;
2247 }
2248
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002249 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002250 ifmgd->auth_data->done = true;
2251 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002252 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002253 run_again(ifmgd, ifmgd->auth_data->timeout);
2254
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002255 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2256 ifmgd->auth_data->expected_transaction != 2) {
2257 /*
2258 * Report auth frame to user space for processing since another
2259 * round of Authentication frames is still needed.
2260 */
2261 return RX_MGMT_CFG80211_RX_AUTH;
2262 }
2263
Johannes Berg66e67e42012-01-20 13:55:27 +01002264 /* move station state to auth */
2265 mutex_lock(&sdata->local->sta_mtx);
2266 sta = sta_info_get(sdata, bssid);
2267 if (!sta) {
2268 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2269 goto out_err;
2270 }
2271 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002272 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002273 goto out_err;
2274 }
2275 mutex_unlock(&sdata->local->sta_mtx);
2276
2277 return RX_MGMT_CFG80211_RX_AUTH;
2278 out_err:
2279 mutex_unlock(&sdata->local->sta_mtx);
2280 /* ignore frame -- wait for timeout */
2281 return RX_MGMT_NONE;
2282}
2283
2284
Johannes Berg77fdaa12009-07-07 03:45:17 +02002285static enum rx_mgmt_action __must_check
2286ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002287 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002288{
Johannes Berg46900292009-02-15 12:44:28 +01002289 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002290 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002291 u16 reason_code;
2292
Johannes Berg66e67e42012-01-20 13:55:27 +01002293 lockdep_assert_held(&ifmgd->mtx);
2294
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002295 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002296 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002297
Johannes Berg66e67e42012-01-20 13:55:27 +01002298 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002299 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002300 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002301
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002302 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002303
2304 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2305
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002306 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2307 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002308
Johannes Berg37ad3882012-02-24 13:50:54 +01002309 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2310
Johannes Berg77fdaa12009-07-07 03:45:17 +02002311 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002312}
2313
2314
Johannes Berg77fdaa12009-07-07 03:45:17 +02002315static enum rx_mgmt_action __must_check
2316ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2317 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002318{
Johannes Berg46900292009-02-15 12:44:28 +01002319 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002320 u16 reason_code;
2321
Johannes Berg66e67e42012-01-20 13:55:27 +01002322 lockdep_assert_held(&ifmgd->mtx);
2323
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002324 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002325 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002326
Johannes Berg66e67e42012-01-20 13:55:27 +01002327 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002328 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002329 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002330
2331 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2332
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002333 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2334 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002335
Johannes Berg37ad3882012-02-24 13:50:54 +01002336 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2337
Johannes Berg77fdaa12009-07-07 03:45:17 +02002338 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002339}
2340
Christian Lamparterc74d0842011-10-15 00:14:49 +02002341static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2342 u8 *supp_rates, unsigned int supp_rates_len,
2343 u32 *rates, u32 *basic_rates,
2344 bool *have_higher_than_11mbit,
2345 int *min_rate, int *min_rate_index)
2346{
2347 int i, j;
2348
2349 for (i = 0; i < supp_rates_len; i++) {
2350 int rate = (supp_rates[i] & 0x7f) * 5;
2351 bool is_basic = !!(supp_rates[i] & 0x80);
2352
2353 if (rate > 110)
2354 *have_higher_than_11mbit = true;
2355
2356 /*
2357 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2358 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2359 *
2360 * Note: Even through the membership selector and the basic
2361 * rate flag share the same bit, they are not exactly
2362 * the same.
2363 */
2364 if (!!(supp_rates[i] & 0x80) &&
2365 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2366 continue;
2367
2368 for (j = 0; j < sband->n_bitrates; j++) {
2369 if (sband->bitrates[j].bitrate == rate) {
2370 *rates |= BIT(j);
2371 if (is_basic)
2372 *basic_rates |= BIT(j);
2373 if (rate < *min_rate) {
2374 *min_rate = rate;
2375 *min_rate_index = j;
2376 }
2377 break;
2378 }
2379 }
2380 }
2381}
Jiri Bencf0706e82007-05-05 11:45:53 -07002382
Johannes Berg66e67e42012-01-20 13:55:27 +01002383static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2384 bool assoc)
2385{
2386 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2387
2388 lockdep_assert_held(&sdata->u.mgd.mtx);
2389
Johannes Berg66e67e42012-01-20 13:55:27 +01002390 if (!assoc) {
2391 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2392
2393 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2394 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002395 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002396 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002397 }
2398
2399 kfree(assoc_data);
2400 sdata->u.mgd.assoc_data = NULL;
2401}
2402
2403static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2404 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002405 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002406{
Johannes Berg46900292009-02-15 12:44:28 +01002407 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002408 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002409 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002410 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002411 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002412 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002413 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002414 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002415 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002416 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002417
Johannes Bergaf6b6372009-12-23 13:15:35 +01002418 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002419
Jiri Bencf0706e82007-05-05 11:45:53 -07002420 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002421 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002422
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002423 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002424 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2425 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002426 aid &= ~(BIT(15) | BIT(14));
2427
Johannes Berg05cb9102011-10-28 11:59:47 +02002428 ifmgd->broken_ap = false;
2429
2430 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002431 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2432 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002433 aid = 0;
2434 ifmgd->broken_ap = true;
2435 }
2436
Johannes Bergaf6b6372009-12-23 13:15:35 +01002437 pos = mgmt->u.assoc_resp.variable;
2438 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2439
Jiri Bencf0706e82007-05-05 11:45:53 -07002440 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002441 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002442 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002443 }
2444
Johannes Berg46900292009-02-15 12:44:28 +01002445 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002446
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002447 /*
2448 * We previously checked these in the beacon/probe response, so
2449 * they should be present here. This is just a safety net.
2450 */
2451 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2452 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2453 sdata_info(sdata,
2454 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2455 return false;
2456 }
2457
2458 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2459 (!elems.vht_cap_elem || !elems.vht_operation)) {
2460 sdata_info(sdata,
2461 "VHT AP is missing VHT capability/operation in AssocResp\n");
2462 return false;
2463 }
2464
Guy Eilam2a33bee2011-08-17 15:18:15 +03002465 mutex_lock(&sdata->local->sta_mtx);
2466 /*
2467 * station info was already allocated and inserted before
2468 * the association and should be available to us
2469 */
Johannes Berg7852e362012-01-20 13:55:24 +01002470 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002471 if (WARN_ON(!sta)) {
2472 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002473 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002474 }
2475
Johannes Berg55de9082012-07-26 17:24:39 +02002476 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002477
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002478 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002479 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002480 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002481 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002482
Mahesh Palivela818255e2012-10-10 11:33:04 +00002483 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2484 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002485 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002486
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002487 /*
2488 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2489 * in their association response, so ignore that data for our own
2490 * configuration. If it changed since the last beacon, we'll get the
2491 * next beacon and update then.
2492 */
Johannes Berg11289582013-02-07 17:36:12 +01002493
Johannes Bergbee7f5862013-02-07 22:24:55 +01002494 /*
2495 * If an operating mode notification IE is present, override the
2496 * NSS calculation (that would be done in rate_control_rate_init())
2497 * and use the # of streams from that element.
2498 */
2499 if (elems.opmode_notif &&
2500 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2501 u8 nss;
2502
2503 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2504 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2505 nss += 1;
2506 sta->sta.rx_nss = nss;
2507 }
2508
Johannes Berg4b7679a2008-09-18 18:14:18 +02002509 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002510
Johannes Berg46900292009-02-15 12:44:28 +01002511 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002512 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002513
Johannes Bergddf4ac52008-10-22 11:41:38 +02002514 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002515 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002516
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002517 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002518 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2519 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2520 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002521 sdata_info(sdata,
2522 "failed to move station %pM to desired state\n",
2523 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002524 WARN_ON(__sta_info_destroy(sta));
2525 mutex_unlock(&sdata->local->sta_mtx);
2526 return false;
2527 }
2528
Johannes Berg7852e362012-01-20 13:55:24 +01002529 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002530
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002531 /*
2532 * Always handle WMM once after association regardless
2533 * of the first value the AP uses. Setting -1 here has
2534 * that effect because the AP values is an unsigned
2535 * 4-bit value.
2536 */
2537 ifmgd->wmm_last_param_set = -1;
2538
Johannes Bergddf4ac52008-10-22 11:41:38 +02002539 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002540 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002541 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002542 else
Johannes Berg3abead52012-03-02 15:56:59 +01002543 ieee80211_set_wmm_default(sdata, false);
2544 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002545
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002546 /* set AID and assoc capability,
2547 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002548 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002549 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002550 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002551
Kalle Valo15b7b062009-03-22 21:57:14 +02002552 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002553 * If we're using 4-addr mode, let the AP know that we're
2554 * doing so, so that it can create the STA VLAN on its side
2555 */
2556 if (ifmgd->use_4addr)
2557 ieee80211_send_4addr_nullfunc(local, sdata);
2558
2559 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002560 * Start timer to probe the connection to the AP now.
2561 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002562 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002563 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002564 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002565
Johannes Bergaf6b6372009-12-23 13:15:35 +01002566 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002567}
2568
Johannes Berg66e67e42012-01-20 13:55:27 +01002569static enum rx_mgmt_action __must_check
2570ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2571 struct ieee80211_mgmt *mgmt, size_t len,
2572 struct cfg80211_bss **bss)
2573{
2574 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2575 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2576 u16 capab_info, status_code, aid;
2577 struct ieee802_11_elems elems;
2578 u8 *pos;
2579 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002580
Johannes Berg66e67e42012-01-20 13:55:27 +01002581 lockdep_assert_held(&ifmgd->mtx);
2582
2583 if (!assoc_data)
2584 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002585 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002586 return RX_MGMT_NONE;
2587
2588 /*
2589 * AssocResp and ReassocResp have identical structure, so process both
2590 * of them in this function.
2591 */
2592
2593 if (len < 24 + 6)
2594 return RX_MGMT_NONE;
2595
2596 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2597 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2598 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2599 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2600
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002601 sdata_info(sdata,
2602 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2603 reassoc ? "Rea" : "A", mgmt->sa,
2604 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002605
2606 pos = mgmt->u.assoc_resp.variable;
2607 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2608
2609 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002610 elems.timeout_int &&
2611 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002612 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002613 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002614 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002615 sdata_info(sdata,
2616 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2617 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002618 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002619 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002620 if (ms > IEEE80211_ASSOC_TIMEOUT)
2621 run_again(ifmgd, assoc_data->timeout);
2622 return RX_MGMT_NONE;
2623 }
2624
2625 *bss = assoc_data->bss;
2626
2627 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002628 sdata_info(sdata, "%pM denied association (code=%d)\n",
2629 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002630 ieee80211_destroy_assoc_data(sdata, false);
2631 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002632 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2633 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002634 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002635 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002636 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2637 }
John W. Linville635d9992012-07-09 16:34:34 -04002638 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002639
2640 /*
2641 * destroy assoc_data afterwards, as otherwise an idle
2642 * recalc after assoc_data is NULL but before associated
2643 * is set can cause the interface to go idle
2644 */
2645 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002646 }
2647
2648 return RX_MGMT_CFG80211_RX_ASSOC;
2649}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002650
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002651static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002652 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002653 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002654 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002655{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002656 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002657 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002658 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002659 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002660 bool need_ps = false;
2661
Johannes Berg826262c2012-12-10 16:38:14 +02002662 if ((sdata->u.mgd.associated &&
2663 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2664 (sdata->u.mgd.assoc_data &&
2665 ether_addr_equal(mgmt->bssid,
2666 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002667 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002668 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2669
2670 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002671 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002672 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2673 }
Johannes Berg56007a02010-01-26 14:19:52 +01002674 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002675
Johannes Berg1cd8e882013-03-27 14:30:12 +01002676 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002677 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2678 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002679 else
2680 freq = rx_status->freq;
2681
2682 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2683
2684 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2685 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002686
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002687 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002688 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002689 if (bss)
2690 ieee80211_rx_bss_put(local, bss);
2691
2692 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002693 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002694
Johannes Berg56007a02010-01-26 14:19:52 +01002695 if (need_ps) {
2696 mutex_lock(&local->iflist_mtx);
2697 ieee80211_recalc_ps(local, -1);
2698 mutex_unlock(&local->iflist_mtx);
2699 }
2700
Johannes Berg5bc14202012-08-01 16:13:02 +02002701 if (elems->ch_switch_ie &&
2702 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2703 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002704 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002705}
2706
2707
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002708static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002709 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002710{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002711 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002712 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002713 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2714 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002715 struct ieee802_11_elems elems;
2716
Kalle Valo15b7b062009-03-22 21:57:14 +02002717 ifmgd = &sdata->u.mgd;
2718
Johannes Berg77fdaa12009-07-07 03:45:17 +02002719 ASSERT_MGD_MTX(ifmgd);
2720
Joe Perchesb203ca32012-05-08 18:56:52 +00002721 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002722 return; /* ignore ProbeResp to foreign address */
2723
Ester Kummerae6a44e2008-06-27 18:54:48 +03002724 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2725 if (baselen > len)
2726 return;
2727
2728 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2729 &elems);
2730
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002731 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002732
Johannes Berg77fdaa12009-07-07 03:45:17 +02002733 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002734 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002735 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002736
2737 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002738 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002739 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002740 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002741 ifmgd->auth_data->tries = 0;
2742 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002743 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002744 run_again(ifmgd, ifmgd->auth_data->timeout);
2745 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002746}
2747
Johannes Bergd91f36d2009-04-16 13:17:26 +02002748/*
2749 * This is the canonical list of information elements we care about,
2750 * the filter code also gives us all changes to the Microsoft OUI
2751 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2752 *
2753 * We implement beacon filtering in software since that means we can
2754 * avoid processing the frame here and in cfg80211, and userspace
2755 * will not be able to tell whether the hardware supports it or not.
2756 *
2757 * XXX: This list needs to be dynamic -- userspace needs to be able to
2758 * add items it requires. It also needs to be able to tell us to
2759 * look out for other vendor IEs.
2760 */
2761static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002762 (1ULL << WLAN_EID_COUNTRY) |
2763 (1ULL << WLAN_EID_ERP_INFO) |
2764 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2765 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2766 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002767 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002768
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002769static enum rx_mgmt_action
2770ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2771 struct ieee80211_mgmt *mgmt, size_t len,
2772 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002773{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002774 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002775 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002776 size_t baselen;
2777 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002778 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002779 struct ieee80211_chanctx_conf *chanctx_conf;
2780 struct ieee80211_channel *chan;
Johannes Bergbee7f5862013-02-07 22:24:55 +01002781 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002782 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002783 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002784 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002785 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002786 u8 *bssid;
2787
Johannes Berg66e67e42012-01-20 13:55:27 +01002788 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002789
Ester Kummerae6a44e2008-06-27 18:54:48 +03002790 /* Process beacon from the current BSS */
2791 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2792 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002793 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002794
Johannes Berg55de9082012-07-26 17:24:39 +02002795 rcu_read_lock();
2796 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2797 if (!chanctx_conf) {
2798 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002799 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002800 }
2801
Johannes Berg4bf88532012-11-09 11:39:59 +01002802 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002803 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002804 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002805 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002806 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002807 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002808
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002809 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002810 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002811 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2812 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002813
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002814 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002815 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002816 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002817 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2818 sdata->vif.bss_conf.sync_tsf =
2819 le64_to_cpu(mgmt->u.beacon.timestamp);
2820 sdata->vif.bss_conf.sync_device_ts =
2821 rx_status->device_timestamp;
2822 if (elems.tim)
2823 sdata->vif.bss_conf.sync_dtim_count =
2824 elems.tim->dtim_count;
2825 else
2826 sdata->vif.bss_conf.sync_dtim_count = 0;
2827 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002828 /* continue assoc process */
2829 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002830 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002831 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002832 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002833 }
2834
2835 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002836 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002837 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002838 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002839
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002840 /* Track average RSSI from the Beacon frames of the current AP */
2841 ifmgd->last_beacon_signal = rx_status->signal;
2842 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2843 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002844 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002845 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002846 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002847 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002848 } else {
2849 ifmgd->ave_beacon_signal =
2850 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2851 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2852 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002853 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002854 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002855
2856 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2857 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2858 int sig = ifmgd->ave_beacon_signal;
2859 int last_sig = ifmgd->last_ave_beacon_signal;
2860
2861 /*
2862 * if signal crosses either of the boundaries, invoke callback
2863 * with appropriate parameters
2864 */
2865 if (sig > ifmgd->rssi_max_thold &&
2866 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2867 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002868 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002869 } else if (sig < ifmgd->rssi_min_thold &&
2870 (last_sig >= ifmgd->rssi_max_thold ||
2871 last_sig == 0)) {
2872 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002873 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002874 }
2875 }
2876
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002877 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002878 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002879 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002880 int sig = ifmgd->ave_beacon_signal / 16;
2881 int last_event = ifmgd->last_cqm_event_signal;
2882 int thold = bss_conf->cqm_rssi_thold;
2883 int hyst = bss_conf->cqm_rssi_hyst;
2884 if (sig < thold &&
2885 (last_event == 0 || sig < last_event - hyst)) {
2886 ifmgd->last_cqm_event_signal = sig;
2887 ieee80211_cqm_rssi_notify(
2888 &sdata->vif,
2889 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2890 GFP_KERNEL);
2891 } else if (sig > thold &&
2892 (last_event == 0 || sig > last_event + hyst)) {
2893 ifmgd->last_cqm_event_signal = sig;
2894 ieee80211_cqm_rssi_notify(
2895 &sdata->vif,
2896 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2897 GFP_KERNEL);
2898 }
2899 }
2900
Johannes Bergb291ba12009-07-10 15:29:03 +02002901 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002902 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002903 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002904 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002905 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002906 ieee80211_run_deferred_scan(local);
2907 mutex_unlock(&local->mtx);
2908
Johannes Berg4e751842009-06-10 15:16:52 +02002909 mutex_lock(&local->iflist_mtx);
2910 ieee80211_recalc_ps(local, -1);
2911 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002912 }
2913
Johannes Bergb291ba12009-07-10 15:29:03 +02002914 /*
2915 * Push the beacon loss detection into the future since
2916 * we are processing a beacon from the AP just now.
2917 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002918 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002919
Johannes Bergd91f36d2009-04-16 13:17:26 +02002920 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2921 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2922 len - baselen, &elems,
2923 care_about_ies, ncrc);
2924
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302925 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002926 bool directed_tim = ieee80211_check_tim(elems.tim,
2927 elems.tim_len,
2928 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002929 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002930 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002931 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2932 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2933 ieee80211_hw_config(local,
2934 IEEE80211_CONF_CHANGE_PS);
2935 }
Kalle Valo572e0012009-02-10 17:09:31 +02002936 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302937 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002938 local->pspolling = true;
2939
2940 /*
2941 * Here is assumed that the driver will be
2942 * able to send ps-poll frame and receive a
2943 * response even though power save mode is
2944 * enabled, but some drivers might require
2945 * to disable power save here. This needs
2946 * to be investigated.
2947 */
2948 ieee80211_send_pspoll(local, sdata);
2949 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002950 }
2951 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002952
Johannes Berg488dd7b2012-10-29 20:08:01 +01002953 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002954 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01002955 int ret;
2956
2957 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2958 len - baselen,
2959 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01002960 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002961 if (ret >= 2) {
2962 if (sdata->u.mgd.p2p_noa_index != noa.index) {
2963 /* valid noa_attr and index changed */
2964 sdata->u.mgd.p2p_noa_index = noa.index;
2965 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
2966 changed |= BSS_CHANGED_P2P_PS;
2967 /*
2968 * make sure we update all information, the CRC
2969 * mechanism doesn't look at P2P attributes.
2970 */
2971 ifmgd->beacon_crc_valid = false;
2972 }
2973 } else if (sdata->u.mgd.p2p_noa_index != -1) {
2974 /* noa_attr not found and we had valid noa_attr before */
2975 sdata->u.mgd.p2p_noa_index = -1;
2976 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002977 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002978 ifmgd->beacon_crc_valid = false;
2979 }
2980 }
2981
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002982 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002983 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03002984 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002985 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002986
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002987 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002988
2989 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2990 elems.wmm_param_len))
2991 changed |= BSS_CHANGED_QOS;
2992
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002993 /*
2994 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002995 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002996 */
2997 if (!bss_conf->dtim_period) {
2998 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2999 if (elems.tim)
3000 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3001 else
3002 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003003
3004 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3005 sdata->vif.bss_conf.sync_tsf =
3006 le64_to_cpu(mgmt->u.beacon.timestamp);
3007 sdata->vif.bss_conf.sync_device_ts =
3008 rx_status->device_timestamp;
3009 if (elems.tim)
3010 sdata->vif.bss_conf.sync_dtim_count =
3011 elems.tim->dtim_count;
3012 else
3013 sdata->vif.bss_conf.sync_dtim_count = 0;
3014 }
3015
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003016 changed |= BSS_CHANGED_DTIM_PERIOD;
3017 }
3018
Johannes Berg1946bed2013-03-27 14:31:53 +01003019 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003020 erp_valid = true;
3021 erp_value = elems.erp_info[0];
3022 } else {
3023 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003024 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003025 changed |= ieee80211_handle_bss_capability(sdata,
3026 le16_to_cpu(mgmt->u.beacon.capab_info),
3027 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003028
Johannes Berg11289582013-02-07 17:36:12 +01003029 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01003030 sta = sta_info_get(sdata, bssid);
3031
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003032 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3033 elems.vht_operation, bssid, &changed)) {
3034 mutex_unlock(&local->sta_mtx);
3035 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3036 WLAN_REASON_DEAUTH_LEAVING,
3037 true, deauth_buf);
3038 return RX_MGMT_CFG80211_TX_DEAUTH;
3039 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01003040
3041 if (sta && elems.opmode_notif)
3042 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3043 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003044 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003045
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003046 if (elems.country_elem && elems.pwr_constr_elem &&
3047 mgmt->u.probe_resp.capab_info &
3048 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003049 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3050 elems.country_elem,
3051 elems.country_elem_len,
3052 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003053
Johannes Berg471b3ef2007-12-28 14:32:58 +01003054 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003055
3056 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003057}
3058
Johannes Berg1fa57d02010-06-10 10:21:32 +02003059void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3060 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003061{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003062 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003063 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003064 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003065 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003066 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003067 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003068 u16 fc;
3069
Jiri Bencf0706e82007-05-05 11:45:53 -07003070 rx_status = (struct ieee80211_rx_status *) skb->cb;
3071 mgmt = (struct ieee80211_mgmt *) skb->data;
3072 fc = le16_to_cpu(mgmt->frame_control);
3073
Johannes Berg77fdaa12009-07-07 03:45:17 +02003074 mutex_lock(&ifmgd->mtx);
3075
Johannes Berg66e67e42012-01-20 13:55:27 +01003076 switch (fc & IEEE80211_FCTL_STYPE) {
3077 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003078 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3079 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003080 break;
3081 case IEEE80211_STYPE_PROBE_RESP:
3082 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3083 break;
3084 case IEEE80211_STYPE_AUTH:
3085 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3086 break;
3087 case IEEE80211_STYPE_DEAUTH:
3088 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3089 break;
3090 case IEEE80211_STYPE_DISASSOC:
3091 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3092 break;
3093 case IEEE80211_STYPE_ASSOC_RESP:
3094 case IEEE80211_STYPE_REASSOC_RESP:
3095 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3096 break;
3097 case IEEE80211_STYPE_ACTION:
3098 switch (mgmt->u.action.category) {
3099 case WLAN_CATEGORY_SPECTRUM_MGMT:
3100 ieee80211_sta_process_chanswitch(sdata,
3101 &mgmt->u.action.u.chan_switch.sw_elem,
3102 (void *)ifmgd->associated->priv,
3103 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003104 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003105 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003106 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003107 mutex_unlock(&ifmgd->mtx);
3108
Johannes Berg66e67e42012-01-20 13:55:27 +01003109 switch (rma) {
3110 case RX_MGMT_NONE:
3111 /* no action */
3112 break;
3113 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003114 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003115 break;
3116 case RX_MGMT_CFG80211_DISASSOC:
3117 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3118 break;
3119 case RX_MGMT_CFG80211_RX_AUTH:
3120 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3121 break;
3122 case RX_MGMT_CFG80211_RX_ASSOC:
3123 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3124 break;
3125 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3126 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3127 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003128 case RX_MGMT_CFG80211_TX_DEAUTH:
3129 cfg80211_send_deauth(sdata->dev, deauth_buf,
3130 sizeof(deauth_buf));
3131 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003132 default:
3133 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003134 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003135}
3136
Johannes Berg9c6bd792008-09-11 00:01:52 +02003137static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003138{
3139 struct ieee80211_sub_if_data *sdata =
3140 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003141
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003142 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003143}
3144
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003145static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003146 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003147{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003148 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003149 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003150
Johannes Berg37ad3882012-02-24 13:50:54 +01003151 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003152 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003153 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003154
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003155 /*
3156 * must be outside lock due to cfg80211,
3157 * but that's not a problem.
3158 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003159 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003160
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003161 mutex_lock(&ifmgd->mtx);
3162}
3163
Johannes Berg66e67e42012-01-20 13:55:27 +01003164static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3165{
3166 struct ieee80211_local *local = sdata->local;
3167 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3168 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003169 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003170
3171 lockdep_assert_held(&ifmgd->mtx);
3172
3173 if (WARN_ON_ONCE(!auth_data))
3174 return -EINVAL;
3175
Johannes Berg1672c0e32013-01-29 15:02:27 +01003176 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3177 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3178 IEEE80211_TX_INTFL_MLME_CONN_TX;
3179
Johannes Berg66e67e42012-01-20 13:55:27 +01003180 auth_data->tries++;
3181
3182 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003183 sdata_info(sdata, "authentication with %pM timed out\n",
3184 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003185
3186 /*
3187 * Most likely AP is not in the range so remove the
3188 * bss struct for that AP.
3189 */
3190 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3191
3192 return -ETIMEDOUT;
3193 }
3194
Johannes Berga1845fc2012-06-27 13:18:36 +02003195 drv_mgd_prepare_tx(local, sdata);
3196
Johannes Berg66e67e42012-01-20 13:55:27 +01003197 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003198 u16 trans = 1;
3199 u16 status = 0;
3200
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003201 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3202 auth_data->bss->bssid, auth_data->tries,
3203 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003204
3205 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003206
3207 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3208 trans = auth_data->sae_trans;
3209 status = auth_data->sae_status;
3210 auth_data->expected_transaction = trans;
3211 }
3212
3213 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3214 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003215 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003216 auth_data->bss->bssid, NULL, 0, 0,
3217 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003218 } else {
3219 const u8 *ssidie;
3220
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003221 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3222 auth_data->bss->bssid, auth_data->tries,
3223 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003224
Johannes Berg9caf0362012-11-29 01:25:20 +01003225 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003226 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003227 if (!ssidie) {
3228 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003229 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003230 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003231 /*
3232 * Direct probe is sent to broadcast address as some APs
3233 * will not answer to direct packet in unassociated state.
3234 */
3235 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01003236 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003237 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003238 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003239 }
3240
Johannes Berg1672c0e32013-01-29 15:02:27 +01003241 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3242 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003243 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003244 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003245 } else {
3246 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003247 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003248
3249 return 0;
3250}
3251
3252static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3253{
3254 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3255 struct ieee80211_local *local = sdata->local;
3256
3257 lockdep_assert_held(&sdata->u.mgd.mtx);
3258
Johannes Berg66e67e42012-01-20 13:55:27 +01003259 assoc_data->tries++;
3260 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003261 sdata_info(sdata, "association with %pM timed out\n",
3262 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003263
3264 /*
3265 * Most likely AP is not in the range so remove the
3266 * bss struct for that AP.
3267 */
3268 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3269
3270 return -ETIMEDOUT;
3271 }
3272
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003273 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3274 assoc_data->bss->bssid, assoc_data->tries,
3275 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003276 ieee80211_send_assoc(sdata);
3277
Johannes Berg1672c0e32013-01-29 15:02:27 +01003278 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3279 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003280 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003281 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003282 } else {
3283 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003284 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003285
3286 return 0;
3287}
3288
Johannes Berg1672c0e32013-01-29 15:02:27 +01003289void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3290 __le16 fc, bool acked)
3291{
3292 struct ieee80211_local *local = sdata->local;
3293
3294 sdata->u.mgd.status_fc = fc;
3295 sdata->u.mgd.status_acked = acked;
3296 sdata->u.mgd.status_received = true;
3297
3298 ieee80211_queue_work(&local->hw, &sdata->work);
3299}
3300
Johannes Berg1fa57d02010-06-10 10:21:32 +02003301void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003302{
Johannes Berg60f8b392008-09-08 17:44:22 +02003303 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003304 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003305
Johannes Berg77fdaa12009-07-07 03:45:17 +02003306 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003307
Johannes Berg1672c0e32013-01-29 15:02:27 +01003308 if (ifmgd->status_received) {
3309 __le16 fc = ifmgd->status_fc;
3310 bool status_acked = ifmgd->status_acked;
3311
3312 ifmgd->status_received = false;
3313 if (ifmgd->auth_data &&
3314 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3315 if (status_acked) {
3316 ifmgd->auth_data->timeout =
3317 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3318 run_again(ifmgd, ifmgd->auth_data->timeout);
3319 } else {
3320 ifmgd->auth_data->timeout = jiffies - 1;
3321 }
Johannes Berg89afe612013-02-13 15:39:57 +01003322 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003323 } else if (ifmgd->assoc_data &&
3324 (ieee80211_is_assoc_req(fc) ||
3325 ieee80211_is_reassoc_req(fc))) {
3326 if (status_acked) {
3327 ifmgd->assoc_data->timeout =
3328 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3329 run_again(ifmgd, ifmgd->assoc_data->timeout);
3330 } else {
3331 ifmgd->assoc_data->timeout = jiffies - 1;
3332 }
Johannes Berg89afe612013-02-13 15:39:57 +01003333 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003334 }
3335 }
3336
Johannes Berg89afe612013-02-13 15:39:57 +01003337 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003338 time_after(jiffies, ifmgd->auth_data->timeout)) {
3339 if (ifmgd->auth_data->done) {
3340 /*
3341 * ok ... we waited for assoc but userspace didn't,
3342 * so let's just kill the auth data
3343 */
3344 ieee80211_destroy_auth_data(sdata, false);
3345 } else if (ieee80211_probe_auth(sdata)) {
3346 u8 bssid[ETH_ALEN];
3347
3348 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3349
3350 ieee80211_destroy_auth_data(sdata, false);
3351
3352 mutex_unlock(&ifmgd->mtx);
3353 cfg80211_send_auth_timeout(sdata->dev, bssid);
3354 mutex_lock(&ifmgd->mtx);
3355 }
Johannes Berg89afe612013-02-13 15:39:57 +01003356 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003357 run_again(ifmgd, ifmgd->auth_data->timeout);
3358
Johannes Berg89afe612013-02-13 15:39:57 +01003359 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003360 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003361 if ((ifmgd->assoc_data->need_beacon &&
3362 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003363 ieee80211_do_assoc(sdata)) {
3364 u8 bssid[ETH_ALEN];
3365
3366 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3367
3368 ieee80211_destroy_assoc_data(sdata, false);
3369
3370 mutex_unlock(&ifmgd->mtx);
3371 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3372 mutex_lock(&ifmgd->mtx);
3373 }
Johannes Berg89afe612013-02-13 15:39:57 +01003374 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003375 run_again(ifmgd, ifmgd->assoc_data->timeout);
3376
Johannes Bergb291ba12009-07-10 15:29:03 +02003377 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3378 IEEE80211_STA_CONNECTION_POLL) &&
3379 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003380 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003381 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003382
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003383 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003384
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003385 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003386 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003387 else
Ben Greear180205b2011-02-04 15:30:24 -08003388 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003389
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003390 /* ACK received for nullfunc probing frame */
3391 if (!ifmgd->probe_send_count)
3392 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003393 else if (ifmgd->nullfunc_failed) {
3394 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003395 mlme_dbg(sdata,
3396 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3397 bssid, ifmgd->probe_send_count,
3398 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003399 ieee80211_mgd_probe_ap_send(sdata);
3400 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003401 mlme_dbg(sdata,
3402 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3403 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003404 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003405 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3406 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003407 }
3408 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003409 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003410 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003411 mlme_dbg(sdata,
3412 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3413 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003414 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003415 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003416 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003417 mlme_dbg(sdata,
3418 "No probe response from AP %pM after %dms, try %d/%i\n",
3419 bssid, probe_wait_ms,
3420 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003421 ieee80211_mgd_probe_ap_send(sdata);
3422 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003423 /*
3424 * We actually lost the connection ... or did we?
3425 * Let's make sure!
3426 */
Ben Greearb38afa82010-10-07 16:12:06 -07003427 wiphy_debug(local->hw.wiphy,
3428 "%s: No probe response from AP %pM"
3429 " after %dms, disconnecting.\n",
3430 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003431 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003432
Johannes Berg95acac62011-07-12 12:30:59 +02003433 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003434 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003435 }
3436 }
3437
Johannes Berg77fdaa12009-07-07 03:45:17 +02003438 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003439}
Johannes Berg0a51b272008-09-08 17:44:25 +02003440
Johannes Bergb291ba12009-07-10 15:29:03 +02003441static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3442{
3443 struct ieee80211_sub_if_data *sdata =
3444 (struct ieee80211_sub_if_data *) data;
3445 struct ieee80211_local *local = sdata->local;
3446
3447 if (local->quiescing)
3448 return;
3449
Johannes Berg682bd382013-01-29 13:13:50 +01003450 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003451 ieee80211_queue_work(&sdata->local->hw,
3452 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003453}
3454
3455static void ieee80211_sta_conn_mon_timer(unsigned long data)
3456{
3457 struct ieee80211_sub_if_data *sdata =
3458 (struct ieee80211_sub_if_data *) data;
3459 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3460 struct ieee80211_local *local = sdata->local;
3461
3462 if (local->quiescing)
3463 return;
3464
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003465 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003466}
3467
3468static void ieee80211_sta_monitor_work(struct work_struct *work)
3469{
3470 struct ieee80211_sub_if_data *sdata =
3471 container_of(work, struct ieee80211_sub_if_data,
3472 u.mgd.monitor_work);
3473
3474 ieee80211_mgd_probe_ap(sdata, false);
3475}
3476
Johannes Berga1678f82008-09-11 00:01:47 +02003477static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3478{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003479 u32 flags;
3480
Kalle Vaload935682009-04-19 08:47:19 +03003481 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003482 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003483
Johannes Bergb291ba12009-07-10 15:29:03 +02003484 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003485 flags = sdata->local->hw.flags;
3486 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3487 ieee80211_queue_work(&sdata->local->hw,
3488 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003489 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003490 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003491 }
Johannes Berga1678f82008-09-11 00:01:47 +02003492}
3493
Johannes Berg9c6bd792008-09-11 00:01:52 +02003494/* interface setup */
3495void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3496{
Johannes Berg46900292009-02-15 12:44:28 +01003497 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003498
Johannes Berg46900292009-02-15 12:44:28 +01003499 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003500 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003501 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003502 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3503 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003504 INIT_WORK(&ifmgd->csa_connection_drop_work,
3505 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003506 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003507 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003508 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003509 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3510 (unsigned long) sdata);
3511 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3512 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003513 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303514 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003515
Johannes Bergab1faea2009-07-01 21:41:17 +02003516 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303517 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003518 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3519 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003520 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003521
Johannes Berg77fdaa12009-07-07 03:45:17 +02003522 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003523
3524 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3525 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3526 else
3527 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003528}
3529
3530/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003531void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3532{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003533 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003534
3535 /* Restart STA timers */
3536 rcu_read_lock();
3537 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3538 ieee80211_restart_sta_timer(sdata);
3539 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003540}
Johannes Berg10f644a2009-04-16 13:17:25 +02003541
3542int ieee80211_max_network_latency(struct notifier_block *nb,
3543 unsigned long data, void *dummy)
3544{
3545 s32 latency_usec = (s32) data;
3546 struct ieee80211_local *local =
3547 container_of(nb, struct ieee80211_local,
3548 network_latency_notifier);
3549
3550 mutex_lock(&local->iflist_mtx);
3551 ieee80211_recalc_ps(local, latency_usec);
3552 mutex_unlock(&local->iflist_mtx);
3553
3554 return 0;
3555}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003556
Johannes Bergf2d9d272012-11-22 14:11:39 +01003557static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3558 struct cfg80211_bss *cbss)
3559{
3560 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3561 const u8 *ht_cap_ie, *vht_cap_ie;
3562 const struct ieee80211_ht_cap *ht_cap;
3563 const struct ieee80211_vht_cap *vht_cap;
3564 u8 chains = 1;
3565
3566 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3567 return chains;
3568
Johannes Berg9caf0362012-11-29 01:25:20 +01003569 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003570 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3571 ht_cap = (void *)(ht_cap_ie + 2);
3572 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3573 /*
3574 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3575 * "Tx Unequal Modulation Supported" fields.
3576 */
3577 }
3578
3579 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3580 return chains;
3581
Johannes Berg9caf0362012-11-29 01:25:20 +01003582 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003583 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3584 u8 nss;
3585 u16 tx_mcs_map;
3586
3587 vht_cap = (void *)(vht_cap_ie + 2);
3588 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3589 for (nss = 8; nss > 0; nss--) {
3590 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3591 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3592 break;
3593 }
3594 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3595 chains = max(chains, nss);
3596 }
3597
3598 return chains;
3599}
3600
Johannes Bergb17166a2012-07-27 11:41:27 +02003601static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3602 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003603{
3604 struct ieee80211_local *local = sdata->local;
3605 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003606 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003607 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003608 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003609 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003610 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003611
Johannes Berg24398e32012-03-28 10:58:36 +02003612 sband = local->hw.wiphy->bands[cbss->channel->band];
3613
Johannes Bergf2d9d272012-11-22 14:11:39 +01003614 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3615 IEEE80211_STA_DISABLE_80P80MHZ |
3616 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003617
Johannes Berg9caf0362012-11-29 01:25:20 +01003618 rcu_read_lock();
3619
Johannes Bergf2d9d272012-11-22 14:11:39 +01003620 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3621 sband->ht_cap.ht_supported) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01003622 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003623
Johannes Berg9caf0362012-11-29 01:25:20 +01003624 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003625 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3626 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6effa2013-02-07 23:33:32 +01003627
3628 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3629 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3630 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3631 ht_oper = NULL;
3632 }
Johannes Berg24398e32012-03-28 10:58:36 +02003633 }
3634
Johannes Bergf2d9d272012-11-22 14:11:39 +01003635 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3636 sband->vht_cap.vht_supported) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01003637 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003638
Johannes Berg9caf0362012-11-29 01:25:20 +01003639 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3640 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003641 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3642 vht_oper = (void *)(vht_oper_ie + 2);
3643 if (vht_oper && !ht_oper) {
3644 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003645 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003646 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003647 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3648 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003649 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01003650
3651 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3652 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3653 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3654 vht_oper = NULL;
3655 }
Johannes Berg24398e32012-03-28 10:58:36 +02003656 }
3657
Johannes Bergf2d9d272012-11-22 14:11:39 +01003658 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3659 cbss->channel,
3660 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003661 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003662
Johannes Bergf2d9d272012-11-22 14:11:39 +01003663 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3664 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003665
Johannes Berg9caf0362012-11-29 01:25:20 +01003666 rcu_read_unlock();
3667
Johannes Berg04ecd252012-09-11 14:34:12 +02003668 /* will change later if needed */
3669 sdata->smps_mode = IEEE80211_SMPS_OFF;
3670
Johannes Bergf2d9d272012-11-22 14:11:39 +01003671 /*
3672 * If this fails (possibly due to channel context sharing
3673 * on incompatible channels, e.g. 80+80 and 160 sharing the
3674 * same control channel) try to use a smaller bandwidth.
3675 */
3676 ret = ieee80211_vif_use_channel(sdata, &chandef,
3677 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003678 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003679 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003680 ret = ieee80211_vif_use_channel(sdata, &chandef,
3681 IEEE80211_CHANCTX_SHARED);
3682 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003683 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003684}
3685
3686static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3687 struct cfg80211_bss *cbss, bool assoc)
3688{
3689 struct ieee80211_local *local = sdata->local;
3690 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3691 struct ieee80211_bss *bss = (void *)cbss->priv;
3692 struct sta_info *new_sta = NULL;
3693 bool have_sta = false;
3694 int err;
3695
3696 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3697 return -EINVAL;
3698
3699 if (assoc) {
3700 rcu_read_lock();
3701 have_sta = sta_info_get(sdata, cbss->bssid);
3702 rcu_read_unlock();
3703 }
3704
3705 if (!have_sta) {
3706 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3707 if (!new_sta)
3708 return -ENOMEM;
3709 }
3710
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003711 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003712 u32 rates = 0, basic_rates = 0;
3713 bool have_higher_than_11mbit;
3714 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003715 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003716 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003717
3718 sband = local->hw.wiphy->bands[cbss->channel->band];
3719
3720 err = ieee80211_prep_channel(sdata, cbss);
3721 if (err) {
3722 sta_info_free(local, new_sta);
3723 return err;
3724 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003725
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003726 ieee80211_get_rates(sband, bss->supp_rates,
3727 bss->supp_rates_len,
3728 &rates, &basic_rates,
3729 &have_higher_than_11mbit,
3730 &min_rate, &min_rate_index);
3731
3732 /*
3733 * This used to be a workaround for basic rates missing
3734 * in the association response frame. Now that we no
3735 * longer use the basic rates from there, it probably
3736 * doesn't happen any more, but keep the workaround so
3737 * in case some *other* APs are buggy in different ways
3738 * we can connect -- with a warning.
3739 */
3740 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003741 sdata_info(sdata,
3742 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003743 basic_rates = BIT(min_rate_index);
3744 }
3745
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003746 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003747 sdata->vif.bss_conf.basic_rates = basic_rates;
3748
3749 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003750 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003751 have_higher_than_11mbit)
3752 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3753 else
3754 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3755
3756 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3757
Johannes Berg8c358bc2012-05-22 22:13:05 +02003758 /* set timing information */
3759 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003760 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003761 ies = rcu_dereference(cbss->beacon_ies);
3762 if (ies) {
3763 const u8 *tim_ie;
3764
3765 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3766 sdata->vif.bss_conf.sync_device_ts =
3767 bss->device_ts_beacon;
3768 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3769 ies->data, ies->len);
3770 if (tim_ie && tim_ie[1] >= 2)
3771 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3772 else
3773 sdata->vif.bss_conf.sync_dtim_count = 0;
3774 } else if (!(local->hw.flags &
3775 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3776 ies = rcu_dereference(cbss->proberesp_ies);
3777 /* must be non-NULL since beacon IEs were NULL */
3778 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3779 sdata->vif.bss_conf.sync_device_ts =
3780 bss->device_ts_presp;
3781 sdata->vif.bss_conf.sync_dtim_count = 0;
3782 } else {
3783 sdata->vif.bss_conf.sync_tsf = 0;
3784 sdata->vif.bss_conf.sync_device_ts = 0;
3785 sdata->vif.bss_conf.sync_dtim_count = 0;
3786 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003787 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003788
3789 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003790 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003791 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3792 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003793
3794 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003795 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003796
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003797 err = sta_info_insert(new_sta);
3798 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003799 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003800 sdata_info(sdata,
3801 "failed to insert STA entry for the AP (error %d)\n",
3802 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003803 return err;
3804 }
3805 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003806 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003807
3808 return 0;
3809}
3810
Johannes Berg77fdaa12009-07-07 03:45:17 +02003811/* config hooks */
3812int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3813 struct cfg80211_auth_request *req)
3814{
Johannes Berg66e67e42012-01-20 13:55:27 +01003815 struct ieee80211_local *local = sdata->local;
3816 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3817 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003818 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003819 int err;
3820
3821 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003822
3823 switch (req->auth_type) {
3824 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3825 auth_alg = WLAN_AUTH_OPEN;
3826 break;
3827 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003828 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003829 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003830 auth_alg = WLAN_AUTH_SHARED_KEY;
3831 break;
3832 case NL80211_AUTHTYPE_FT:
3833 auth_alg = WLAN_AUTH_FT;
3834 break;
3835 case NL80211_AUTHTYPE_NETWORK_EAP:
3836 auth_alg = WLAN_AUTH_LEAP;
3837 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003838 case NL80211_AUTHTYPE_SAE:
3839 auth_alg = WLAN_AUTH_SAE;
3840 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003841 default:
3842 return -EOPNOTSUPP;
3843 }
3844
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003845 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3846 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003847 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003848 return -ENOMEM;
3849
Johannes Berg66e67e42012-01-20 13:55:27 +01003850 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003851
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003852 if (req->sae_data_len >= 4) {
3853 __le16 *pos = (__le16 *) req->sae_data;
3854 auth_data->sae_trans = le16_to_cpu(pos[0]);
3855 auth_data->sae_status = le16_to_cpu(pos[1]);
3856 memcpy(auth_data->data, req->sae_data + 4,
3857 req->sae_data_len - 4);
3858 auth_data->data_len += req->sae_data_len - 4;
3859 }
3860
Johannes Berg77fdaa12009-07-07 03:45:17 +02003861 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003862 memcpy(&auth_data->data[auth_data->data_len],
3863 req->ie, req->ie_len);
3864 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003865 }
3866
Johannes Bergfffd0932009-07-08 14:22:54 +02003867 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003868 auth_data->key_len = req->key_len;
3869 auth_data->key_idx = req->key_idx;
3870 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003871 }
3872
Johannes Berg66e67e42012-01-20 13:55:27 +01003873 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003874
Johannes Berg66e67e42012-01-20 13:55:27 +01003875 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003876
Johannes Berg66e67e42012-01-20 13:55:27 +01003877 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003878
Johannes Berg66e67e42012-01-20 13:55:27 +01003879 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3880 ifmgd->assoc_data) {
3881 err = -EBUSY;
3882 goto err_free;
3883 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003884
Johannes Berg66e67e42012-01-20 13:55:27 +01003885 if (ifmgd->auth_data)
3886 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003887
Johannes Berg66e67e42012-01-20 13:55:27 +01003888 /* prep auth_data so we don't go into idle on disassoc */
3889 ifmgd->auth_data = auth_data;
3890
3891 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003892 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003893
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003894 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003895
Johannes Berga1cf7752012-03-08 15:02:07 +01003896 err = ieee80211_prep_connection(sdata, req->bss, false);
3897 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003898 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003899
Johannes Berg66e67e42012-01-20 13:55:27 +01003900 err = ieee80211_probe_auth(sdata);
3901 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003902 sta_info_destroy_addr(sdata, req->bss->bssid);
3903 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003904 }
3905
Johannes Berg66e67e42012-01-20 13:55:27 +01003906 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003907 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003908 err = 0;
3909 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003910
Johannes Berg66e67e42012-01-20 13:55:27 +01003911 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003912 memset(ifmgd->bssid, 0, ETH_ALEN);
3913 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003914 ifmgd->auth_data = NULL;
3915 err_free:
3916 kfree(auth_data);
3917 out_unlock:
3918 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003919
Johannes Berg66e67e42012-01-20 13:55:27 +01003920 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003921}
3922
Johannes Berg77fdaa12009-07-07 03:45:17 +02003923int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3924 struct cfg80211_assoc_request *req)
3925{
Johannes Berg66e67e42012-01-20 13:55:27 +01003926 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003927 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003928 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003929 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003930 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003931 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003932 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003933 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003934
Johannes Berg66e67e42012-01-20 13:55:27 +01003935 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3936 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003937 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003938
Johannes Berg9caf0362012-11-29 01:25:20 +01003939 rcu_read_lock();
3940 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3941 if (!ssidie) {
3942 rcu_read_unlock();
3943 kfree(assoc_data);
3944 return -EINVAL;
3945 }
3946 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3947 assoc_data->ssid_len = ssidie[1];
3948 rcu_read_unlock();
3949
Johannes Berg66e67e42012-01-20 13:55:27 +01003950 mutex_lock(&ifmgd->mtx);
3951
3952 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003953 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003954
3955 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3956 err = -EBUSY;
3957 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003958 }
3959
Johannes Berg66e67e42012-01-20 13:55:27 +01003960 if (ifmgd->assoc_data) {
3961 err = -EBUSY;
3962 goto err_free;
3963 }
3964
3965 if (ifmgd->auth_data) {
3966 bool match;
3967
3968 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003969 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003970 ieee80211_destroy_auth_data(sdata, match);
3971 }
3972
3973 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003974
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003975 ifmgd->beacon_crc_valid = false;
3976
Johannes Bergde5036a2012-03-08 15:02:03 +01003977 /*
3978 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3979 * We still associate in non-HT mode (11a/b/g) if any one of these
3980 * ciphers is configured as pairwise.
3981 * We can set this to true for non-11n hardware, that'll be checked
3982 * separately along with the peer capabilities.
3983 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003984 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003985 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3986 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003987 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003988 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003989 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003990 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003991 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003992 }
3993 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003994
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003995 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003996 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003997 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3998 }
Ben Greearef96a8422011-11-18 11:32:00 -08003999
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004000 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4001 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4002
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004003 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4004 sband = local->hw.wiphy->bands[req->bss->channel->band];
4005 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004006 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004007 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004008 if (!bss->wmm_used)
4009 netdev_info(sdata->dev,
4010 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004011 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004012
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004013 /* disable VHT if we don't support it or the AP doesn't use WMM */
4014 if (!sband->vht_cap.vht_supported ||
4015 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4016 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004017 if (!bss->wmm_used)
4018 netdev_info(sdata->dev,
4019 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004020 }
4021
Ben Greearef96a8422011-11-18 11:32:00 -08004022 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4023 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4024 sizeof(ifmgd->ht_capa_mask));
4025
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004026 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4027 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4028 sizeof(ifmgd->vht_capa_mask));
4029
Johannes Berg77fdaa12009-07-07 03:45:17 +02004030 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004031 memcpy(assoc_data->ie, req->ie, req->ie_len);
4032 assoc_data->ie_len = req->ie_len;
4033 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004034
Johannes Berg66e67e42012-01-20 13:55:27 +01004035 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004036
Johannes Bergaf6b6372009-12-23 13:15:35 +01004037 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4038 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004039 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004040 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004041 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004042 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004043 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004044
Johannes Berg66e67e42012-01-20 13:55:27 +01004045 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004046 assoc_data->wmm = bss->wmm_used &&
4047 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004048 assoc_data->supp_rates = bss->supp_rates;
4049 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004050
Johannes Berg9caf0362012-11-29 01:25:20 +01004051 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004052 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4053 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4054 assoc_data->ap_ht_param =
4055 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4056 else
Johannes Berga8243b72012-11-22 14:32:09 +01004057 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004058 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4059 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4060 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4061 sizeof(struct ieee80211_vht_cap));
4062 else
4063 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004064 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004065
Kalle Valoab133152010-01-12 10:42:31 +02004066 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004067 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4068 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004069 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004070 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4071 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004072 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004073 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4074 }
4075
Johannes Berg77fdaa12009-07-07 03:45:17 +02004076 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004077 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004078
4079 if (req->use_mfp) {
4080 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4081 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4082 } else {
4083 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4084 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4085 }
4086
4087 if (req->crypto.control_port)
4088 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4089 else
4090 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4091
Johannes Berga621fa42010-08-27 14:26:54 +03004092 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4093 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4094
Johannes Berg66e67e42012-01-20 13:55:27 +01004095 /* kick off associate process */
4096
4097 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004098 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004099
Johannes Berga1cf7752012-03-08 15:02:07 +01004100 err = ieee80211_prep_connection(sdata, req->bss, true);
4101 if (err)
4102 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004103
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004104 rcu_read_lock();
4105 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004106
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004107 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4108 !beacon_ies) {
4109 /*
4110 * Wait up to one beacon interval ...
4111 * should this be more if we miss one?
4112 */
4113 sdata_info(sdata, "waiting for beacon from %pM\n",
4114 ifmgd->bssid);
4115 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004116 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004117 assoc_data->need_beacon = true;
4118 } else if (beacon_ies) {
4119 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4120 beacon_ies->data,
4121 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004122 u8 dtim_count = 0;
4123
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004124 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4125 const struct ieee80211_tim_ie *tim;
4126 tim = (void *)(tim_ie + 2);
4127 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004128 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004129 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004130 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004131 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004132 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004133
4134 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4135 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4136 sdata->vif.bss_conf.sync_device_ts =
4137 bss->device_ts_beacon;
4138 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4139 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004140 } else {
4141 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004142 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004143 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004144 rcu_read_unlock();
4145
Johannes Berg66e67e42012-01-20 13:55:27 +01004146 run_again(ifmgd, assoc_data->timeout);
4147
Paul Stewartfcff4f12012-02-23 17:59:53 -08004148 if (bss->corrupt_data) {
4149 char *corrupt_type = "data";
4150 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4151 if (bss->corrupt_data &
4152 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4153 corrupt_type = "beacon and probe response";
4154 else
4155 corrupt_type = "beacon";
4156 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4157 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004158 sdata_info(sdata, "associating with AP with corrupt %s\n",
4159 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004160 }
4161
Johannes Berg66e67e42012-01-20 13:55:27 +01004162 err = 0;
4163 goto out;
4164 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004165 memset(ifmgd->bssid, 0, ETH_ALEN);
4166 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004167 ifmgd->assoc_data = NULL;
4168 err_free:
4169 kfree(assoc_data);
4170 out:
4171 mutex_unlock(&ifmgd->mtx);
4172
4173 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004174}
4175
4176int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004177 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004178{
4179 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004180 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004181 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004182 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004183
Johannes Berg77fdaa12009-07-07 03:45:17 +02004184 mutex_lock(&ifmgd->mtx);
4185
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004186 sdata_info(sdata,
4187 "deauthenticating from %pM by local choice (reason=%d)\n",
4188 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004189
Johannes Berg86552012012-10-29 09:46:31 +01004190 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004191 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004192 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4193 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004194 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004195 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004196 ieee80211_destroy_auth_data(sdata, false);
4197 mutex_unlock(&ifmgd->mtx);
4198
4199 sent_frame = tx;
4200 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004201 }
4202
Johannes Berg86552012012-10-29 09:46:31 +01004203 if (ifmgd->associated &&
4204 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4205 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4206 req->reason_code, tx, frame_buf);
4207 sent_frame = tx;
4208 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004209 mutex_unlock(&ifmgd->mtx);
4210
Johannes Berg86552012012-10-29 09:46:31 +01004211 out:
Johannes Berg86552012012-10-29 09:46:31 +01004212 if (sent_frame)
4213 __cfg80211_send_deauth(sdata->dev, frame_buf,
4214 IEEE80211_DEAUTH_FRAME_LEN);
4215
Johannes Berg77fdaa12009-07-07 03:45:17 +02004216 return 0;
4217}
4218
4219int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004220 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004221{
4222 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004223 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004224 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004225
Johannes Berg77fdaa12009-07-07 03:45:17 +02004226 mutex_lock(&ifmgd->mtx);
4227
Johannes Berg8d8b2612009-07-25 11:58:36 +02004228 /*
4229 * cfg80211 should catch this ... but it's racy since
4230 * we can receive a disassoc frame, process it, hand it
4231 * to cfg80211 while that's in a locked section already
4232 * trying to tell us that the user wants to disconnect.
4233 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004234 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004235 mutex_unlock(&ifmgd->mtx);
4236 return -ENOLINK;
4237 }
4238
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004239 sdata_info(sdata,
4240 "disassociating from %pM by local choice (reason=%d)\n",
4241 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004242
Jouni Malinene69e95d2010-03-29 23:29:31 -07004243 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004244 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4245 req->reason_code, !req->local_state_change,
4246 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004247 mutex_unlock(&ifmgd->mtx);
4248
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004249 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4250 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004251
Johannes Berg77fdaa12009-07-07 03:45:17 +02004252 return 0;
4253}
Jouni Malinen026331c2010-02-15 12:53:10 +02004254
Eliad Pellerafa762f2012-04-23 14:45:15 +03004255void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004256{
4257 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4258
Ben Greear49921852013-02-20 09:41:09 -08004259 /*
4260 * Make sure some work items will not run after this,
4261 * they will not do anything but might not have been
4262 * cancelled when disconnecting.
4263 */
4264 cancel_work_sync(&ifmgd->monitor_work);
4265 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4266 cancel_work_sync(&ifmgd->request_smps_work);
4267 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4268 cancel_work_sync(&ifmgd->chswitch_work);
4269
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004270 mutex_lock(&ifmgd->mtx);
4271 if (ifmgd->assoc_data)
4272 ieee80211_destroy_assoc_data(sdata, false);
4273 if (ifmgd->auth_data)
4274 ieee80211_destroy_auth_data(sdata, false);
4275 del_timer_sync(&ifmgd->timer);
4276 mutex_unlock(&ifmgd->mtx);
4277}
4278
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004279void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4280 enum nl80211_cqm_rssi_threshold_event rssi_event,
4281 gfp_t gfp)
4282{
4283 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4284
Johannes Bergb5878a22010-04-07 16:48:40 +02004285 trace_api_cqm_rssi_notify(sdata, rssi_event);
4286
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004287 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4288}
4289EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);