blob: ade3cd6c337d338c7e4fc705961c9fdfd2a938e3 [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 Berg1672c0e2013-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
Johannes Berg6565ec92013-02-08 14:52:32 +0100306 switch (vht_oper->chan_width) {
307 case IEEE80211_VHT_CHANWIDTH_USE_HT:
308 vht_chandef.width = chandef->width;
309 break;
310 case IEEE80211_VHT_CHANWIDTH_80MHZ:
311 vht_chandef.width = NL80211_CHAN_WIDTH_80;
312 break;
313 case IEEE80211_VHT_CHANWIDTH_160MHZ:
314 vht_chandef.width = NL80211_CHAN_WIDTH_160;
315 break;
316 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
317 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
Johannes Berg6553bf02013-03-28 10:26:17 +0100318 vht_chandef.center_freq2 =
319 ieee80211_channel_to_frequency(
320 vht_oper->center_freq_seg2_idx,
321 channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100322 break;
323 default:
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100324 if (verbose)
325 sdata_info(sdata,
326 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
327 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100328 ret = IEEE80211_STA_DISABLE_VHT;
329 goto out;
330 }
331
332 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100333 if (verbose)
334 sdata_info(sdata,
335 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100336 ret = IEEE80211_STA_DISABLE_VHT;
337 goto out;
338 }
339
340 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
341 ret = 0;
342 goto out;
343 }
344
345 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100346 if (verbose)
347 sdata_info(sdata,
348 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100349 ret = IEEE80211_STA_DISABLE_VHT;
350 goto out;
351 }
352
353 *chandef = vht_chandef;
354
355 ret = 0;
356
357out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100358 /* don't print the message below for VHT mismatch if VHT is disabled */
359 if (ret & IEEE80211_STA_DISABLE_VHT)
360 vht_chandef = *chandef;
361
Johannes Berg6565ec92013-02-08 14:52:32 +0100362 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
363 IEEE80211_CHAN_DISABLED)) {
364 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
365 ret = IEEE80211_STA_DISABLE_HT |
366 IEEE80211_STA_DISABLE_VHT;
367 goto out;
368 }
369
370 ret |= chandef_downgrade(chandef);
371 }
372
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100373 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100374 sdata_info(sdata,
375 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
376
377 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
378 return ret;
379}
380
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100381static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
382 struct sta_info *sta,
383 const struct ieee80211_ht_operation *ht_oper,
384 const struct ieee80211_vht_operation *vht_oper,
385 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200386{
387 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100388 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200389 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200390 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100391 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200392 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100393 u32 flags;
394 enum ieee80211_sta_rx_bandwidth new_sta_bw;
395 int ret;
396
397 /* if HT was/is disabled, don't track any bandwidth changes */
398 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
399 return 0;
400
401 /* don't check VHT if we associated as non-VHT station */
402 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
403 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200404
Johannes Berg11289582013-02-07 17:36:12 +0100405 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100406 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100407
Johannes Bergf2d93302013-02-08 13:15:55 +0100408 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200409 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200410
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100411 /* calculate new channel (type) based on HT/VHT operation IEs */
412 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
413 vht_oper, &chandef, false);
414
415 /*
416 * Downgrade the new channel if we associated with restricted
417 * capabilities. For example, if we associated as a 20 MHz STA
418 * to a 40 MHz AP (due to regulatory, capabilities or config
419 * reasons) then switching to a 40 MHz channel now won't do us
420 * any good -- we couldn't use it with the AP.
421 */
422 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
423 chandef.width == NL80211_CHAN_WIDTH_80P80)
424 flags |= chandef_downgrade(&chandef);
425 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
426 chandef.width == NL80211_CHAN_WIDTH_160)
427 flags |= chandef_downgrade(&chandef);
428 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
429 chandef.width > NL80211_CHAN_WIDTH_20)
430 flags |= chandef_downgrade(&chandef);
431
432 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
433 return 0;
434
435 sdata_info(sdata,
436 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
437 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
438 chandef.center_freq1, chandef.center_freq2);
439
440 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
441 IEEE80211_STA_DISABLE_VHT |
442 IEEE80211_STA_DISABLE_40MHZ |
443 IEEE80211_STA_DISABLE_80P80MHZ |
444 IEEE80211_STA_DISABLE_160MHZ)) ||
445 !cfg80211_chandef_valid(&chandef)) {
446 sdata_info(sdata,
447 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
448 ifmgd->bssid);
449 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200450 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200451
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100452 switch (chandef.width) {
453 case NL80211_CHAN_WIDTH_20_NOHT:
454 case NL80211_CHAN_WIDTH_20:
455 new_sta_bw = IEEE80211_STA_RX_BW_20;
456 break;
457 case NL80211_CHAN_WIDTH_40:
458 new_sta_bw = IEEE80211_STA_RX_BW_40;
459 break;
460 case NL80211_CHAN_WIDTH_80:
461 new_sta_bw = IEEE80211_STA_RX_BW_80;
462 break;
463 case NL80211_CHAN_WIDTH_80P80:
464 case NL80211_CHAN_WIDTH_160:
465 new_sta_bw = IEEE80211_STA_RX_BW_160;
466 break;
467 default:
468 return -EINVAL;
469 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200470
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100471 if (new_sta_bw > sta->cur_max_bandwidth)
472 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200473
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100474 if (new_sta_bw < sta->sta.bandwidth) {
475 sta->sta.bandwidth = new_sta_bw;
476 rate_control_rate_update(local, sband, sta,
477 IEEE80211_RC_BW_CHANGED);
478 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530479
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100480 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
481 if (ret) {
482 sdata_info(sdata,
483 "AP %pM changed bandwidth to incompatible one - disconnect\n",
484 ifmgd->bssid);
485 return ret;
486 }
487
488 if (new_sta_bw > sta->sta.bandwidth) {
489 sta->sta.bandwidth = new_sta_bw;
490 rate_control_rate_update(local, sband, sta,
491 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200492 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200493
Johannes Berg074d46d2012-03-15 19:45:16 +0100494 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200495
496 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100497 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
498 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200499 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200500 }
501
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100502 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200503}
504
Johannes Berg9c6bd792008-09-11 00:01:52 +0200505/* frame sending functions */
506
Johannes Berg66e67e42012-01-20 13:55:27 +0100507static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
508 struct ieee80211_supported_band *sband,
509 u32 *rates)
510{
511 int i, j, count;
512 *rates = 0;
513 count = 0;
514 for (i = 0; i < supp_rates_len; i++) {
515 int rate = (supp_rates[i] & 0x7F) * 5;
516
517 for (j = 0; j < sband->n_bitrates; j++)
518 if (sband->bitrates[j].bitrate == rate) {
519 *rates |= BIT(j);
520 count++;
521 break;
522 }
523 }
524
525 return count;
526}
527
528static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200529 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100530 struct ieee80211_supported_band *sband,
531 struct ieee80211_channel *channel,
532 enum ieee80211_smps_mode smps)
533{
Johannes Berg66e67e42012-01-20 13:55:27 +0100534 u8 *pos;
535 u32 flags = channel->flags;
536 u16 cap;
537 struct ieee80211_sta_ht_cap ht_cap;
538
539 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
540
Johannes Berg66e67e42012-01-20 13:55:27 +0100541 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
542 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
543
Johannes Berg66e67e42012-01-20 13:55:27 +0100544 /* determine capability flags */
545 cap = ht_cap.cap;
546
Johannes Berg9dde6422012-05-16 23:43:19 +0200547 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100548 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
549 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
550 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
551 cap &= ~IEEE80211_HT_CAP_SGI_40;
552 }
553 break;
554 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
555 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
556 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
557 cap &= ~IEEE80211_HT_CAP_SGI_40;
558 }
559 break;
560 }
561
Johannes Berg24398e32012-03-28 10:58:36 +0200562 /*
563 * If 40 MHz was disabled associate as though we weren't
564 * capable of 40 MHz -- some broken APs will never fall
565 * back to trying to transmit in 20 MHz.
566 */
567 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
568 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
569 cap &= ~IEEE80211_HT_CAP_SGI_40;
570 }
571
Johannes Berg66e67e42012-01-20 13:55:27 +0100572 /* set SM PS mode properly */
573 cap &= ~IEEE80211_HT_CAP_SM_PS;
574 switch (smps) {
575 case IEEE80211_SMPS_AUTOMATIC:
576 case IEEE80211_SMPS_NUM_MODES:
577 WARN_ON(1);
578 case IEEE80211_SMPS_OFF:
579 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
580 IEEE80211_HT_CAP_SM_PS_SHIFT;
581 break;
582 case IEEE80211_SMPS_STATIC:
583 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
584 IEEE80211_HT_CAP_SM_PS_SHIFT;
585 break;
586 case IEEE80211_SMPS_DYNAMIC:
587 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
588 IEEE80211_HT_CAP_SM_PS_SHIFT;
589 break;
590 }
591
592 /* reserve and fill IE */
593 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
594 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
595}
596
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000597static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
598 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100599 struct ieee80211_supported_band *sband,
600 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000601{
602 u8 *pos;
603 u32 cap;
604 struct ieee80211_sta_vht_cap vht_cap;
605
606 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
607
608 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100609 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000610
611 /* determine capability flags */
612 cap = vht_cap.cap;
613
Johannes Bergf2d9d272012-11-22 14:11:39 +0100614 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
615 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
616 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
617 }
618
619 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
620 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
621 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
622 }
623
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100624 /*
625 * Some APs apparently get confused if our capabilities are better
626 * than theirs, so restrict what we advertise in the assoc request.
627 */
628 if (!(ap_vht_cap->vht_cap_info &
629 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
630 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
631
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000632 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000633 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000634 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
635}
636
Johannes Berg66e67e42012-01-20 13:55:27 +0100637static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
638{
639 struct ieee80211_local *local = sdata->local;
640 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
641 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
642 struct sk_buff *skb;
643 struct ieee80211_mgmt *mgmt;
644 u8 *pos, qos_info;
645 size_t offset = 0, noffset;
646 int i, count, rates_len, supp_rates_len;
647 u16 capab;
648 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200649 struct ieee80211_chanctx_conf *chanctx_conf;
650 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100651 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100652
653 lockdep_assert_held(&ifmgd->mtx);
654
Johannes Berg55de9082012-07-26 17:24:39 +0200655 rcu_read_lock();
656 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
657 if (WARN_ON(!chanctx_conf)) {
658 rcu_read_unlock();
659 return;
660 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100661 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200662 rcu_read_unlock();
663 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100664
665 if (assoc_data->supp_rates_len) {
666 /*
667 * Get all rates supported by the device and the AP as
668 * some APs don't like getting a superset of their rates
669 * in the association request (e.g. D-Link DAP 1353 in
670 * b-only mode)...
671 */
672 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
673 assoc_data->supp_rates_len,
674 sband, &rates);
675 } else {
676 /*
677 * In case AP not provide any supported rates information
678 * before association, we send information element(s) with
679 * all rates that we support.
680 */
681 rates = ~0;
682 rates_len = sband->n_bitrates;
683 }
684
685 skb = alloc_skb(local->hw.extra_tx_headroom +
686 sizeof(*mgmt) + /* bit too much but doesn't matter */
687 2 + assoc_data->ssid_len + /* SSID */
688 4 + rates_len + /* (extended) rates */
689 4 + /* power capability */
690 2 + 2 * sband->n_channels + /* supported channels */
691 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000692 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100693 assoc_data->ie_len + /* extra IEs */
694 9, /* WMM */
695 GFP_KERNEL);
696 if (!skb)
697 return;
698
699 skb_reserve(skb, local->hw.extra_tx_headroom);
700
701 capab = WLAN_CAPABILITY_ESS;
702
703 if (sband->band == IEEE80211_BAND_2GHZ) {
704 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
705 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
706 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
707 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
708 }
709
710 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
711 capab |= WLAN_CAPABILITY_PRIVACY;
712
713 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
714 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
715 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
716
717 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
718 memset(mgmt, 0, 24);
719 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
720 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
721 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
722
723 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
724 skb_put(skb, 10);
725 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
726 IEEE80211_STYPE_REASSOC_REQ);
727 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
728 mgmt->u.reassoc_req.listen_interval =
729 cpu_to_le16(local->hw.conf.listen_interval);
730 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
731 ETH_ALEN);
732 } else {
733 skb_put(skb, 4);
734 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
735 IEEE80211_STYPE_ASSOC_REQ);
736 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
737 mgmt->u.assoc_req.listen_interval =
738 cpu_to_le16(local->hw.conf.listen_interval);
739 }
740
741 /* SSID */
742 pos = skb_put(skb, 2 + assoc_data->ssid_len);
743 *pos++ = WLAN_EID_SSID;
744 *pos++ = assoc_data->ssid_len;
745 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
746
747 /* add all rates which were marked to be used above */
748 supp_rates_len = rates_len;
749 if (supp_rates_len > 8)
750 supp_rates_len = 8;
751
752 pos = skb_put(skb, supp_rates_len + 2);
753 *pos++ = WLAN_EID_SUPP_RATES;
754 *pos++ = supp_rates_len;
755
756 count = 0;
757 for (i = 0; i < sband->n_bitrates; i++) {
758 if (BIT(i) & rates) {
759 int rate = sband->bitrates[i].bitrate;
760 *pos++ = (u8) (rate / 5);
761 if (++count == 8)
762 break;
763 }
764 }
765
766 if (rates_len > count) {
767 pos = skb_put(skb, rates_len - count + 2);
768 *pos++ = WLAN_EID_EXT_SUPP_RATES;
769 *pos++ = rates_len - count;
770
771 for (i++; i < sband->n_bitrates; i++) {
772 if (BIT(i) & rates) {
773 int rate = sband->bitrates[i].bitrate;
774 *pos++ = (u8) (rate / 5);
775 }
776 }
777 }
778
779 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
780 /* 1. power capabilities */
781 pos = skb_put(skb, 4);
782 *pos++ = WLAN_EID_PWR_CAPABILITY;
783 *pos++ = 2;
784 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200785 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100786
787 /* 2. supported channels */
788 /* TODO: get this in reg domain format */
789 pos = skb_put(skb, 2 * sband->n_channels + 2);
790 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
791 *pos++ = 2 * sband->n_channels;
792 for (i = 0; i < sband->n_channels; i++) {
793 *pos++ = ieee80211_frequency_to_channel(
794 sband->channels[i].center_freq);
795 *pos++ = 1; /* one channel in the subband*/
796 }
797 }
798
799 /* if present, add any custom IEs that go before HT */
800 if (assoc_data->ie_len && assoc_data->ie) {
801 static const u8 before_ht[] = {
802 WLAN_EID_SSID,
803 WLAN_EID_SUPP_RATES,
804 WLAN_EID_EXT_SUPP_RATES,
805 WLAN_EID_PWR_CAPABILITY,
806 WLAN_EID_SUPPORTED_CHANNELS,
807 WLAN_EID_RSN,
808 WLAN_EID_QOS_CAPA,
809 WLAN_EID_RRM_ENABLED_CAPABILITIES,
810 WLAN_EID_MOBILITY_DOMAIN,
811 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
812 };
813 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
814 before_ht, ARRAY_SIZE(before_ht),
815 offset);
816 pos = skb_put(skb, noffset - offset);
817 memcpy(pos, assoc_data->ie + offset, noffset - offset);
818 offset = noffset;
819 }
820
Johannes Bergf2d9d272012-11-22 14:11:39 +0100821 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
822 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
823 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
824
Johannes Berga8243b72012-11-22 14:32:09 +0100825 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200826 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200827 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100828
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000829 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100830 ieee80211_add_vht_ie(sdata, skb, sband,
831 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000832
Johannes Berg66e67e42012-01-20 13:55:27 +0100833 /* if present, add any custom non-vendor IEs that go after HT */
834 if (assoc_data->ie_len && assoc_data->ie) {
835 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
836 assoc_data->ie_len,
837 offset);
838 pos = skb_put(skb, noffset - offset);
839 memcpy(pos, assoc_data->ie + offset, noffset - offset);
840 offset = noffset;
841 }
842
Johannes Berg76f03032012-03-08 15:02:05 +0100843 if (assoc_data->wmm) {
844 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200845 qos_info = ifmgd->uapsd_queues;
846 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100847 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
848 } else {
849 qos_info = 0;
850 }
851
852 pos = skb_put(skb, 9);
853 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
854 *pos++ = 7; /* len */
855 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
856 *pos++ = 0x50;
857 *pos++ = 0xf2;
858 *pos++ = 2; /* WME */
859 *pos++ = 0; /* WME info */
860 *pos++ = 1; /* WME ver */
861 *pos++ = qos_info;
862 }
863
864 /* add any remaining custom (i.e. vendor specific here) IEs */
865 if (assoc_data->ie_len && assoc_data->ie) {
866 noffset = assoc_data->ie_len;
867 pos = skb_put(skb, noffset - offset);
868 memcpy(pos, assoc_data->ie + offset, noffset - offset);
869 }
870
Johannes Berga1845fc2012-06-27 13:18:36 +0200871 drv_mgd_prepare_tx(local, sdata);
872
Johannes Berg66e67e42012-01-20 13:55:27 +0100873 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e2013-01-29 15:02:27 +0100874 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
875 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
876 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100877 ieee80211_tx_skb(sdata, skb);
878}
879
Kalle Valo572e0012009-02-10 17:09:31 +0200880void ieee80211_send_pspoll(struct ieee80211_local *local,
881 struct ieee80211_sub_if_data *sdata)
882{
Kalle Valo572e0012009-02-10 17:09:31 +0200883 struct ieee80211_pspoll *pspoll;
884 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200885
Kalle Valod8cd1892010-01-05 20:16:26 +0200886 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
887 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200888 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200889
Kalle Valod8cd1892010-01-05 20:16:26 +0200890 pspoll = (struct ieee80211_pspoll *) skb->data;
891 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200892
Johannes Berg62ae67b2009-11-18 18:42:05 +0100893 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
894 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200895}
896
Johannes Berg965beda2009-04-16 13:17:24 +0200897void ieee80211_send_nullfunc(struct ieee80211_local *local,
898 struct ieee80211_sub_if_data *sdata,
899 int powersave)
900{
901 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200902 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530903 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200904
Kalle Valod8cd1892010-01-05 20:16:26 +0200905 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
906 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200907 return;
908
Kalle Valod8cd1892010-01-05 20:16:26 +0200909 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200910 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200911 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200912
Seth Forshee6c17b772013-02-11 11:21:07 -0600913 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
914 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530915 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
916 IEEE80211_STA_CONNECTION_POLL))
917 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
918
Johannes Berg62ae67b2009-11-18 18:42:05 +0100919 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200920}
921
Felix Fietkaud5242152010-01-08 18:06:26 +0100922static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
923 struct ieee80211_sub_if_data *sdata)
924{
925 struct sk_buff *skb;
926 struct ieee80211_hdr *nullfunc;
927 __le16 fc;
928
929 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
930 return;
931
932 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700933 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100934 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700935
Felix Fietkaud5242152010-01-08 18:06:26 +0100936 skb_reserve(skb, local->hw.extra_tx_headroom);
937
938 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
939 memset(nullfunc, 0, 30);
940 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
941 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
942 nullfunc->frame_control = fc;
943 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
944 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
945 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
946 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
947
948 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
949 ieee80211_tx_skb(sdata, skb);
950}
951
Johannes Bergcc32abd2009-05-15 11:52:31 +0200952/* spectrum management related things */
953static void ieee80211_chswitch_work(struct work_struct *work)
954{
955 struct ieee80211_sub_if_data *sdata =
956 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100957 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
959
Johannes Berg9607e6b2009-12-23 13:15:31 +0100960 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200961 return;
962
Johannes Berg77fdaa12009-07-07 03:45:17 +0200963 mutex_lock(&ifmgd->mtx);
964 if (!ifmgd->associated)
965 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200966
Karl Beldan675a0b02013-03-25 16:26:57 +0100967 /*
968 * FIXME: Here we are downgrading to NL80211_CHAN_WIDTH_20_NOHT
969 * and don't adjust our ht/vht settings
970 * This is wrong - we should behave according to the CSA params
971 */
972 local->_oper_chandef.chan = local->csa_channel;
973 local->_oper_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
974 local->_oper_chandef.center_freq1 =
975 local->_oper_chandef.chan->center_freq;
976 local->_oper_chandef.center_freq2 = 0;
977
978 if (!local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +0200979 /* call "hw_config" only if doing sw channel switch */
Karl Beldan675a0b02013-03-25 16:26:57 +0100980 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300981 } else {
982 /* update the device channel directly */
Karl Beldan675a0b02013-03-25 16:26:57 +0100983 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200984 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200985
Johannes Berg77fdaa12009-07-07 03:45:17 +0200986 /* XXX: shouldn't really modify cfg80211-owned data! */
Karl Beldan675a0b02013-03-25 16:26:57 +0100987 ifmgd->associated->channel = local->_oper_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200988
Johannes Berg57eebdf2012-08-01 15:50:46 +0200989 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +0100990 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100991 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200992 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200993 out:
994 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
995 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200996}
997
Johannes Berg5ce6e432010-05-11 16:20:57 +0200998void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
999{
Johannes Berg55de9082012-07-26 17:24:39 +02001000 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1001 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001002
1003 trace_api_chswitch_done(sdata, success);
1004 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001005 sdata_info(sdata,
1006 "driver channel switch failed, disconnecting\n");
1007 ieee80211_queue_work(&sdata->local->hw,
1008 &ifmgd->csa_connection_drop_work);
1009 } else {
1010 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001011 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001012}
1013EXPORT_SYMBOL(ieee80211_chswitch_done);
1014
Johannes Bergcc32abd2009-05-15 11:52:31 +02001015static void ieee80211_chswitch_timer(unsigned long data)
1016{
1017 struct ieee80211_sub_if_data *sdata =
1018 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001019
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001020 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001021}
1022
Johannes Berg37799e52013-03-26 14:02:26 +01001023static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001024ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01001025 u64 timestamp, struct ieee802_11_elems *elems)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001026{
Johannes Bergcc32abd2009-05-15 11:52:31 +02001027 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001028 struct cfg80211_bss *cbss = ifmgd->associated;
1029 struct ieee80211_bss *bss;
1030 struct ieee80211_channel *new_ch;
1031 int new_freq;
Johannes Berg55de9082012-07-26 17:24:39 +02001032 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001033
Johannes Berg77fdaa12009-07-07 03:45:17 +02001034 ASSERT_MGD_MTX(ifmgd);
1035
Johannes Berg37799e52013-03-26 14:02:26 +01001036 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001037 return;
1038
Helmut Schaafbe9c422009-07-23 12:14:04 +02001039 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001040 return;
1041
Johannes Berg37799e52013-03-26 14:02:26 +01001042 /* disregard subsequent announcements if we are already processing */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001043 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1044 return;
1045
Johannes Berg37799e52013-03-26 14:02:26 +01001046 if (!elems->ch_switch_ie)
1047 return;
1048
1049 bss = (void *)cbss->priv;
1050
1051 new_freq = ieee80211_channel_to_frequency(
1052 elems->ch_switch_ie->new_ch_num,
1053 cbss->channel->band);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001054 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001055 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1056 sdata_info(sdata,
1057 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1058 ifmgd->associated->bssid, new_freq);
1059 ieee80211_queue_work(&sdata->local->hw,
1060 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001061 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001062 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001063
Johannes Berg57eebdf2012-08-01 15:50:46 +02001064 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1065
Johannes Berg55de9082012-07-26 17:24:39 +02001066 if (sdata->local->use_chanctx) {
1067 sdata_info(sdata,
1068 "not handling channel switch with channel contexts\n");
1069 ieee80211_queue_work(&sdata->local->hw,
1070 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001071 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001072 }
1073
1074 mutex_lock(&sdata->local->chanctx_mtx);
1075 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1076 mutex_unlock(&sdata->local->chanctx_mtx);
1077 return;
1078 }
1079 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1080 struct ieee80211_chanctx, conf);
1081 if (chanctx->refcount > 1) {
1082 sdata_info(sdata,
1083 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1084 ieee80211_queue_work(&sdata->local->hw,
1085 &ifmgd->csa_connection_drop_work);
1086 mutex_unlock(&sdata->local->chanctx_mtx);
1087 return;
1088 }
1089 mutex_unlock(&sdata->local->chanctx_mtx);
1090
1091 sdata->local->csa_channel = new_ch;
1092
Johannes Berg37799e52013-03-26 14:02:26 +01001093 if (elems->ch_switch_ie->mode)
Johannes Berg57eebdf2012-08-01 15:50:46 +02001094 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001095 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001096 IEEE80211_QUEUE_STOP_REASON_CSA);
1097
Johannes Berg5ce6e432010-05-11 16:20:57 +02001098 if (sdata->local->ops->channel_switch) {
1099 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001100 struct ieee80211_channel_switch ch_switch = {
1101 .timestamp = timestamp,
Johannes Berg37799e52013-03-26 14:02:26 +01001102 .block_tx = elems->ch_switch_ie->mode,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001103 .channel = new_ch,
Johannes Berg37799e52013-03-26 14:02:26 +01001104 .count = elems->ch_switch_ie->count,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001105 };
1106
Johannes Berg5ce6e432010-05-11 16:20:57 +02001107 drv_channel_switch(sdata->local, &ch_switch);
1108 return;
1109 }
1110
1111 /* channel switch handled in software */
Johannes Berg37799e52013-03-26 14:02:26 +01001112 if (elems->ch_switch_ie->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001113 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001114 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001115 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg37799e52013-03-26 14:02:26 +01001116 TU_TO_EXP_TIME(elems->ch_switch_ie->count *
Johannes Berg30490002012-08-01 15:53:45 +02001117 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001118}
1119
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001120static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1121 struct ieee80211_channel *channel,
1122 const u8 *country_ie, u8 country_ie_len,
1123 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001124{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001125 struct ieee80211_country_ie_triplet *triplet;
1126 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1127 int i, chan_pwr, chan_increment, new_ap_level;
1128 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001129
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001130 /* Invalid IE */
1131 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001132 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001133
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001134 triplet = (void *)(country_ie + 3);
1135 country_ie_len -= 3;
1136
1137 switch (channel->band) {
1138 default:
1139 WARN_ON_ONCE(1);
1140 /* fall through */
1141 case IEEE80211_BAND_2GHZ:
1142 case IEEE80211_BAND_60GHZ:
1143 chan_increment = 1;
1144 break;
1145 case IEEE80211_BAND_5GHZ:
1146 chan_increment = 4;
1147 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001148 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001149
1150 /* find channel */
1151 while (country_ie_len >= 3) {
1152 u8 first_channel = triplet->chans.first_channel;
1153
1154 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1155 goto next;
1156
1157 for (i = 0; i < triplet->chans.num_channels; i++) {
1158 if (first_channel + i * chan_increment == chan) {
1159 have_chan_pwr = true;
1160 chan_pwr = triplet->chans.max_power;
1161 break;
1162 }
1163 }
1164 if (have_chan_pwr)
1165 break;
1166
1167 next:
1168 triplet++;
1169 country_ie_len -= 3;
1170 }
1171
1172 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001173 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001174
1175 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1176
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001177 if (sdata->ap_power_level == new_ap_level)
1178 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001179
1180 sdata_info(sdata,
1181 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1182 new_ap_level, chan_pwr, *pwr_constr_elem,
1183 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001184 sdata->ap_power_level = new_ap_level;
1185 if (__ieee80211_recalc_txpower(sdata))
1186 return BSS_CHANGED_TXPOWER;
1187 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001188}
1189
Johannes Berg965beda2009-04-16 13:17:24 +02001190/* powersave */
1191static void ieee80211_enable_ps(struct ieee80211_local *local,
1192 struct ieee80211_sub_if_data *sdata)
1193{
1194 struct ieee80211_conf *conf = &local->hw.conf;
1195
Johannes Bergd5edaed2009-04-22 23:02:51 +02001196 /*
1197 * If we are scanning right now then the parameters will
1198 * take effect when scan finishes.
1199 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001200 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001201 return;
1202
Johannes Berg965beda2009-04-16 13:17:24 +02001203 if (conf->dynamic_ps_timeout > 0 &&
1204 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1205 mod_timer(&local->dynamic_ps_timer, jiffies +
1206 msecs_to_jiffies(conf->dynamic_ps_timeout));
1207 } else {
1208 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1209 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301210
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001211 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1212 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1213 return;
1214
1215 conf->flags |= IEEE80211_CONF_PS;
1216 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001217 }
1218}
1219
1220static void ieee80211_change_ps(struct ieee80211_local *local)
1221{
1222 struct ieee80211_conf *conf = &local->hw.conf;
1223
1224 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001225 ieee80211_enable_ps(local, local->ps_sdata);
1226 } else if (conf->flags & IEEE80211_CONF_PS) {
1227 conf->flags &= ~IEEE80211_CONF_PS;
1228 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1229 del_timer_sync(&local->dynamic_ps_timer);
1230 cancel_work_sync(&local->dynamic_ps_enable_work);
1231 }
1232}
1233
Jason Young808118c2011-03-10 16:43:19 -08001234static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1235{
1236 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1237 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001238 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001239
1240 if (!mgd->powersave)
1241 return false;
1242
Johannes Berg05cb9102011-10-28 11:59:47 +02001243 if (mgd->broken_ap)
1244 return false;
1245
Jason Young808118c2011-03-10 16:43:19 -08001246 if (!mgd->associated)
1247 return false;
1248
Jason Young808118c2011-03-10 16:43:19 -08001249 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1250 IEEE80211_STA_CONNECTION_POLL))
1251 return false;
1252
1253 rcu_read_lock();
1254 sta = sta_info_get(sdata, mgd->bssid);
1255 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001256 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001257 rcu_read_unlock();
1258
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001259 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001260}
1261
Johannes Berg965beda2009-04-16 13:17:24 +02001262/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001263void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001264{
1265 struct ieee80211_sub_if_data *sdata, *found = NULL;
1266 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001267 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001268
1269 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1270 local->ps_sdata = NULL;
1271 return;
1272 }
1273
1274 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001275 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001276 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301277 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1278 /* If an AP vif is found, then disable PS
1279 * by setting the count to zero thereby setting
1280 * ps_sdata to NULL.
1281 */
1282 count = 0;
1283 break;
1284 }
Johannes Berg965beda2009-04-16 13:17:24 +02001285 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1286 continue;
1287 found = sdata;
1288 count++;
1289 }
1290
Jason Young808118c2011-03-10 16:43:19 -08001291 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001292 s32 beaconint_us;
1293
1294 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001295 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001296
1297 beaconint_us = ieee80211_tu_to_usec(
1298 found->vif.bss_conf.beacon_int);
1299
Juuso Oikarinenff616382010-06-09 09:51:52 +03001300 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001301 if (timeout < 0) {
1302 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001303 * Go to full PSM if the user configures a very low
1304 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001305 * The 2000 second value is there for compatibility
1306 * until the PM_QOS_NETWORK_LATENCY is configured
1307 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001308 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001309 if (latency > (1900 * USEC_PER_MSEC) &&
1310 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001311 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001312 else
1313 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001314 }
Johannes Berg09b85562013-02-06 23:07:41 +01001315 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001316
Johannes Berg04fe2032009-04-22 18:44:37 +02001317 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001318 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001319 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001320 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001321 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001322
1323 /* If the TIM IE is invalid, pretend the value is 1 */
1324 if (!dtimper)
1325 dtimper = 1;
1326 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001327 maxslp = min_t(int, dtimper,
1328 latency / beaconint_us);
1329
Johannes Berg9ccebe62009-04-23 10:32:36 +02001330 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001331 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001332 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001333 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001334 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001335 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001336 }
Johannes Berg965beda2009-04-16 13:17:24 +02001337
1338 ieee80211_change_ps(local);
1339}
1340
Eliad Pellerab095872012-07-27 12:33:22 +03001341void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1342{
1343 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1344
1345 if (sdata->vif.bss_conf.ps != ps_allowed) {
1346 sdata->vif.bss_conf.ps = ps_allowed;
1347 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1348 }
1349}
1350
Johannes Berg965beda2009-04-16 13:17:24 +02001351void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1352{
1353 struct ieee80211_local *local =
1354 container_of(work, struct ieee80211_local,
1355 dynamic_ps_disable_work);
1356
1357 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1358 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1359 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1360 }
1361
1362 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001363 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001364 IEEE80211_QUEUE_STOP_REASON_PS);
1365}
1366
1367void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1368{
1369 struct ieee80211_local *local =
1370 container_of(work, struct ieee80211_local,
1371 dynamic_ps_enable_work);
1372 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001373 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301374 unsigned long flags;
1375 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001376
1377 /* can only happen when PS was just disabled anyway */
1378 if (!sdata)
1379 return;
1380
Christian Lamparter5e340692011-06-30 21:08:43 +02001381 ifmgd = &sdata->u.mgd;
1382
Johannes Berg965beda2009-04-16 13:17:24 +02001383 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1384 return;
1385
Johannes Berg09b85562013-02-06 23:07:41 +01001386 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001387 /* don't enter PS if TX frames are pending */
1388 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301389 mod_timer(&local->dynamic_ps_timer, jiffies +
1390 msecs_to_jiffies(
1391 local->hw.conf.dynamic_ps_timeout));
1392 return;
1393 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001394
1395 /*
1396 * transmission can be stopped by others which leads to
1397 * dynamic_ps_timer expiry. Postpone the ps timer if it
1398 * is not the actual idle state.
1399 */
1400 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1401 for (q = 0; q < local->hw.queues; q++) {
1402 if (local->queue_stop_reasons[q]) {
1403 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1404 flags);
1405 mod_timer(&local->dynamic_ps_timer, jiffies +
1406 msecs_to_jiffies(
1407 local->hw.conf.dynamic_ps_timeout));
1408 return;
1409 }
1410 }
1411 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301412 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301413
Vivek Natarajan375177b2010-02-09 14:50:28 +05301414 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301415 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001416 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301417 mod_timer(&local->dynamic_ps_timer, jiffies +
1418 msecs_to_jiffies(
1419 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001420 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301421 ieee80211_send_nullfunc(local, sdata, 1);
1422 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001423 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301424 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301425 }
1426
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001427 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1428 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301429 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1430 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1431 local->hw.conf.flags |= IEEE80211_CONF_PS;
1432 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1433 }
Johannes Berg965beda2009-04-16 13:17:24 +02001434}
1435
1436void ieee80211_dynamic_ps_timer(unsigned long data)
1437{
1438 struct ieee80211_local *local = (void *) data;
1439
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001440 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001441 return;
1442
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001443 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001444}
1445
Simon Wunderlich164eb022013-02-08 18:16:20 +01001446void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1447{
1448 struct delayed_work *delayed_work =
1449 container_of(work, struct delayed_work, work);
1450 struct ieee80211_sub_if_data *sdata =
1451 container_of(delayed_work, struct ieee80211_sub_if_data,
1452 dfs_cac_timer_work);
1453
1454 ieee80211_vif_release_channel(sdata);
1455
1456 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1457}
1458
Johannes Berg60f8b392008-09-08 17:44:22 +02001459/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001460static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001461 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001462 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001463{
Jiri Bencf0706e82007-05-05 11:45:53 -07001464 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001465 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001466 size_t left;
1467 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001468 const u8 *pos;
1469 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001470
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001471 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001472 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001473
Johannes Berg32c5057b2012-03-28 11:04:29 +02001474 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001475 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001476
1477 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001478 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001479
Jiri Bencf0706e82007-05-05 11:45:53 -07001480 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001481 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001482
1483 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001484 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001485
Jiri Bencf0706e82007-05-05 11:45:53 -07001486 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001487 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001488 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001489 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001490
1491 pos = wmm_param + 8;
1492 left = wmm_param_len - 8;
1493
1494 memset(&params, 0, sizeof(params));
1495
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001496 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001497 for (; left >= 4; left -= 4, pos += 4) {
1498 int aci = (pos[0] >> 5) & 0x03;
1499 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001500 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001501 int queue;
1502
1503 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001504 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001505 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001506 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001507 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001508 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1509 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001510 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001511 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001512 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001513 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001514 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001515 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1516 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001517 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001518 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001519 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001520 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001521 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001522 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1523 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001524 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001525 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001526 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001527 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001528 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001529 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001530 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1531 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001532 break;
1533 }
1534
1535 params.aifs = pos[0] & 0x0f;
1536 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1537 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001538 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001539 params.uapsd = uapsd;
1540
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001541 mlme_dbg(sdata,
1542 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1543 queue, aci, acm,
1544 params.aifs, params.cw_min, params.cw_max,
1545 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001546 sdata->tx_conf[queue] = params;
1547 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001548 sdata_err(sdata,
1549 "failed to set TX queue parameters for queue %d\n",
1550 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001551 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001552
1553 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001554 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001555 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001556}
1557
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001558static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1559{
1560 lockdep_assert_held(&sdata->local->mtx);
1561
1562 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1563 IEEE80211_STA_BEACON_POLL);
1564 ieee80211_run_deferred_scan(sdata->local);
1565}
1566
1567static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1568{
1569 mutex_lock(&sdata->local->mtx);
1570 __ieee80211_stop_poll(sdata);
1571 mutex_unlock(&sdata->local->mtx);
1572}
1573
Johannes Berg7a5158e2008-10-08 10:59:33 +02001574static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1575 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001576{
Johannes Bergbda39332008-10-11 01:51:51 +02001577 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001578 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001579 bool use_protection;
1580 bool use_short_preamble;
1581 bool use_short_slot;
1582
1583 if (erp_valid) {
1584 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1585 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1586 } else {
1587 use_protection = false;
1588 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1589 }
1590
1591 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001592 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001593 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001594
Johannes Berg471b3ef2007-12-28 14:32:58 +01001595 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001596 bss_conf->use_cts_prot = use_protection;
1597 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001598 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001599
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001600 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001601 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001602 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001603 }
Daniel Draked9430a32007-07-27 15:43:24 +02001604
Johannes Berg7a5158e2008-10-08 10:59:33 +02001605 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001606 bss_conf->use_short_slot = use_short_slot;
1607 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001608 }
1609
1610 return changed;
1611}
1612
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001613static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001614 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001615 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001616{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001617 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001618 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001619 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001620
Johannes Bergae5eb022008-10-14 16:58:37 +02001621 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001622 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001623 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001624
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001625 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001626 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001627
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001628 sdata->u.mgd.associated = cbss;
1629 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001630
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001631 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1632
Johannes Berg488dd7b2012-10-29 20:08:01 +01001633 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001634 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001635
Johannes Berg9caf0362012-11-29 01:25:20 +01001636 rcu_read_lock();
1637 ies = rcu_dereference(cbss->ies);
1638 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001639 int ret;
1640
1641 ret = cfg80211_get_p2p_attr(
1642 ies->data, ies->len,
1643 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001644 (u8 *) &bss_conf->p2p_noa_attr,
1645 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001646 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001647 sdata->u.mgd.p2p_noa_index =
1648 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001649 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001650 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001651 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001652 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001653 }
1654
Johannes Bergb291ba12009-07-10 15:29:03 +02001655 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001656 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001657
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001658 ieee80211_led_assoc(local, 1);
1659
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001660 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001661 /*
1662 * If the AP is buggy we may get here with no DTIM period
1663 * known, so assume it's 1 which is the only safe assumption
1664 * in that case, although if the TIM IE is broken powersave
1665 * probably just won't work at all.
1666 */
1667 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001668 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001669 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001670 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001671 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001672
Juuso Oikarinen68542962010-06-09 13:43:26 +03001673 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001674
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001675 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001676 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001677 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001678 bss_info_changed |= BSS_CHANGED_CQM;
1679
Juuso Oikarinen68542962010-06-09 13:43:26 +03001680 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001681 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001682 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001683
Johannes Bergae5eb022008-10-14 16:58:37 +02001684 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001685
Johannes Berg056508d2009-07-30 21:43:55 +02001686 mutex_lock(&local->iflist_mtx);
1687 ieee80211_recalc_ps(local, -1);
1688 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001689
Johannes Berg04ecd252012-09-11 14:34:12 +02001690 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001691 ieee80211_recalc_ps_vif(sdata);
1692
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001693 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001694}
1695
Jouni Malinene69e95d2010-03-29 23:29:31 -07001696static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001697 u16 stype, u16 reason, bool tx,
1698 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001699{
Johannes Berg46900292009-02-15 12:44:28 +01001700 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001701 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001702 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001703
Johannes Berg77fdaa12009-07-07 03:45:17 +02001704 ASSERT_MGD_MTX(ifmgd);
1705
Johannes Berg37ad3882012-02-24 13:50:54 +01001706 if (WARN_ON_ONCE(tx && !frame_buf))
1707 return;
1708
Johannes Bergb291ba12009-07-10 15:29:03 +02001709 if (WARN_ON(!ifmgd->associated))
1710 return;
1711
David Spinadel79543d82012-06-12 09:59:45 +03001712 ieee80211_stop_poll(sdata);
1713
Johannes Berg77fdaa12009-07-07 03:45:17 +02001714 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001715 netif_carrier_off(sdata->dev);
1716
Eliad Peller88bc40e2012-07-12 17:35:33 +03001717 /*
1718 * if we want to get out of ps before disassoc (why?) we have
1719 * to do it before sending disassoc, as otherwise the null-packet
1720 * won't be valid.
1721 */
1722 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1723 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1724 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1725 }
1726 local->ps_sdata = NULL;
1727
Eliad Pellerab095872012-07-27 12:33:22 +03001728 /* disable per-vif ps */
1729 ieee80211_recalc_ps_vif(sdata);
1730
Eliad Pellerf8239812012-06-27 14:18:22 +03001731 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1732 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001733 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001734
Johannes Berg37ad3882012-02-24 13:50:54 +01001735 /* deauthenticate/disassociate now */
1736 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001737 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1738 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001739
1740 /* flush out frame */
1741 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001742 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001743
Eliad Peller88a9e312012-06-01 11:14:03 +03001744 /* clear bssid only after building the needed mgmt frames */
1745 memset(ifmgd->bssid, 0, ETH_ALEN);
1746
Johannes Berg37ad3882012-02-24 13:50:54 +01001747 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001748 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001749
1750 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001751 changed |= ieee80211_reset_erp_info(sdata);
1752
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001753 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001754 changed |= BSS_CHANGED_ASSOC;
1755 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001756
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001757 ifmgd->p2p_noa_index = -1;
1758 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1759 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001760
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001761 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001762 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1763 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001764 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1765 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001766
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001767 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301768
Kalle Valo520eb822008-12-18 23:35:27 +02001769 del_timer_sync(&local->dynamic_ps_timer);
1770 cancel_work_sync(&local->dynamic_ps_enable_work);
1771
Juuso Oikarinen68542962010-06-09 13:43:26 +03001772 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001773 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001774 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001775
Johannes Berg3abead52012-03-02 15:56:59 +01001776 sdata->vif.bss_conf.qos = false;
1777 changed |= BSS_CHANGED_QOS;
1778
Johannes Berg0aaffa92010-05-05 15:28:27 +02001779 /* The BSSID (not really interesting) and HT changed */
1780 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001781 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001782
Johannes Berg3abead52012-03-02 15:56:59 +01001783 /* disassociated - set to defaults now */
1784 ieee80211_set_wmm_default(sdata, false);
1785
Johannes Bergb9dcf712010-08-27 12:35:54 +02001786 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1787 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1788 del_timer_sync(&sdata->u.mgd.timer);
1789 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001790
Johannes Berg826262c2012-12-10 16:38:14 +02001791 sdata->vif.bss_conf.dtim_period = 0;
1792
Johannes Berg028e8da02012-11-26 11:57:41 +01001793 ifmgd->flags = 0;
1794 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001795}
Jiri Bencf0706e82007-05-05 11:45:53 -07001796
Kalle Valo3cf335d2009-03-22 21:57:06 +02001797void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1798 struct ieee80211_hdr *hdr)
1799{
1800 /*
1801 * We can postpone the mgd.timer whenever receiving unicast frames
1802 * from AP because we know that the connection is working both ways
1803 * at that time. But multicast frames (and hence also beacons) must
1804 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001805 * data idle periods for sending the periodic probe request to the
1806 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001807 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001808 if (is_multicast_ether_addr(hdr->addr1))
1809 return;
1810
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001811 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001812}
Jiri Bencf0706e82007-05-05 11:45:53 -07001813
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001814static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1815{
1816 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001817 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001818
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001819 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001820 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001821 IEEE80211_STA_CONNECTION_POLL))) {
1822 mutex_unlock(&local->mtx);
1823 return;
1824 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001825
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001826 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001827
1828 mutex_lock(&local->iflist_mtx);
1829 ieee80211_recalc_ps(local, -1);
1830 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001831
1832 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001833 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001834
1835 /*
1836 * We've received a probe response, but are not sure whether
1837 * we have or will be receiving any beacons or data, so let's
1838 * schedule the timers again, just in case.
1839 */
1840 ieee80211_sta_reset_beacon_monitor(sdata);
1841
1842 mod_timer(&ifmgd->conn_mon_timer,
1843 round_jiffies_up(jiffies +
1844 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001845out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001846 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001847}
1848
1849void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001850 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001851{
Felix Fietkau75706d02010-12-02 21:01:07 +01001852 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001853 return;
1854
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001855 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1856 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001857 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001858 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001859 else
1860 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001861 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001862 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001863 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001864
1865 if (ack)
1866 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001867}
1868
Maxim Levitskya43abf22009-07-31 18:54:12 +03001869static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1870{
1871 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1872 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001873 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001874 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001875
1876 /*
1877 * Try sending broadcast probe requests for the last three
1878 * probe requests after the first ones failed since some
1879 * buggy APs only support broadcast probe requests.
1880 */
1881 if (ifmgd->probe_send_count >= unicast_limit)
1882 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001883
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001884 /*
1885 * When the hardware reports an accurate Tx ACK status, it's
1886 * better to send a nullfunc frame instead of a probe request,
1887 * as it will kick us off the AP quickly if we aren't associated
1888 * anymore. The timeout will be reset if the frame is ACKed by
1889 * the AP.
1890 */
Soumik Das992e68b2012-05-20 15:31:13 +05301891 ifmgd->probe_send_count++;
1892
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001893 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1894 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001895 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001896 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001897 int ssid_len;
1898
Johannes Berg9caf0362012-11-29 01:25:20 +01001899 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001900 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001901 if (WARN_ON_ONCE(ssid == NULL))
1902 ssid_len = 0;
1903 else
1904 ssid_len = ssid[1];
1905
1906 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e2013-01-29 15:02:27 +01001907 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001908 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001909 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001910 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001911
Ben Greear180205b2011-02-04 15:30:24 -08001912 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001913 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301914 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01001915 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001916}
1917
Johannes Bergb291ba12009-07-10 15:29:03 +02001918static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1919 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001920{
Kalle Valo04de8382009-03-22 21:57:35 +02001921 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001922 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001923
Johannes Berg9607e6b2009-12-23 13:15:31 +01001924 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001925 return;
1926
Johannes Berg77fdaa12009-07-07 03:45:17 +02001927 mutex_lock(&ifmgd->mtx);
1928
1929 if (!ifmgd->associated)
1930 goto out;
1931
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001932 mutex_lock(&sdata->local->mtx);
1933
1934 if (sdata->local->tmp_channel || sdata->local->scanning) {
1935 mutex_unlock(&sdata->local->mtx);
1936 goto out;
1937 }
1938
Ben Greeara13fbe52013-03-25 11:19:35 -07001939 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001940 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07001941 "detected beacon loss from AP (missed %d beacons) - probing\n",
1942 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001943
Ben Greeara13fbe52013-03-25 11:19:35 -07001944 ieee80211_cqm_rssi_notify(&sdata->vif,
1945 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
1946 GFP_KERNEL);
1947 }
Kalle Valo04de8382009-03-22 21:57:35 +02001948
Johannes Bergb291ba12009-07-10 15:29:03 +02001949 /*
1950 * The driver/our work has already reported this event or the
1951 * connection monitoring has kicked in and we have already sent
1952 * a probe request. Or maybe the AP died and the driver keeps
1953 * reporting until we disassociate...
1954 *
1955 * In either case we have to ignore the current call to this
1956 * function (except for setting the correct probe reason bit)
1957 * because otherwise we would reset the timer every time and
1958 * never check whether we received a probe response!
1959 */
1960 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1961 IEEE80211_STA_CONNECTION_POLL))
1962 already = true;
1963
1964 if (beacon)
1965 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1966 else
1967 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1968
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001969 mutex_unlock(&sdata->local->mtx);
1970
Johannes Bergb291ba12009-07-10 15:29:03 +02001971 if (already)
1972 goto out;
1973
Johannes Berg4e751842009-06-10 15:16:52 +02001974 mutex_lock(&sdata->local->iflist_mtx);
1975 ieee80211_recalc_ps(sdata->local, -1);
1976 mutex_unlock(&sdata->local->iflist_mtx);
1977
Maxim Levitskya43abf22009-07-31 18:54:12 +03001978 ifmgd->probe_send_count = 0;
1979 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001980 out:
1981 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001982}
1983
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001984struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1985 struct ieee80211_vif *vif)
1986{
1987 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1988 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001989 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001990 struct sk_buff *skb;
1991 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001992 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001993
1994 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1995 return NULL;
1996
1997 ASSERT_MGD_MTX(ifmgd);
1998
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001999 if (ifmgd->associated)
2000 cbss = ifmgd->associated;
2001 else if (ifmgd->auth_data)
2002 cbss = ifmgd->auth_data->bss;
2003 else if (ifmgd->assoc_data)
2004 cbss = ifmgd->assoc_data->bss;
2005 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002006 return NULL;
2007
Johannes Berg9caf0362012-11-29 01:25:20 +01002008 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002009 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002010 if (WARN_ON_ONCE(ssid == NULL))
2011 ssid_len = 0;
2012 else
2013 ssid_len = ssid[1];
2014
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002015 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002016 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002017 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002018 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002019 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002020
2021 return skb;
2022}
2023EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2024
Johannes Bergeef9e542013-01-29 13:09:34 +01002025static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002026{
2027 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002028 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002029
2030 mutex_lock(&ifmgd->mtx);
2031 if (!ifmgd->associated) {
2032 mutex_unlock(&ifmgd->mtx);
2033 return;
2034 }
2035
Johannes Berg37ad3882012-02-24 13:50:54 +01002036 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2037 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002038 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002039 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002040 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002041 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002042 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002043 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002044
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002045 /*
2046 * must be outside lock due to cfg80211,
2047 * but that's not a problem.
2048 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002049 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002050}
2051
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002052static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002053{
2054 struct ieee80211_sub_if_data *sdata =
2055 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002056 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002057 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2058 struct sta_info *sta;
2059
2060 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302061 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002062 sta = sta_info_get(sdata, ifmgd->bssid);
2063 if (sta)
2064 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302065 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002066 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002067
Johannes Berg682bd382013-01-29 13:13:50 +01002068 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002069 sdata_info(sdata, "Connection to AP %pM lost\n",
2070 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002071 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002072 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002073 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002074 }
2075}
2076
2077static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2078{
2079 struct ieee80211_sub_if_data *sdata =
2080 container_of(work, struct ieee80211_sub_if_data,
2081 u.mgd.csa_connection_drop_work);
2082
Johannes Bergeef9e542013-01-29 13:09:34 +01002083 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002084}
2085
Kalle Valo04de8382009-03-22 21:57:35 +02002086void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2087{
2088 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002089 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002090
Johannes Bergb5878a22010-04-07 16:48:40 +02002091 trace_api_beacon_loss(sdata);
2092
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002093 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002094 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002095 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002096}
2097EXPORT_SYMBOL(ieee80211_beacon_loss);
2098
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002099void ieee80211_connection_loss(struct ieee80211_vif *vif)
2100{
2101 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2102 struct ieee80211_hw *hw = &sdata->local->hw;
2103
Johannes Bergb5878a22010-04-07 16:48:40 +02002104 trace_api_connection_loss(sdata);
2105
Johannes Berg682bd382013-01-29 13:13:50 +01002106 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002107 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2108}
2109EXPORT_SYMBOL(ieee80211_connection_loss);
2110
2111
Johannes Berg66e67e42012-01-20 13:55:27 +01002112static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2113 bool assoc)
2114{
2115 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2116
2117 lockdep_assert_held(&sdata->u.mgd.mtx);
2118
Johannes Berg66e67e42012-01-20 13:55:27 +01002119 if (!assoc) {
2120 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2121
2122 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2123 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002124 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002125 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002126 }
2127
Johannes Berg5b112d32013-02-01 01:49:58 +01002128 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002129 kfree(auth_data);
2130 sdata->u.mgd.auth_data = NULL;
2131}
2132
2133static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2134 struct ieee80211_mgmt *mgmt, size_t len)
2135{
Johannes Berg1672c0e2013-01-29 15:02:27 +01002136 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002137 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2138 u8 *pos;
2139 struct ieee802_11_elems elems;
Johannes Berg1672c0e2013-01-29 15:02:27 +01002140 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002141
2142 pos = mgmt->u.auth.variable;
2143 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2144 if (!elems.challenge)
2145 return;
2146 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002147 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e2013-01-29 15:02:27 +01002148 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2149 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2150 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002151 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002152 elems.challenge - 2, elems.challenge_len + 2,
2153 auth_data->bss->bssid, auth_data->bss->bssid,
2154 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e2013-01-29 15:02:27 +01002155 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002156}
2157
2158static enum rx_mgmt_action __must_check
2159ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2160 struct ieee80211_mgmt *mgmt, size_t len)
2161{
2162 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2163 u8 bssid[ETH_ALEN];
2164 u16 auth_alg, auth_transaction, status_code;
2165 struct sta_info *sta;
2166
2167 lockdep_assert_held(&ifmgd->mtx);
2168
2169 if (len < 24 + 6)
2170 return RX_MGMT_NONE;
2171
2172 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2173 return RX_MGMT_NONE;
2174
2175 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2176
Joe Perchesb203ca32012-05-08 18:56:52 +00002177 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002178 return RX_MGMT_NONE;
2179
2180 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2181 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2182 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2183
2184 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002185 auth_transaction != ifmgd->auth_data->expected_transaction) {
2186 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2187 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2188 auth_transaction,
2189 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002190 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002191 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002192
2193 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002194 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2195 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002196 ieee80211_destroy_auth_data(sdata, false);
2197 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002198 }
2199
2200 switch (ifmgd->auth_data->algorithm) {
2201 case WLAN_AUTH_OPEN:
2202 case WLAN_AUTH_LEAP:
2203 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002204 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002205 break;
2206 case WLAN_AUTH_SHARED_KEY:
2207 if (ifmgd->auth_data->expected_transaction != 4) {
2208 ieee80211_auth_challenge(sdata, mgmt, len);
2209 /* need another frame */
2210 return RX_MGMT_NONE;
2211 }
2212 break;
2213 default:
2214 WARN_ONCE(1, "invalid auth alg %d",
2215 ifmgd->auth_data->algorithm);
2216 return RX_MGMT_NONE;
2217 }
2218
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002219 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002220 ifmgd->auth_data->done = true;
2221 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002222 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002223 run_again(ifmgd, ifmgd->auth_data->timeout);
2224
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002225 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2226 ifmgd->auth_data->expected_transaction != 2) {
2227 /*
2228 * Report auth frame to user space for processing since another
2229 * round of Authentication frames is still needed.
2230 */
2231 return RX_MGMT_CFG80211_RX_AUTH;
2232 }
2233
Johannes Berg66e67e42012-01-20 13:55:27 +01002234 /* move station state to auth */
2235 mutex_lock(&sdata->local->sta_mtx);
2236 sta = sta_info_get(sdata, bssid);
2237 if (!sta) {
2238 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2239 goto out_err;
2240 }
2241 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002242 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002243 goto out_err;
2244 }
2245 mutex_unlock(&sdata->local->sta_mtx);
2246
2247 return RX_MGMT_CFG80211_RX_AUTH;
2248 out_err:
2249 mutex_unlock(&sdata->local->sta_mtx);
2250 /* ignore frame -- wait for timeout */
2251 return RX_MGMT_NONE;
2252}
2253
2254
Johannes Berg77fdaa12009-07-07 03:45:17 +02002255static enum rx_mgmt_action __must_check
2256ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002257 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002258{
Johannes Berg46900292009-02-15 12:44:28 +01002259 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002260 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002261 u16 reason_code;
2262
Johannes Berg66e67e42012-01-20 13:55:27 +01002263 lockdep_assert_held(&ifmgd->mtx);
2264
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002265 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002266 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002267
Johannes Berg66e67e42012-01-20 13:55:27 +01002268 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002269 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002270 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002271
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002272 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002273
2274 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2275
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002276 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2277 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002278
Johannes Berg37ad3882012-02-24 13:50:54 +01002279 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2280
Johannes Berg77fdaa12009-07-07 03:45:17 +02002281 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002282}
2283
2284
Johannes Berg77fdaa12009-07-07 03:45:17 +02002285static enum rx_mgmt_action __must_check
2286ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2287 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;
Jiri Bencf0706e82007-05-05 11:45:53 -07002290 u16 reason_code;
2291
Johannes Berg66e67e42012-01-20 13:55:27 +01002292 lockdep_assert_held(&ifmgd->mtx);
2293
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002294 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002295 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002296
Johannes Berg66e67e42012-01-20 13:55:27 +01002297 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002298 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002299 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002300
2301 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2302
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002303 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2304 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002305
Johannes Berg37ad3882012-02-24 13:50:54 +01002306 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2307
Johannes Berg77fdaa12009-07-07 03:45:17 +02002308 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002309}
2310
Christian Lamparterc74d0842011-10-15 00:14:49 +02002311static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2312 u8 *supp_rates, unsigned int supp_rates_len,
2313 u32 *rates, u32 *basic_rates,
2314 bool *have_higher_than_11mbit,
2315 int *min_rate, int *min_rate_index)
2316{
2317 int i, j;
2318
2319 for (i = 0; i < supp_rates_len; i++) {
2320 int rate = (supp_rates[i] & 0x7f) * 5;
2321 bool is_basic = !!(supp_rates[i] & 0x80);
2322
2323 if (rate > 110)
2324 *have_higher_than_11mbit = true;
2325
2326 /*
2327 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2328 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2329 *
2330 * Note: Even through the membership selector and the basic
2331 * rate flag share the same bit, they are not exactly
2332 * the same.
2333 */
2334 if (!!(supp_rates[i] & 0x80) &&
2335 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2336 continue;
2337
2338 for (j = 0; j < sband->n_bitrates; j++) {
2339 if (sband->bitrates[j].bitrate == rate) {
2340 *rates |= BIT(j);
2341 if (is_basic)
2342 *basic_rates |= BIT(j);
2343 if (rate < *min_rate) {
2344 *min_rate = rate;
2345 *min_rate_index = j;
2346 }
2347 break;
2348 }
2349 }
2350 }
2351}
Jiri Bencf0706e82007-05-05 11:45:53 -07002352
Johannes Berg66e67e42012-01-20 13:55:27 +01002353static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2354 bool assoc)
2355{
2356 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2357
2358 lockdep_assert_held(&sdata->u.mgd.mtx);
2359
Johannes Berg66e67e42012-01-20 13:55:27 +01002360 if (!assoc) {
2361 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2362
2363 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2364 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002365 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002366 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002367 }
2368
2369 kfree(assoc_data);
2370 sdata->u.mgd.assoc_data = NULL;
2371}
2372
2373static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2374 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002375 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002376{
Johannes Berg46900292009-02-15 12:44:28 +01002377 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002378 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002379 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002380 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002381 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002382 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002383 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002384 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002385 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002386 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002387
Johannes Bergaf6b6372009-12-23 13:15:35 +01002388 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002389
Jiri Bencf0706e82007-05-05 11:45:53 -07002390 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002391 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002392
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002393 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002394 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2395 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002396 aid &= ~(BIT(15) | BIT(14));
2397
Johannes Berg05cb9102011-10-28 11:59:47 +02002398 ifmgd->broken_ap = false;
2399
2400 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002401 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2402 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002403 aid = 0;
2404 ifmgd->broken_ap = true;
2405 }
2406
Johannes Bergaf6b6372009-12-23 13:15:35 +01002407 pos = mgmt->u.assoc_resp.variable;
2408 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2409
Jiri Bencf0706e82007-05-05 11:45:53 -07002410 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002411 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002412 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002413 }
2414
Johannes Berg46900292009-02-15 12:44:28 +01002415 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002416
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002417 /*
2418 * We previously checked these in the beacon/probe response, so
2419 * they should be present here. This is just a safety net.
2420 */
2421 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2422 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2423 sdata_info(sdata,
2424 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2425 return false;
2426 }
2427
2428 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2429 (!elems.vht_cap_elem || !elems.vht_operation)) {
2430 sdata_info(sdata,
2431 "VHT AP is missing VHT capability/operation in AssocResp\n");
2432 return false;
2433 }
2434
Guy Eilam2a33bee2011-08-17 15:18:15 +03002435 mutex_lock(&sdata->local->sta_mtx);
2436 /*
2437 * station info was already allocated and inserted before
2438 * the association and should be available to us
2439 */
Johannes Berg7852e362012-01-20 13:55:24 +01002440 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002441 if (WARN_ON(!sta)) {
2442 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002443 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002444 }
2445
Johannes Berg55de9082012-07-26 17:24:39 +02002446 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002447
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002448 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002449 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002450 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002451 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002452
Mahesh Palivela818255e2012-10-10 11:33:04 +00002453 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2454 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002455 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002456
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002457 /*
2458 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2459 * in their association response, so ignore that data for our own
2460 * configuration. If it changed since the last beacon, we'll get the
2461 * next beacon and update then.
2462 */
Johannes Berg11289582013-02-07 17:36:12 +01002463
Johannes Bergbee7f582013-02-07 22:24:55 +01002464 /*
2465 * If an operating mode notification IE is present, override the
2466 * NSS calculation (that would be done in rate_control_rate_init())
2467 * and use the # of streams from that element.
2468 */
2469 if (elems.opmode_notif &&
2470 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2471 u8 nss;
2472
2473 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2474 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2475 nss += 1;
2476 sta->sta.rx_nss = nss;
2477 }
2478
Johannes Berg4b7679a2008-09-18 18:14:18 +02002479 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002480
Johannes Berg46900292009-02-15 12:44:28 +01002481 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002482 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002483
Johannes Bergddf4ac52008-10-22 11:41:38 +02002484 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002485 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002486
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002487 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002488 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2489 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2490 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002491 sdata_info(sdata,
2492 "failed to move station %pM to desired state\n",
2493 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002494 WARN_ON(__sta_info_destroy(sta));
2495 mutex_unlock(&sdata->local->sta_mtx);
2496 return false;
2497 }
2498
Johannes Berg7852e362012-01-20 13:55:24 +01002499 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002500
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002501 /*
2502 * Always handle WMM once after association regardless
2503 * of the first value the AP uses. Setting -1 here has
2504 * that effect because the AP values is an unsigned
2505 * 4-bit value.
2506 */
2507 ifmgd->wmm_last_param_set = -1;
2508
Johannes Bergddf4ac52008-10-22 11:41:38 +02002509 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002510 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002511 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002512 else
Johannes Berg3abead52012-03-02 15:56:59 +01002513 ieee80211_set_wmm_default(sdata, false);
2514 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002515
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002516 /* set AID and assoc capability,
2517 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002518 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002519 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002520 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002521
Kalle Valo15b7b062009-03-22 21:57:14 +02002522 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002523 * If we're using 4-addr mode, let the AP know that we're
2524 * doing so, so that it can create the STA VLAN on its side
2525 */
2526 if (ifmgd->use_4addr)
2527 ieee80211_send_4addr_nullfunc(local, sdata);
2528
2529 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002530 * Start timer to probe the connection to the AP now.
2531 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002532 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002533 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002534 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002535
Johannes Bergaf6b6372009-12-23 13:15:35 +01002536 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002537}
2538
Johannes Berg66e67e42012-01-20 13:55:27 +01002539static enum rx_mgmt_action __must_check
2540ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2541 struct ieee80211_mgmt *mgmt, size_t len,
2542 struct cfg80211_bss **bss)
2543{
2544 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2545 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2546 u16 capab_info, status_code, aid;
2547 struct ieee802_11_elems elems;
2548 u8 *pos;
2549 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002550
Johannes Berg66e67e42012-01-20 13:55:27 +01002551 lockdep_assert_held(&ifmgd->mtx);
2552
2553 if (!assoc_data)
2554 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002555 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002556 return RX_MGMT_NONE;
2557
2558 /*
2559 * AssocResp and ReassocResp have identical structure, so process both
2560 * of them in this function.
2561 */
2562
2563 if (len < 24 + 6)
2564 return RX_MGMT_NONE;
2565
2566 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2567 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2568 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2569 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2570
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002571 sdata_info(sdata,
2572 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2573 reassoc ? "Rea" : "A", mgmt->sa,
2574 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002575
2576 pos = mgmt->u.assoc_resp.variable;
2577 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2578
2579 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002580 elems.timeout_int &&
2581 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002582 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002583 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002584 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002585 sdata_info(sdata,
2586 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2587 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002588 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002589 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002590 if (ms > IEEE80211_ASSOC_TIMEOUT)
2591 run_again(ifmgd, assoc_data->timeout);
2592 return RX_MGMT_NONE;
2593 }
2594
2595 *bss = assoc_data->bss;
2596
2597 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002598 sdata_info(sdata, "%pM denied association (code=%d)\n",
2599 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002600 ieee80211_destroy_assoc_data(sdata, false);
2601 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002602 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2603 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002604 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002605 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002606 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2607 }
John W. Linville635d9992012-07-09 16:34:34 -04002608 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002609
2610 /*
2611 * destroy assoc_data afterwards, as otherwise an idle
2612 * recalc after assoc_data is NULL but before associated
2613 * is set can cause the interface to go idle
2614 */
2615 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002616 }
2617
2618 return RX_MGMT_CFG80211_RX_ASSOC;
2619}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002620
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002621static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002622 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002623 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002624 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002625{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002626 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002627 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002628 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002629 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002630 bool need_ps = false;
2631
Johannes Berg826262c2012-12-10 16:38:14 +02002632 if ((sdata->u.mgd.associated &&
2633 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2634 (sdata->u.mgd.assoc_data &&
2635 ether_addr_equal(mgmt->bssid,
2636 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002637 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002638 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2639
2640 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002641 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002642 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2643 }
Johannes Berg56007a02010-01-26 14:19:52 +01002644 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002645
Johannes Berg1cd8e882013-03-27 14:30:12 +01002646 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002647 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2648 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002649 else
2650 freq = rx_status->freq;
2651
2652 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2653
2654 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2655 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002656
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002657 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002658 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002659 if (bss)
2660 ieee80211_rx_bss_put(local, bss);
2661
Johannes Berg37799e52013-03-26 14:02:26 +01002662 if (!sdata->u.mgd.associated ||
2663 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002664 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002665
Johannes Berg56007a02010-01-26 14:19:52 +01002666 if (need_ps) {
2667 mutex_lock(&local->iflist_mtx);
2668 ieee80211_recalc_ps(local, -1);
2669 mutex_unlock(&local->iflist_mtx);
2670 }
2671
Johannes Berg37799e52013-03-26 14:02:26 +01002672 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime, elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002673}
2674
2675
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002676static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002677 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002678{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002679 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002680 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002681 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2682 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002683 struct ieee802_11_elems elems;
2684
Kalle Valo15b7b062009-03-22 21:57:14 +02002685 ifmgd = &sdata->u.mgd;
2686
Johannes Berg77fdaa12009-07-07 03:45:17 +02002687 ASSERT_MGD_MTX(ifmgd);
2688
Joe Perchesb203ca32012-05-08 18:56:52 +00002689 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002690 return; /* ignore ProbeResp to foreign address */
2691
Ester Kummerae6a44e2008-06-27 18:54:48 +03002692 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2693 if (baselen > len)
2694 return;
2695
2696 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2697 &elems);
2698
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002699 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002700
Johannes Berg77fdaa12009-07-07 03:45:17 +02002701 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002702 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002703 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002704
2705 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002706 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002707 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002708 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002709 ifmgd->auth_data->tries = 0;
2710 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002711 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002712 run_again(ifmgd, ifmgd->auth_data->timeout);
2713 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002714}
2715
Johannes Bergd91f36d2009-04-16 13:17:26 +02002716/*
2717 * This is the canonical list of information elements we care about,
2718 * the filter code also gives us all changes to the Microsoft OUI
2719 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2720 *
2721 * We implement beacon filtering in software since that means we can
2722 * avoid processing the frame here and in cfg80211, and userspace
2723 * will not be able to tell whether the hardware supports it or not.
2724 *
2725 * XXX: This list needs to be dynamic -- userspace needs to be able to
2726 * add items it requires. It also needs to be able to tell us to
2727 * look out for other vendor IEs.
2728 */
2729static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002730 (1ULL << WLAN_EID_COUNTRY) |
2731 (1ULL << WLAN_EID_ERP_INFO) |
2732 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2733 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2734 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002735 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002736
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002737static enum rx_mgmt_action
2738ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2739 struct ieee80211_mgmt *mgmt, size_t len,
2740 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002741{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002742 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002743 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002744 size_t baselen;
2745 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002746 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002747 struct ieee80211_chanctx_conf *chanctx_conf;
2748 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002749 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002750 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002751 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002752 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002753 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002754 u8 *bssid;
2755
Johannes Berg66e67e42012-01-20 13:55:27 +01002756 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002757
Ester Kummerae6a44e2008-06-27 18:54:48 +03002758 /* Process beacon from the current BSS */
2759 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2760 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002761 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002762
Johannes Berg55de9082012-07-26 17:24:39 +02002763 rcu_read_lock();
2764 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2765 if (!chanctx_conf) {
2766 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002767 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002768 }
2769
Johannes Berg4bf88532012-11-09 11:39:59 +01002770 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002771 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002772 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002773 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002774 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002775 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002776
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002777 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002778 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002779 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2780 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002781
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002782 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002783 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002784 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002785 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2786 sdata->vif.bss_conf.sync_tsf =
2787 le64_to_cpu(mgmt->u.beacon.timestamp);
2788 sdata->vif.bss_conf.sync_device_ts =
2789 rx_status->device_timestamp;
2790 if (elems.tim)
2791 sdata->vif.bss_conf.sync_dtim_count =
2792 elems.tim->dtim_count;
2793 else
2794 sdata->vif.bss_conf.sync_dtim_count = 0;
2795 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002796 /* continue assoc process */
2797 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002798 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002799 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002800 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002801 }
2802
2803 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002804 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002805 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002806 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002807
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002808 /* Track average RSSI from the Beacon frames of the current AP */
2809 ifmgd->last_beacon_signal = rx_status->signal;
2810 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2811 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002812 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002813 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002814 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002815 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002816 } else {
2817 ifmgd->ave_beacon_signal =
2818 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2819 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2820 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002821 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002822 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002823
2824 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2825 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2826 int sig = ifmgd->ave_beacon_signal;
2827 int last_sig = ifmgd->last_ave_beacon_signal;
2828
2829 /*
2830 * if signal crosses either of the boundaries, invoke callback
2831 * with appropriate parameters
2832 */
2833 if (sig > ifmgd->rssi_max_thold &&
2834 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2835 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002836 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002837 } else if (sig < ifmgd->rssi_min_thold &&
2838 (last_sig >= ifmgd->rssi_max_thold ||
2839 last_sig == 0)) {
2840 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002841 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002842 }
2843 }
2844
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002845 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002846 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002847 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002848 int sig = ifmgd->ave_beacon_signal / 16;
2849 int last_event = ifmgd->last_cqm_event_signal;
2850 int thold = bss_conf->cqm_rssi_thold;
2851 int hyst = bss_conf->cqm_rssi_hyst;
2852 if (sig < thold &&
2853 (last_event == 0 || sig < last_event - hyst)) {
2854 ifmgd->last_cqm_event_signal = sig;
2855 ieee80211_cqm_rssi_notify(
2856 &sdata->vif,
2857 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2858 GFP_KERNEL);
2859 } else if (sig > thold &&
2860 (last_event == 0 || sig > last_event + hyst)) {
2861 ifmgd->last_cqm_event_signal = sig;
2862 ieee80211_cqm_rssi_notify(
2863 &sdata->vif,
2864 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2865 GFP_KERNEL);
2866 }
2867 }
2868
Johannes Bergb291ba12009-07-10 15:29:03 +02002869 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002870 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002871 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002872 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002873 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002874 ieee80211_run_deferred_scan(local);
2875 mutex_unlock(&local->mtx);
2876
Johannes Berg4e751842009-06-10 15:16:52 +02002877 mutex_lock(&local->iflist_mtx);
2878 ieee80211_recalc_ps(local, -1);
2879 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002880 }
2881
Johannes Bergb291ba12009-07-10 15:29:03 +02002882 /*
2883 * Push the beacon loss detection into the future since
2884 * we are processing a beacon from the AP just now.
2885 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002886 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002887
Johannes Bergd91f36d2009-04-16 13:17:26 +02002888 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2889 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2890 len - baselen, &elems,
2891 care_about_ies, ncrc);
2892
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302893 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002894 bool directed_tim = ieee80211_check_tim(elems.tim,
2895 elems.tim_len,
2896 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002897 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002898 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002899 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2900 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2901 ieee80211_hw_config(local,
2902 IEEE80211_CONF_CHANGE_PS);
2903 }
Kalle Valo572e0012009-02-10 17:09:31 +02002904 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302905 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002906 local->pspolling = true;
2907
2908 /*
2909 * Here is assumed that the driver will be
2910 * able to send ps-poll frame and receive a
2911 * response even though power save mode is
2912 * enabled, but some drivers might require
2913 * to disable power save here. This needs
2914 * to be investigated.
2915 */
2916 ieee80211_send_pspoll(local, sdata);
2917 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002918 }
2919 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002920
Johannes Berg488dd7b2012-10-29 20:08:01 +01002921 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002922 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01002923 int ret;
2924
2925 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2926 len - baselen,
2927 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01002928 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002929 if (ret >= 2) {
2930 if (sdata->u.mgd.p2p_noa_index != noa.index) {
2931 /* valid noa_attr and index changed */
2932 sdata->u.mgd.p2p_noa_index = noa.index;
2933 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
2934 changed |= BSS_CHANGED_P2P_PS;
2935 /*
2936 * make sure we update all information, the CRC
2937 * mechanism doesn't look at P2P attributes.
2938 */
2939 ifmgd->beacon_crc_valid = false;
2940 }
2941 } else if (sdata->u.mgd.p2p_noa_index != -1) {
2942 /* noa_attr not found and we had valid noa_attr before */
2943 sdata->u.mgd.p2p_noa_index = -1;
2944 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002945 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002946 ifmgd->beacon_crc_valid = false;
2947 }
2948 }
2949
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002950 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002951 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03002952 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002953 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002954
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002955 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002956
2957 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2958 elems.wmm_param_len))
2959 changed |= BSS_CHANGED_QOS;
2960
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002961 /*
2962 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002963 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002964 */
2965 if (!bss_conf->dtim_period) {
2966 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2967 if (elems.tim)
2968 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2969 else
2970 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01002971
2972 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2973 sdata->vif.bss_conf.sync_tsf =
2974 le64_to_cpu(mgmt->u.beacon.timestamp);
2975 sdata->vif.bss_conf.sync_device_ts =
2976 rx_status->device_timestamp;
2977 if (elems.tim)
2978 sdata->vif.bss_conf.sync_dtim_count =
2979 elems.tim->dtim_count;
2980 else
2981 sdata->vif.bss_conf.sync_dtim_count = 0;
2982 }
2983
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002984 changed |= BSS_CHANGED_DTIM_PERIOD;
2985 }
2986
Johannes Berg1946bed2013-03-27 14:31:53 +01002987 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02002988 erp_valid = true;
2989 erp_value = elems.erp_info[0];
2990 } else {
2991 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002992 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002993 changed |= ieee80211_handle_bss_capability(sdata,
2994 le16_to_cpu(mgmt->u.beacon.capab_info),
2995 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002996
Johannes Berg11289582013-02-07 17:36:12 +01002997 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01002998 sta = sta_info_get(sdata, bssid);
2999
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003000 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3001 elems.vht_operation, bssid, &changed)) {
3002 mutex_unlock(&local->sta_mtx);
3003 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3004 WLAN_REASON_DEAUTH_LEAVING,
3005 true, deauth_buf);
3006 return RX_MGMT_CFG80211_TX_DEAUTH;
3007 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003008
3009 if (sta && elems.opmode_notif)
3010 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3011 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003012 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003013
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003014 if (elems.country_elem && elems.pwr_constr_elem &&
3015 mgmt->u.probe_resp.capab_info &
3016 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003017 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3018 elems.country_elem,
3019 elems.country_elem_len,
3020 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003021
Johannes Berg471b3ef2007-12-28 14:32:58 +01003022 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003023
3024 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003025}
3026
Johannes Berg1fa57d02010-06-10 10:21:32 +02003027void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3028 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003029{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003030 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003031 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003032 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003033 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003034 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003035 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003036 u16 fc;
3037
Jiri Bencf0706e82007-05-05 11:45:53 -07003038 rx_status = (struct ieee80211_rx_status *) skb->cb;
3039 mgmt = (struct ieee80211_mgmt *) skb->data;
3040 fc = le16_to_cpu(mgmt->frame_control);
3041
Johannes Berg77fdaa12009-07-07 03:45:17 +02003042 mutex_lock(&ifmgd->mtx);
3043
Johannes Berg66e67e42012-01-20 13:55:27 +01003044 switch (fc & IEEE80211_FCTL_STYPE) {
3045 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003046 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3047 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003048 break;
3049 case IEEE80211_STYPE_PROBE_RESP:
3050 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3051 break;
3052 case IEEE80211_STYPE_AUTH:
3053 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3054 break;
3055 case IEEE80211_STYPE_DEAUTH:
3056 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3057 break;
3058 case IEEE80211_STYPE_DISASSOC:
3059 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3060 break;
3061 case IEEE80211_STYPE_ASSOC_RESP:
3062 case IEEE80211_STYPE_REASSOC_RESP:
3063 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3064 break;
3065 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01003066 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
3067 struct ieee802_11_elems elems;
3068 int ies_len = skb->len -
3069 offsetof(struct ieee80211_mgmt,
3070 u.action.u.chan_switch.variable);
3071
3072 if (ies_len < 0)
3073 break;
3074
3075 ieee802_11_parse_elems(
3076 mgmt->u.action.u.chan_switch.variable,
3077 ies_len, &elems);
3078
3079 if (elems.parse_error)
3080 break;
3081
Johannes Berg66e67e42012-01-20 13:55:27 +01003082 ieee80211_sta_process_chanswitch(sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01003083 rx_status->mactime,
3084 &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003085 }
Johannes Berg37799e52013-03-26 14:02:26 +01003086 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003087 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003088 mutex_unlock(&ifmgd->mtx);
3089
Johannes Berg66e67e42012-01-20 13:55:27 +01003090 switch (rma) {
3091 case RX_MGMT_NONE:
3092 /* no action */
3093 break;
3094 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003095 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003096 break;
3097 case RX_MGMT_CFG80211_DISASSOC:
3098 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3099 break;
3100 case RX_MGMT_CFG80211_RX_AUTH:
3101 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3102 break;
3103 case RX_MGMT_CFG80211_RX_ASSOC:
3104 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3105 break;
3106 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3107 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3108 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003109 case RX_MGMT_CFG80211_TX_DEAUTH:
3110 cfg80211_send_deauth(sdata->dev, deauth_buf,
3111 sizeof(deauth_buf));
3112 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003113 default:
3114 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003115 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003116}
3117
Johannes Berg9c6bd792008-09-11 00:01:52 +02003118static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003119{
3120 struct ieee80211_sub_if_data *sdata =
3121 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003122
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003123 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003124}
3125
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003126static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003127 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003128{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003130 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003131
Johannes Berg37ad3882012-02-24 13:50:54 +01003132 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003133 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003134 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003135
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003136 /*
3137 * must be outside lock due to cfg80211,
3138 * but that's not a problem.
3139 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003140 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003141
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003142 mutex_lock(&ifmgd->mtx);
3143}
3144
Johannes Berg66e67e42012-01-20 13:55:27 +01003145static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3146{
3147 struct ieee80211_local *local = sdata->local;
3148 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3149 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003150 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003151
3152 lockdep_assert_held(&ifmgd->mtx);
3153
3154 if (WARN_ON_ONCE(!auth_data))
3155 return -EINVAL;
3156
Johannes Berg1672c0e2013-01-29 15:02:27 +01003157 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3158 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3159 IEEE80211_TX_INTFL_MLME_CONN_TX;
3160
Johannes Berg66e67e42012-01-20 13:55:27 +01003161 auth_data->tries++;
3162
3163 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003164 sdata_info(sdata, "authentication with %pM timed out\n",
3165 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003166
3167 /*
3168 * Most likely AP is not in the range so remove the
3169 * bss struct for that AP.
3170 */
3171 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3172
3173 return -ETIMEDOUT;
3174 }
3175
Johannes Berga1845fc2012-06-27 13:18:36 +02003176 drv_mgd_prepare_tx(local, sdata);
3177
Johannes Berg66e67e42012-01-20 13:55:27 +01003178 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003179 u16 trans = 1;
3180 u16 status = 0;
3181
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003182 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3183 auth_data->bss->bssid, auth_data->tries,
3184 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003185
3186 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003187
3188 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3189 trans = auth_data->sae_trans;
3190 status = auth_data->sae_status;
3191 auth_data->expected_transaction = trans;
3192 }
3193
3194 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3195 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003196 auth_data->bss->bssid,
Johannes Berg1672c0e2013-01-29 15:02:27 +01003197 auth_data->bss->bssid, NULL, 0, 0,
3198 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003199 } else {
3200 const u8 *ssidie;
3201
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003202 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3203 auth_data->bss->bssid, auth_data->tries,
3204 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003205
Johannes Berg9caf0362012-11-29 01:25:20 +01003206 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003207 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003208 if (!ssidie) {
3209 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003210 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003211 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003212 /*
3213 * Direct probe is sent to broadcast address as some APs
3214 * will not answer to direct packet in unassociated state.
3215 */
3216 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e2013-01-29 15:02:27 +01003217 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003218 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003219 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003220 }
3221
Johannes Berg1672c0e2013-01-29 15:02:27 +01003222 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3223 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003224 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003225 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003226 } else {
3227 auth_data->timeout_started = false;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003228 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003229
3230 return 0;
3231}
3232
3233static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3234{
3235 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3236 struct ieee80211_local *local = sdata->local;
3237
3238 lockdep_assert_held(&sdata->u.mgd.mtx);
3239
Johannes Berg66e67e42012-01-20 13:55:27 +01003240 assoc_data->tries++;
3241 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003242 sdata_info(sdata, "association with %pM timed out\n",
3243 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003244
3245 /*
3246 * Most likely AP is not in the range so remove the
3247 * bss struct for that AP.
3248 */
3249 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3250
3251 return -ETIMEDOUT;
3252 }
3253
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003254 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3255 assoc_data->bss->bssid, assoc_data->tries,
3256 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003257 ieee80211_send_assoc(sdata);
3258
Johannes Berg1672c0e2013-01-29 15:02:27 +01003259 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3260 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003261 assoc_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003262 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003263 } else {
3264 assoc_data->timeout_started = false;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003265 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003266
3267 return 0;
3268}
3269
Johannes Berg1672c0e2013-01-29 15:02:27 +01003270void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3271 __le16 fc, bool acked)
3272{
3273 struct ieee80211_local *local = sdata->local;
3274
3275 sdata->u.mgd.status_fc = fc;
3276 sdata->u.mgd.status_acked = acked;
3277 sdata->u.mgd.status_received = true;
3278
3279 ieee80211_queue_work(&local->hw, &sdata->work);
3280}
3281
Johannes Berg1fa57d02010-06-10 10:21:32 +02003282void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003283{
Johannes Berg60f8b392008-09-08 17:44:22 +02003284 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003285 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003286
Johannes Berg77fdaa12009-07-07 03:45:17 +02003287 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003288
Johannes Berg1672c0e2013-01-29 15:02:27 +01003289 if (ifmgd->status_received) {
3290 __le16 fc = ifmgd->status_fc;
3291 bool status_acked = ifmgd->status_acked;
3292
3293 ifmgd->status_received = false;
3294 if (ifmgd->auth_data &&
3295 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3296 if (status_acked) {
3297 ifmgd->auth_data->timeout =
3298 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3299 run_again(ifmgd, ifmgd->auth_data->timeout);
3300 } else {
3301 ifmgd->auth_data->timeout = jiffies - 1;
3302 }
Johannes Berg89afe612013-02-13 15:39:57 +01003303 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003304 } else if (ifmgd->assoc_data &&
3305 (ieee80211_is_assoc_req(fc) ||
3306 ieee80211_is_reassoc_req(fc))) {
3307 if (status_acked) {
3308 ifmgd->assoc_data->timeout =
3309 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3310 run_again(ifmgd, ifmgd->assoc_data->timeout);
3311 } else {
3312 ifmgd->assoc_data->timeout = jiffies - 1;
3313 }
Johannes Berg89afe612013-02-13 15:39:57 +01003314 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003315 }
3316 }
3317
Johannes Berg89afe612013-02-13 15:39:57 +01003318 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003319 time_after(jiffies, ifmgd->auth_data->timeout)) {
3320 if (ifmgd->auth_data->done) {
3321 /*
3322 * ok ... we waited for assoc but userspace didn't,
3323 * so let's just kill the auth data
3324 */
3325 ieee80211_destroy_auth_data(sdata, false);
3326 } else if (ieee80211_probe_auth(sdata)) {
3327 u8 bssid[ETH_ALEN];
3328
3329 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3330
3331 ieee80211_destroy_auth_data(sdata, false);
3332
3333 mutex_unlock(&ifmgd->mtx);
3334 cfg80211_send_auth_timeout(sdata->dev, bssid);
3335 mutex_lock(&ifmgd->mtx);
3336 }
Johannes Berg89afe612013-02-13 15:39:57 +01003337 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003338 run_again(ifmgd, ifmgd->auth_data->timeout);
3339
Johannes Berg89afe612013-02-13 15:39:57 +01003340 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003341 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003342 if ((ifmgd->assoc_data->need_beacon &&
3343 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003344 ieee80211_do_assoc(sdata)) {
3345 u8 bssid[ETH_ALEN];
3346
3347 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3348
3349 ieee80211_destroy_assoc_data(sdata, false);
3350
3351 mutex_unlock(&ifmgd->mtx);
3352 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3353 mutex_lock(&ifmgd->mtx);
3354 }
Johannes Berg89afe612013-02-13 15:39:57 +01003355 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003356 run_again(ifmgd, ifmgd->assoc_data->timeout);
3357
Johannes Bergb291ba12009-07-10 15:29:03 +02003358 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3359 IEEE80211_STA_CONNECTION_POLL) &&
3360 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003361 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003362 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003363
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003364 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003365
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003366 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003367 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003368 else
Ben Greear180205b2011-02-04 15:30:24 -08003369 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003370
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003371 /* ACK received for nullfunc probing frame */
3372 if (!ifmgd->probe_send_count)
3373 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003374 else if (ifmgd->nullfunc_failed) {
3375 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003376 mlme_dbg(sdata,
3377 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3378 bssid, ifmgd->probe_send_count,
3379 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003380 ieee80211_mgd_probe_ap_send(sdata);
3381 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003382 mlme_dbg(sdata,
3383 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3384 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003385 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003386 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3387 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003388 }
3389 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003390 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003391 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003392 mlme_dbg(sdata,
3393 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3394 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003395 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003396 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003397 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003398 mlme_dbg(sdata,
3399 "No probe response from AP %pM after %dms, try %d/%i\n",
3400 bssid, probe_wait_ms,
3401 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003402 ieee80211_mgd_probe_ap_send(sdata);
3403 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003404 /*
3405 * We actually lost the connection ... or did we?
3406 * Let's make sure!
3407 */
Ben Greearb38afa82010-10-07 16:12:06 -07003408 wiphy_debug(local->hw.wiphy,
3409 "%s: No probe response from AP %pM"
3410 " after %dms, disconnecting.\n",
3411 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003412 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003413
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);
Johannes Bergb291ba12009-07-10 15:29:03 +02003416 }
3417 }
3418
Johannes Berg77fdaa12009-07-07 03:45:17 +02003419 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003420}
Johannes Berg0a51b272008-09-08 17:44:25 +02003421
Johannes Bergb291ba12009-07-10 15:29:03 +02003422static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3423{
3424 struct ieee80211_sub_if_data *sdata =
3425 (struct ieee80211_sub_if_data *) data;
3426 struct ieee80211_local *local = sdata->local;
3427
3428 if (local->quiescing)
3429 return;
3430
Johannes Berg682bd382013-01-29 13:13:50 +01003431 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003432 ieee80211_queue_work(&sdata->local->hw,
3433 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003434}
3435
3436static void ieee80211_sta_conn_mon_timer(unsigned long data)
3437{
3438 struct ieee80211_sub_if_data *sdata =
3439 (struct ieee80211_sub_if_data *) data;
3440 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3441 struct ieee80211_local *local = sdata->local;
3442
3443 if (local->quiescing)
3444 return;
3445
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003446 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003447}
3448
3449static void ieee80211_sta_monitor_work(struct work_struct *work)
3450{
3451 struct ieee80211_sub_if_data *sdata =
3452 container_of(work, struct ieee80211_sub_if_data,
3453 u.mgd.monitor_work);
3454
3455 ieee80211_mgd_probe_ap(sdata, false);
3456}
3457
Johannes Berga1678f82008-09-11 00:01:47 +02003458static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3459{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003460 u32 flags;
3461
Kalle Vaload935682009-04-19 08:47:19 +03003462 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003463 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003464
Johannes Bergb291ba12009-07-10 15:29:03 +02003465 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003466 flags = sdata->local->hw.flags;
3467 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3468 ieee80211_queue_work(&sdata->local->hw,
3469 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003470 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003471 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003472 }
Johannes Berga1678f82008-09-11 00:01:47 +02003473}
3474
Johannes Berg9c6bd792008-09-11 00:01:52 +02003475/* interface setup */
3476void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3477{
Johannes Berg46900292009-02-15 12:44:28 +01003478 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003479
Johannes Berg46900292009-02-15 12:44:28 +01003480 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003481 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003482 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003483 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3484 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003485 INIT_WORK(&ifmgd->csa_connection_drop_work,
3486 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003487 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003488 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003489 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003490 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3491 (unsigned long) sdata);
3492 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3493 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003494 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303495 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003496
Johannes Bergab1faea2009-07-01 21:41:17 +02003497 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303498 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003499 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3500 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003501 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003502
Johannes Berg77fdaa12009-07-07 03:45:17 +02003503 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003504
3505 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3506 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3507 else
3508 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003509}
3510
3511/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003512void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3513{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003514 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003515
3516 /* Restart STA timers */
3517 rcu_read_lock();
3518 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3519 ieee80211_restart_sta_timer(sdata);
3520 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003521}
Johannes Berg10f644a2009-04-16 13:17:25 +02003522
3523int ieee80211_max_network_latency(struct notifier_block *nb,
3524 unsigned long data, void *dummy)
3525{
3526 s32 latency_usec = (s32) data;
3527 struct ieee80211_local *local =
3528 container_of(nb, struct ieee80211_local,
3529 network_latency_notifier);
3530
3531 mutex_lock(&local->iflist_mtx);
3532 ieee80211_recalc_ps(local, latency_usec);
3533 mutex_unlock(&local->iflist_mtx);
3534
3535 return 0;
3536}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003537
Johannes Bergf2d9d272012-11-22 14:11:39 +01003538static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3539 struct cfg80211_bss *cbss)
3540{
3541 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3542 const u8 *ht_cap_ie, *vht_cap_ie;
3543 const struct ieee80211_ht_cap *ht_cap;
3544 const struct ieee80211_vht_cap *vht_cap;
3545 u8 chains = 1;
3546
3547 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3548 return chains;
3549
Johannes Berg9caf0362012-11-29 01:25:20 +01003550 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003551 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3552 ht_cap = (void *)(ht_cap_ie + 2);
3553 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3554 /*
3555 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3556 * "Tx Unequal Modulation Supported" fields.
3557 */
3558 }
3559
3560 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3561 return chains;
3562
Johannes Berg9caf0362012-11-29 01:25:20 +01003563 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003564 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3565 u8 nss;
3566 u16 tx_mcs_map;
3567
3568 vht_cap = (void *)(vht_cap_ie + 2);
3569 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3570 for (nss = 8; nss > 0; nss--) {
3571 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3572 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3573 break;
3574 }
3575 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3576 chains = max(chains, nss);
3577 }
3578
3579 return chains;
3580}
3581
Johannes Bergb17166a2012-07-27 11:41:27 +02003582static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3583 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003584{
3585 struct ieee80211_local *local = sdata->local;
3586 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003587 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003588 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003589 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003590 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003591 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003592
Johannes Berg24398e32012-03-28 10:58:36 +02003593 sband = local->hw.wiphy->bands[cbss->channel->band];
3594
Johannes Bergf2d9d272012-11-22 14:11:39 +01003595 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3596 IEEE80211_STA_DISABLE_80P80MHZ |
3597 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003598
Johannes Berg9caf0362012-11-29 01:25:20 +01003599 rcu_read_lock();
3600
Johannes Bergf2d9d272012-11-22 14:11:39 +01003601 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3602 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003603 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003604
Johannes Berg9caf0362012-11-29 01:25:20 +01003605 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003606 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3607 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003608
3609 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3610 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3611 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3612 ht_oper = NULL;
3613 }
Johannes Berg24398e32012-03-28 10:58:36 +02003614 }
3615
Johannes Bergf2d9d272012-11-22 14:11:39 +01003616 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3617 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003618 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003619
Johannes Berg9caf0362012-11-29 01:25:20 +01003620 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3621 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003622 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3623 vht_oper = (void *)(vht_oper_ie + 2);
3624 if (vht_oper && !ht_oper) {
3625 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003626 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003627 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003628 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3629 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003630 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003631
3632 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3633 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3634 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3635 vht_oper = NULL;
3636 }
Johannes Berg24398e32012-03-28 10:58:36 +02003637 }
3638
Johannes Bergf2d9d272012-11-22 14:11:39 +01003639 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3640 cbss->channel,
3641 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003642 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003643
Johannes Bergf2d9d272012-11-22 14:11:39 +01003644 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3645 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003646
Johannes Berg9caf0362012-11-29 01:25:20 +01003647 rcu_read_unlock();
3648
Johannes Berg04ecd252012-09-11 14:34:12 +02003649 /* will change later if needed */
3650 sdata->smps_mode = IEEE80211_SMPS_OFF;
3651
Johannes Bergf2d9d272012-11-22 14:11:39 +01003652 /*
3653 * If this fails (possibly due to channel context sharing
3654 * on incompatible channels, e.g. 80+80 and 160 sharing the
3655 * same control channel) try to use a smaller bandwidth.
3656 */
3657 ret = ieee80211_vif_use_channel(sdata, &chandef,
3658 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003659 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003660 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003661 ret = ieee80211_vif_use_channel(sdata, &chandef,
3662 IEEE80211_CHANCTX_SHARED);
3663 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003664 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003665}
3666
3667static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3668 struct cfg80211_bss *cbss, bool assoc)
3669{
3670 struct ieee80211_local *local = sdata->local;
3671 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3672 struct ieee80211_bss *bss = (void *)cbss->priv;
3673 struct sta_info *new_sta = NULL;
3674 bool have_sta = false;
3675 int err;
3676
3677 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3678 return -EINVAL;
3679
3680 if (assoc) {
3681 rcu_read_lock();
3682 have_sta = sta_info_get(sdata, cbss->bssid);
3683 rcu_read_unlock();
3684 }
3685
3686 if (!have_sta) {
3687 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3688 if (!new_sta)
3689 return -ENOMEM;
3690 }
3691
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003692 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003693 u32 rates = 0, basic_rates = 0;
3694 bool have_higher_than_11mbit;
3695 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003696 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003697 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003698
3699 sband = local->hw.wiphy->bands[cbss->channel->band];
3700
3701 err = ieee80211_prep_channel(sdata, cbss);
3702 if (err) {
3703 sta_info_free(local, new_sta);
3704 return err;
3705 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003706
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003707 ieee80211_get_rates(sband, bss->supp_rates,
3708 bss->supp_rates_len,
3709 &rates, &basic_rates,
3710 &have_higher_than_11mbit,
3711 &min_rate, &min_rate_index);
3712
3713 /*
3714 * This used to be a workaround for basic rates missing
3715 * in the association response frame. Now that we no
3716 * longer use the basic rates from there, it probably
3717 * doesn't happen any more, but keep the workaround so
3718 * in case some *other* APs are buggy in different ways
3719 * we can connect -- with a warning.
3720 */
3721 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003722 sdata_info(sdata,
3723 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003724 basic_rates = BIT(min_rate_index);
3725 }
3726
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003727 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003728 sdata->vif.bss_conf.basic_rates = basic_rates;
3729
3730 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003731 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003732 have_higher_than_11mbit)
3733 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3734 else
3735 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3736
3737 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3738
Johannes Berg8c358bc2012-05-22 22:13:05 +02003739 /* set timing information */
3740 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003741 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003742 ies = rcu_dereference(cbss->beacon_ies);
3743 if (ies) {
3744 const u8 *tim_ie;
3745
3746 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3747 sdata->vif.bss_conf.sync_device_ts =
3748 bss->device_ts_beacon;
3749 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3750 ies->data, ies->len);
3751 if (tim_ie && tim_ie[1] >= 2)
3752 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3753 else
3754 sdata->vif.bss_conf.sync_dtim_count = 0;
3755 } else if (!(local->hw.flags &
3756 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3757 ies = rcu_dereference(cbss->proberesp_ies);
3758 /* must be non-NULL since beacon IEs were NULL */
3759 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3760 sdata->vif.bss_conf.sync_device_ts =
3761 bss->device_ts_presp;
3762 sdata->vif.bss_conf.sync_dtim_count = 0;
3763 } else {
3764 sdata->vif.bss_conf.sync_tsf = 0;
3765 sdata->vif.bss_conf.sync_device_ts = 0;
3766 sdata->vif.bss_conf.sync_dtim_count = 0;
3767 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003768 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003769
3770 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003771 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003772 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3773 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003774
3775 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003776 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003777
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003778 err = sta_info_insert(new_sta);
3779 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003780 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003781 sdata_info(sdata,
3782 "failed to insert STA entry for the AP (error %d)\n",
3783 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003784 return err;
3785 }
3786 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003787 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003788
3789 return 0;
3790}
3791
Johannes Berg77fdaa12009-07-07 03:45:17 +02003792/* config hooks */
3793int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3794 struct cfg80211_auth_request *req)
3795{
Johannes Berg66e67e42012-01-20 13:55:27 +01003796 struct ieee80211_local *local = sdata->local;
3797 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3798 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003799 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003800 int err;
3801
3802 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003803
3804 switch (req->auth_type) {
3805 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3806 auth_alg = WLAN_AUTH_OPEN;
3807 break;
3808 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003809 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003810 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003811 auth_alg = WLAN_AUTH_SHARED_KEY;
3812 break;
3813 case NL80211_AUTHTYPE_FT:
3814 auth_alg = WLAN_AUTH_FT;
3815 break;
3816 case NL80211_AUTHTYPE_NETWORK_EAP:
3817 auth_alg = WLAN_AUTH_LEAP;
3818 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003819 case NL80211_AUTHTYPE_SAE:
3820 auth_alg = WLAN_AUTH_SAE;
3821 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003822 default:
3823 return -EOPNOTSUPP;
3824 }
3825
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003826 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3827 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003828 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003829 return -ENOMEM;
3830
Johannes Berg66e67e42012-01-20 13:55:27 +01003831 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003832
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003833 if (req->sae_data_len >= 4) {
3834 __le16 *pos = (__le16 *) req->sae_data;
3835 auth_data->sae_trans = le16_to_cpu(pos[0]);
3836 auth_data->sae_status = le16_to_cpu(pos[1]);
3837 memcpy(auth_data->data, req->sae_data + 4,
3838 req->sae_data_len - 4);
3839 auth_data->data_len += req->sae_data_len - 4;
3840 }
3841
Johannes Berg77fdaa12009-07-07 03:45:17 +02003842 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003843 memcpy(&auth_data->data[auth_data->data_len],
3844 req->ie, req->ie_len);
3845 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003846 }
3847
Johannes Bergfffd0932009-07-08 14:22:54 +02003848 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003849 auth_data->key_len = req->key_len;
3850 auth_data->key_idx = req->key_idx;
3851 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003852 }
3853
Johannes Berg66e67e42012-01-20 13:55:27 +01003854 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003855
Johannes Berg66e67e42012-01-20 13:55:27 +01003856 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003857
Johannes Berg66e67e42012-01-20 13:55:27 +01003858 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003859
Johannes Berg66e67e42012-01-20 13:55:27 +01003860 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3861 ifmgd->assoc_data) {
3862 err = -EBUSY;
3863 goto err_free;
3864 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003865
Johannes Berg66e67e42012-01-20 13:55:27 +01003866 if (ifmgd->auth_data)
3867 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003868
Johannes Berg66e67e42012-01-20 13:55:27 +01003869 /* prep auth_data so we don't go into idle on disassoc */
3870 ifmgd->auth_data = auth_data;
3871
3872 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003873 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003874
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003875 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003876
Johannes Berga1cf7752012-03-08 15:02:07 +01003877 err = ieee80211_prep_connection(sdata, req->bss, false);
3878 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003879 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003880
Johannes Berg66e67e42012-01-20 13:55:27 +01003881 err = ieee80211_probe_auth(sdata);
3882 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003883 sta_info_destroy_addr(sdata, req->bss->bssid);
3884 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003885 }
3886
Johannes Berg66e67e42012-01-20 13:55:27 +01003887 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003888 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003889 err = 0;
3890 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003891
Johannes Berg66e67e42012-01-20 13:55:27 +01003892 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003893 memset(ifmgd->bssid, 0, ETH_ALEN);
3894 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003895 ifmgd->auth_data = NULL;
3896 err_free:
3897 kfree(auth_data);
3898 out_unlock:
3899 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003900
Johannes Berg66e67e42012-01-20 13:55:27 +01003901 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003902}
3903
Johannes Berg77fdaa12009-07-07 03:45:17 +02003904int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3905 struct cfg80211_assoc_request *req)
3906{
Johannes Berg66e67e42012-01-20 13:55:27 +01003907 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003908 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003909 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003910 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003911 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003912 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003913 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003914 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003915
Johannes Berg66e67e42012-01-20 13:55:27 +01003916 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3917 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003918 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003919
Johannes Berg9caf0362012-11-29 01:25:20 +01003920 rcu_read_lock();
3921 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3922 if (!ssidie) {
3923 rcu_read_unlock();
3924 kfree(assoc_data);
3925 return -EINVAL;
3926 }
3927 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3928 assoc_data->ssid_len = ssidie[1];
3929 rcu_read_unlock();
3930
Johannes Berg66e67e42012-01-20 13:55:27 +01003931 mutex_lock(&ifmgd->mtx);
3932
3933 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003934 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003935
3936 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3937 err = -EBUSY;
3938 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003939 }
3940
Johannes Berg66e67e42012-01-20 13:55:27 +01003941 if (ifmgd->assoc_data) {
3942 err = -EBUSY;
3943 goto err_free;
3944 }
3945
3946 if (ifmgd->auth_data) {
3947 bool match;
3948
3949 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003950 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003951 ieee80211_destroy_auth_data(sdata, match);
3952 }
3953
3954 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003955
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003956 ifmgd->beacon_crc_valid = false;
3957
Johannes Bergde5036a2012-03-08 15:02:03 +01003958 /*
3959 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3960 * We still associate in non-HT mode (11a/b/g) if any one of these
3961 * ciphers is configured as pairwise.
3962 * We can set this to true for non-11n hardware, that'll be checked
3963 * separately along with the peer capabilities.
3964 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003965 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003966 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3967 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003968 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003969 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003970 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003971 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003972 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003973 }
3974 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003975
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003976 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003977 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003978 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3979 }
Ben Greearef96a8422011-11-18 11:32:00 -08003980
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01003981 if (req->flags & ASSOC_REQ_DISABLE_VHT)
3982 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3983
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003984 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3985 sband = local->hw.wiphy->bands[req->bss->channel->band];
3986 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003987 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003988 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003989 if (!bss->wmm_used)
3990 netdev_info(sdata->dev,
3991 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003992 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003993
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003994 /* disable VHT if we don't support it or the AP doesn't use WMM */
3995 if (!sband->vht_cap.vht_supported ||
3996 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3997 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003998 if (!bss->wmm_used)
3999 netdev_info(sdata->dev,
4000 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004001 }
4002
Ben Greearef96a8422011-11-18 11:32:00 -08004003 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4004 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4005 sizeof(ifmgd->ht_capa_mask));
4006
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004007 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4008 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4009 sizeof(ifmgd->vht_capa_mask));
4010
Johannes Berg77fdaa12009-07-07 03:45:17 +02004011 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004012 memcpy(assoc_data->ie, req->ie, req->ie_len);
4013 assoc_data->ie_len = req->ie_len;
4014 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004015
Johannes Berg66e67e42012-01-20 13:55:27 +01004016 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004017
Johannes Bergaf6b6372009-12-23 13:15:35 +01004018 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4019 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004020 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004021 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004022 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004023 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004024 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004025
Johannes Berg66e67e42012-01-20 13:55:27 +01004026 assoc_data->capability = req->bss->capability;
Johannes Berg32c5057b2012-03-28 11:04:29 +02004027 assoc_data->wmm = bss->wmm_used &&
4028 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004029 assoc_data->supp_rates = bss->supp_rates;
4030 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004031
Johannes Berg9caf0362012-11-29 01:25:20 +01004032 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004033 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4034 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4035 assoc_data->ap_ht_param =
4036 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4037 else
Johannes Berga8243b72012-11-22 14:32:09 +01004038 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004039 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4040 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4041 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4042 sizeof(struct ieee80211_vht_cap));
4043 else
4044 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004045 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004046
Kalle Valoab133152010-01-12 10:42:31 +02004047 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004048 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4049 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004050 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004051 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4052 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004053 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004054 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4055 }
4056
Johannes Berg77fdaa12009-07-07 03:45:17 +02004057 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004058 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004059
4060 if (req->use_mfp) {
4061 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4062 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4063 } else {
4064 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4065 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4066 }
4067
4068 if (req->crypto.control_port)
4069 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4070 else
4071 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4072
Johannes Berga621fa42010-08-27 14:26:54 +03004073 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4074 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4075
Johannes Berg66e67e42012-01-20 13:55:27 +01004076 /* kick off associate process */
4077
4078 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004079 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004080
Johannes Berga1cf7752012-03-08 15:02:07 +01004081 err = ieee80211_prep_connection(sdata, req->bss, true);
4082 if (err)
4083 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004084
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004085 rcu_read_lock();
4086 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004087
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004088 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4089 !beacon_ies) {
4090 /*
4091 * Wait up to one beacon interval ...
4092 * should this be more if we miss one?
4093 */
4094 sdata_info(sdata, "waiting for beacon from %pM\n",
4095 ifmgd->bssid);
4096 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004097 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004098 assoc_data->need_beacon = true;
4099 } else if (beacon_ies) {
4100 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4101 beacon_ies->data,
4102 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004103 u8 dtim_count = 0;
4104
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004105 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4106 const struct ieee80211_tim_ie *tim;
4107 tim = (void *)(tim_ie + 2);
4108 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004109 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004110 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004111 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004112 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004113 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004114
4115 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4116 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4117 sdata->vif.bss_conf.sync_device_ts =
4118 bss->device_ts_beacon;
4119 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4120 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004121 } else {
4122 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004123 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004124 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004125 rcu_read_unlock();
4126
Johannes Berg66e67e42012-01-20 13:55:27 +01004127 run_again(ifmgd, assoc_data->timeout);
4128
Paul Stewartfcff4f12012-02-23 17:59:53 -08004129 if (bss->corrupt_data) {
4130 char *corrupt_type = "data";
4131 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4132 if (bss->corrupt_data &
4133 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4134 corrupt_type = "beacon and probe response";
4135 else
4136 corrupt_type = "beacon";
4137 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4138 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004139 sdata_info(sdata, "associating with AP with corrupt %s\n",
4140 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004141 }
4142
Johannes Berg66e67e42012-01-20 13:55:27 +01004143 err = 0;
4144 goto out;
4145 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004146 memset(ifmgd->bssid, 0, ETH_ALEN);
4147 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004148 ifmgd->assoc_data = NULL;
4149 err_free:
4150 kfree(assoc_data);
4151 out:
4152 mutex_unlock(&ifmgd->mtx);
4153
4154 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004155}
4156
4157int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004158 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004159{
4160 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004161 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004162 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004163 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004164
Johannes Berg77fdaa12009-07-07 03:45:17 +02004165 mutex_lock(&ifmgd->mtx);
4166
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004167 sdata_info(sdata,
4168 "deauthenticating from %pM by local choice (reason=%d)\n",
4169 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004170
Johannes Berg86552012012-10-29 09:46:31 +01004171 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004172 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004173 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4174 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004175 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004176 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004177 ieee80211_destroy_auth_data(sdata, false);
4178 mutex_unlock(&ifmgd->mtx);
4179
4180 sent_frame = tx;
4181 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004182 }
4183
Johannes Berg86552012012-10-29 09:46:31 +01004184 if (ifmgd->associated &&
4185 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4186 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4187 req->reason_code, tx, frame_buf);
4188 sent_frame = tx;
4189 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004190 mutex_unlock(&ifmgd->mtx);
4191
Johannes Berg86552012012-10-29 09:46:31 +01004192 out:
Johannes Berg86552012012-10-29 09:46:31 +01004193 if (sent_frame)
4194 __cfg80211_send_deauth(sdata->dev, frame_buf,
4195 IEEE80211_DEAUTH_FRAME_LEN);
4196
Johannes Berg77fdaa12009-07-07 03:45:17 +02004197 return 0;
4198}
4199
4200int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004201 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004202{
4203 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004204 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004205 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004206
Johannes Berg77fdaa12009-07-07 03:45:17 +02004207 mutex_lock(&ifmgd->mtx);
4208
Johannes Berg8d8b2612009-07-25 11:58:36 +02004209 /*
4210 * cfg80211 should catch this ... but it's racy since
4211 * we can receive a disassoc frame, process it, hand it
4212 * to cfg80211 while that's in a locked section already
4213 * trying to tell us that the user wants to disconnect.
4214 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004215 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004216 mutex_unlock(&ifmgd->mtx);
4217 return -ENOLINK;
4218 }
4219
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004220 sdata_info(sdata,
4221 "disassociating from %pM by local choice (reason=%d)\n",
4222 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004223
Jouni Malinene69e95d2010-03-29 23:29:31 -07004224 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004225 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4226 req->reason_code, !req->local_state_change,
4227 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004228 mutex_unlock(&ifmgd->mtx);
4229
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004230 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4231 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004232
Johannes Berg77fdaa12009-07-07 03:45:17 +02004233 return 0;
4234}
Jouni Malinen026331c2010-02-15 12:53:10 +02004235
Eliad Pellerafa762f2012-04-23 14:45:15 +03004236void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004237{
4238 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4239
Ben Greear49921852013-02-20 09:41:09 -08004240 /*
4241 * Make sure some work items will not run after this,
4242 * they will not do anything but might not have been
4243 * cancelled when disconnecting.
4244 */
4245 cancel_work_sync(&ifmgd->monitor_work);
4246 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4247 cancel_work_sync(&ifmgd->request_smps_work);
4248 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4249 cancel_work_sync(&ifmgd->chswitch_work);
4250
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004251 mutex_lock(&ifmgd->mtx);
4252 if (ifmgd->assoc_data)
4253 ieee80211_destroy_assoc_data(sdata, false);
4254 if (ifmgd->auth_data)
4255 ieee80211_destroy_auth_data(sdata, false);
4256 del_timer_sync(&ifmgd->timer);
4257 mutex_unlock(&ifmgd->mtx);
4258}
4259
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004260void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4261 enum nl80211_cqm_rssi_threshold_event rssi_event,
4262 gfp_t gfp)
4263{
4264 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4265
Johannes Bergb5878a22010-04-07 16:48:40 +02004266 trace_api_cqm_rssi_notify(sdata, rssi_event);
4267
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004268 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4269}
4270EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);