blob: 50423c66c239f66dc4779e6c9db3a36c3e02f9bd [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 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010059#define IEEE80211_BEACON_LOSS_COUNT 7
60
Johannes Bergb291ba12009-07-10 15:29:03 +020061/*
62 * Time the connection can be idle before we probe
63 * it to see if we can still talk to the AP.
64 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030065#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time we wait for a probe response after sending
68 * a probe request because of beacon loss or for
69 * checking the connection still works.
70 */
Ben Greear180205b2011-02-04 15:30:24 -080071static int probe_wait_ms = 500;
72module_param(probe_wait_ms, int, 0644);
73MODULE_PARM_DESC(probe_wait_ms,
74 "Maximum time(ms) to wait for probe response"
75 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jouni Malinen17e4ec12010-03-29 23:28:30 -070077/*
78 * Weight given to the latest Beacon frame when calculating average signal
79 * strength for Beacon frames received in the current BSS. This must be
80 * between 1 and 15.
81 */
82#define IEEE80211_SIGNAL_AVE_WEIGHT 3
83
Jouni Malinen391a2002010-08-27 22:22:00 +030084/*
85 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg5bb644a2009-05-17 11:40:42 +020090#define TMR_RUNNING_TIMER 0
91#define TMR_RUNNING_CHANSW 1
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200117};
118
Johannes Berg5484e232008-09-08 17:44:27 +0200119/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200122 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123}
124
Johannes Bergca386f32009-07-10 02:39:48 +0200125/*
126 * We can have multiple work items (and connection probing)
127 * scheduling this timer, but we need to take care to only
128 * reschedule it when it should fire _earlier_ than it was
129 * asked for before, or if it's not pending right now. This
130 * function ensures that. Note that it then is required to
131 * run this function for all timeouts after the first one
132 * has happened -- the work that runs from this timer will
133 * do that.
134 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100135static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200136{
137 ASSERT_MGD_MTX(ifmgd);
138
139 if (!timer_pending(&ifmgd->timer) ||
140 time_before(timeout, ifmgd->timer.expires))
141 mod_timer(&ifmgd->timer, timeout);
142}
143
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400144void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145{
Johannes Bergc1288b12012-01-19 09:29:57 +0100146 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200147 return;
148
Johannes Berg7eeff742012-07-18 10:27:27 +0200149 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
150 return;
151
Johannes Bergb291ba12009-07-10 15:29:03 +0200152 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100153 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200154}
155
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400156void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
157{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400158 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
159
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100160 if (unlikely(!sdata->u.mgd.associated))
161 return;
162
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400163 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
164 return;
165
166 mod_timer(&sdata->u.mgd.conn_mon_timer,
167 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400168
169 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400170}
171
Johannes Berg5484e232008-09-08 17:44:27 +0200172static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200173{
Johannes Berg5484e232008-09-08 17:44:27 +0200174 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200175}
176
Johannes Berg6565ec92013-02-08 14:52:32 +0100177static u32 chandef_downgrade(struct cfg80211_chan_def *c)
178{
179 u32 ret;
180 int tmp;
181
182 switch (c->width) {
183 case NL80211_CHAN_WIDTH_20:
184 c->width = NL80211_CHAN_WIDTH_20_NOHT;
185 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
186 break;
187 case NL80211_CHAN_WIDTH_40:
188 c->width = NL80211_CHAN_WIDTH_20;
189 c->center_freq1 = c->chan->center_freq;
190 ret = IEEE80211_STA_DISABLE_40MHZ |
191 IEEE80211_STA_DISABLE_VHT;
192 break;
193 case NL80211_CHAN_WIDTH_80:
194 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
195 /* n_P40 */
196 tmp /= 2;
197 /* freq_P40 */
198 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
199 c->width = NL80211_CHAN_WIDTH_40;
200 ret = IEEE80211_STA_DISABLE_VHT;
201 break;
202 case NL80211_CHAN_WIDTH_80P80:
203 c->center_freq2 = 0;
204 c->width = NL80211_CHAN_WIDTH_80;
205 ret = IEEE80211_STA_DISABLE_80P80MHZ |
206 IEEE80211_STA_DISABLE_160MHZ;
207 break;
208 case NL80211_CHAN_WIDTH_160:
209 /* n_P20 */
210 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
211 /* n_P80 */
212 tmp /= 4;
213 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
214 c->width = NL80211_CHAN_WIDTH_80;
215 ret = IEEE80211_STA_DISABLE_80P80MHZ |
216 IEEE80211_STA_DISABLE_160MHZ;
217 break;
218 default:
219 case NL80211_CHAN_WIDTH_20_NOHT:
220 WARN_ON_ONCE(1);
221 c->width = NL80211_CHAN_WIDTH_20_NOHT;
222 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
223 break;
224 }
225
226 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
227
228 return ret;
229}
230
231static u32
232ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
233 struct ieee80211_supported_band *sband,
234 struct ieee80211_channel *channel,
235 const struct ieee80211_ht_operation *ht_oper,
236 const struct ieee80211_vht_operation *vht_oper,
237 struct cfg80211_chan_def *chandef)
238{
239 struct cfg80211_chan_def vht_chandef;
240 u32 ht_cfreq, ret;
241
242 chandef->chan = channel;
243 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
244 chandef->center_freq1 = channel->center_freq;
245 chandef->center_freq2 = 0;
246
247 if (!ht_oper || !sband->ht_cap.ht_supported) {
248 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
249 goto out;
250 }
251
252 chandef->width = NL80211_CHAN_WIDTH_20;
253
254 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
255 channel->band);
256 /* check that channel matches the right operating channel */
257 if (channel->center_freq != ht_cfreq) {
258 /*
259 * It's possible that some APs are confused here;
260 * Netgear WNDR3700 sometimes reports 4 higher than
261 * the actual channel in association responses, but
262 * since we look at probe response/beacon data here
263 * it should be OK.
264 */
265 sdata_info(sdata,
266 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
267 channel->center_freq, ht_cfreq,
268 ht_oper->primary_chan, channel->band);
269 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
270 goto out;
271 }
272
273 /* check 40 MHz support, if we have it */
274 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
275 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
276 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
277 chandef->width = NL80211_CHAN_WIDTH_40;
278 chandef->center_freq1 += 10;
279 break;
280 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
281 chandef->width = NL80211_CHAN_WIDTH_40;
282 chandef->center_freq1 -= 10;
283 break;
284 }
285 } else {
286 /* 40 MHz (and 80 MHz) must be supported for VHT */
287 ret = IEEE80211_STA_DISABLE_VHT;
288 goto out;
289 }
290
291 if (!vht_oper || !sband->vht_cap.vht_supported) {
292 ret = IEEE80211_STA_DISABLE_VHT;
293 goto out;
294 }
295
296 vht_chandef.chan = channel;
297 vht_chandef.center_freq1 =
298 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
299 channel->band);
300 vht_chandef.center_freq2 = 0;
301
302 if (vht_oper->center_freq_seg2_idx)
303 vht_chandef.center_freq2 =
304 ieee80211_channel_to_frequency(
305 vht_oper->center_freq_seg2_idx,
306 channel->band);
307
308 switch (vht_oper->chan_width) {
309 case IEEE80211_VHT_CHANWIDTH_USE_HT:
310 vht_chandef.width = chandef->width;
311 break;
312 case IEEE80211_VHT_CHANWIDTH_80MHZ:
313 vht_chandef.width = NL80211_CHAN_WIDTH_80;
314 break;
315 case IEEE80211_VHT_CHANWIDTH_160MHZ:
316 vht_chandef.width = NL80211_CHAN_WIDTH_160;
317 break;
318 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
319 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
320 break;
321 default:
322 sdata_info(sdata,
323 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
324 vht_oper->chan_width);
325 ret = IEEE80211_STA_DISABLE_VHT;
326 goto out;
327 }
328
329 if (!cfg80211_chandef_valid(&vht_chandef)) {
330 sdata_info(sdata,
331 "AP VHT information is invalid, disable VHT\n");
332 ret = IEEE80211_STA_DISABLE_VHT;
333 goto out;
334 }
335
336 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
337 ret = 0;
338 goto out;
339 }
340
341 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
342 sdata_info(sdata,
343 "AP VHT information doesn't match HT, disable VHT\n");
344 ret = IEEE80211_STA_DISABLE_VHT;
345 goto out;
346 }
347
348 *chandef = vht_chandef;
349
350 ret = 0;
351
352out:
353 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
354 IEEE80211_CHAN_DISABLED)) {
355 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
356 ret = IEEE80211_STA_DISABLE_HT |
357 IEEE80211_STA_DISABLE_VHT;
358 goto out;
359 }
360
361 ret |= chandef_downgrade(chandef);
362 }
363
364 if (chandef->width != vht_chandef.width)
365 sdata_info(sdata,
366 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
367
368 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
369 return ret;
370}
371
Johannes Berg24398e32012-03-28 10:58:36 +0200372static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg11289582013-02-07 17:36:12 +0100373 struct sta_info *sta,
Johannes Berg24398e32012-03-28 10:58:36 +0200374 struct ieee80211_ht_operation *ht_oper,
375 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200376{
377 struct ieee80211_local *local = sdata->local;
378 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200379 struct ieee80211_channel *chan;
Johannes Bergd5522e02009-03-30 13:23:35 +0200380 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200381 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200382 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200383
Johannes Berg11289582013-02-07 17:36:12 +0100384 if (WARN_ON_ONCE(!sta))
385 return 0;
386
Johannes Bergf2d93302013-02-08 13:15:55 +0100387 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200388 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200389
Johannes Berg4bf88532012-11-09 11:39:59 +0100390 switch (sdata->vif.bss_conf.chandef.width) {
391 case NL80211_CHAN_WIDTH_40:
Johannes Bergf2d93302013-02-08 13:15:55 +0100392 if (chan->center_freq >
Johannes Berg4bf88532012-11-09 11:39:59 +0100393 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100394 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200395 disable_40 = true;
Johannes Bergf2d93302013-02-08 13:15:55 +0100396 if (chan->center_freq <
Johannes Berg4bf88532012-11-09 11:39:59 +0100397 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100398 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200399 disable_40 = true;
400 break;
401 default:
402 break;
403 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200404
Johannes Berg24398e32012-03-28 10:58:36 +0200405 /* This can change during the lifetime of the BSS */
406 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200407 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200408
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100409 if (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Berg64f68e52012-03-28 10:58:37 +0200410 disable_40 = true;
411
Johannes Berg11289582013-02-07 17:36:12 +0100412 if (disable_40 != (sta->sta.bandwidth < IEEE80211_STA_RX_BW_40)) {
Johannes Berg64f68e52012-03-28 10:58:37 +0200413 if (disable_40)
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100414 sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
Johannes Berg64f68e52012-03-28 10:58:37 +0200415 else
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100416 sta->sta.bandwidth = ieee80211_sta_cur_vht_bw(sta);
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530417
Johannes Berg11289582013-02-07 17:36:12 +0100418 if (reconfig)
419 rate_control_rate_update(local, sband, sta,
420 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200421 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200422
Johannes Berg074d46d2012-03-15 19:45:16 +0100423 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200424
425 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200426 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200427 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200428 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200429 }
430
431 return changed;
432}
433
Johannes Berg9c6bd792008-09-11 00:01:52 +0200434/* frame sending functions */
435
Johannes Berg66e67e42012-01-20 13:55:27 +0100436static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
437 struct ieee80211_supported_band *sband,
438 u32 *rates)
439{
440 int i, j, count;
441 *rates = 0;
442 count = 0;
443 for (i = 0; i < supp_rates_len; i++) {
444 int rate = (supp_rates[i] & 0x7F) * 5;
445
446 for (j = 0; j < sband->n_bitrates; j++)
447 if (sband->bitrates[j].bitrate == rate) {
448 *rates |= BIT(j);
449 count++;
450 break;
451 }
452 }
453
454 return count;
455}
456
457static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200458 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100459 struct ieee80211_supported_band *sband,
460 struct ieee80211_channel *channel,
461 enum ieee80211_smps_mode smps)
462{
Johannes Berg66e67e42012-01-20 13:55:27 +0100463 u8 *pos;
464 u32 flags = channel->flags;
465 u16 cap;
466 struct ieee80211_sta_ht_cap ht_cap;
467
468 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
469
Johannes Berg66e67e42012-01-20 13:55:27 +0100470 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
471 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
472
Johannes Berg66e67e42012-01-20 13:55:27 +0100473 /* determine capability flags */
474 cap = ht_cap.cap;
475
Johannes Berg9dde6422012-05-16 23:43:19 +0200476 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100477 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
478 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
479 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
480 cap &= ~IEEE80211_HT_CAP_SGI_40;
481 }
482 break;
483 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
484 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
485 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
486 cap &= ~IEEE80211_HT_CAP_SGI_40;
487 }
488 break;
489 }
490
Johannes Berg24398e32012-03-28 10:58:36 +0200491 /*
492 * If 40 MHz was disabled associate as though we weren't
493 * capable of 40 MHz -- some broken APs will never fall
494 * back to trying to transmit in 20 MHz.
495 */
496 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
497 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
498 cap &= ~IEEE80211_HT_CAP_SGI_40;
499 }
500
Johannes Berg66e67e42012-01-20 13:55:27 +0100501 /* set SM PS mode properly */
502 cap &= ~IEEE80211_HT_CAP_SM_PS;
503 switch (smps) {
504 case IEEE80211_SMPS_AUTOMATIC:
505 case IEEE80211_SMPS_NUM_MODES:
506 WARN_ON(1);
507 case IEEE80211_SMPS_OFF:
508 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
509 IEEE80211_HT_CAP_SM_PS_SHIFT;
510 break;
511 case IEEE80211_SMPS_STATIC:
512 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
513 IEEE80211_HT_CAP_SM_PS_SHIFT;
514 break;
515 case IEEE80211_SMPS_DYNAMIC:
516 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
517 IEEE80211_HT_CAP_SM_PS_SHIFT;
518 break;
519 }
520
521 /* reserve and fill IE */
522 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
523 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
524}
525
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000526static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
527 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100528 struct ieee80211_supported_band *sband,
529 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000530{
531 u8 *pos;
532 u32 cap;
533 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100534 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000535
536 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
537
538 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
539
540 /* determine capability flags */
541 cap = vht_cap.cap;
542
Johannes Bergf2d9d272012-11-22 14:11:39 +0100543 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
544 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
545 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
546 }
547
548 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
549 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
550 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
551 }
552
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100553 /*
554 * Some APs apparently get confused if our capabilities are better
555 * than theirs, so restrict what we advertise in the assoc request.
556 */
557 if (!(ap_vht_cap->vht_cap_info &
558 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
559 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
560
561 if (!(ap_vht_cap->vht_cap_info &
562 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
563 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
564 IEEE80211_VHT_CAP_RXSTBC_3 |
565 IEEE80211_VHT_CAP_RXSTBC_4);
566
567 for (i = 0; i < 8; i++) {
568 int shift = i * 2;
569 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
570 u16 ap_mcs, our_mcs;
571
572 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
573 mask) >> shift;
574 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
575 mask) >> shift;
576
577 switch (ap_mcs) {
578 default:
579 if (our_mcs <= ap_mcs)
580 break;
581 /* fall through */
582 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
583 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
584 vht_cap.vht_mcs.rx_mcs_map |=
585 cpu_to_le16(ap_mcs << shift);
586 }
587 }
588
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000589 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000590 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000591 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
592}
593
Johannes Berg66e67e42012-01-20 13:55:27 +0100594static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
595{
596 struct ieee80211_local *local = sdata->local;
597 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
598 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
599 struct sk_buff *skb;
600 struct ieee80211_mgmt *mgmt;
601 u8 *pos, qos_info;
602 size_t offset = 0, noffset;
603 int i, count, rates_len, supp_rates_len;
604 u16 capab;
605 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200606 struct ieee80211_chanctx_conf *chanctx_conf;
607 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100608 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100609
610 lockdep_assert_held(&ifmgd->mtx);
611
Johannes Berg55de9082012-07-26 17:24:39 +0200612 rcu_read_lock();
613 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
614 if (WARN_ON(!chanctx_conf)) {
615 rcu_read_unlock();
616 return;
617 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100618 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200619 rcu_read_unlock();
620 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100621
622 if (assoc_data->supp_rates_len) {
623 /*
624 * Get all rates supported by the device and the AP as
625 * some APs don't like getting a superset of their rates
626 * in the association request (e.g. D-Link DAP 1353 in
627 * b-only mode)...
628 */
629 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
630 assoc_data->supp_rates_len,
631 sband, &rates);
632 } else {
633 /*
634 * In case AP not provide any supported rates information
635 * before association, we send information element(s) with
636 * all rates that we support.
637 */
638 rates = ~0;
639 rates_len = sband->n_bitrates;
640 }
641
642 skb = alloc_skb(local->hw.extra_tx_headroom +
643 sizeof(*mgmt) + /* bit too much but doesn't matter */
644 2 + assoc_data->ssid_len + /* SSID */
645 4 + rates_len + /* (extended) rates */
646 4 + /* power capability */
647 2 + 2 * sband->n_channels + /* supported channels */
648 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000649 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100650 assoc_data->ie_len + /* extra IEs */
651 9, /* WMM */
652 GFP_KERNEL);
653 if (!skb)
654 return;
655
656 skb_reserve(skb, local->hw.extra_tx_headroom);
657
658 capab = WLAN_CAPABILITY_ESS;
659
660 if (sband->band == IEEE80211_BAND_2GHZ) {
661 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
662 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
663 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
664 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
665 }
666
667 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
668 capab |= WLAN_CAPABILITY_PRIVACY;
669
670 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
671 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
672 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
673
674 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
675 memset(mgmt, 0, 24);
676 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
677 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
678 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
679
680 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
681 skb_put(skb, 10);
682 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
683 IEEE80211_STYPE_REASSOC_REQ);
684 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
685 mgmt->u.reassoc_req.listen_interval =
686 cpu_to_le16(local->hw.conf.listen_interval);
687 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
688 ETH_ALEN);
689 } else {
690 skb_put(skb, 4);
691 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
692 IEEE80211_STYPE_ASSOC_REQ);
693 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
694 mgmt->u.assoc_req.listen_interval =
695 cpu_to_le16(local->hw.conf.listen_interval);
696 }
697
698 /* SSID */
699 pos = skb_put(skb, 2 + assoc_data->ssid_len);
700 *pos++ = WLAN_EID_SSID;
701 *pos++ = assoc_data->ssid_len;
702 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
703
704 /* add all rates which were marked to be used above */
705 supp_rates_len = rates_len;
706 if (supp_rates_len > 8)
707 supp_rates_len = 8;
708
709 pos = skb_put(skb, supp_rates_len + 2);
710 *pos++ = WLAN_EID_SUPP_RATES;
711 *pos++ = supp_rates_len;
712
713 count = 0;
714 for (i = 0; i < sband->n_bitrates; i++) {
715 if (BIT(i) & rates) {
716 int rate = sband->bitrates[i].bitrate;
717 *pos++ = (u8) (rate / 5);
718 if (++count == 8)
719 break;
720 }
721 }
722
723 if (rates_len > count) {
724 pos = skb_put(skb, rates_len - count + 2);
725 *pos++ = WLAN_EID_EXT_SUPP_RATES;
726 *pos++ = rates_len - count;
727
728 for (i++; i < sband->n_bitrates; i++) {
729 if (BIT(i) & rates) {
730 int rate = sband->bitrates[i].bitrate;
731 *pos++ = (u8) (rate / 5);
732 }
733 }
734 }
735
736 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
737 /* 1. power capabilities */
738 pos = skb_put(skb, 4);
739 *pos++ = WLAN_EID_PWR_CAPABILITY;
740 *pos++ = 2;
741 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200742 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100743
744 /* 2. supported channels */
745 /* TODO: get this in reg domain format */
746 pos = skb_put(skb, 2 * sband->n_channels + 2);
747 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
748 *pos++ = 2 * sband->n_channels;
749 for (i = 0; i < sband->n_channels; i++) {
750 *pos++ = ieee80211_frequency_to_channel(
751 sband->channels[i].center_freq);
752 *pos++ = 1; /* one channel in the subband*/
753 }
754 }
755
756 /* if present, add any custom IEs that go before HT */
757 if (assoc_data->ie_len && assoc_data->ie) {
758 static const u8 before_ht[] = {
759 WLAN_EID_SSID,
760 WLAN_EID_SUPP_RATES,
761 WLAN_EID_EXT_SUPP_RATES,
762 WLAN_EID_PWR_CAPABILITY,
763 WLAN_EID_SUPPORTED_CHANNELS,
764 WLAN_EID_RSN,
765 WLAN_EID_QOS_CAPA,
766 WLAN_EID_RRM_ENABLED_CAPABILITIES,
767 WLAN_EID_MOBILITY_DOMAIN,
768 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
769 };
770 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
771 before_ht, ARRAY_SIZE(before_ht),
772 offset);
773 pos = skb_put(skb, noffset - offset);
774 memcpy(pos, assoc_data->ie + offset, noffset - offset);
775 offset = noffset;
776 }
777
Johannes Bergf2d9d272012-11-22 14:11:39 +0100778 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
779 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
780 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
781
Johannes Berga8243b72012-11-22 14:32:09 +0100782 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200783 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200784 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100785
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000786 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100787 ieee80211_add_vht_ie(sdata, skb, sband,
788 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000789
Johannes Berg66e67e42012-01-20 13:55:27 +0100790 /* if present, add any custom non-vendor IEs that go after HT */
791 if (assoc_data->ie_len && assoc_data->ie) {
792 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
793 assoc_data->ie_len,
794 offset);
795 pos = skb_put(skb, noffset - offset);
796 memcpy(pos, assoc_data->ie + offset, noffset - offset);
797 offset = noffset;
798 }
799
Johannes Berg76f03032012-03-08 15:02:05 +0100800 if (assoc_data->wmm) {
801 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200802 qos_info = ifmgd->uapsd_queues;
803 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100804 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
805 } else {
806 qos_info = 0;
807 }
808
809 pos = skb_put(skb, 9);
810 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
811 *pos++ = 7; /* len */
812 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
813 *pos++ = 0x50;
814 *pos++ = 0xf2;
815 *pos++ = 2; /* WME */
816 *pos++ = 0; /* WME info */
817 *pos++ = 1; /* WME ver */
818 *pos++ = qos_info;
819 }
820
821 /* add any remaining custom (i.e. vendor specific here) IEs */
822 if (assoc_data->ie_len && assoc_data->ie) {
823 noffset = assoc_data->ie_len;
824 pos = skb_put(skb, noffset - offset);
825 memcpy(pos, assoc_data->ie + offset, noffset - offset);
826 }
827
Johannes Berga1845fc2012-06-27 13:18:36 +0200828 drv_mgd_prepare_tx(local, sdata);
829
Johannes Berg66e67e42012-01-20 13:55:27 +0100830 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e2013-01-29 15:02:27 +0100831 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
832 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
833 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100834 ieee80211_tx_skb(sdata, skb);
835}
836
Kalle Valo572e0012009-02-10 17:09:31 +0200837void ieee80211_send_pspoll(struct ieee80211_local *local,
838 struct ieee80211_sub_if_data *sdata)
839{
Kalle Valo572e0012009-02-10 17:09:31 +0200840 struct ieee80211_pspoll *pspoll;
841 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200842
Kalle Valod8cd1892010-01-05 20:16:26 +0200843 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
844 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200845 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200846
Kalle Valod8cd1892010-01-05 20:16:26 +0200847 pspoll = (struct ieee80211_pspoll *) skb->data;
848 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200849
Johannes Berg62ae67b2009-11-18 18:42:05 +0100850 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
851 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200852}
853
Johannes Berg965beda2009-04-16 13:17:24 +0200854void ieee80211_send_nullfunc(struct ieee80211_local *local,
855 struct ieee80211_sub_if_data *sdata,
856 int powersave)
857{
858 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200859 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530860 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200861
Kalle Valod8cd1892010-01-05 20:16:26 +0200862 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
863 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200864 return;
865
Kalle Valod8cd1892010-01-05 20:16:26 +0200866 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200867 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200868 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200869
Seth Forshee6c17b772013-02-11 11:21:07 -0600870 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
871 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530872 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
873 IEEE80211_STA_CONNECTION_POLL))
874 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
875
Johannes Berg62ae67b2009-11-18 18:42:05 +0100876 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200877}
878
Felix Fietkaud5242152010-01-08 18:06:26 +0100879static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
880 struct ieee80211_sub_if_data *sdata)
881{
882 struct sk_buff *skb;
883 struct ieee80211_hdr *nullfunc;
884 __le16 fc;
885
886 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
887 return;
888
889 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700890 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100891 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700892
Felix Fietkaud5242152010-01-08 18:06:26 +0100893 skb_reserve(skb, local->hw.extra_tx_headroom);
894
895 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
896 memset(nullfunc, 0, 30);
897 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
898 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
899 nullfunc->frame_control = fc;
900 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
901 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
902 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
903 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
904
905 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
906 ieee80211_tx_skb(sdata, skb);
907}
908
Johannes Bergcc32abd2009-05-15 11:52:31 +0200909/* spectrum management related things */
910static void ieee80211_chswitch_work(struct work_struct *work)
911{
912 struct ieee80211_sub_if_data *sdata =
913 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200914 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
915
Johannes Berg9607e6b2009-12-23 13:15:31 +0100916 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200917 return;
918
Johannes Berg77fdaa12009-07-07 03:45:17 +0200919 mutex_lock(&ifmgd->mtx);
920 if (!ifmgd->associated)
921 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200922
Johannes Berg55de9082012-07-26 17:24:39 +0200923 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200924 if (!sdata->local->ops->channel_switch) {
925 /* call "hw_config" only if doing sw channel switch */
926 ieee80211_hw_config(sdata->local,
927 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300928 } else {
929 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200930 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200931 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200932
Johannes Berg77fdaa12009-07-07 03:45:17 +0200933 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200934 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200935
Johannes Berg57eebdf2012-08-01 15:50:46 +0200936 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200937 ieee80211_wake_queues_by_reason(&sdata->local->hw,
938 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200939 out:
940 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
941 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200942}
943
Johannes Berg5ce6e432010-05-11 16:20:57 +0200944void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
945{
Johannes Berg55de9082012-07-26 17:24:39 +0200946 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
947 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200948
949 trace_api_chswitch_done(sdata, success);
950 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200951 sdata_info(sdata,
952 "driver channel switch failed, disconnecting\n");
953 ieee80211_queue_work(&sdata->local->hw,
954 &ifmgd->csa_connection_drop_work);
955 } else {
956 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200957 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200958}
959EXPORT_SYMBOL(ieee80211_chswitch_done);
960
Johannes Bergcc32abd2009-05-15 11:52:31 +0200961static void ieee80211_chswitch_timer(unsigned long data)
962{
963 struct ieee80211_sub_if_data *sdata =
964 (struct ieee80211_sub_if_data *) data;
965 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
966
Johannes Berg5bb644a2009-05-17 11:40:42 +0200967 if (sdata->local->quiescing) {
968 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
969 return;
970 }
971
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400972 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200973}
974
975void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
976 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200977 struct ieee80211_bss *bss,
978 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200979{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100980 struct cfg80211_bss *cbss =
981 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200982 struct ieee80211_channel *new_ch;
983 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900984 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
985 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200986 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200987
Johannes Berg77fdaa12009-07-07 03:45:17 +0200988 ASSERT_MGD_MTX(ifmgd);
989
990 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200991 return;
992
Helmut Schaafbe9c422009-07-23 12:14:04 +0200993 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200994 return;
995
996 /* Disregard subsequent beacons if we are already running a timer
997 processing a CSA */
998
999 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1000 return;
1001
1002 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +02001003 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1004 sdata_info(sdata,
1005 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1006 ifmgd->associated->bssid, new_freq);
1007 ieee80211_queue_work(&sdata->local->hw,
1008 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001009 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001010 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001011
Johannes Berg57eebdf2012-08-01 15:50:46 +02001012 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1013
Johannes Berg55de9082012-07-26 17:24:39 +02001014 if (sdata->local->use_chanctx) {
1015 sdata_info(sdata,
1016 "not handling channel switch with channel contexts\n");
1017 ieee80211_queue_work(&sdata->local->hw,
1018 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001019 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001020 }
1021
1022 mutex_lock(&sdata->local->chanctx_mtx);
1023 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1024 mutex_unlock(&sdata->local->chanctx_mtx);
1025 return;
1026 }
1027 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1028 struct ieee80211_chanctx, conf);
1029 if (chanctx->refcount > 1) {
1030 sdata_info(sdata,
1031 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1032 ieee80211_queue_work(&sdata->local->hw,
1033 &ifmgd->csa_connection_drop_work);
1034 mutex_unlock(&sdata->local->chanctx_mtx);
1035 return;
1036 }
1037 mutex_unlock(&sdata->local->chanctx_mtx);
1038
1039 sdata->local->csa_channel = new_ch;
1040
Johannes Berg57eebdf2012-08-01 15:50:46 +02001041 if (sw_elem->mode)
1042 ieee80211_stop_queues_by_reason(&sdata->local->hw,
1043 IEEE80211_QUEUE_STOP_REASON_CSA);
1044
Johannes Berg5ce6e432010-05-11 16:20:57 +02001045 if (sdata->local->ops->channel_switch) {
1046 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001047 struct ieee80211_channel_switch ch_switch = {
1048 .timestamp = timestamp,
1049 .block_tx = sw_elem->mode,
1050 .channel = new_ch,
1051 .count = sw_elem->count,
1052 };
1053
Johannes Berg5ce6e432010-05-11 16:20:57 +02001054 drv_channel_switch(sdata->local, &ch_switch);
1055 return;
1056 }
1057
1058 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001059 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001060 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001061 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001062 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +02001063 TU_TO_EXP_TIME(sw_elem->count *
1064 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001065}
1066
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001067static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1068 struct ieee80211_channel *channel,
1069 const u8 *country_ie, u8 country_ie_len,
1070 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001071{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001072 struct ieee80211_country_ie_triplet *triplet;
1073 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1074 int i, chan_pwr, chan_increment, new_ap_level;
1075 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001076
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001077 /* Invalid IE */
1078 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001079 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001080
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001081 triplet = (void *)(country_ie + 3);
1082 country_ie_len -= 3;
1083
1084 switch (channel->band) {
1085 default:
1086 WARN_ON_ONCE(1);
1087 /* fall through */
1088 case IEEE80211_BAND_2GHZ:
1089 case IEEE80211_BAND_60GHZ:
1090 chan_increment = 1;
1091 break;
1092 case IEEE80211_BAND_5GHZ:
1093 chan_increment = 4;
1094 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001095 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001096
1097 /* find channel */
1098 while (country_ie_len >= 3) {
1099 u8 first_channel = triplet->chans.first_channel;
1100
1101 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1102 goto next;
1103
1104 for (i = 0; i < triplet->chans.num_channels; i++) {
1105 if (first_channel + i * chan_increment == chan) {
1106 have_chan_pwr = true;
1107 chan_pwr = triplet->chans.max_power;
1108 break;
1109 }
1110 }
1111 if (have_chan_pwr)
1112 break;
1113
1114 next:
1115 triplet++;
1116 country_ie_len -= 3;
1117 }
1118
1119 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001120 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001121
1122 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1123
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001124 if (sdata->ap_power_level == new_ap_level)
1125 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001126
1127 sdata_info(sdata,
1128 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1129 new_ap_level, chan_pwr, *pwr_constr_elem,
1130 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001131 sdata->ap_power_level = new_ap_level;
1132 if (__ieee80211_recalc_txpower(sdata))
1133 return BSS_CHANGED_TXPOWER;
1134 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001135}
1136
Johannes Berg965beda2009-04-16 13:17:24 +02001137/* powersave */
1138static void ieee80211_enable_ps(struct ieee80211_local *local,
1139 struct ieee80211_sub_if_data *sdata)
1140{
1141 struct ieee80211_conf *conf = &local->hw.conf;
1142
Johannes Bergd5edaed2009-04-22 23:02:51 +02001143 /*
1144 * If we are scanning right now then the parameters will
1145 * take effect when scan finishes.
1146 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001147 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001148 return;
1149
Johannes Berg965beda2009-04-16 13:17:24 +02001150 if (conf->dynamic_ps_timeout > 0 &&
1151 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1152 mod_timer(&local->dynamic_ps_timer, jiffies +
1153 msecs_to_jiffies(conf->dynamic_ps_timeout));
1154 } else {
1155 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1156 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301157
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001158 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1159 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1160 return;
1161
1162 conf->flags |= IEEE80211_CONF_PS;
1163 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001164 }
1165}
1166
1167static void ieee80211_change_ps(struct ieee80211_local *local)
1168{
1169 struct ieee80211_conf *conf = &local->hw.conf;
1170
1171 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001172 ieee80211_enable_ps(local, local->ps_sdata);
1173 } else if (conf->flags & IEEE80211_CONF_PS) {
1174 conf->flags &= ~IEEE80211_CONF_PS;
1175 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1176 del_timer_sync(&local->dynamic_ps_timer);
1177 cancel_work_sync(&local->dynamic_ps_enable_work);
1178 }
1179}
1180
Jason Young808118c2011-03-10 16:43:19 -08001181static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1182{
1183 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1184 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001185 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001186
1187 if (!mgd->powersave)
1188 return false;
1189
Johannes Berg05cb9102011-10-28 11:59:47 +02001190 if (mgd->broken_ap)
1191 return false;
1192
Jason Young808118c2011-03-10 16:43:19 -08001193 if (!mgd->associated)
1194 return false;
1195
Jason Young808118c2011-03-10 16:43:19 -08001196 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1197 IEEE80211_STA_CONNECTION_POLL))
1198 return false;
1199
1200 rcu_read_lock();
1201 sta = sta_info_get(sdata, mgd->bssid);
1202 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001203 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001204 rcu_read_unlock();
1205
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001206 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001207}
1208
Johannes Berg965beda2009-04-16 13:17:24 +02001209/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001210void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001211{
1212 struct ieee80211_sub_if_data *sdata, *found = NULL;
1213 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001214 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001215
1216 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1217 local->ps_sdata = NULL;
1218 return;
1219 }
1220
1221 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001222 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001223 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301224 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1225 /* If an AP vif is found, then disable PS
1226 * by setting the count to zero thereby setting
1227 * ps_sdata to NULL.
1228 */
1229 count = 0;
1230 break;
1231 }
Johannes Berg965beda2009-04-16 13:17:24 +02001232 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1233 continue;
1234 found = sdata;
1235 count++;
1236 }
1237
Jason Young808118c2011-03-10 16:43:19 -08001238 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001239 s32 beaconint_us;
1240
1241 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001242 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001243
1244 beaconint_us = ieee80211_tu_to_usec(
1245 found->vif.bss_conf.beacon_int);
1246
Juuso Oikarinenff616382010-06-09 09:51:52 +03001247 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001248 if (timeout < 0) {
1249 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001250 * Go to full PSM if the user configures a very low
1251 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001252 * The 2000 second value is there for compatibility
1253 * until the PM_QOS_NETWORK_LATENCY is configured
1254 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001255 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001256 if (latency > (1900 * USEC_PER_MSEC) &&
1257 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001258 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001259 else
1260 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001261 }
Johannes Berg09b85562013-02-06 23:07:41 +01001262 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001263
Johannes Berg04fe2032009-04-22 18:44:37 +02001264 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001265 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001266 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001267 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001268 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001269
1270 /* If the TIM IE is invalid, pretend the value is 1 */
1271 if (!dtimper)
1272 dtimper = 1;
1273 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001274 maxslp = min_t(int, dtimper,
1275 latency / beaconint_us);
1276
Johannes Berg9ccebe62009-04-23 10:32:36 +02001277 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001278 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001279 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001280 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001281 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001282 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001283 }
Johannes Berg965beda2009-04-16 13:17:24 +02001284
1285 ieee80211_change_ps(local);
1286}
1287
Eliad Pellerab095872012-07-27 12:33:22 +03001288void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1289{
1290 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1291
1292 if (sdata->vif.bss_conf.ps != ps_allowed) {
1293 sdata->vif.bss_conf.ps = ps_allowed;
1294 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1295 }
1296}
1297
Johannes Berg965beda2009-04-16 13:17:24 +02001298void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1299{
1300 struct ieee80211_local *local =
1301 container_of(work, struct ieee80211_local,
1302 dynamic_ps_disable_work);
1303
1304 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1305 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1306 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1307 }
1308
1309 ieee80211_wake_queues_by_reason(&local->hw,
1310 IEEE80211_QUEUE_STOP_REASON_PS);
1311}
1312
1313void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1314{
1315 struct ieee80211_local *local =
1316 container_of(work, struct ieee80211_local,
1317 dynamic_ps_enable_work);
1318 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001319 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301320 unsigned long flags;
1321 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001322
1323 /* can only happen when PS was just disabled anyway */
1324 if (!sdata)
1325 return;
1326
Christian Lamparter5e340692011-06-30 21:08:43 +02001327 ifmgd = &sdata->u.mgd;
1328
Johannes Berg965beda2009-04-16 13:17:24 +02001329 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1330 return;
1331
Johannes Berg09b85562013-02-06 23:07:41 +01001332 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001333 /* don't enter PS if TX frames are pending */
1334 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301335 mod_timer(&local->dynamic_ps_timer, jiffies +
1336 msecs_to_jiffies(
1337 local->hw.conf.dynamic_ps_timeout));
1338 return;
1339 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001340
1341 /*
1342 * transmission can be stopped by others which leads to
1343 * dynamic_ps_timer expiry. Postpone the ps timer if it
1344 * is not the actual idle state.
1345 */
1346 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1347 for (q = 0; q < local->hw.queues; q++) {
1348 if (local->queue_stop_reasons[q]) {
1349 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1350 flags);
1351 mod_timer(&local->dynamic_ps_timer, jiffies +
1352 msecs_to_jiffies(
1353 local->hw.conf.dynamic_ps_timeout));
1354 return;
1355 }
1356 }
1357 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301358 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301359
Vivek Natarajan375177b2010-02-09 14:50:28 +05301360 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301361 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301362 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001363
Vivek Natarajane8306f92011-04-06 11:41:10 +05301364 if (drv_tx_frames_pending(local))
1365 mod_timer(&local->dynamic_ps_timer, jiffies +
1366 msecs_to_jiffies(
1367 local->hw.conf.dynamic_ps_timeout));
1368 else {
1369 ieee80211_send_nullfunc(local, sdata, 1);
1370 /* Flush to get the tx status of nullfunc frame */
1371 drv_flush(local, false);
1372 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301373 }
1374
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001375 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1376 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301377 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1378 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1379 local->hw.conf.flags |= IEEE80211_CONF_PS;
1380 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1381 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301382
Arik Nemtsov77b70232011-06-26 12:06:54 +03001383 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1384 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001385}
1386
1387void ieee80211_dynamic_ps_timer(unsigned long data)
1388{
1389 struct ieee80211_local *local = (void *) data;
1390
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001391 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001392 return;
1393
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001394 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001395}
1396
Simon Wunderlich164eb022013-02-08 18:16:20 +01001397void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1398{
1399 struct delayed_work *delayed_work =
1400 container_of(work, struct delayed_work, work);
1401 struct ieee80211_sub_if_data *sdata =
1402 container_of(delayed_work, struct ieee80211_sub_if_data,
1403 dfs_cac_timer_work);
1404
1405 ieee80211_vif_release_channel(sdata);
1406
1407 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1408}
1409
Johannes Berg60f8b392008-09-08 17:44:22 +02001410/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001411static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001412 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001413 u8 *wmm_param, size_t wmm_param_len)
1414{
Jiri Bencf0706e82007-05-05 11:45:53 -07001415 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001416 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001417 size_t left;
1418 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001419 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001420
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001421 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001422 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001423
Johannes Berg32c5057b2012-03-28 11:04:29 +02001424 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001425 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001426
1427 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001428 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001429
Jiri Bencf0706e82007-05-05 11:45:53 -07001430 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001431 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001432
1433 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001434 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001435
Jiri Bencf0706e82007-05-05 11:45:53 -07001436 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001437 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001438 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001439 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001440
1441 pos = wmm_param + 8;
1442 left = wmm_param_len - 8;
1443
1444 memset(&params, 0, sizeof(params));
1445
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001446 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001447 for (; left >= 4; left -= 4, pos += 4) {
1448 int aci = (pos[0] >> 5) & 0x03;
1449 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001450 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001451 int queue;
1452
1453 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001454 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001455 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001456 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001457 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001458 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1459 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001460 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001461 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001462 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001463 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001464 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001465 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1466 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001467 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001468 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001469 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001470 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001471 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001472 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1473 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001474 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001475 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001476 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001477 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001478 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001479 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001480 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1481 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001482 break;
1483 }
1484
1485 params.aifs = pos[0] & 0x0f;
1486 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1487 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001488 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001489 params.uapsd = uapsd;
1490
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001491 mlme_dbg(sdata,
1492 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1493 queue, aci, acm,
1494 params.aifs, params.cw_min, params.cw_max,
1495 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001496 sdata->tx_conf[queue] = params;
1497 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001498 sdata_err(sdata,
1499 "failed to set TX queue parameters for queue %d\n",
1500 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001501 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001502
1503 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001504 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001505 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001506}
1507
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001508static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1509{
1510 lockdep_assert_held(&sdata->local->mtx);
1511
1512 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1513 IEEE80211_STA_BEACON_POLL);
1514 ieee80211_run_deferred_scan(sdata->local);
1515}
1516
1517static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1518{
1519 mutex_lock(&sdata->local->mtx);
1520 __ieee80211_stop_poll(sdata);
1521 mutex_unlock(&sdata->local->mtx);
1522}
1523
Johannes Berg7a5158e2008-10-08 10:59:33 +02001524static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1525 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001526{
Johannes Bergbda39332008-10-11 01:51:51 +02001527 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001528 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001529 bool use_protection;
1530 bool use_short_preamble;
1531 bool use_short_slot;
1532
1533 if (erp_valid) {
1534 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1535 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1536 } else {
1537 use_protection = false;
1538 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1539 }
1540
1541 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001542 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001543 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001544
Johannes Berg471b3ef2007-12-28 14:32:58 +01001545 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001546 bss_conf->use_cts_prot = use_protection;
1547 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001548 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001549
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001550 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001551 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001552 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001553 }
Daniel Draked9430a32007-07-27 15:43:24 +02001554
Johannes Berg7a5158e2008-10-08 10:59:33 +02001555 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001556 bss_conf->use_short_slot = use_short_slot;
1557 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001558 }
1559
1560 return changed;
1561}
1562
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001563static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001564 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001565 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001566{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001567 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001568 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001569 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001570
Johannes Bergae5eb022008-10-14 16:58:37 +02001571 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001572 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001573 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001574
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001575 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1576 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1577
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001578 sdata->u.mgd.associated = cbss;
1579 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001580
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001581 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1582
Johannes Berg488dd7b2012-10-29 20:08:01 +01001583 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001584 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001585
Johannes Berg9caf0362012-11-29 01:25:20 +01001586 rcu_read_lock();
1587 ies = rcu_dereference(cbss->ies);
1588 if (ies) {
1589 u8 noa[2];
1590 int ret;
1591
1592 ret = cfg80211_get_p2p_attr(
1593 ies->data, ies->len,
1594 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1595 noa, sizeof(noa));
1596 if (ret >= 2) {
1597 bss_conf->p2p_oppps = noa[1] & 0x80;
1598 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1599 bss_info_changed |= BSS_CHANGED_P2P_PS;
1600 sdata->u.mgd.p2p_noa_index = noa[0];
1601 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001602 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001603 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001604 }
1605
Johannes Bergb291ba12009-07-10 15:29:03 +02001606 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001607 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001608
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001609 ieee80211_led_assoc(local, 1);
1610
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001611 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001612 /*
1613 * If the AP is buggy we may get here with no DTIM period
1614 * known, so assume it's 1 which is the only safe assumption
1615 * in that case, although if the TIM IE is broken powersave
1616 * probably just won't work at all.
1617 */
1618 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001619 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001620 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001621 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001622 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001623
Juuso Oikarinen68542962010-06-09 13:43:26 +03001624 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001625
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001626 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001627 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001628 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001629 bss_info_changed |= BSS_CHANGED_CQM;
1630
Juuso Oikarinen68542962010-06-09 13:43:26 +03001631 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001632 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001633 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001634
Johannes Bergae5eb022008-10-14 16:58:37 +02001635 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001636
Johannes Berg056508d2009-07-30 21:43:55 +02001637 mutex_lock(&local->iflist_mtx);
1638 ieee80211_recalc_ps(local, -1);
1639 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001640
Johannes Berg04ecd252012-09-11 14:34:12 +02001641 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001642 ieee80211_recalc_ps_vif(sdata);
1643
John W. Linville8a5b33f2010-01-06 15:39:39 -05001644 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001645 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001646}
1647
Jouni Malinene69e95d2010-03-29 23:29:31 -07001648static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001649 u16 stype, u16 reason, bool tx,
1650 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001651{
Johannes Berg46900292009-02-15 12:44:28 +01001652 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001653 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001654 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001655
Johannes Berg77fdaa12009-07-07 03:45:17 +02001656 ASSERT_MGD_MTX(ifmgd);
1657
Johannes Berg37ad3882012-02-24 13:50:54 +01001658 if (WARN_ON_ONCE(tx && !frame_buf))
1659 return;
1660
Johannes Bergb291ba12009-07-10 15:29:03 +02001661 if (WARN_ON(!ifmgd->associated))
1662 return;
1663
David Spinadel79543d82012-06-12 09:59:45 +03001664 ieee80211_stop_poll(sdata);
1665
Johannes Berg77fdaa12009-07-07 03:45:17 +02001666 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001667
1668 /*
1669 * we need to commit the associated = NULL change because the
1670 * scan code uses that to determine whether this iface should
1671 * go to/wake up from powersave or not -- and could otherwise
1672 * wake the queues erroneously.
1673 */
1674 smp_mb();
1675
1676 /*
1677 * Thus, we can only afterwards stop the queues -- to account
1678 * for the case where another CPU is finishing a scan at this
1679 * time -- we don't want the scan code to enable queues.
1680 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001681
John W. Linville8a5b33f2010-01-06 15:39:39 -05001682 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001683 netif_carrier_off(sdata->dev);
1684
Eliad Peller88bc40e2012-07-12 17:35:33 +03001685 /*
1686 * if we want to get out of ps before disassoc (why?) we have
1687 * to do it before sending disassoc, as otherwise the null-packet
1688 * won't be valid.
1689 */
1690 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1691 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1692 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1693 }
1694 local->ps_sdata = NULL;
1695
Eliad Pellerab095872012-07-27 12:33:22 +03001696 /* disable per-vif ps */
1697 ieee80211_recalc_ps_vif(sdata);
1698
Eliad Pellerf8239812012-06-27 14:18:22 +03001699 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1700 if (tx)
1701 drv_flush(local, false);
1702
Johannes Berg37ad3882012-02-24 13:50:54 +01001703 /* deauthenticate/disassociate now */
1704 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001705 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1706 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001707
1708 /* flush out frame */
1709 if (tx)
1710 drv_flush(local, false);
1711
Eliad Peller88a9e312012-06-01 11:14:03 +03001712 /* clear bssid only after building the needed mgmt frames */
1713 memset(ifmgd->bssid, 0, ETH_ALEN);
1714
Johannes Berg37ad3882012-02-24 13:50:54 +01001715 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001716 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001717
1718 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001719 changed |= ieee80211_reset_erp_info(sdata);
1720
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001721 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001722 changed |= BSS_CHANGED_ASSOC;
1723 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001724
Johannes Berg488dd7b2012-10-29 20:08:01 +01001725 sdata->vif.bss_conf.p2p_ctwindow = 0;
1726 sdata->vif.bss_conf.p2p_oppps = false;
1727
Johannes Berg413ad502009-05-08 21:21:06 +02001728 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001729 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1730 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001731
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001732 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301733
Kalle Valo520eb822008-12-18 23:35:27 +02001734 del_timer_sync(&local->dynamic_ps_timer);
1735 cancel_work_sync(&local->dynamic_ps_enable_work);
1736
Juuso Oikarinen68542962010-06-09 13:43:26 +03001737 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001738 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001739 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001740
Johannes Berg3abead52012-03-02 15:56:59 +01001741 sdata->vif.bss_conf.qos = false;
1742 changed |= BSS_CHANGED_QOS;
1743
Johannes Berg0aaffa92010-05-05 15:28:27 +02001744 /* The BSSID (not really interesting) and HT changed */
1745 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001746 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001747
Johannes Berg3abead52012-03-02 15:56:59 +01001748 /* disassociated - set to defaults now */
1749 ieee80211_set_wmm_default(sdata, false);
1750
Johannes Bergb9dcf712010-08-27 12:35:54 +02001751 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1752 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1753 del_timer_sync(&sdata->u.mgd.timer);
1754 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001755
1756 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001757
Johannes Berg826262c2012-12-10 16:38:14 +02001758 sdata->vif.bss_conf.dtim_period = 0;
1759
Johannes Berg028e8da02012-11-26 11:57:41 +01001760 ifmgd->flags = 0;
1761 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001762}
Jiri Bencf0706e82007-05-05 11:45:53 -07001763
Kalle Valo3cf335d2009-03-22 21:57:06 +02001764void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1765 struct ieee80211_hdr *hdr)
1766{
1767 /*
1768 * We can postpone the mgd.timer whenever receiving unicast frames
1769 * from AP because we know that the connection is working both ways
1770 * at that time. But multicast frames (and hence also beacons) must
1771 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001772 * data idle periods for sending the periodic probe request to the
1773 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001774 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001775 if (is_multicast_ether_addr(hdr->addr1))
1776 return;
1777
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001778 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001779}
Jiri Bencf0706e82007-05-05 11:45:53 -07001780
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001781static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1782{
1783 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001784 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001785
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001786 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001787 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001788 IEEE80211_STA_CONNECTION_POLL))) {
1789 mutex_unlock(&local->mtx);
1790 return;
1791 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001792
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001793 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001794
1795 mutex_lock(&local->iflist_mtx);
1796 ieee80211_recalc_ps(local, -1);
1797 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001798
1799 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001800 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001801
1802 /*
1803 * We've received a probe response, but are not sure whether
1804 * we have or will be receiving any beacons or data, so let's
1805 * schedule the timers again, just in case.
1806 */
1807 ieee80211_sta_reset_beacon_monitor(sdata);
1808
1809 mod_timer(&ifmgd->conn_mon_timer,
1810 round_jiffies_up(jiffies +
1811 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001812out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001813 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001814}
1815
1816void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001817 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001818{
Felix Fietkau75706d02010-12-02 21:01:07 +01001819 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001820 return;
1821
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001822 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1823 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001824 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001825 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001826 else
1827 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001828 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001829 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001830 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001831
1832 if (ack)
1833 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001834}
1835
Maxim Levitskya43abf22009-07-31 18:54:12 +03001836static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1837{
1838 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1839 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001840 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001841 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001842
1843 /*
1844 * Try sending broadcast probe requests for the last three
1845 * probe requests after the first ones failed since some
1846 * buggy APs only support broadcast probe requests.
1847 */
1848 if (ifmgd->probe_send_count >= unicast_limit)
1849 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001850
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001851 /*
1852 * When the hardware reports an accurate Tx ACK status, it's
1853 * better to send a nullfunc frame instead of a probe request,
1854 * as it will kick us off the AP quickly if we aren't associated
1855 * anymore. The timeout will be reset if the frame is ACKed by
1856 * the AP.
1857 */
Soumik Das992e68b2012-05-20 15:31:13 +05301858 ifmgd->probe_send_count++;
1859
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001860 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1861 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001862 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001863 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001864 int ssid_len;
1865
Johannes Berg9caf0362012-11-29 01:25:20 +01001866 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001867 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001868 if (WARN_ON_ONCE(ssid == NULL))
1869 ssid_len = 0;
1870 else
1871 ssid_len = ssid[1];
1872
1873 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e2013-01-29 15:02:27 +01001874 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001875 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001876 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001877 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001878
Ben Greear180205b2011-02-04 15:30:24 -08001879 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001880 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301881 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1882 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001883}
1884
Johannes Bergb291ba12009-07-10 15:29:03 +02001885static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1886 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001887{
Kalle Valo04de8382009-03-22 21:57:35 +02001888 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001889 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001890
Johannes Berg9607e6b2009-12-23 13:15:31 +01001891 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001892 return;
1893
Johannes Berg77fdaa12009-07-07 03:45:17 +02001894 mutex_lock(&ifmgd->mtx);
1895
1896 if (!ifmgd->associated)
1897 goto out;
1898
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001899 mutex_lock(&sdata->local->mtx);
1900
1901 if (sdata->local->tmp_channel || sdata->local->scanning) {
1902 mutex_unlock(&sdata->local->mtx);
1903 goto out;
1904 }
1905
Joe Perchese87cc472012-05-13 21:56:26 +00001906 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001907 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001908 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001909
Holger Schurig6efb71b2012-05-15 15:38:59 +02001910 ieee80211_cqm_rssi_notify(&sdata->vif,
1911 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001912
Johannes Bergb291ba12009-07-10 15:29:03 +02001913 /*
1914 * The driver/our work has already reported this event or the
1915 * connection monitoring has kicked in and we have already sent
1916 * a probe request. Or maybe the AP died and the driver keeps
1917 * reporting until we disassociate...
1918 *
1919 * In either case we have to ignore the current call to this
1920 * function (except for setting the correct probe reason bit)
1921 * because otherwise we would reset the timer every time and
1922 * never check whether we received a probe response!
1923 */
1924 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1925 IEEE80211_STA_CONNECTION_POLL))
1926 already = true;
1927
1928 if (beacon)
1929 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1930 else
1931 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1932
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001933 mutex_unlock(&sdata->local->mtx);
1934
Johannes Bergb291ba12009-07-10 15:29:03 +02001935 if (already)
1936 goto out;
1937
Johannes Berg4e751842009-06-10 15:16:52 +02001938 mutex_lock(&sdata->local->iflist_mtx);
1939 ieee80211_recalc_ps(sdata->local, -1);
1940 mutex_unlock(&sdata->local->iflist_mtx);
1941
Maxim Levitskya43abf22009-07-31 18:54:12 +03001942 ifmgd->probe_send_count = 0;
1943 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001944 out:
1945 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001946}
1947
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001948struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1949 struct ieee80211_vif *vif)
1950{
1951 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1952 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001953 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001954 struct sk_buff *skb;
1955 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001956 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001957
1958 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1959 return NULL;
1960
1961 ASSERT_MGD_MTX(ifmgd);
1962
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001963 if (ifmgd->associated)
1964 cbss = ifmgd->associated;
1965 else if (ifmgd->auth_data)
1966 cbss = ifmgd->auth_data->bss;
1967 else if (ifmgd->assoc_data)
1968 cbss = ifmgd->assoc_data->bss;
1969 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001970 return NULL;
1971
Johannes Berg9caf0362012-11-29 01:25:20 +01001972 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001973 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001974 if (WARN_ON_ONCE(ssid == NULL))
1975 ssid_len = 0;
1976 else
1977 ssid_len = ssid[1];
1978
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001979 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001980 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001981 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001982 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001983 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001984
1985 return skb;
1986}
1987EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1988
Johannes Bergeef9e542013-01-29 13:09:34 +01001989static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001990{
1991 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001992 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001993
1994 mutex_lock(&ifmgd->mtx);
1995 if (!ifmgd->associated) {
1996 mutex_unlock(&ifmgd->mtx);
1997 return;
1998 }
1999
Johannes Berg37ad3882012-02-24 13:50:54 +01002000 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2001 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002002 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002003 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002004 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2005 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002006 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002007
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002008 /*
2009 * must be outside lock due to cfg80211,
2010 * but that's not a problem.
2011 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002012 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002013}
2014
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002015static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002016{
2017 struct ieee80211_sub_if_data *sdata =
2018 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002019 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002020 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2021 struct sta_info *sta;
2022
2023 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302024 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002025 sta = sta_info_get(sdata, ifmgd->bssid);
2026 if (sta)
2027 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302028 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002029 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002030
Johannes Berg682bd382013-01-29 13:13:50 +01002031 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002032 sdata_info(sdata, "Connection to AP %pM lost\n",
2033 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002034 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002035 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002036 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002037 }
2038}
2039
2040static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2041{
2042 struct ieee80211_sub_if_data *sdata =
2043 container_of(work, struct ieee80211_sub_if_data,
2044 u.mgd.csa_connection_drop_work);
2045
Johannes Bergeef9e542013-01-29 13:09:34 +01002046 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002047}
2048
Kalle Valo04de8382009-03-22 21:57:35 +02002049void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2050{
2051 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002052 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002053
Johannes Bergb5878a22010-04-07 16:48:40 +02002054 trace_api_beacon_loss(sdata);
2055
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002056 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01002057 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002058 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002059}
2060EXPORT_SYMBOL(ieee80211_beacon_loss);
2061
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002062void ieee80211_connection_loss(struct ieee80211_vif *vif)
2063{
2064 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2065 struct ieee80211_hw *hw = &sdata->local->hw;
2066
Johannes Bergb5878a22010-04-07 16:48:40 +02002067 trace_api_connection_loss(sdata);
2068
Johannes Berg682bd382013-01-29 13:13:50 +01002069 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002070 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2071}
2072EXPORT_SYMBOL(ieee80211_connection_loss);
2073
2074
Johannes Berg66e67e42012-01-20 13:55:27 +01002075static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2076 bool assoc)
2077{
2078 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2079
2080 lockdep_assert_held(&sdata->u.mgd.mtx);
2081
Johannes Berg66e67e42012-01-20 13:55:27 +01002082 if (!assoc) {
2083 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2084
2085 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2086 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002087 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002088 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002089 }
2090
Johannes Berg5b112d32013-02-01 01:49:58 +01002091 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002092 kfree(auth_data);
2093 sdata->u.mgd.auth_data = NULL;
2094}
2095
2096static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2097 struct ieee80211_mgmt *mgmt, size_t len)
2098{
Johannes Berg1672c0e2013-01-29 15:02:27 +01002099 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002100 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2101 u8 *pos;
2102 struct ieee802_11_elems elems;
Johannes Berg1672c0e2013-01-29 15:02:27 +01002103 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002104
2105 pos = mgmt->u.auth.variable;
2106 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2107 if (!elems.challenge)
2108 return;
2109 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002110 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e2013-01-29 15:02:27 +01002111 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2112 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2113 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002114 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002115 elems.challenge - 2, elems.challenge_len + 2,
2116 auth_data->bss->bssid, auth_data->bss->bssid,
2117 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e2013-01-29 15:02:27 +01002118 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002119}
2120
2121static enum rx_mgmt_action __must_check
2122ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2123 struct ieee80211_mgmt *mgmt, size_t len)
2124{
2125 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2126 u8 bssid[ETH_ALEN];
2127 u16 auth_alg, auth_transaction, status_code;
2128 struct sta_info *sta;
2129
2130 lockdep_assert_held(&ifmgd->mtx);
2131
2132 if (len < 24 + 6)
2133 return RX_MGMT_NONE;
2134
2135 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2136 return RX_MGMT_NONE;
2137
2138 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2139
Joe Perchesb203ca32012-05-08 18:56:52 +00002140 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002141 return RX_MGMT_NONE;
2142
2143 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2144 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2145 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2146
2147 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002148 auth_transaction != ifmgd->auth_data->expected_transaction) {
2149 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2150 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2151 auth_transaction,
2152 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002153 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002154 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002155
2156 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002157 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2158 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002159 ieee80211_destroy_auth_data(sdata, false);
2160 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002161 }
2162
2163 switch (ifmgd->auth_data->algorithm) {
2164 case WLAN_AUTH_OPEN:
2165 case WLAN_AUTH_LEAP:
2166 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002167 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002168 break;
2169 case WLAN_AUTH_SHARED_KEY:
2170 if (ifmgd->auth_data->expected_transaction != 4) {
2171 ieee80211_auth_challenge(sdata, mgmt, len);
2172 /* need another frame */
2173 return RX_MGMT_NONE;
2174 }
2175 break;
2176 default:
2177 WARN_ONCE(1, "invalid auth alg %d",
2178 ifmgd->auth_data->algorithm);
2179 return RX_MGMT_NONE;
2180 }
2181
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002182 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002183 ifmgd->auth_data->done = true;
2184 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002185 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002186 run_again(ifmgd, ifmgd->auth_data->timeout);
2187
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002188 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2189 ifmgd->auth_data->expected_transaction != 2) {
2190 /*
2191 * Report auth frame to user space for processing since another
2192 * round of Authentication frames is still needed.
2193 */
2194 return RX_MGMT_CFG80211_RX_AUTH;
2195 }
2196
Johannes Berg66e67e42012-01-20 13:55:27 +01002197 /* move station state to auth */
2198 mutex_lock(&sdata->local->sta_mtx);
2199 sta = sta_info_get(sdata, bssid);
2200 if (!sta) {
2201 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2202 goto out_err;
2203 }
2204 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002205 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002206 goto out_err;
2207 }
2208 mutex_unlock(&sdata->local->sta_mtx);
2209
2210 return RX_MGMT_CFG80211_RX_AUTH;
2211 out_err:
2212 mutex_unlock(&sdata->local->sta_mtx);
2213 /* ignore frame -- wait for timeout */
2214 return RX_MGMT_NONE;
2215}
2216
2217
Johannes Berg77fdaa12009-07-07 03:45:17 +02002218static enum rx_mgmt_action __must_check
2219ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002220 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002221{
Johannes Berg46900292009-02-15 12:44:28 +01002222 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002223 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002224 u16 reason_code;
2225
Johannes Berg66e67e42012-01-20 13:55:27 +01002226 lockdep_assert_held(&ifmgd->mtx);
2227
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002228 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002229 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002230
Johannes Berg66e67e42012-01-20 13:55:27 +01002231 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002232 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002233 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002234
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002235 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002236
2237 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2238
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002239 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2240 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002241
Johannes Berg37ad3882012-02-24 13:50:54 +01002242 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2243
Johannes Berg77fdaa12009-07-07 03:45:17 +02002244 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002245}
2246
2247
Johannes Berg77fdaa12009-07-07 03:45:17 +02002248static enum rx_mgmt_action __must_check
2249ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2250 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002251{
Johannes Berg46900292009-02-15 12:44:28 +01002252 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002253 u16 reason_code;
2254
Johannes Berg66e67e42012-01-20 13:55:27 +01002255 lockdep_assert_held(&ifmgd->mtx);
2256
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002257 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002258 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002259
Johannes Berg66e67e42012-01-20 13:55:27 +01002260 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002261 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002262 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002263
2264 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2265
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002266 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2267 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002268
Johannes Berg37ad3882012-02-24 13:50:54 +01002269 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2270
Johannes Berg77fdaa12009-07-07 03:45:17 +02002271 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002272}
2273
Christian Lamparterc74d0842011-10-15 00:14:49 +02002274static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2275 u8 *supp_rates, unsigned int supp_rates_len,
2276 u32 *rates, u32 *basic_rates,
2277 bool *have_higher_than_11mbit,
2278 int *min_rate, int *min_rate_index)
2279{
2280 int i, j;
2281
2282 for (i = 0; i < supp_rates_len; i++) {
2283 int rate = (supp_rates[i] & 0x7f) * 5;
2284 bool is_basic = !!(supp_rates[i] & 0x80);
2285
2286 if (rate > 110)
2287 *have_higher_than_11mbit = true;
2288
2289 /*
2290 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2291 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2292 *
2293 * Note: Even through the membership selector and the basic
2294 * rate flag share the same bit, they are not exactly
2295 * the same.
2296 */
2297 if (!!(supp_rates[i] & 0x80) &&
2298 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2299 continue;
2300
2301 for (j = 0; j < sband->n_bitrates; j++) {
2302 if (sband->bitrates[j].bitrate == rate) {
2303 *rates |= BIT(j);
2304 if (is_basic)
2305 *basic_rates |= BIT(j);
2306 if (rate < *min_rate) {
2307 *min_rate = rate;
2308 *min_rate_index = j;
2309 }
2310 break;
2311 }
2312 }
2313 }
2314}
Jiri Bencf0706e82007-05-05 11:45:53 -07002315
Johannes Berg66e67e42012-01-20 13:55:27 +01002316static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2317 bool assoc)
2318{
2319 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2320
2321 lockdep_assert_held(&sdata->u.mgd.mtx);
2322
Johannes Berg66e67e42012-01-20 13:55:27 +01002323 if (!assoc) {
2324 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2325
2326 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2327 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002328 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002329 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002330 }
2331
2332 kfree(assoc_data);
2333 sdata->u.mgd.assoc_data = NULL;
2334}
2335
2336static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2337 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002338 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002339{
Johannes Berg46900292009-02-15 12:44:28 +01002340 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002341 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002342 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002343 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002344 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002345 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002346 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002347 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002348 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002349 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002350
Johannes Bergaf6b6372009-12-23 13:15:35 +01002351 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002352
Jiri Bencf0706e82007-05-05 11:45:53 -07002353 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002354 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002355
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002356 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002357 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2358 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002359 aid &= ~(BIT(15) | BIT(14));
2360
Johannes Berg05cb9102011-10-28 11:59:47 +02002361 ifmgd->broken_ap = false;
2362
2363 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002364 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2365 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002366 aid = 0;
2367 ifmgd->broken_ap = true;
2368 }
2369
Johannes Bergaf6b6372009-12-23 13:15:35 +01002370 pos = mgmt->u.assoc_resp.variable;
2371 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2372
Jiri Bencf0706e82007-05-05 11:45:53 -07002373 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002374 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002375 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002376 }
2377
Johannes Berg46900292009-02-15 12:44:28 +01002378 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002379
Guy Eilam2a33bee2011-08-17 15:18:15 +03002380 mutex_lock(&sdata->local->sta_mtx);
2381 /*
2382 * station info was already allocated and inserted before
2383 * the association and should be available to us
2384 */
Johannes Berg7852e362012-01-20 13:55:24 +01002385 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002386 if (WARN_ON(!sta)) {
2387 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002388 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002389 }
2390
Johannes Berg55de9082012-07-26 17:24:39 +02002391 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002392
Johannes Berga8243b72012-11-22 14:32:09 +01002393 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002394 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002395 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002396
Mahesh Palivela818255e2012-10-10 11:33:04 +00002397 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2398 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002399 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002400
Johannes Berg11289582013-02-07 17:36:12 +01002401 if (elems.ht_operation && elems.wmm_param &&
2402 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2403 changed |= ieee80211_config_ht_tx(sdata, sta,
2404 elems.ht_operation,
2405 cbss->bssid, false);
2406
Johannes Bergbee7f582013-02-07 22:24:55 +01002407 /*
2408 * If an operating mode notification IE is present, override the
2409 * NSS calculation (that would be done in rate_control_rate_init())
2410 * and use the # of streams from that element.
2411 */
2412 if (elems.opmode_notif &&
2413 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2414 u8 nss;
2415
2416 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2417 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2418 nss += 1;
2419 sta->sta.rx_nss = nss;
2420 }
2421
Johannes Berg4b7679a2008-09-18 18:14:18 +02002422 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002423
Johannes Berg46900292009-02-15 12:44:28 +01002424 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002425 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002426
Johannes Bergddf4ac52008-10-22 11:41:38 +02002427 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002428 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002429
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002430 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002431 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2432 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2433 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002434 sdata_info(sdata,
2435 "failed to move station %pM to desired state\n",
2436 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002437 WARN_ON(__sta_info_destroy(sta));
2438 mutex_unlock(&sdata->local->sta_mtx);
2439 return false;
2440 }
2441
Johannes Berg7852e362012-01-20 13:55:24 +01002442 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002443
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002444 /*
2445 * Always handle WMM once after association regardless
2446 * of the first value the AP uses. Setting -1 here has
2447 * that effect because the AP values is an unsigned
2448 * 4-bit value.
2449 */
2450 ifmgd->wmm_last_param_set = -1;
2451
Johannes Bergddf4ac52008-10-22 11:41:38 +02002452 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002453 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002454 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002455 else
Johannes Berg3abead52012-03-02 15:56:59 +01002456 ieee80211_set_wmm_default(sdata, false);
2457 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002458
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002459 /* set AID and assoc capability,
2460 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002461 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002462 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002463 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002464
Kalle Valo15b7b062009-03-22 21:57:14 +02002465 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002466 * If we're using 4-addr mode, let the AP know that we're
2467 * doing so, so that it can create the STA VLAN on its side
2468 */
2469 if (ifmgd->use_4addr)
2470 ieee80211_send_4addr_nullfunc(local, sdata);
2471
2472 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002473 * Start timer to probe the connection to the AP now.
2474 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002475 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002476 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002477 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002478
Johannes Bergaf6b6372009-12-23 13:15:35 +01002479 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002480}
2481
Johannes Berg66e67e42012-01-20 13:55:27 +01002482static enum rx_mgmt_action __must_check
2483ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2484 struct ieee80211_mgmt *mgmt, size_t len,
2485 struct cfg80211_bss **bss)
2486{
2487 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2488 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2489 u16 capab_info, status_code, aid;
2490 struct ieee802_11_elems elems;
2491 u8 *pos;
2492 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002493
Johannes Berg66e67e42012-01-20 13:55:27 +01002494 lockdep_assert_held(&ifmgd->mtx);
2495
2496 if (!assoc_data)
2497 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002498 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002499 return RX_MGMT_NONE;
2500
2501 /*
2502 * AssocResp and ReassocResp have identical structure, so process both
2503 * of them in this function.
2504 */
2505
2506 if (len < 24 + 6)
2507 return RX_MGMT_NONE;
2508
2509 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2510 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2511 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2512 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2513
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002514 sdata_info(sdata,
2515 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2516 reassoc ? "Rea" : "A", mgmt->sa,
2517 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002518
2519 pos = mgmt->u.assoc_resp.variable;
2520 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2521
2522 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2523 elems.timeout_int && elems.timeout_int_len == 5 &&
2524 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2525 u32 tu, ms;
2526 tu = get_unaligned_le32(elems.timeout_int + 1);
2527 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002528 sdata_info(sdata,
2529 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2530 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002531 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002532 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002533 if (ms > IEEE80211_ASSOC_TIMEOUT)
2534 run_again(ifmgd, assoc_data->timeout);
2535 return RX_MGMT_NONE;
2536 }
2537
2538 *bss = assoc_data->bss;
2539
2540 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002541 sdata_info(sdata, "%pM denied association (code=%d)\n",
2542 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002543 ieee80211_destroy_assoc_data(sdata, false);
2544 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002545 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2546 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002547 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002548 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002549 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2550 }
John W. Linville635d9992012-07-09 16:34:34 -04002551 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002552
2553 /*
2554 * destroy assoc_data afterwards, as otherwise an idle
2555 * recalc after assoc_data is NULL but before associated
2556 * is set can cause the interface to go idle
2557 */
2558 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002559 }
2560
2561 return RX_MGMT_CFG80211_RX_ASSOC;
2562}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002563
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002564static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002565 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002566 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002567 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002568{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002569 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002570 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002571 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002572 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002573 bool need_ps = false;
2574
Johannes Berg826262c2012-12-10 16:38:14 +02002575 if ((sdata->u.mgd.associated &&
2576 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2577 (sdata->u.mgd.assoc_data &&
2578 ether_addr_equal(mgmt->bssid,
2579 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002580 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002581 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2582
2583 if (elems->tim && !elems->parse_error) {
2584 struct ieee80211_tim_ie *tim_ie = elems->tim;
2585 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2586 }
Johannes Berg56007a02010-01-26 14:19:52 +01002587 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002588
Johannes Berg5bda6172008-09-08 11:05:10 +02002589 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002590 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2591 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002592 else
2593 freq = rx_status->freq;
2594
2595 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2596
2597 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2598 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002599
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002600 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002601 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002602 if (bss)
2603 ieee80211_rx_bss_put(local, bss);
2604
2605 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002606 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002607
Johannes Berg56007a02010-01-26 14:19:52 +01002608 if (need_ps) {
2609 mutex_lock(&local->iflist_mtx);
2610 ieee80211_recalc_ps(local, -1);
2611 mutex_unlock(&local->iflist_mtx);
2612 }
2613
Johannes Berg5bc14202012-08-01 16:13:02 +02002614 if (elems->ch_switch_ie &&
2615 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2616 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002617 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002618}
2619
2620
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002621static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002622 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002623{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002624 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002625 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002626 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2627 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002628 struct ieee802_11_elems elems;
2629
Kalle Valo15b7b062009-03-22 21:57:14 +02002630 ifmgd = &sdata->u.mgd;
2631
Johannes Berg77fdaa12009-07-07 03:45:17 +02002632 ASSERT_MGD_MTX(ifmgd);
2633
Joe Perchesb203ca32012-05-08 18:56:52 +00002634 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002635 return; /* ignore ProbeResp to foreign address */
2636
Ester Kummerae6a44e2008-06-27 18:54:48 +03002637 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2638 if (baselen > len)
2639 return;
2640
2641 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2642 &elems);
2643
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002644 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002645
Johannes Berg77fdaa12009-07-07 03:45:17 +02002646 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002647 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002648 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002649
2650 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002651 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002652 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002653 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002654 ifmgd->auth_data->tries = 0;
2655 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002656 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002657 run_again(ifmgd, ifmgd->auth_data->timeout);
2658 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002659}
2660
Johannes Bergd91f36d2009-04-16 13:17:26 +02002661/*
2662 * This is the canonical list of information elements we care about,
2663 * the filter code also gives us all changes to the Microsoft OUI
2664 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2665 *
2666 * We implement beacon filtering in software since that means we can
2667 * avoid processing the frame here and in cfg80211, and userspace
2668 * will not be able to tell whether the hardware supports it or not.
2669 *
2670 * XXX: This list needs to be dynamic -- userspace needs to be able to
2671 * add items it requires. It also needs to be able to tell us to
2672 * look out for other vendor IEs.
2673 */
2674static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002675 (1ULL << WLAN_EID_COUNTRY) |
2676 (1ULL << WLAN_EID_ERP_INFO) |
2677 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2678 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2679 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002680 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002681
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002682static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002683 struct ieee80211_mgmt *mgmt,
2684 size_t len,
2685 struct ieee80211_rx_status *rx_status)
2686{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002687 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002688 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002689 size_t baselen;
2690 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002691 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002692 struct ieee80211_chanctx_conf *chanctx_conf;
2693 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002694 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002695 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002696 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002697 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002698 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002699 u8 *bssid;
2700
Johannes Berg66e67e42012-01-20 13:55:27 +01002701 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002702
Ester Kummerae6a44e2008-06-27 18:54:48 +03002703 /* Process beacon from the current BSS */
2704 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2705 if (baselen > len)
2706 return;
2707
Johannes Berg55de9082012-07-26 17:24:39 +02002708 rcu_read_lock();
2709 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2710 if (!chanctx_conf) {
2711 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002712 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002713 }
2714
Johannes Berg4bf88532012-11-09 11:39:59 +01002715 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002716 rcu_read_unlock();
2717 return;
2718 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002719 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002720 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002721
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002722 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002723 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002724 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2725 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002726
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002727 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002728 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002729 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002730 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2731 sdata->vif.bss_conf.sync_tsf =
2732 le64_to_cpu(mgmt->u.beacon.timestamp);
2733 sdata->vif.bss_conf.sync_device_ts =
2734 rx_status->device_timestamp;
2735 if (elems.tim)
2736 sdata->vif.bss_conf.sync_dtim_count =
2737 elems.tim->dtim_count;
2738 else
2739 sdata->vif.bss_conf.sync_dtim_count = 0;
2740 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002741 /* continue assoc process */
2742 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002743 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002744 run_again(ifmgd, ifmgd->assoc_data->timeout);
2745 return;
2746 }
2747
2748 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002749 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002750 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002751 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002752
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002753 /* Track average RSSI from the Beacon frames of the current AP */
2754 ifmgd->last_beacon_signal = rx_status->signal;
2755 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2756 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002757 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002758 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002759 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002760 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002761 } else {
2762 ifmgd->ave_beacon_signal =
2763 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2764 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2765 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002766 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002767 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002768
2769 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2770 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2771 int sig = ifmgd->ave_beacon_signal;
2772 int last_sig = ifmgd->last_ave_beacon_signal;
2773
2774 /*
2775 * if signal crosses either of the boundaries, invoke callback
2776 * with appropriate parameters
2777 */
2778 if (sig > ifmgd->rssi_max_thold &&
2779 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2780 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002781 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002782 } else if (sig < ifmgd->rssi_min_thold &&
2783 (last_sig >= ifmgd->rssi_max_thold ||
2784 last_sig == 0)) {
2785 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002786 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002787 }
2788 }
2789
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002790 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002791 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002792 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002793 int sig = ifmgd->ave_beacon_signal / 16;
2794 int last_event = ifmgd->last_cqm_event_signal;
2795 int thold = bss_conf->cqm_rssi_thold;
2796 int hyst = bss_conf->cqm_rssi_hyst;
2797 if (sig < thold &&
2798 (last_event == 0 || sig < last_event - hyst)) {
2799 ifmgd->last_cqm_event_signal = sig;
2800 ieee80211_cqm_rssi_notify(
2801 &sdata->vif,
2802 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2803 GFP_KERNEL);
2804 } else if (sig > thold &&
2805 (last_event == 0 || sig > last_event + hyst)) {
2806 ifmgd->last_cqm_event_signal = sig;
2807 ieee80211_cqm_rssi_notify(
2808 &sdata->vif,
2809 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2810 GFP_KERNEL);
2811 }
2812 }
2813
Johannes Bergb291ba12009-07-10 15:29:03 +02002814 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002815 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002816 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002817 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002818 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002819 ieee80211_run_deferred_scan(local);
2820 mutex_unlock(&local->mtx);
2821
Johannes Berg4e751842009-06-10 15:16:52 +02002822 mutex_lock(&local->iflist_mtx);
2823 ieee80211_recalc_ps(local, -1);
2824 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002825 }
2826
Johannes Bergb291ba12009-07-10 15:29:03 +02002827 /*
2828 * Push the beacon loss detection into the future since
2829 * we are processing a beacon from the AP just now.
2830 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002831 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002832
Johannes Bergd91f36d2009-04-16 13:17:26 +02002833 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2834 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2835 len - baselen, &elems,
2836 care_about_ies, ncrc);
2837
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302838 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002839 bool directed_tim = ieee80211_check_tim(elems.tim,
2840 elems.tim_len,
2841 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002842 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002843 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002844 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2845 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2846 ieee80211_hw_config(local,
2847 IEEE80211_CONF_CHANGE_PS);
2848 }
Kalle Valo572e0012009-02-10 17:09:31 +02002849 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302850 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002851 local->pspolling = true;
2852
2853 /*
2854 * Here is assumed that the driver will be
2855 * able to send ps-poll frame and receive a
2856 * response even though power save mode is
2857 * enabled, but some drivers might require
2858 * to disable power save here. This needs
2859 * to be investigated.
2860 */
2861 ieee80211_send_pspoll(local, sdata);
2862 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002863 }
2864 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002865
Johannes Berg488dd7b2012-10-29 20:08:01 +01002866 if (sdata->vif.p2p) {
2867 u8 noa[2];
2868 int ret;
2869
2870 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2871 len - baselen,
2872 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2873 noa, sizeof(noa));
2874 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2875 bss_conf->p2p_oppps = noa[1] & 0x80;
2876 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2877 changed |= BSS_CHANGED_P2P_PS;
2878 sdata->u.mgd.p2p_noa_index = noa[0];
2879 /*
2880 * make sure we update all information, the CRC
2881 * mechanism doesn't look at P2P attributes.
2882 */
2883 ifmgd->beacon_crc_valid = false;
2884 }
2885 }
2886
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002887 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002888 return;
2889 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002890 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002891
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002892 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002893
2894 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2895 elems.wmm_param_len))
2896 changed |= BSS_CHANGED_QOS;
2897
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002898 /*
2899 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002900 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002901 */
2902 if (!bss_conf->dtim_period) {
2903 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2904 if (elems.tim)
2905 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2906 else
2907 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01002908
2909 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2910 sdata->vif.bss_conf.sync_tsf =
2911 le64_to_cpu(mgmt->u.beacon.timestamp);
2912 sdata->vif.bss_conf.sync_device_ts =
2913 rx_status->device_timestamp;
2914 if (elems.tim)
2915 sdata->vif.bss_conf.sync_dtim_count =
2916 elems.tim->dtim_count;
2917 else
2918 sdata->vif.bss_conf.sync_dtim_count = 0;
2919 }
2920
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002921 changed |= BSS_CHANGED_DTIM_PERIOD;
2922 }
2923
Johannes Berg7a5158e2008-10-08 10:59:33 +02002924 if (elems.erp_info && elems.erp_info_len >= 1) {
2925 erp_valid = true;
2926 erp_value = elems.erp_info[0];
2927 } else {
2928 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002929 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002930 changed |= ieee80211_handle_bss_capability(sdata,
2931 le16_to_cpu(mgmt->u.beacon.capab_info),
2932 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002933
Johannes Berg11289582013-02-07 17:36:12 +01002934 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01002935 sta = sta_info_get(sdata, bssid);
2936
Johannes Berg074d46d2012-03-15 19:45:16 +01002937 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002938 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Bergbee7f582013-02-07 22:24:55 +01002939 changed |= ieee80211_config_ht_tx(sdata, sta,
Johannes Berg11289582013-02-07 17:36:12 +01002940 elems.ht_operation,
Johannes Berg24398e32012-03-28 10:58:36 +02002941 bssid, true);
Johannes Bergbee7f582013-02-07 22:24:55 +01002942
2943 if (sta && elems.opmode_notif)
2944 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
2945 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01002946 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002947
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002948 if (elems.country_elem && elems.pwr_constr_elem &&
2949 mgmt->u.probe_resp.capab_info &
2950 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002951 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2952 elems.country_elem,
2953 elems.country_elem_len,
2954 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002955
Johannes Berg471b3ef2007-12-28 14:32:58 +01002956 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002957}
2958
Johannes Berg1fa57d02010-06-10 10:21:32 +02002959void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2960 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002961{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002962 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002963 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002964 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002965 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002966 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002967 u16 fc;
2968
Jiri Bencf0706e82007-05-05 11:45:53 -07002969 rx_status = (struct ieee80211_rx_status *) skb->cb;
2970 mgmt = (struct ieee80211_mgmt *) skb->data;
2971 fc = le16_to_cpu(mgmt->frame_control);
2972
Johannes Berg77fdaa12009-07-07 03:45:17 +02002973 mutex_lock(&ifmgd->mtx);
2974
Johannes Berg66e67e42012-01-20 13:55:27 +01002975 switch (fc & IEEE80211_FCTL_STYPE) {
2976 case IEEE80211_STYPE_BEACON:
2977 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2978 break;
2979 case IEEE80211_STYPE_PROBE_RESP:
2980 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2981 break;
2982 case IEEE80211_STYPE_AUTH:
2983 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2984 break;
2985 case IEEE80211_STYPE_DEAUTH:
2986 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2987 break;
2988 case IEEE80211_STYPE_DISASSOC:
2989 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2990 break;
2991 case IEEE80211_STYPE_ASSOC_RESP:
2992 case IEEE80211_STYPE_REASSOC_RESP:
2993 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2994 break;
2995 case IEEE80211_STYPE_ACTION:
2996 switch (mgmt->u.action.category) {
2997 case WLAN_CATEGORY_SPECTRUM_MGMT:
2998 ieee80211_sta_process_chanswitch(sdata,
2999 &mgmt->u.action.u.chan_switch.sw_elem,
3000 (void *)ifmgd->associated->priv,
3001 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003002 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003003 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003004 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003005 mutex_unlock(&ifmgd->mtx);
3006
Johannes Berg66e67e42012-01-20 13:55:27 +01003007 switch (rma) {
3008 case RX_MGMT_NONE:
3009 /* no action */
3010 break;
3011 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003012 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003013 break;
3014 case RX_MGMT_CFG80211_DISASSOC:
3015 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3016 break;
3017 case RX_MGMT_CFG80211_RX_AUTH:
3018 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3019 break;
3020 case RX_MGMT_CFG80211_RX_ASSOC:
3021 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3022 break;
3023 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3024 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3025 break;
3026 default:
3027 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003028 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003029}
3030
Johannes Berg9c6bd792008-09-11 00:01:52 +02003031static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003032{
3033 struct ieee80211_sub_if_data *sdata =
3034 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01003035 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003036 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003037
Johannes Berg5bb644a2009-05-17 11:40:42 +02003038 if (local->quiescing) {
3039 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3040 return;
3041 }
3042
Johannes Berg64592c82010-06-10 10:21:31 +02003043 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003044}
3045
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003046static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003047 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003048{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003049 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003050 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003051
Johannes Berg37ad3882012-02-24 13:50:54 +01003052 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003053 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003054 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003055
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003056 /*
3057 * must be outside lock due to cfg80211,
3058 * but that's not a problem.
3059 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003060 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003061
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003062 mutex_lock(&ifmgd->mtx);
3063}
3064
Johannes Berg66e67e42012-01-20 13:55:27 +01003065static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3066{
3067 struct ieee80211_local *local = sdata->local;
3068 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3069 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003070 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003071
3072 lockdep_assert_held(&ifmgd->mtx);
3073
3074 if (WARN_ON_ONCE(!auth_data))
3075 return -EINVAL;
3076
Johannes Berg1672c0e2013-01-29 15:02:27 +01003077 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3078 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3079 IEEE80211_TX_INTFL_MLME_CONN_TX;
3080
Johannes Berg66e67e42012-01-20 13:55:27 +01003081 auth_data->tries++;
3082
3083 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003084 sdata_info(sdata, "authentication with %pM timed out\n",
3085 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003086
3087 /*
3088 * Most likely AP is not in the range so remove the
3089 * bss struct for that AP.
3090 */
3091 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3092
3093 return -ETIMEDOUT;
3094 }
3095
Johannes Berga1845fc2012-06-27 13:18:36 +02003096 drv_mgd_prepare_tx(local, sdata);
3097
Johannes Berg66e67e42012-01-20 13:55:27 +01003098 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003099 u16 trans = 1;
3100 u16 status = 0;
3101
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003102 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3103 auth_data->bss->bssid, auth_data->tries,
3104 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003105
3106 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003107
3108 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3109 trans = auth_data->sae_trans;
3110 status = auth_data->sae_status;
3111 auth_data->expected_transaction = trans;
3112 }
3113
3114 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3115 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003116 auth_data->bss->bssid,
Johannes Berg1672c0e2013-01-29 15:02:27 +01003117 auth_data->bss->bssid, NULL, 0, 0,
3118 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003119 } else {
3120 const u8 *ssidie;
3121
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003122 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3123 auth_data->bss->bssid, auth_data->tries,
3124 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003125
Johannes Berg9caf0362012-11-29 01:25:20 +01003126 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003127 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003128 if (!ssidie) {
3129 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003130 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003131 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003132 /*
3133 * Direct probe is sent to broadcast address as some APs
3134 * will not answer to direct packet in unassociated state.
3135 */
3136 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e2013-01-29 15:02:27 +01003137 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003138 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003139 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003140 }
3141
Johannes Berg1672c0e2013-01-29 15:02:27 +01003142 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3143 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003144 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003145 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003146 } else {
3147 auth_data->timeout_started = false;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003148 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003149
3150 return 0;
3151}
3152
3153static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3154{
3155 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3156 struct ieee80211_local *local = sdata->local;
3157
3158 lockdep_assert_held(&sdata->u.mgd.mtx);
3159
Johannes Berg66e67e42012-01-20 13:55:27 +01003160 assoc_data->tries++;
3161 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003162 sdata_info(sdata, "association with %pM timed out\n",
3163 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003164
3165 /*
3166 * Most likely AP is not in the range so remove the
3167 * bss struct for that AP.
3168 */
3169 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3170
3171 return -ETIMEDOUT;
3172 }
3173
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003174 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3175 assoc_data->bss->bssid, assoc_data->tries,
3176 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003177 ieee80211_send_assoc(sdata);
3178
Johannes Berg1672c0e2013-01-29 15:02:27 +01003179 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3180 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003181 assoc_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003182 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003183 } else {
3184 assoc_data->timeout_started = false;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003185 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003186
3187 return 0;
3188}
3189
Johannes Berg1672c0e2013-01-29 15:02:27 +01003190void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3191 __le16 fc, bool acked)
3192{
3193 struct ieee80211_local *local = sdata->local;
3194
3195 sdata->u.mgd.status_fc = fc;
3196 sdata->u.mgd.status_acked = acked;
3197 sdata->u.mgd.status_received = true;
3198
3199 ieee80211_queue_work(&local->hw, &sdata->work);
3200}
3201
Johannes Berg1fa57d02010-06-10 10:21:32 +02003202void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003203{
Johannes Berg60f8b392008-09-08 17:44:22 +02003204 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003205 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003206
Johannes Berg77fdaa12009-07-07 03:45:17 +02003207 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003208
Johannes Berg1672c0e2013-01-29 15:02:27 +01003209 if (ifmgd->status_received) {
3210 __le16 fc = ifmgd->status_fc;
3211 bool status_acked = ifmgd->status_acked;
3212
3213 ifmgd->status_received = false;
3214 if (ifmgd->auth_data &&
3215 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3216 if (status_acked) {
3217 ifmgd->auth_data->timeout =
3218 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3219 run_again(ifmgd, ifmgd->auth_data->timeout);
3220 } else {
3221 ifmgd->auth_data->timeout = jiffies - 1;
3222 }
Johannes Berg89afe612013-02-13 15:39:57 +01003223 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003224 } else if (ifmgd->assoc_data &&
3225 (ieee80211_is_assoc_req(fc) ||
3226 ieee80211_is_reassoc_req(fc))) {
3227 if (status_acked) {
3228 ifmgd->assoc_data->timeout =
3229 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3230 run_again(ifmgd, ifmgd->assoc_data->timeout);
3231 } else {
3232 ifmgd->assoc_data->timeout = jiffies - 1;
3233 }
Johannes Berg89afe612013-02-13 15:39:57 +01003234 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e2013-01-29 15:02:27 +01003235 }
3236 }
3237
Johannes Berg89afe612013-02-13 15:39:57 +01003238 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003239 time_after(jiffies, ifmgd->auth_data->timeout)) {
3240 if (ifmgd->auth_data->done) {
3241 /*
3242 * ok ... we waited for assoc but userspace didn't,
3243 * so let's just kill the auth data
3244 */
3245 ieee80211_destroy_auth_data(sdata, false);
3246 } else if (ieee80211_probe_auth(sdata)) {
3247 u8 bssid[ETH_ALEN];
3248
3249 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3250
3251 ieee80211_destroy_auth_data(sdata, false);
3252
3253 mutex_unlock(&ifmgd->mtx);
3254 cfg80211_send_auth_timeout(sdata->dev, bssid);
3255 mutex_lock(&ifmgd->mtx);
3256 }
Johannes Berg89afe612013-02-13 15:39:57 +01003257 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003258 run_again(ifmgd, ifmgd->auth_data->timeout);
3259
Johannes Berg89afe612013-02-13 15:39:57 +01003260 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003261 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003262 if ((ifmgd->assoc_data->need_beacon &&
3263 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003264 ieee80211_do_assoc(sdata)) {
3265 u8 bssid[ETH_ALEN];
3266
3267 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3268
3269 ieee80211_destroy_assoc_data(sdata, false);
3270
3271 mutex_unlock(&ifmgd->mtx);
3272 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3273 mutex_lock(&ifmgd->mtx);
3274 }
Johannes Berg89afe612013-02-13 15:39:57 +01003275 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003276 run_again(ifmgd, ifmgd->assoc_data->timeout);
3277
Johannes Bergb291ba12009-07-10 15:29:03 +02003278 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3279 IEEE80211_STA_CONNECTION_POLL) &&
3280 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003281 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003282 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003283
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003284 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003285
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003286 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003287 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003288 else
Ben Greear180205b2011-02-04 15:30:24 -08003289 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003290
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003291 /* ACK received for nullfunc probing frame */
3292 if (!ifmgd->probe_send_count)
3293 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003294 else if (ifmgd->nullfunc_failed) {
3295 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003296 mlme_dbg(sdata,
3297 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3298 bssid, ifmgd->probe_send_count,
3299 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003300 ieee80211_mgd_probe_ap_send(sdata);
3301 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003302 mlme_dbg(sdata,
3303 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3304 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003305 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003306 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3307 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003308 }
3309 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003310 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003311 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003312 mlme_dbg(sdata,
3313 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3314 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003315 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003316 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003317 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003318 mlme_dbg(sdata,
3319 "No probe response from AP %pM after %dms, try %d/%i\n",
3320 bssid, probe_wait_ms,
3321 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003322 ieee80211_mgd_probe_ap_send(sdata);
3323 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003324 /*
3325 * We actually lost the connection ... or did we?
3326 * Let's make sure!
3327 */
Ben Greearb38afa82010-10-07 16:12:06 -07003328 wiphy_debug(local->hw.wiphy,
3329 "%s: No probe response from AP %pM"
3330 " after %dms, disconnecting.\n",
3331 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003332 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003333
Johannes Berg95acac62011-07-12 12:30:59 +02003334 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003335 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003336 }
3337 }
3338
Johannes Berg77fdaa12009-07-07 03:45:17 +02003339 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003340}
Johannes Berg0a51b272008-09-08 17:44:25 +02003341
Johannes Bergb291ba12009-07-10 15:29:03 +02003342static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3343{
3344 struct ieee80211_sub_if_data *sdata =
3345 (struct ieee80211_sub_if_data *) data;
3346 struct ieee80211_local *local = sdata->local;
3347
3348 if (local->quiescing)
3349 return;
3350
Johannes Berg682bd382013-01-29 13:13:50 +01003351 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003352 ieee80211_queue_work(&sdata->local->hw,
3353 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003354}
3355
3356static void ieee80211_sta_conn_mon_timer(unsigned long data)
3357{
3358 struct ieee80211_sub_if_data *sdata =
3359 (struct ieee80211_sub_if_data *) data;
3360 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3361 struct ieee80211_local *local = sdata->local;
3362
3363 if (local->quiescing)
3364 return;
3365
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003366 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003367}
3368
3369static void ieee80211_sta_monitor_work(struct work_struct *work)
3370{
3371 struct ieee80211_sub_if_data *sdata =
3372 container_of(work, struct ieee80211_sub_if_data,
3373 u.mgd.monitor_work);
3374
3375 ieee80211_mgd_probe_ap(sdata, false);
3376}
3377
Johannes Berga1678f82008-09-11 00:01:47 +02003378static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3379{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003380 u32 flags;
3381
Kalle Vaload935682009-04-19 08:47:19 +03003382 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003383 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003384
Johannes Bergb291ba12009-07-10 15:29:03 +02003385 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003386 flags = sdata->local->hw.flags;
3387 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3388 ieee80211_queue_work(&sdata->local->hw,
3389 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003390 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003391 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003392 }
Johannes Berga1678f82008-09-11 00:01:47 +02003393}
3394
Johannes Berg5bb644a2009-05-17 11:40:42 +02003395#ifdef CONFIG_PM
3396void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3397{
3398 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3399
3400 /*
3401 * we need to use atomic bitops for the running bits
3402 * only because both timers might fire at the same
3403 * time -- the code here is properly synchronised.
3404 */
3405
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003406 cancel_work_sync(&ifmgd->request_smps_work);
3407
Johannes Berg0ecfe802011-11-09 12:14:16 +01003408 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003409 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003410 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003411 if (del_timer_sync(&ifmgd->timer))
3412 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3413
3414 cancel_work_sync(&ifmgd->chswitch_work);
3415 if (del_timer_sync(&ifmgd->chswitch_timer))
3416 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003417
Johannes Bergb291ba12009-07-10 15:29:03 +02003418 /* these will just be re-established on connection */
3419 del_timer_sync(&ifmgd->conn_mon_timer);
3420 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003421}
3422
3423void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3424{
3425 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3426
Johannes Berg782d2672013-01-18 21:21:31 +01003427 mutex_lock(&ifmgd->mtx);
3428 if (!ifmgd->associated) {
3429 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303430 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003431 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303432
Johannes Berg95acac62011-07-12 12:30:59 +02003433 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3434 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003435 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3436 ieee80211_sta_connection_lost(sdata,
3437 ifmgd->associated->bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003438 WLAN_REASON_UNSPECIFIED,
3439 true);
Johannes Berg95acac62011-07-12 12:30:59 +02003440 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003441 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003442 }
Johannes Berg782d2672013-01-18 21:21:31 +01003443 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003444
Johannes Berg5bb644a2009-05-17 11:40:42 +02003445 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3446 add_timer(&ifmgd->timer);
3447 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3448 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003449 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003450
3451 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003452 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003453 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003454}
3455#endif
3456
Johannes Berg9c6bd792008-09-11 00:01:52 +02003457/* interface setup */
3458void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3459{
Johannes Berg46900292009-02-15 12:44:28 +01003460 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003461
Johannes Berg46900292009-02-15 12:44:28 +01003462 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003463 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003464 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003465 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3466 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003467 INIT_WORK(&ifmgd->csa_connection_drop_work,
3468 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003469 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003470 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003471 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003472 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3473 (unsigned long) sdata);
3474 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3475 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003476 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303477 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003478
Johannes Bergab1faea2009-07-01 21:41:17 +02003479 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303480 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003481 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3482 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003483
Johannes Berg77fdaa12009-07-07 03:45:17 +02003484 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003485
3486 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3487 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3488 else
3489 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003490}
3491
3492/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003493void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3494{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003495 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003496
3497 /* Restart STA timers */
3498 rcu_read_lock();
3499 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3500 ieee80211_restart_sta_timer(sdata);
3501 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003502}
Johannes Berg10f644a2009-04-16 13:17:25 +02003503
3504int ieee80211_max_network_latency(struct notifier_block *nb,
3505 unsigned long data, void *dummy)
3506{
3507 s32 latency_usec = (s32) data;
3508 struct ieee80211_local *local =
3509 container_of(nb, struct ieee80211_local,
3510 network_latency_notifier);
3511
3512 mutex_lock(&local->iflist_mtx);
3513 ieee80211_recalc_ps(local, latency_usec);
3514 mutex_unlock(&local->iflist_mtx);
3515
3516 return 0;
3517}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003518
Johannes Bergf2d9d272012-11-22 14:11:39 +01003519static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3520 struct cfg80211_bss *cbss)
3521{
3522 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3523 const u8 *ht_cap_ie, *vht_cap_ie;
3524 const struct ieee80211_ht_cap *ht_cap;
3525 const struct ieee80211_vht_cap *vht_cap;
3526 u8 chains = 1;
3527
3528 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3529 return chains;
3530
Johannes Berg9caf0362012-11-29 01:25:20 +01003531 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003532 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3533 ht_cap = (void *)(ht_cap_ie + 2);
3534 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3535 /*
3536 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3537 * "Tx Unequal Modulation Supported" fields.
3538 */
3539 }
3540
3541 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3542 return chains;
3543
Johannes Berg9caf0362012-11-29 01:25:20 +01003544 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003545 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3546 u8 nss;
3547 u16 tx_mcs_map;
3548
3549 vht_cap = (void *)(vht_cap_ie + 2);
3550 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3551 for (nss = 8; nss > 0; nss--) {
3552 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3553 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3554 break;
3555 }
3556 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3557 chains = max(chains, nss);
3558 }
3559
3560 return chains;
3561}
3562
Johannes Bergb17166a2012-07-27 11:41:27 +02003563static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3564 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003565{
3566 struct ieee80211_local *local = sdata->local;
3567 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003568 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003569 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003570 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003571 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003572 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003573
Johannes Berg24398e32012-03-28 10:58:36 +02003574 sband = local->hw.wiphy->bands[cbss->channel->band];
3575
Johannes Bergf2d9d272012-11-22 14:11:39 +01003576 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3577 IEEE80211_STA_DISABLE_80P80MHZ |
3578 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003579
Johannes Berg9caf0362012-11-29 01:25:20 +01003580 rcu_read_lock();
3581
Johannes Bergf2d9d272012-11-22 14:11:39 +01003582 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3583 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003584 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003585
Johannes Berg9caf0362012-11-29 01:25:20 +01003586 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003587 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3588 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003589
3590 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3591 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3592 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3593 ht_oper = NULL;
3594 }
Johannes Berg24398e32012-03-28 10:58:36 +02003595 }
3596
Johannes Bergf2d9d272012-11-22 14:11:39 +01003597 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3598 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003599 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003600
Johannes Berg9caf0362012-11-29 01:25:20 +01003601 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3602 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003603 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3604 vht_oper = (void *)(vht_oper_ie + 2);
3605 if (vht_oper && !ht_oper) {
3606 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003607 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003608 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003609 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3610 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003611 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003612
3613 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3614 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3615 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3616 vht_oper = NULL;
3617 }
Johannes Berg24398e32012-03-28 10:58:36 +02003618 }
3619
Johannes Bergf2d9d272012-11-22 14:11:39 +01003620 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3621 cbss->channel,
3622 ht_oper, vht_oper,
3623 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003624
Johannes Bergf2d9d272012-11-22 14:11:39 +01003625 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3626 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003627
Johannes Berg9caf0362012-11-29 01:25:20 +01003628 rcu_read_unlock();
3629
Johannes Berg04ecd252012-09-11 14:34:12 +02003630 /* will change later if needed */
3631 sdata->smps_mode = IEEE80211_SMPS_OFF;
3632
Johannes Bergf2d9d272012-11-22 14:11:39 +01003633 /*
3634 * If this fails (possibly due to channel context sharing
3635 * on incompatible channels, e.g. 80+80 and 160 sharing the
3636 * same control channel) try to use a smaller bandwidth.
3637 */
3638 ret = ieee80211_vif_use_channel(sdata, &chandef,
3639 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003640 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003641 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003642 ret = ieee80211_vif_use_channel(sdata, &chandef,
3643 IEEE80211_CHANCTX_SHARED);
3644 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003645 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003646}
3647
3648static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3649 struct cfg80211_bss *cbss, bool assoc)
3650{
3651 struct ieee80211_local *local = sdata->local;
3652 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3653 struct ieee80211_bss *bss = (void *)cbss->priv;
3654 struct sta_info *new_sta = NULL;
3655 bool have_sta = false;
3656 int err;
3657
3658 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3659 return -EINVAL;
3660
3661 if (assoc) {
3662 rcu_read_lock();
3663 have_sta = sta_info_get(sdata, cbss->bssid);
3664 rcu_read_unlock();
3665 }
3666
3667 if (!have_sta) {
3668 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3669 if (!new_sta)
3670 return -ENOMEM;
3671 }
3672
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003673 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003674 u32 rates = 0, basic_rates = 0;
3675 bool have_higher_than_11mbit;
3676 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003677 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003678 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003679
3680 sband = local->hw.wiphy->bands[cbss->channel->band];
3681
3682 err = ieee80211_prep_channel(sdata, cbss);
3683 if (err) {
3684 sta_info_free(local, new_sta);
3685 return err;
3686 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003687
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003688 ieee80211_get_rates(sband, bss->supp_rates,
3689 bss->supp_rates_len,
3690 &rates, &basic_rates,
3691 &have_higher_than_11mbit,
3692 &min_rate, &min_rate_index);
3693
3694 /*
3695 * This used to be a workaround for basic rates missing
3696 * in the association response frame. Now that we no
3697 * longer use the basic rates from there, it probably
3698 * doesn't happen any more, but keep the workaround so
3699 * in case some *other* APs are buggy in different ways
3700 * we can connect -- with a warning.
3701 */
3702 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003703 sdata_info(sdata,
3704 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003705 basic_rates = BIT(min_rate_index);
3706 }
3707
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003708 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003709 sdata->vif.bss_conf.basic_rates = basic_rates;
3710
3711 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003712 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003713 have_higher_than_11mbit)
3714 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3715 else
3716 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3717
3718 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3719
Johannes Berg8c358bc2012-05-22 22:13:05 +02003720 /* set timing information */
3721 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003722 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003723 ies = rcu_dereference(cbss->beacon_ies);
3724 if (ies) {
3725 const u8 *tim_ie;
3726
3727 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3728 sdata->vif.bss_conf.sync_device_ts =
3729 bss->device_ts_beacon;
3730 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3731 ies->data, ies->len);
3732 if (tim_ie && tim_ie[1] >= 2)
3733 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3734 else
3735 sdata->vif.bss_conf.sync_dtim_count = 0;
3736 } else if (!(local->hw.flags &
3737 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3738 ies = rcu_dereference(cbss->proberesp_ies);
3739 /* must be non-NULL since beacon IEs were NULL */
3740 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3741 sdata->vif.bss_conf.sync_device_ts =
3742 bss->device_ts_presp;
3743 sdata->vif.bss_conf.sync_dtim_count = 0;
3744 } else {
3745 sdata->vif.bss_conf.sync_tsf = 0;
3746 sdata->vif.bss_conf.sync_device_ts = 0;
3747 sdata->vif.bss_conf.sync_dtim_count = 0;
3748 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003749 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003750
3751 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003752 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003753 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3754 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003755
3756 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003757 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003758
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003759 err = sta_info_insert(new_sta);
3760 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003761 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003762 sdata_info(sdata,
3763 "failed to insert STA entry for the AP (error %d)\n",
3764 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003765 return err;
3766 }
3767 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003768 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003769
3770 return 0;
3771}
3772
Johannes Berg77fdaa12009-07-07 03:45:17 +02003773/* config hooks */
3774int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3775 struct cfg80211_auth_request *req)
3776{
Johannes Berg66e67e42012-01-20 13:55:27 +01003777 struct ieee80211_local *local = sdata->local;
3778 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3779 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003780 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003781 int err;
3782
3783 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003784
3785 switch (req->auth_type) {
3786 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3787 auth_alg = WLAN_AUTH_OPEN;
3788 break;
3789 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003790 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003791 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003792 auth_alg = WLAN_AUTH_SHARED_KEY;
3793 break;
3794 case NL80211_AUTHTYPE_FT:
3795 auth_alg = WLAN_AUTH_FT;
3796 break;
3797 case NL80211_AUTHTYPE_NETWORK_EAP:
3798 auth_alg = WLAN_AUTH_LEAP;
3799 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003800 case NL80211_AUTHTYPE_SAE:
3801 auth_alg = WLAN_AUTH_SAE;
3802 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003803 default:
3804 return -EOPNOTSUPP;
3805 }
3806
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003807 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3808 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003809 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003810 return -ENOMEM;
3811
Johannes Berg66e67e42012-01-20 13:55:27 +01003812 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003813
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003814 if (req->sae_data_len >= 4) {
3815 __le16 *pos = (__le16 *) req->sae_data;
3816 auth_data->sae_trans = le16_to_cpu(pos[0]);
3817 auth_data->sae_status = le16_to_cpu(pos[1]);
3818 memcpy(auth_data->data, req->sae_data + 4,
3819 req->sae_data_len - 4);
3820 auth_data->data_len += req->sae_data_len - 4;
3821 }
3822
Johannes Berg77fdaa12009-07-07 03:45:17 +02003823 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003824 memcpy(&auth_data->data[auth_data->data_len],
3825 req->ie, req->ie_len);
3826 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003827 }
3828
Johannes Bergfffd0932009-07-08 14:22:54 +02003829 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003830 auth_data->key_len = req->key_len;
3831 auth_data->key_idx = req->key_idx;
3832 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003833 }
3834
Johannes Berg66e67e42012-01-20 13:55:27 +01003835 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003836
Johannes Berg66e67e42012-01-20 13:55:27 +01003837 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003838
Johannes Berg66e67e42012-01-20 13:55:27 +01003839 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003840
Johannes Berg66e67e42012-01-20 13:55:27 +01003841 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3842 ifmgd->assoc_data) {
3843 err = -EBUSY;
3844 goto err_free;
3845 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003846
Johannes Berg66e67e42012-01-20 13:55:27 +01003847 if (ifmgd->auth_data)
3848 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003849
Johannes Berg66e67e42012-01-20 13:55:27 +01003850 /* prep auth_data so we don't go into idle on disassoc */
3851 ifmgd->auth_data = auth_data;
3852
3853 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003854 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003855
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003856 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003857
Johannes Berga1cf7752012-03-08 15:02:07 +01003858 err = ieee80211_prep_connection(sdata, req->bss, false);
3859 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003860 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003861
Johannes Berg66e67e42012-01-20 13:55:27 +01003862 err = ieee80211_probe_auth(sdata);
3863 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003864 sta_info_destroy_addr(sdata, req->bss->bssid);
3865 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003866 }
3867
Johannes Berg66e67e42012-01-20 13:55:27 +01003868 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003869 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003870 err = 0;
3871 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003872
Johannes Berg66e67e42012-01-20 13:55:27 +01003873 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003874 memset(ifmgd->bssid, 0, ETH_ALEN);
3875 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003876 ifmgd->auth_data = NULL;
3877 err_free:
3878 kfree(auth_data);
3879 out_unlock:
3880 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003881
Johannes Berg66e67e42012-01-20 13:55:27 +01003882 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003883}
3884
Johannes Berg77fdaa12009-07-07 03:45:17 +02003885int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3886 struct cfg80211_assoc_request *req)
3887{
Johannes Berg66e67e42012-01-20 13:55:27 +01003888 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003889 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003890 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003891 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003892 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003893 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003894 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003895 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003896
Johannes Berg66e67e42012-01-20 13:55:27 +01003897 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3898 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003899 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003900
Johannes Berg9caf0362012-11-29 01:25:20 +01003901 rcu_read_lock();
3902 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3903 if (!ssidie) {
3904 rcu_read_unlock();
3905 kfree(assoc_data);
3906 return -EINVAL;
3907 }
3908 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3909 assoc_data->ssid_len = ssidie[1];
3910 rcu_read_unlock();
3911
Johannes Berg66e67e42012-01-20 13:55:27 +01003912 mutex_lock(&ifmgd->mtx);
3913
3914 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003915 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003916
3917 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3918 err = -EBUSY;
3919 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003920 }
3921
Johannes Berg66e67e42012-01-20 13:55:27 +01003922 if (ifmgd->assoc_data) {
3923 err = -EBUSY;
3924 goto err_free;
3925 }
3926
3927 if (ifmgd->auth_data) {
3928 bool match;
3929
3930 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003931 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003932 ieee80211_destroy_auth_data(sdata, match);
3933 }
3934
3935 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003936
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003937 ifmgd->beacon_crc_valid = false;
3938
Johannes Bergde5036a2012-03-08 15:02:03 +01003939 /*
3940 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3941 * We still associate in non-HT mode (11a/b/g) if any one of these
3942 * ciphers is configured as pairwise.
3943 * We can set this to true for non-11n hardware, that'll be checked
3944 * separately along with the peer capabilities.
3945 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003946 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003947 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3948 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003949 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003950 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003951 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003952 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003953 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003954 }
3955 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003956
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003957 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003958 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003959 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3960 }
Ben Greearef96a8422011-11-18 11:32:00 -08003961
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003962 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3963 sband = local->hw.wiphy->bands[req->bss->channel->band];
3964 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003965 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003966 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003967 if (!bss->wmm_used)
3968 netdev_info(sdata->dev,
3969 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003970 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003971
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003972 /* disable VHT if we don't support it or the AP doesn't use WMM */
3973 if (!sband->vht_cap.vht_supported ||
3974 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3975 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003976 if (!bss->wmm_used)
3977 netdev_info(sdata->dev,
3978 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003979 }
3980
Ben Greearef96a8422011-11-18 11:32:00 -08003981 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3982 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3983 sizeof(ifmgd->ht_capa_mask));
3984
Johannes Berg77fdaa12009-07-07 03:45:17 +02003985 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003986 memcpy(assoc_data->ie, req->ie, req->ie_len);
3987 assoc_data->ie_len = req->ie_len;
3988 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003989
Johannes Berg66e67e42012-01-20 13:55:27 +01003990 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003991
Johannes Bergaf6b6372009-12-23 13:15:35 +01003992 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3993 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003994 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003995 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003996 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003997 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003998 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003999
Johannes Berg66e67e42012-01-20 13:55:27 +01004000 assoc_data->capability = req->bss->capability;
Johannes Berg32c5057b2012-03-28 11:04:29 +02004001 assoc_data->wmm = bss->wmm_used &&
4002 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004003 assoc_data->supp_rates = bss->supp_rates;
4004 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004005
Johannes Berg9caf0362012-11-29 01:25:20 +01004006 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004007 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4008 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4009 assoc_data->ap_ht_param =
4010 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4011 else
Johannes Berga8243b72012-11-22 14:32:09 +01004012 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004013 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4014 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4015 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4016 sizeof(struct ieee80211_vht_cap));
4017 else
4018 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004019 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004020
Kalle Valoab133152010-01-12 10:42:31 +02004021 if (bss->wmm_used && bss->uapsd_supported &&
4022 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01004023 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004024 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4025 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004026 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004027 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4028 }
4029
Johannes Berg77fdaa12009-07-07 03:45:17 +02004030 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004031 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004032
4033 if (req->use_mfp) {
4034 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4035 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4036 } else {
4037 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4038 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4039 }
4040
4041 if (req->crypto.control_port)
4042 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4043 else
4044 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4045
Johannes Berga621fa42010-08-27 14:26:54 +03004046 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4047 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4048
Johannes Berg66e67e42012-01-20 13:55:27 +01004049 /* kick off associate process */
4050
4051 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004052 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004053
Johannes Berga1cf7752012-03-08 15:02:07 +01004054 err = ieee80211_prep_connection(sdata, req->bss, true);
4055 if (err)
4056 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004057
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004058 rcu_read_lock();
4059 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004060
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004061 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4062 !beacon_ies) {
4063 /*
4064 * Wait up to one beacon interval ...
4065 * should this be more if we miss one?
4066 */
4067 sdata_info(sdata, "waiting for beacon from %pM\n",
4068 ifmgd->bssid);
4069 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004070 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004071 assoc_data->need_beacon = true;
4072 } else if (beacon_ies) {
4073 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4074 beacon_ies->data,
4075 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004076 u8 dtim_count = 0;
4077
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004078 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4079 const struct ieee80211_tim_ie *tim;
4080 tim = (void *)(tim_ie + 2);
4081 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004082 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004083 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004084 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004085 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004086 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004087
4088 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4089 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4090 sdata->vif.bss_conf.sync_device_ts =
4091 bss->device_ts_beacon;
4092 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4093 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004094 } else {
4095 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004096 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004097 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004098 rcu_read_unlock();
4099
Johannes Berg66e67e42012-01-20 13:55:27 +01004100 run_again(ifmgd, assoc_data->timeout);
4101
Paul Stewartfcff4f12012-02-23 17:59:53 -08004102 if (bss->corrupt_data) {
4103 char *corrupt_type = "data";
4104 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4105 if (bss->corrupt_data &
4106 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4107 corrupt_type = "beacon and probe response";
4108 else
4109 corrupt_type = "beacon";
4110 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4111 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004112 sdata_info(sdata, "associating with AP with corrupt %s\n",
4113 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004114 }
4115
Johannes Berg66e67e42012-01-20 13:55:27 +01004116 err = 0;
4117 goto out;
4118 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004119 memset(ifmgd->bssid, 0, ETH_ALEN);
4120 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004121 ifmgd->assoc_data = NULL;
4122 err_free:
4123 kfree(assoc_data);
4124 out:
4125 mutex_unlock(&ifmgd->mtx);
4126
4127 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004128}
4129
4130int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004131 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004132{
4133 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004134 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004135 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004136 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004137
Johannes Berg77fdaa12009-07-07 03:45:17 +02004138 mutex_lock(&ifmgd->mtx);
4139
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004140 sdata_info(sdata,
4141 "deauthenticating from %pM by local choice (reason=%d)\n",
4142 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004143
Johannes Berg86552012012-10-29 09:46:31 +01004144 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004145 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004146 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4147 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004148 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004149 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004150 ieee80211_destroy_auth_data(sdata, false);
4151 mutex_unlock(&ifmgd->mtx);
4152
4153 sent_frame = tx;
4154 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004155 }
4156
Johannes Berg86552012012-10-29 09:46:31 +01004157 if (ifmgd->associated &&
4158 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4159 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4160 req->reason_code, tx, frame_buf);
4161 sent_frame = tx;
4162 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004163 mutex_unlock(&ifmgd->mtx);
4164
Johannes Berg86552012012-10-29 09:46:31 +01004165 out:
Johannes Berg86552012012-10-29 09:46:31 +01004166 if (sent_frame)
4167 __cfg80211_send_deauth(sdata->dev, frame_buf,
4168 IEEE80211_DEAUTH_FRAME_LEN);
4169
Johannes Berg77fdaa12009-07-07 03:45:17 +02004170 return 0;
4171}
4172
4173int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004174 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004175{
4176 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004177 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004178 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004179
Johannes Berg77fdaa12009-07-07 03:45:17 +02004180 mutex_lock(&ifmgd->mtx);
4181
Johannes Berg8d8b2612009-07-25 11:58:36 +02004182 /*
4183 * cfg80211 should catch this ... but it's racy since
4184 * we can receive a disassoc frame, process it, hand it
4185 * to cfg80211 while that's in a locked section already
4186 * trying to tell us that the user wants to disconnect.
4187 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004188 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004189 mutex_unlock(&ifmgd->mtx);
4190 return -ENOLINK;
4191 }
4192
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004193 sdata_info(sdata,
4194 "disassociating from %pM by local choice (reason=%d)\n",
4195 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004196
Jouni Malinene69e95d2010-03-29 23:29:31 -07004197 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004198 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4199 req->reason_code, !req->local_state_change,
4200 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004201 mutex_unlock(&ifmgd->mtx);
4202
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004203 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4204 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004205
Johannes Berg77fdaa12009-07-07 03:45:17 +02004206 return 0;
4207}
Jouni Malinen026331c2010-02-15 12:53:10 +02004208
Eliad Pellerafa762f2012-04-23 14:45:15 +03004209void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004210{
4211 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4212
4213 mutex_lock(&ifmgd->mtx);
4214 if (ifmgd->assoc_data)
4215 ieee80211_destroy_assoc_data(sdata, false);
4216 if (ifmgd->auth_data)
4217 ieee80211_destroy_auth_data(sdata, false);
4218 del_timer_sync(&ifmgd->timer);
4219 mutex_unlock(&ifmgd->mtx);
4220}
4221
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004222void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4223 enum nl80211_cqm_rssi_threshold_event rssi_event,
4224 gfp_t gfp)
4225{
4226 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4227
Johannes Bergb5878a22010-04-07 16:48:40 +02004228 trace_api_cqm_rssi_notify(sdata, rssi_event);
4229
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004230 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4231}
4232EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);