blob: 0a762a9ba4df3311eb810325940f2752637cb4e9 [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>
Johannes Bergd0709a62008-02-25 16:27:46 +010019#include <linux/rtnetlink.h>
Johannes Berg10f644a2009-04-16 13:17:25 +020020#include <linux/pm_qos_params.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020021#include <linux/crc32.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070022#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020023#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020024
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020026#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040027#include "rate.h"
28#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070029
30#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
31#define IEEE80211_AUTH_MAX_TRIES 3
32#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
33#define IEEE80211_ASSOC_MAX_TRIES 3
Maxim Levitskya43abf22009-07-31 18:54:12 +030034#define IEEE80211_MAX_PROBE_TRIES 5
Johannes Bergb291ba12009-07-10 15:29:03 +020035
36/*
37 * beacon loss detection timeout
38 * XXX: should depend on beacon interval
39 */
40#define IEEE80211_BEACON_LOSS_TIME (2 * HZ)
41/*
42 * Time the connection can be idle before we probe
43 * it to see if we can still talk to the AP.
44 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030045#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020046/*
47 * Time we wait for a probe response after sending
48 * a probe request because of beacon loss or for
49 * checking the connection still works.
50 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030051#define IEEE80211_PROBE_WAIT (HZ / 2)
Jiri Bencf0706e82007-05-05 11:45:53 -070052
Johannes Berg5bb644a2009-05-17 11:40:42 +020053#define TMR_RUNNING_TIMER 0
54#define TMR_RUNNING_CHANSW 1
55
Johannes Berg77fdaa12009-07-07 03:45:17 +020056/*
57 * All cfg80211 functions have to be called outside a locked
58 * section so that they can acquire a lock themselves... This
59 * is much simpler than queuing up things in cfg80211, but we
60 * do need some indirection for that here.
61 */
62enum rx_mgmt_action {
63 /* no action required */
64 RX_MGMT_NONE,
65
66 /* caller must call cfg80211_send_rx_auth() */
67 RX_MGMT_CFG80211_AUTH,
68
69 /* caller must call cfg80211_send_rx_assoc() */
70 RX_MGMT_CFG80211_ASSOC,
71
72 /* caller must call cfg80211_send_deauth() */
73 RX_MGMT_CFG80211_DEAUTH,
74
75 /* caller must call cfg80211_send_disassoc() */
76 RX_MGMT_CFG80211_DISASSOC,
77
78 /* caller must call cfg80211_auth_timeout() & free work */
79 RX_MGMT_CFG80211_AUTH_TO,
80
81 /* caller must call cfg80211_assoc_timeout() & free work */
82 RX_MGMT_CFG80211_ASSOC_TO,
83};
84
Johannes Berg5484e232008-09-08 17:44:27 +020085/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +020086static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
87{
88 WARN_ON(!mutex_is_locked(&ifmgd->mtx));
89}
90
Johannes Bergca386f32009-07-10 02:39:48 +020091/*
92 * We can have multiple work items (and connection probing)
93 * scheduling this timer, but we need to take care to only
94 * reschedule it when it should fire _earlier_ than it was
95 * asked for before, or if it's not pending right now. This
96 * function ensures that. Note that it then is required to
97 * run this function for all timeouts after the first one
98 * has happened -- the work that runs from this timer will
99 * do that.
100 */
101static void run_again(struct ieee80211_if_managed *ifmgd,
102 unsigned long timeout)
103{
104 ASSERT_MGD_MTX(ifmgd);
105
106 if (!timer_pending(&ifmgd->timer) ||
107 time_before(timeout, ifmgd->timer.expires))
108 mod_timer(&ifmgd->timer, timeout);
109}
110
Johannes Bergb291ba12009-07-10 15:29:03 +0200111static void mod_beacon_timer(struct ieee80211_sub_if_data *sdata)
112{
113 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
114 return;
115
116 mod_timer(&sdata->u.mgd.bcn_mon_timer,
117 round_jiffies_up(jiffies + IEEE80211_BEACON_LOSS_TIME));
118}
119
Johannes Berg5484e232008-09-08 17:44:27 +0200120static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200121{
Johannes Berg5484e232008-09-08 17:44:27 +0200122 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200123}
124
Johannes Bergc2b13452008-09-11 00:01:55 +0200125static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
Johannes Berg9ac19a92008-09-09 10:57:09 +0200126 struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +0100127 u32 *rates)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200128{
129 int i, j, count;
130 *rates = 0;
131 count = 0;
132 for (i = 0; i < bss->supp_rates_len; i++) {
133 int rate = (bss->supp_rates[i] & 0x7F) * 5;
134
135 for (j = 0; j < sband->n_bitrates; j++)
136 if (sband->bitrates[j].bitrate == rate) {
137 *rates |= BIT(j);
138 count++;
139 break;
140 }
141 }
142
143 return count;
144}
145
Johannes Bergd5522e02009-03-30 13:23:35 +0200146/*
147 * ieee80211_enable_ht should be called only after the operating band
148 * has been determined as ht configuration depends on the hw's
149 * HT abilities for a specific band.
150 */
151static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
152 struct ieee80211_ht_info *hti,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200153 const u8 *bssid, u16 ap_ht_cap_flags)
Johannes Bergd5522e02009-03-30 13:23:35 +0200154{
155 struct ieee80211_local *local = sdata->local;
156 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200157 struct sta_info *sta;
158 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200159 u16 ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200160 bool enable_ht = true, ht_changed;
161 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
162
163 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
164
Johannes Bergd5522e02009-03-30 13:23:35 +0200165 /* HT is not supported */
166 if (!sband->ht_cap.ht_supported)
167 enable_ht = false;
168
169 /* check that channel matches the right operating channel */
170 if (local->hw.conf.channel->center_freq !=
171 ieee80211_channel_to_frequency(hti->control_chan))
172 enable_ht = false;
173
174 if (enable_ht) {
175 channel_type = NL80211_CHAN_HT20;
176
177 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
178 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
179 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
180 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
181 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400182 if (!(local->hw.conf.channel->flags &
183 IEEE80211_CHAN_NO_HT40PLUS))
184 channel_type = NL80211_CHAN_HT40PLUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200185 break;
186 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400187 if (!(local->hw.conf.channel->flags &
188 IEEE80211_CHAN_NO_HT40MINUS))
189 channel_type = NL80211_CHAN_HT40MINUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200190 break;
191 }
192 }
193 }
194
195 ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
196 channel_type != local->hw.conf.channel_type;
197
198 local->oper_channel_type = channel_type;
199
200 if (ht_changed) {
201 /* channel_type change automatically detected */
202 ieee80211_hw_config(local, 0);
203
204 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100205 sta = sta_info_get(sdata, bssid);
Johannes Bergd5522e02009-03-30 13:23:35 +0200206 if (sta)
207 rate_control_rate_update(local, sband, sta,
208 IEEE80211_RC_HT_CHANGED);
Johannes Bergd5522e02009-03-30 13:23:35 +0200209 rcu_read_unlock();
Johannes Bergd5522e02009-03-30 13:23:35 +0200210 }
211
212 /* disable HT */
213 if (!enable_ht)
214 return 0;
215
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200216 ht_opmode = le16_to_cpu(hti->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200217
218 /* if bss configuration changed store the new one */
Johannes Berg413ad502009-05-08 21:21:06 +0200219 if (!sdata->ht_opmode_valid ||
220 sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200221 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200222 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Berg413ad502009-05-08 21:21:06 +0200223 sdata->ht_opmode_valid = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200224 }
225
226 return changed;
227}
228
Johannes Berg9c6bd792008-09-11 00:01:52 +0200229/* frame sending functions */
230
Johannes Berg77fdaa12009-07-07 03:45:17 +0200231static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
232 struct ieee80211_mgd_work *wk)
Johannes Berg60f8b392008-09-08 17:44:22 +0200233{
Johannes Berg46900292009-02-15 12:44:28 +0100234 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200235 struct ieee80211_local *local = sdata->local;
236 struct sk_buff *skb;
237 struct ieee80211_mgmt *mgmt;
Johannes Berg517357c2009-07-02 17:18:40 +0200238 u8 *pos;
239 const u8 *ies, *ht_ie;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200240 int i, len, count, rates_len, supp_rates_len;
241 u16 capab;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200242 int wmm = 0;
243 struct ieee80211_supported_band *sband;
Johannes Berg881d9482009-01-21 15:13:48 +0100244 u32 rates = 0;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200245
246 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
Johannes Berg77fdaa12009-07-07 03:45:17 +0200247 sizeof(*mgmt) + 200 + wk->ie_len +
248 wk->ssid_len);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200249 if (!skb) {
250 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
Johannes Berg47846c92009-11-25 17:46:19 +0100251 "frame\n", sdata->name);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200252 return;
253 }
254 skb_reserve(skb, local->hw.extra_tx_headroom);
255
256 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
257
Johannes Berg46900292009-02-15 12:44:28 +0100258 capab = ifmgd->capab;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200259
260 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
261 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
262 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
263 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
264 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
265 }
266
Johannes Berg77fdaa12009-07-07 03:45:17 +0200267 if (wk->bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
268 capab |= WLAN_CAPABILITY_PRIVACY;
269 if (wk->bss->wmm_used)
270 wmm = 1;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200271
Johannes Berg77fdaa12009-07-07 03:45:17 +0200272 /* get all rates supported by the device and the AP as
273 * some APs don't like getting a superset of their rates
274 * in the association request (e.g. D-Link DAP 1353 in
275 * b-only mode) */
276 rates_len = ieee80211_compatible_rates(wk->bss, sband, &rates);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200277
Johannes Berg77fdaa12009-07-07 03:45:17 +0200278 if ((wk->bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
279 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
280 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200281
282 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
283 memset(mgmt, 0, 24);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200284 memcpy(mgmt->da, wk->bss->cbss.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100285 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200286 memcpy(mgmt->bssid, wk->bss->cbss.bssid, ETH_ALEN);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200287
Johannes Berg77fdaa12009-07-07 03:45:17 +0200288 if (!is_zero_ether_addr(wk->prev_bssid)) {
Johannes Berg9ac19a92008-09-09 10:57:09 +0200289 skb_put(skb, 10);
290 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
291 IEEE80211_STYPE_REASSOC_REQ);
292 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
293 mgmt->u.reassoc_req.listen_interval =
294 cpu_to_le16(local->hw.conf.listen_interval);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200295 memcpy(mgmt->u.reassoc_req.current_ap, wk->prev_bssid,
Johannes Berg9ac19a92008-09-09 10:57:09 +0200296 ETH_ALEN);
297 } else {
298 skb_put(skb, 4);
299 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
300 IEEE80211_STYPE_ASSOC_REQ);
301 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Rami Rosen13554122008-12-16 22:38:29 +0200302 mgmt->u.assoc_req.listen_interval =
Johannes Berg9ac19a92008-09-09 10:57:09 +0200303 cpu_to_le16(local->hw.conf.listen_interval);
304 }
305
306 /* SSID */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200307 ies = pos = skb_put(skb, 2 + wk->ssid_len);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200308 *pos++ = WLAN_EID_SSID;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200309 *pos++ = wk->ssid_len;
310 memcpy(pos, wk->ssid, wk->ssid_len);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200311
312 /* add all rates which were marked to be used above */
313 supp_rates_len = rates_len;
314 if (supp_rates_len > 8)
315 supp_rates_len = 8;
316
317 len = sband->n_bitrates;
318 pos = skb_put(skb, supp_rates_len + 2);
319 *pos++ = WLAN_EID_SUPP_RATES;
320 *pos++ = supp_rates_len;
321
322 count = 0;
323 for (i = 0; i < sband->n_bitrates; i++) {
324 if (BIT(i) & rates) {
325 int rate = sband->bitrates[i].bitrate;
326 *pos++ = (u8) (rate / 5);
327 if (++count == 8)
328 break;
329 }
330 }
331
332 if (rates_len > count) {
333 pos = skb_put(skb, rates_len - count + 2);
334 *pos++ = WLAN_EID_EXT_SUPP_RATES;
335 *pos++ = rates_len - count;
336
337 for (i++; i < sband->n_bitrates; i++) {
338 if (BIT(i) & rates) {
339 int rate = sband->bitrates[i].bitrate;
340 *pos++ = (u8) (rate / 5);
341 }
342 }
343 }
344
345 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
346 /* 1. power capabilities */
347 pos = skb_put(skb, 4);
348 *pos++ = WLAN_EID_PWR_CAPABILITY;
349 *pos++ = 2;
350 *pos++ = 0; /* min tx power */
351 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
352
353 /* 2. supported channels */
354 /* TODO: get this in reg domain format */
355 pos = skb_put(skb, 2 * sband->n_channels + 2);
356 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
357 *pos++ = 2 * sband->n_channels;
358 for (i = 0; i < sband->n_channels; i++) {
359 *pos++ = ieee80211_frequency_to_channel(
360 sband->channels[i].center_freq);
361 *pos++ = 1; /* one channel in the subband*/
362 }
363 }
364
Johannes Berg77fdaa12009-07-07 03:45:17 +0200365 if (wk->ie_len && wk->ie) {
366 pos = skb_put(skb, wk->ie_len);
367 memcpy(pos, wk->ie, wk->ie_len);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200368 }
369
Johannes Berg46900292009-02-15 12:44:28 +0100370 if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
Johannes Berg9ac19a92008-09-09 10:57:09 +0200371 pos = skb_put(skb, 9);
372 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
373 *pos++ = 7; /* len */
374 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
375 *pos++ = 0x50;
376 *pos++ = 0xf2;
377 *pos++ = 2; /* WME */
378 *pos++ = 0; /* WME info */
379 *pos++ = 1; /* WME ver */
380 *pos++ = 0;
381 }
382
383 /* wmm support is a must to HT */
Vasanthakumar Thiagarajaneb469362008-12-23 21:30:50 +0530384 /*
385 * IEEE802.11n does not allow TKIP/WEP as pairwise
386 * ciphers in HT mode. We still associate in non-ht
387 * mode (11a/b/g) if any one of these ciphers is
388 * configured as pairwise.
389 */
Johannes Berg46900292009-02-15 12:44:28 +0100390 if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200391 sband->ht_cap.ht_supported &&
Johannes Berg77fdaa12009-07-07 03:45:17 +0200392 (ht_ie = ieee80211_bss_get_ie(&wk->bss->cbss, WLAN_EID_HT_INFORMATION)) &&
Vasanthakumar Thiagarajaneb469362008-12-23 21:30:50 +0530393 ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
Johannes Bergab1faea2009-07-01 21:41:17 +0200394 (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))) {
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200395 struct ieee80211_ht_info *ht_info =
396 (struct ieee80211_ht_info *)(ht_ie + 2);
397 u16 cap = sband->ht_cap.cap;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200398 __le16 tmp;
399 u32 flags = local->hw.conf.channel->flags;
400
Johannes Berg0f782312009-12-01 13:37:02 +0100401 /* determine capability flags */
402
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200403 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
404 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -0400405 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200406 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200407 cap &= ~IEEE80211_HT_CAP_SGI_40;
408 }
409 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200410 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -0400411 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200412 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200413 cap &= ~IEEE80211_HT_CAP_SGI_40;
414 }
415 break;
416 }
417
Johannes Berg0f782312009-12-01 13:37:02 +0100418 /* set SM PS mode properly */
419 cap &= ~IEEE80211_HT_CAP_SM_PS;
420 /* new association always uses requested smps mode */
421 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
422 if (ifmgd->powersave)
423 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
424 else
425 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
426 } else
427 ifmgd->ap_smps = ifmgd->req_smps;
428
429 switch (ifmgd->ap_smps) {
430 case IEEE80211_SMPS_AUTOMATIC:
431 case IEEE80211_SMPS_NUM_MODES:
432 WARN_ON(1);
433 case IEEE80211_SMPS_OFF:
434 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
435 IEEE80211_HT_CAP_SM_PS_SHIFT;
436 break;
437 case IEEE80211_SMPS_STATIC:
438 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
439 IEEE80211_HT_CAP_SM_PS_SHIFT;
440 break;
441 case IEEE80211_SMPS_DYNAMIC:
442 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
443 IEEE80211_HT_CAP_SM_PS_SHIFT;
444 break;
445 }
446
447 /* reserve and fill IE */
448
449 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200450 *pos++ = WLAN_EID_HT_CAPABILITY;
451 *pos++ = sizeof(struct ieee80211_ht_cap);
452 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
Johannes Berg0f782312009-12-01 13:37:02 +0100453
454 /* capability flags */
455 tmp = cpu_to_le16(cap);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200456 memcpy(pos, &tmp, sizeof(u16));
457 pos += sizeof(u16);
Johannes Berg0f782312009-12-01 13:37:02 +0100458
459 /* AMPDU parameters */
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200460 *pos++ = sband->ht_cap.ampdu_factor |
Johannes Berg0f782312009-12-01 13:37:02 +0100461 (sband->ht_cap.ampdu_density <<
462 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
463
464 /* MCS set */
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200465 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
Johannes Berg0f782312009-12-01 13:37:02 +0100466 pos += sizeof(sband->ht_cap.mcs);
467
468 /* extended capabilities */
469 pos += sizeof(__le16);
470
471 /* BF capabilities */
472 pos += sizeof(__le32);
473
474 /* antenna selection */
475 pos += sizeof(u8);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200476 }
477
Johannes Berg62ae67b2009-11-18 18:42:05 +0100478 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
479 ieee80211_tx_skb(sdata, skb);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200480}
481
482
Johannes Bergef422bc2008-09-09 10:58:25 +0200483static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503dd2009-07-07 03:56:11 +0200484 const u8 *bssid, u16 stype, u16 reason,
485 void *cookie)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200486{
487 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100488 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200489 struct sk_buff *skb;
490 struct ieee80211_mgmt *mgmt;
491
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200492 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
Johannes Berg9ac19a92008-09-09 10:57:09 +0200493 if (!skb) {
Johannes Bergef422bc2008-09-09 10:58:25 +0200494 printk(KERN_DEBUG "%s: failed to allocate buffer for "
Johannes Berg47846c92009-11-25 17:46:19 +0100495 "deauth/disassoc frame\n", sdata->name);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200496 return;
497 }
498 skb_reserve(skb, local->hw.extra_tx_headroom);
499
500 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
501 memset(mgmt, 0, 24);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200502 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100503 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200504 memcpy(mgmt->bssid, bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200505 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200506 skb_put(skb, 2);
Johannes Bergef422bc2008-09-09 10:58:25 +0200507 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200508 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
509
Jouni Malinen53b46b82009-03-27 20:53:56 +0200510 if (stype == IEEE80211_STYPE_DEAUTH)
Holger Schurigce470612009-10-13 13:28:13 +0200511 if (cookie)
512 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
513 else
514 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Jouni Malinen53b46b82009-03-27 20:53:56 +0200515 else
Holger Schurigce470612009-10-13 13:28:13 +0200516 if (cookie)
517 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
518 else
519 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg62ae67b2009-11-18 18:42:05 +0100520 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
521 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
522 ieee80211_tx_skb(sdata, skb);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200523}
524
Kalle Valo572e0012009-02-10 17:09:31 +0200525void ieee80211_send_pspoll(struct ieee80211_local *local,
526 struct ieee80211_sub_if_data *sdata)
527{
Johannes Berg46900292009-02-15 12:44:28 +0100528 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Kalle Valo572e0012009-02-10 17:09:31 +0200529 struct ieee80211_pspoll *pspoll;
530 struct sk_buff *skb;
531 u16 fc;
532
533 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
534 if (!skb) {
535 printk(KERN_DEBUG "%s: failed to allocate buffer for "
Johannes Berg47846c92009-11-25 17:46:19 +0100536 "pspoll frame\n", sdata->name);
Kalle Valo572e0012009-02-10 17:09:31 +0200537 return;
538 }
539 skb_reserve(skb, local->hw.extra_tx_headroom);
540
541 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
542 memset(pspoll, 0, sizeof(*pspoll));
543 fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
544 pspoll->frame_control = cpu_to_le16(fc);
Johannes Berg46900292009-02-15 12:44:28 +0100545 pspoll->aid = cpu_to_le16(ifmgd->aid);
Kalle Valo572e0012009-02-10 17:09:31 +0200546
547 /* aid in PS-Poll has its two MSBs each set to 1 */
548 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
549
Johannes Berg46900292009-02-15 12:44:28 +0100550 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100551 memcpy(pspoll->ta, sdata->vif.addr, ETH_ALEN);
Kalle Valo572e0012009-02-10 17:09:31 +0200552
Johannes Berg62ae67b2009-11-18 18:42:05 +0100553 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
554 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200555}
556
Johannes Berg965beda2009-04-16 13:17:24 +0200557void ieee80211_send_nullfunc(struct ieee80211_local *local,
558 struct ieee80211_sub_if_data *sdata,
559 int powersave)
560{
561 struct sk_buff *skb;
562 struct ieee80211_hdr *nullfunc;
563 __le16 fc;
564
565 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
566 return;
567
568 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
569 if (!skb) {
570 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
Johannes Berg47846c92009-11-25 17:46:19 +0100571 "frame\n", sdata->name);
Johannes Berg965beda2009-04-16 13:17:24 +0200572 return;
573 }
574 skb_reserve(skb, local->hw.extra_tx_headroom);
575
576 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
577 memset(nullfunc, 0, 24);
578 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
579 IEEE80211_FCTL_TODS);
580 if (powersave)
581 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
582 nullfunc->frame_control = fc;
583 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100584 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berg965beda2009-04-16 13:17:24 +0200585 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
586
Johannes Berg62ae67b2009-11-18 18:42:05 +0100587 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
588 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200589}
590
Johannes Bergcc32abd2009-05-15 11:52:31 +0200591/* spectrum management related things */
592static void ieee80211_chswitch_work(struct work_struct *work)
593{
594 struct ieee80211_sub_if_data *sdata =
595 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
597
598 if (!netif_running(sdata->dev))
599 return;
600
Johannes Berg77fdaa12009-07-07 03:45:17 +0200601 mutex_lock(&ifmgd->mtx);
602 if (!ifmgd->associated)
603 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200604
605 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200606 ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200607
Johannes Berg77fdaa12009-07-07 03:45:17 +0200608 /* XXX: shouldn't really modify cfg80211-owned data! */
609 ifmgd->associated->cbss.channel = sdata->local->oper_channel;
610
Johannes Bergcc32abd2009-05-15 11:52:31 +0200611 ieee80211_wake_queues_by_reason(&sdata->local->hw,
612 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200613 out:
614 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
615 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200616}
617
618static void ieee80211_chswitch_timer(unsigned long data)
619{
620 struct ieee80211_sub_if_data *sdata =
621 (struct ieee80211_sub_if_data *) data;
622 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
623
Johannes Berg5bb644a2009-05-17 11:40:42 +0200624 if (sdata->local->quiescing) {
625 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
626 return;
627 }
628
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400629 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200630}
631
632void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
633 struct ieee80211_channel_sw_ie *sw_elem,
634 struct ieee80211_bss *bss)
635{
636 struct ieee80211_channel *new_ch;
637 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
638 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
639
Johannes Berg77fdaa12009-07-07 03:45:17 +0200640 ASSERT_MGD_MTX(ifmgd);
641
642 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200643 return;
644
Helmut Schaafbe9c422009-07-23 12:14:04 +0200645 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200646 return;
647
648 /* Disregard subsequent beacons if we are already running a timer
649 processing a CSA */
650
651 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
652 return;
653
654 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
655 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
656 return;
657
658 sdata->local->csa_channel = new_ch;
659
660 if (sw_elem->count <= 1) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400661 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200662 } else {
663 ieee80211_stop_queues_by_reason(&sdata->local->hw,
664 IEEE80211_QUEUE_STOP_REASON_CSA);
665 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
666 mod_timer(&ifmgd->chswitch_timer,
667 jiffies +
668 msecs_to_jiffies(sw_elem->count *
669 bss->cbss.beacon_interval));
670 }
671}
672
673static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
674 u16 capab_info, u8 *pwr_constr_elem,
675 u8 pwr_constr_elem_len)
676{
677 struct ieee80211_conf *conf = &sdata->local->hw.conf;
678
679 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
680 return;
681
682 /* Power constraint IE length should be 1 octet */
683 if (pwr_constr_elem_len != 1)
684 return;
685
686 if ((*pwr_constr_elem <= conf->channel->max_power) &&
687 (*pwr_constr_elem != sdata->local->power_constr_level)) {
688 sdata->local->power_constr_level = *pwr_constr_elem;
689 ieee80211_hw_config(sdata->local, 0);
690 }
691}
692
Johannes Berg965beda2009-04-16 13:17:24 +0200693/* powersave */
694static void ieee80211_enable_ps(struct ieee80211_local *local,
695 struct ieee80211_sub_if_data *sdata)
696{
697 struct ieee80211_conf *conf = &local->hw.conf;
698
Johannes Bergd5edaed2009-04-22 23:02:51 +0200699 /*
700 * If we are scanning right now then the parameters will
701 * take effect when scan finishes.
702 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200703 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200704 return;
705
Johannes Berg965beda2009-04-16 13:17:24 +0200706 if (conf->dynamic_ps_timeout > 0 &&
707 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
708 mod_timer(&local->dynamic_ps_timer, jiffies +
709 msecs_to_jiffies(conf->dynamic_ps_timeout));
710 } else {
711 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
712 ieee80211_send_nullfunc(local, sdata, 1);
713 conf->flags |= IEEE80211_CONF_PS;
714 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
715 }
716}
717
718static void ieee80211_change_ps(struct ieee80211_local *local)
719{
720 struct ieee80211_conf *conf = &local->hw.conf;
721
722 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200723 ieee80211_enable_ps(local, local->ps_sdata);
724 } else if (conf->flags & IEEE80211_CONF_PS) {
725 conf->flags &= ~IEEE80211_CONF_PS;
726 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
727 del_timer_sync(&local->dynamic_ps_timer);
728 cancel_work_sync(&local->dynamic_ps_enable_work);
729 }
730}
731
732/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200733void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200734{
735 struct ieee80211_sub_if_data *sdata, *found = NULL;
736 int count = 0;
737
738 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
739 local->ps_sdata = NULL;
740 return;
741 }
742
743 list_for_each_entry(sdata, &local->interfaces, list) {
744 if (!netif_running(sdata->dev))
745 continue;
746 if (sdata->vif.type != NL80211_IFTYPE_STATION)
747 continue;
748 found = sdata;
749 count++;
750 }
751
Luis R. Rodriguez43f78532009-06-10 15:16:15 +0200752 if (count == 1 && found->u.mgd.powersave &&
Johannes Berg77fdaa12009-07-07 03:45:17 +0200753 found->u.mgd.associated && list_empty(&found->u.mgd.work_list) &&
Johannes Bergb291ba12009-07-10 15:29:03 +0200754 !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
755 IEEE80211_STA_CONNECTION_POLL))) {
Johannes Berg10f644a2009-04-16 13:17:25 +0200756 s32 beaconint_us;
757
758 if (latency < 0)
759 latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
760
761 beaconint_us = ieee80211_tu_to_usec(
762 found->vif.bss_conf.beacon_int);
763
Johannes Berg04fe2032009-04-22 18:44:37 +0200764 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +0200765 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +0200766 } else {
767 u8 dtimper = found->vif.bss_conf.dtim_period;
768 int maxslp = 1;
769
770 if (dtimper > 1)
771 maxslp = min_t(int, dtimper,
772 latency / beaconint_us);
773
Johannes Berg9ccebe62009-04-23 10:32:36 +0200774 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg10f644a2009-04-16 13:17:25 +0200775 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +0200776 }
Johannes Berg10f644a2009-04-16 13:17:25 +0200777 } else {
Johannes Berg965beda2009-04-16 13:17:24 +0200778 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +0200779 }
Johannes Berg965beda2009-04-16 13:17:24 +0200780
781 ieee80211_change_ps(local);
782}
783
784void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
785{
786 struct ieee80211_local *local =
787 container_of(work, struct ieee80211_local,
788 dynamic_ps_disable_work);
789
790 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
791 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
792 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
793 }
794
795 ieee80211_wake_queues_by_reason(&local->hw,
796 IEEE80211_QUEUE_STOP_REASON_PS);
797}
798
799void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
800{
801 struct ieee80211_local *local =
802 container_of(work, struct ieee80211_local,
803 dynamic_ps_enable_work);
804 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
805
806 /* can only happen when PS was just disabled anyway */
807 if (!sdata)
808 return;
809
810 if (local->hw.conf.flags & IEEE80211_CONF_PS)
811 return;
812
813 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
814 ieee80211_send_nullfunc(local, sdata, 1);
815
816 local->hw.conf.flags |= IEEE80211_CONF_PS;
817 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
818}
819
820void ieee80211_dynamic_ps_timer(unsigned long data)
821{
822 struct ieee80211_local *local = (void *) data;
823
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -0700824 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +0200825 return;
826
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400827 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +0200828}
829
Johannes Berg60f8b392008-09-08 17:44:22 +0200830/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200831static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg46900292009-02-15 12:44:28 +0100832 struct ieee80211_if_managed *ifmgd,
Jiri Bencf0706e82007-05-05 11:45:53 -0700833 u8 *wmm_param, size_t wmm_param_len)
834{
Jiri Bencf0706e82007-05-05 11:45:53 -0700835 struct ieee80211_tx_queue_params params;
836 size_t left;
837 int count;
838 u8 *pos;
839
Johannes Berg46900292009-02-15 12:44:28 +0100840 if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
Johannes Berg3434fbd2008-05-03 00:59:37 +0200841 return;
842
843 if (!wmm_param)
844 return;
845
Jiri Bencf0706e82007-05-05 11:45:53 -0700846 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
847 return;
848 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +0100849 if (count == ifmgd->wmm_last_param_set)
Jiri Bencf0706e82007-05-05 11:45:53 -0700850 return;
Johannes Berg46900292009-02-15 12:44:28 +0100851 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -0700852
853 pos = wmm_param + 8;
854 left = wmm_param_len - 8;
855
856 memset(&params, 0, sizeof(params));
857
Jiri Bencf0706e82007-05-05 11:45:53 -0700858 local->wmm_acm = 0;
859 for (; left >= 4; left -= 4, pos += 4) {
860 int aci = (pos[0] >> 5) & 0x03;
861 int acm = (pos[0] >> 4) & 0x01;
862 int queue;
863
864 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200865 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +0200866 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200867 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200868 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Jiri Bencf0706e82007-05-05 11:45:53 -0700869 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200870 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +0200871 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200872 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200873 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Jiri Bencf0706e82007-05-05 11:45:53 -0700874 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200875 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +0200876 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200877 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200878 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Jiri Bencf0706e82007-05-05 11:45:53 -0700879 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200880 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -0700881 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200882 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200883 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200884 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Jiri Bencf0706e82007-05-05 11:45:53 -0700885 break;
886 }
887
888 params.aifs = pos[0] & 0x0f;
889 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
890 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200891 params.txop = get_unaligned_le16(pos + 2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200892#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700893 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100894 "cWmin=%d cWmax=%d txop=%d\n",
Johannes Berg0bffe402009-06-09 16:18:32 +0200895 wiphy_name(local->hw.wiphy), queue, aci, acm,
896 params.aifs, params.cw_min, params.cw_max, params.txop);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100897#endif
Johannes Berg24487982009-04-23 18:52:52 +0200898 if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700899 printk(KERN_DEBUG "%s: failed to set TX queue "
Johannes Berg0bffe402009-06-09 16:18:32 +0200900 "parameters for queue %d\n",
901 wiphy_name(local->hw.wiphy), queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700902 }
903}
904
Johannes Berg7a5158e2008-10-08 10:59:33 +0200905static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
906 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +0200907{
Johannes Bergbda39332008-10-11 01:51:51 +0200908 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100909 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +0200910 bool use_protection;
911 bool use_short_preamble;
912 bool use_short_slot;
913
914 if (erp_valid) {
915 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
916 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
917 } else {
918 use_protection = false;
919 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
920 }
921
922 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Daniel Drake56282212007-07-10 19:32:10 +0200923
Johannes Berg471b3ef2007-12-28 14:32:58 +0100924 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +0100925 bss_conf->use_cts_prot = use_protection;
926 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200927 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200928
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200929 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200930 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100931 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200932 }
Daniel Draked9430a32007-07-27 15:43:24 +0200933
Johannes Berg7a5158e2008-10-08 10:59:33 +0200934 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +0200935 bss_conf->use_short_slot = use_short_slot;
936 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -0400937 }
938
939 return changed;
940}
941
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200942static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berge21546a2009-08-06 20:41:32 +0200943 struct ieee80211_mgd_work *wk,
Johannes Bergae5eb022008-10-14 16:58:37 +0200944 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700945{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100946 struct ieee80211_local *local = sdata->local;
Johannes Berge21546a2009-08-06 20:41:32 +0200947 struct ieee80211_bss *bss = wk->bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400948
Johannes Bergae5eb022008-10-14 16:58:37 +0200949 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200950 /* set timing information */
951 sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
952 sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
953 sdata->vif.bss_conf.dtim_period = bss->dtim_period;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400954
Johannes Berg77fdaa12009-07-07 03:45:17 +0200955 bss_info_changed |= BSS_CHANGED_BEACON_INT;
956 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
957 bss->cbss.capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700958
Johannes Berg77fdaa12009-07-07 03:45:17 +0200959 sdata->u.mgd.associated = bss;
Johannes Berge21546a2009-08-06 20:41:32 +0200960 sdata->u.mgd.old_associate_work = wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200961 memcpy(sdata->u.mgd.bssid, bss->cbss.bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100962
Johannes Bergb291ba12009-07-10 15:29:03 +0200963 /* just to be sure */
964 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
965 IEEE80211_STA_BEACON_POLL);
966
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200967 ieee80211_led_assoc(local, 1);
968
Johannes Bergbda39332008-10-11 01:51:51 +0200969 sdata->vif.bss_conf.assoc = 1;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200970 /*
971 * For now just always ask the driver to update the basic rateset
972 * when we have associated, we aren't checking whether it actually
973 * changed or not.
974 */
Johannes Bergae5eb022008-10-14 16:58:37 +0200975 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
Johannes Berg9cef8732009-05-14 13:10:14 +0200976
977 /* And the BSSID changed - we're associated now */
978 bss_info_changed |= BSS_CHANGED_BSSID;
979
Johannes Bergae5eb022008-10-14 16:58:37 +0200980 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300981
Johannes Berg056508d2009-07-30 21:43:55 +0200982 mutex_lock(&local->iflist_mtx);
983 ieee80211_recalc_ps(local, -1);
Johannes Berg0f782312009-12-01 13:37:02 +0100984 ieee80211_recalc_smps(local, sdata);
Johannes Berg056508d2009-07-30 21:43:55 +0200985 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +0200986
John W. Linville53623f12009-10-15 15:10:16 -0400987 netif_start_queue(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200988 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700989}
990
Johannes Berg77fdaa12009-07-07 03:45:17 +0200991static enum rx_mgmt_action __must_check
992ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
993 struct ieee80211_mgd_work *wk)
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300994{
Johannes Berg46900292009-02-15 12:44:28 +0100995 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Helmut Schaa11432372009-03-12 14:04:34 +0100996 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100997
Johannes Berg77fdaa12009-07-07 03:45:17 +0200998 wk->tries++;
999 if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg0c68ae262008-10-27 15:56:10 -07001000 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001001 sdata->name, wk->bss->cbss.bssid);
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +05301002
1003 /*
1004 * Most likely AP is not in the range so remove the
Johannes Berg77fdaa12009-07-07 03:45:17 +02001005 * bss struct for that AP.
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +05301006 */
Johannes Berg77fdaa12009-07-07 03:45:17 +02001007 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
Helmut Schaa11432372009-03-12 14:04:34 +01001008
1009 /*
1010 * We might have a pending scan which had no chance to run yet
Johannes Berg77fdaa12009-07-07 03:45:17 +02001011 * due to work needing to be done. Hence, queue the STAs work
1012 * again for that.
Helmut Schaa11432372009-03-12 14:04:34 +01001013 */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001014 ieee80211_queue_work(&local->hw, &ifmgd->work);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001015 return RX_MGMT_CFG80211_AUTH_TO;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001016 }
1017
Johannes Berg77fdaa12009-07-07 03:45:17 +02001018 printk(KERN_DEBUG "%s: direct probe to AP %pM (try %d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001019 sdata->name, wk->bss->cbss.bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001020 wk->tries);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001021
Johannes Berg77fdaa12009-07-07 03:45:17 +02001022 /*
1023 * Direct probe is sent to broadcast address as some APs
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001024 * will not answer to direct packet in unassociated state.
1025 */
Johannes Berg77fdaa12009-07-07 03:45:17 +02001026 ieee80211_send_probe_req(sdata, NULL, wk->ssid, wk->ssid_len, NULL, 0);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001027
Johannes Berg77fdaa12009-07-07 03:45:17 +02001028 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Bergca386f32009-07-10 02:39:48 +02001029 run_again(ifmgd, wk->timeout);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001030
1031 return RX_MGMT_NONE;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001032}
1033
Jiri Bencf0706e82007-05-05 11:45:53 -07001034
Johannes Berg77fdaa12009-07-07 03:45:17 +02001035static enum rx_mgmt_action __must_check
1036ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
1037 struct ieee80211_mgd_work *wk)
Jiri Bencf0706e82007-05-05 11:45:53 -07001038{
Johannes Berg46900292009-02-15 12:44:28 +01001039 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Helmut Schaa11432372009-03-12 14:04:34 +01001040 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +01001041
Johannes Berg77fdaa12009-07-07 03:45:17 +02001042 wk->tries++;
1043 if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg0c68ae262008-10-27 15:56:10 -07001044 printk(KERN_DEBUG "%s: authentication with AP %pM"
Jiri Bencf0706e82007-05-05 11:45:53 -07001045 " timed out\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001046 sdata->name, wk->bss->cbss.bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001047
1048 /*
1049 * Most likely AP is not in the range so remove the
1050 * bss struct for that AP.
1051 */
1052 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
Helmut Schaa11432372009-03-12 14:04:34 +01001053
1054 /*
1055 * We might have a pending scan which had no chance to run yet
Johannes Berg77fdaa12009-07-07 03:45:17 +02001056 * due to work needing to be done. Hence, queue the STAs work
1057 * again for that.
Helmut Schaa11432372009-03-12 14:04:34 +01001058 */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001059 ieee80211_queue_work(&local->hw, &ifmgd->work);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001060 return RX_MGMT_CFG80211_AUTH_TO;
Jiri Bencf0706e82007-05-05 11:45:53 -07001061 }
1062
Johannes Berg77fdaa12009-07-07 03:45:17 +02001063 printk(KERN_DEBUG "%s: authenticate with AP %pM (try %d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001064 sdata->name, wk->bss->cbss.bssid, wk->tries);
Jiri Bencf0706e82007-05-05 11:45:53 -07001065
Johannes Berg77fdaa12009-07-07 03:45:17 +02001066 ieee80211_send_auth(sdata, 1, wk->auth_alg, wk->ie, wk->ie_len,
Johannes Bergfffd0932009-07-08 14:22:54 +02001067 wk->bss->cbss.bssid, NULL, 0, 0);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001068 wk->auth_transaction = 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07001069
Johannes Berg77fdaa12009-07-07 03:45:17 +02001070 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Bergca386f32009-07-10 02:39:48 +02001071 run_again(ifmgd, wk->timeout);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001072
1073 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001074}
1075
Johannes Berge21546a2009-08-06 20:41:32 +02001076static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1077 bool deauth)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001078{
Johannes Berg46900292009-02-15 12:44:28 +01001079 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001080 struct ieee80211_local *local = sdata->local;
1081 struct sta_info *sta;
Kalle Valoe0cb6862008-12-18 23:35:13 +02001082 u32 changed = 0, config_changed = 0;
Johannes Bergb291ba12009-07-10 15:29:03 +02001083 u8 bssid[ETH_ALEN];
Tomas Winkleraa458d12008-09-09 00:32:12 +03001084
Johannes Berg77fdaa12009-07-07 03:45:17 +02001085 ASSERT_MGD_MTX(ifmgd);
1086
Johannes Bergb291ba12009-07-10 15:29:03 +02001087 if (WARN_ON(!ifmgd->associated))
1088 return;
1089
1090 memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
1091
Johannes Berg77fdaa12009-07-07 03:45:17 +02001092 ifmgd->associated = NULL;
1093 memset(ifmgd->bssid, 0, ETH_ALEN);
1094
Johannes Berge21546a2009-08-06 20:41:32 +02001095 if (deauth) {
1096 kfree(ifmgd->old_associate_work);
1097 ifmgd->old_associate_work = NULL;
1098 } else {
1099 struct ieee80211_mgd_work *wk = ifmgd->old_associate_work;
1100
1101 wk->state = IEEE80211_MGD_STATE_IDLE;
1102 list_add(&wk->list, &ifmgd->work_list);
1103 }
1104
Johannes Berg77fdaa12009-07-07 03:45:17 +02001105 /*
1106 * we need to commit the associated = NULL change because the
1107 * scan code uses that to determine whether this iface should
1108 * go to/wake up from powersave or not -- and could otherwise
1109 * wake the queues erroneously.
1110 */
1111 smp_mb();
1112
1113 /*
1114 * Thus, we can only afterwards stop the queues -- to account
1115 * for the case where another CPU is finishing a scan at this
1116 * time -- we don't want the scan code to enable queues.
1117 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001118
John W. Linville53623f12009-10-15 15:10:16 -04001119 netif_stop_queue(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001120 netif_carrier_off(sdata->dev);
1121
Johannes Berg1fa6f4a2009-06-15 18:13:58 +02001122 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +01001123 sta = sta_info_get(sdata, bssid);
Johannes Berg1fa6f4a2009-06-15 18:13:58 +02001124 if (sta)
1125 ieee80211_sta_tear_down_BA_sessions(sta);
1126 rcu_read_unlock();
Tomas Winkleraa458d12008-09-09 00:32:12 +03001127
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001128 changed |= ieee80211_reset_erp_info(sdata);
1129
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001130 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001131 changed |= BSS_CHANGED_ASSOC;
1132 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001133
Johannes Bergaa837e12009-05-07 16:16:24 +02001134 ieee80211_set_wmm_default(sdata);
1135
Johannes Berg5cff20e2009-04-29 12:26:17 +02001136 ieee80211_recalc_idle(local);
1137
Johannes Berg47979382009-01-07 10:13:27 +01001138 /* channel(_type) changes are handled by ieee80211_hw_config */
Sujith094d05d2008-12-12 11:57:43 +05301139 local->oper_channel_type = NL80211_CHAN_NO_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001140
Johannes Berg413ad502009-05-08 21:21:06 +02001141 /* on the next assoc, re-program HT parameters */
1142 sdata->ht_opmode_valid = false;
1143
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301144 local->power_constr_level = 0;
1145
Kalle Valo520eb822008-12-18 23:35:27 +02001146 del_timer_sync(&local->dynamic_ps_timer);
1147 cancel_work_sync(&local->dynamic_ps_enable_work);
1148
Kalle Valoe0cb6862008-12-18 23:35:13 +02001149 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1150 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1151 config_changed |= IEEE80211_CONF_CHANGE_PS;
1152 }
1153
1154 ieee80211_hw_config(local, config_changed);
Johannes Berg9cef8732009-05-14 13:10:14 +02001155
1156 /* And the BSSID changed -- not very interesting here */
1157 changed |= BSS_CHANGED_BSSID;
Johannes Bergae5eb022008-10-14 16:58:37 +02001158 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001159
1160 rcu_read_lock();
1161
Johannes Bergabe60632009-11-25 17:46:18 +01001162 sta = sta_info_get(sdata, bssid);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001163 if (!sta) {
1164 rcu_read_unlock();
1165 return;
1166 }
1167
1168 sta_info_unlink(&sta);
1169
1170 rcu_read_unlock();
1171
1172 sta_info_destroy(sta);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001173}
Jiri Bencf0706e82007-05-05 11:45:53 -07001174
Johannes Berg77fdaa12009-07-07 03:45:17 +02001175static enum rx_mgmt_action __must_check
1176ieee80211_associate(struct ieee80211_sub_if_data *sdata,
1177 struct ieee80211_mgd_work *wk)
Jiri Bencf0706e82007-05-05 11:45:53 -07001178{
Johannes Berg46900292009-02-15 12:44:28 +01001179 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Helmut Schaa11432372009-03-12 14:04:34 +01001180 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +01001181
Johannes Berg77fdaa12009-07-07 03:45:17 +02001182 wk->tries++;
1183 if (wk->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Berg0c68ae262008-10-27 15:56:10 -07001184 printk(KERN_DEBUG "%s: association with AP %pM"
Jiri Bencf0706e82007-05-05 11:45:53 -07001185 " timed out\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001186 sdata->name, wk->bss->cbss.bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001187
1188 /*
1189 * Most likely AP is not in the range so remove the
1190 * bss struct for that AP.
1191 */
1192 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
1193
Helmut Schaa11432372009-03-12 14:04:34 +01001194 /*
1195 * We might have a pending scan which had no chance to run yet
Johannes Berg77fdaa12009-07-07 03:45:17 +02001196 * due to work needing to be done. Hence, queue the STAs work
1197 * again for that.
Helmut Schaa11432372009-03-12 14:04:34 +01001198 */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001199 ieee80211_queue_work(&local->hw, &ifmgd->work);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001200 return RX_MGMT_CFG80211_ASSOC_TO;
Jiri Bencf0706e82007-05-05 11:45:53 -07001201 }
1202
Johannes Berg77fdaa12009-07-07 03:45:17 +02001203 printk(KERN_DEBUG "%s: associate with AP %pM (try %d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001204 sdata->name, wk->bss->cbss.bssid, wk->tries);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001205 ieee80211_send_assoc(sdata, wk);
Jiri Bencf0706e82007-05-05 11:45:53 -07001206
Johannes Berg77fdaa12009-07-07 03:45:17 +02001207 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Bergca386f32009-07-10 02:39:48 +02001208 run_again(ifmgd, wk->timeout);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001209
1210 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001211}
1212
Kalle Valo3cf335d2009-03-22 21:57:06 +02001213void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1214 struct ieee80211_hdr *hdr)
1215{
1216 /*
1217 * We can postpone the mgd.timer whenever receiving unicast frames
1218 * from AP because we know that the connection is working both ways
1219 * at that time. But multicast frames (and hence also beacons) must
1220 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001221 * data idle periods for sending the periodic probe request to the
1222 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001223 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001224 if (is_multicast_ether_addr(hdr->addr1))
1225 return;
1226
1227 mod_timer(&sdata->u.mgd.conn_mon_timer,
1228 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Kalle Valo3cf335d2009-03-22 21:57:06 +02001229}
Jiri Bencf0706e82007-05-05 11:45:53 -07001230
Maxim Levitskya43abf22009-07-31 18:54:12 +03001231static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1232{
1233 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1234 const u8 *ssid;
1235
1236 ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
1237 ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
1238 ssid + 2, ssid[1], NULL, 0);
1239
1240 ifmgd->probe_send_count++;
1241 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1242 run_again(ifmgd, ifmgd->probe_timeout);
1243}
1244
Johannes Bergb291ba12009-07-10 15:29:03 +02001245static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1246 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001247{
Kalle Valo04de8382009-03-22 21:57:35 +02001248 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001249 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001250
Johannes Berg0e2b6282009-07-13 13:23:39 +02001251 if (!netif_running(sdata->dev))
1252 return;
1253
Luis R. Rodriguez91a3bd72009-07-23 16:37:47 -07001254 if (sdata->local->scanning)
1255 return;
1256
Johannes Berg77fdaa12009-07-07 03:45:17 +02001257 mutex_lock(&ifmgd->mtx);
1258
1259 if (!ifmgd->associated)
1260 goto out;
1261
Michael Buescha8604022009-04-15 14:41:22 -04001262#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergb291ba12009-07-10 15:29:03 +02001263 if (beacon && net_ratelimit())
1264 printk(KERN_DEBUG "%s: detected beacon loss from AP "
Johannes Berg47846c92009-11-25 17:46:19 +01001265 "- sending probe request\n", sdata->name);
Michael Buescha8604022009-04-15 14:41:22 -04001266#endif
Kalle Valo04de8382009-03-22 21:57:35 +02001267
Johannes Bergb291ba12009-07-10 15:29:03 +02001268 /*
1269 * The driver/our work has already reported this event or the
1270 * connection monitoring has kicked in and we have already sent
1271 * a probe request. Or maybe the AP died and the driver keeps
1272 * reporting until we disassociate...
1273 *
1274 * In either case we have to ignore the current call to this
1275 * function (except for setting the correct probe reason bit)
1276 * because otherwise we would reset the timer every time and
1277 * never check whether we received a probe response!
1278 */
1279 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1280 IEEE80211_STA_CONNECTION_POLL))
1281 already = true;
1282
1283 if (beacon)
1284 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1285 else
1286 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1287
1288 if (already)
1289 goto out;
1290
Johannes Berg4e751842009-06-10 15:16:52 +02001291 mutex_lock(&sdata->local->iflist_mtx);
1292 ieee80211_recalc_ps(sdata->local, -1);
1293 mutex_unlock(&sdata->local->iflist_mtx);
1294
Maxim Levitskya43abf22009-07-31 18:54:12 +03001295 ifmgd->probe_send_count = 0;
1296 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001297 out:
1298 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001299}
1300
Johannes Bergb291ba12009-07-10 15:29:03 +02001301void ieee80211_beacon_loss_work(struct work_struct *work)
1302{
1303 struct ieee80211_sub_if_data *sdata =
1304 container_of(work, struct ieee80211_sub_if_data,
1305 u.mgd.beacon_loss_work);
1306
1307 ieee80211_mgd_probe_ap(sdata, true);
1308}
1309
Kalle Valo04de8382009-03-22 21:57:35 +02001310void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1311{
1312 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1313
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001314 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001315}
1316EXPORT_SYMBOL(ieee80211_beacon_loss);
1317
Johannes Berg77fdaa12009-07-07 03:45:17 +02001318static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1319 struct ieee80211_mgd_work *wk)
Jiri Bencf0706e82007-05-05 11:45:53 -07001320{
Johannes Berg77fdaa12009-07-07 03:45:17 +02001321 wk->state = IEEE80211_MGD_STATE_IDLE;
Johannes Berg47846c92009-11-25 17:46:19 +01001322 printk(KERN_DEBUG "%s: authenticated\n", sdata->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001323}
1324
1325
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001326static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001327 struct ieee80211_mgd_work *wk,
Jiri Bencf0706e82007-05-05 11:45:53 -07001328 struct ieee80211_mgmt *mgmt,
1329 size_t len)
1330{
1331 u8 *pos;
1332 struct ieee802_11_elems elems;
1333
Jiri Bencf0706e82007-05-05 11:45:53 -07001334 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001335 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001336 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001337 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001338 ieee80211_send_auth(sdata, 3, wk->auth_alg,
Johannes Berg46900292009-02-15 12:44:28 +01001339 elems.challenge - 2, elems.challenge_len + 2,
Johannes Bergfffd0932009-07-08 14:22:54 +02001340 wk->bss->cbss.bssid,
1341 wk->key, wk->key_len, wk->key_idx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001342 wk->auth_transaction = 4;
Johannes Bergfe3d2c32009-02-10 21:26:03 +01001343}
1344
Johannes Berg77fdaa12009-07-07 03:45:17 +02001345static enum rx_mgmt_action __must_check
1346ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1347 struct ieee80211_mgd_work *wk,
1348 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001349{
Jiri Bencf0706e82007-05-05 11:45:53 -07001350 u16 auth_alg, auth_transaction, status_code;
1351
Johannes Berg77fdaa12009-07-07 03:45:17 +02001352 if (wk->state != IEEE80211_MGD_STATE_AUTH)
1353 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001354
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001355 if (len < 24 + 6)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001356 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001357
Johannes Berg77fdaa12009-07-07 03:45:17 +02001358 if (memcmp(wk->bss->cbss.bssid, mgmt->sa, ETH_ALEN) != 0)
1359 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001360
Johannes Berg77fdaa12009-07-07 03:45:17 +02001361 if (memcmp(wk->bss->cbss.bssid, mgmt->bssid, ETH_ALEN) != 0)
1362 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001363
1364 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1365 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1366 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1367
Johannes Berg77fdaa12009-07-07 03:45:17 +02001368 if (auth_alg != wk->auth_alg ||
1369 auth_transaction != wk->auth_transaction)
1370 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001371
1372 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001373 list_del(&wk->list);
1374 kfree(wk);
1375 return RX_MGMT_CFG80211_AUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001376 }
1377
Johannes Berg77fdaa12009-07-07 03:45:17 +02001378 switch (wk->auth_alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001379 case WLAN_AUTH_OPEN:
1380 case WLAN_AUTH_LEAP:
Jouni Malinen636a5d32009-03-19 13:39:22 +02001381 case WLAN_AUTH_FT:
Johannes Berg77fdaa12009-07-07 03:45:17 +02001382 ieee80211_auth_completed(sdata, wk);
1383 return RX_MGMT_CFG80211_AUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001384 case WLAN_AUTH_SHARED_KEY:
Johannes Berg77fdaa12009-07-07 03:45:17 +02001385 if (wk->auth_transaction == 4) {
1386 ieee80211_auth_completed(sdata, wk);
1387 return RX_MGMT_CFG80211_AUTH;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02001388 } else
Johannes Berg77fdaa12009-07-07 03:45:17 +02001389 ieee80211_auth_challenge(sdata, wk, mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001390 break;
1391 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02001392
1393 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001394}
1395
1396
Johannes Berg77fdaa12009-07-07 03:45:17 +02001397static enum rx_mgmt_action __must_check
1398ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1399 struct ieee80211_mgd_work *wk,
1400 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001401{
Johannes Berg46900292009-02-15 12:44:28 +01001402 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001403 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001404 u16 reason_code;
1405
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001406 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001407 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001408
Johannes Berg77fdaa12009-07-07 03:45:17 +02001409 ASSERT_MGD_MTX(ifmgd);
1410
1411 if (wk)
1412 bssid = wk->bss->cbss.bssid;
1413 else
1414 bssid = ifmgd->associated->cbss.bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001415
1416 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1417
Johannes Berg77fdaa12009-07-07 03:45:17 +02001418 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001419 sdata->name, bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001420
Johannes Berg77fdaa12009-07-07 03:45:17 +02001421 if (!wk) {
Johannes Berge21546a2009-08-06 20:41:32 +02001422 ieee80211_set_disassoc(sdata, true);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001423 } else {
1424 list_del(&wk->list);
1425 kfree(wk);
1426 }
1427
1428 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001429}
1430
1431
Johannes Berg77fdaa12009-07-07 03:45:17 +02001432static enum rx_mgmt_action __must_check
1433ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1434 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001435{
Johannes Berg46900292009-02-15 12:44:28 +01001436 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001437 u16 reason_code;
1438
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001439 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001440 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001441
Johannes Berg77fdaa12009-07-07 03:45:17 +02001442 ASSERT_MGD_MTX(ifmgd);
1443
1444 if (WARN_ON(!ifmgd->associated))
1445 return RX_MGMT_NONE;
1446
1447 if (WARN_ON(memcmp(ifmgd->associated->cbss.bssid, mgmt->sa, ETH_ALEN)))
1448 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001449
1450 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1451
Johannes Berg0ff71612009-09-26 14:45:41 +02001452 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001453 sdata->name, mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001454
Johannes Berge21546a2009-08-06 20:41:32 +02001455 ieee80211_set_disassoc(sdata, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001456 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001457}
1458
1459
Johannes Berg77fdaa12009-07-07 03:45:17 +02001460static enum rx_mgmt_action __must_check
1461ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1462 struct ieee80211_mgd_work *wk,
1463 struct ieee80211_mgmt *mgmt, size_t len,
1464 bool reassoc)
Jiri Bencf0706e82007-05-05 11:45:53 -07001465{
Johannes Berg46900292009-02-15 12:44:28 +01001466 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001467 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001468 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001469 struct sta_info *sta;
Johannes Berg881d9482009-01-21 15:13:48 +01001470 u32 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001471 u16 capab_info, status_code, aid;
1472 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02001473 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001474 u8 *pos;
Johannes Bergae5eb022008-10-14 16:58:37 +02001475 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001476 int i, j;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001477 bool have_higher_than_11mbit = false, newsta = false;
Johannes Bergae5eb022008-10-14 16:58:37 +02001478 u16 ap_ht_cap_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07001479
Johannes Berg77fdaa12009-07-07 03:45:17 +02001480 /*
1481 * AssocResp and ReassocResp have identical structure, so process both
1482 * of them in this function.
1483 */
Jiri Bencf0706e82007-05-05 11:45:53 -07001484
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001485 if (len < 24 + 6)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001486 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001487
Johannes Berg77fdaa12009-07-07 03:45:17 +02001488 if (memcmp(wk->bss->cbss.bssid, mgmt->sa, ETH_ALEN) != 0)
1489 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001490
1491 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1492 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1493 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001494
Johannes Berg0c68ae262008-10-27 15:56:10 -07001495 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001496 "status=%d aid=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001497 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
Johannes Bergddd68582007-10-22 14:51:37 +02001498 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001499
Jouni Malinen63a5ab82009-01-08 13:32:09 +02001500 pos = mgmt->u.assoc_resp.variable;
1501 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1502
1503 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Jouni Malinenf797eb72009-01-19 18:48:46 +02001504 elems.timeout_int && elems.timeout_int_len == 5 &&
1505 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Jouni Malinen63a5ab82009-01-08 13:32:09 +02001506 u32 tu, ms;
Jouni Malinenf797eb72009-01-19 18:48:46 +02001507 tu = get_unaligned_le32(elems.timeout_int + 1);
Jouni Malinen63a5ab82009-01-08 13:32:09 +02001508 ms = tu * 1024 / 1000;
1509 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
1510 "comeback duration %u TU (%u ms)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001511 sdata->name, tu, ms);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001512 wk->timeout = jiffies + msecs_to_jiffies(ms);
Jouni Malinen63a5ab82009-01-08 13:32:09 +02001513 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Bergca386f32009-07-10 02:39:48 +02001514 run_again(ifmgd, jiffies + msecs_to_jiffies(ms));
Johannes Berg77fdaa12009-07-07 03:45:17 +02001515 return RX_MGMT_NONE;
Jouni Malinen63a5ab82009-01-08 13:32:09 +02001516 }
1517
Jiri Bencf0706e82007-05-05 11:45:53 -07001518 if (status_code != WLAN_STATUS_SUCCESS) {
1519 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001520 sdata->name, status_code);
Johannes Berg2ef6e442009-10-20 15:08:12 +09001521 wk->state = IEEE80211_MGD_STATE_IDLE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001522 return RX_MGMT_CFG80211_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001523 }
1524
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001525 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1526 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Johannes Berg47846c92009-11-25 17:46:19 +01001527 "set\n", sdata->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001528 aid &= ~(BIT(15) | BIT(14));
1529
Jiri Bencf0706e82007-05-05 11:45:53 -07001530 if (!elems.supp_rates) {
1531 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001532 sdata->name);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001533 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001534 }
1535
Johannes Berg47846c92009-11-25 17:46:19 +01001536 printk(KERN_DEBUG "%s: associated\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +01001537 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001538
Johannes Bergd0709a62008-02-25 16:27:46 +01001539 rcu_read_lock();
1540
Jiri Bencf0706e82007-05-05 11:45:53 -07001541 /* Add STA entry for the AP */
Johannes Bergabe60632009-11-25 17:46:18 +01001542 sta = sta_info_get(sdata, wk->bss->cbss.bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001543 if (!sta) {
Johannes Bergddf4ac52008-10-22 11:41:38 +02001544 newsta = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001545
Johannes Berg77fdaa12009-07-07 03:45:17 +02001546 rcu_read_unlock();
1547
1548 sta = sta_info_alloc(sdata, wk->bss->cbss.bssid, GFP_KERNEL);
Johannes Berg73651ee2008-02-25 16:27:47 +01001549 if (!sta) {
1550 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
Johannes Berg47846c92009-11-25 17:46:19 +01001551 " the AP\n", sdata->name);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001552 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001553 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001554
Johannes Berg77fdaa12009-07-07 03:45:17 +02001555 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1556 WLAN_STA_ASSOC_AP);
1557 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1558 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1559
1560 rcu_read_lock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001561 }
1562
Jiri Bencf0706e82007-05-05 11:45:53 -07001563 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001564 basic_rates = 0;
1565 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1566
Jiri Bencf0706e82007-05-05 11:45:53 -07001567 for (i = 0; i < elems.supp_rates_len; i++) {
1568 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Tomas Winklerd61272c2008-10-30 17:08:08 +02001569 bool is_basic = !!(elems.supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001570
1571 if (rate > 110)
1572 have_higher_than_11mbit = true;
1573
1574 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001575 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001576 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001577 if (is_basic)
1578 basic_rates |= BIT(j);
1579 break;
1580 }
Johannes Berg8318d782008-01-24 19:38:38 +01001581 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001582 }
Johannes Berg8318d782008-01-24 19:38:38 +01001583
Jiri Bencf0706e82007-05-05 11:45:53 -07001584 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1585 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg7e0986c2009-04-19 13:22:11 +02001586 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001587
1588 if (rate > 110)
1589 have_higher_than_11mbit = true;
1590
1591 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001592 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001593 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001594 if (is_basic)
1595 basic_rates |= BIT(j);
1596 break;
1597 }
Johannes Berg8318d782008-01-24 19:38:38 +01001598 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001599 }
Johannes Berg8318d782008-01-24 19:38:38 +01001600
Johannes Berg323ce792008-09-11 02:45:11 +02001601 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
Johannes Bergbda39332008-10-11 01:51:51 +02001602 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01001603
1604 /* cf. IEEE 802.11 9.2.12 */
1605 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1606 have_higher_than_11mbit)
1607 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1608 else
1609 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001610
Johannes Bergab1faea2009-07-01 21:41:17 +02001611 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001612 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001613 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02001614
1615 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001616
Johannes Berg4b7679a2008-09-18 18:14:18 +02001617 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001618
Johannes Berg46900292009-02-15 12:44:28 +01001619 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Jouni Malinen5394af42009-01-08 13:31:59 +02001620 set_sta_flags(sta, WLAN_STA_MFP);
1621
Johannes Bergddf4ac52008-10-22 11:41:38 +02001622 if (elems.wmm_param)
Johannes Berg07346f812008-05-03 01:02:02 +02001623 set_sta_flags(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02001624
1625 if (newsta) {
1626 int err = sta_info_insert(sta);
1627 if (err) {
1628 printk(KERN_DEBUG "%s: failed to insert STA entry for"
Johannes Berg47846c92009-11-25 17:46:19 +01001629 " the AP (error %d)\n", sdata->name, err);
Johannes Bergddf4ac52008-10-22 11:41:38 +02001630 rcu_read_unlock();
Johannes Berg77fdaa12009-07-07 03:45:17 +02001631 return RX_MGMT_NONE;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001632 }
1633 }
1634
1635 rcu_read_unlock();
1636
1637 if (elems.wmm_param)
Johannes Berg46900292009-02-15 12:44:28 +01001638 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07001639 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02001640 else
1641 ieee80211_set_wmm_default(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07001642
Johannes Bergae5eb022008-10-14 16:58:37 +02001643 if (elems.ht_info_elem && elems.wmm_param &&
Johannes Berg46900292009-02-15 12:44:28 +01001644 (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001645 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001646 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001647 wk->bss->cbss.bssid,
Johannes Bergae5eb022008-10-14 16:58:37 +02001648 ap_ht_cap_flags);
1649
Johannes Berg056508d2009-07-30 21:43:55 +02001650 /* delete work item -- must be before set_associated for PS */
1651 list_del(&wk->list);
1652
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001653 /* set AID and assoc capability,
1654 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01001655 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001656 bss_conf->assoc_capability = capab_info;
Johannes Berge21546a2009-08-06 20:41:32 +02001657 /* this will take ownership of wk */
1658 ieee80211_set_associated(sdata, wk, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001659
Kalle Valo15b7b062009-03-22 21:57:14 +02001660 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02001661 * Start timer to probe the connection to the AP now.
1662 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02001663 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001664 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
1665 mod_beacon_timer(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02001666
Johannes Berg77fdaa12009-07-07 03:45:17 +02001667 return RX_MGMT_CFG80211_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001668}
1669
1670
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001671static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001672 struct ieee80211_mgmt *mgmt,
1673 size_t len,
1674 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001675 struct ieee802_11_elems *elems,
1676 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07001677{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001678 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001679 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02001680 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01001681 struct ieee80211_channel *channel;
Jiri Bencf0706e82007-05-05 11:45:53 -07001682
Johannes Berg5bda6172008-09-08 11:05:10 +02001683 if (elems->ds_params && elems->ds_params_len == 1)
1684 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1685 else
1686 freq = rx_status->freq;
1687
1688 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1689
1690 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1691 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001692
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001693 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01001694 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001695 if (bss)
1696 ieee80211_rx_bss_put(local, bss);
1697
1698 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001699 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001700
Sujithc481ec92009-01-06 09:28:37 +05301701 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg77fdaa12009-07-07 03:45:17 +02001702 (memcmp(mgmt->bssid, sdata->u.mgd.associated->cbss.bssid,
1703 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05301704 struct ieee80211_channel_sw_ie *sw_elem =
1705 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001706 ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
Sujithc481ec92009-01-06 09:28:37 +05301707 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001708}
1709
1710
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001711static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001712 struct ieee80211_mgd_work *wk,
1713 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07001714 struct ieee80211_rx_status *rx_status)
1715{
Kalle Valo15b7b062009-03-22 21:57:14 +02001716 struct ieee80211_if_managed *ifmgd;
Ester Kummerae6a44e2008-06-27 18:54:48 +03001717 size_t baselen;
1718 struct ieee802_11_elems elems;
1719
Kalle Valo15b7b062009-03-22 21:57:14 +02001720 ifmgd = &sdata->u.mgd;
1721
Johannes Berg77fdaa12009-07-07 03:45:17 +02001722 ASSERT_MGD_MTX(ifmgd);
1723
Johannes Berg47846c92009-11-25 17:46:19 +01001724 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03001725 return; /* ignore ProbeResp to foreign address */
1726
Ester Kummerae6a44e2008-06-27 18:54:48 +03001727 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1728 if (baselen > len)
1729 return;
1730
1731 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1732 &elems);
1733
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001734 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001735
1736 /* direct probe may be part of the association flow */
Johannes Berg77fdaa12009-07-07 03:45:17 +02001737 if (wk && wk->state == IEEE80211_MGD_STATE_PROBE) {
Johannes Berg0ff71612009-09-26 14:45:41 +02001738 printk(KERN_DEBUG "%s: direct probe responded\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001739 sdata->name);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001740 wk->tries = 0;
1741 wk->state = IEEE80211_MGD_STATE_AUTH;
1742 WARN_ON(ieee80211_authenticate(sdata, wk) != RX_MGMT_NONE);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001743 }
Kalle Valo15b7b062009-03-22 21:57:14 +02001744
Johannes Berg77fdaa12009-07-07 03:45:17 +02001745 if (ifmgd->associated &&
1746 memcmp(mgmt->bssid, ifmgd->associated->cbss.bssid, ETH_ALEN) == 0 &&
Johannes Bergb291ba12009-07-10 15:29:03 +02001747 ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1748 IEEE80211_STA_CONNECTION_POLL)) {
1749 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1750 IEEE80211_STA_BEACON_POLL);
Johannes Berg4e751842009-06-10 15:16:52 +02001751 mutex_lock(&sdata->local->iflist_mtx);
1752 ieee80211_recalc_ps(sdata->local, -1);
1753 mutex_unlock(&sdata->local->iflist_mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02001754 /*
1755 * We've received a probe response, but are not sure whether
1756 * we have or will be receiving any beacons or data, so let's
1757 * schedule the timers again, just in case.
1758 */
1759 mod_beacon_timer(sdata);
1760 mod_timer(&ifmgd->conn_mon_timer,
1761 round_jiffies_up(jiffies +
1762 IEEE80211_CONNECTION_IDLE_TIME));
Johannes Berg4e751842009-06-10 15:16:52 +02001763 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001764}
1765
Johannes Bergd91f36d2009-04-16 13:17:26 +02001766/*
1767 * This is the canonical list of information elements we care about,
1768 * the filter code also gives us all changes to the Microsoft OUI
1769 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1770 *
1771 * We implement beacon filtering in software since that means we can
1772 * avoid processing the frame here and in cfg80211, and userspace
1773 * will not be able to tell whether the hardware supports it or not.
1774 *
1775 * XXX: This list needs to be dynamic -- userspace needs to be able to
1776 * add items it requires. It also needs to be able to tell us to
1777 * look out for other vendor IEs.
1778 */
1779static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02001780 (1ULL << WLAN_EID_COUNTRY) |
1781 (1ULL << WLAN_EID_ERP_INFO) |
1782 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1783 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1784 (1ULL << WLAN_EID_HT_CAPABILITY) |
1785 (1ULL << WLAN_EID_HT_INFORMATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001786
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001787static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001788 struct ieee80211_mgmt *mgmt,
1789 size_t len,
1790 struct ieee80211_rx_status *rx_status)
1791{
Johannes Bergd91f36d2009-04-16 13:17:26 +02001792 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001793 size_t baselen;
1794 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001795 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001796 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001797 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001798 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001799 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001800 u8 *bssid;
1801
1802 ASSERT_MGD_MTX(ifmgd);
Jiri Bencf0706e82007-05-05 11:45:53 -07001803
Ester Kummerae6a44e2008-06-27 18:54:48 +03001804 /* Process beacon from the current BSS */
1805 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1806 if (baselen > len)
1807 return;
1808
Johannes Bergd91f36d2009-04-16 13:17:26 +02001809 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07001810 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001811
Johannes Bergb291ba12009-07-10 15:29:03 +02001812 /*
1813 * We might have received a number of frames, among them a
1814 * disassoc frame and a beacon...
1815 */
1816 if (!ifmgd->associated)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001817 return;
1818
1819 bssid = ifmgd->associated->cbss.bssid;
1820
Johannes Bergb291ba12009-07-10 15:29:03 +02001821 /*
1822 * And in theory even frames from a different AP we were just
1823 * associated to a split-second ago!
1824 */
1825 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001826 return;
1827
Johannes Bergb291ba12009-07-10 15:29:03 +02001828 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Jouni Malinen92778182009-05-14 21:15:36 +03001829#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1830 if (net_ratelimit()) {
1831 printk(KERN_DEBUG "%s: cancelling probereq poll due "
Johannes Berg47846c92009-11-25 17:46:19 +01001832 "to a received beacon\n", sdata->name);
Jouni Malinen92778182009-05-14 21:15:36 +03001833 }
1834#endif
Johannes Bergb291ba12009-07-10 15:29:03 +02001835 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Johannes Berg4e751842009-06-10 15:16:52 +02001836 mutex_lock(&local->iflist_mtx);
1837 ieee80211_recalc_ps(local, -1);
1838 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03001839 }
1840
Johannes Bergb291ba12009-07-10 15:29:03 +02001841 /*
1842 * Push the beacon loss detection into the future since
1843 * we are processing a beacon from the AP just now.
1844 */
1845 mod_beacon_timer(sdata);
1846
Johannes Bergd91f36d2009-04-16 13:17:26 +02001847 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1848 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1849 len - baselen, &elems,
1850 care_about_ies, ncrc);
1851
1852 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02001853 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1854 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001855
Jouni Malinen30196672009-05-19 17:01:43 +03001856 if (ncrc != ifmgd->beacon_crc) {
1857 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1858 true);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001859
Jouni Malinen30196672009-05-19 17:01:43 +03001860 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1861 elems.wmm_param_len);
1862 }
Johannes Bergfe3fa822008-09-08 11:05:09 +02001863
Vivek Natarajan25c9c872009-03-02 20:20:30 +05301864 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02001865 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02001866 if (local->hw.conf.dynamic_ps_timeout > 0) {
1867 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1868 ieee80211_hw_config(local,
1869 IEEE80211_CONF_CHANGE_PS);
1870 ieee80211_send_nullfunc(local, sdata, 0);
1871 } else {
1872 local->pspolling = true;
1873
1874 /*
1875 * Here is assumed that the driver will be
1876 * able to send ps-poll frame and receive a
1877 * response even though power save mode is
1878 * enabled, but some drivers might require
1879 * to disable power save here. This needs
1880 * to be investigated.
1881 */
1882 ieee80211_send_pspoll(local, sdata);
1883 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08001884 }
1885 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001886
Jouni Malinen30196672009-05-19 17:01:43 +03001887 if (ncrc == ifmgd->beacon_crc)
1888 return;
1889 ifmgd->beacon_crc = ncrc;
1890
Johannes Berg7a5158e2008-10-08 10:59:33 +02001891 if (elems.erp_info && elems.erp_info_len >= 1) {
1892 erp_valid = true;
1893 erp_value = elems.erp_info[0];
1894 } else {
1895 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04001896 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001897 changed |= ieee80211_handle_bss_capability(sdata,
1898 le16_to_cpu(mgmt->u.beacon.capab_info),
1899 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07001900
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001901
Vasanthakumar Thiagarajan53d6f81c2009-02-11 22:18:49 +05301902 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001903 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001904 struct sta_info *sta;
1905 struct ieee80211_supported_band *sband;
1906 u16 ap_ht_cap_flags;
1907
1908 rcu_read_lock();
1909
Johannes Bergabe60632009-11-25 17:46:18 +01001910 sta = sta_info_get(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001911 if (WARN_ON(!sta)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001912 rcu_read_unlock();
1913 return;
1914 }
1915
1916 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1917
1918 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1919 elems.ht_cap_elem, &sta->sta.ht_cap);
1920
1921 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1922
1923 rcu_read_unlock();
1924
1925 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001926 bssid, ap_ht_cap_flags);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001927 }
1928
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001929 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001930 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301931 /* TODO: IBSS also needs this */
1932 if (elems.pwr_constr_elem)
1933 ieee80211_handle_pwr_constr(sdata,
1934 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1935 elems.pwr_constr_elem,
1936 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001937 }
1938
Johannes Berg471b3ef2007-12-28 14:32:58 +01001939 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001940}
1941
Johannes Berg46900292009-02-15 12:44:28 +01001942ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
Johannes Bergf1d58c22009-06-17 13:13:00 +02001943 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001944{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001945 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001946 struct ieee80211_mgmt *mgmt;
1947 u16 fc;
1948
1949 if (skb->len < 24)
Johannes Berg46900292009-02-15 12:44:28 +01001950 return RX_DROP_MONITOR;
Jiri Bencf0706e82007-05-05 11:45:53 -07001951
1952 mgmt = (struct ieee80211_mgmt *) skb->data;
1953 fc = le16_to_cpu(mgmt->frame_control);
1954
1955 switch (fc & IEEE80211_FCTL_STYPE) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001956 case IEEE80211_STYPE_PROBE_RESP:
1957 case IEEE80211_STYPE_BEACON:
Jiri Bencf0706e82007-05-05 11:45:53 -07001958 case IEEE80211_STYPE_AUTH:
1959 case IEEE80211_STYPE_ASSOC_RESP:
1960 case IEEE80211_STYPE_REASSOC_RESP:
1961 case IEEE80211_STYPE_DEAUTH:
1962 case IEEE80211_STYPE_DISASSOC:
Johannes Berg77fdaa12009-07-07 03:45:17 +02001963 case IEEE80211_STYPE_ACTION:
Johannes Berg46900292009-02-15 12:44:28 +01001964 skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001965 ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
Johannes Berg46900292009-02-15 12:44:28 +01001966 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001967 }
1968
Johannes Berg46900292009-02-15 12:44:28 +01001969 return RX_DROP_MONITOR;
Jiri Bencf0706e82007-05-05 11:45:53 -07001970}
1971
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001972static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001973 struct sk_buff *skb)
1974{
Johannes Berg77fdaa12009-07-07 03:45:17 +02001975 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001976 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07001977 struct ieee80211_mgmt *mgmt;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001978 struct ieee80211_mgd_work *wk;
1979 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001980 u16 fc;
1981
Jiri Bencf0706e82007-05-05 11:45:53 -07001982 rx_status = (struct ieee80211_rx_status *) skb->cb;
1983 mgmt = (struct ieee80211_mgmt *) skb->data;
1984 fc = le16_to_cpu(mgmt->frame_control);
1985
Johannes Berg77fdaa12009-07-07 03:45:17 +02001986 mutex_lock(&ifmgd->mtx);
1987
1988 if (ifmgd->associated &&
1989 memcmp(ifmgd->associated->cbss.bssid, mgmt->bssid,
1990 ETH_ALEN) == 0) {
1991 switch (fc & IEEE80211_FCTL_STYPE) {
1992 case IEEE80211_STYPE_BEACON:
1993 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1994 rx_status);
1995 break;
1996 case IEEE80211_STYPE_PROBE_RESP:
1997 ieee80211_rx_mgmt_probe_resp(sdata, NULL, mgmt,
1998 skb->len, rx_status);
1999 break;
2000 case IEEE80211_STYPE_DEAUTH:
2001 rma = ieee80211_rx_mgmt_deauth(sdata, NULL,
2002 mgmt, skb->len);
2003 break;
2004 case IEEE80211_STYPE_DISASSOC:
2005 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2006 break;
2007 case IEEE80211_STYPE_ACTION:
2008 /* XXX: differentiate, can only happen for CSA now! */
2009 ieee80211_sta_process_chanswitch(sdata,
2010 &mgmt->u.action.u.chan_switch.sw_elem,
2011 ifmgd->associated);
2012 break;
2013 }
2014 mutex_unlock(&ifmgd->mtx);
2015
2016 switch (rma) {
2017 case RX_MGMT_NONE:
2018 /* no action */
2019 break;
2020 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002021 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002022 break;
2023 case RX_MGMT_CFG80211_DISASSOC:
Holger Schurigce470612009-10-13 13:28:13 +02002024 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002025 break;
2026 default:
2027 WARN(1, "unexpected: %d", rma);
2028 }
2029 goto out;
2030 }
2031
2032 list_for_each_entry(wk, &ifmgd->work_list, list) {
2033 if (memcmp(wk->bss->cbss.bssid, mgmt->bssid, ETH_ALEN) != 0)
2034 continue;
2035
2036 switch (fc & IEEE80211_FCTL_STYPE) {
2037 case IEEE80211_STYPE_PROBE_RESP:
2038 ieee80211_rx_mgmt_probe_resp(sdata, wk, mgmt, skb->len,
2039 rx_status);
2040 break;
2041 case IEEE80211_STYPE_AUTH:
2042 rma = ieee80211_rx_mgmt_auth(sdata, wk, mgmt, skb->len);
2043 break;
2044 case IEEE80211_STYPE_ASSOC_RESP:
2045 rma = ieee80211_rx_mgmt_assoc_resp(sdata, wk, mgmt,
2046 skb->len, false);
2047 break;
2048 case IEEE80211_STYPE_REASSOC_RESP:
2049 rma = ieee80211_rx_mgmt_assoc_resp(sdata, wk, mgmt,
2050 skb->len, true);
2051 break;
2052 case IEEE80211_STYPE_DEAUTH:
2053 rma = ieee80211_rx_mgmt_deauth(sdata, wk, mgmt,
2054 skb->len);
2055 break;
2056 }
2057 /*
2058 * We've processed this frame for that work, so it can't
2059 * belong to another work struct.
2060 * NB: this is also required for correctness because the
2061 * called functions can free 'wk', and for 'rma'!
2062 */
Johannes Berg46900292009-02-15 12:44:28 +01002063 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002064 }
2065
Johannes Berg77fdaa12009-07-07 03:45:17 +02002066 mutex_unlock(&ifmgd->mtx);
2067
2068 switch (rma) {
2069 case RX_MGMT_NONE:
2070 /* no action */
2071 break;
2072 case RX_MGMT_CFG80211_AUTH:
Johannes Bergcb0b4be2009-07-07 03:56:07 +02002073 cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002074 break;
2075 case RX_MGMT_CFG80211_ASSOC:
Johannes Bergcb0b4be2009-07-07 03:56:07 +02002076 cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002077 break;
Johannes Berg8d8b2612009-07-25 11:58:36 +02002078 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002079 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg8d8b2612009-07-25 11:58:36 +02002080 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002081 default:
2082 WARN(1, "unexpected: %d", rma);
2083 }
2084
2085 out:
Jiri Bencf0706e82007-05-05 11:45:53 -07002086 kfree_skb(skb);
2087}
2088
Johannes Berg9c6bd792008-09-11 00:01:52 +02002089static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002090{
2091 struct ieee80211_sub_if_data *sdata =
2092 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002093 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002094 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002095
Johannes Berg5bb644a2009-05-17 11:40:42 +02002096 if (local->quiescing) {
2097 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2098 return;
2099 }
2100
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002101 ieee80211_queue_work(&local->hw, &ifmgd->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002102}
2103
Johannes Berg9c6bd792008-09-11 00:01:52 +02002104static void ieee80211_sta_work(struct work_struct *work)
Johannes Berg60f8b392008-09-08 17:44:22 +02002105{
2106 struct ieee80211_sub_if_data *sdata =
Johannes Berg46900292009-02-15 12:44:28 +01002107 container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
Johannes Berg60f8b392008-09-08 17:44:22 +02002108 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +01002109 struct ieee80211_if_managed *ifmgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002110 struct sk_buff *skb;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002111 struct ieee80211_mgd_work *wk, *tmp;
2112 LIST_HEAD(free_work);
2113 enum rx_mgmt_action rma;
2114 bool anybusy = false;
Johannes Berg60f8b392008-09-08 17:44:22 +02002115
2116 if (!netif_running(sdata->dev))
2117 return;
2118
Helmut Schaafbe9c422009-07-23 12:14:04 +02002119 if (local->scanning)
Johannes Berg60f8b392008-09-08 17:44:22 +02002120 return;
2121
Johannes Berg46900292009-02-15 12:44:28 +01002122 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
Johannes Berg60f8b392008-09-08 17:44:22 +02002123 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +02002124
2125 /*
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002126 * ieee80211_queue_work() should have picked up most cases,
2127 * here we'll pick the the rest.
Johannes Berg5bb644a2009-05-17 11:40:42 +02002128 */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002129 if (WARN(local->suspended, "STA MLME work scheduled while "
2130 "going to suspend\n"))
Johannes Berg5bb644a2009-05-17 11:40:42 +02002131 return;
2132
Johannes Berg46900292009-02-15 12:44:28 +01002133 ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002134
Johannes Berg77fdaa12009-07-07 03:45:17 +02002135 /* first process frames to avoid timing out while a frame is pending */
Johannes Berg46900292009-02-15 12:44:28 +01002136 while ((skb = skb_dequeue(&ifmgd->skb_queue)))
Johannes Berg60f8b392008-09-08 17:44:22 +02002137 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2138
Johannes Berg77fdaa12009-07-07 03:45:17 +02002139 /* then process the rest of the work */
2140 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002141
Johannes Bergb291ba12009-07-10 15:29:03 +02002142 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2143 IEEE80211_STA_CONNECTION_POLL) &&
2144 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002145 u8 bssid[ETH_ALEN];
2146
2147 memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
Johannes Bergb291ba12009-07-10 15:29:03 +02002148 if (time_is_after_jiffies(ifmgd->probe_timeout))
2149 run_again(ifmgd, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002150
2151 else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
2152#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2153 printk(KERN_DEBUG "No probe response from AP %pM"
2154 " after %dms, try %d\n", bssid,
2155 (1000 * IEEE80211_PROBE_WAIT)/HZ,
2156 ifmgd->probe_send_count);
2157#endif
2158 ieee80211_mgd_probe_ap_send(sdata);
2159 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002160 /*
2161 * We actually lost the connection ... or did we?
2162 * Let's make sure!
2163 */
2164 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2165 IEEE80211_STA_BEACON_POLL);
Johannes Bergb291ba12009-07-10 15:29:03 +02002166 printk(KERN_DEBUG "No probe response from AP %pM"
2167 " after %dms, disconnecting.\n",
2168 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
Johannes Berge21546a2009-08-06 20:41:32 +02002169 ieee80211_set_disassoc(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02002170 mutex_unlock(&ifmgd->mtx);
2171 /*
2172 * must be outside lock due to cfg80211,
2173 * but that's not a problem.
2174 */
2175 ieee80211_send_deauth_disassoc(sdata, bssid,
2176 IEEE80211_STYPE_DEAUTH,
2177 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2178 NULL);
2179 mutex_lock(&ifmgd->mtx);
2180 }
2181 }
2182
Johannes Berg60f8b392008-09-08 17:44:22 +02002183
Johannes Berg5cff20e2009-04-29 12:26:17 +02002184 ieee80211_recalc_idle(local);
2185
Johannes Berg77fdaa12009-07-07 03:45:17 +02002186 list_for_each_entry_safe(wk, tmp, &ifmgd->work_list, list) {
Johannes Bergca386f32009-07-10 02:39:48 +02002187 if (time_is_after_jiffies(wk->timeout)) {
2188 /*
2189 * This work item isn't supposed to be worked on
2190 * right now, but take care to adjust the timer
2191 * properly.
2192 */
2193 run_again(ifmgd, wk->timeout);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002194 continue;
Johannes Bergca386f32009-07-10 02:39:48 +02002195 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002196
2197 switch (wk->state) {
2198 default:
2199 WARN_ON(1);
2200 /* fall through */
2201 case IEEE80211_MGD_STATE_IDLE:
2202 /* nothing */
2203 rma = RX_MGMT_NONE;
2204 break;
2205 case IEEE80211_MGD_STATE_PROBE:
2206 rma = ieee80211_direct_probe(sdata, wk);
2207 break;
2208 case IEEE80211_MGD_STATE_AUTH:
2209 rma = ieee80211_authenticate(sdata, wk);
2210 break;
2211 case IEEE80211_MGD_STATE_ASSOC:
2212 rma = ieee80211_associate(sdata, wk);
2213 break;
2214 }
2215
2216 switch (rma) {
2217 case RX_MGMT_NONE:
2218 /* no action required */
2219 break;
2220 case RX_MGMT_CFG80211_AUTH_TO:
2221 case RX_MGMT_CFG80211_ASSOC_TO:
2222 list_del(&wk->list);
2223 list_add(&wk->list, &free_work);
2224 wk->tries = rma; /* small abuse but only local */
2225 break;
2226 default:
2227 WARN(1, "unexpected: %d", rma);
2228 }
2229 }
2230
Jouni Malinen5bf6fcc2009-08-25 17:44:28 +03002231 list_for_each_entry(wk, &ifmgd->work_list, list) {
2232 if (wk->state != IEEE80211_MGD_STATE_IDLE) {
2233 anybusy = true;
2234 break;
2235 }
2236 }
2237 if (!anybusy &&
2238 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
2239 ieee80211_queue_delayed_work(&local->hw,
2240 &local->scan_work,
2241 round_jiffies_relative(0));
2242
Johannes Berg77fdaa12009-07-07 03:45:17 +02002243 mutex_unlock(&ifmgd->mtx);
2244
2245 list_for_each_entry_safe(wk, tmp, &free_work, list) {
2246 switch (wk->tries) {
2247 case RX_MGMT_CFG80211_AUTH_TO:
2248 cfg80211_send_auth_timeout(sdata->dev,
Johannes Bergcb0b4be2009-07-07 03:56:07 +02002249 wk->bss->cbss.bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002250 break;
2251 case RX_MGMT_CFG80211_ASSOC_TO:
Johannes Bergcb0b4be2009-07-07 03:56:07 +02002252 cfg80211_send_assoc_timeout(sdata->dev,
2253 wk->bss->cbss.bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002254 break;
2255 default:
2256 WARN(1, "unexpected: %d", wk->tries);
2257 }
2258
2259 list_del(&wk->list);
2260 kfree(wk);
2261 }
2262
2263 ieee80211_recalc_idle(local);
Johannes Berg60f8b392008-09-08 17:44:22 +02002264}
Johannes Berg0a51b272008-09-08 17:44:25 +02002265
Johannes Bergb291ba12009-07-10 15:29:03 +02002266static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2267{
2268 struct ieee80211_sub_if_data *sdata =
2269 (struct ieee80211_sub_if_data *) data;
2270 struct ieee80211_local *local = sdata->local;
2271
2272 if (local->quiescing)
2273 return;
2274
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002275 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002276}
2277
2278static void ieee80211_sta_conn_mon_timer(unsigned long data)
2279{
2280 struct ieee80211_sub_if_data *sdata =
2281 (struct ieee80211_sub_if_data *) data;
2282 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2283 struct ieee80211_local *local = sdata->local;
2284
2285 if (local->quiescing)
2286 return;
2287
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002288 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002289}
2290
2291static void ieee80211_sta_monitor_work(struct work_struct *work)
2292{
2293 struct ieee80211_sub_if_data *sdata =
2294 container_of(work, struct ieee80211_sub_if_data,
2295 u.mgd.monitor_work);
2296
2297 ieee80211_mgd_probe_ap(sdata, false);
2298}
2299
Johannes Berga1678f82008-09-11 00:01:47 +02002300static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2301{
Kalle Vaload935682009-04-19 08:47:19 +03002302 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Bergb291ba12009-07-10 15:29:03 +02002303 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2304 IEEE80211_STA_CONNECTION_POLL);
Kalle Vaload935682009-04-19 08:47:19 +03002305
Johannes Bergb291ba12009-07-10 15:29:03 +02002306 /* let's probe the connection once */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002307 ieee80211_queue_work(&sdata->local->hw,
Johannes Bergb291ba12009-07-10 15:29:03 +02002308 &sdata->u.mgd.monitor_work);
2309 /* and do all the other regular work too */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002310 ieee80211_queue_work(&sdata->local->hw,
Johannes Berg46900292009-02-15 12:44:28 +01002311 &sdata->u.mgd.work);
Kalle Vaload935682009-04-19 08:47:19 +03002312 }
Johannes Berga1678f82008-09-11 00:01:47 +02002313}
2314
Johannes Berg5bb644a2009-05-17 11:40:42 +02002315#ifdef CONFIG_PM
2316void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2317{
2318 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2319
2320 /*
2321 * we need to use atomic bitops for the running bits
2322 * only because both timers might fire at the same
2323 * time -- the code here is properly synchronised.
2324 */
2325
2326 cancel_work_sync(&ifmgd->work);
2327 cancel_work_sync(&ifmgd->beacon_loss_work);
2328 if (del_timer_sync(&ifmgd->timer))
2329 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2330
2331 cancel_work_sync(&ifmgd->chswitch_work);
2332 if (del_timer_sync(&ifmgd->chswitch_timer))
2333 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002334
2335 cancel_work_sync(&ifmgd->monitor_work);
2336 /* these will just be re-established on connection */
2337 del_timer_sync(&ifmgd->conn_mon_timer);
2338 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002339}
2340
2341void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2342{
2343 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2344
2345 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2346 add_timer(&ifmgd->timer);
2347 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2348 add_timer(&ifmgd->chswitch_timer);
2349}
2350#endif
2351
Johannes Berg9c6bd792008-09-11 00:01:52 +02002352/* interface setup */
2353void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2354{
Johannes Berg46900292009-02-15 12:44:28 +01002355 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002356
Johannes Berg46900292009-02-15 12:44:28 +01002357 ifmgd = &sdata->u.mgd;
2358 INIT_WORK(&ifmgd->work, ieee80211_sta_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002359 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01002360 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002361 INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
Johannes Berg46900292009-02-15 12:44:28 +01002362 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02002363 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002364 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2365 (unsigned long) sdata);
2366 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2367 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002368 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05302369 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002370 skb_queue_head_init(&ifmgd->skb_queue);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002371
Johannes Berg77fdaa12009-07-07 03:45:17 +02002372 INIT_LIST_HEAD(&ifmgd->work_list);
2373
Johannes Berg46900292009-02-15 12:44:28 +01002374 ifmgd->capab = WLAN_CAPABILITY_ESS;
Johannes Bergab1faea2009-07-01 21:41:17 +02002375 ifmgd->flags = 0;
Johannes Berg176be722009-03-12 23:49:28 +01002376 if (sdata->local->hw.queues >= 4)
Johannes Berg46900292009-02-15 12:44:28 +01002377 ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
Johannes Bergbbbdff92009-04-16 13:27:42 +02002378
Johannes Berg77fdaa12009-07-07 03:45:17 +02002379 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01002380
2381 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2382 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2383 else
2384 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002385}
2386
2387/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002388void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2389{
2390 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02002391
2392 /* Restart STA timers */
2393 rcu_read_lock();
2394 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2395 ieee80211_restart_sta_timer(sdata);
2396 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02002397}
Johannes Berg10f644a2009-04-16 13:17:25 +02002398
2399int ieee80211_max_network_latency(struct notifier_block *nb,
2400 unsigned long data, void *dummy)
2401{
2402 s32 latency_usec = (s32) data;
2403 struct ieee80211_local *local =
2404 container_of(nb, struct ieee80211_local,
2405 network_latency_notifier);
2406
2407 mutex_lock(&local->iflist_mtx);
2408 ieee80211_recalc_ps(local, latency_usec);
2409 mutex_unlock(&local->iflist_mtx);
2410
2411 return 0;
2412}
Johannes Berg77fdaa12009-07-07 03:45:17 +02002413
2414/* config hooks */
2415int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2416 struct cfg80211_auth_request *req)
2417{
2418 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2419 const u8 *ssid;
2420 struct ieee80211_mgd_work *wk;
2421 u16 auth_alg;
2422
2423 switch (req->auth_type) {
2424 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2425 auth_alg = WLAN_AUTH_OPEN;
2426 break;
2427 case NL80211_AUTHTYPE_SHARED_KEY:
2428 auth_alg = WLAN_AUTH_SHARED_KEY;
2429 break;
2430 case NL80211_AUTHTYPE_FT:
2431 auth_alg = WLAN_AUTH_FT;
2432 break;
2433 case NL80211_AUTHTYPE_NETWORK_EAP:
2434 auth_alg = WLAN_AUTH_LEAP;
2435 break;
2436 default:
2437 return -EOPNOTSUPP;
2438 }
2439
2440 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2441 if (!wk)
2442 return -ENOMEM;
2443
2444 wk->bss = (void *)req->bss;
2445
2446 if (req->ie && req->ie_len) {
2447 memcpy(wk->ie, req->ie, req->ie_len);
2448 wk->ie_len = req->ie_len;
2449 }
2450
Johannes Bergfffd0932009-07-08 14:22:54 +02002451 if (req->key && req->key_len) {
2452 wk->key_len = req->key_len;
2453 wk->key_idx = req->key_idx;
2454 memcpy(wk->key, req->key, req->key_len);
2455 }
2456
Johannes Berg77fdaa12009-07-07 03:45:17 +02002457 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2458 memcpy(wk->ssid, ssid + 2, ssid[1]);
2459 wk->ssid_len = ssid[1];
2460
2461 wk->state = IEEE80211_MGD_STATE_PROBE;
2462 wk->auth_alg = auth_alg;
Johannes Berg48531842009-07-23 16:50:16 +02002463 wk->timeout = jiffies; /* run right away */
Johannes Berg77fdaa12009-07-07 03:45:17 +02002464
2465 /*
2466 * XXX: if still associated need to tell AP that we're going
2467 * to sleep and then change channel etc.
2468 */
2469 sdata->local->oper_channel = req->bss->channel;
2470 ieee80211_hw_config(sdata->local, 0);
2471
2472 mutex_lock(&ifmgd->mtx);
2473 list_add(&wk->list, &sdata->u.mgd.work_list);
2474 mutex_unlock(&ifmgd->mtx);
2475
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002476 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002477 return 0;
2478}
2479
2480int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2481 struct cfg80211_assoc_request *req)
2482{
2483 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2484 struct ieee80211_mgd_work *wk, *found = NULL;
2485 int i, err;
2486
2487 mutex_lock(&ifmgd->mtx);
2488
2489 list_for_each_entry(wk, &ifmgd->work_list, list) {
2490 if (&wk->bss->cbss == req->bss &&
2491 wk->state == IEEE80211_MGD_STATE_IDLE) {
2492 found = wk;
2493 break;
2494 }
2495 }
2496
2497 if (!found) {
2498 err = -ENOLINK;
2499 goto out;
2500 }
2501
2502 list_del(&found->list);
2503
2504 wk = krealloc(found, sizeof(*wk) + req->ie_len, GFP_KERNEL);
2505 if (!wk) {
2506 list_add(&found->list, &ifmgd->work_list);
2507 err = -ENOMEM;
2508 goto out;
2509 }
2510
2511 list_add(&wk->list, &ifmgd->work_list);
2512
2513 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
2514
2515 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2516 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2517 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2518 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2519 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2520
2521 sdata->local->oper_channel = req->bss->channel;
2522 ieee80211_hw_config(sdata->local, 0);
2523
2524 if (req->ie && req->ie_len) {
2525 memcpy(wk->ie, req->ie, req->ie_len);
2526 wk->ie_len = req->ie_len;
2527 } else
2528 wk->ie_len = 0;
2529
2530 if (req->prev_bssid)
2531 memcpy(wk->prev_bssid, req->prev_bssid, ETH_ALEN);
2532
2533 wk->state = IEEE80211_MGD_STATE_ASSOC;
2534 wk->tries = 0;
Johannes Berg48531842009-07-23 16:50:16 +02002535 wk->timeout = jiffies; /* run right away */
Johannes Berg77fdaa12009-07-07 03:45:17 +02002536
2537 if (req->use_mfp) {
2538 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2539 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2540 } else {
2541 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2542 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2543 }
2544
2545 if (req->crypto.control_port)
2546 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2547 else
2548 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2549
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002550 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002551
2552 err = 0;
2553
2554 out:
2555 mutex_unlock(&ifmgd->mtx);
2556 return err;
2557}
2558
2559int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503dd2009-07-07 03:56:11 +02002560 struct cfg80211_deauth_request *req,
2561 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002562{
2563 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2564 struct ieee80211_mgd_work *wk;
2565 const u8 *bssid = NULL;
Johannes Berga58ce432009-11-19 12:45:42 +01002566 bool not_auth_yet = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002567
Johannes Berg77fdaa12009-07-07 03:45:17 +02002568 mutex_lock(&ifmgd->mtx);
2569
2570 if (ifmgd->associated && &ifmgd->associated->cbss == req->bss) {
2571 bssid = req->bss->bssid;
Johannes Berge21546a2009-08-06 20:41:32 +02002572 ieee80211_set_disassoc(sdata, true);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002573 } else list_for_each_entry(wk, &ifmgd->work_list, list) {
2574 if (&wk->bss->cbss == req->bss) {
2575 bssid = req->bss->bssid;
Johannes Berga58ce432009-11-19 12:45:42 +01002576 if (wk->state == IEEE80211_MGD_STATE_PROBE)
2577 not_auth_yet = true;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002578 list_del(&wk->list);
2579 kfree(wk);
2580 break;
2581 }
2582 }
2583
Johannes Berg8d8b2612009-07-25 11:58:36 +02002584 /*
Johannes Berga58ce432009-11-19 12:45:42 +01002585 * If somebody requests authentication and we haven't
2586 * sent out an auth frame yet there's no need to send
2587 * out a deauth frame either. If the state was PROBE,
2588 * then this is the case. If it's AUTH we have sent a
2589 * frame, and if it's IDLE we have completed the auth
2590 * process already.
2591 */
2592 if (not_auth_yet) {
2593 mutex_unlock(&ifmgd->mtx);
2594 __cfg80211_auth_canceled(sdata->dev, bssid);
2595 return 0;
2596 }
2597
2598 /*
Johannes Berg8d8b2612009-07-25 11:58:36 +02002599 * cfg80211 should catch this ... but it's racy since
2600 * we can receive a deauth frame, process it, hand it
2601 * to cfg80211 while that's in a locked section already
2602 * trying to tell us that the user wants to disconnect.
2603 */
2604 if (!bssid) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002605 mutex_unlock(&ifmgd->mtx);
2606 return -ENOLINK;
2607 }
2608
2609 mutex_unlock(&ifmgd->mtx);
2610
Johannes Berg0ff71612009-09-26 14:45:41 +02002611 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002612 sdata->name, bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002613
Johannes Berg77fdaa12009-07-07 03:45:17 +02002614 ieee80211_send_deauth_disassoc(sdata, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +02002615 IEEE80211_STYPE_DEAUTH, req->reason_code,
2616 cookie);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002617
2618 return 0;
2619}
2620
2621int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503dd2009-07-07 03:56:11 +02002622 struct cfg80211_disassoc_request *req,
2623 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002624{
2625 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2626
Johannes Berg77fdaa12009-07-07 03:45:17 +02002627 mutex_lock(&ifmgd->mtx);
2628
Johannes Berg8d8b2612009-07-25 11:58:36 +02002629 /*
2630 * cfg80211 should catch this ... but it's racy since
2631 * we can receive a disassoc frame, process it, hand it
2632 * to cfg80211 while that's in a locked section already
2633 * trying to tell us that the user wants to disconnect.
2634 */
2635 if (&ifmgd->associated->cbss != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002636 mutex_unlock(&ifmgd->mtx);
2637 return -ENOLINK;
2638 }
2639
Johannes Berg0ff71612009-09-26 14:45:41 +02002640 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002641 sdata->name, req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002642
Johannes Berge21546a2009-08-06 20:41:32 +02002643 ieee80211_set_disassoc(sdata, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002644
2645 mutex_unlock(&ifmgd->mtx);
2646
2647 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +02002648 IEEE80211_STYPE_DISASSOC, req->reason_code,
2649 cookie);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002650 return 0;
2651}