blob: 849d9c639add2d7f0656c094d05a0cd29b374d1a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070023#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020024#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020025
Jiri Bencf0706e82007-05-05 11:45:53 -070026#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020027#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040028#include "rate.h"
29#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030
Maxim Levitskya43abf22009-07-31 18:54:12 +030031#define IEEE80211_MAX_PROBE_TRIES 5
Johannes Bergb291ba12009-07-10 15:29:03 +020032
33/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010034 * Beacon loss timeout is calculated as N frames times the
35 * advertised beacon interval. This may need to be somewhat
36 * higher than what hardware might detect to account for
37 * delays in the host processing frames. But since we also
38 * probe on beacon miss before declaring the connection lost
39 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020040 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010041#define IEEE80211_BEACON_LOSS_COUNT 7
42
Johannes Bergb291ba12009-07-10 15:29:03 +020043/*
44 * Time the connection can be idle before we probe
45 * it to see if we can still talk to the AP.
46 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030047#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020048/*
49 * Time we wait for a probe response after sending
50 * a probe request because of beacon loss or for
51 * checking the connection still works.
52 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030053#define IEEE80211_PROBE_WAIT (HZ / 2)
Jiri Bencf0706e82007-05-05 11:45:53 -070054
Jouni Malinen17e4ec12010-03-29 23:28:30 -070055/*
56 * Weight given to the latest Beacon frame when calculating average signal
57 * strength for Beacon frames received in the current BSS. This must be
58 * between 1 and 15.
59 */
60#define IEEE80211_SIGNAL_AVE_WEIGHT 3
61
Jouni Malinen391a2002010-08-27 22:22:00 +030062/*
63 * How many Beacon frames need to have been used in average signal strength
64 * before starting to indicate signal change events.
65 */
66#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
67
Johannes Berg5bb644a2009-05-17 11:40:42 +020068#define TMR_RUNNING_TIMER 0
69#define TMR_RUNNING_CHANSW 1
70
Johannes Berg77fdaa12009-07-07 03:45:17 +020071/*
72 * All cfg80211 functions have to be called outside a locked
73 * section so that they can acquire a lock themselves... This
74 * is much simpler than queuing up things in cfg80211, but we
75 * do need some indirection for that here.
76 */
77enum rx_mgmt_action {
78 /* no action required */
79 RX_MGMT_NONE,
80
81 /* caller must call cfg80211_send_rx_auth() */
82 RX_MGMT_CFG80211_AUTH,
83
84 /* caller must call cfg80211_send_rx_assoc() */
85 RX_MGMT_CFG80211_ASSOC,
86
87 /* caller must call cfg80211_send_deauth() */
88 RX_MGMT_CFG80211_DEAUTH,
89
90 /* caller must call cfg80211_send_disassoc() */
91 RX_MGMT_CFG80211_DISASSOC,
92
Johannes Berg5d1ec852009-12-02 12:43:43 +010093 /* caller must tell cfg80211 about internal error */
94 RX_MGMT_CFG80211_ASSOC_ERROR,
Johannes Berg77fdaa12009-07-07 03:45:17 +020095};
96
Johannes Berg5484e232008-09-08 17:44:27 +020097/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +020098static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
99{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200100 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200101}
102
Johannes Bergca386f32009-07-10 02:39:48 +0200103/*
104 * We can have multiple work items (and connection probing)
105 * scheduling this timer, but we need to take care to only
106 * reschedule it when it should fire _earlier_ than it was
107 * asked for before, or if it's not pending right now. This
108 * function ensures that. Note that it then is required to
109 * run this function for all timeouts after the first one
110 * has happened -- the work that runs from this timer will
111 * do that.
112 */
113static void run_again(struct ieee80211_if_managed *ifmgd,
114 unsigned long timeout)
115{
116 ASSERT_MGD_MTX(ifmgd);
117
118 if (!timer_pending(&ifmgd->timer) ||
119 time_before(timeout, ifmgd->timer.expires))
120 mod_timer(&ifmgd->timer, timeout);
121}
122
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400123void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200124{
125 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
126 return;
127
128 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100129 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200130}
131
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400132void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
133{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400134 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
135
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400136 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
137 return;
138
139 mod_timer(&sdata->u.mgd.conn_mon_timer,
140 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400141
142 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400143}
144
Johannes Berg5484e232008-09-08 17:44:27 +0200145static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200146{
Johannes Berg5484e232008-09-08 17:44:27 +0200147 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200148}
149
Johannes Bergd5522e02009-03-30 13:23:35 +0200150/*
151 * ieee80211_enable_ht should be called only after the operating band
152 * has been determined as ht configuration depends on the hw's
153 * HT abilities for a specific band.
154 */
155static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
156 struct ieee80211_ht_info *hti,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200157 const u8 *bssid, u16 ap_ht_cap_flags)
Johannes Bergd5522e02009-03-30 13:23:35 +0200158{
159 struct ieee80211_local *local = sdata->local;
160 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200161 struct sta_info *sta;
162 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200163 u16 ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200164 bool enable_ht = true;
165 enum nl80211_channel_type prev_chantype;
Johannes Bergd5522e02009-03-30 13:23:35 +0200166 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
167
168 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
169
Johannes Berg0aaffa92010-05-05 15:28:27 +0200170 prev_chantype = sdata->vif.bss_conf.channel_type;
171
Johannes Bergd5522e02009-03-30 13:23:35 +0200172 /* HT is not supported */
173 if (!sband->ht_cap.ht_supported)
174 enable_ht = false;
175
176 /* check that channel matches the right operating channel */
177 if (local->hw.conf.channel->center_freq !=
178 ieee80211_channel_to_frequency(hti->control_chan))
179 enable_ht = false;
180
181 if (enable_ht) {
182 channel_type = NL80211_CHAN_HT20;
183
184 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
185 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
186 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
187 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
188 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400189 if (!(local->hw.conf.channel->flags &
190 IEEE80211_CHAN_NO_HT40PLUS))
191 channel_type = NL80211_CHAN_HT40PLUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200192 break;
193 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400194 if (!(local->hw.conf.channel->flags &
195 IEEE80211_CHAN_NO_HT40MINUS))
196 channel_type = NL80211_CHAN_HT40MINUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200197 break;
198 }
199 }
200 }
201
Reinette Chatrefe6f2122010-04-19 10:46:31 -0700202 if (local->tmp_channel)
203 local->tmp_channel_type = channel_type;
Johannes Bergd5522e02009-03-30 13:23:35 +0200204
Johannes Berg0aaffa92010-05-05 15:28:27 +0200205 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
206 /* can only fail due to HT40+/- mismatch */
207 channel_type = NL80211_CHAN_HT20;
208 WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
209 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200210
Johannes Berg0aaffa92010-05-05 15:28:27 +0200211 /* channel_type change automatically detected */
212 ieee80211_hw_config(local, 0);
213
214 if (prev_chantype != channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200215 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100216 sta = sta_info_get(sdata, bssid);
Johannes Bergd5522e02009-03-30 13:23:35 +0200217 if (sta)
218 rate_control_rate_update(local, sband, sta,
Sujith4fa00432010-03-01 14:42:57 +0530219 IEEE80211_RC_HT_CHANGED,
Johannes Berg0aaffa92010-05-05 15:28:27 +0200220 channel_type);
Johannes Bergd5522e02009-03-30 13:23:35 +0200221 rcu_read_unlock();
Johannes Berg0aaffa92010-05-05 15:28:27 +0200222 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200223
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200224 ht_opmode = le16_to_cpu(hti->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200225
226 /* if bss configuration changed store the new one */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200227 if (sdata->ht_opmode_valid != enable_ht ||
228 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
229 prev_chantype != channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200230 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200231 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200232 sdata->ht_opmode_valid = enable_ht;
Johannes Bergd5522e02009-03-30 13:23:35 +0200233 }
234
235 return changed;
236}
237
Johannes Berg9c6bd792008-09-11 00:01:52 +0200238/* frame sending functions */
239
Johannes Bergef422bc2008-09-09 10:58:25 +0200240static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503dd2009-07-07 03:56:11 +0200241 const u8 *bssid, u16 stype, u16 reason,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300242 void *cookie, bool send_frame)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200243{
244 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100245 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200246 struct sk_buff *skb;
247 struct ieee80211_mgmt *mgmt;
248
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200249 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
Johannes Berg9ac19a92008-09-09 10:57:09 +0200250 if (!skb) {
Johannes Bergef422bc2008-09-09 10:58:25 +0200251 printk(KERN_DEBUG "%s: failed to allocate buffer for "
Johannes Berg47846c92009-11-25 17:46:19 +0100252 "deauth/disassoc frame\n", sdata->name);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200253 return;
254 }
255 skb_reserve(skb, local->hw.extra_tx_headroom);
256
257 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
258 memset(mgmt, 0, 24);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200259 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100260 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200261 memcpy(mgmt->bssid, bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200262 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200263 skb_put(skb, 2);
Johannes Bergef422bc2008-09-09 10:58:25 +0200264 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200265 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
266
Jouni Malinen53b46b82009-03-27 20:53:56 +0200267 if (stype == IEEE80211_STYPE_DEAUTH)
Holger Schurigce470612009-10-13 13:28:13 +0200268 if (cookie)
269 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
270 else
271 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Jouni Malinen53b46b82009-03-27 20:53:56 +0200272 else
Holger Schurigce470612009-10-13 13:28:13 +0200273 if (cookie)
274 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
275 else
276 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg62ae67b2009-11-18 18:42:05 +0100277 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
278 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300279
280 if (send_frame)
281 ieee80211_tx_skb(sdata, skb);
282 else
283 kfree_skb(skb);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200284}
285
Kalle Valo572e0012009-02-10 17:09:31 +0200286void ieee80211_send_pspoll(struct ieee80211_local *local,
287 struct ieee80211_sub_if_data *sdata)
288{
Kalle Valo572e0012009-02-10 17:09:31 +0200289 struct ieee80211_pspoll *pspoll;
290 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200291
Kalle Valod8cd1892010-01-05 20:16:26 +0200292 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
293 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200294 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200295
Kalle Valod8cd1892010-01-05 20:16:26 +0200296 pspoll = (struct ieee80211_pspoll *) skb->data;
297 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200298
Johannes Berg62ae67b2009-11-18 18:42:05 +0100299 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
300 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200301}
302
Johannes Berg965beda2009-04-16 13:17:24 +0200303void ieee80211_send_nullfunc(struct ieee80211_local *local,
304 struct ieee80211_sub_if_data *sdata,
305 int powersave)
306{
307 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200308 struct ieee80211_hdr_3addr *nullfunc;
Johannes Berg965beda2009-04-16 13:17:24 +0200309
Kalle Valod8cd1892010-01-05 20:16:26 +0200310 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
311 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200312 return;
313
Kalle Valod8cd1892010-01-05 20:16:26 +0200314 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200315 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200316 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200317
Johannes Berg62ae67b2009-11-18 18:42:05 +0100318 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
319 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200320}
321
Felix Fietkaud5242152010-01-08 18:06:26 +0100322static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
323 struct ieee80211_sub_if_data *sdata)
324{
325 struct sk_buff *skb;
326 struct ieee80211_hdr *nullfunc;
327 __le16 fc;
328
329 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
330 return;
331
332 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
333 if (!skb) {
334 printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
335 "nullfunc frame\n", sdata->name);
336 return;
337 }
338 skb_reserve(skb, local->hw.extra_tx_headroom);
339
340 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
341 memset(nullfunc, 0, 30);
342 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
343 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
344 nullfunc->frame_control = fc;
345 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
346 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
347 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
348 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
349
350 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
351 ieee80211_tx_skb(sdata, skb);
352}
353
Johannes Bergcc32abd2009-05-15 11:52:31 +0200354/* spectrum management related things */
355static void ieee80211_chswitch_work(struct work_struct *work)
356{
357 struct ieee80211_sub_if_data *sdata =
358 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200359 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
360
Johannes Berg9607e6b2009-12-23 13:15:31 +0100361 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200362 return;
363
Johannes Berg77fdaa12009-07-07 03:45:17 +0200364 mutex_lock(&ifmgd->mtx);
365 if (!ifmgd->associated)
366 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200367
368 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200369 if (!sdata->local->ops->channel_switch) {
370 /* call "hw_config" only if doing sw channel switch */
371 ieee80211_hw_config(sdata->local,
372 IEEE80211_CONF_CHANGE_CHANNEL);
373 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200374
Johannes Berg77fdaa12009-07-07 03:45:17 +0200375 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100376 ifmgd->associated->channel = sdata->local->oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200377
Johannes Bergcc32abd2009-05-15 11:52:31 +0200378 ieee80211_wake_queues_by_reason(&sdata->local->hw,
379 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200380 out:
381 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
382 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200383}
384
Johannes Berg5ce6e432010-05-11 16:20:57 +0200385void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
386{
387 struct ieee80211_sub_if_data *sdata;
388 struct ieee80211_if_managed *ifmgd;
389
390 sdata = vif_to_sdata(vif);
391 ifmgd = &sdata->u.mgd;
392
393 trace_api_chswitch_done(sdata, success);
394 if (!success) {
395 /*
396 * If the channel switch was not successful, stay
397 * around on the old channel. We currently lack
398 * good handling of this situation, possibly we
399 * should just drop the association.
400 */
401 sdata->local->csa_channel = sdata->local->oper_channel;
402 }
403
404 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
405}
406EXPORT_SYMBOL(ieee80211_chswitch_done);
407
Johannes Bergcc32abd2009-05-15 11:52:31 +0200408static void ieee80211_chswitch_timer(unsigned long data)
409{
410 struct ieee80211_sub_if_data *sdata =
411 (struct ieee80211_sub_if_data *) data;
412 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
413
Johannes Berg5bb644a2009-05-17 11:40:42 +0200414 if (sdata->local->quiescing) {
415 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
416 return;
417 }
418
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400419 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200420}
421
422void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
423 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200424 struct ieee80211_bss *bss,
425 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200426{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100427 struct cfg80211_bss *cbss =
428 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200429 struct ieee80211_channel *new_ch;
430 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
431 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
432
Johannes Berg77fdaa12009-07-07 03:45:17 +0200433 ASSERT_MGD_MTX(ifmgd);
434
435 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200436 return;
437
Helmut Schaafbe9c422009-07-23 12:14:04 +0200438 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200439 return;
440
441 /* Disregard subsequent beacons if we are already running a timer
442 processing a CSA */
443
444 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
445 return;
446
447 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
448 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
449 return;
450
451 sdata->local->csa_channel = new_ch;
452
Johannes Berg5ce6e432010-05-11 16:20:57 +0200453 if (sdata->local->ops->channel_switch) {
454 /* use driver's channel switch callback */
455 struct ieee80211_channel_switch ch_switch;
456 memset(&ch_switch, 0, sizeof(ch_switch));
457 ch_switch.timestamp = timestamp;
458 if (sw_elem->mode) {
459 ch_switch.block_tx = true;
460 ieee80211_stop_queues_by_reason(&sdata->local->hw,
461 IEEE80211_QUEUE_STOP_REASON_CSA);
462 }
463 ch_switch.channel = new_ch;
464 ch_switch.count = sw_elem->count;
465 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
466 drv_channel_switch(sdata->local, &ch_switch);
467 return;
468 }
469
470 /* channel switch handled in software */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200471 if (sw_elem->count <= 1) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400472 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200473 } else {
Wey-Yi Guy9feaddc2010-05-05 20:34:02 -0700474 if (sw_elem->mode)
475 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200476 IEEE80211_QUEUE_STOP_REASON_CSA);
477 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
478 mod_timer(&ifmgd->chswitch_timer,
479 jiffies +
480 msecs_to_jiffies(sw_elem->count *
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100481 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200482 }
483}
484
485static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
486 u16 capab_info, u8 *pwr_constr_elem,
487 u8 pwr_constr_elem_len)
488{
489 struct ieee80211_conf *conf = &sdata->local->hw.conf;
490
491 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
492 return;
493
494 /* Power constraint IE length should be 1 octet */
495 if (pwr_constr_elem_len != 1)
496 return;
497
498 if ((*pwr_constr_elem <= conf->channel->max_power) &&
499 (*pwr_constr_elem != sdata->local->power_constr_level)) {
500 sdata->local->power_constr_level = *pwr_constr_elem;
501 ieee80211_hw_config(sdata->local, 0);
502 }
503}
504
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300505void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
506{
507 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
508 struct ieee80211_local *local = sdata->local;
509 struct ieee80211_conf *conf = &local->hw.conf;
510
511 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
512 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
513 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
514
515 local->disable_dynamic_ps = false;
516 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
517}
518EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
519
520void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
521{
522 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
523 struct ieee80211_local *local = sdata->local;
524 struct ieee80211_conf *conf = &local->hw.conf;
525
526 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
527 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
528 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
529
530 local->disable_dynamic_ps = true;
531 conf->dynamic_ps_timeout = 0;
532 del_timer_sync(&local->dynamic_ps_timer);
533 ieee80211_queue_work(&local->hw,
534 &local->dynamic_ps_enable_work);
535}
536EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
537
Johannes Berg965beda2009-04-16 13:17:24 +0200538/* powersave */
539static void ieee80211_enable_ps(struct ieee80211_local *local,
540 struct ieee80211_sub_if_data *sdata)
541{
542 struct ieee80211_conf *conf = &local->hw.conf;
543
Johannes Bergd5edaed2009-04-22 23:02:51 +0200544 /*
545 * If we are scanning right now then the parameters will
546 * take effect when scan finishes.
547 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200548 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200549 return;
550
Johannes Berg965beda2009-04-16 13:17:24 +0200551 if (conf->dynamic_ps_timeout > 0 &&
552 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
553 mod_timer(&local->dynamic_ps_timer, jiffies +
554 msecs_to_jiffies(conf->dynamic_ps_timeout));
555 } else {
556 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
557 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530558
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200559 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
560 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
561 return;
562
563 conf->flags |= IEEE80211_CONF_PS;
564 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200565 }
566}
567
568static void ieee80211_change_ps(struct ieee80211_local *local)
569{
570 struct ieee80211_conf *conf = &local->hw.conf;
571
572 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200573 ieee80211_enable_ps(local, local->ps_sdata);
574 } else if (conf->flags & IEEE80211_CONF_PS) {
575 conf->flags &= ~IEEE80211_CONF_PS;
576 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
577 del_timer_sync(&local->dynamic_ps_timer);
578 cancel_work_sync(&local->dynamic_ps_enable_work);
579 }
580}
581
582/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200583void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200584{
585 struct ieee80211_sub_if_data *sdata, *found = NULL;
586 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300587 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +0200588
589 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
590 local->ps_sdata = NULL;
591 return;
592 }
593
Johannes Bergaf6b6372009-12-23 13:15:35 +0100594 if (!list_empty(&local->work_list)) {
595 local->ps_sdata = NULL;
596 goto change;
597 }
598
Johannes Berg965beda2009-04-16 13:17:24 +0200599 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100600 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +0200601 continue;
602 if (sdata->vif.type != NL80211_IFTYPE_STATION)
603 continue;
604 found = sdata;
605 count++;
606 }
607
Luis R. Rodriguez43f78532009-06-10 15:16:15 +0200608 if (count == 1 && found->u.mgd.powersave &&
Johannes Bergaf6b6372009-12-23 13:15:35 +0100609 found->u.mgd.associated &&
Johannes Berg56007a02010-01-26 14:19:52 +0100610 found->u.mgd.associated->beacon_ies &&
Johannes Bergb291ba12009-07-10 15:29:03 +0200611 !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
612 IEEE80211_STA_CONNECTION_POLL))) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300613 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +0200614 s32 beaconint_us;
615
616 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +0200617 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +0200618
619 beaconint_us = ieee80211_tu_to_usec(
620 found->vif.bss_conf.beacon_int);
621
Juuso Oikarinenff616382010-06-09 09:51:52 +0300622 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300623 if (timeout < 0) {
624 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +0300625 * Go to full PSM if the user configures a very low
626 * latency requirement.
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300627 * The 2 second value is there for compatibility until
628 * the PM_QOS_NETWORK_LATENCY is configured with real
629 * values.
630 */
Juuso Oikarinenff616382010-06-09 09:51:52 +0300631 if (latency > 1900000000 && latency != 2000000000)
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300632 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +0300633 else
634 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300635 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300636 local->dynamic_ps_user_timeout = timeout;
637 if (!local->disable_dynamic_ps)
638 conf->dynamic_ps_timeout =
639 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300640
Johannes Berg04fe2032009-04-22 18:44:37 +0200641 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +0200642 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +0200643 } else {
Johannes Berg56007a02010-01-26 14:19:52 +0100644 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +0200645 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +0100646 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +0200647
Johannes Berg56007a02010-01-26 14:19:52 +0100648 bss = (void *)found->u.mgd.associated->priv;
649 dtimper = bss->dtim_period;
650
651 /* If the TIM IE is invalid, pretend the value is 1 */
652 if (!dtimper)
653 dtimper = 1;
654 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +0200655 maxslp = min_t(int, dtimper,
656 latency / beaconint_us);
657
Johannes Berg9ccebe62009-04-23 10:32:36 +0200658 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +0100659 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +0200660 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +0200661 }
Johannes Berg10f644a2009-04-16 13:17:25 +0200662 } else {
Johannes Berg965beda2009-04-16 13:17:24 +0200663 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +0200664 }
Johannes Berg965beda2009-04-16 13:17:24 +0200665
Johannes Bergaf6b6372009-12-23 13:15:35 +0100666 change:
Johannes Berg965beda2009-04-16 13:17:24 +0200667 ieee80211_change_ps(local);
668}
669
670void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
671{
672 struct ieee80211_local *local =
673 container_of(work, struct ieee80211_local,
674 dynamic_ps_disable_work);
675
676 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
677 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
678 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
679 }
680
681 ieee80211_wake_queues_by_reason(&local->hw,
682 IEEE80211_QUEUE_STOP_REASON_PS);
683}
684
685void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
686{
687 struct ieee80211_local *local =
688 container_of(work, struct ieee80211_local,
689 dynamic_ps_enable_work);
690 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Vivek Natarajan375177b2010-02-09 14:50:28 +0530691 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200692
693 /* can only happen when PS was just disabled anyway */
694 if (!sdata)
695 return;
696
697 if (local->hw.conf.flags & IEEE80211_CONF_PS)
698 return;
699
Vivek Natarajan375177b2010-02-09 14:50:28 +0530700 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
701 (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
Johannes Berg965beda2009-04-16 13:17:24 +0200702 ieee80211_send_nullfunc(local, sdata, 1);
703
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200704 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
705 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +0530706 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
707 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
708 local->hw.conf.flags |= IEEE80211_CONF_PS;
709 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
710 }
Johannes Berg965beda2009-04-16 13:17:24 +0200711}
712
713void ieee80211_dynamic_ps_timer(unsigned long data)
714{
715 struct ieee80211_local *local = (void *) data;
716
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -0700717 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +0200718 return;
719
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400720 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +0200721}
722
Johannes Berg60f8b392008-09-08 17:44:22 +0200723/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200724static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +0200725 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700726 u8 *wmm_param, size_t wmm_param_len)
727{
Jiri Bencf0706e82007-05-05 11:45:53 -0700728 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +0200729 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -0700730 size_t left;
731 int count;
Kalle Valoab133152010-01-12 10:42:31 +0200732 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700733
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200734 if (!local->ops->conf_tx)
735 return;
736
Johannes Bergaf6b6372009-12-23 13:15:35 +0100737 if (local->hw.queues < 4)
Johannes Berg3434fbd2008-05-03 00:59:37 +0200738 return;
739
740 if (!wmm_param)
741 return;
742
Jiri Bencf0706e82007-05-05 11:45:53 -0700743 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
744 return;
Kalle Valoab133152010-01-12 10:42:31 +0200745
746 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Kalle Valo50ae0cf2010-01-12 10:42:39 +0200747 uapsd_queues = local->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +0200748
Jiri Bencf0706e82007-05-05 11:45:53 -0700749 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +0100750 if (count == ifmgd->wmm_last_param_set)
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 return;
Johannes Berg46900292009-02-15 12:44:28 +0100752 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -0700753
754 pos = wmm_param + 8;
755 left = wmm_param_len - 8;
756
757 memset(&params, 0, sizeof(params));
758
Jiri Bencf0706e82007-05-05 11:45:53 -0700759 local->wmm_acm = 0;
760 for (; left >= 4; left -= 4, pos += 4) {
761 int aci = (pos[0] >> 5) & 0x03;
762 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +0200763 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700764 int queue;
765
766 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200767 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +0200768 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200769 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200770 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +0200771 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
772 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700773 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200774 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +0200775 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200776 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200777 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +0200778 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
779 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700780 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200781 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +0200782 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200783 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200784 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +0200785 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
786 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700787 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200788 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -0700789 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200790 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200791 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200792 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +0200793 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
794 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700795 break;
796 }
797
798 params.aifs = pos[0] & 0x0f;
799 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
800 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200801 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +0200802 params.uapsd = uapsd;
803
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200804#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700805 wiphy_debug(local->hw.wiphy,
806 "WMM queue=%d aci=%d acm=%d aifs=%d "
807 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
808 queue, aci, acm,
809 params.aifs, params.cw_min, params.cw_max,
810 params.txop, params.uapsd);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100811#endif
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200812 if (drv_conf_tx(local, queue, &params))
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700813 wiphy_debug(local->hw.wiphy,
814 "failed to set TX queue parameters for queue %d\n",
815 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700816 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200817
818 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +0200819 sdata->vif.bss_conf.qos = true;
820 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
Jiri Bencf0706e82007-05-05 11:45:53 -0700821}
822
Johannes Berg7a5158e2008-10-08 10:59:33 +0200823static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
824 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +0200825{
Johannes Bergbda39332008-10-11 01:51:51 +0200826 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100827 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +0200828 bool use_protection;
829 bool use_short_preamble;
830 bool use_short_slot;
831
832 if (erp_valid) {
833 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
834 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
835 } else {
836 use_protection = false;
837 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
838 }
839
840 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Felix Fietkau43d35342010-01-15 03:00:48 +0100841 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
842 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +0200843
Johannes Berg471b3ef2007-12-28 14:32:58 +0100844 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +0100845 bss_conf->use_cts_prot = use_protection;
846 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200847 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200848
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200849 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200850 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100851 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200852 }
Daniel Draked9430a32007-07-27 15:43:24 +0200853
Johannes Berg7a5158e2008-10-08 10:59:33 +0200854 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +0200855 bss_conf->use_short_slot = use_short_slot;
856 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -0400857 }
858
859 return changed;
860}
861
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200862static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100863 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +0200864 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700865{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100866 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100867 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +0300868 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400869
Johannes Bergae5eb022008-10-14 16:58:37 +0200870 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200871 /* set timing information */
Juuso Oikarinen68542962010-06-09 13:43:26 +0300872 bss_conf->beacon_int = cbss->beacon_interval;
873 bss_conf->timestamp = cbss->tsf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400874
Johannes Berg77fdaa12009-07-07 03:45:17 +0200875 bss_info_changed |= BSS_CHANGED_BEACON_INT;
876 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100877 cbss->capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700878
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100879 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
880 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
881
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100882 sdata->u.mgd.associated = cbss;
883 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100884
Jouni Malinen17e4ec12010-03-29 23:28:30 -0700885 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
886
Johannes Bergb291ba12009-07-10 15:29:03 +0200887 /* just to be sure */
888 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
889 IEEE80211_STA_BEACON_POLL);
890
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200891 ieee80211_led_assoc(local, 1);
892
Johannes Berge5b900d2010-07-29 16:08:55 +0200893 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
894 bss_conf->dtim_period = bss->dtim_period;
895 else
896 bss_conf->dtim_period = 0;
897
Juuso Oikarinen68542962010-06-09 13:43:26 +0300898 bss_conf->assoc = 1;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200899 /*
900 * For now just always ask the driver to update the basic rateset
901 * when we have associated, we aren't checking whether it actually
902 * changed or not.
903 */
Johannes Bergae5eb022008-10-14 16:58:37 +0200904 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
Johannes Berg9cef8732009-05-14 13:10:14 +0200905
906 /* And the BSSID changed - we're associated now */
907 bss_info_changed |= BSS_CHANGED_BSSID;
908
Juuso Oikarinena97c13c2010-03-23 09:02:34 +0200909 /* Tell the driver to monitor connection quality (if supported) */
910 if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
Juuso Oikarinen68542962010-06-09 13:43:26 +0300911 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +0200912 bss_info_changed |= BSS_CHANGED_CQM;
913
Juuso Oikarinen68542962010-06-09 13:43:26 +0300914 /* Enable ARP filtering */
915 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
916 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
917 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
918 }
919
Johannes Bergae5eb022008-10-14 16:58:37 +0200920 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300921
Johannes Berg056508d2009-07-30 21:43:55 +0200922 mutex_lock(&local->iflist_mtx);
923 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +0200924 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +0200925 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +0200926
John W. Linville8a5b33f2010-01-06 15:39:39 -0500927 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200928 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700929}
930
Jouni Malinene69e95d2010-03-29 23:29:31 -0700931static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg53f73c02010-10-05 19:37:40 +0200932 bool remove_sta, bool tx)
Tomas Winkleraa458d12008-09-09 00:32:12 +0300933{
Johannes Berg46900292009-02-15 12:44:28 +0100934 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +0300935 struct ieee80211_local *local = sdata->local;
936 struct sta_info *sta;
Kalle Valoe0cb6862008-12-18 23:35:13 +0200937 u32 changed = 0, config_changed = 0;
Johannes Bergb291ba12009-07-10 15:29:03 +0200938 u8 bssid[ETH_ALEN];
Tomas Winkleraa458d12008-09-09 00:32:12 +0300939
Johannes Berg77fdaa12009-07-07 03:45:17 +0200940 ASSERT_MGD_MTX(ifmgd);
941
Johannes Bergb291ba12009-07-10 15:29:03 +0200942 if (WARN_ON(!ifmgd->associated))
943 return;
944
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100945 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Johannes Bergb291ba12009-07-10 15:29:03 +0200946
Johannes Berg77fdaa12009-07-07 03:45:17 +0200947 ifmgd->associated = NULL;
948 memset(ifmgd->bssid, 0, ETH_ALEN);
949
950 /*
951 * we need to commit the associated = NULL change because the
952 * scan code uses that to determine whether this iface should
953 * go to/wake up from powersave or not -- and could otherwise
954 * wake the queues erroneously.
955 */
956 smp_mb();
957
958 /*
959 * Thus, we can only afterwards stop the queues -- to account
960 * for the case where another CPU is finishing a scan at this
961 * time -- we don't want the scan code to enable queues.
962 */
Tomas Winkleraa458d12008-09-09 00:32:12 +0300963
John W. Linville8a5b33f2010-01-06 15:39:39 -0500964 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300965 netif_carrier_off(sdata->dev);
966
Johannes Berg2a419052010-06-10 10:21:29 +0200967 mutex_lock(&local->sta_mtx);
Johannes Bergabe60632009-11-25 17:46:18 +0100968 sta = sta_info_get(sdata, bssid);
Sujith4cad6c72010-02-10 14:52:21 +0530969 if (sta) {
Johannes Berg2a419052010-06-10 10:21:29 +0200970 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +0200971 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +0530972 }
Johannes Berg2a419052010-06-10 10:21:29 +0200973 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300974
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200975 changed |= ieee80211_reset_erp_info(sdata);
976
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200977 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +0200978 changed |= BSS_CHANGED_ASSOC;
979 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200980
Johannes Bergaa837e12009-05-07 16:16:24 +0200981 ieee80211_set_wmm_default(sdata);
982
Johannes Berg47979382009-01-07 10:13:27 +0100983 /* channel(_type) changes are handled by ieee80211_hw_config */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200984 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergae5eb022008-10-14 16:58:37 +0200985
Johannes Berg413ad502009-05-08 21:21:06 +0200986 /* on the next assoc, re-program HT parameters */
987 sdata->ht_opmode_valid = false;
988
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +0530989 local->power_constr_level = 0;
990
Kalle Valo520eb822008-12-18 23:35:27 +0200991 del_timer_sync(&local->dynamic_ps_timer);
992 cancel_work_sync(&local->dynamic_ps_enable_work);
993
Kalle Valoe0cb6862008-12-18 23:35:13 +0200994 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
995 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
996 config_changed |= IEEE80211_CONF_CHANGE_PS;
997 }
998
999 ieee80211_hw_config(local, config_changed);
Johannes Berg9cef8732009-05-14 13:10:14 +02001000
Juuso Oikarinen68542962010-06-09 13:43:26 +03001001 /* Disable ARP filtering */
1002 if (sdata->vif.bss_conf.arp_filter_enabled) {
1003 sdata->vif.bss_conf.arp_filter_enabled = false;
1004 changed |= BSS_CHANGED_ARP_FILTER;
1005 }
1006
Johannes Berg0aaffa92010-05-05 15:28:27 +02001007 /* The BSSID (not really interesting) and HT changed */
1008 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001009 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001010
Jouni Malinene69e95d2010-03-29 23:29:31 -07001011 if (remove_sta)
1012 sta_info_destroy_addr(sdata, bssid);
Johannes Bergb9dcf712010-08-27 12:35:54 +02001013
1014 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1015 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1016 del_timer_sync(&sdata->u.mgd.timer);
1017 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001018}
Jiri Bencf0706e82007-05-05 11:45:53 -07001019
Kalle Valo3cf335d2009-03-22 21:57:06 +02001020void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1021 struct ieee80211_hdr *hdr)
1022{
1023 /*
1024 * We can postpone the mgd.timer whenever receiving unicast frames
1025 * from AP because we know that the connection is working both ways
1026 * at that time. But multicast frames (and hence also beacons) must
1027 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001028 * data idle periods for sending the periodic probe request to the
1029 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001030 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001031 if (is_multicast_ether_addr(hdr->addr1))
1032 return;
1033
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001034 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001035}
Jiri Bencf0706e82007-05-05 11:45:53 -07001036
Maxim Levitskya43abf22009-07-31 18:54:12 +03001037static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1038{
1039 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1040 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001041 u8 *dst = ifmgd->associated->bssid;
1042 u8 unicast_limit = max(1, IEEE80211_MAX_PROBE_TRIES - 3);
1043
1044 /*
1045 * Try sending broadcast probe requests for the last three
1046 * probe requests after the first ones failed since some
1047 * buggy APs only support broadcast probe requests.
1048 */
1049 if (ifmgd->probe_send_count >= unicast_limit)
1050 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001051
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001052 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001053 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001054
1055 ifmgd->probe_send_count++;
1056 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1057 run_again(ifmgd, ifmgd->probe_timeout);
1058}
1059
Johannes Bergb291ba12009-07-10 15:29:03 +02001060static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1061 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001062{
Kalle Valo04de8382009-03-22 21:57:35 +02001063 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001064 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001065
Johannes Berg9607e6b2009-12-23 13:15:31 +01001066 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001067 return;
1068
Luis R. Rodriguez91a3bd72009-07-23 16:37:47 -07001069 if (sdata->local->scanning)
1070 return;
1071
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001072 if (sdata->local->tmp_channel)
1073 return;
1074
Johannes Berg77fdaa12009-07-07 03:45:17 +02001075 mutex_lock(&ifmgd->mtx);
1076
1077 if (!ifmgd->associated)
1078 goto out;
1079
Michael Buescha8604022009-04-15 14:41:22 -04001080#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergb291ba12009-07-10 15:29:03 +02001081 if (beacon && net_ratelimit())
1082 printk(KERN_DEBUG "%s: detected beacon loss from AP "
Johannes Berg47846c92009-11-25 17:46:19 +01001083 "- sending probe request\n", sdata->name);
Michael Buescha8604022009-04-15 14:41:22 -04001084#endif
Kalle Valo04de8382009-03-22 21:57:35 +02001085
Johannes Bergb291ba12009-07-10 15:29:03 +02001086 /*
1087 * The driver/our work has already reported this event or the
1088 * connection monitoring has kicked in and we have already sent
1089 * a probe request. Or maybe the AP died and the driver keeps
1090 * reporting until we disassociate...
1091 *
1092 * In either case we have to ignore the current call to this
1093 * function (except for setting the correct probe reason bit)
1094 * because otherwise we would reset the timer every time and
1095 * never check whether we received a probe response!
1096 */
1097 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1098 IEEE80211_STA_CONNECTION_POLL))
1099 already = true;
1100
1101 if (beacon)
1102 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1103 else
1104 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1105
1106 if (already)
1107 goto out;
1108
Johannes Berg4e751842009-06-10 15:16:52 +02001109 mutex_lock(&sdata->local->iflist_mtx);
1110 ieee80211_recalc_ps(sdata->local, -1);
1111 mutex_unlock(&sdata->local->iflist_mtx);
1112
Maxim Levitskya43abf22009-07-31 18:54:12 +03001113 ifmgd->probe_send_count = 0;
1114 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001115 out:
1116 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001117}
1118
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001119struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1120 struct ieee80211_vif *vif)
1121{
1122 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1123 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1124 struct sk_buff *skb;
1125 const u8 *ssid;
1126
1127 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1128 return NULL;
1129
1130 ASSERT_MGD_MTX(ifmgd);
1131
1132 if (!ifmgd->associated)
1133 return NULL;
1134
1135 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1136 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
1137 ssid + 2, ssid[1], NULL, 0);
1138
1139 return skb;
1140}
1141EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1142
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001143static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1144{
1145 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1146 struct ieee80211_local *local = sdata->local;
1147 u8 bssid[ETH_ALEN];
1148
1149 mutex_lock(&ifmgd->mtx);
1150 if (!ifmgd->associated) {
1151 mutex_unlock(&ifmgd->mtx);
1152 return;
1153 }
1154
1155 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1156
1157 printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
1158
Johannes Berg53f73c02010-10-05 19:37:40 +02001159 ieee80211_set_disassoc(sdata, true, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001160 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001161
1162 mutex_lock(&local->mtx);
1163 ieee80211_recalc_idle(local);
1164 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001165 /*
1166 * must be outside lock due to cfg80211,
1167 * but that's not a problem.
1168 */
1169 ieee80211_send_deauth_disassoc(sdata, bssid,
1170 IEEE80211_STYPE_DEAUTH,
1171 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001172 NULL, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001173}
1174
1175void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001176{
1177 struct ieee80211_sub_if_data *sdata =
1178 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001179 u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02001180
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001181 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1182 __ieee80211_connection_loss(sdata);
1183 else
1184 ieee80211_mgd_probe_ap(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001185}
1186
Kalle Valo04de8382009-03-22 21:57:35 +02001187void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1188{
1189 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001190 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001191
Johannes Bergb5878a22010-04-07 16:48:40 +02001192 trace_api_beacon_loss(sdata);
1193
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001194 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1195 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001196}
1197EXPORT_SYMBOL(ieee80211_beacon_loss);
1198
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001199void ieee80211_connection_loss(struct ieee80211_vif *vif)
1200{
1201 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1202 struct ieee80211_hw *hw = &sdata->local->hw;
1203
Johannes Bergb5878a22010-04-07 16:48:40 +02001204 trace_api_connection_loss(sdata);
1205
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001206 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1207 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1208}
1209EXPORT_SYMBOL(ieee80211_connection_loss);
1210
1211
Johannes Berg77fdaa12009-07-07 03:45:17 +02001212static enum rx_mgmt_action __must_check
1213ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001214 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001215{
Johannes Berg46900292009-02-15 12:44:28 +01001216 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001217 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001218 u16 reason_code;
1219
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001220 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001221 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001222
Johannes Berg77fdaa12009-07-07 03:45:17 +02001223 ASSERT_MGD_MTX(ifmgd);
1224
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001225 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001226
1227 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1228
Johannes Berg77fdaa12009-07-07 03:45:17 +02001229 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001230 sdata->name, bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001231
Johannes Berg53f73c02010-10-05 19:37:40 +02001232 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001233 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001234 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001235 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001236
1237 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001238}
1239
1240
Johannes Berg77fdaa12009-07-07 03:45:17 +02001241static enum rx_mgmt_action __must_check
1242ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1243 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001244{
Johannes Berg46900292009-02-15 12:44:28 +01001245 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001246 u16 reason_code;
1247
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001248 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001249 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001250
Johannes Berg77fdaa12009-07-07 03:45:17 +02001251 ASSERT_MGD_MTX(ifmgd);
1252
1253 if (WARN_ON(!ifmgd->associated))
1254 return RX_MGMT_NONE;
1255
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001256 if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001257 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001258
1259 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1260
Johannes Berg0ff71612009-09-26 14:45:41 +02001261 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001262 sdata->name, mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001263
Johannes Berg53f73c02010-10-05 19:37:40 +02001264 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001265 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001266 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001267 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001268 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001269}
1270
1271
Johannes Bergaf6b6372009-12-23 13:15:35 +01001272static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1273 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001274{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001275 struct ieee80211_sub_if_data *sdata = wk->sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001276 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001277 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001278 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001279 struct sta_info *sta;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001280 struct cfg80211_bss *cbss = wk->assoc.bss;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001281 u8 *pos;
Johannes Berg881d9482009-01-21 15:13:48 +01001282 u32 rates, basic_rates;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001283 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001284 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02001285 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02001286 u32 changed = 0;
Johannes Berg5d1ec852009-12-02 12:43:43 +01001287 int i, j, err;
1288 bool have_higher_than_11mbit = false;
Johannes Bergae5eb022008-10-14 16:58:37 +02001289 u16 ap_ht_cap_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07001290
Johannes Bergaf6b6372009-12-23 13:15:35 +01001291 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07001292
Jiri Bencf0706e82007-05-05 11:45:53 -07001293 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001294 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07001295
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001296 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1297 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Johannes Berg47846c92009-11-25 17:46:19 +01001298 "set\n", sdata->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001299 aid &= ~(BIT(15) | BIT(14));
1300
Johannes Bergaf6b6372009-12-23 13:15:35 +01001301 pos = mgmt->u.assoc_resp.variable;
1302 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1303
Jiri Bencf0706e82007-05-05 11:45:53 -07001304 if (!elems.supp_rates) {
1305 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001306 sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001307 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001308 }
1309
Johannes Berg46900292009-02-15 12:44:28 +01001310 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001311
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001312 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001313 if (!sta) {
Johannes Berg5d1ec852009-12-02 12:43:43 +01001314 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1315 " the AP\n", sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001316 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001317 }
1318
Johannes Berg5d1ec852009-12-02 12:43:43 +01001319 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1320 WLAN_STA_ASSOC_AP);
1321 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1322 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1323
Jiri Bencf0706e82007-05-05 11:45:53 -07001324 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001325 basic_rates = 0;
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001326 sband = local->hw.wiphy->bands[wk->chan->band];
Johannes Berg8318d782008-01-24 19:38:38 +01001327
Jiri Bencf0706e82007-05-05 11:45:53 -07001328 for (i = 0; i < elems.supp_rates_len; i++) {
1329 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Tomas Winklerd61272c2008-10-30 17:08:08 +02001330 bool is_basic = !!(elems.supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001331
1332 if (rate > 110)
1333 have_higher_than_11mbit = true;
1334
1335 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001336 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001337 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001338 if (is_basic)
1339 basic_rates |= BIT(j);
1340 break;
1341 }
Johannes Berg8318d782008-01-24 19:38:38 +01001342 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001343 }
Johannes Berg8318d782008-01-24 19:38:38 +01001344
Jiri Bencf0706e82007-05-05 11:45:53 -07001345 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1346 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg7e0986c2009-04-19 13:22:11 +02001347 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001348
1349 if (rate > 110)
1350 have_higher_than_11mbit = true;
1351
1352 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001353 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001354 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001355 if (is_basic)
1356 basic_rates |= BIT(j);
1357 break;
1358 }
Johannes Berg8318d782008-01-24 19:38:38 +01001359 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001360 }
Johannes Berg8318d782008-01-24 19:38:38 +01001361
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001362 sta->sta.supp_rates[wk->chan->band] = rates;
Johannes Bergbda39332008-10-11 01:51:51 +02001363 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01001364
1365 /* cf. IEEE 802.11 9.2.12 */
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001366 if (wk->chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg8318d782008-01-24 19:38:38 +01001367 have_higher_than_11mbit)
1368 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1369 else
1370 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001371
Johannes Bergab1faea2009-07-01 21:41:17 +02001372 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001373 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001374 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02001375
1376 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001377
Johannes Berg4b7679a2008-09-18 18:14:18 +02001378 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001379
Johannes Berg46900292009-02-15 12:44:28 +01001380 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Jouni Malinen5394af42009-01-08 13:31:59 +02001381 set_sta_flags(sta, WLAN_STA_MFP);
1382
Johannes Bergddf4ac52008-10-22 11:41:38 +02001383 if (elems.wmm_param)
Johannes Berg07346f812008-05-03 01:02:02 +02001384 set_sta_flags(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02001385
Johannes Berg5d1ec852009-12-02 12:43:43 +01001386 err = sta_info_insert(sta);
1387 sta = NULL;
1388 if (err) {
1389 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1390 " the AP (error %d)\n", sdata->name, err);
Johannes Berg90be5612010-01-08 19:01:07 +01001391 return false;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001392 }
1393
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03001394 /*
1395 * Always handle WMM once after association regardless
1396 * of the first value the AP uses. Setting -1 here has
1397 * that effect because the AP values is an unsigned
1398 * 4-bit value.
1399 */
1400 ifmgd->wmm_last_param_set = -1;
1401
Johannes Bergddf4ac52008-10-22 11:41:38 +02001402 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02001403 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07001404 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02001405 else
1406 ieee80211_set_wmm_default(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07001407
Johannes Berge4da8c32009-12-23 13:15:43 +01001408 local->oper_channel = wk->chan;
1409
Johannes Bergae5eb022008-10-14 16:58:37 +02001410 if (elems.ht_info_elem && elems.wmm_param &&
Johannes Bergaf6b6372009-12-23 13:15:35 +01001411 (sdata->local->hw.queues >= 4) &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001412 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001413 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001414 cbss->bssid, ap_ht_cap_flags);
Johannes Bergae5eb022008-10-14 16:58:37 +02001415
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001416 /* set AID and assoc capability,
1417 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01001418 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001419 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001420 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001421
Kalle Valo15b7b062009-03-22 21:57:14 +02001422 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01001423 * If we're using 4-addr mode, let the AP know that we're
1424 * doing so, so that it can create the STA VLAN on its side
1425 */
1426 if (ifmgd->use_4addr)
1427 ieee80211_send_4addr_nullfunc(local, sdata);
1428
1429 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02001430 * Start timer to probe the connection to the AP now.
1431 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02001432 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001433 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001434 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02001435
Johannes Bergaf6b6372009-12-23 13:15:35 +01001436 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001437}
1438
1439
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001440static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001441 struct ieee80211_mgmt *mgmt,
1442 size_t len,
1443 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001444 struct ieee802_11_elems *elems,
1445 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07001446{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001447 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001448 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02001449 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01001450 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01001451 bool need_ps = false;
1452
1453 if (sdata->u.mgd.associated) {
1454 bss = (void *)sdata->u.mgd.associated->priv;
1455 /* not previously set so we may need to recalc */
1456 need_ps = !bss->dtim_period;
1457 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001458
Johannes Berg5bda6172008-09-08 11:05:10 +02001459 if (elems->ds_params && elems->ds_params_len == 1)
1460 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1461 else
1462 freq = rx_status->freq;
1463
1464 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1465
1466 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1467 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001468
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001469 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01001470 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001471 if (bss)
1472 ieee80211_rx_bss_put(local, bss);
1473
1474 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001475 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001476
Johannes Berg56007a02010-01-26 14:19:52 +01001477 if (need_ps) {
1478 mutex_lock(&local->iflist_mtx);
1479 ieee80211_recalc_ps(local, -1);
1480 mutex_unlock(&local->iflist_mtx);
1481 }
1482
Sujithc481ec92009-01-06 09:28:37 +05301483 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001484 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001485 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05301486 struct ieee80211_channel_sw_ie *sw_elem =
1487 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001488 ieee80211_sta_process_chanswitch(sdata, sw_elem,
1489 bss, rx_status->mactime);
Sujithc481ec92009-01-06 09:28:37 +05301490 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001491}
1492
1493
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001494static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01001495 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001496{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001497 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02001498 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001499 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1500 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03001501 struct ieee802_11_elems elems;
1502
Kalle Valo15b7b062009-03-22 21:57:14 +02001503 ifmgd = &sdata->u.mgd;
1504
Johannes Berg77fdaa12009-07-07 03:45:17 +02001505 ASSERT_MGD_MTX(ifmgd);
1506
Johannes Berg47846c92009-11-25 17:46:19 +01001507 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03001508 return; /* ignore ProbeResp to foreign address */
1509
Ester Kummerae6a44e2008-06-27 18:54:48 +03001510 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1511 if (baselen > len)
1512 return;
1513
1514 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1515 &elems);
1516
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001517 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001518
Johannes Berg77fdaa12009-07-07 03:45:17 +02001519 if (ifmgd->associated &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001520 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0 &&
Johannes Bergb291ba12009-07-10 15:29:03 +02001521 ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1522 IEEE80211_STA_CONNECTION_POLL)) {
1523 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1524 IEEE80211_STA_BEACON_POLL);
Johannes Berg4e751842009-06-10 15:16:52 +02001525 mutex_lock(&sdata->local->iflist_mtx);
1526 ieee80211_recalc_ps(sdata->local, -1);
1527 mutex_unlock(&sdata->local->iflist_mtx);
Juuso Oikarinen7bdfcaa2010-04-20 13:15:56 +03001528
1529 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1530 return;
1531
Johannes Bergb291ba12009-07-10 15:29:03 +02001532 /*
1533 * We've received a probe response, but are not sure whether
1534 * we have or will be receiving any beacons or data, so let's
1535 * schedule the timers again, just in case.
1536 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001537 ieee80211_sta_reset_beacon_monitor(sdata);
Juuso Oikarinen7bdfcaa2010-04-20 13:15:56 +03001538
Johannes Bergb291ba12009-07-10 15:29:03 +02001539 mod_timer(&ifmgd->conn_mon_timer,
1540 round_jiffies_up(jiffies +
1541 IEEE80211_CONNECTION_IDLE_TIME));
Johannes Berg4e751842009-06-10 15:16:52 +02001542 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001543}
1544
Johannes Bergd91f36d2009-04-16 13:17:26 +02001545/*
1546 * This is the canonical list of information elements we care about,
1547 * the filter code also gives us all changes to the Microsoft OUI
1548 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1549 *
1550 * We implement beacon filtering in software since that means we can
1551 * avoid processing the frame here and in cfg80211, and userspace
1552 * will not be able to tell whether the hardware supports it or not.
1553 *
1554 * XXX: This list needs to be dynamic -- userspace needs to be able to
1555 * add items it requires. It also needs to be able to tell us to
1556 * look out for other vendor IEs.
1557 */
1558static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02001559 (1ULL << WLAN_EID_COUNTRY) |
1560 (1ULL << WLAN_EID_ERP_INFO) |
1561 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1562 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1563 (1ULL << WLAN_EID_HT_CAPABILITY) |
1564 (1ULL << WLAN_EID_HT_INFORMATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001565
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001566static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001567 struct ieee80211_mgmt *mgmt,
1568 size_t len,
1569 struct ieee80211_rx_status *rx_status)
1570{
Johannes Bergd91f36d2009-04-16 13:17:26 +02001571 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001572 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001573 size_t baselen;
1574 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001575 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001576 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001577 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001578 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001579 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001580 u8 *bssid;
1581
1582 ASSERT_MGD_MTX(ifmgd);
Jiri Bencf0706e82007-05-05 11:45:53 -07001583
Ester Kummerae6a44e2008-06-27 18:54:48 +03001584 /* Process beacon from the current BSS */
1585 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1586 if (baselen > len)
1587 return;
1588
Johannes Bergd91f36d2009-04-16 13:17:26 +02001589 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07001590 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001591
Johannes Bergb291ba12009-07-10 15:29:03 +02001592 /*
1593 * We might have received a number of frames, among them a
1594 * disassoc frame and a beacon...
1595 */
1596 if (!ifmgd->associated)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001597 return;
1598
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001599 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001600
Johannes Bergb291ba12009-07-10 15:29:03 +02001601 /*
1602 * And in theory even frames from a different AP we were just
1603 * associated to a split-second ago!
1604 */
1605 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001606 return;
1607
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001608 /* Track average RSSI from the Beacon frames of the current AP */
1609 ifmgd->last_beacon_signal = rx_status->signal;
1610 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1611 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03001612 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001613 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03001614 ifmgd->count_beacon_signal = 1;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001615 } else {
1616 ifmgd->ave_beacon_signal =
1617 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1618 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1619 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03001620 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001621 }
1622 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03001623 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001624 !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1625 int sig = ifmgd->ave_beacon_signal / 16;
1626 int last_event = ifmgd->last_cqm_event_signal;
1627 int thold = bss_conf->cqm_rssi_thold;
1628 int hyst = bss_conf->cqm_rssi_hyst;
1629 if (sig < thold &&
1630 (last_event == 0 || sig < last_event - hyst)) {
1631 ifmgd->last_cqm_event_signal = sig;
1632 ieee80211_cqm_rssi_notify(
1633 &sdata->vif,
1634 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1635 GFP_KERNEL);
1636 } else if (sig > thold &&
1637 (last_event == 0 || sig > last_event + hyst)) {
1638 ifmgd->last_cqm_event_signal = sig;
1639 ieee80211_cqm_rssi_notify(
1640 &sdata->vif,
1641 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1642 GFP_KERNEL);
1643 }
1644 }
1645
Johannes Bergb291ba12009-07-10 15:29:03 +02001646 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Jouni Malinen92778182009-05-14 21:15:36 +03001647#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1648 if (net_ratelimit()) {
1649 printk(KERN_DEBUG "%s: cancelling probereq poll due "
Johannes Berg47846c92009-11-25 17:46:19 +01001650 "to a received beacon\n", sdata->name);
Jouni Malinen92778182009-05-14 21:15:36 +03001651 }
1652#endif
Johannes Bergb291ba12009-07-10 15:29:03 +02001653 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Johannes Berg4e751842009-06-10 15:16:52 +02001654 mutex_lock(&local->iflist_mtx);
1655 ieee80211_recalc_ps(local, -1);
1656 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03001657 }
1658
Johannes Bergb291ba12009-07-10 15:29:03 +02001659 /*
1660 * Push the beacon loss detection into the future since
1661 * we are processing a beacon from the AP just now.
1662 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001663 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001664
Johannes Bergd91f36d2009-04-16 13:17:26 +02001665 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1666 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1667 len - baselen, &elems,
1668 care_about_ies, ncrc);
1669
1670 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02001671 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1672 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001673
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001674 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
Jouni Malinen30196672009-05-19 17:01:43 +03001675 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1676 true);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001677
Johannes Berg4ced3f72010-07-19 16:39:04 +02001678 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jouni Malinen30196672009-05-19 17:01:43 +03001679 elems.wmm_param_len);
1680 }
Johannes Bergfe3fa822008-09-08 11:05:09 +02001681
Vivek Natarajan25c9c872009-03-02 20:20:30 +05301682 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02001683 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02001684 if (local->hw.conf.dynamic_ps_timeout > 0) {
1685 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1686 ieee80211_hw_config(local,
1687 IEEE80211_CONF_CHANGE_PS);
1688 ieee80211_send_nullfunc(local, sdata, 0);
1689 } else {
1690 local->pspolling = true;
1691
1692 /*
1693 * Here is assumed that the driver will be
1694 * able to send ps-poll frame and receive a
1695 * response even though power save mode is
1696 * enabled, but some drivers might require
1697 * to disable power save here. This needs
1698 * to be investigated.
1699 */
1700 ieee80211_send_pspoll(local, sdata);
1701 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08001702 }
1703 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001704
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001705 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03001706 return;
1707 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001708 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03001709
Johannes Berg7a5158e2008-10-08 10:59:33 +02001710 if (elems.erp_info && elems.erp_info_len >= 1) {
1711 erp_valid = true;
1712 erp_value = elems.erp_info[0];
1713 } else {
1714 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04001715 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001716 changed |= ieee80211_handle_bss_capability(sdata,
1717 le16_to_cpu(mgmt->u.beacon.capab_info),
1718 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07001719
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001720
Vasanthakumar Thiagarajan53d6f812009-02-11 22:18:49 +05301721 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001722 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001723 struct sta_info *sta;
1724 struct ieee80211_supported_band *sband;
1725 u16 ap_ht_cap_flags;
1726
1727 rcu_read_lock();
1728
Johannes Bergabe60632009-11-25 17:46:18 +01001729 sta = sta_info_get(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001730 if (WARN_ON(!sta)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001731 rcu_read_unlock();
1732 return;
1733 }
1734
1735 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1736
1737 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1738 elems.ht_cap_elem, &sta->sta.ht_cap);
1739
1740 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1741
1742 rcu_read_unlock();
1743
1744 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001745 bssid, ap_ht_cap_flags);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001746 }
1747
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001748 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001749 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301750 /* TODO: IBSS also needs this */
1751 if (elems.pwr_constr_elem)
1752 ieee80211_handle_pwr_constr(sdata,
1753 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1754 elems.pwr_constr_elem,
1755 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001756 }
1757
Johannes Berg471b3ef2007-12-28 14:32:58 +01001758 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001759}
1760
Johannes Berg1fa57d02010-06-10 10:21:32 +02001761void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1762 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001763{
Johannes Berg77fdaa12009-07-07 03:45:17 +02001764 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001765 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07001766 struct ieee80211_mgmt *mgmt;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001767 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001768 u16 fc;
1769
Jiri Bencf0706e82007-05-05 11:45:53 -07001770 rx_status = (struct ieee80211_rx_status *) skb->cb;
1771 mgmt = (struct ieee80211_mgmt *) skb->data;
1772 fc = le16_to_cpu(mgmt->frame_control);
1773
Johannes Berg77fdaa12009-07-07 03:45:17 +02001774 mutex_lock(&ifmgd->mtx);
1775
1776 if (ifmgd->associated &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001777 memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001778 switch (fc & IEEE80211_FCTL_STYPE) {
1779 case IEEE80211_STYPE_BEACON:
1780 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1781 rx_status);
1782 break;
1783 case IEEE80211_STYPE_PROBE_RESP:
Johannes Bergaf6b6372009-12-23 13:15:35 +01001784 ieee80211_rx_mgmt_probe_resp(sdata, skb);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001785 break;
1786 case IEEE80211_STYPE_DEAUTH:
Johannes Berg63f170e2009-12-23 13:15:33 +01001787 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001788 break;
1789 case IEEE80211_STYPE_DISASSOC:
1790 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1791 break;
1792 case IEEE80211_STYPE_ACTION:
Johannes Berg8b9a4e62010-05-28 15:22:58 +02001793 switch (mgmt->u.action.category) {
Johannes Berg8b9a4e62010-05-28 15:22:58 +02001794 case WLAN_CATEGORY_SPECTRUM_MGMT:
1795 ieee80211_sta_process_chanswitch(sdata,
1796 &mgmt->u.action.u.chan_switch.sw_elem,
1797 (void *)ifmgd->associated->priv,
1798 rx_status->mactime);
1799 break;
1800 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02001801 }
1802 mutex_unlock(&ifmgd->mtx);
1803
1804 switch (rma) {
1805 case RX_MGMT_NONE:
1806 /* no action */
1807 break;
1808 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02001809 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001810 break;
1811 case RX_MGMT_CFG80211_DISASSOC:
Holger Schurigce470612009-10-13 13:28:13 +02001812 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001813 break;
1814 default:
1815 WARN(1, "unexpected: %d", rma);
1816 }
Johannes Berg36b3a622010-06-10 10:21:33 +02001817 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001818 }
1819
Johannes Berg77fdaa12009-07-07 03:45:17 +02001820 mutex_unlock(&ifmgd->mtx);
1821
Johannes Berg63f170e2009-12-23 13:15:33 +01001822 if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
Johannes Bergb054b742010-06-07 21:50:07 +02001823 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
1824 struct ieee80211_local *local = sdata->local;
1825 struct ieee80211_work *wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001826
Johannes Berga1699b72010-07-30 16:46:07 +02001827 mutex_lock(&local->mtx);
Johannes Bergb054b742010-06-07 21:50:07 +02001828 list_for_each_entry(wk, &local->work_list, list) {
1829 if (wk->sdata != sdata)
1830 continue;
1831
Johannes Berge5b900d2010-07-29 16:08:55 +02001832 if (wk->type != IEEE80211_WORK_ASSOC &&
1833 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
Johannes Bergb054b742010-06-07 21:50:07 +02001834 continue;
1835
1836 if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
1837 continue;
1838 if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
1839 continue;
1840
1841 /*
1842 * Printing the message only here means we can't
1843 * spuriously print it, but it also means that it
1844 * won't be printed when the frame comes in before
1845 * we even tried to associate or in similar cases.
1846 *
1847 * Ultimately, I suspect cfg80211 should print the
1848 * messages instead.
1849 */
1850 printk(KERN_DEBUG
1851 "%s: deauthenticated from %pM (Reason: %u)\n",
1852 sdata->name, mgmt->bssid,
1853 le16_to_cpu(mgmt->u.deauth.reason_code));
1854
1855 list_del_rcu(&wk->list);
1856 free_work(wk);
1857 break;
1858 }
Johannes Berga1699b72010-07-30 16:46:07 +02001859 mutex_unlock(&local->mtx);
Johannes Bergb054b742010-06-07 21:50:07 +02001860
Johannes Berg77fdaa12009-07-07 03:45:17 +02001861 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Bergb054b742010-06-07 21:50:07 +02001862 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001863}
1864
Johannes Berg9c6bd792008-09-11 00:01:52 +02001865static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07001866{
1867 struct ieee80211_sub_if_data *sdata =
1868 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01001869 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001870 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001871
Johannes Berg5bb644a2009-05-17 11:40:42 +02001872 if (local->quiescing) {
1873 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
1874 return;
1875 }
1876
Johannes Berg64592c82010-06-10 10:21:31 +02001877 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001878}
1879
Johannes Berg1fa57d02010-06-10 10:21:32 +02001880void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02001881{
Johannes Berg60f8b392008-09-08 17:44:22 +02001882 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02001883 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02001884
Johannes Berg77fdaa12009-07-07 03:45:17 +02001885 /* then process the rest of the work */
1886 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02001887
Johannes Bergb291ba12009-07-10 15:29:03 +02001888 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1889 IEEE80211_STA_CONNECTION_POLL) &&
1890 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03001891 u8 bssid[ETH_ALEN];
1892
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001893 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Johannes Bergb291ba12009-07-10 15:29:03 +02001894 if (time_is_after_jiffies(ifmgd->probe_timeout))
1895 run_again(ifmgd, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001896
1897 else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
1898#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Ben Greearb38afa82010-10-07 16:12:06 -07001899 wiphy_debug(local->hw.wiphy,
1900 "%s: No probe response from AP %pM"
1901 " after %dms, try %d\n",
1902 sdata->name,
1903 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ,
1904 ifmgd->probe_send_count);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001905#endif
1906 ieee80211_mgd_probe_ap_send(sdata);
1907 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02001908 /*
1909 * We actually lost the connection ... or did we?
1910 * Let's make sure!
1911 */
1912 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1913 IEEE80211_STA_BEACON_POLL);
Ben Greearb38afa82010-10-07 16:12:06 -07001914 wiphy_debug(local->hw.wiphy,
1915 "%s: No probe response from AP %pM"
1916 " after %dms, disconnecting.\n",
1917 sdata->name,
1918 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
Johannes Berg53f73c02010-10-05 19:37:40 +02001919 ieee80211_set_disassoc(sdata, true, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001920 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001921 mutex_lock(&local->mtx);
1922 ieee80211_recalc_idle(local);
1923 mutex_unlock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02001924 /*
1925 * must be outside lock due to cfg80211,
1926 * but that's not a problem.
1927 */
1928 ieee80211_send_deauth_disassoc(sdata, bssid,
1929 IEEE80211_STYPE_DEAUTH,
1930 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001931 NULL, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001932 mutex_lock(&ifmgd->mtx);
1933 }
1934 }
1935
Johannes Berg77fdaa12009-07-07 03:45:17 +02001936 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02001937}
Johannes Berg0a51b272008-09-08 17:44:25 +02001938
Johannes Bergb291ba12009-07-10 15:29:03 +02001939static void ieee80211_sta_bcn_mon_timer(unsigned long data)
1940{
1941 struct ieee80211_sub_if_data *sdata =
1942 (struct ieee80211_sub_if_data *) data;
1943 struct ieee80211_local *local = sdata->local;
1944
1945 if (local->quiescing)
1946 return;
1947
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001948 ieee80211_queue_work(&sdata->local->hw,
1949 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02001950}
1951
1952static void ieee80211_sta_conn_mon_timer(unsigned long data)
1953{
1954 struct ieee80211_sub_if_data *sdata =
1955 (struct ieee80211_sub_if_data *) data;
1956 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1957 struct ieee80211_local *local = sdata->local;
1958
1959 if (local->quiescing)
1960 return;
1961
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001962 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02001963}
1964
1965static void ieee80211_sta_monitor_work(struct work_struct *work)
1966{
1967 struct ieee80211_sub_if_data *sdata =
1968 container_of(work, struct ieee80211_sub_if_data,
1969 u.mgd.monitor_work);
1970
1971 ieee80211_mgd_probe_ap(sdata, false);
1972}
1973
Johannes Berga1678f82008-09-11 00:01:47 +02001974static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
1975{
Kalle Vaload935682009-04-19 08:47:19 +03001976 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Bergb291ba12009-07-10 15:29:03 +02001977 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
1978 IEEE80211_STA_CONNECTION_POLL);
Kalle Vaload935682009-04-19 08:47:19 +03001979
Johannes Bergb291ba12009-07-10 15:29:03 +02001980 /* let's probe the connection once */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001981 ieee80211_queue_work(&sdata->local->hw,
Johannes Bergb291ba12009-07-10 15:29:03 +02001982 &sdata->u.mgd.monitor_work);
1983 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02001984 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03001985 }
Johannes Berga1678f82008-09-11 00:01:47 +02001986}
1987
Johannes Berg5bb644a2009-05-17 11:40:42 +02001988#ifdef CONFIG_PM
1989void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
1990{
1991 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1992
1993 /*
1994 * we need to use atomic bitops for the running bits
1995 * only because both timers might fire at the same
1996 * time -- the code here is properly synchronised.
1997 */
1998
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02001999 cancel_work_sync(&ifmgd->request_smps_work);
2000
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002001 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002002 if (del_timer_sync(&ifmgd->timer))
2003 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2004
2005 cancel_work_sync(&ifmgd->chswitch_work);
2006 if (del_timer_sync(&ifmgd->chswitch_timer))
2007 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002008
2009 cancel_work_sync(&ifmgd->monitor_work);
2010 /* these will just be re-established on connection */
2011 del_timer_sync(&ifmgd->conn_mon_timer);
2012 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002013}
2014
2015void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2016{
2017 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2018
2019 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2020 add_timer(&ifmgd->timer);
2021 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2022 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01002023 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau46090972010-11-23 20:28:03 +01002024 ieee80211_restart_sta_timer(sdata);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002025}
2026#endif
2027
Johannes Berg9c6bd792008-09-11 00:01:52 +02002028/* interface setup */
2029void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2030{
Johannes Berg46900292009-02-15 12:44:28 +01002031 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002032
Johannes Berg46900292009-02-15 12:44:28 +01002033 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002034 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01002035 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002036 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2037 ieee80211_beacon_connection_loss_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002038 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01002039 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02002040 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002041 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2042 (unsigned long) sdata);
2043 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2044 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002045 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05302046 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002047
Johannes Bergab1faea2009-07-01 21:41:17 +02002048 ifmgd->flags = 0;
Johannes Bergbbbdff92009-04-16 13:27:42 +02002049
Johannes Berg77fdaa12009-07-07 03:45:17 +02002050 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01002051
2052 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2053 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2054 else
2055 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002056}
2057
2058/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002059void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2060{
2061 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02002062
2063 /* Restart STA timers */
2064 rcu_read_lock();
2065 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2066 ieee80211_restart_sta_timer(sdata);
2067 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02002068}
Johannes Berg10f644a2009-04-16 13:17:25 +02002069
2070int ieee80211_max_network_latency(struct notifier_block *nb,
2071 unsigned long data, void *dummy)
2072{
2073 s32 latency_usec = (s32) data;
2074 struct ieee80211_local *local =
2075 container_of(nb, struct ieee80211_local,
2076 network_latency_notifier);
2077
2078 mutex_lock(&local->iflist_mtx);
2079 ieee80211_recalc_ps(local, latency_usec);
2080 mutex_unlock(&local->iflist_mtx);
2081
2082 return 0;
2083}
Johannes Berg77fdaa12009-07-07 03:45:17 +02002084
2085/* config hooks */
Johannes Bergaf6b6372009-12-23 13:15:35 +01002086static enum work_done_result
2087ieee80211_probe_auth_done(struct ieee80211_work *wk,
2088 struct sk_buff *skb)
2089{
2090 if (!skb) {
2091 cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
2092 return WORK_DONE_DESTROY;
2093 }
2094
2095 if (wk->type == IEEE80211_WORK_AUTH) {
2096 cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
2097 return WORK_DONE_DESTROY;
2098 }
2099
2100 mutex_lock(&wk->sdata->u.mgd.mtx);
2101 ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2102 mutex_unlock(&wk->sdata->u.mgd.mtx);
2103
2104 wk->type = IEEE80211_WORK_AUTH;
2105 wk->probe_auth.tries = 0;
2106 return WORK_DONE_REQUEUE;
2107}
2108
Johannes Berg77fdaa12009-07-07 03:45:17 +02002109int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2110 struct cfg80211_auth_request *req)
2111{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002112 const u8 *ssid;
Johannes Bergf679f652009-12-23 13:15:34 +01002113 struct ieee80211_work *wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002114 u16 auth_alg;
2115
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002116 if (req->local_state_change)
2117 return 0; /* no need to update mac80211 state */
2118
Johannes Berg77fdaa12009-07-07 03:45:17 +02002119 switch (req->auth_type) {
2120 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2121 auth_alg = WLAN_AUTH_OPEN;
2122 break;
2123 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg9dca9c42010-07-21 10:09:25 +02002124 if (IS_ERR(sdata->local->wep_tx_tfm))
2125 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002126 auth_alg = WLAN_AUTH_SHARED_KEY;
2127 break;
2128 case NL80211_AUTHTYPE_FT:
2129 auth_alg = WLAN_AUTH_FT;
2130 break;
2131 case NL80211_AUTHTYPE_NETWORK_EAP:
2132 auth_alg = WLAN_AUTH_LEAP;
2133 break;
2134 default:
2135 return -EOPNOTSUPP;
2136 }
2137
2138 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2139 if (!wk)
2140 return -ENOMEM;
2141
Joe Perches5e124bd2010-01-08 22:33:38 -08002142 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002143
2144 if (req->ie && req->ie_len) {
2145 memcpy(wk->ie, req->ie, req->ie_len);
2146 wk->ie_len = req->ie_len;
2147 }
2148
Johannes Bergfffd0932009-07-08 14:22:54 +02002149 if (req->key && req->key_len) {
Johannes Bergaf6b6372009-12-23 13:15:35 +01002150 wk->probe_auth.key_len = req->key_len;
2151 wk->probe_auth.key_idx = req->key_idx;
2152 memcpy(wk->probe_auth.key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02002153 }
2154
Johannes Berg77fdaa12009-07-07 03:45:17 +02002155 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002156 memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2157 wk->probe_auth.ssid_len = ssid[1];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002158
Johannes Bergaf6b6372009-12-23 13:15:35 +01002159 wk->probe_auth.algorithm = auth_alg;
2160 wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
Johannes Bergf679f652009-12-23 13:15:34 +01002161
Johannes Berg070bb542010-02-03 13:57:46 +01002162 /* if we already have a probe, don't probe again */
2163 if (req->bss->proberesp_ies)
2164 wk->type = IEEE80211_WORK_AUTH;
2165 else
2166 wk->type = IEEE80211_WORK_DIRECT_PROBE;
Johannes Bergf679f652009-12-23 13:15:34 +01002167 wk->chan = req->bss->channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002168 wk->sdata = sdata;
2169 wk->done = ieee80211_probe_auth_done;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002170
Johannes Bergaf6b6372009-12-23 13:15:35 +01002171 ieee80211_add_work(wk);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002172 return 0;
2173}
2174
Johannes Bergaf6b6372009-12-23 13:15:35 +01002175static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2176 struct sk_buff *skb)
2177{
2178 struct ieee80211_mgmt *mgmt;
Johannes Berge5b900d2010-07-29 16:08:55 +02002179 struct ieee80211_rx_status *rx_status;
2180 struct ieee802_11_elems elems;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002181 u16 status;
2182
2183 if (!skb) {
2184 cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
2185 return WORK_DONE_DESTROY;
2186 }
2187
Johannes Berge5b900d2010-07-29 16:08:55 +02002188 if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2189 mutex_lock(&wk->sdata->u.mgd.mtx);
2190 rx_status = (void *) skb->cb;
2191 ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2192 ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2193 &elems, true);
2194 mutex_unlock(&wk->sdata->u.mgd.mtx);
2195
2196 wk->type = IEEE80211_WORK_ASSOC;
2197 /* not really done yet */
2198 return WORK_DONE_REQUEUE;
2199 }
2200
Johannes Bergaf6b6372009-12-23 13:15:35 +01002201 mgmt = (void *)skb->data;
2202 status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2203
2204 if (status == WLAN_STATUS_SUCCESS) {
2205 mutex_lock(&wk->sdata->u.mgd.mtx);
2206 if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2207 mutex_unlock(&wk->sdata->u.mgd.mtx);
2208 /* oops -- internal error -- send timeout for now */
2209 cfg80211_send_assoc_timeout(wk->sdata->dev,
2210 wk->filter_ta);
2211 return WORK_DONE_DESTROY;
2212 }
Juuso Oikarinen68542962010-06-09 13:43:26 +03002213
2214 mutex_unlock(&wk->sdata->u.mgd.mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002215 }
2216
2217 cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
2218 return WORK_DONE_DESTROY;
2219}
2220
Johannes Berg77fdaa12009-07-07 03:45:17 +02002221int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2222 struct cfg80211_assoc_request *req)
2223{
2224 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002225 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Bergf679f652009-12-23 13:15:34 +01002226 struct ieee80211_work *wk;
Johannes Berg63f170e2009-12-23 13:15:33 +01002227 const u8 *ssid;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002228 int i;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002229
2230 mutex_lock(&ifmgd->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002231 if (ifmgd->associated) {
Jouni Malinen9c87ba62010-02-28 12:13:46 +02002232 if (!req->prev_bssid ||
2233 memcmp(req->prev_bssid, ifmgd->associated->bssid,
2234 ETH_ALEN)) {
2235 /*
2236 * We are already associated and the request was not a
2237 * reassociation request from the current BSS, so
2238 * reject it.
2239 */
2240 mutex_unlock(&ifmgd->mtx);
2241 return -EALREADY;
2242 }
2243
2244 /* Trying to reassociate - clear previous association state */
Johannes Berg53f73c02010-10-05 19:37:40 +02002245 ieee80211_set_disassoc(sdata, true, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002246 }
2247 mutex_unlock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002248
Johannes Berg63f170e2009-12-23 13:15:33 +01002249 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002250 if (!wk)
2251 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002252
Johannes Berg77fdaa12009-07-07 03:45:17 +02002253 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
Vivek Natarajan375177b2010-02-09 14:50:28 +05302254 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002255
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002256 ifmgd->beacon_crc_valid = false;
2257
Johannes Berg77fdaa12009-07-07 03:45:17 +02002258 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2259 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2260 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2261 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2262 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2263
Johannes Berg77fdaa12009-07-07 03:45:17 +02002264
2265 if (req->ie && req->ie_len) {
2266 memcpy(wk->ie, req->ie, req->ie_len);
2267 wk->ie_len = req->ie_len;
2268 } else
2269 wk->ie_len = 0;
2270
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002271 wk->assoc.bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01002272
Johannes Bergaf6b6372009-12-23 13:15:35 +01002273 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
Johannes Bergf679f652009-12-23 13:15:34 +01002274
Johannes Bergaf6b6372009-12-23 13:15:35 +01002275 /* new association always uses requested smps mode */
2276 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2277 if (ifmgd->powersave)
2278 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2279 else
2280 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2281 } else
2282 ifmgd->ap_smps = ifmgd->req_smps;
2283
2284 wk->assoc.smps = ifmgd->ap_smps;
Johannes Berg77c81442009-12-23 13:15:37 +01002285 /*
2286 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2287 * We still associate in non-HT mode (11a/b/g) if any one of these
2288 * ciphers is configured as pairwise.
2289 * We can set this to true for non-11n hardware, that'll be checked
2290 * separately along with the peer capabilities.
2291 */
Johannes Bergaf6b6372009-12-23 13:15:35 +01002292 wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
Johannes Bergf679f652009-12-23 13:15:34 +01002293 wk->assoc.capability = req->bss->capability;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002294 wk->assoc.wmm_used = bss->wmm_used;
2295 wk->assoc.supp_rates = bss->supp_rates;
2296 wk->assoc.supp_rates_len = bss->supp_rates_len;
Johannes Bergf679f652009-12-23 13:15:34 +01002297 wk->assoc.ht_information_ie =
2298 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
Johannes Berg63f170e2009-12-23 13:15:33 +01002299
Kalle Valoab133152010-01-12 10:42:31 +02002300 if (bss->wmm_used && bss->uapsd_supported &&
2301 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2302 wk->assoc.uapsd_used = true;
2303 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2304 } else {
2305 wk->assoc.uapsd_used = false;
2306 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2307 }
2308
Johannes Berg63f170e2009-12-23 13:15:33 +01002309 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Johannes Bergf679f652009-12-23 13:15:34 +01002310 memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2311 wk->assoc.ssid_len = ssid[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01002312
Johannes Berg77fdaa12009-07-07 03:45:17 +02002313 if (req->prev_bssid)
Johannes Bergf679f652009-12-23 13:15:34 +01002314 memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002315
Johannes Bergf679f652009-12-23 13:15:34 +01002316 wk->chan = req->bss->channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002317 wk->sdata = sdata;
2318 wk->done = ieee80211_assoc_done;
Johannes Berge5b900d2010-07-29 16:08:55 +02002319 if (!bss->dtim_period &&
2320 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2321 wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2322 else
2323 wk->type = IEEE80211_WORK_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002324
2325 if (req->use_mfp) {
2326 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2327 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2328 } else {
2329 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2330 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2331 }
2332
2333 if (req->crypto.control_port)
2334 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2335 else
2336 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2337
Johannes Berga621fa42010-08-27 14:26:54 +03002338 sdata->control_port_protocol = req->crypto.control_port_ethertype;
2339 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2340
Johannes Bergaf6b6372009-12-23 13:15:35 +01002341 ieee80211_add_work(wk);
2342 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002343}
2344
2345int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503dd2009-07-07 03:56:11 +02002346 struct cfg80211_deauth_request *req,
2347 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002348{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002349 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002350 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergf679f652009-12-23 13:15:34 +01002351 struct ieee80211_work *wk;
Jouni Malinen05e48e82010-06-14 11:55:56 -07002352 u8 bssid[ETH_ALEN];
2353 bool assoc_bss = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002354
Johannes Berg77fdaa12009-07-07 03:45:17 +02002355 mutex_lock(&ifmgd->mtx);
2356
Jouni Malinen05e48e82010-06-14 11:55:56 -07002357 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002358 if (ifmgd->associated == req->bss) {
Johannes Berg53f73c02010-10-05 19:37:40 +02002359 ieee80211_set_disassoc(sdata, false, true);
Johannes Berga58ce432009-11-19 12:45:42 +01002360 mutex_unlock(&ifmgd->mtx);
Jouni Malinen05e48e82010-06-14 11:55:56 -07002361 assoc_bss = true;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002362 } else {
2363 bool not_auth_yet = false;
Johannes Berga58ce432009-11-19 12:45:42 +01002364
Johannes Bergaf6b6372009-12-23 13:15:35 +01002365 mutex_unlock(&ifmgd->mtx);
2366
Johannes Berga1699b72010-07-30 16:46:07 +02002367 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002368 list_for_each_entry(wk, &local->work_list, list) {
Johannes Berg29165e42010-02-06 15:20:13 +01002369 if (wk->sdata != sdata)
Johannes Bergaf6b6372009-12-23 13:15:35 +01002370 continue;
Johannes Berg29165e42010-02-06 15:20:13 +01002371
2372 if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
Reinette Chatre79733a82010-05-04 16:04:49 -07002373 wk->type != IEEE80211_WORK_AUTH &&
Johannes Berge5b900d2010-07-29 16:08:55 +02002374 wk->type != IEEE80211_WORK_ASSOC &&
2375 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
Johannes Berg29165e42010-02-06 15:20:13 +01002376 continue;
2377
Johannes Bergaf6b6372009-12-23 13:15:35 +01002378 if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2379 continue;
Johannes Berg29165e42010-02-06 15:20:13 +01002380
2381 not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2382 list_del_rcu(&wk->list);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002383 free_work(wk);
2384 break;
2385 }
Johannes Berga1699b72010-07-30 16:46:07 +02002386 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002387
2388 /*
2389 * If somebody requests authentication and we haven't
2390 * sent out an auth frame yet there's no need to send
2391 * out a deauth frame either. If the state was PROBE,
2392 * then this is the case. If it's AUTH we have sent a
2393 * frame, and if it's IDLE we have completed the auth
2394 * process already.
2395 */
2396 if (not_auth_yet) {
2397 __cfg80211_auth_canceled(sdata->dev, bssid);
2398 return 0;
2399 }
2400 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002401
Johannes Berg0ff71612009-09-26 14:45:41 +02002402 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002403 sdata->name, bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002404
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002405 ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2406 req->reason_code, cookie,
2407 !req->local_state_change);
Jouni Malinen05e48e82010-06-14 11:55:56 -07002408 if (assoc_bss)
2409 sta_info_destroy_addr(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002410
Johannes Berg7da7cc12010-08-05 17:02:38 +02002411 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002412 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002413 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002414
Johannes Berg77fdaa12009-07-07 03:45:17 +02002415 return 0;
2416}
2417
2418int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503dd2009-07-07 03:56:11 +02002419 struct cfg80211_disassoc_request *req,
2420 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002421{
2422 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07002423 u8 bssid[ETH_ALEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002424
Johannes Berg77fdaa12009-07-07 03:45:17 +02002425 mutex_lock(&ifmgd->mtx);
2426
Johannes Berg8d8b2612009-07-25 11:58:36 +02002427 /*
2428 * cfg80211 should catch this ... but it's racy since
2429 * we can receive a disassoc frame, process it, hand it
2430 * to cfg80211 while that's in a locked section already
2431 * trying to tell us that the user wants to disconnect.
2432 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002433 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002434 mutex_unlock(&ifmgd->mtx);
2435 return -ENOLINK;
2436 }
2437
Johannes Berg0ff71612009-09-26 14:45:41 +02002438 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002439 sdata->name, req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002440
Jouni Malinene69e95d2010-03-29 23:29:31 -07002441 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg53f73c02010-10-05 19:37:40 +02002442 ieee80211_set_disassoc(sdata, false, true);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002443
2444 mutex_unlock(&ifmgd->mtx);
2445
2446 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +02002447 IEEE80211_STYPE_DISASSOC, req->reason_code,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002448 cookie, !req->local_state_change);
Jouni Malinene69e95d2010-03-29 23:29:31 -07002449 sta_info_destroy_addr(sdata, bssid);
Johannes Bergbc83b682009-11-29 12:19:06 +01002450
Johannes Berg7da7cc12010-08-05 17:02:38 +02002451 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002452 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002453 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002454
Johannes Berg77fdaa12009-07-07 03:45:17 +02002455 return 0;
2456}
Jouni Malinen026331c2010-02-15 12:53:10 +02002457
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002458void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2459 enum nl80211_cqm_rssi_threshold_event rssi_event,
2460 gfp_t gfp)
2461{
2462 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2463
Johannes Bergb5878a22010-04-07 16:48:40 +02002464 trace_api_cqm_rssi_notify(sdata, rssi_event);
2465
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002466 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2467}
2468EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);