blob: edba1d8158fc2e9f9c37601cf4564620b8dec88b [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 Berg66e67e42012-01-20 13:55:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_MAX_TRIES 3
35#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37#define IEEE80211_ASSOC_MAX_TRIES 3
38
Ben Greear180205b2011-02-04 15:30:24 -080039static int max_nullfunc_tries = 2;
40module_param(max_nullfunc_tries, int, 0644);
41MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44static int max_probe_tries = 5;
45module_param(max_probe_tries, int, 0644);
46MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020048
49/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010050 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020056 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010057#define IEEE80211_BEACON_LOSS_COUNT 7
58
Johannes Bergb291ba12009-07-10 15:29:03 +020059/*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030063#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
Ben Greear180205b2011-02-04 15:30:24 -080069static int probe_wait_ms = 500;
70module_param(probe_wait_ms, int, 0644);
71MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070074
Jouni Malinen17e4ec12010-03-29 23:28:30 -070075/*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80#define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
Jouni Malinen391a2002010-08-27 22:22:00 +030082/*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
Johannes Berg5bb644a2009-05-17 11:40:42 +020088#define TMR_RUNNING_TIMER 0
89#define TMR_RUNNING_CHANSW 1
90
Johannes Berg77fdaa12009-07-07 03:45:17 +020091/*
92 * All cfg80211 functions have to be called outside a locked
93 * section so that they can acquire a lock themselves... This
94 * is much simpler than queuing up things in cfg80211, but we
95 * do need some indirection for that here.
96 */
97enum rx_mgmt_action {
98 /* no action required */
99 RX_MGMT_NONE,
100
Johannes Berg77fdaa12009-07-07 03:45:17 +0200101 /* caller must call cfg80211_send_deauth() */
102 RX_MGMT_CFG80211_DEAUTH,
103
104 /* caller must call cfg80211_send_disassoc() */
105 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100106
107 /* caller must call cfg80211_send_rx_auth() */
108 RX_MGMT_CFG80211_RX_AUTH,
109
110 /* caller must call cfg80211_send_rx_assoc() */
111 RX_MGMT_CFG80211_RX_ASSOC,
112
113 /* caller must call cfg80211_send_assoc_timeout() */
114 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200115};
116
Johannes Berg5484e232008-09-08 17:44:27 +0200117/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200118static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
119{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200120 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200121}
122
Johannes Bergca386f32009-07-10 02:39:48 +0200123/*
124 * We can have multiple work items (and connection probing)
125 * scheduling this timer, but we need to take care to only
126 * reschedule it when it should fire _earlier_ than it was
127 * asked for before, or if it's not pending right now. This
128 * function ensures that. Note that it then is required to
129 * run this function for all timeouts after the first one
130 * has happened -- the work that runs from this timer will
131 * do that.
132 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100133static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200134{
135 ASSERT_MGD_MTX(ifmgd);
136
137 if (!timer_pending(&ifmgd->timer) ||
138 time_before(timeout, ifmgd->timer.expires))
139 mod_timer(&ifmgd->timer, timeout);
140}
141
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400142void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200143{
Johannes Bergc1288b12012-01-19 09:29:57 +0100144 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145 return;
146
147 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100148 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200149}
150
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400151void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
152{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
154
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100155 if (unlikely(!sdata->u.mgd.associated))
156 return;
157
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400158 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
159 return;
160
161 mod_timer(&sdata->u.mgd.conn_mon_timer,
162 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400163
164 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400165}
166
Johannes Berg5484e232008-09-08 17:44:27 +0200167static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200168{
Johannes Berg5484e232008-09-08 17:44:27 +0200169 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200170}
171
Johannes Bergd5522e02009-03-30 13:23:35 +0200172/*
173 * ieee80211_enable_ht should be called only after the operating band
174 * has been determined as ht configuration depends on the hw's
175 * HT abilities for a specific band.
176 */
177static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
178 struct ieee80211_ht_info *hti,
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530179 const u8 *bssid, u16 ap_ht_cap_flags,
180 bool beacon_htcap_ie)
Johannes Bergd5522e02009-03-30 13:23:35 +0200181{
182 struct ieee80211_local *local = sdata->local;
183 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200184 struct sta_info *sta;
185 u32 changed = 0;
Ben Greear172710b2011-01-28 17:05:43 -0800186 int hti_cfreq;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200187 u16 ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200188 bool enable_ht = true;
189 enum nl80211_channel_type prev_chantype;
Johannes Bergd5522e02009-03-30 13:23:35 +0200190 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
191
192 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
193
Johannes Berg0aaffa92010-05-05 15:28:27 +0200194 prev_chantype = sdata->vif.bss_conf.channel_type;
195
Johannes Bergd5522e02009-03-30 13:23:35 +0200196 /* HT is not supported */
197 if (!sband->ht_cap.ht_supported)
198 enable_ht = false;
199
Ben Greear172710b2011-01-28 17:05:43 -0800200 if (enable_ht) {
201 hti_cfreq = ieee80211_channel_to_frequency(hti->control_chan,
202 sband->band);
203 /* check that channel matches the right operating channel */
204 if (local->hw.conf.channel->center_freq != hti_cfreq) {
205 /* Some APs mess this up, evidently.
206 * Netgear WNDR3700 sometimes reports 4 higher than
207 * the actual channel, for instance.
208 */
209 printk(KERN_DEBUG
210 "%s: Wrong control channel in association"
211 " response: configured center-freq: %d"
212 " hti-cfreq: %d hti->control_chan: %d"
213 " band: %d. Disabling HT.\n",
214 sdata->name,
215 local->hw.conf.channel->center_freq,
216 hti_cfreq, hti->control_chan,
217 sband->band);
218 enable_ht = false;
219 }
220 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200221
222 if (enable_ht) {
223 channel_type = NL80211_CHAN_HT20;
224
225 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
Ben Greearef96a8422011-11-18 11:32:00 -0800226 !ieee80111_cfg_override_disables_ht40(sdata) &&
Johannes Bergd5522e02009-03-30 13:23:35 +0200227 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
228 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
229 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
230 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400231 if (!(local->hw.conf.channel->flags &
232 IEEE80211_CHAN_NO_HT40PLUS))
233 channel_type = NL80211_CHAN_HT40PLUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200234 break;
235 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400236 if (!(local->hw.conf.channel->flags &
237 IEEE80211_CHAN_NO_HT40MINUS))
238 channel_type = NL80211_CHAN_HT40MINUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200239 break;
240 }
241 }
242 }
243
Reinette Chatrefe6f2122010-04-19 10:46:31 -0700244 if (local->tmp_channel)
245 local->tmp_channel_type = channel_type;
Johannes Bergd5522e02009-03-30 13:23:35 +0200246
Johannes Berg0aaffa92010-05-05 15:28:27 +0200247 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
248 /* can only fail due to HT40+/- mismatch */
249 channel_type = NL80211_CHAN_HT20;
250 WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
251 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200252
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530253 if (beacon_htcap_ie && (prev_chantype != channel_type)) {
254 /*
255 * Whenever the AP announces the HT mode change that can be
256 * 40MHz intolerant or etc., it would be safer to stop tx
257 * queues before doing hw config to avoid buffer overflow.
258 */
259 ieee80211_stop_queues_by_reason(&sdata->local->hw,
260 IEEE80211_QUEUE_STOP_REASON_CHTYPE_CHANGE);
261
262 /* flush out all packets */
263 synchronize_net();
264
265 drv_flush(local, false);
266 }
267
Johannes Berg0aaffa92010-05-05 15:28:27 +0200268 /* channel_type change automatically detected */
269 ieee80211_hw_config(local, 0);
270
271 if (prev_chantype != channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200272 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100273 sta = sta_info_get(sdata, bssid);
Johannes Bergd5522e02009-03-30 13:23:35 +0200274 if (sta)
275 rate_control_rate_update(local, sband, sta,
Sujith4fa00432010-03-01 14:42:57 +0530276 IEEE80211_RC_HT_CHANGED,
Johannes Berg0aaffa92010-05-05 15:28:27 +0200277 channel_type);
Johannes Bergd5522e02009-03-30 13:23:35 +0200278 rcu_read_unlock();
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530279
280 if (beacon_htcap_ie)
281 ieee80211_wake_queues_by_reason(&sdata->local->hw,
282 IEEE80211_QUEUE_STOP_REASON_CHTYPE_CHANGE);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200283 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200284
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200285 ht_opmode = le16_to_cpu(hti->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200286
287 /* if bss configuration changed store the new one */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200288 if (sdata->ht_opmode_valid != enable_ht ||
289 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
290 prev_chantype != channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200291 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200292 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200293 sdata->ht_opmode_valid = enable_ht;
Johannes Bergd5522e02009-03-30 13:23:35 +0200294 }
295
296 return changed;
297}
298
Johannes Berg9c6bd792008-09-11 00:01:52 +0200299/* frame sending functions */
300
Johannes Berg66e67e42012-01-20 13:55:27 +0100301static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
302 struct ieee80211_supported_band *sband,
303 u32 *rates)
304{
305 int i, j, count;
306 *rates = 0;
307 count = 0;
308 for (i = 0; i < supp_rates_len; i++) {
309 int rate = (supp_rates[i] & 0x7F) * 5;
310
311 for (j = 0; j < sband->n_bitrates; j++)
312 if (sband->bitrates[j].bitrate == rate) {
313 *rates |= BIT(j);
314 count++;
315 break;
316 }
317 }
318
319 return count;
320}
321
322static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
323 struct sk_buff *skb, const u8 *ht_info_ie,
324 struct ieee80211_supported_band *sband,
325 struct ieee80211_channel *channel,
326 enum ieee80211_smps_mode smps)
327{
328 struct ieee80211_ht_info *ht_info;
329 u8 *pos;
330 u32 flags = channel->flags;
331 u16 cap;
332 struct ieee80211_sta_ht_cap ht_cap;
333
334 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
335
336 if (!sband->ht_cap.ht_supported)
337 return;
338
339 if (!ht_info_ie)
340 return;
341
342 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
343 return;
344
345 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
346 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
347
348 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
349
350 /* determine capability flags */
351 cap = ht_cap.cap;
352
353 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
354 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
355 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
356 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
357 cap &= ~IEEE80211_HT_CAP_SGI_40;
358 }
359 break;
360 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
361 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
362 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
363 cap &= ~IEEE80211_HT_CAP_SGI_40;
364 }
365 break;
366 }
367
368 /* set SM PS mode properly */
369 cap &= ~IEEE80211_HT_CAP_SM_PS;
370 switch (smps) {
371 case IEEE80211_SMPS_AUTOMATIC:
372 case IEEE80211_SMPS_NUM_MODES:
373 WARN_ON(1);
374 case IEEE80211_SMPS_OFF:
375 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
376 IEEE80211_HT_CAP_SM_PS_SHIFT;
377 break;
378 case IEEE80211_SMPS_STATIC:
379 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
380 IEEE80211_HT_CAP_SM_PS_SHIFT;
381 break;
382 case IEEE80211_SMPS_DYNAMIC:
383 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
384 IEEE80211_HT_CAP_SM_PS_SHIFT;
385 break;
386 }
387
388 /* reserve and fill IE */
389 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
390 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
391}
392
393static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
394{
395 struct ieee80211_local *local = sdata->local;
396 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
397 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
398 struct sk_buff *skb;
399 struct ieee80211_mgmt *mgmt;
400 u8 *pos, qos_info;
401 size_t offset = 0, noffset;
402 int i, count, rates_len, supp_rates_len;
403 u16 capab;
404 struct ieee80211_supported_band *sband;
405 u32 rates = 0;
406 struct ieee80211_bss *bss = (void *)assoc_data->bss->priv;
407
408 lockdep_assert_held(&ifmgd->mtx);
409
410 sband = local->hw.wiphy->bands[local->oper_channel->band];
411
412 if (assoc_data->supp_rates_len) {
413 /*
414 * Get all rates supported by the device and the AP as
415 * some APs don't like getting a superset of their rates
416 * in the association request (e.g. D-Link DAP 1353 in
417 * b-only mode)...
418 */
419 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
420 assoc_data->supp_rates_len,
421 sband, &rates);
422 } else {
423 /*
424 * In case AP not provide any supported rates information
425 * before association, we send information element(s) with
426 * all rates that we support.
427 */
428 rates = ~0;
429 rates_len = sband->n_bitrates;
430 }
431
432 skb = alloc_skb(local->hw.extra_tx_headroom +
433 sizeof(*mgmt) + /* bit too much but doesn't matter */
434 2 + assoc_data->ssid_len + /* SSID */
435 4 + rates_len + /* (extended) rates */
436 4 + /* power capability */
437 2 + 2 * sband->n_channels + /* supported channels */
438 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
439 assoc_data->ie_len + /* extra IEs */
440 9, /* WMM */
441 GFP_KERNEL);
442 if (!skb)
443 return;
444
445 skb_reserve(skb, local->hw.extra_tx_headroom);
446
447 capab = WLAN_CAPABILITY_ESS;
448
449 if (sband->band == IEEE80211_BAND_2GHZ) {
450 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
451 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
452 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
453 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
454 }
455
456 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
457 capab |= WLAN_CAPABILITY_PRIVACY;
458
459 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
460 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
461 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
462
463 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
464 memset(mgmt, 0, 24);
465 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
466 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
467 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
468
469 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
470 skb_put(skb, 10);
471 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
472 IEEE80211_STYPE_REASSOC_REQ);
473 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
474 mgmt->u.reassoc_req.listen_interval =
475 cpu_to_le16(local->hw.conf.listen_interval);
476 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
477 ETH_ALEN);
478 } else {
479 skb_put(skb, 4);
480 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
481 IEEE80211_STYPE_ASSOC_REQ);
482 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
483 mgmt->u.assoc_req.listen_interval =
484 cpu_to_le16(local->hw.conf.listen_interval);
485 }
486
487 /* SSID */
488 pos = skb_put(skb, 2 + assoc_data->ssid_len);
489 *pos++ = WLAN_EID_SSID;
490 *pos++ = assoc_data->ssid_len;
491 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
492
493 /* add all rates which were marked to be used above */
494 supp_rates_len = rates_len;
495 if (supp_rates_len > 8)
496 supp_rates_len = 8;
497
498 pos = skb_put(skb, supp_rates_len + 2);
499 *pos++ = WLAN_EID_SUPP_RATES;
500 *pos++ = supp_rates_len;
501
502 count = 0;
503 for (i = 0; i < sband->n_bitrates; i++) {
504 if (BIT(i) & rates) {
505 int rate = sband->bitrates[i].bitrate;
506 *pos++ = (u8) (rate / 5);
507 if (++count == 8)
508 break;
509 }
510 }
511
512 if (rates_len > count) {
513 pos = skb_put(skb, rates_len - count + 2);
514 *pos++ = WLAN_EID_EXT_SUPP_RATES;
515 *pos++ = rates_len - count;
516
517 for (i++; i < sband->n_bitrates; i++) {
518 if (BIT(i) & rates) {
519 int rate = sband->bitrates[i].bitrate;
520 *pos++ = (u8) (rate / 5);
521 }
522 }
523 }
524
525 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
526 /* 1. power capabilities */
527 pos = skb_put(skb, 4);
528 *pos++ = WLAN_EID_PWR_CAPABILITY;
529 *pos++ = 2;
530 *pos++ = 0; /* min tx power */
531 *pos++ = local->oper_channel->max_power; /* max tx power */
532
533 /* 2. supported channels */
534 /* TODO: get this in reg domain format */
535 pos = skb_put(skb, 2 * sband->n_channels + 2);
536 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
537 *pos++ = 2 * sband->n_channels;
538 for (i = 0; i < sband->n_channels; i++) {
539 *pos++ = ieee80211_frequency_to_channel(
540 sband->channels[i].center_freq);
541 *pos++ = 1; /* one channel in the subband*/
542 }
543 }
544
545 /* if present, add any custom IEs that go before HT */
546 if (assoc_data->ie_len && assoc_data->ie) {
547 static const u8 before_ht[] = {
548 WLAN_EID_SSID,
549 WLAN_EID_SUPP_RATES,
550 WLAN_EID_EXT_SUPP_RATES,
551 WLAN_EID_PWR_CAPABILITY,
552 WLAN_EID_SUPPORTED_CHANNELS,
553 WLAN_EID_RSN,
554 WLAN_EID_QOS_CAPA,
555 WLAN_EID_RRM_ENABLED_CAPABILITIES,
556 WLAN_EID_MOBILITY_DOMAIN,
557 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
558 };
559 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
560 before_ht, ARRAY_SIZE(before_ht),
561 offset);
562 pos = skb_put(skb, noffset - offset);
563 memcpy(pos, assoc_data->ie + offset, noffset - offset);
564 offset = noffset;
565 }
566
567 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N) &&
568 bss->wmm_used && local->hw.queues >= 4)
569 ieee80211_add_ht_ie(sdata, skb, assoc_data->ht_information_ie,
570 sband, local->oper_channel, ifmgd->ap_smps);
571
572 /* if present, add any custom non-vendor IEs that go after HT */
573 if (assoc_data->ie_len && assoc_data->ie) {
574 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
575 assoc_data->ie_len,
576 offset);
577 pos = skb_put(skb, noffset - offset);
578 memcpy(pos, assoc_data->ie + offset, noffset - offset);
579 offset = noffset;
580 }
581
582 if (assoc_data->wmm_used && local->hw.queues >= 4) {
583 if (assoc_data->uapsd_used) {
584 qos_info = local->uapsd_queues;
585 qos_info |= (local->uapsd_max_sp_len <<
586 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
587 } else {
588 qos_info = 0;
589 }
590
591 pos = skb_put(skb, 9);
592 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
593 *pos++ = 7; /* len */
594 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
595 *pos++ = 0x50;
596 *pos++ = 0xf2;
597 *pos++ = 2; /* WME */
598 *pos++ = 0; /* WME info */
599 *pos++ = 1; /* WME ver */
600 *pos++ = qos_info;
601 }
602
603 /* add any remaining custom (i.e. vendor specific here) IEs */
604 if (assoc_data->ie_len && assoc_data->ie) {
605 noffset = assoc_data->ie_len;
606 pos = skb_put(skb, noffset - offset);
607 memcpy(pos, assoc_data->ie + offset, noffset - offset);
608 }
609
610 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
611 ieee80211_tx_skb(sdata, skb);
612}
613
Johannes Bergef422bc2008-09-09 10:58:25 +0200614static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100615 const u8 *bssid, u16 stype,
616 u16 reason, bool cfg80211_locked,
617 bool send_frame)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200618{
619 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100620 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200621 struct sk_buff *skb;
622 struct ieee80211_mgmt *mgmt;
623
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200624 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
Joe Perchesd15b8452011-08-29 14:17:31 -0700625 if (!skb)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200626 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700627
Johannes Berg9ac19a92008-09-09 10:57:09 +0200628 skb_reserve(skb, local->hw.extra_tx_headroom);
629
630 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
631 memset(mgmt, 0, 24);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200632 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100633 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200634 memcpy(mgmt->bssid, bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200635 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200636 skb_put(skb, 2);
Johannes Bergef422bc2008-09-09 10:58:25 +0200637 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200638 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
639
Jouni Malinen53b46b82009-03-27 20:53:56 +0200640 if (stype == IEEE80211_STYPE_DEAUTH)
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100641 if (cfg80211_locked)
Holger Schurigce470612009-10-13 13:28:13 +0200642 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
643 else
644 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Jouni Malinen53b46b82009-03-27 20:53:56 +0200645 else
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100646 if (cfg80211_locked)
Holger Schurigce470612009-10-13 13:28:13 +0200647 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
648 else
649 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg62ae67b2009-11-18 18:42:05 +0100650 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
651 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300652
653 if (send_frame)
654 ieee80211_tx_skb(sdata, skb);
655 else
656 kfree_skb(skb);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200657}
658
Kalle Valo572e0012009-02-10 17:09:31 +0200659void ieee80211_send_pspoll(struct ieee80211_local *local,
660 struct ieee80211_sub_if_data *sdata)
661{
Kalle Valo572e0012009-02-10 17:09:31 +0200662 struct ieee80211_pspoll *pspoll;
663 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200664
Kalle Valod8cd1892010-01-05 20:16:26 +0200665 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
666 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200667 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200668
Kalle Valod8cd1892010-01-05 20:16:26 +0200669 pspoll = (struct ieee80211_pspoll *) skb->data;
670 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200671
Johannes Berg62ae67b2009-11-18 18:42:05 +0100672 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
673 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200674}
675
Johannes Berg965beda2009-04-16 13:17:24 +0200676void ieee80211_send_nullfunc(struct ieee80211_local *local,
677 struct ieee80211_sub_if_data *sdata,
678 int powersave)
679{
680 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200681 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530682 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200683
Kalle Valod8cd1892010-01-05 20:16:26 +0200684 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
685 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200686 return;
687
Kalle Valod8cd1892010-01-05 20:16:26 +0200688 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200689 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200690 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200691
Johannes Berg62ae67b2009-11-18 18:42:05 +0100692 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530693 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
694 IEEE80211_STA_CONNECTION_POLL))
695 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
696
Johannes Berg62ae67b2009-11-18 18:42:05 +0100697 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200698}
699
Felix Fietkaud5242152010-01-08 18:06:26 +0100700static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
701 struct ieee80211_sub_if_data *sdata)
702{
703 struct sk_buff *skb;
704 struct ieee80211_hdr *nullfunc;
705 __le16 fc;
706
707 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
708 return;
709
710 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700711 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100712 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700713
Felix Fietkaud5242152010-01-08 18:06:26 +0100714 skb_reserve(skb, local->hw.extra_tx_headroom);
715
716 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
717 memset(nullfunc, 0, 30);
718 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
719 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
720 nullfunc->frame_control = fc;
721 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
722 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
723 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
724 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
725
726 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
727 ieee80211_tx_skb(sdata, skb);
728}
729
Johannes Bergcc32abd2009-05-15 11:52:31 +0200730/* spectrum management related things */
731static void ieee80211_chswitch_work(struct work_struct *work)
732{
733 struct ieee80211_sub_if_data *sdata =
734 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200735 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
736
Johannes Berg9607e6b2009-12-23 13:15:31 +0100737 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200738 return;
739
Johannes Berg77fdaa12009-07-07 03:45:17 +0200740 mutex_lock(&ifmgd->mtx);
741 if (!ifmgd->associated)
742 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200743
744 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200745 if (!sdata->local->ops->channel_switch) {
746 /* call "hw_config" only if doing sw channel switch */
747 ieee80211_hw_config(sdata->local,
748 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300749 } else {
750 /* update the device channel directly */
751 sdata->local->hw.conf.channel = sdata->local->oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200752 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200753
Johannes Berg77fdaa12009-07-07 03:45:17 +0200754 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100755 ifmgd->associated->channel = sdata->local->oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200756
Johannes Bergcc32abd2009-05-15 11:52:31 +0200757 ieee80211_wake_queues_by_reason(&sdata->local->hw,
758 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200759 out:
760 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
761 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200762}
763
Johannes Berg5ce6e432010-05-11 16:20:57 +0200764void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
765{
766 struct ieee80211_sub_if_data *sdata;
767 struct ieee80211_if_managed *ifmgd;
768
769 sdata = vif_to_sdata(vif);
770 ifmgd = &sdata->u.mgd;
771
772 trace_api_chswitch_done(sdata, success);
773 if (!success) {
774 /*
775 * If the channel switch was not successful, stay
776 * around on the old channel. We currently lack
777 * good handling of this situation, possibly we
778 * should just drop the association.
779 */
780 sdata->local->csa_channel = sdata->local->oper_channel;
781 }
782
783 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
784}
785EXPORT_SYMBOL(ieee80211_chswitch_done);
786
Johannes Bergcc32abd2009-05-15 11:52:31 +0200787static void ieee80211_chswitch_timer(unsigned long data)
788{
789 struct ieee80211_sub_if_data *sdata =
790 (struct ieee80211_sub_if_data *) data;
791 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
792
Johannes Berg5bb644a2009-05-17 11:40:42 +0200793 if (sdata->local->quiescing) {
794 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
795 return;
796 }
797
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400798 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200799}
800
801void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
802 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200803 struct ieee80211_bss *bss,
804 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200805{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100806 struct cfg80211_bss *cbss =
807 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200808 struct ieee80211_channel *new_ch;
809 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900810 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
811 cbss->channel->band);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200812
Johannes Berg77fdaa12009-07-07 03:45:17 +0200813 ASSERT_MGD_MTX(ifmgd);
814
815 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200816 return;
817
Helmut Schaafbe9c422009-07-23 12:14:04 +0200818 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200819 return;
820
821 /* Disregard subsequent beacons if we are already running a timer
822 processing a CSA */
823
824 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
825 return;
826
827 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
828 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
829 return;
830
831 sdata->local->csa_channel = new_ch;
832
Johannes Berg5ce6e432010-05-11 16:20:57 +0200833 if (sdata->local->ops->channel_switch) {
834 /* use driver's channel switch callback */
835 struct ieee80211_channel_switch ch_switch;
836 memset(&ch_switch, 0, sizeof(ch_switch));
837 ch_switch.timestamp = timestamp;
838 if (sw_elem->mode) {
839 ch_switch.block_tx = true;
840 ieee80211_stop_queues_by_reason(&sdata->local->hw,
841 IEEE80211_QUEUE_STOP_REASON_CSA);
842 }
843 ch_switch.channel = new_ch;
844 ch_switch.count = sw_elem->count;
845 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
846 drv_channel_switch(sdata->local, &ch_switch);
847 return;
848 }
849
850 /* channel switch handled in software */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200851 if (sw_elem->count <= 1) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400852 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200853 } else {
Wey-Yi Guy9feaddc2010-05-05 20:34:02 -0700854 if (sw_elem->mode)
855 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200856 IEEE80211_QUEUE_STOP_REASON_CSA);
857 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
858 mod_timer(&ifmgd->chswitch_timer,
859 jiffies +
860 msecs_to_jiffies(sw_elem->count *
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100861 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200862 }
863}
864
865static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
866 u16 capab_info, u8 *pwr_constr_elem,
867 u8 pwr_constr_elem_len)
868{
869 struct ieee80211_conf *conf = &sdata->local->hw.conf;
870
871 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
872 return;
873
874 /* Power constraint IE length should be 1 octet */
875 if (pwr_constr_elem_len != 1)
876 return;
877
Hong Wua48b13a2012-01-11 20:34:30 +0200878 if ((*pwr_constr_elem <= conf->channel->max_reg_power) &&
Johannes Bergcc32abd2009-05-15 11:52:31 +0200879 (*pwr_constr_elem != sdata->local->power_constr_level)) {
880 sdata->local->power_constr_level = *pwr_constr_elem;
881 ieee80211_hw_config(sdata->local, 0);
882 }
883}
884
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300885void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
886{
887 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
888 struct ieee80211_local *local = sdata->local;
889 struct ieee80211_conf *conf = &local->hw.conf;
890
891 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
892 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
893 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
894
895 local->disable_dynamic_ps = false;
896 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
897}
898EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
899
900void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
901{
902 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
903 struct ieee80211_local *local = sdata->local;
904 struct ieee80211_conf *conf = &local->hw.conf;
905
906 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
907 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
908 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
909
910 local->disable_dynamic_ps = true;
911 conf->dynamic_ps_timeout = 0;
912 del_timer_sync(&local->dynamic_ps_timer);
913 ieee80211_queue_work(&local->hw,
914 &local->dynamic_ps_enable_work);
915}
916EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
917
Johannes Berg965beda2009-04-16 13:17:24 +0200918/* powersave */
919static void ieee80211_enable_ps(struct ieee80211_local *local,
920 struct ieee80211_sub_if_data *sdata)
921{
922 struct ieee80211_conf *conf = &local->hw.conf;
923
Johannes Bergd5edaed2009-04-22 23:02:51 +0200924 /*
925 * If we are scanning right now then the parameters will
926 * take effect when scan finishes.
927 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200928 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200929 return;
930
Johannes Berg965beda2009-04-16 13:17:24 +0200931 if (conf->dynamic_ps_timeout > 0 &&
932 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
933 mod_timer(&local->dynamic_ps_timer, jiffies +
934 msecs_to_jiffies(conf->dynamic_ps_timeout));
935 } else {
936 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
937 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530938
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200939 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
940 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
941 return;
942
943 conf->flags |= IEEE80211_CONF_PS;
944 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200945 }
946}
947
948static void ieee80211_change_ps(struct ieee80211_local *local)
949{
950 struct ieee80211_conf *conf = &local->hw.conf;
951
952 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200953 ieee80211_enable_ps(local, local->ps_sdata);
954 } else if (conf->flags & IEEE80211_CONF_PS) {
955 conf->flags &= ~IEEE80211_CONF_PS;
956 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
957 del_timer_sync(&local->dynamic_ps_timer);
958 cancel_work_sync(&local->dynamic_ps_enable_work);
959 }
960}
961
Jason Young808118c2011-03-10 16:43:19 -0800962static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
963{
964 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
965 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200966 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800967
968 if (!mgd->powersave)
969 return false;
970
Johannes Berg05cb9102011-10-28 11:59:47 +0200971 if (mgd->broken_ap)
972 return false;
973
Jason Young808118c2011-03-10 16:43:19 -0800974 if (!mgd->associated)
975 return false;
976
977 if (!mgd->associated->beacon_ies)
978 return false;
979
980 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
981 IEEE80211_STA_CONNECTION_POLL))
982 return false;
983
984 rcu_read_lock();
985 sta = sta_info_get(sdata, mgd->bssid);
986 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200987 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -0800988 rcu_read_unlock();
989
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200990 return authorized;
Jason Young808118c2011-03-10 16:43:19 -0800991}
992
Johannes Berg965beda2009-04-16 13:17:24 +0200993/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200994void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200995{
996 struct ieee80211_sub_if_data *sdata, *found = NULL;
997 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300998 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +0200999
1000 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1001 local->ps_sdata = NULL;
1002 return;
1003 }
1004
Johannes Bergaf6b6372009-12-23 13:15:35 +01001005 if (!list_empty(&local->work_list)) {
1006 local->ps_sdata = NULL;
1007 goto change;
1008 }
1009
Johannes Berg965beda2009-04-16 13:17:24 +02001010 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001011 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001012 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301013 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1014 /* If an AP vif is found, then disable PS
1015 * by setting the count to zero thereby setting
1016 * ps_sdata to NULL.
1017 */
1018 count = 0;
1019 break;
1020 }
Johannes Berg965beda2009-04-16 13:17:24 +02001021 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1022 continue;
1023 found = sdata;
1024 count++;
1025 }
1026
Jason Young808118c2011-03-10 16:43:19 -08001027 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001028 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001029 s32 beaconint_us;
1030
1031 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001032 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001033
1034 beaconint_us = ieee80211_tu_to_usec(
1035 found->vif.bss_conf.beacon_int);
1036
Juuso Oikarinenff616382010-06-09 09:51:52 +03001037 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001038 if (timeout < 0) {
1039 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001040 * Go to full PSM if the user configures a very low
1041 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001042 * The 2000 second value is there for compatibility
1043 * until the PM_QOS_NETWORK_LATENCY is configured
1044 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001045 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001046 if (latency > (1900 * USEC_PER_MSEC) &&
1047 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001048 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001049 else
1050 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001051 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001052 local->dynamic_ps_user_timeout = timeout;
1053 if (!local->disable_dynamic_ps)
1054 conf->dynamic_ps_timeout =
1055 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001056
Johannes Berg04fe2032009-04-22 18:44:37 +02001057 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001058 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001059 } else {
Johannes Berg56007a02010-01-26 14:19:52 +01001060 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +02001061 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001062 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +02001063
Johannes Berg56007a02010-01-26 14:19:52 +01001064 bss = (void *)found->u.mgd.associated->priv;
1065 dtimper = bss->dtim_period;
1066
1067 /* If the TIM IE is invalid, pretend the value is 1 */
1068 if (!dtimper)
1069 dtimper = 1;
1070 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001071 maxslp = min_t(int, dtimper,
1072 latency / beaconint_us);
1073
Johannes Berg9ccebe62009-04-23 10:32:36 +02001074 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001075 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001076 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001077 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001078 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001079 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001080 }
Johannes Berg965beda2009-04-16 13:17:24 +02001081
Johannes Bergaf6b6372009-12-23 13:15:35 +01001082 change:
Johannes Berg965beda2009-04-16 13:17:24 +02001083 ieee80211_change_ps(local);
1084}
1085
1086void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1087{
1088 struct ieee80211_local *local =
1089 container_of(work, struct ieee80211_local,
1090 dynamic_ps_disable_work);
1091
1092 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1093 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1094 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1095 }
1096
1097 ieee80211_wake_queues_by_reason(&local->hw,
1098 IEEE80211_QUEUE_STOP_REASON_PS);
1099}
1100
1101void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1102{
1103 struct ieee80211_local *local =
1104 container_of(work, struct ieee80211_local,
1105 dynamic_ps_enable_work);
1106 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001107 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301108 unsigned long flags;
1109 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001110
1111 /* can only happen when PS was just disabled anyway */
1112 if (!sdata)
1113 return;
1114
Christian Lamparter5e340692011-06-30 21:08:43 +02001115 ifmgd = &sdata->u.mgd;
1116
Johannes Berg965beda2009-04-16 13:17:24 +02001117 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1118 return;
1119
Arik Nemtsov77b70232011-06-26 12:06:54 +03001120 if (!local->disable_dynamic_ps &&
1121 local->hw.conf.dynamic_ps_timeout > 0) {
1122 /* don't enter PS if TX frames are pending */
1123 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301124 mod_timer(&local->dynamic_ps_timer, jiffies +
1125 msecs_to_jiffies(
1126 local->hw.conf.dynamic_ps_timeout));
1127 return;
1128 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001129
1130 /*
1131 * transmission can be stopped by others which leads to
1132 * dynamic_ps_timer expiry. Postpone the ps timer if it
1133 * is not the actual idle state.
1134 */
1135 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1136 for (q = 0; q < local->hw.queues; q++) {
1137 if (local->queue_stop_reasons[q]) {
1138 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1139 flags);
1140 mod_timer(&local->dynamic_ps_timer, jiffies +
1141 msecs_to_jiffies(
1142 local->hw.conf.dynamic_ps_timeout));
1143 return;
1144 }
1145 }
1146 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301147 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301148
Vivek Natarajan375177b2010-02-09 14:50:28 +05301149 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301150 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301151 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001152
Vivek Natarajane8306f92011-04-06 11:41:10 +05301153 if (drv_tx_frames_pending(local))
1154 mod_timer(&local->dynamic_ps_timer, jiffies +
1155 msecs_to_jiffies(
1156 local->hw.conf.dynamic_ps_timeout));
1157 else {
1158 ieee80211_send_nullfunc(local, sdata, 1);
1159 /* Flush to get the tx status of nullfunc frame */
1160 drv_flush(local, false);
1161 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301162 }
1163
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001164 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1165 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301166 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1167 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1168 local->hw.conf.flags |= IEEE80211_CONF_PS;
1169 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1170 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301171
Arik Nemtsov77b70232011-06-26 12:06:54 +03001172 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1173 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001174}
1175
1176void ieee80211_dynamic_ps_timer(unsigned long data)
1177{
1178 struct ieee80211_local *local = (void *) data;
1179
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001180 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001181 return;
1182
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001183 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001184}
1185
Johannes Berg60f8b392008-09-08 17:44:22 +02001186/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001187static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001188 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001189 u8 *wmm_param, size_t wmm_param_len)
1190{
Jiri Bencf0706e82007-05-05 11:45:53 -07001191 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001192 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001193 size_t left;
1194 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001195 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001196
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001197 if (!local->ops->conf_tx)
1198 return;
1199
Johannes Bergaf6b6372009-12-23 13:15:35 +01001200 if (local->hw.queues < 4)
Johannes Berg3434fbd2008-05-03 00:59:37 +02001201 return;
1202
1203 if (!wmm_param)
1204 return;
1205
Jiri Bencf0706e82007-05-05 11:45:53 -07001206 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1207 return;
Kalle Valoab133152010-01-12 10:42:31 +02001208
1209 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Kalle Valo50ae0cf2010-01-12 10:42:39 +02001210 uapsd_queues = local->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001211
Jiri Bencf0706e82007-05-05 11:45:53 -07001212 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001213 if (count == ifmgd->wmm_last_param_set)
Jiri Bencf0706e82007-05-05 11:45:53 -07001214 return;
Johannes Berg46900292009-02-15 12:44:28 +01001215 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001216
1217 pos = wmm_param + 8;
1218 left = wmm_param_len - 8;
1219
1220 memset(&params, 0, sizeof(params));
1221
Jiri Bencf0706e82007-05-05 11:45:53 -07001222 local->wmm_acm = 0;
1223 for (; left >= 4; left -= 4, pos += 4) {
1224 int aci = (pos[0] >> 5) & 0x03;
1225 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001226 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001227 int queue;
1228
1229 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001230 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001231 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001232 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001233 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001234 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1235 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001236 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001237 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001238 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001239 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001240 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001241 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1242 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001243 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001244 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001245 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001246 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001247 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001248 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1249 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001250 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001251 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001252 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001253 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001254 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001255 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001256 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1257 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001258 break;
1259 }
1260
1261 params.aifs = pos[0] & 0x0f;
1262 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1263 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001264 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001265 params.uapsd = uapsd;
1266
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001267#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001268 wiphy_debug(local->hw.wiphy,
1269 "WMM queue=%d aci=%d acm=%d aifs=%d "
1270 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1271 queue, aci, acm,
1272 params.aifs, params.cw_min, params.cw_max,
1273 params.txop, params.uapsd);
Johannes Berg3330d7b2008-02-10 16:49:38 +01001274#endif
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001275 sdata->tx_conf[queue] = params;
1276 if (drv_conf_tx(local, sdata, queue, &params))
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001277 wiphy_debug(local->hw.wiphy,
1278 "failed to set TX queue parameters for queue %d\n",
1279 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001280 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001281
1282 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001283 sdata->vif.bss_conf.qos = true;
1284 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
Jiri Bencf0706e82007-05-05 11:45:53 -07001285}
1286
Johannes Berg7a5158e2008-10-08 10:59:33 +02001287static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1288 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001289{
Johannes Bergbda39332008-10-11 01:51:51 +02001290 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001291 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001292 bool use_protection;
1293 bool use_short_preamble;
1294 bool use_short_slot;
1295
1296 if (erp_valid) {
1297 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1298 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1299 } else {
1300 use_protection = false;
1301 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1302 }
1303
1304 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Felix Fietkau43d35342010-01-15 03:00:48 +01001305 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
1306 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001307
Johannes Berg471b3ef2007-12-28 14:32:58 +01001308 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001309 bss_conf->use_cts_prot = use_protection;
1310 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001311 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001312
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001313 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001314 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001315 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001316 }
Daniel Draked9430a32007-07-27 15:43:24 +02001317
Johannes Berg7a5158e2008-10-08 10:59:33 +02001318 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001319 bss_conf->use_short_slot = use_short_slot;
1320 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001321 }
1322
1323 return changed;
1324}
1325
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001326static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001327 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001328 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001329{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001330 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001331 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001332 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001333
Johannes Bergae5eb022008-10-14 16:58:37 +02001334 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001335 /* set timing information */
Juuso Oikarinen68542962010-06-09 13:43:26 +03001336 bss_conf->beacon_int = cbss->beacon_interval;
1337 bss_conf->timestamp = cbss->tsf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001338
Johannes Berg77fdaa12009-07-07 03:45:17 +02001339 bss_info_changed |= BSS_CHANGED_BEACON_INT;
1340 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001341 cbss->capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001342
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001343 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1344 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1345
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001346 sdata->u.mgd.associated = cbss;
1347 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001348
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001349 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1350
Johannes Bergb291ba12009-07-10 15:29:03 +02001351 /* just to be sure */
1352 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1353 IEEE80211_STA_BEACON_POLL);
1354
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001355 ieee80211_led_assoc(local, 1);
1356
Johannes Berge5b900d2010-07-29 16:08:55 +02001357 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1358 bss_conf->dtim_period = bss->dtim_period;
1359 else
1360 bss_conf->dtim_period = 0;
1361
Juuso Oikarinen68542962010-06-09 13:43:26 +03001362 bss_conf->assoc = 1;
Johannes Berg96dd22a2008-09-11 00:01:57 +02001363 /*
1364 * For now just always ask the driver to update the basic rateset
1365 * when we have associated, we aren't checking whether it actually
1366 * changed or not.
1367 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001368 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
Johannes Berg9cef8732009-05-14 13:10:14 +02001369
1370 /* And the BSSID changed - we're associated now */
1371 bss_info_changed |= BSS_CHANGED_BSSID;
1372
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001373 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001374 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001375 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001376 bss_info_changed |= BSS_CHANGED_CQM;
1377
Juuso Oikarinen68542962010-06-09 13:43:26 +03001378 /* Enable ARP filtering */
1379 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1380 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1381 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1382 }
1383
Johannes Bergae5eb022008-10-14 16:58:37 +02001384 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001385
Johannes Berg056508d2009-07-30 21:43:55 +02001386 mutex_lock(&local->iflist_mtx);
1387 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +02001388 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02001389 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001390
John W. Linville8a5b33f2010-01-06 15:39:39 -05001391 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001392 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001393}
1394
Jouni Malinene69e95d2010-03-29 23:29:31 -07001395static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg53f73c02010-10-05 19:37:40 +02001396 bool remove_sta, bool tx)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001397{
Johannes Berg46900292009-02-15 12:44:28 +01001398 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001399 struct ieee80211_local *local = sdata->local;
1400 struct sta_info *sta;
Kalle Valoe0cb6862008-12-18 23:35:13 +02001401 u32 changed = 0, config_changed = 0;
Johannes Bergb291ba12009-07-10 15:29:03 +02001402 u8 bssid[ETH_ALEN];
Tomas Winkleraa458d12008-09-09 00:32:12 +03001403
Johannes Berg77fdaa12009-07-07 03:45:17 +02001404 ASSERT_MGD_MTX(ifmgd);
1405
Johannes Bergb291ba12009-07-10 15:29:03 +02001406 if (WARN_ON(!ifmgd->associated))
1407 return;
1408
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001409 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Johannes Bergb291ba12009-07-10 15:29:03 +02001410
Johannes Berg77fdaa12009-07-07 03:45:17 +02001411 ifmgd->associated = NULL;
1412 memset(ifmgd->bssid, 0, ETH_ALEN);
1413
1414 /*
1415 * we need to commit the associated = NULL change because the
1416 * scan code uses that to determine whether this iface should
1417 * go to/wake up from powersave or not -- and could otherwise
1418 * wake the queues erroneously.
1419 */
1420 smp_mb();
1421
1422 /*
1423 * Thus, we can only afterwards stop the queues -- to account
1424 * for the case where another CPU is finishing a scan at this
1425 * time -- we don't want the scan code to enable queues.
1426 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001427
John W. Linville8a5b33f2010-01-06 15:39:39 -05001428 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001429 netif_carrier_off(sdata->dev);
1430
Johannes Berg2a419052010-06-10 10:21:29 +02001431 mutex_lock(&local->sta_mtx);
Johannes Bergabe60632009-11-25 17:46:18 +01001432 sta = sta_info_get(sdata, bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301433 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001434 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +02001435 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +05301436 }
Johannes Berg2a419052010-06-10 10:21:29 +02001437 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001438
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001439 changed |= ieee80211_reset_erp_info(sdata);
1440
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001441 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001442 changed |= BSS_CHANGED_ASSOC;
1443 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001444
Johannes Bergaa837e12009-05-07 16:16:24 +02001445 ieee80211_set_wmm_default(sdata);
1446
Johannes Berg47979382009-01-07 10:13:27 +01001447 /* channel(_type) changes are handled by ieee80211_hw_config */
Johannes Berg0aaffa92010-05-05 15:28:27 +02001448 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergae5eb022008-10-14 16:58:37 +02001449
Johannes Berg413ad502009-05-08 21:21:06 +02001450 /* on the next assoc, re-program HT parameters */
1451 sdata->ht_opmode_valid = false;
Ben Greearef96a8422011-11-18 11:32:00 -08001452 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1453 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001454
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301455 local->power_constr_level = 0;
1456
Kalle Valo520eb822008-12-18 23:35:27 +02001457 del_timer_sync(&local->dynamic_ps_timer);
1458 cancel_work_sync(&local->dynamic_ps_enable_work);
1459
Kalle Valoe0cb6862008-12-18 23:35:13 +02001460 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1461 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1462 config_changed |= IEEE80211_CONF_CHANGE_PS;
1463 }
Eliad Pellera3314002011-05-26 11:46:37 +03001464 local->ps_sdata = NULL;
Kalle Valoe0cb6862008-12-18 23:35:13 +02001465
1466 ieee80211_hw_config(local, config_changed);
Johannes Berg9cef8732009-05-14 13:10:14 +02001467
Juuso Oikarinen68542962010-06-09 13:43:26 +03001468 /* Disable ARP filtering */
1469 if (sdata->vif.bss_conf.arp_filter_enabled) {
1470 sdata->vif.bss_conf.arp_filter_enabled = false;
1471 changed |= BSS_CHANGED_ARP_FILTER;
1472 }
1473
Johannes Berg0aaffa92010-05-05 15:28:27 +02001474 /* The BSSID (not really interesting) and HT changed */
1475 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001476 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001477
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03001478 /* remove AP and TDLS peers */
Jouni Malinene69e95d2010-03-29 23:29:31 -07001479 if (remove_sta)
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03001480 sta_info_flush(local, sdata);
Johannes Bergb9dcf712010-08-27 12:35:54 +02001481
1482 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1483 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1484 del_timer_sync(&sdata->u.mgd.timer);
1485 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001486}
Jiri Bencf0706e82007-05-05 11:45:53 -07001487
Kalle Valo3cf335d2009-03-22 21:57:06 +02001488void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1489 struct ieee80211_hdr *hdr)
1490{
1491 /*
1492 * We can postpone the mgd.timer whenever receiving unicast frames
1493 * from AP because we know that the connection is working both ways
1494 * at that time. But multicast frames (and hence also beacons) must
1495 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001496 * data idle periods for sending the periodic probe request to the
1497 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001498 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001499 if (is_multicast_ether_addr(hdr->addr1))
1500 return;
1501
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001502 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001503}
Jiri Bencf0706e82007-05-05 11:45:53 -07001504
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001505static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1506{
1507 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1508
1509 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1510 IEEE80211_STA_CONNECTION_POLL)))
1511 return;
1512
1513 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1514 IEEE80211_STA_BEACON_POLL);
1515 mutex_lock(&sdata->local->iflist_mtx);
1516 ieee80211_recalc_ps(sdata->local, -1);
1517 mutex_unlock(&sdata->local->iflist_mtx);
1518
1519 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1520 return;
1521
1522 /*
1523 * We've received a probe response, but are not sure whether
1524 * we have or will be receiving any beacons or data, so let's
1525 * schedule the timers again, just in case.
1526 */
1527 ieee80211_sta_reset_beacon_monitor(sdata);
1528
1529 mod_timer(&ifmgd->conn_mon_timer,
1530 round_jiffies_up(jiffies +
1531 IEEE80211_CONNECTION_IDLE_TIME));
1532}
1533
1534void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001535 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001536{
Felix Fietkau75706d02010-12-02 21:01:07 +01001537 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001538 return;
1539
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001540 if (ack)
1541 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001542
1543 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1544 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001545 if (ack)
1546 sdata->u.mgd.probe_send_count = 0;
1547 else
1548 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001549 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1550 }
1551}
1552
Maxim Levitskya43abf22009-07-31 18:54:12 +03001553static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1554{
1555 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1556 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001557 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001558 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001559
1560 /*
1561 * Try sending broadcast probe requests for the last three
1562 * probe requests after the first ones failed since some
1563 * buggy APs only support broadcast probe requests.
1564 */
1565 if (ifmgd->probe_send_count >= unicast_limit)
1566 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001567
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001568 /*
1569 * When the hardware reports an accurate Tx ACK status, it's
1570 * better to send a nullfunc frame instead of a probe request,
1571 * as it will kick us off the AP quickly if we aren't associated
1572 * anymore. The timeout will be reset if the frame is ACKed by
1573 * the AP.
1574 */
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001575 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1576 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001577 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001578 } else {
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001579 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Paul Stewarta806c552011-06-23 09:00:11 -08001580 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0,
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +05301581 (u32) -1, true, false);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001582 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001583
1584 ifmgd->probe_send_count++;
Ben Greear180205b2011-02-04 15:30:24 -08001585 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001586 run_again(ifmgd, ifmgd->probe_timeout);
1587}
1588
Johannes Bergb291ba12009-07-10 15:29:03 +02001589static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1590 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001591{
Kalle Valo04de8382009-03-22 21:57:35 +02001592 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001593 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001594
Johannes Berg9607e6b2009-12-23 13:15:31 +01001595 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001596 return;
1597
Luis R. Rodriguez91a3bd72009-07-23 16:37:47 -07001598 if (sdata->local->scanning)
1599 return;
1600
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001601 if (sdata->local->tmp_channel)
1602 return;
1603
Johannes Berg77fdaa12009-07-07 03:45:17 +02001604 mutex_lock(&ifmgd->mtx);
1605
1606 if (!ifmgd->associated)
1607 goto out;
1608
Michael Buescha8604022009-04-15 14:41:22 -04001609#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergb291ba12009-07-10 15:29:03 +02001610 if (beacon && net_ratelimit())
1611 printk(KERN_DEBUG "%s: detected beacon loss from AP "
Johannes Berg47846c92009-11-25 17:46:19 +01001612 "- sending probe request\n", sdata->name);
Michael Buescha8604022009-04-15 14:41:22 -04001613#endif
Kalle Valo04de8382009-03-22 21:57:35 +02001614
Johannes Bergb291ba12009-07-10 15:29:03 +02001615 /*
1616 * The driver/our work has already reported this event or the
1617 * connection monitoring has kicked in and we have already sent
1618 * a probe request. Or maybe the AP died and the driver keeps
1619 * reporting until we disassociate...
1620 *
1621 * In either case we have to ignore the current call to this
1622 * function (except for setting the correct probe reason bit)
1623 * because otherwise we would reset the timer every time and
1624 * never check whether we received a probe response!
1625 */
1626 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1627 IEEE80211_STA_CONNECTION_POLL))
1628 already = true;
1629
1630 if (beacon)
1631 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1632 else
1633 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1634
1635 if (already)
1636 goto out;
1637
Johannes Berg4e751842009-06-10 15:16:52 +02001638 mutex_lock(&sdata->local->iflist_mtx);
1639 ieee80211_recalc_ps(sdata->local, -1);
1640 mutex_unlock(&sdata->local->iflist_mtx);
1641
Maxim Levitskya43abf22009-07-31 18:54:12 +03001642 ifmgd->probe_send_count = 0;
1643 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001644 out:
1645 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001646}
1647
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001648struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1649 struct ieee80211_vif *vif)
1650{
1651 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1652 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1653 struct sk_buff *skb;
1654 const u8 *ssid;
1655
1656 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1657 return NULL;
1658
1659 ASSERT_MGD_MTX(ifmgd);
1660
1661 if (!ifmgd->associated)
1662 return NULL;
1663
1664 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1665 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
Johannes Berg85a237f2011-07-18 18:08:36 +02001666 (u32) -1, ssid + 2, ssid[1],
1667 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001668
1669 return skb;
1670}
1671EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1672
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001673static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1674{
1675 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1676 struct ieee80211_local *local = sdata->local;
1677 u8 bssid[ETH_ALEN];
1678
1679 mutex_lock(&ifmgd->mtx);
1680 if (!ifmgd->associated) {
1681 mutex_unlock(&ifmgd->mtx);
1682 return;
1683 }
1684
1685 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1686
Ben Greear180205b2011-02-04 15:30:24 -08001687 printk(KERN_DEBUG "%s: Connection to AP %pM lost.\n",
1688 sdata->name, bssid);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001689
Johannes Berg53f73c02010-10-05 19:37:40 +02001690 ieee80211_set_disassoc(sdata, true, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001691 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001692
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001693 /*
1694 * must be outside lock due to cfg80211,
1695 * but that's not a problem.
1696 */
1697 ieee80211_send_deauth_disassoc(sdata, bssid,
1698 IEEE80211_STYPE_DEAUTH,
1699 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01001700 false, true);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001701
1702 mutex_lock(&local->mtx);
1703 ieee80211_recalc_idle(local);
1704 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001705}
1706
1707void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001708{
1709 struct ieee80211_sub_if_data *sdata =
1710 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001711 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001712 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1713 struct sta_info *sta;
1714
1715 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301716 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001717 sta = sta_info_get(sdata, ifmgd->bssid);
1718 if (sta)
1719 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301720 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001721 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001722
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001723 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1724 __ieee80211_connection_loss(sdata);
1725 else
1726 ieee80211_mgd_probe_ap(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001727}
1728
Kalle Valo04de8382009-03-22 21:57:35 +02001729void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1730{
1731 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001732 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001733
Johannes Bergb5878a22010-04-07 16:48:40 +02001734 trace_api_beacon_loss(sdata);
1735
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001736 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1737 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001738}
1739EXPORT_SYMBOL(ieee80211_beacon_loss);
1740
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001741void ieee80211_connection_loss(struct ieee80211_vif *vif)
1742{
1743 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1744 struct ieee80211_hw *hw = &sdata->local->hw;
1745
Johannes Bergb5878a22010-04-07 16:48:40 +02001746 trace_api_connection_loss(sdata);
1747
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001748 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1749 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1750}
1751EXPORT_SYMBOL(ieee80211_connection_loss);
1752
1753
Johannes Berg66e67e42012-01-20 13:55:27 +01001754static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1755 bool assoc)
1756{
1757 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1758
1759 lockdep_assert_held(&sdata->u.mgd.mtx);
1760
1761 if (auth_data->synced)
1762 drv_finish_tx_sync(sdata->local, sdata,
1763 auth_data->bss->bssid,
1764 IEEE80211_TX_SYNC_AUTH);
1765
1766 if (!assoc) {
1767 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1768
1769 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1770 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1771 }
1772
1773 cfg80211_put_bss(auth_data->bss);
1774 kfree(auth_data);
1775 sdata->u.mgd.auth_data = NULL;
1776}
1777
1778static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1779 struct ieee80211_mgmt *mgmt, size_t len)
1780{
1781 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1782 u8 *pos;
1783 struct ieee802_11_elems elems;
1784
1785 pos = mgmt->u.auth.variable;
1786 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1787 if (!elems.challenge)
1788 return;
1789 auth_data->expected_transaction = 4;
1790 ieee80211_send_auth(sdata, 3, auth_data->algorithm,
1791 elems.challenge - 2, elems.challenge_len + 2,
1792 auth_data->bss->bssid, auth_data->bss->bssid,
1793 auth_data->key, auth_data->key_len,
1794 auth_data->key_idx);
1795}
1796
1797static enum rx_mgmt_action __must_check
1798ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1799 struct ieee80211_mgmt *mgmt, size_t len)
1800{
1801 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1802 u8 bssid[ETH_ALEN];
1803 u16 auth_alg, auth_transaction, status_code;
1804 struct sta_info *sta;
1805
1806 lockdep_assert_held(&ifmgd->mtx);
1807
1808 if (len < 24 + 6)
1809 return RX_MGMT_NONE;
1810
1811 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1812 return RX_MGMT_NONE;
1813
1814 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1815
1816 if (memcmp(bssid, mgmt->bssid, ETH_ALEN))
1817 return RX_MGMT_NONE;
1818
1819 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1820 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1821 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1822
1823 if (auth_alg != ifmgd->auth_data->algorithm ||
1824 auth_transaction != ifmgd->auth_data->expected_transaction)
1825 return RX_MGMT_NONE;
1826
1827 if (status_code != WLAN_STATUS_SUCCESS) {
1828 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
1829 sdata->name, mgmt->sa, status_code);
1830 goto out;
1831 }
1832
1833 switch (ifmgd->auth_data->algorithm) {
1834 case WLAN_AUTH_OPEN:
1835 case WLAN_AUTH_LEAP:
1836 case WLAN_AUTH_FT:
1837 break;
1838 case WLAN_AUTH_SHARED_KEY:
1839 if (ifmgd->auth_data->expected_transaction != 4) {
1840 ieee80211_auth_challenge(sdata, mgmt, len);
1841 /* need another frame */
1842 return RX_MGMT_NONE;
1843 }
1844 break;
1845 default:
1846 WARN_ONCE(1, "invalid auth alg %d",
1847 ifmgd->auth_data->algorithm);
1848 return RX_MGMT_NONE;
1849 }
1850
1851 printk(KERN_DEBUG "%s: authenticated\n", sdata->name);
1852 out:
1853 if (ifmgd->auth_data->synced)
1854 drv_finish_tx_sync(sdata->local, sdata, bssid,
1855 IEEE80211_TX_SYNC_AUTH);
1856 ifmgd->auth_data->synced = false;
1857 ifmgd->auth_data->done = true;
1858 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1859 run_again(ifmgd, ifmgd->auth_data->timeout);
1860
1861 /* move station state to auth */
1862 mutex_lock(&sdata->local->sta_mtx);
1863 sta = sta_info_get(sdata, bssid);
1864 if (!sta) {
1865 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1866 goto out_err;
1867 }
1868 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
1869 printk(KERN_DEBUG "%s: failed moving %pM to auth\n",
1870 sdata->name, bssid);
1871 goto out_err;
1872 }
1873 mutex_unlock(&sdata->local->sta_mtx);
1874
1875 return RX_MGMT_CFG80211_RX_AUTH;
1876 out_err:
1877 mutex_unlock(&sdata->local->sta_mtx);
1878 /* ignore frame -- wait for timeout */
1879 return RX_MGMT_NONE;
1880}
1881
1882
Johannes Berg77fdaa12009-07-07 03:45:17 +02001883static enum rx_mgmt_action __must_check
1884ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001885 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001886{
Johannes Berg46900292009-02-15 12:44:28 +01001887 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001888 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001889 u16 reason_code;
1890
Johannes Berg66e67e42012-01-20 13:55:27 +01001891 lockdep_assert_held(&ifmgd->mtx);
1892
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001893 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001894 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001895
Johannes Berg66e67e42012-01-20 13:55:27 +01001896 if (!ifmgd->associated ||
1897 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN))
1898 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001899
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001900 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001901
1902 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1903
Johannes Berg77fdaa12009-07-07 03:45:17 +02001904 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001905 sdata->name, bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001906
Johannes Berg53f73c02010-10-05 19:37:40 +02001907 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001908 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001909 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001910 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001911
1912 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001913}
1914
1915
Johannes Berg77fdaa12009-07-07 03:45:17 +02001916static enum rx_mgmt_action __must_check
1917ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1918 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001919{
Johannes Berg46900292009-02-15 12:44:28 +01001920 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001921 u16 reason_code;
1922
Johannes Berg66e67e42012-01-20 13:55:27 +01001923 lockdep_assert_held(&ifmgd->mtx);
1924
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001925 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001926 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001927
Johannes Berg66e67e42012-01-20 13:55:27 +01001928 if (!ifmgd->associated ||
1929 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001930 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001931
1932 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1933
Johannes Berg0ff71612009-09-26 14:45:41 +02001934 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001935 sdata->name, mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001936
Johannes Berg53f73c02010-10-05 19:37:40 +02001937 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001938 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001939 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001940 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001941 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001942}
1943
Christian Lamparterc74d0842011-10-15 00:14:49 +02001944static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
1945 u8 *supp_rates, unsigned int supp_rates_len,
1946 u32 *rates, u32 *basic_rates,
1947 bool *have_higher_than_11mbit,
1948 int *min_rate, int *min_rate_index)
1949{
1950 int i, j;
1951
1952 for (i = 0; i < supp_rates_len; i++) {
1953 int rate = (supp_rates[i] & 0x7f) * 5;
1954 bool is_basic = !!(supp_rates[i] & 0x80);
1955
1956 if (rate > 110)
1957 *have_higher_than_11mbit = true;
1958
1959 /*
1960 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
1961 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
1962 *
1963 * Note: Even through the membership selector and the basic
1964 * rate flag share the same bit, they are not exactly
1965 * the same.
1966 */
1967 if (!!(supp_rates[i] & 0x80) &&
1968 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1969 continue;
1970
1971 for (j = 0; j < sband->n_bitrates; j++) {
1972 if (sband->bitrates[j].bitrate == rate) {
1973 *rates |= BIT(j);
1974 if (is_basic)
1975 *basic_rates |= BIT(j);
1976 if (rate < *min_rate) {
1977 *min_rate = rate;
1978 *min_rate_index = j;
1979 }
1980 break;
1981 }
1982 }
1983 }
1984}
Jiri Bencf0706e82007-05-05 11:45:53 -07001985
Johannes Berg66e67e42012-01-20 13:55:27 +01001986static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
1987 bool assoc)
1988{
1989 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
1990
1991 lockdep_assert_held(&sdata->u.mgd.mtx);
1992
1993 if (assoc_data->synced)
1994 drv_finish_tx_sync(sdata->local, sdata,
1995 assoc_data->bss->bssid,
1996 IEEE80211_TX_SYNC_ASSOC);
1997
1998 if (!assoc) {
1999 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2000
2001 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2002 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2003 }
2004
2005 kfree(assoc_data);
2006 sdata->u.mgd.assoc_data = NULL;
2007}
2008
2009static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2010 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002011 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002012{
Johannes Berg46900292009-02-15 12:44:28 +01002013 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002014 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002015 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002016 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002017 u8 *pos;
Johannes Berg881d9482009-01-21 15:13:48 +01002018 u32 rates, basic_rates;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002019 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002020 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002021 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002022 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002023 int err;
Johannes Berg5d1ec852009-12-02 12:43:43 +01002024 bool have_higher_than_11mbit = false;
Johannes Bergae5eb022008-10-14 16:58:37 +02002025 u16 ap_ht_cap_flags;
Eliad Peller3432f922011-10-30 15:41:59 +02002026 int min_rate = INT_MAX, min_rate_index = -1;
Jiri Bencf0706e82007-05-05 11:45:53 -07002027
Johannes Bergaf6b6372009-12-23 13:15:35 +01002028 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002029
Jiri Bencf0706e82007-05-05 11:45:53 -07002030 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002031 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002032
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002033 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Berg05cb9102011-10-28 11:59:47 +02002034 printk(KERN_DEBUG
2035 "%s: invalid AID value 0x%x; bits 15:14 not set\n",
2036 sdata->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002037 aid &= ~(BIT(15) | BIT(14));
2038
Johannes Berg05cb9102011-10-28 11:59:47 +02002039 ifmgd->broken_ap = false;
2040
2041 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2042 printk(KERN_DEBUG
2043 "%s: invalid AID value %d (out of range), turn off PS\n",
2044 sdata->name, aid);
2045 aid = 0;
2046 ifmgd->broken_ap = true;
2047 }
2048
Johannes Bergaf6b6372009-12-23 13:15:35 +01002049 pos = mgmt->u.assoc_resp.variable;
2050 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2051
Jiri Bencf0706e82007-05-05 11:45:53 -07002052 if (!elems.supp_rates) {
2053 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002054 sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002055 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002056 }
2057
Johannes Berg46900292009-02-15 12:44:28 +01002058 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002059
Guy Eilam2a33bee2011-08-17 15:18:15 +03002060 mutex_lock(&sdata->local->sta_mtx);
2061 /*
2062 * station info was already allocated and inserted before
2063 * the association and should be available to us
2064 */
Johannes Berg7852e362012-01-20 13:55:24 +01002065 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002066 if (WARN_ON(!sta)) {
2067 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002068 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002069 }
2070
Jiri Bencf0706e82007-05-05 11:45:53 -07002071 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002072 basic_rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002073 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +01002074
Christian Lamparterc74d0842011-10-15 00:14:49 +02002075 ieee80211_get_rates(sband, elems.supp_rates, elems.supp_rates_len,
2076 &rates, &basic_rates, &have_higher_than_11mbit,
2077 &min_rate, &min_rate_index);
Johannes Berg8318d782008-01-24 19:38:38 +01002078
Christian Lamparterc74d0842011-10-15 00:14:49 +02002079 ieee80211_get_rates(sband, elems.ext_supp_rates,
2080 elems.ext_supp_rates_len, &rates, &basic_rates,
2081 &have_higher_than_11mbit,
2082 &min_rate, &min_rate_index);
Johannes Berg8318d782008-01-24 19:38:38 +01002083
Eliad Peller3432f922011-10-30 15:41:59 +02002084 /*
2085 * some buggy APs don't advertise basic_rates. use the lowest
2086 * supported rate instead.
2087 */
2088 if (unlikely(!basic_rates) && min_rate_index >= 0) {
2089 printk(KERN_DEBUG "%s: No basic rates in AssocResp. "
2090 "Using min supported rate instead.\n", sdata->name);
2091 basic_rates = BIT(min_rate_index);
2092 }
2093
Johannes Berg66e67e42012-01-20 13:55:27 +01002094 sta->sta.supp_rates[local->oper_channel->band] = rates;
Johannes Bergbda39332008-10-11 01:51:51 +02002095 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01002096
2097 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg66e67e42012-01-20 13:55:27 +01002098 if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg8318d782008-01-24 19:38:38 +01002099 have_higher_than_11mbit)
2100 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2101 else
2102 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002103
Johannes Bergab1faea2009-07-01 21:41:17 +02002104 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Ben Greearef96a8422011-11-18 11:32:00 -08002105 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002106 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002107
2108 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002109
Johannes Berg4b7679a2008-09-18 18:14:18 +02002110 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002111
Johannes Berg46900292009-02-15 12:44:28 +01002112 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002113 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002114
Johannes Bergddf4ac52008-10-22 11:41:38 +02002115 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002116 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002117
Johannes Bergc8987872012-01-20 13:55:17 +01002118 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2119 if (!err)
2120 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2121 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2122 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2123 if (err) {
2124 printk(KERN_DEBUG
2125 "%s: failed to move station %pM to desired state\n",
2126 sdata->name, sta->sta.addr);
2127 WARN_ON(__sta_info_destroy(sta));
2128 mutex_unlock(&sdata->local->sta_mtx);
2129 return false;
2130 }
2131
Johannes Berg7852e362012-01-20 13:55:24 +01002132 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002133
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002134 /*
2135 * Always handle WMM once after association regardless
2136 * of the first value the AP uses. Setting -1 here has
2137 * that effect because the AP values is an unsigned
2138 * 4-bit value.
2139 */
2140 ifmgd->wmm_last_param_set = -1;
2141
Johannes Bergddf4ac52008-10-22 11:41:38 +02002142 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002143 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002144 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002145 else
2146 ieee80211_set_wmm_default(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07002147
Johannes Bergae5eb022008-10-14 16:58:37 +02002148 if (elems.ht_info_elem && elems.wmm_param &&
Johannes Bergaf6b6372009-12-23 13:15:35 +01002149 (sdata->local->hw.queues >= 4) &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002150 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02002151 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +05302152 cbss->bssid, ap_ht_cap_flags,
2153 false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002154
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002155 /* set AID and assoc capability,
2156 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002157 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002158 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002159 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002160
Kalle Valo15b7b062009-03-22 21:57:14 +02002161 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002162 * If we're using 4-addr mode, let the AP know that we're
2163 * doing so, so that it can create the STA VLAN on its side
2164 */
2165 if (ifmgd->use_4addr)
2166 ieee80211_send_4addr_nullfunc(local, sdata);
2167
2168 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002169 * Start timer to probe the connection to the AP now.
2170 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002171 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002172 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002173 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002174
Johannes Bergaf6b6372009-12-23 13:15:35 +01002175 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002176}
2177
Johannes Berg66e67e42012-01-20 13:55:27 +01002178static enum rx_mgmt_action __must_check
2179ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2180 struct ieee80211_mgmt *mgmt, size_t len,
2181 struct cfg80211_bss **bss)
2182{
2183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2184 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2185 u16 capab_info, status_code, aid;
2186 struct ieee802_11_elems elems;
2187 u8 *pos;
2188 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002189
Johannes Berg66e67e42012-01-20 13:55:27 +01002190 lockdep_assert_held(&ifmgd->mtx);
2191
2192 if (!assoc_data)
2193 return RX_MGMT_NONE;
2194 if (memcmp(assoc_data->bss->bssid, mgmt->bssid, ETH_ALEN))
2195 return RX_MGMT_NONE;
2196
2197 /*
2198 * AssocResp and ReassocResp have identical structure, so process both
2199 * of them in this function.
2200 */
2201
2202 if (len < 24 + 6)
2203 return RX_MGMT_NONE;
2204
2205 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2206 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2207 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2208 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2209
2210 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
2211 "status=%d aid=%d)\n",
2212 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
2213 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2214
2215 pos = mgmt->u.assoc_resp.variable;
2216 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2217
2218 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2219 elems.timeout_int && elems.timeout_int_len == 5 &&
2220 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2221 u32 tu, ms;
2222 tu = get_unaligned_le32(elems.timeout_int + 1);
2223 ms = tu * 1024 / 1000;
2224 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
2225 "comeback duration %u TU (%u ms)\n",
2226 sdata->name, mgmt->sa, tu, ms);
2227 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2228 if (ms > IEEE80211_ASSOC_TIMEOUT)
2229 run_again(ifmgd, assoc_data->timeout);
2230 return RX_MGMT_NONE;
2231 }
2232
2233 *bss = assoc_data->bss;
2234
2235 if (status_code != WLAN_STATUS_SUCCESS) {
2236 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
2237 sdata->name, mgmt->sa, status_code);
2238 ieee80211_destroy_assoc_data(sdata, false);
2239 } else {
2240 printk(KERN_DEBUG "%s: associated\n", sdata->name);
2241
Johannes Berg79ebfb82012-02-20 14:19:58 +01002242 /* tell driver about sync done first */
2243 if (assoc_data->synced) {
2244 drv_finish_tx_sync(sdata->local, sdata,
2245 assoc_data->bss->bssid,
2246 IEEE80211_TX_SYNC_ASSOC);
2247 assoc_data->synced = false;
2248 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002249
2250 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2251 /* oops -- internal error -- send timeout for now */
Johannes Berg79ebfb82012-02-20 14:19:58 +01002252 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002253 sta_info_destroy_addr(sdata, mgmt->bssid);
2254 cfg80211_put_bss(*bss);
2255 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2256 }
Johannes Berg79ebfb82012-02-20 14:19:58 +01002257
2258 /*
2259 * destroy assoc_data afterwards, as otherwise an idle
2260 * recalc after assoc_data is NULL but before associated
2261 * is set can cause the interface to go idle
2262 */
2263 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002264 }
2265
2266 return RX_MGMT_CFG80211_RX_ASSOC;
2267}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002268static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002269 struct ieee80211_mgmt *mgmt,
2270 size_t len,
2271 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002272 struct ieee802_11_elems *elems,
2273 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002274{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002275 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002276 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002277 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002278 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002279 bool need_ps = false;
2280
Johannes Berg66e67e42012-01-20 13:55:27 +01002281 if (sdata->u.mgd.associated &&
2282 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
2283 ETH_ALEN) == 0) {
Johannes Berg56007a02010-01-26 14:19:52 +01002284 bss = (void *)sdata->u.mgd.associated->priv;
2285 /* not previously set so we may need to recalc */
2286 need_ps = !bss->dtim_period;
2287 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002288
Johannes Berg5bda6172008-09-08 11:05:10 +02002289 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002290 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2291 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002292 else
2293 freq = rx_status->freq;
2294
2295 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2296
2297 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2298 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002299
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002300 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002301 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002302 if (bss)
2303 ieee80211_rx_bss_put(local, bss);
2304
2305 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002306 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002307
Johannes Berg56007a02010-01-26 14:19:52 +01002308 if (need_ps) {
2309 mutex_lock(&local->iflist_mtx);
2310 ieee80211_recalc_ps(local, -1);
2311 mutex_unlock(&local->iflist_mtx);
2312 }
2313
Sujithc481ec92009-01-06 09:28:37 +05302314 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002315 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002316 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05302317 struct ieee80211_channel_sw_ie *sw_elem =
2318 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Berg5ce6e432010-05-11 16:20:57 +02002319 ieee80211_sta_process_chanswitch(sdata, sw_elem,
2320 bss, rx_status->mactime);
Sujithc481ec92009-01-06 09:28:37 +05302321 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002322}
2323
2324
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002325static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002326 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002327{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002328 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002329 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002330 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2331 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002332 struct ieee802_11_elems elems;
2333
Kalle Valo15b7b062009-03-22 21:57:14 +02002334 ifmgd = &sdata->u.mgd;
2335
Johannes Berg77fdaa12009-07-07 03:45:17 +02002336 ASSERT_MGD_MTX(ifmgd);
2337
Johannes Berg47846c92009-11-25 17:46:19 +01002338 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002339 return; /* ignore ProbeResp to foreign address */
2340
Ester Kummerae6a44e2008-06-27 18:54:48 +03002341 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2342 if (baselen > len)
2343 return;
2344
2345 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2346 &elems);
2347
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002348 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002349
Johannes Berg77fdaa12009-07-07 03:45:17 +02002350 if (ifmgd->associated &&
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002351 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
2352 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002353
2354 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2355 memcmp(mgmt->bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN) == 0) {
2356 /* got probe response, continue with auth */
2357 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
2358 ifmgd->auth_data->tries = 0;
2359 ifmgd->auth_data->timeout = jiffies;
2360 run_again(ifmgd, ifmgd->auth_data->timeout);
2361 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002362}
2363
Johannes Bergd91f36d2009-04-16 13:17:26 +02002364/*
2365 * This is the canonical list of information elements we care about,
2366 * the filter code also gives us all changes to the Microsoft OUI
2367 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2368 *
2369 * We implement beacon filtering in software since that means we can
2370 * avoid processing the frame here and in cfg80211, and userspace
2371 * will not be able to tell whether the hardware supports it or not.
2372 *
2373 * XXX: This list needs to be dynamic -- userspace needs to be able to
2374 * add items it requires. It also needs to be able to tell us to
2375 * look out for other vendor IEs.
2376 */
2377static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002378 (1ULL << WLAN_EID_COUNTRY) |
2379 (1ULL << WLAN_EID_ERP_INFO) |
2380 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2381 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2382 (1ULL << WLAN_EID_HT_CAPABILITY) |
2383 (1ULL << WLAN_EID_HT_INFORMATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002384
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002385static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002386 struct ieee80211_mgmt *mgmt,
2387 size_t len,
2388 struct ieee80211_rx_status *rx_status)
2389{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002390 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002391 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002392 size_t baselen;
2393 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002394 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002395 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002396 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002397 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002398 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002399 u8 *bssid;
2400
Johannes Berg66e67e42012-01-20 13:55:27 +01002401 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002402
Ester Kummerae6a44e2008-06-27 18:54:48 +03002403 /* Process beacon from the current BSS */
2404 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2405 if (baselen > len)
2406 return;
2407
Johannes Bergd91f36d2009-04-16 13:17:26 +02002408 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07002409 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002410
Johannes Berg66e67e42012-01-20 13:55:27 +01002411 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
2412 memcmp(mgmt->bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN) == 0) {
2413 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2414 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002415
Johannes Berg66e67e42012-01-20 13:55:27 +01002416 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2417 false);
2418 ifmgd->assoc_data->have_beacon = true;
2419 ifmgd->assoc_data->sent_assoc = false;
2420 /* continue assoc process */
2421 ifmgd->assoc_data->timeout = jiffies;
2422 run_again(ifmgd, ifmgd->assoc_data->timeout);
2423 return;
2424 }
2425
2426 if (!ifmgd->associated ||
2427 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN))
2428 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002429 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002430
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002431 /* Track average RSSI from the Beacon frames of the current AP */
2432 ifmgd->last_beacon_signal = rx_status->signal;
2433 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2434 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002435 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002436 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002437 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002438 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002439 } else {
2440 ifmgd->ave_beacon_signal =
2441 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2442 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2443 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002444 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002445 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002446
2447 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2448 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2449 int sig = ifmgd->ave_beacon_signal;
2450 int last_sig = ifmgd->last_ave_beacon_signal;
2451
2452 /*
2453 * if signal crosses either of the boundaries, invoke callback
2454 * with appropriate parameters
2455 */
2456 if (sig > ifmgd->rssi_max_thold &&
2457 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2458 ifmgd->last_ave_beacon_signal = sig;
2459 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2460 } else if (sig < ifmgd->rssi_min_thold &&
2461 (last_sig >= ifmgd->rssi_max_thold ||
2462 last_sig == 0)) {
2463 ifmgd->last_ave_beacon_signal = sig;
2464 drv_rssi_callback(local, RSSI_EVENT_LOW);
2465 }
2466 }
2467
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002468 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002469 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002470 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002471 int sig = ifmgd->ave_beacon_signal / 16;
2472 int last_event = ifmgd->last_cqm_event_signal;
2473 int thold = bss_conf->cqm_rssi_thold;
2474 int hyst = bss_conf->cqm_rssi_hyst;
2475 if (sig < thold &&
2476 (last_event == 0 || sig < last_event - hyst)) {
2477 ifmgd->last_cqm_event_signal = sig;
2478 ieee80211_cqm_rssi_notify(
2479 &sdata->vif,
2480 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2481 GFP_KERNEL);
2482 } else if (sig > thold &&
2483 (last_event == 0 || sig > last_event + hyst)) {
2484 ifmgd->last_cqm_event_signal = sig;
2485 ieee80211_cqm_rssi_notify(
2486 &sdata->vif,
2487 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2488 GFP_KERNEL);
2489 }
2490 }
2491
Johannes Bergb291ba12009-07-10 15:29:03 +02002492 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Jouni Malinen92778182009-05-14 21:15:36 +03002493#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2494 if (net_ratelimit()) {
2495 printk(KERN_DEBUG "%s: cancelling probereq poll due "
Johannes Berg47846c92009-11-25 17:46:19 +01002496 "to a received beacon\n", sdata->name);
Jouni Malinen92778182009-05-14 21:15:36 +03002497 }
2498#endif
Johannes Bergb291ba12009-07-10 15:29:03 +02002499 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Johannes Berg4e751842009-06-10 15:16:52 +02002500 mutex_lock(&local->iflist_mtx);
2501 ieee80211_recalc_ps(local, -1);
2502 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002503 }
2504
Johannes Bergb291ba12009-07-10 15:29:03 +02002505 /*
2506 * Push the beacon loss detection into the future since
2507 * we are processing a beacon from the AP just now.
2508 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002509 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002510
Johannes Bergd91f36d2009-04-16 13:17:26 +02002511 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2512 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2513 len - baselen, &elems,
2514 care_about_ies, ncrc);
2515
2516 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02002517 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
2518 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002519
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002520 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
Jouni Malinen30196672009-05-19 17:01:43 +03002521 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2522 true);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002523
Johannes Berg4ced3f72010-07-19 16:39:04 +02002524 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jouni Malinen30196672009-05-19 17:01:43 +03002525 elems.wmm_param_len);
2526 }
Johannes Bergfe3fa822008-09-08 11:05:09 +02002527
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302528 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02002529 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002530 if (local->hw.conf.dynamic_ps_timeout > 0) {
2531 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2532 ieee80211_hw_config(local,
2533 IEEE80211_CONF_CHANGE_PS);
2534 ieee80211_send_nullfunc(local, sdata, 0);
2535 } else {
2536 local->pspolling = true;
2537
2538 /*
2539 * Here is assumed that the driver will be
2540 * able to send ps-poll frame and receive a
2541 * response even though power save mode is
2542 * enabled, but some drivers might require
2543 * to disable power save here. This needs
2544 * to be investigated.
2545 */
2546 ieee80211_send_pspoll(local, sdata);
2547 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002548 }
2549 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002550
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002551 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002552 return;
2553 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002554 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002555
Johannes Berg7a5158e2008-10-08 10:59:33 +02002556 if (elems.erp_info && elems.erp_info_len >= 1) {
2557 erp_valid = true;
2558 erp_value = elems.erp_info[0];
2559 } else {
2560 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002561 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002562 changed |= ieee80211_handle_bss_capability(sdata,
2563 le16_to_cpu(mgmt->u.beacon.capab_info),
2564 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002565
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002566
Vasanthakumar Thiagarajan53d6f812009-02-11 22:18:49 +05302567 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002568 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02002569 struct sta_info *sta;
2570 struct ieee80211_supported_band *sband;
2571 u16 ap_ht_cap_flags;
2572
2573 rcu_read_lock();
2574
Johannes Bergabe60632009-11-25 17:46:18 +01002575 sta = sta_info_get(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002576 if (WARN_ON(!sta)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02002577 rcu_read_unlock();
2578 return;
2579 }
2580
2581 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2582
Ben Greearef96a8422011-11-18 11:32:00 -08002583 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergae5eb022008-10-14 16:58:37 +02002584 elems.ht_cap_elem, &sta->sta.ht_cap);
2585
2586 ap_ht_cap_flags = sta->sta.ht_cap.cap;
2587
2588 rcu_read_unlock();
2589
2590 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +05302591 bssid, ap_ht_cap_flags, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002592 }
2593
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002594 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002595 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302596 /* TODO: IBSS also needs this */
2597 if (elems.pwr_constr_elem)
2598 ieee80211_handle_pwr_constr(sdata,
2599 le16_to_cpu(mgmt->u.probe_resp.capab_info),
2600 elems.pwr_constr_elem,
2601 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002602 }
2603
Johannes Berg471b3ef2007-12-28 14:32:58 +01002604 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002605}
2606
Johannes Berg1fa57d02010-06-10 10:21:32 +02002607void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2608 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002609{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002610 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002611 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002612 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002613 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002614 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002615 u16 fc;
2616
Jiri Bencf0706e82007-05-05 11:45:53 -07002617 rx_status = (struct ieee80211_rx_status *) skb->cb;
2618 mgmt = (struct ieee80211_mgmt *) skb->data;
2619 fc = le16_to_cpu(mgmt->frame_control);
2620
Johannes Berg77fdaa12009-07-07 03:45:17 +02002621 mutex_lock(&ifmgd->mtx);
2622
Johannes Berg66e67e42012-01-20 13:55:27 +01002623 switch (fc & IEEE80211_FCTL_STYPE) {
2624 case IEEE80211_STYPE_BEACON:
2625 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2626 break;
2627 case IEEE80211_STYPE_PROBE_RESP:
2628 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2629 break;
2630 case IEEE80211_STYPE_AUTH:
2631 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2632 break;
2633 case IEEE80211_STYPE_DEAUTH:
2634 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2635 break;
2636 case IEEE80211_STYPE_DISASSOC:
2637 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2638 break;
2639 case IEEE80211_STYPE_ASSOC_RESP:
2640 case IEEE80211_STYPE_REASSOC_RESP:
2641 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2642 break;
2643 case IEEE80211_STYPE_ACTION:
2644 switch (mgmt->u.action.category) {
2645 case WLAN_CATEGORY_SPECTRUM_MGMT:
2646 ieee80211_sta_process_chanswitch(sdata,
2647 &mgmt->u.action.u.chan_switch.sw_elem,
2648 (void *)ifmgd->associated->priv,
2649 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002650 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002651 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002652 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002653 mutex_unlock(&ifmgd->mtx);
2654
Johannes Berg66e67e42012-01-20 13:55:27 +01002655 switch (rma) {
2656 case RX_MGMT_NONE:
2657 /* no action */
2658 break;
2659 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002660 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002661 break;
2662 case RX_MGMT_CFG80211_DISASSOC:
2663 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2664 break;
2665 case RX_MGMT_CFG80211_RX_AUTH:
2666 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2667 break;
2668 case RX_MGMT_CFG80211_RX_ASSOC:
2669 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2670 break;
2671 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2672 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2673 break;
2674 default:
2675 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002676 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002677}
2678
Johannes Berg9c6bd792008-09-11 00:01:52 +02002679static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002680{
2681 struct ieee80211_sub_if_data *sdata =
2682 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002683 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002684 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002685
Johannes Berg5bb644a2009-05-17 11:40:42 +02002686 if (local->quiescing) {
2687 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2688 return;
2689 }
2690
Johannes Berg64592c82010-06-10 10:21:31 +02002691 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002692}
2693
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002694static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002695 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002696{
2697 struct ieee80211_local *local = sdata->local;
2698 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2699
2700 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2701 IEEE80211_STA_BEACON_POLL);
2702
2703 ieee80211_set_disassoc(sdata, true, true);
2704 mutex_unlock(&ifmgd->mtx);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002705 /*
2706 * must be outside lock due to cfg80211,
2707 * but that's not a problem.
2708 */
2709 ieee80211_send_deauth_disassoc(sdata, bssid,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01002710 IEEE80211_STYPE_DEAUTH,
2711 reason, false, true);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002712
2713 mutex_lock(&local->mtx);
2714 ieee80211_recalc_idle(local);
2715 mutex_unlock(&local->mtx);
2716
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002717 mutex_lock(&ifmgd->mtx);
2718}
2719
Johannes Berg66e67e42012-01-20 13:55:27 +01002720static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2721{
2722 struct ieee80211_local *local = sdata->local;
2723 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2724 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2725
2726 lockdep_assert_held(&ifmgd->mtx);
2727
2728 if (WARN_ON_ONCE(!auth_data))
2729 return -EINVAL;
2730
2731 if (!auth_data->synced) {
2732 int ret = drv_tx_sync(local, sdata, auth_data->bss->bssid,
2733 IEEE80211_TX_SYNC_AUTH);
2734 if (ret)
2735 return ret;
2736 }
2737 auth_data->synced = true;
2738
2739 auth_data->tries++;
2740
2741 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
2742 printk(KERN_DEBUG "%s: authentication with %pM timed out\n",
2743 sdata->name, auth_data->bss->bssid);
2744
2745 /*
2746 * Most likely AP is not in the range so remove the
2747 * bss struct for that AP.
2748 */
2749 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2750
2751 return -ETIMEDOUT;
2752 }
2753
2754 if (auth_data->bss->proberesp_ies) {
2755 printk(KERN_DEBUG "%s: send auth to %pM (try %d/%d)\n",
2756 sdata->name, auth_data->bss->bssid, auth_data->tries,
2757 IEEE80211_AUTH_MAX_TRIES);
2758
2759 auth_data->expected_transaction = 2;
2760 ieee80211_send_auth(sdata, 1, auth_data->algorithm,
2761 auth_data->ie, auth_data->ie_len,
2762 auth_data->bss->bssid,
2763 auth_data->bss->bssid, NULL, 0, 0);
2764 } else {
2765 const u8 *ssidie;
2766
2767 printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
2768 sdata->name, auth_data->bss->bssid, auth_data->tries,
2769 IEEE80211_AUTH_MAX_TRIES);
2770
2771 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2772 if (!ssidie)
2773 return -EINVAL;
2774 /*
2775 * Direct probe is sent to broadcast address as some APs
2776 * will not answer to direct packet in unassociated state.
2777 */
2778 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
2779 NULL, 0, (u32) -1, true, false);
2780 }
2781
2782 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2783 run_again(ifmgd, auth_data->timeout);
2784
2785 return 0;
2786}
2787
2788static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2789{
2790 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2791 struct ieee80211_local *local = sdata->local;
2792
2793 lockdep_assert_held(&sdata->u.mgd.mtx);
2794
2795 if (!assoc_data->synced) {
2796 int ret = drv_tx_sync(local, sdata, assoc_data->bss->bssid,
2797 IEEE80211_TX_SYNC_ASSOC);
2798 if (ret)
2799 return ret;
2800 }
2801 assoc_data->synced = true;
2802
2803 assoc_data->tries++;
2804 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
2805 printk(KERN_DEBUG "%s: association with %pM timed out\n",
2806 sdata->name, assoc_data->bss->bssid);
2807
2808 /*
2809 * Most likely AP is not in the range so remove the
2810 * bss struct for that AP.
2811 */
2812 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2813
2814 return -ETIMEDOUT;
2815 }
2816
2817 printk(KERN_DEBUG "%s: associate with %pM (try %d/%d)\n",
2818 sdata->name, assoc_data->bss->bssid, assoc_data->tries,
2819 IEEE80211_ASSOC_MAX_TRIES);
2820 ieee80211_send_assoc(sdata);
2821
2822 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2823 run_again(&sdata->u.mgd, assoc_data->timeout);
2824
2825 return 0;
2826}
2827
Johannes Berg1fa57d02010-06-10 10:21:32 +02002828void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002829{
Johannes Berg60f8b392008-09-08 17:44:22 +02002830 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002831 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002832
Johannes Berg77fdaa12009-07-07 03:45:17 +02002833 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002834
Johannes Berg66e67e42012-01-20 13:55:27 +01002835 if (ifmgd->auth_data &&
2836 time_after(jiffies, ifmgd->auth_data->timeout)) {
2837 if (ifmgd->auth_data->done) {
2838 /*
2839 * ok ... we waited for assoc but userspace didn't,
2840 * so let's just kill the auth data
2841 */
2842 ieee80211_destroy_auth_data(sdata, false);
2843 } else if (ieee80211_probe_auth(sdata)) {
2844 u8 bssid[ETH_ALEN];
2845
2846 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2847
2848 ieee80211_destroy_auth_data(sdata, false);
2849
2850 mutex_unlock(&ifmgd->mtx);
2851 cfg80211_send_auth_timeout(sdata->dev, bssid);
2852 mutex_lock(&ifmgd->mtx);
2853 }
2854 } else if (ifmgd->auth_data)
2855 run_again(ifmgd, ifmgd->auth_data->timeout);
2856
2857 if (ifmgd->assoc_data &&
2858 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2859 if (!ifmgd->assoc_data->have_beacon ||
2860 ieee80211_do_assoc(sdata)) {
2861 u8 bssid[ETH_ALEN];
2862
2863 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2864
2865 ieee80211_destroy_assoc_data(sdata, false);
2866
2867 mutex_unlock(&ifmgd->mtx);
2868 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2869 mutex_lock(&ifmgd->mtx);
2870 }
2871 } else if (ifmgd->assoc_data)
2872 run_again(ifmgd, ifmgd->assoc_data->timeout);
2873
Johannes Bergb291ba12009-07-10 15:29:03 +02002874 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2875 IEEE80211_STA_CONNECTION_POLL) &&
2876 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002877 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002878 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002879
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002880 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002881
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002882 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002883 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002884 else
Ben Greear180205b2011-02-04 15:30:24 -08002885 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002886
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002887 /* ACK received for nullfunc probing frame */
2888 if (!ifmgd->probe_send_count)
2889 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002890 else if (ifmgd->nullfunc_failed) {
2891 if (ifmgd->probe_send_count < max_tries) {
2892#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2893 wiphy_debug(local->hw.wiphy,
2894 "%s: No ack for nullfunc frame to"
Ben Greearbfc31df2011-01-14 09:32:18 -08002895 " AP %pM, try %d/%i\n",
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002896 sdata->name, bssid,
Ben Greearbfc31df2011-01-14 09:32:18 -08002897 ifmgd->probe_send_count, max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002898#endif
2899 ieee80211_mgd_probe_ap_send(sdata);
2900 } else {
2901#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2902 wiphy_debug(local->hw.wiphy,
2903 "%s: No ack for nullfunc frame to"
2904 " AP %pM, disconnecting.\n",
Felix Fietkauc658e5d2010-12-07 04:40:18 +01002905 sdata->name, bssid);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002906#endif
Johannes Berg95acac62011-07-12 12:30:59 +02002907 ieee80211_sta_connection_lost(sdata, bssid,
2908 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002909 }
2910 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002911 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002912 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2913#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2914 wiphy_debug(local->hw.wiphy,
2915 "%s: Failed to send nullfunc to AP %pM"
2916 " after %dms, disconnecting.\n",
2917 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002918 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002919#endif
Johannes Berg95acac62011-07-12 12:30:59 +02002920 ieee80211_sta_connection_lost(sdata, bssid,
2921 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002922 } else if (ifmgd->probe_send_count < max_tries) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002923#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Ben Greearb38afa82010-10-07 16:12:06 -07002924 wiphy_debug(local->hw.wiphy,
2925 "%s: No probe response from AP %pM"
Ben Greearbfc31df2011-01-14 09:32:18 -08002926 " after %dms, try %d/%i\n",
Ben Greearb38afa82010-10-07 16:12:06 -07002927 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002928 bssid, probe_wait_ms,
Ben Greearbfc31df2011-01-14 09:32:18 -08002929 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002930#endif
2931 ieee80211_mgd_probe_ap_send(sdata);
2932 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002933 /*
2934 * We actually lost the connection ... or did we?
2935 * Let's make sure!
2936 */
Ben Greearb38afa82010-10-07 16:12:06 -07002937 wiphy_debug(local->hw.wiphy,
2938 "%s: No probe response from AP %pM"
2939 " after %dms, disconnecting.\n",
2940 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002941 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002942
Johannes Berg95acac62011-07-12 12:30:59 +02002943 ieee80211_sta_connection_lost(sdata, bssid,
2944 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02002945 }
2946 }
2947
Johannes Berg77fdaa12009-07-07 03:45:17 +02002948 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002949
2950 mutex_lock(&local->mtx);
2951 ieee80211_recalc_idle(local);
2952 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002953}
Johannes Berg0a51b272008-09-08 17:44:25 +02002954
Johannes Bergb291ba12009-07-10 15:29:03 +02002955static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2956{
2957 struct ieee80211_sub_if_data *sdata =
2958 (struct ieee80211_sub_if_data *) data;
2959 struct ieee80211_local *local = sdata->local;
2960
2961 if (local->quiescing)
2962 return;
2963
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002964 ieee80211_queue_work(&sdata->local->hw,
2965 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002966}
2967
2968static void ieee80211_sta_conn_mon_timer(unsigned long data)
2969{
2970 struct ieee80211_sub_if_data *sdata =
2971 (struct ieee80211_sub_if_data *) data;
2972 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2973 struct ieee80211_local *local = sdata->local;
2974
2975 if (local->quiescing)
2976 return;
2977
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002978 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002979}
2980
2981static void ieee80211_sta_monitor_work(struct work_struct *work)
2982{
2983 struct ieee80211_sub_if_data *sdata =
2984 container_of(work, struct ieee80211_sub_if_data,
2985 u.mgd.monitor_work);
2986
2987 ieee80211_mgd_probe_ap(sdata, false);
2988}
2989
Johannes Berga1678f82008-09-11 00:01:47 +02002990static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2991{
Eliad Peller494f1fe2012-02-19 15:26:09 +02002992 u32 flags;
2993
Kalle Vaload935682009-04-19 08:47:19 +03002994 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Bergb291ba12009-07-10 15:29:03 +02002995 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2996 IEEE80211_STA_CONNECTION_POLL);
Kalle Vaload935682009-04-19 08:47:19 +03002997
Johannes Bergb291ba12009-07-10 15:29:03 +02002998 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02002999 flags = sdata->local->hw.flags;
3000 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3001 ieee80211_queue_work(&sdata->local->hw,
3002 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003003 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003004 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003005 }
Johannes Berga1678f82008-09-11 00:01:47 +02003006}
3007
Johannes Berg5bb644a2009-05-17 11:40:42 +02003008#ifdef CONFIG_PM
3009void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3010{
3011 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3012
3013 /*
3014 * we need to use atomic bitops for the running bits
3015 * only because both timers might fire at the same
3016 * time -- the code here is properly synchronised.
3017 */
3018
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003019 cancel_work_sync(&ifmgd->request_smps_work);
3020
Johannes Berg0ecfe802011-11-09 12:14:16 +01003021 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003022 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003023 if (del_timer_sync(&ifmgd->timer))
3024 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3025
3026 cancel_work_sync(&ifmgd->chswitch_work);
3027 if (del_timer_sync(&ifmgd->chswitch_timer))
3028 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003029
Johannes Bergb291ba12009-07-10 15:29:03 +02003030 /* these will just be re-established on connection */
3031 del_timer_sync(&ifmgd->conn_mon_timer);
3032 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003033}
3034
3035void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3036{
3037 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3038
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303039 if (!ifmgd->associated)
3040 return;
3041
Johannes Berg95acac62011-07-12 12:30:59 +02003042 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3043 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3044 mutex_lock(&ifmgd->mtx);
3045 if (ifmgd->associated) {
3046#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3047 wiphy_debug(sdata->local->hw.wiphy,
3048 "%s: driver requested disconnect after resume.\n",
3049 sdata->name);
3050#endif
3051 ieee80211_sta_connection_lost(sdata,
3052 ifmgd->associated->bssid,
3053 WLAN_REASON_UNSPECIFIED);
3054 mutex_unlock(&ifmgd->mtx);
3055 return;
3056 }
3057 mutex_unlock(&ifmgd->mtx);
3058 }
3059
Johannes Berg5bb644a2009-05-17 11:40:42 +02003060 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3061 add_timer(&ifmgd->timer);
3062 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3063 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003064 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau46090972010-11-23 20:28:03 +01003065 ieee80211_restart_sta_timer(sdata);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003066}
3067#endif
3068
Johannes Berg9c6bd792008-09-11 00:01:52 +02003069/* interface setup */
3070void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3071{
Johannes Berg46900292009-02-15 12:44:28 +01003072 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003073
Johannes Berg46900292009-02-15 12:44:28 +01003074 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003075 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003076 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003077 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3078 ieee80211_beacon_connection_loss_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003079 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003080 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003081 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003082 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3083 (unsigned long) sdata);
3084 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3085 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003086 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303087 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003088
Johannes Bergab1faea2009-07-01 21:41:17 +02003089 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303090 ifmgd->powersave = sdata->wdev.ps;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003091
Johannes Berg77fdaa12009-07-07 03:45:17 +02003092 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003093
3094 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3095 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3096 else
3097 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003098}
3099
3100/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003101void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3102{
3103 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003104
3105 /* Restart STA timers */
3106 rcu_read_lock();
3107 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3108 ieee80211_restart_sta_timer(sdata);
3109 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003110}
Johannes Berg10f644a2009-04-16 13:17:25 +02003111
3112int ieee80211_max_network_latency(struct notifier_block *nb,
3113 unsigned long data, void *dummy)
3114{
3115 s32 latency_usec = (s32) data;
3116 struct ieee80211_local *local =
3117 container_of(nb, struct ieee80211_local,
3118 network_latency_notifier);
3119
3120 mutex_lock(&local->iflist_mtx);
3121 ieee80211_recalc_ps(local, latency_usec);
3122 mutex_unlock(&local->iflist_mtx);
3123
3124 return 0;
3125}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003126
3127/* config hooks */
3128int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3129 struct cfg80211_auth_request *req)
3130{
Johannes Berg66e67e42012-01-20 13:55:27 +01003131 struct ieee80211_local *local = sdata->local;
3132 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3133 struct ieee80211_mgd_auth_data *auth_data;
3134 struct sta_info *sta;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003135 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003136 int err;
3137
3138 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003139
3140 switch (req->auth_type) {
3141 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3142 auth_alg = WLAN_AUTH_OPEN;
3143 break;
3144 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003145 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003146 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003147 auth_alg = WLAN_AUTH_SHARED_KEY;
3148 break;
3149 case NL80211_AUTHTYPE_FT:
3150 auth_alg = WLAN_AUTH_FT;
3151 break;
3152 case NL80211_AUTHTYPE_NETWORK_EAP:
3153 auth_alg = WLAN_AUTH_LEAP;
3154 break;
3155 default:
3156 return -EOPNOTSUPP;
3157 }
3158
Johannes Berg66e67e42012-01-20 13:55:27 +01003159 auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
3160 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003161 return -ENOMEM;
3162
Johannes Berg66e67e42012-01-20 13:55:27 +01003163 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003164
3165 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003166 memcpy(auth_data->ie, req->ie, req->ie_len);
3167 auth_data->ie_len = req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003168 }
3169
Johannes Bergfffd0932009-07-08 14:22:54 +02003170 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003171 auth_data->key_len = req->key_len;
3172 auth_data->key_idx = req->key_idx;
3173 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003174 }
3175
Johannes Berg66e67e42012-01-20 13:55:27 +01003176 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003177
Johannes Berg66e67e42012-01-20 13:55:27 +01003178 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003179
Johannes Berg66e67e42012-01-20 13:55:27 +01003180 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003181
Johannes Berg66e67e42012-01-20 13:55:27 +01003182 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3183 ifmgd->assoc_data) {
3184 err = -EBUSY;
3185 goto err_free;
3186 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003187
Johannes Berg66e67e42012-01-20 13:55:27 +01003188 if (ifmgd->auth_data)
3189 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003190
Johannes Berg66e67e42012-01-20 13:55:27 +01003191 /* prep auth_data so we don't go into idle on disassoc */
3192 ifmgd->auth_data = auth_data;
3193
3194 if (ifmgd->associated)
3195 ieee80211_set_disassoc(sdata, true, false);
3196
3197 printk(KERN_DEBUG "%s: authenticate with %pM\n",
3198 sdata->name, req->bss->bssid);
3199
3200 mutex_lock(&local->mtx);
3201 ieee80211_recalc_idle(sdata->local);
3202 mutex_unlock(&local->mtx);
3203
3204 /* switch to the right channel */
3205 local->oper_channel = req->bss->channel;
3206 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
3207
3208 /* set BSSID */
3209 memcpy(ifmgd->bssid, req->bss->bssid, ETH_ALEN);
3210 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
3211
3212 /* add station entry */
3213 sta = sta_info_alloc(sdata, req->bss->bssid, GFP_KERNEL);
3214 if (!sta) {
3215 err = -ENOMEM;
3216 goto err_clear;
3217 }
Guy Eilam2a33bee2011-08-17 15:18:15 +03003218
Guy Eilam2a33bee2011-08-17 15:18:15 +03003219 err = sta_info_insert(sta);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003220 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003221 printk(KERN_DEBUG
3222 "%s: failed to insert STA entry for the AP %pM (error %d)\n",
3223 sdata->name, req->bss->bssid, err);
3224 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003225 }
3226
Johannes Berg66e67e42012-01-20 13:55:27 +01003227 err = ieee80211_probe_auth(sdata);
3228 if (err) {
3229 if (auth_data->synced)
3230 drv_finish_tx_sync(local, sdata, req->bss->bssid,
3231 IEEE80211_TX_SYNC_AUTH);
3232 sta_info_destroy_addr(sdata, req->bss->bssid);
3233 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003234 }
3235
Johannes Berg66e67e42012-01-20 13:55:27 +01003236 /* hold our own reference */
3237 cfg80211_ref_bss(auth_data->bss);
3238 err = 0;
3239 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003240
Johannes Berg66e67e42012-01-20 13:55:27 +01003241 err_clear:
3242 ifmgd->auth_data = NULL;
3243 err_free:
3244 kfree(auth_data);
3245 out_unlock:
3246 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003247
Johannes Berg66e67e42012-01-20 13:55:27 +01003248 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003249}
3250
Johannes Berg77fdaa12009-07-07 03:45:17 +02003251int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3252 struct cfg80211_assoc_request *req)
3253{
Johannes Berg66e67e42012-01-20 13:55:27 +01003254 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003255 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003256 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003257 struct ieee80211_mgd_assoc_data *assoc_data;
3258 struct sta_info *sta;
3259 const u8 *ssidie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003260 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003261
Johannes Berg66e67e42012-01-20 13:55:27 +01003262 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3263 if (!ssidie)
3264 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003265
Johannes Berg66e67e42012-01-20 13:55:27 +01003266 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3267 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003268 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003269
Johannes Berg66e67e42012-01-20 13:55:27 +01003270 mutex_lock(&ifmgd->mtx);
3271
3272 if (ifmgd->associated)
3273 ieee80211_set_disassoc(sdata, true, false);
3274
3275 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3276 err = -EBUSY;
3277 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003278 }
3279
Johannes Berg66e67e42012-01-20 13:55:27 +01003280 if (ifmgd->assoc_data) {
3281 err = -EBUSY;
3282 goto err_free;
3283 }
3284
3285 if (ifmgd->auth_data) {
3286 bool match;
3287
3288 /* keep sta info, bssid if matching */
3289 match = memcmp(ifmgd->bssid, req->bss->bssid, ETH_ALEN) == 0;
3290 ieee80211_destroy_auth_data(sdata, match);
3291 }
3292
3293 /* prepare assoc data */
3294
Johannes Berg77fdaa12009-07-07 03:45:17 +02003295 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
Vivek Natarajan375177b2010-02-09 14:50:28 +05303296 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003297
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003298 ifmgd->beacon_crc_valid = false;
3299
Johannes Berg77fdaa12009-07-07 03:45:17 +02003300 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
3301 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3302 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
3303 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
3304 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3305
Ben Greearef96a8422011-11-18 11:32:00 -08003306 if (req->flags & ASSOC_REQ_DISABLE_HT)
3307 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3308
3309 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3310 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3311 sizeof(ifmgd->ht_capa_mask));
3312
Johannes Berg77fdaa12009-07-07 03:45:17 +02003313 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003314 memcpy(assoc_data->ie, req->ie, req->ie_len);
3315 assoc_data->ie_len = req->ie_len;
3316 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003317
Johannes Berg66e67e42012-01-20 13:55:27 +01003318 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003319
Johannes Bergaf6b6372009-12-23 13:15:35 +01003320 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3321 if (ifmgd->powersave)
3322 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
3323 else
3324 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
3325 } else
3326 ifmgd->ap_smps = ifmgd->req_smps;
3327
Johannes Berg77c81442009-12-23 13:15:37 +01003328 /*
3329 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3330 * We still associate in non-HT mode (11a/b/g) if any one of these
3331 * ciphers is configured as pairwise.
3332 * We can set this to true for non-11n hardware, that'll be checked
3333 * separately along with the peer capabilities.
3334 */
Johannes Berg66e67e42012-01-20 13:55:27 +01003335 assoc_data->capability = req->bss->capability;
3336 assoc_data->wmm_used = bss->wmm_used;
3337 assoc_data->supp_rates = bss->supp_rates;
3338 assoc_data->supp_rates_len = bss->supp_rates_len;
3339 assoc_data->ht_information_ie =
Johannes Bergf679f652009-12-23 13:15:34 +01003340 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
Johannes Berg63f170e2009-12-23 13:15:33 +01003341
Kalle Valoab133152010-01-12 10:42:31 +02003342 if (bss->wmm_used && bss->uapsd_supported &&
3343 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003344 assoc_data->uapsd_used = true;
Kalle Valoab133152010-01-12 10:42:31 +02003345 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3346 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01003347 assoc_data->uapsd_used = false;
Kalle Valoab133152010-01-12 10:42:31 +02003348 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3349 }
3350
Johannes Berg66e67e42012-01-20 13:55:27 +01003351 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3352 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003353
Johannes Berg77fdaa12009-07-07 03:45:17 +02003354 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003355 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003356
3357 if (req->use_mfp) {
3358 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3359 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3360 } else {
3361 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3362 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3363 }
3364
3365 if (req->crypto.control_port)
3366 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3367 else
3368 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3369
Johannes Berga621fa42010-08-27 14:26:54 +03003370 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3371 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3372
Johannes Berg66e67e42012-01-20 13:55:27 +01003373 /* kick off associate process */
3374
3375 ifmgd->assoc_data = assoc_data;
3376
3377 mutex_lock(&local->mtx);
3378 ieee80211_recalc_idle(sdata->local);
3379 mutex_unlock(&local->mtx);
3380
3381 /* switch to the right channel */
3382 local->oper_channel = req->bss->channel;
3383 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
3384
3385 rcu_read_lock();
3386 sta = sta_info_get(sdata, req->bss->bssid);
3387 rcu_read_unlock();
3388
3389 if (!sta) {
3390 /* set BSSID */
3391 memcpy(ifmgd->bssid, req->bss->bssid, ETH_ALEN);
3392 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
3393
3394 sta = sta_info_alloc(sdata, req->bss->bssid, GFP_KERNEL);
3395 if (!sta) {
3396 err = -ENOMEM;
3397 goto err_clear;
3398 }
3399
3400 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
3401
3402 err = sta_info_insert(sta);
3403 sta = NULL;
3404 if (err) {
3405 printk(KERN_DEBUG
3406 "%s: failed to insert STA entry for the AP (error %d)\n",
3407 sdata->name, err);
3408 goto err_clear;
3409 }
3410 } else
3411 WARN_ON_ONCE(memcmp(ifmgd->bssid, req->bss->bssid, ETH_ALEN));
3412
3413 if (!bss->dtim_period &&
3414 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3415 /*
3416 * Wait up to one beacon interval ...
3417 * should this be more if we miss one?
3418 */
3419 printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
3420 sdata->name, ifmgd->bssid);
3421 assoc_data->timeout = jiffies +
3422 TU_TO_EXP_TIME(req->bss->beacon_interval);
3423 } else {
3424 assoc_data->have_beacon = true;
3425 assoc_data->sent_assoc = false;
3426 assoc_data->timeout = jiffies;
3427 }
3428 run_again(ifmgd, assoc_data->timeout);
3429
3430 err = 0;
3431 goto out;
3432 err_clear:
3433 ifmgd->assoc_data = NULL;
3434 err_free:
3435 kfree(assoc_data);
3436 out:
3437 mutex_unlock(&ifmgd->mtx);
3438
3439 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003440}
3441
3442int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003443 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003444{
3445 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen05e48e82010-06-14 11:55:56 -07003446 bool assoc_bss = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003447
Johannes Berg77fdaa12009-07-07 03:45:17 +02003448 mutex_lock(&ifmgd->mtx);
3449
Johannes Berg95de8172012-01-20 13:55:25 +01003450 if (ifmgd->associated &&
3451 memcmp(ifmgd->associated->bssid, req->bssid, ETH_ALEN) == 0) {
Johannes Berg53f73c02010-10-05 19:37:40 +02003452 ieee80211_set_disassoc(sdata, false, true);
Jouni Malinen05e48e82010-06-14 11:55:56 -07003453 assoc_bss = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003454 } else if (ifmgd->auth_data) {
3455 ieee80211_destroy_auth_data(sdata, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003456 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003457 return 0;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003458 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003459 mutex_unlock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003460
Johannes Berg0ff71612009-09-26 14:45:41 +02003461 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
Johannes Berg95de8172012-01-20 13:55:25 +01003462 sdata->name, req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003463
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003464 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3465 IEEE80211_STYPE_DEAUTH,
3466 req->reason_code, true, true);
Jouni Malinen05e48e82010-06-14 11:55:56 -07003467 if (assoc_bss)
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03003468 sta_info_flush(sdata->local, sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003469
Johannes Berg7da7cc12010-08-05 17:02:38 +02003470 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003471 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003472 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003473
Johannes Berg77fdaa12009-07-07 03:45:17 +02003474 return 0;
3475}
3476
3477int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003478 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003479{
3480 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003481 u8 bssid[ETH_ALEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003482
Johannes Berg77fdaa12009-07-07 03:45:17 +02003483 mutex_lock(&ifmgd->mtx);
3484
Johannes Berg8d8b2612009-07-25 11:58:36 +02003485 /*
3486 * cfg80211 should catch this ... but it's racy since
3487 * we can receive a disassoc frame, process it, hand it
3488 * to cfg80211 while that's in a locked section already
3489 * trying to tell us that the user wants to disconnect.
3490 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003491 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003492 mutex_unlock(&ifmgd->mtx);
3493 return -ENOLINK;
3494 }
3495
Johannes Berg0ff71612009-09-26 14:45:41 +02003496 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01003497 sdata->name, req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003498
Jouni Malinene69e95d2010-03-29 23:29:31 -07003499 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg53f73c02010-10-05 19:37:40 +02003500 ieee80211_set_disassoc(sdata, false, true);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003501
3502 mutex_unlock(&ifmgd->mtx);
3503
3504 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +02003505 IEEE80211_STYPE_DISASSOC, req->reason_code,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003506 true, !req->local_state_change);
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03003507 sta_info_flush(sdata->local, sdata);
Johannes Bergbc83b682009-11-29 12:19:06 +01003508
Johannes Berg7da7cc12010-08-05 17:02:38 +02003509 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003510 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003511 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003512
Johannes Berg77fdaa12009-07-07 03:45:17 +02003513 return 0;
3514}
Jouni Malinen026331c2010-02-15 12:53:10 +02003515
Johannes Berg54e4ffb2012-02-25 21:48:08 +01003516void ieee80211_mgd_teardown(struct ieee80211_sub_if_data *sdata)
3517{
3518 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3519
3520 mutex_lock(&ifmgd->mtx);
3521 if (ifmgd->assoc_data)
3522 ieee80211_destroy_assoc_data(sdata, false);
3523 if (ifmgd->auth_data)
3524 ieee80211_destroy_auth_data(sdata, false);
3525 del_timer_sync(&ifmgd->timer);
3526 mutex_unlock(&ifmgd->mtx);
3527}
3528
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003529void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3530 enum nl80211_cqm_rssi_threshold_event rssi_event,
3531 gfp_t gfp)
3532{
3533 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3534
Johannes Bergb5878a22010-04-07 16:48:40 +02003535 trace_api_cqm_rssi_notify(sdata, rssi_event);
3536
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003537 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3538}
3539EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Eliad Peller1d34d102011-06-06 12:59:29 +03003540
3541unsigned char ieee80211_get_operstate(struct ieee80211_vif *vif)
3542{
3543 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3544 return sdata->dev->operstate;
3545}
3546EXPORT_SYMBOL(ieee80211_get_operstate);