blob: f570801514f1b1ecf57812513835179a4a3ea763 [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
Felix Fietkau72a8a3e2010-11-23 03:10:32 +010031#define IEEE80211_MAX_NULLFUNC_TRIES 2
Maxim Levitskya43abf22009-07-31 18:54:12 +030032#define IEEE80211_MAX_PROBE_TRIES 5
Johannes Bergb291ba12009-07-10 15:29:03 +020033
34/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010035 * Beacon loss timeout is calculated as N frames times the
36 * advertised beacon interval. This may need to be somewhat
37 * higher than what hardware might detect to account for
38 * delays in the host processing frames. But since we also
39 * probe on beacon miss before declaring the connection lost
40 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020041 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010042#define IEEE80211_BEACON_LOSS_COUNT 7
43
Johannes Bergb291ba12009-07-10 15:29:03 +020044/*
45 * Time the connection can be idle before we probe
46 * it to see if we can still talk to the AP.
47 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030048#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020049/*
50 * Time we wait for a probe response after sending
51 * a probe request because of beacon loss or for
52 * checking the connection still works.
53 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030054#define IEEE80211_PROBE_WAIT (HZ / 2)
Jiri Bencf0706e82007-05-05 11:45:53 -070055
Jouni Malinen17e4ec12010-03-29 23:28:30 -070056/*
57 * Weight given to the latest Beacon frame when calculating average signal
58 * strength for Beacon frames received in the current BSS. This must be
59 * between 1 and 15.
60 */
61#define IEEE80211_SIGNAL_AVE_WEIGHT 3
62
Jouni Malinen391a2002010-08-27 22:22:00 +030063/*
64 * How many Beacon frames need to have been used in average signal strength
65 * before starting to indicate signal change events.
66 */
67#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
68
Johannes Berg5bb644a2009-05-17 11:40:42 +020069#define TMR_RUNNING_TIMER 0
70#define TMR_RUNNING_CHANSW 1
71
Johannes Berg77fdaa12009-07-07 03:45:17 +020072/*
73 * All cfg80211 functions have to be called outside a locked
74 * section so that they can acquire a lock themselves... This
75 * is much simpler than queuing up things in cfg80211, but we
76 * do need some indirection for that here.
77 */
78enum rx_mgmt_action {
79 /* no action required */
80 RX_MGMT_NONE,
81
82 /* caller must call cfg80211_send_rx_auth() */
83 RX_MGMT_CFG80211_AUTH,
84
85 /* caller must call cfg80211_send_rx_assoc() */
86 RX_MGMT_CFG80211_ASSOC,
87
88 /* caller must call cfg80211_send_deauth() */
89 RX_MGMT_CFG80211_DEAUTH,
90
91 /* caller must call cfg80211_send_disassoc() */
92 RX_MGMT_CFG80211_DISASSOC,
93
Johannes Berg5d1ec852009-12-02 12:43:43 +010094 /* caller must tell cfg80211 about internal error */
95 RX_MGMT_CFG80211_ASSOC_ERROR,
Johannes Berg77fdaa12009-07-07 03:45:17 +020096};
97
Johannes Berg5484e232008-09-08 17:44:27 +020098/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +020099static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
100{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200101 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200102}
103
Johannes Bergca386f32009-07-10 02:39:48 +0200104/*
105 * We can have multiple work items (and connection probing)
106 * scheduling this timer, but we need to take care to only
107 * reschedule it when it should fire _earlier_ than it was
108 * asked for before, or if it's not pending right now. This
109 * function ensures that. Note that it then is required to
110 * run this function for all timeouts after the first one
111 * has happened -- the work that runs from this timer will
112 * do that.
113 */
114static void run_again(struct ieee80211_if_managed *ifmgd,
115 unsigned long timeout)
116{
117 ASSERT_MGD_MTX(ifmgd);
118
119 if (!timer_pending(&ifmgd->timer) ||
120 time_before(timeout, ifmgd->timer.expires))
121 mod_timer(&ifmgd->timer, timeout);
122}
123
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400124void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200125{
126 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
127 return;
128
129 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100130 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200131}
132
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400133void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
134{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400135 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
136
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400137 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
138 return;
139
140 mod_timer(&sdata->u.mgd.conn_mon_timer,
141 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400142
143 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400144}
145
Johannes Berg5484e232008-09-08 17:44:27 +0200146static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200147{
Johannes Berg5484e232008-09-08 17:44:27 +0200148 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200149}
150
Johannes Bergd5522e02009-03-30 13:23:35 +0200151/*
152 * ieee80211_enable_ht should be called only after the operating band
153 * has been determined as ht configuration depends on the hw's
154 * HT abilities for a specific band.
155 */
156static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
157 struct ieee80211_ht_info *hti,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200158 const u8 *bssid, u16 ap_ht_cap_flags)
Johannes Bergd5522e02009-03-30 13:23:35 +0200159{
160 struct ieee80211_local *local = sdata->local;
161 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200162 struct sta_info *sta;
163 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200164 u16 ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200165 bool enable_ht = true;
166 enum nl80211_channel_type prev_chantype;
Johannes Bergd5522e02009-03-30 13:23:35 +0200167 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
168
169 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
170
Johannes Berg0aaffa92010-05-05 15:28:27 +0200171 prev_chantype = sdata->vif.bss_conf.channel_type;
172
Johannes Bergd5522e02009-03-30 13:23:35 +0200173 /* HT is not supported */
174 if (!sband->ht_cap.ht_supported)
175 enable_ht = false;
176
177 /* check that channel matches the right operating channel */
178 if (local->hw.conf.channel->center_freq !=
179 ieee80211_channel_to_frequency(hti->control_chan))
180 enable_ht = false;
181
182 if (enable_ht) {
183 channel_type = NL80211_CHAN_HT20;
184
185 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
186 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
187 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
188 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
189 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400190 if (!(local->hw.conf.channel->flags &
191 IEEE80211_CHAN_NO_HT40PLUS))
192 channel_type = NL80211_CHAN_HT40PLUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200193 break;
194 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Luis R. Rodriguez768777e2009-05-02 00:37:19 -0400195 if (!(local->hw.conf.channel->flags &
196 IEEE80211_CHAN_NO_HT40MINUS))
197 channel_type = NL80211_CHAN_HT40MINUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200198 break;
199 }
200 }
201 }
202
Reinette Chatrefe6f2122010-04-19 10:46:31 -0700203 if (local->tmp_channel)
204 local->tmp_channel_type = channel_type;
Johannes Bergd5522e02009-03-30 13:23:35 +0200205
Johannes Berg0aaffa92010-05-05 15:28:27 +0200206 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
207 /* can only fail due to HT40+/- mismatch */
208 channel_type = NL80211_CHAN_HT20;
209 WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
210 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200211
Johannes Berg0aaffa92010-05-05 15:28:27 +0200212 /* channel_type change automatically detected */
213 ieee80211_hw_config(local, 0);
214
215 if (prev_chantype != channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200216 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100217 sta = sta_info_get(sdata, bssid);
Johannes Bergd5522e02009-03-30 13:23:35 +0200218 if (sta)
219 rate_control_rate_update(local, sband, sta,
Sujith4fa00432010-03-01 14:42:57 +0530220 IEEE80211_RC_HT_CHANGED,
Johannes Berg0aaffa92010-05-05 15:28:27 +0200221 channel_type);
Johannes Bergd5522e02009-03-30 13:23:35 +0200222 rcu_read_unlock();
Johannes Berg0aaffa92010-05-05 15:28:27 +0200223 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200224
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200225 ht_opmode = le16_to_cpu(hti->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200226
227 /* if bss configuration changed store the new one */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200228 if (sdata->ht_opmode_valid != enable_ht ||
229 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
230 prev_chantype != channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200231 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200232 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200233 sdata->ht_opmode_valid = enable_ht;
Johannes Bergd5522e02009-03-30 13:23:35 +0200234 }
235
236 return changed;
237}
238
Johannes Berg9c6bd792008-09-11 00:01:52 +0200239/* frame sending functions */
240
Johannes Bergef422bc2008-09-09 10:58:25 +0200241static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503d2009-07-07 03:56:11 +0200242 const u8 *bssid, u16 stype, u16 reason,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300243 void *cookie, bool send_frame)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200244{
245 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100246 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200247 struct sk_buff *skb;
248 struct ieee80211_mgmt *mgmt;
249
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200250 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
Johannes Berg9ac19a92008-09-09 10:57:09 +0200251 if (!skb) {
Johannes Bergef422bc2008-09-09 10:58:25 +0200252 printk(KERN_DEBUG "%s: failed to allocate buffer for "
Johannes Berg47846c92009-11-25 17:46:19 +0100253 "deauth/disassoc frame\n", sdata->name);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200254 return;
255 }
256 skb_reserve(skb, local->hw.extra_tx_headroom);
257
258 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
259 memset(mgmt, 0, 24);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200260 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100261 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200262 memcpy(mgmt->bssid, bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200263 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200264 skb_put(skb, 2);
Johannes Bergef422bc2008-09-09 10:58:25 +0200265 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200266 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
267
Jouni Malinen53b46b82009-03-27 20:53:56 +0200268 if (stype == IEEE80211_STYPE_DEAUTH)
Holger Schurigce470612009-10-13 13:28:13 +0200269 if (cookie)
270 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
271 else
272 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Jouni Malinen53b46b82009-03-27 20:53:56 +0200273 else
Holger Schurigce470612009-10-13 13:28:13 +0200274 if (cookie)
275 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
276 else
277 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg62ae67b2009-11-18 18:42:05 +0100278 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
279 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300280
281 if (send_frame)
282 ieee80211_tx_skb(sdata, skb);
283 else
284 kfree_skb(skb);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200285}
286
Kalle Valo572e0012009-02-10 17:09:31 +0200287void ieee80211_send_pspoll(struct ieee80211_local *local,
288 struct ieee80211_sub_if_data *sdata)
289{
Kalle Valo572e0012009-02-10 17:09:31 +0200290 struct ieee80211_pspoll *pspoll;
291 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200292
Kalle Valod8cd1892010-01-05 20:16:26 +0200293 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
294 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200295 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200296
Kalle Valod8cd1892010-01-05 20:16:26 +0200297 pspoll = (struct ieee80211_pspoll *) skb->data;
298 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200299
Johannes Berg62ae67b2009-11-18 18:42:05 +0100300 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
301 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200302}
303
Johannes Berg965beda2009-04-16 13:17:24 +0200304void ieee80211_send_nullfunc(struct ieee80211_local *local,
305 struct ieee80211_sub_if_data *sdata,
306 int powersave)
307{
308 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200309 struct ieee80211_hdr_3addr *nullfunc;
Johannes Berg965beda2009-04-16 13:17:24 +0200310
Kalle Valod8cd1892010-01-05 20:16:26 +0200311 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
312 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200313 return;
314
Kalle Valod8cd1892010-01-05 20:16:26 +0200315 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200316 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200317 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200318
Johannes Berg62ae67b2009-11-18 18:42:05 +0100319 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
320 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200321}
322
Felix Fietkaud5242152010-01-08 18:06:26 +0100323static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
324 struct ieee80211_sub_if_data *sdata)
325{
326 struct sk_buff *skb;
327 struct ieee80211_hdr *nullfunc;
328 __le16 fc;
329
330 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
331 return;
332
333 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
334 if (!skb) {
335 printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
336 "nullfunc frame\n", sdata->name);
337 return;
338 }
339 skb_reserve(skb, local->hw.extra_tx_headroom);
340
341 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
342 memset(nullfunc, 0, 30);
343 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
344 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
345 nullfunc->frame_control = fc;
346 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
347 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
348 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
349 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
350
351 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
352 ieee80211_tx_skb(sdata, skb);
353}
354
Johannes Bergcc32abd2009-05-15 11:52:31 +0200355/* spectrum management related things */
356static void ieee80211_chswitch_work(struct work_struct *work)
357{
358 struct ieee80211_sub_if_data *sdata =
359 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200360 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
361
Johannes Berg9607e6b2009-12-23 13:15:31 +0100362 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200363 return;
364
Johannes Berg77fdaa12009-07-07 03:45:17 +0200365 mutex_lock(&ifmgd->mtx);
366 if (!ifmgd->associated)
367 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200368
369 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200370 if (!sdata->local->ops->channel_switch) {
371 /* call "hw_config" only if doing sw channel switch */
372 ieee80211_hw_config(sdata->local,
373 IEEE80211_CONF_CHANGE_CHANNEL);
374 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200375
Johannes Berg77fdaa12009-07-07 03:45:17 +0200376 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100377 ifmgd->associated->channel = sdata->local->oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200378
Johannes Bergcc32abd2009-05-15 11:52:31 +0200379 ieee80211_wake_queues_by_reason(&sdata->local->hw,
380 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200381 out:
382 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
383 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200384}
385
Johannes Berg5ce6e432010-05-11 16:20:57 +0200386void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
387{
388 struct ieee80211_sub_if_data *sdata;
389 struct ieee80211_if_managed *ifmgd;
390
391 sdata = vif_to_sdata(vif);
392 ifmgd = &sdata->u.mgd;
393
394 trace_api_chswitch_done(sdata, success);
395 if (!success) {
396 /*
397 * If the channel switch was not successful, stay
398 * around on the old channel. We currently lack
399 * good handling of this situation, possibly we
400 * should just drop the association.
401 */
402 sdata->local->csa_channel = sdata->local->oper_channel;
403 }
404
405 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
406}
407EXPORT_SYMBOL(ieee80211_chswitch_done);
408
Johannes Bergcc32abd2009-05-15 11:52:31 +0200409static void ieee80211_chswitch_timer(unsigned long data)
410{
411 struct ieee80211_sub_if_data *sdata =
412 (struct ieee80211_sub_if_data *) data;
413 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
414
Johannes Berg5bb644a2009-05-17 11:40:42 +0200415 if (sdata->local->quiescing) {
416 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
417 return;
418 }
419
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400420 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200421}
422
423void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
424 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200425 struct ieee80211_bss *bss,
426 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200427{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100428 struct cfg80211_bss *cbss =
429 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200430 struct ieee80211_channel *new_ch;
431 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
432 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
433
Johannes Berg77fdaa12009-07-07 03:45:17 +0200434 ASSERT_MGD_MTX(ifmgd);
435
436 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200437 return;
438
Helmut Schaafbe9c422009-07-23 12:14:04 +0200439 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200440 return;
441
442 /* Disregard subsequent beacons if we are already running a timer
443 processing a CSA */
444
445 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
446 return;
447
448 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
449 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
450 return;
451
452 sdata->local->csa_channel = new_ch;
453
Johannes Berg5ce6e432010-05-11 16:20:57 +0200454 if (sdata->local->ops->channel_switch) {
455 /* use driver's channel switch callback */
456 struct ieee80211_channel_switch ch_switch;
457 memset(&ch_switch, 0, sizeof(ch_switch));
458 ch_switch.timestamp = timestamp;
459 if (sw_elem->mode) {
460 ch_switch.block_tx = true;
461 ieee80211_stop_queues_by_reason(&sdata->local->hw,
462 IEEE80211_QUEUE_STOP_REASON_CSA);
463 }
464 ch_switch.channel = new_ch;
465 ch_switch.count = sw_elem->count;
466 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
467 drv_channel_switch(sdata->local, &ch_switch);
468 return;
469 }
470
471 /* channel switch handled in software */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200472 if (sw_elem->count <= 1) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400473 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200474 } else {
Wey-Yi Guy9feaddc2010-05-05 20:34:02 -0700475 if (sw_elem->mode)
476 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200477 IEEE80211_QUEUE_STOP_REASON_CSA);
478 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
479 mod_timer(&ifmgd->chswitch_timer,
480 jiffies +
481 msecs_to_jiffies(sw_elem->count *
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100482 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200483 }
484}
485
486static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
487 u16 capab_info, u8 *pwr_constr_elem,
488 u8 pwr_constr_elem_len)
489{
490 struct ieee80211_conf *conf = &sdata->local->hw.conf;
491
492 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
493 return;
494
495 /* Power constraint IE length should be 1 octet */
496 if (pwr_constr_elem_len != 1)
497 return;
498
499 if ((*pwr_constr_elem <= conf->channel->max_power) &&
500 (*pwr_constr_elem != sdata->local->power_constr_level)) {
501 sdata->local->power_constr_level = *pwr_constr_elem;
502 ieee80211_hw_config(sdata->local, 0);
503 }
504}
505
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300506void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
507{
508 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
509 struct ieee80211_local *local = sdata->local;
510 struct ieee80211_conf *conf = &local->hw.conf;
511
512 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
513 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
514 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
515
516 local->disable_dynamic_ps = false;
517 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
518}
519EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
520
521void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
522{
523 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
524 struct ieee80211_local *local = sdata->local;
525 struct ieee80211_conf *conf = &local->hw.conf;
526
527 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
528 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
529 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
530
531 local->disable_dynamic_ps = true;
532 conf->dynamic_ps_timeout = 0;
533 del_timer_sync(&local->dynamic_ps_timer);
534 ieee80211_queue_work(&local->hw,
535 &local->dynamic_ps_enable_work);
536}
537EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
538
Johannes Berg965beda2009-04-16 13:17:24 +0200539/* powersave */
540static void ieee80211_enable_ps(struct ieee80211_local *local,
541 struct ieee80211_sub_if_data *sdata)
542{
543 struct ieee80211_conf *conf = &local->hw.conf;
544
Johannes Bergd5edaed2009-04-22 23:02:51 +0200545 /*
546 * If we are scanning right now then the parameters will
547 * take effect when scan finishes.
548 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200549 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200550 return;
551
Johannes Berg965beda2009-04-16 13:17:24 +0200552 if (conf->dynamic_ps_timeout > 0 &&
553 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
554 mod_timer(&local->dynamic_ps_timer, jiffies +
555 msecs_to_jiffies(conf->dynamic_ps_timeout));
556 } else {
557 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
558 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530559
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200560 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
561 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
562 return;
563
564 conf->flags |= IEEE80211_CONF_PS;
565 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200566 }
567}
568
569static void ieee80211_change_ps(struct ieee80211_local *local)
570{
571 struct ieee80211_conf *conf = &local->hw.conf;
572
573 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200574 ieee80211_enable_ps(local, local->ps_sdata);
575 } else if (conf->flags & IEEE80211_CONF_PS) {
576 conf->flags &= ~IEEE80211_CONF_PS;
577 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
578 del_timer_sync(&local->dynamic_ps_timer);
579 cancel_work_sync(&local->dynamic_ps_enable_work);
580 }
581}
582
583/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200584void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200585{
586 struct ieee80211_sub_if_data *sdata, *found = NULL;
587 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300588 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +0200589
590 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
591 local->ps_sdata = NULL;
592 return;
593 }
594
Johannes Bergaf6b6372009-12-23 13:15:35 +0100595 if (!list_empty(&local->work_list)) {
596 local->ps_sdata = NULL;
597 goto change;
598 }
599
Johannes Berg965beda2009-04-16 13:17:24 +0200600 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100601 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +0200602 continue;
603 if (sdata->vif.type != NL80211_IFTYPE_STATION)
604 continue;
605 found = sdata;
606 count++;
607 }
608
Luis R. Rodriguez43f78532009-06-10 15:16:15 +0200609 if (count == 1 && found->u.mgd.powersave &&
Johannes Bergaf6b6372009-12-23 13:15:35 +0100610 found->u.mgd.associated &&
Johannes Berg56007a02010-01-26 14:19:52 +0100611 found->u.mgd.associated->beacon_ies &&
Johannes Bergb291ba12009-07-10 15:29:03 +0200612 !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
613 IEEE80211_STA_CONNECTION_POLL))) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300614 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +0200615 s32 beaconint_us;
616
617 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +0200618 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +0200619
620 beaconint_us = ieee80211_tu_to_usec(
621 found->vif.bss_conf.beacon_int);
622
Juuso Oikarinenff616382010-06-09 09:51:52 +0300623 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300624 if (timeout < 0) {
625 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +0300626 * Go to full PSM if the user configures a very low
627 * latency requirement.
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300628 * The 2 second value is there for compatibility until
629 * the PM_QOS_NETWORK_LATENCY is configured with real
630 * values.
631 */
Juuso Oikarinenff616382010-06-09 09:51:52 +0300632 if (latency > 1900000000 && latency != 2000000000)
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300633 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +0300634 else
635 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300636 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300637 local->dynamic_ps_user_timeout = timeout;
638 if (!local->disable_dynamic_ps)
639 conf->dynamic_ps_timeout =
640 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300641
Johannes Berg04fe2032009-04-22 18:44:37 +0200642 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +0200643 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +0200644 } else {
Johannes Berg56007a02010-01-26 14:19:52 +0100645 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +0200646 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +0100647 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +0200648
Johannes Berg56007a02010-01-26 14:19:52 +0100649 bss = (void *)found->u.mgd.associated->priv;
650 dtimper = bss->dtim_period;
651
652 /* If the TIM IE is invalid, pretend the value is 1 */
653 if (!dtimper)
654 dtimper = 1;
655 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +0200656 maxslp = min_t(int, dtimper,
657 latency / beaconint_us);
658
Johannes Berg9ccebe62009-04-23 10:32:36 +0200659 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +0100660 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +0200661 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +0200662 }
Johannes Berg10f644a2009-04-16 13:17:25 +0200663 } else {
Johannes Berg965beda2009-04-16 13:17:24 +0200664 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +0200665 }
Johannes Berg965beda2009-04-16 13:17:24 +0200666
Johannes Bergaf6b6372009-12-23 13:15:35 +0100667 change:
Johannes Berg965beda2009-04-16 13:17:24 +0200668 ieee80211_change_ps(local);
669}
670
671void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
672{
673 struct ieee80211_local *local =
674 container_of(work, struct ieee80211_local,
675 dynamic_ps_disable_work);
676
677 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
678 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
679 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
680 }
681
682 ieee80211_wake_queues_by_reason(&local->hw,
683 IEEE80211_QUEUE_STOP_REASON_PS);
684}
685
686void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
687{
688 struct ieee80211_local *local =
689 container_of(work, struct ieee80211_local,
690 dynamic_ps_enable_work);
691 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Vivek Natarajan375177b2010-02-09 14:50:28 +0530692 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200693
694 /* can only happen when PS was just disabled anyway */
695 if (!sdata)
696 return;
697
698 if (local->hw.conf.flags & IEEE80211_CONF_PS)
699 return;
700
Vivek Natarajan375177b2010-02-09 14:50:28 +0530701 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
702 (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
Johannes Berg965beda2009-04-16 13:17:24 +0200703 ieee80211_send_nullfunc(local, sdata, 1);
704
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200705 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
706 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +0530707 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
708 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
709 local->hw.conf.flags |= IEEE80211_CONF_PS;
710 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
711 }
Johannes Berg965beda2009-04-16 13:17:24 +0200712}
713
714void ieee80211_dynamic_ps_timer(unsigned long data)
715{
716 struct ieee80211_local *local = (void *) data;
717
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -0700718 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +0200719 return;
720
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400721 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +0200722}
723
Johannes Berg60f8b392008-09-08 17:44:22 +0200724/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200725static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +0200726 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700727 u8 *wmm_param, size_t wmm_param_len)
728{
Jiri Bencf0706e82007-05-05 11:45:53 -0700729 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +0200730 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -0700731 size_t left;
732 int count;
Kalle Valoab133152010-01-12 10:42:31 +0200733 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700734
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200735 if (!local->ops->conf_tx)
736 return;
737
Johannes Bergaf6b6372009-12-23 13:15:35 +0100738 if (local->hw.queues < 4)
Johannes Berg3434fbd2008-05-03 00:59:37 +0200739 return;
740
741 if (!wmm_param)
742 return;
743
Jiri Bencf0706e82007-05-05 11:45:53 -0700744 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
745 return;
Kalle Valoab133152010-01-12 10:42:31 +0200746
747 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Kalle Valo50ae0cf2010-01-12 10:42:39 +0200748 uapsd_queues = local->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +0200749
Jiri Bencf0706e82007-05-05 11:45:53 -0700750 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +0100751 if (count == ifmgd->wmm_last_param_set)
Jiri Bencf0706e82007-05-05 11:45:53 -0700752 return;
Johannes Berg46900292009-02-15 12:44:28 +0100753 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -0700754
755 pos = wmm_param + 8;
756 left = wmm_param_len - 8;
757
758 memset(&params, 0, sizeof(params));
759
Jiri Bencf0706e82007-05-05 11:45:53 -0700760 local->wmm_acm = 0;
761 for (; left >= 4; left -= 4, pos += 4) {
762 int aci = (pos[0] >> 5) & 0x03;
763 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +0200764 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700765 int queue;
766
767 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200768 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +0200769 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200770 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200771 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +0200772 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
773 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700774 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200775 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +0200776 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200777 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200778 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +0200779 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
780 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700781 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200782 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +0200783 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200784 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200785 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +0200786 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
787 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700788 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200789 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -0700790 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200791 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200792 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200793 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +0200794 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
795 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700796 break;
797 }
798
799 params.aifs = pos[0] & 0x0f;
800 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
801 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200802 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +0200803 params.uapsd = uapsd;
804
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200805#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700806 wiphy_debug(local->hw.wiphy,
807 "WMM queue=%d aci=%d acm=%d aifs=%d "
808 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
809 queue, aci, acm,
810 params.aifs, params.cw_min, params.cw_max,
811 params.txop, params.uapsd);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100812#endif
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200813 if (drv_conf_tx(local, queue, &params))
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700814 wiphy_debug(local->hw.wiphy,
815 "failed to set TX queue parameters for queue %d\n",
816 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700817 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200818
819 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +0200820 sdata->vif.bss_conf.qos = true;
821 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
Jiri Bencf0706e82007-05-05 11:45:53 -0700822}
823
Johannes Berg7a5158e2008-10-08 10:59:33 +0200824static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
825 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +0200826{
Johannes Bergbda39332008-10-11 01:51:51 +0200827 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100828 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +0200829 bool use_protection;
830 bool use_short_preamble;
831 bool use_short_slot;
832
833 if (erp_valid) {
834 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
835 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
836 } else {
837 use_protection = false;
838 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
839 }
840
841 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Felix Fietkau43d35342010-01-15 03:00:48 +0100842 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
843 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +0200844
Johannes Berg471b3ef2007-12-28 14:32:58 +0100845 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +0100846 bss_conf->use_cts_prot = use_protection;
847 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200848 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200849
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200850 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200851 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100852 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200853 }
Daniel Draked9430a32007-07-27 15:43:24 +0200854
Johannes Berg7a5158e2008-10-08 10:59:33 +0200855 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +0200856 bss_conf->use_short_slot = use_short_slot;
857 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -0400858 }
859
860 return changed;
861}
862
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200863static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100864 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +0200865 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700866{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100867 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100868 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +0300869 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400870
Johannes Bergae5eb022008-10-14 16:58:37 +0200871 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200872 /* set timing information */
Juuso Oikarinen68542962010-06-09 13:43:26 +0300873 bss_conf->beacon_int = cbss->beacon_interval;
874 bss_conf->timestamp = cbss->tsf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400875
Johannes Berg77fdaa12009-07-07 03:45:17 +0200876 bss_info_changed |= BSS_CHANGED_BEACON_INT;
877 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100878 cbss->capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700879
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100880 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
881 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
882
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100883 sdata->u.mgd.associated = cbss;
884 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100885
Jouni Malinen17e4ec12010-03-29 23:28:30 -0700886 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
887
Johannes Bergb291ba12009-07-10 15:29:03 +0200888 /* just to be sure */
889 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
890 IEEE80211_STA_BEACON_POLL);
891
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200892 ieee80211_led_assoc(local, 1);
893
Johannes Berge5b900d2010-07-29 16:08:55 +0200894 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
895 bss_conf->dtim_period = bss->dtim_period;
896 else
897 bss_conf->dtim_period = 0;
898
Juuso Oikarinen68542962010-06-09 13:43:26 +0300899 bss_conf->assoc = 1;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200900 /*
901 * For now just always ask the driver to update the basic rateset
902 * when we have associated, we aren't checking whether it actually
903 * changed or not.
904 */
Johannes Bergae5eb022008-10-14 16:58:37 +0200905 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
Johannes Berg9cef8732009-05-14 13:10:14 +0200906
907 /* And the BSSID changed - we're associated now */
908 bss_info_changed |= BSS_CHANGED_BSSID;
909
Juuso Oikarinena97c13c2010-03-23 09:02:34 +0200910 /* Tell the driver to monitor connection quality (if supported) */
911 if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
Juuso Oikarinen68542962010-06-09 13:43:26 +0300912 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +0200913 bss_info_changed |= BSS_CHANGED_CQM;
914
Juuso Oikarinen68542962010-06-09 13:43:26 +0300915 /* Enable ARP filtering */
916 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
917 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
918 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
919 }
920
Johannes Bergae5eb022008-10-14 16:58:37 +0200921 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300922
Johannes Berg056508d2009-07-30 21:43:55 +0200923 mutex_lock(&local->iflist_mtx);
924 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +0200925 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +0200926 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +0200927
John W. Linville8a5b33f2010-01-06 15:39:39 -0500928 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200929 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700930}
931
Jouni Malinene69e95d2010-03-29 23:29:31 -0700932static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg53f73c02010-10-05 19:37:40 +0200933 bool remove_sta, bool tx)
Tomas Winkleraa458d12008-09-09 00:32:12 +0300934{
Johannes Berg46900292009-02-15 12:44:28 +0100935 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +0300936 struct ieee80211_local *local = sdata->local;
937 struct sta_info *sta;
Kalle Valoe0cb6862008-12-18 23:35:13 +0200938 u32 changed = 0, config_changed = 0;
Johannes Bergb291ba12009-07-10 15:29:03 +0200939 u8 bssid[ETH_ALEN];
Tomas Winkleraa458d12008-09-09 00:32:12 +0300940
Johannes Berg77fdaa12009-07-07 03:45:17 +0200941 ASSERT_MGD_MTX(ifmgd);
942
Johannes Bergb291ba12009-07-10 15:29:03 +0200943 if (WARN_ON(!ifmgd->associated))
944 return;
945
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100946 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Johannes Bergb291ba12009-07-10 15:29:03 +0200947
Johannes Berg77fdaa12009-07-07 03:45:17 +0200948 ifmgd->associated = NULL;
949 memset(ifmgd->bssid, 0, ETH_ALEN);
950
951 /*
952 * we need to commit the associated = NULL change because the
953 * scan code uses that to determine whether this iface should
954 * go to/wake up from powersave or not -- and could otherwise
955 * wake the queues erroneously.
956 */
957 smp_mb();
958
959 /*
960 * Thus, we can only afterwards stop the queues -- to account
961 * for the case where another CPU is finishing a scan at this
962 * time -- we don't want the scan code to enable queues.
963 */
Tomas Winkleraa458d12008-09-09 00:32:12 +0300964
John W. Linville8a5b33f2010-01-06 15:39:39 -0500965 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300966 netif_carrier_off(sdata->dev);
967
Johannes Berg2a419052010-06-10 10:21:29 +0200968 mutex_lock(&local->sta_mtx);
Johannes Bergabe60632009-11-25 17:46:18 +0100969 sta = sta_info_get(sdata, bssid);
Sujith4cad6c72010-02-10 14:52:21 +0530970 if (sta) {
Johannes Berg2a419052010-06-10 10:21:29 +0200971 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +0200972 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +0530973 }
Johannes Berg2a419052010-06-10 10:21:29 +0200974 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300975
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200976 changed |= ieee80211_reset_erp_info(sdata);
977
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200978 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +0200979 changed |= BSS_CHANGED_ASSOC;
980 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200981
Johannes Bergaa837e12009-05-07 16:16:24 +0200982 ieee80211_set_wmm_default(sdata);
983
Johannes Berg47979382009-01-07 10:13:27 +0100984 /* channel(_type) changes are handled by ieee80211_hw_config */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200985 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergae5eb022008-10-14 16:58:37 +0200986
Johannes Berg413ad502009-05-08 21:21:06 +0200987 /* on the next assoc, re-program HT parameters */
988 sdata->ht_opmode_valid = false;
989
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +0530990 local->power_constr_level = 0;
991
Kalle Valo520eb822008-12-18 23:35:27 +0200992 del_timer_sync(&local->dynamic_ps_timer);
993 cancel_work_sync(&local->dynamic_ps_enable_work);
994
Kalle Valoe0cb6862008-12-18 23:35:13 +0200995 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
996 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
997 config_changed |= IEEE80211_CONF_CHANGE_PS;
998 }
999
1000 ieee80211_hw_config(local, config_changed);
Johannes Berg9cef8732009-05-14 13:10:14 +02001001
Juuso Oikarinen68542962010-06-09 13:43:26 +03001002 /* Disable ARP filtering */
1003 if (sdata->vif.bss_conf.arp_filter_enabled) {
1004 sdata->vif.bss_conf.arp_filter_enabled = false;
1005 changed |= BSS_CHANGED_ARP_FILTER;
1006 }
1007
Johannes Berg0aaffa92010-05-05 15:28:27 +02001008 /* The BSSID (not really interesting) and HT changed */
1009 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001010 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001011
Jouni Malinene69e95d2010-03-29 23:29:31 -07001012 if (remove_sta)
1013 sta_info_destroy_addr(sdata, bssid);
Johannes Bergb9dcf712010-08-27 12:35:54 +02001014
1015 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1016 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1017 del_timer_sync(&sdata->u.mgd.timer);
1018 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001019}
Jiri Bencf0706e82007-05-05 11:45:53 -07001020
Kalle Valo3cf335d2009-03-22 21:57:06 +02001021void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1022 struct ieee80211_hdr *hdr)
1023{
1024 /*
1025 * We can postpone the mgd.timer whenever receiving unicast frames
1026 * from AP because we know that the connection is working both ways
1027 * at that time. But multicast frames (and hence also beacons) must
1028 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001029 * data idle periods for sending the periodic probe request to the
1030 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001031 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001032 if (is_multicast_ether_addr(hdr->addr1))
1033 return;
1034
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001035 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001036}
Jiri Bencf0706e82007-05-05 11:45:53 -07001037
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001038static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1039{
1040 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1041
1042 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1043 IEEE80211_STA_CONNECTION_POLL)))
1044 return;
1045
1046 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1047 IEEE80211_STA_BEACON_POLL);
1048 mutex_lock(&sdata->local->iflist_mtx);
1049 ieee80211_recalc_ps(sdata->local, -1);
1050 mutex_unlock(&sdata->local->iflist_mtx);
1051
1052 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1053 return;
1054
1055 /*
1056 * We've received a probe response, but are not sure whether
1057 * we have or will be receiving any beacons or data, so let's
1058 * schedule the timers again, just in case.
1059 */
1060 ieee80211_sta_reset_beacon_monitor(sdata);
1061
1062 mod_timer(&ifmgd->conn_mon_timer,
1063 round_jiffies_up(jiffies +
1064 IEEE80211_CONNECTION_IDLE_TIME));
1065}
1066
1067void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1068 struct ieee80211_hdr *hdr)
1069{
Felix Fietkau75706d02010-12-02 21:01:07 +01001070 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001071 return;
1072
1073 ieee80211_sta_reset_conn_monitor(sdata);
1074
1075 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1076 sdata->u.mgd.probe_send_count > 0) {
1077 sdata->u.mgd.probe_send_count = 0;
1078 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1079 }
1080}
1081
Maxim Levitskya43abf22009-07-31 18:54:12 +03001082static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1083{
1084 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1085 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001086 u8 *dst = ifmgd->associated->bssid;
1087 u8 unicast_limit = max(1, IEEE80211_MAX_PROBE_TRIES - 3);
1088
1089 /*
1090 * Try sending broadcast probe requests for the last three
1091 * probe requests after the first ones failed since some
1092 * buggy APs only support broadcast probe requests.
1093 */
1094 if (ifmgd->probe_send_count >= unicast_limit)
1095 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001096
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001097 /*
1098 * When the hardware reports an accurate Tx ACK status, it's
1099 * better to send a nullfunc frame instead of a probe request,
1100 * as it will kick us off the AP quickly if we aren't associated
1101 * anymore. The timeout will be reset if the frame is ACKed by
1102 * the AP.
1103 */
1104 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1105 ieee80211_send_nullfunc(sdata->local, sdata, 0);
1106 else {
1107 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1108 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
1109 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001110
1111 ifmgd->probe_send_count++;
1112 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1113 run_again(ifmgd, ifmgd->probe_timeout);
1114}
1115
Johannes Bergb291ba12009-07-10 15:29:03 +02001116static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1117 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001118{
Kalle Valo04de8382009-03-22 21:57:35 +02001119 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001120 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001121
Johannes Berg9607e6b2009-12-23 13:15:31 +01001122 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001123 return;
1124
Luis R. Rodriguez91a3bd72009-07-23 16:37:47 -07001125 if (sdata->local->scanning)
1126 return;
1127
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001128 if (sdata->local->tmp_channel)
1129 return;
1130
Johannes Berg77fdaa12009-07-07 03:45:17 +02001131 mutex_lock(&ifmgd->mtx);
1132
1133 if (!ifmgd->associated)
1134 goto out;
1135
Michael Buescha8604022009-04-15 14:41:22 -04001136#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergb291ba12009-07-10 15:29:03 +02001137 if (beacon && net_ratelimit())
1138 printk(KERN_DEBUG "%s: detected beacon loss from AP "
Johannes Berg47846c92009-11-25 17:46:19 +01001139 "- sending probe request\n", sdata->name);
Michael Buescha8604022009-04-15 14:41:22 -04001140#endif
Kalle Valo04de8382009-03-22 21:57:35 +02001141
Johannes Bergb291ba12009-07-10 15:29:03 +02001142 /*
1143 * The driver/our work has already reported this event or the
1144 * connection monitoring has kicked in and we have already sent
1145 * a probe request. Or maybe the AP died and the driver keeps
1146 * reporting until we disassociate...
1147 *
1148 * In either case we have to ignore the current call to this
1149 * function (except for setting the correct probe reason bit)
1150 * because otherwise we would reset the timer every time and
1151 * never check whether we received a probe response!
1152 */
1153 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1154 IEEE80211_STA_CONNECTION_POLL))
1155 already = true;
1156
1157 if (beacon)
1158 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1159 else
1160 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1161
1162 if (already)
1163 goto out;
1164
Johannes Berg4e751842009-06-10 15:16:52 +02001165 mutex_lock(&sdata->local->iflist_mtx);
1166 ieee80211_recalc_ps(sdata->local, -1);
1167 mutex_unlock(&sdata->local->iflist_mtx);
1168
Maxim Levitskya43abf22009-07-31 18:54:12 +03001169 ifmgd->probe_send_count = 0;
1170 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001171 out:
1172 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001173}
1174
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001175struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1176 struct ieee80211_vif *vif)
1177{
1178 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1179 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1180 struct sk_buff *skb;
1181 const u8 *ssid;
1182
1183 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1184 return NULL;
1185
1186 ASSERT_MGD_MTX(ifmgd);
1187
1188 if (!ifmgd->associated)
1189 return NULL;
1190
1191 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1192 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
1193 ssid + 2, ssid[1], NULL, 0);
1194
1195 return skb;
1196}
1197EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1198
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001199static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1200{
1201 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1202 struct ieee80211_local *local = sdata->local;
1203 u8 bssid[ETH_ALEN];
1204
1205 mutex_lock(&ifmgd->mtx);
1206 if (!ifmgd->associated) {
1207 mutex_unlock(&ifmgd->mtx);
1208 return;
1209 }
1210
1211 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1212
1213 printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
1214
Johannes Berg53f73c02010-10-05 19:37:40 +02001215 ieee80211_set_disassoc(sdata, true, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001216 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001217
1218 mutex_lock(&local->mtx);
1219 ieee80211_recalc_idle(local);
1220 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001221 /*
1222 * must be outside lock due to cfg80211,
1223 * but that's not a problem.
1224 */
1225 ieee80211_send_deauth_disassoc(sdata, bssid,
1226 IEEE80211_STYPE_DEAUTH,
1227 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001228 NULL, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001229}
1230
1231void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001232{
1233 struct ieee80211_sub_if_data *sdata =
1234 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001235 u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02001236
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001237 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1238 __ieee80211_connection_loss(sdata);
1239 else
1240 ieee80211_mgd_probe_ap(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001241}
1242
Kalle Valo04de8382009-03-22 21:57:35 +02001243void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1244{
1245 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001246 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001247
Johannes Bergb5878a22010-04-07 16:48:40 +02001248 trace_api_beacon_loss(sdata);
1249
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001250 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1251 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001252}
1253EXPORT_SYMBOL(ieee80211_beacon_loss);
1254
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001255void ieee80211_connection_loss(struct ieee80211_vif *vif)
1256{
1257 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1258 struct ieee80211_hw *hw = &sdata->local->hw;
1259
Johannes Bergb5878a22010-04-07 16:48:40 +02001260 trace_api_connection_loss(sdata);
1261
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001262 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1263 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1264}
1265EXPORT_SYMBOL(ieee80211_connection_loss);
1266
1267
Johannes Berg77fdaa12009-07-07 03:45:17 +02001268static enum rx_mgmt_action __must_check
1269ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001270 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001271{
Johannes Berg46900292009-02-15 12:44:28 +01001272 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001273 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001274 u16 reason_code;
1275
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001276 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001277 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001278
Johannes Berg77fdaa12009-07-07 03:45:17 +02001279 ASSERT_MGD_MTX(ifmgd);
1280
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001281 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001282
1283 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1284
Johannes Berg77fdaa12009-07-07 03:45:17 +02001285 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001286 sdata->name, bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001287
Johannes Berg53f73c02010-10-05 19:37:40 +02001288 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001289 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001290 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001291 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001292
1293 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001294}
1295
1296
Johannes Berg77fdaa12009-07-07 03:45:17 +02001297static enum rx_mgmt_action __must_check
1298ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1299 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001300{
Johannes Berg46900292009-02-15 12:44:28 +01001301 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001302 u16 reason_code;
1303
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001304 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001305 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001306
Johannes Berg77fdaa12009-07-07 03:45:17 +02001307 ASSERT_MGD_MTX(ifmgd);
1308
1309 if (WARN_ON(!ifmgd->associated))
1310 return RX_MGMT_NONE;
1311
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001312 if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001313 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001314
1315 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1316
Johannes Berg0ff71612009-09-26 14:45:41 +02001317 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001318 sdata->name, mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001319
Johannes Berg53f73c02010-10-05 19:37:40 +02001320 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001321 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001322 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001323 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001324 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001325}
1326
1327
Johannes Bergaf6b6372009-12-23 13:15:35 +01001328static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1329 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001330{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001331 struct ieee80211_sub_if_data *sdata = wk->sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001332 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001333 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001334 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001335 struct sta_info *sta;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001336 struct cfg80211_bss *cbss = wk->assoc.bss;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001337 u8 *pos;
Johannes Berg881d9482009-01-21 15:13:48 +01001338 u32 rates, basic_rates;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001339 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001340 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02001341 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02001342 u32 changed = 0;
Johannes Berg5d1ec852009-12-02 12:43:43 +01001343 int i, j, err;
1344 bool have_higher_than_11mbit = false;
Johannes Bergae5eb022008-10-14 16:58:37 +02001345 u16 ap_ht_cap_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07001346
Johannes Bergaf6b6372009-12-23 13:15:35 +01001347 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07001348
Jiri Bencf0706e82007-05-05 11:45:53 -07001349 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001350 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07001351
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001352 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1353 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Johannes Berg47846c92009-11-25 17:46:19 +01001354 "set\n", sdata->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001355 aid &= ~(BIT(15) | BIT(14));
1356
Johannes Bergaf6b6372009-12-23 13:15:35 +01001357 pos = mgmt->u.assoc_resp.variable;
1358 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1359
Jiri Bencf0706e82007-05-05 11:45:53 -07001360 if (!elems.supp_rates) {
1361 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001362 sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001363 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001364 }
1365
Johannes Berg46900292009-02-15 12:44:28 +01001366 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001367
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001368 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001369 if (!sta) {
Johannes Berg5d1ec852009-12-02 12:43:43 +01001370 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1371 " the AP\n", sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001372 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001373 }
1374
Johannes Berg5d1ec852009-12-02 12:43:43 +01001375 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1376 WLAN_STA_ASSOC_AP);
1377 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1378 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1379
Jiri Bencf0706e82007-05-05 11:45:53 -07001380 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001381 basic_rates = 0;
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001382 sband = local->hw.wiphy->bands[wk->chan->band];
Johannes Berg8318d782008-01-24 19:38:38 +01001383
Jiri Bencf0706e82007-05-05 11:45:53 -07001384 for (i = 0; i < elems.supp_rates_len; i++) {
1385 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Tomas Winklerd61272c2008-10-30 17:08:08 +02001386 bool is_basic = !!(elems.supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001387
1388 if (rate > 110)
1389 have_higher_than_11mbit = true;
1390
1391 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001392 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001393 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001394 if (is_basic)
1395 basic_rates |= BIT(j);
1396 break;
1397 }
Johannes Berg8318d782008-01-24 19:38:38 +01001398 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001399 }
Johannes Berg8318d782008-01-24 19:38:38 +01001400
Jiri Bencf0706e82007-05-05 11:45:53 -07001401 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1402 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg7e0986c2009-04-19 13:22:11 +02001403 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001404
1405 if (rate > 110)
1406 have_higher_than_11mbit = true;
1407
1408 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001409 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001410 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001411 if (is_basic)
1412 basic_rates |= BIT(j);
1413 break;
1414 }
Johannes Berg8318d782008-01-24 19:38:38 +01001415 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001416 }
Johannes Berg8318d782008-01-24 19:38:38 +01001417
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001418 sta->sta.supp_rates[wk->chan->band] = rates;
Johannes Bergbda39332008-10-11 01:51:51 +02001419 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01001420
1421 /* cf. IEEE 802.11 9.2.12 */
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001422 if (wk->chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg8318d782008-01-24 19:38:38 +01001423 have_higher_than_11mbit)
1424 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1425 else
1426 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001427
Johannes Bergab1faea2009-07-01 21:41:17 +02001428 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001429 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001430 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02001431
1432 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001433
Johannes Berg4b7679a2008-09-18 18:14:18 +02001434 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001435
Johannes Berg46900292009-02-15 12:44:28 +01001436 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Jouni Malinen5394af42009-01-08 13:31:59 +02001437 set_sta_flags(sta, WLAN_STA_MFP);
1438
Johannes Bergddf4ac52008-10-22 11:41:38 +02001439 if (elems.wmm_param)
Johannes Berg07346f812008-05-03 01:02:02 +02001440 set_sta_flags(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02001441
Johannes Berg5d1ec852009-12-02 12:43:43 +01001442 err = sta_info_insert(sta);
1443 sta = NULL;
1444 if (err) {
1445 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1446 " the AP (error %d)\n", sdata->name, err);
Johannes Berg90be5612010-01-08 19:01:07 +01001447 return false;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001448 }
1449
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03001450 /*
1451 * Always handle WMM once after association regardless
1452 * of the first value the AP uses. Setting -1 here has
1453 * that effect because the AP values is an unsigned
1454 * 4-bit value.
1455 */
1456 ifmgd->wmm_last_param_set = -1;
1457
Johannes Bergddf4ac52008-10-22 11:41:38 +02001458 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02001459 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07001460 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02001461 else
1462 ieee80211_set_wmm_default(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07001463
Johannes Berge4da8c32009-12-23 13:15:43 +01001464 local->oper_channel = wk->chan;
1465
Johannes Bergae5eb022008-10-14 16:58:37 +02001466 if (elems.ht_info_elem && elems.wmm_param &&
Johannes Bergaf6b6372009-12-23 13:15:35 +01001467 (sdata->local->hw.queues >= 4) &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001468 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001469 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001470 cbss->bssid, ap_ht_cap_flags);
Johannes Bergae5eb022008-10-14 16:58:37 +02001471
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001472 /* set AID and assoc capability,
1473 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01001474 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001475 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001476 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001477
Kalle Valo15b7b062009-03-22 21:57:14 +02001478 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01001479 * If we're using 4-addr mode, let the AP know that we're
1480 * doing so, so that it can create the STA VLAN on its side
1481 */
1482 if (ifmgd->use_4addr)
1483 ieee80211_send_4addr_nullfunc(local, sdata);
1484
1485 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02001486 * Start timer to probe the connection to the AP now.
1487 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02001488 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001489 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001490 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02001491
Johannes Bergaf6b6372009-12-23 13:15:35 +01001492 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001493}
1494
1495
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001496static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001497 struct ieee80211_mgmt *mgmt,
1498 size_t len,
1499 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001500 struct ieee802_11_elems *elems,
1501 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07001502{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001503 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001504 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02001505 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01001506 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01001507 bool need_ps = false;
1508
1509 if (sdata->u.mgd.associated) {
1510 bss = (void *)sdata->u.mgd.associated->priv;
1511 /* not previously set so we may need to recalc */
1512 need_ps = !bss->dtim_period;
1513 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001514
Johannes Berg5bda6172008-09-08 11:05:10 +02001515 if (elems->ds_params && elems->ds_params_len == 1)
1516 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1517 else
1518 freq = rx_status->freq;
1519
1520 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1521
1522 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1523 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001524
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001525 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01001526 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001527 if (bss)
1528 ieee80211_rx_bss_put(local, bss);
1529
1530 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001531 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001532
Johannes Berg56007a02010-01-26 14:19:52 +01001533 if (need_ps) {
1534 mutex_lock(&local->iflist_mtx);
1535 ieee80211_recalc_ps(local, -1);
1536 mutex_unlock(&local->iflist_mtx);
1537 }
1538
Sujithc481ec92009-01-06 09:28:37 +05301539 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001540 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001541 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05301542 struct ieee80211_channel_sw_ie *sw_elem =
1543 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001544 ieee80211_sta_process_chanswitch(sdata, sw_elem,
1545 bss, rx_status->mactime);
Sujithc481ec92009-01-06 09:28:37 +05301546 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001547}
1548
1549
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001550static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01001551 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001552{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001553 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02001554 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001555 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1556 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03001557 struct ieee802_11_elems elems;
1558
Kalle Valo15b7b062009-03-22 21:57:14 +02001559 ifmgd = &sdata->u.mgd;
1560
Johannes Berg77fdaa12009-07-07 03:45:17 +02001561 ASSERT_MGD_MTX(ifmgd);
1562
Johannes Berg47846c92009-11-25 17:46:19 +01001563 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03001564 return; /* ignore ProbeResp to foreign address */
1565
Ester Kummerae6a44e2008-06-27 18:54:48 +03001566 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1567 if (baselen > len)
1568 return;
1569
1570 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1571 &elems);
1572
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001573 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001574
Johannes Berg77fdaa12009-07-07 03:45:17 +02001575 if (ifmgd->associated &&
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001576 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
1577 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07001578}
1579
Johannes Bergd91f36d2009-04-16 13:17:26 +02001580/*
1581 * This is the canonical list of information elements we care about,
1582 * the filter code also gives us all changes to the Microsoft OUI
1583 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1584 *
1585 * We implement beacon filtering in software since that means we can
1586 * avoid processing the frame here and in cfg80211, and userspace
1587 * will not be able to tell whether the hardware supports it or not.
1588 *
1589 * XXX: This list needs to be dynamic -- userspace needs to be able to
1590 * add items it requires. It also needs to be able to tell us to
1591 * look out for other vendor IEs.
1592 */
1593static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02001594 (1ULL << WLAN_EID_COUNTRY) |
1595 (1ULL << WLAN_EID_ERP_INFO) |
1596 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1597 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1598 (1ULL << WLAN_EID_HT_CAPABILITY) |
1599 (1ULL << WLAN_EID_HT_INFORMATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001600
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001601static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001602 struct ieee80211_mgmt *mgmt,
1603 size_t len,
1604 struct ieee80211_rx_status *rx_status)
1605{
Johannes Bergd91f36d2009-04-16 13:17:26 +02001606 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001607 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001608 size_t baselen;
1609 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001610 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001611 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001612 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001613 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001614 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001615 u8 *bssid;
1616
1617 ASSERT_MGD_MTX(ifmgd);
Jiri Bencf0706e82007-05-05 11:45:53 -07001618
Ester Kummerae6a44e2008-06-27 18:54:48 +03001619 /* Process beacon from the current BSS */
1620 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1621 if (baselen > len)
1622 return;
1623
Johannes Bergd91f36d2009-04-16 13:17:26 +02001624 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07001625 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001626
Johannes Bergb291ba12009-07-10 15:29:03 +02001627 /*
1628 * We might have received a number of frames, among them a
1629 * disassoc frame and a beacon...
1630 */
1631 if (!ifmgd->associated)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001632 return;
1633
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001634 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001635
Johannes Bergb291ba12009-07-10 15:29:03 +02001636 /*
1637 * And in theory even frames from a different AP we were just
1638 * associated to a split-second ago!
1639 */
1640 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001641 return;
1642
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001643 /* Track average RSSI from the Beacon frames of the current AP */
1644 ifmgd->last_beacon_signal = rx_status->signal;
1645 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1646 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03001647 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001648 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03001649 ifmgd->count_beacon_signal = 1;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001650 } else {
1651 ifmgd->ave_beacon_signal =
1652 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1653 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1654 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03001655 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001656 }
1657 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03001658 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001659 !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1660 int sig = ifmgd->ave_beacon_signal / 16;
1661 int last_event = ifmgd->last_cqm_event_signal;
1662 int thold = bss_conf->cqm_rssi_thold;
1663 int hyst = bss_conf->cqm_rssi_hyst;
1664 if (sig < thold &&
1665 (last_event == 0 || sig < last_event - hyst)) {
1666 ifmgd->last_cqm_event_signal = sig;
1667 ieee80211_cqm_rssi_notify(
1668 &sdata->vif,
1669 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1670 GFP_KERNEL);
1671 } else if (sig > thold &&
1672 (last_event == 0 || sig > last_event + hyst)) {
1673 ifmgd->last_cqm_event_signal = sig;
1674 ieee80211_cqm_rssi_notify(
1675 &sdata->vif,
1676 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1677 GFP_KERNEL);
1678 }
1679 }
1680
Johannes Bergb291ba12009-07-10 15:29:03 +02001681 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Jouni Malinen92778182009-05-14 21:15:36 +03001682#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1683 if (net_ratelimit()) {
1684 printk(KERN_DEBUG "%s: cancelling probereq poll due "
Johannes Berg47846c92009-11-25 17:46:19 +01001685 "to a received beacon\n", sdata->name);
Jouni Malinen92778182009-05-14 21:15:36 +03001686 }
1687#endif
Johannes Bergb291ba12009-07-10 15:29:03 +02001688 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Johannes Berg4e751842009-06-10 15:16:52 +02001689 mutex_lock(&local->iflist_mtx);
1690 ieee80211_recalc_ps(local, -1);
1691 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03001692 }
1693
Johannes Bergb291ba12009-07-10 15:29:03 +02001694 /*
1695 * Push the beacon loss detection into the future since
1696 * we are processing a beacon from the AP just now.
1697 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001698 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001699
Johannes Bergd91f36d2009-04-16 13:17:26 +02001700 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1701 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1702 len - baselen, &elems,
1703 care_about_ies, ncrc);
1704
1705 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02001706 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1707 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001708
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001709 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
Jouni Malinen30196672009-05-19 17:01:43 +03001710 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1711 true);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001712
Johannes Berg4ced3f72010-07-19 16:39:04 +02001713 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jouni Malinen30196672009-05-19 17:01:43 +03001714 elems.wmm_param_len);
1715 }
Johannes Bergfe3fa822008-09-08 11:05:09 +02001716
Vivek Natarajan25c9c872009-03-02 20:20:30 +05301717 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02001718 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02001719 if (local->hw.conf.dynamic_ps_timeout > 0) {
1720 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1721 ieee80211_hw_config(local,
1722 IEEE80211_CONF_CHANGE_PS);
1723 ieee80211_send_nullfunc(local, sdata, 0);
1724 } else {
1725 local->pspolling = true;
1726
1727 /*
1728 * Here is assumed that the driver will be
1729 * able to send ps-poll frame and receive a
1730 * response even though power save mode is
1731 * enabled, but some drivers might require
1732 * to disable power save here. This needs
1733 * to be investigated.
1734 */
1735 ieee80211_send_pspoll(local, sdata);
1736 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08001737 }
1738 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001739
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001740 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03001741 return;
1742 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001743 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03001744
Johannes Berg7a5158e2008-10-08 10:59:33 +02001745 if (elems.erp_info && elems.erp_info_len >= 1) {
1746 erp_valid = true;
1747 erp_value = elems.erp_info[0];
1748 } else {
1749 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04001750 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001751 changed |= ieee80211_handle_bss_capability(sdata,
1752 le16_to_cpu(mgmt->u.beacon.capab_info),
1753 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07001754
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001755
Vasanthakumar Thiagarajan53d6f81c2009-02-11 22:18:49 +05301756 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001757 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001758 struct sta_info *sta;
1759 struct ieee80211_supported_band *sband;
1760 u16 ap_ht_cap_flags;
1761
1762 rcu_read_lock();
1763
Johannes Bergabe60632009-11-25 17:46:18 +01001764 sta = sta_info_get(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001765 if (WARN_ON(!sta)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001766 rcu_read_unlock();
1767 return;
1768 }
1769
1770 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1771
1772 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1773 elems.ht_cap_elem, &sta->sta.ht_cap);
1774
1775 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1776
1777 rcu_read_unlock();
1778
1779 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001780 bssid, ap_ht_cap_flags);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001781 }
1782
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001783 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001784 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301785 /* TODO: IBSS also needs this */
1786 if (elems.pwr_constr_elem)
1787 ieee80211_handle_pwr_constr(sdata,
1788 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1789 elems.pwr_constr_elem,
1790 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001791 }
1792
Johannes Berg471b3ef2007-12-28 14:32:58 +01001793 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001794}
1795
Johannes Berg1fa57d02010-06-10 10:21:32 +02001796void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1797 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001798{
Johannes Berg77fdaa12009-07-07 03:45:17 +02001799 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001800 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07001801 struct ieee80211_mgmt *mgmt;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001802 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001803 u16 fc;
1804
Jiri Bencf0706e82007-05-05 11:45:53 -07001805 rx_status = (struct ieee80211_rx_status *) skb->cb;
1806 mgmt = (struct ieee80211_mgmt *) skb->data;
1807 fc = le16_to_cpu(mgmt->frame_control);
1808
Johannes Berg77fdaa12009-07-07 03:45:17 +02001809 mutex_lock(&ifmgd->mtx);
1810
1811 if (ifmgd->associated &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001812 memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001813 switch (fc & IEEE80211_FCTL_STYPE) {
1814 case IEEE80211_STYPE_BEACON:
1815 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1816 rx_status);
1817 break;
1818 case IEEE80211_STYPE_PROBE_RESP:
Johannes Bergaf6b6372009-12-23 13:15:35 +01001819 ieee80211_rx_mgmt_probe_resp(sdata, skb);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001820 break;
1821 case IEEE80211_STYPE_DEAUTH:
Johannes Berg63f170e2009-12-23 13:15:33 +01001822 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001823 break;
1824 case IEEE80211_STYPE_DISASSOC:
1825 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1826 break;
1827 case IEEE80211_STYPE_ACTION:
Johannes Berg8b9a4e62010-05-28 15:22:58 +02001828 switch (mgmt->u.action.category) {
Johannes Berg8b9a4e62010-05-28 15:22:58 +02001829 case WLAN_CATEGORY_SPECTRUM_MGMT:
1830 ieee80211_sta_process_chanswitch(sdata,
1831 &mgmt->u.action.u.chan_switch.sw_elem,
1832 (void *)ifmgd->associated->priv,
1833 rx_status->mactime);
1834 break;
1835 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02001836 }
1837 mutex_unlock(&ifmgd->mtx);
1838
1839 switch (rma) {
1840 case RX_MGMT_NONE:
1841 /* no action */
1842 break;
1843 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02001844 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001845 break;
1846 case RX_MGMT_CFG80211_DISASSOC:
Holger Schurigce470612009-10-13 13:28:13 +02001847 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001848 break;
1849 default:
1850 WARN(1, "unexpected: %d", rma);
1851 }
Johannes Berg36b3a622010-06-10 10:21:33 +02001852 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001853 }
1854
Johannes Berg77fdaa12009-07-07 03:45:17 +02001855 mutex_unlock(&ifmgd->mtx);
1856
Johannes Berg63f170e2009-12-23 13:15:33 +01001857 if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
Johannes Bergb054b742010-06-07 21:50:07 +02001858 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
1859 struct ieee80211_local *local = sdata->local;
1860 struct ieee80211_work *wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001861
Johannes Berga1699b72010-07-30 16:46:07 +02001862 mutex_lock(&local->mtx);
Johannes Bergb054b742010-06-07 21:50:07 +02001863 list_for_each_entry(wk, &local->work_list, list) {
1864 if (wk->sdata != sdata)
1865 continue;
1866
Johannes Berge5b900d2010-07-29 16:08:55 +02001867 if (wk->type != IEEE80211_WORK_ASSOC &&
1868 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
Johannes Bergb054b742010-06-07 21:50:07 +02001869 continue;
1870
1871 if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
1872 continue;
1873 if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
1874 continue;
1875
1876 /*
1877 * Printing the message only here means we can't
1878 * spuriously print it, but it also means that it
1879 * won't be printed when the frame comes in before
1880 * we even tried to associate or in similar cases.
1881 *
1882 * Ultimately, I suspect cfg80211 should print the
1883 * messages instead.
1884 */
1885 printk(KERN_DEBUG
1886 "%s: deauthenticated from %pM (Reason: %u)\n",
1887 sdata->name, mgmt->bssid,
1888 le16_to_cpu(mgmt->u.deauth.reason_code));
1889
1890 list_del_rcu(&wk->list);
1891 free_work(wk);
1892 break;
1893 }
Johannes Berga1699b72010-07-30 16:46:07 +02001894 mutex_unlock(&local->mtx);
Johannes Bergb054b742010-06-07 21:50:07 +02001895
Johannes Berg77fdaa12009-07-07 03:45:17 +02001896 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Bergb054b742010-06-07 21:50:07 +02001897 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001898}
1899
Johannes Berg9c6bd792008-09-11 00:01:52 +02001900static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07001901{
1902 struct ieee80211_sub_if_data *sdata =
1903 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01001904 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001905 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001906
Johannes Berg5bb644a2009-05-17 11:40:42 +02001907 if (local->quiescing) {
1908 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
1909 return;
1910 }
1911
Johannes Berg64592c82010-06-10 10:21:31 +02001912 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001913}
1914
Johannes Berg1fa57d02010-06-10 10:21:32 +02001915void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02001916{
Johannes Berg60f8b392008-09-08 17:44:22 +02001917 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02001918 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02001919
Johannes Berg77fdaa12009-07-07 03:45:17 +02001920 /* then process the rest of the work */
1921 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02001922
Johannes Bergb291ba12009-07-10 15:29:03 +02001923 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1924 IEEE80211_STA_CONNECTION_POLL) &&
1925 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03001926 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01001927 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001928
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001929 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001930
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01001931 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1932 max_tries = IEEE80211_MAX_NULLFUNC_TRIES;
1933 else
1934 max_tries = IEEE80211_MAX_PROBE_TRIES;
1935
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001936 /* ACK received for nullfunc probing frame */
1937 if (!ifmgd->probe_send_count)
1938 ieee80211_reset_ap_probe(sdata);
1939
1940 else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02001941 run_again(ifmgd, ifmgd->probe_timeout);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001942
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01001943 else if (ifmgd->probe_send_count < max_tries) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03001944#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Ben Greearb38afa82010-10-07 16:12:06 -07001945 wiphy_debug(local->hw.wiphy,
1946 "%s: No probe response from AP %pM"
1947 " after %dms, try %d\n",
1948 sdata->name,
1949 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ,
1950 ifmgd->probe_send_count);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001951#endif
1952 ieee80211_mgd_probe_ap_send(sdata);
1953 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02001954 /*
1955 * We actually lost the connection ... or did we?
1956 * Let's make sure!
1957 */
1958 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1959 IEEE80211_STA_BEACON_POLL);
Ben Greearb38afa82010-10-07 16:12:06 -07001960 wiphy_debug(local->hw.wiphy,
1961 "%s: No probe response from AP %pM"
1962 " after %dms, disconnecting.\n",
1963 sdata->name,
1964 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
Johannes Berg53f73c02010-10-05 19:37:40 +02001965 ieee80211_set_disassoc(sdata, true, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001966 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001967 mutex_lock(&local->mtx);
1968 ieee80211_recalc_idle(local);
1969 mutex_unlock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02001970 /*
1971 * must be outside lock due to cfg80211,
1972 * but that's not a problem.
1973 */
1974 ieee80211_send_deauth_disassoc(sdata, bssid,
1975 IEEE80211_STYPE_DEAUTH,
1976 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001977 NULL, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001978 mutex_lock(&ifmgd->mtx);
1979 }
1980 }
1981
Johannes Berg77fdaa12009-07-07 03:45:17 +02001982 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02001983}
Johannes Berg0a51b272008-09-08 17:44:25 +02001984
Johannes Bergb291ba12009-07-10 15:29:03 +02001985static void ieee80211_sta_bcn_mon_timer(unsigned long data)
1986{
1987 struct ieee80211_sub_if_data *sdata =
1988 (struct ieee80211_sub_if_data *) data;
1989 struct ieee80211_local *local = sdata->local;
1990
1991 if (local->quiescing)
1992 return;
1993
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001994 ieee80211_queue_work(&sdata->local->hw,
1995 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02001996}
1997
1998static void ieee80211_sta_conn_mon_timer(unsigned long data)
1999{
2000 struct ieee80211_sub_if_data *sdata =
2001 (struct ieee80211_sub_if_data *) data;
2002 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2003 struct ieee80211_local *local = sdata->local;
2004
2005 if (local->quiescing)
2006 return;
2007
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002008 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002009}
2010
2011static void ieee80211_sta_monitor_work(struct work_struct *work)
2012{
2013 struct ieee80211_sub_if_data *sdata =
2014 container_of(work, struct ieee80211_sub_if_data,
2015 u.mgd.monitor_work);
2016
2017 ieee80211_mgd_probe_ap(sdata, false);
2018}
2019
Johannes Berga1678f82008-09-11 00:01:47 +02002020static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2021{
Kalle Vaload935682009-04-19 08:47:19 +03002022 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Bergb291ba12009-07-10 15:29:03 +02002023 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2024 IEEE80211_STA_CONNECTION_POLL);
Kalle Vaload935682009-04-19 08:47:19 +03002025
Johannes Bergb291ba12009-07-10 15:29:03 +02002026 /* let's probe the connection once */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002027 ieee80211_queue_work(&sdata->local->hw,
Johannes Bergb291ba12009-07-10 15:29:03 +02002028 &sdata->u.mgd.monitor_work);
2029 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02002030 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03002031 }
Johannes Berga1678f82008-09-11 00:01:47 +02002032}
2033
Johannes Berg5bb644a2009-05-17 11:40:42 +02002034#ifdef CONFIG_PM
2035void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2036{
2037 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2038
2039 /*
2040 * we need to use atomic bitops for the running bits
2041 * only because both timers might fire at the same
2042 * time -- the code here is properly synchronised.
2043 */
2044
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002045 cancel_work_sync(&ifmgd->request_smps_work);
2046
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002047 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002048 if (del_timer_sync(&ifmgd->timer))
2049 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2050
2051 cancel_work_sync(&ifmgd->chswitch_work);
2052 if (del_timer_sync(&ifmgd->chswitch_timer))
2053 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002054
2055 cancel_work_sync(&ifmgd->monitor_work);
2056 /* these will just be re-established on connection */
2057 del_timer_sync(&ifmgd->conn_mon_timer);
2058 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002059}
2060
2061void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2062{
2063 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2064
2065 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2066 add_timer(&ifmgd->timer);
2067 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2068 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01002069 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau46090972010-11-23 20:28:03 +01002070 ieee80211_restart_sta_timer(sdata);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002071}
2072#endif
2073
Johannes Berg9c6bd792008-09-11 00:01:52 +02002074/* interface setup */
2075void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2076{
Johannes Berg46900292009-02-15 12:44:28 +01002077 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002078
Johannes Berg46900292009-02-15 12:44:28 +01002079 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002080 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01002081 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002082 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2083 ieee80211_beacon_connection_loss_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002084 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01002085 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02002086 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002087 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2088 (unsigned long) sdata);
2089 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2090 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002091 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05302092 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002093
Johannes Bergab1faea2009-07-01 21:41:17 +02002094 ifmgd->flags = 0;
Johannes Bergbbbdff92009-04-16 13:27:42 +02002095
Johannes Berg77fdaa12009-07-07 03:45:17 +02002096 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01002097
2098 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2099 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2100 else
2101 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002102}
2103
2104/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002105void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2106{
2107 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02002108
2109 /* Restart STA timers */
2110 rcu_read_lock();
2111 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2112 ieee80211_restart_sta_timer(sdata);
2113 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02002114}
Johannes Berg10f644a2009-04-16 13:17:25 +02002115
2116int ieee80211_max_network_latency(struct notifier_block *nb,
2117 unsigned long data, void *dummy)
2118{
2119 s32 latency_usec = (s32) data;
2120 struct ieee80211_local *local =
2121 container_of(nb, struct ieee80211_local,
2122 network_latency_notifier);
2123
2124 mutex_lock(&local->iflist_mtx);
2125 ieee80211_recalc_ps(local, latency_usec);
2126 mutex_unlock(&local->iflist_mtx);
2127
2128 return 0;
2129}
Johannes Berg77fdaa12009-07-07 03:45:17 +02002130
2131/* config hooks */
Johannes Bergaf6b6372009-12-23 13:15:35 +01002132static enum work_done_result
2133ieee80211_probe_auth_done(struct ieee80211_work *wk,
2134 struct sk_buff *skb)
2135{
2136 if (!skb) {
2137 cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
2138 return WORK_DONE_DESTROY;
2139 }
2140
2141 if (wk->type == IEEE80211_WORK_AUTH) {
2142 cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
2143 return WORK_DONE_DESTROY;
2144 }
2145
2146 mutex_lock(&wk->sdata->u.mgd.mtx);
2147 ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2148 mutex_unlock(&wk->sdata->u.mgd.mtx);
2149
2150 wk->type = IEEE80211_WORK_AUTH;
2151 wk->probe_auth.tries = 0;
2152 return WORK_DONE_REQUEUE;
2153}
2154
Johannes Berg77fdaa12009-07-07 03:45:17 +02002155int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2156 struct cfg80211_auth_request *req)
2157{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002158 const u8 *ssid;
Johannes Bergf679f652009-12-23 13:15:34 +01002159 struct ieee80211_work *wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002160 u16 auth_alg;
2161
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002162 if (req->local_state_change)
2163 return 0; /* no need to update mac80211 state */
2164
Johannes Berg77fdaa12009-07-07 03:45:17 +02002165 switch (req->auth_type) {
2166 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2167 auth_alg = WLAN_AUTH_OPEN;
2168 break;
2169 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg9dca9c42010-07-21 10:09:25 +02002170 if (IS_ERR(sdata->local->wep_tx_tfm))
2171 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002172 auth_alg = WLAN_AUTH_SHARED_KEY;
2173 break;
2174 case NL80211_AUTHTYPE_FT:
2175 auth_alg = WLAN_AUTH_FT;
2176 break;
2177 case NL80211_AUTHTYPE_NETWORK_EAP:
2178 auth_alg = WLAN_AUTH_LEAP;
2179 break;
2180 default:
2181 return -EOPNOTSUPP;
2182 }
2183
2184 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2185 if (!wk)
2186 return -ENOMEM;
2187
Joe Perches5e124bd2010-01-08 22:33:38 -08002188 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002189
2190 if (req->ie && req->ie_len) {
2191 memcpy(wk->ie, req->ie, req->ie_len);
2192 wk->ie_len = req->ie_len;
2193 }
2194
Johannes Bergfffd0932009-07-08 14:22:54 +02002195 if (req->key && req->key_len) {
Johannes Bergaf6b6372009-12-23 13:15:35 +01002196 wk->probe_auth.key_len = req->key_len;
2197 wk->probe_auth.key_idx = req->key_idx;
2198 memcpy(wk->probe_auth.key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02002199 }
2200
Johannes Berg77fdaa12009-07-07 03:45:17 +02002201 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002202 memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2203 wk->probe_auth.ssid_len = ssid[1];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002204
Johannes Bergaf6b6372009-12-23 13:15:35 +01002205 wk->probe_auth.algorithm = auth_alg;
2206 wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
Johannes Bergf679f652009-12-23 13:15:34 +01002207
Johannes Berg070bb542010-02-03 13:57:46 +01002208 /* if we already have a probe, don't probe again */
2209 if (req->bss->proberesp_ies)
2210 wk->type = IEEE80211_WORK_AUTH;
2211 else
2212 wk->type = IEEE80211_WORK_DIRECT_PROBE;
Johannes Bergf679f652009-12-23 13:15:34 +01002213 wk->chan = req->bss->channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002214 wk->sdata = sdata;
2215 wk->done = ieee80211_probe_auth_done;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002216
Johannes Bergaf6b6372009-12-23 13:15:35 +01002217 ieee80211_add_work(wk);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002218 return 0;
2219}
2220
Johannes Bergaf6b6372009-12-23 13:15:35 +01002221static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2222 struct sk_buff *skb)
2223{
2224 struct ieee80211_mgmt *mgmt;
Johannes Berge5b900d2010-07-29 16:08:55 +02002225 struct ieee80211_rx_status *rx_status;
2226 struct ieee802_11_elems elems;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002227 u16 status;
2228
2229 if (!skb) {
2230 cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
2231 return WORK_DONE_DESTROY;
2232 }
2233
Johannes Berge5b900d2010-07-29 16:08:55 +02002234 if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2235 mutex_lock(&wk->sdata->u.mgd.mtx);
2236 rx_status = (void *) skb->cb;
2237 ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2238 ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2239 &elems, true);
2240 mutex_unlock(&wk->sdata->u.mgd.mtx);
2241
2242 wk->type = IEEE80211_WORK_ASSOC;
2243 /* not really done yet */
2244 return WORK_DONE_REQUEUE;
2245 }
2246
Johannes Bergaf6b6372009-12-23 13:15:35 +01002247 mgmt = (void *)skb->data;
2248 status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2249
2250 if (status == WLAN_STATUS_SUCCESS) {
2251 mutex_lock(&wk->sdata->u.mgd.mtx);
2252 if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2253 mutex_unlock(&wk->sdata->u.mgd.mtx);
2254 /* oops -- internal error -- send timeout for now */
2255 cfg80211_send_assoc_timeout(wk->sdata->dev,
2256 wk->filter_ta);
2257 return WORK_DONE_DESTROY;
2258 }
Juuso Oikarinen68542962010-06-09 13:43:26 +03002259
2260 mutex_unlock(&wk->sdata->u.mgd.mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002261 }
2262
2263 cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
2264 return WORK_DONE_DESTROY;
2265}
2266
Johannes Berg77fdaa12009-07-07 03:45:17 +02002267int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2268 struct cfg80211_assoc_request *req)
2269{
2270 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002271 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Bergf679f652009-12-23 13:15:34 +01002272 struct ieee80211_work *wk;
Johannes Berg63f170e2009-12-23 13:15:33 +01002273 const u8 *ssid;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002274 int i;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002275
2276 mutex_lock(&ifmgd->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002277 if (ifmgd->associated) {
Jouni Malinen9c87ba62010-02-28 12:13:46 +02002278 if (!req->prev_bssid ||
2279 memcmp(req->prev_bssid, ifmgd->associated->bssid,
2280 ETH_ALEN)) {
2281 /*
2282 * We are already associated and the request was not a
2283 * reassociation request from the current BSS, so
2284 * reject it.
2285 */
2286 mutex_unlock(&ifmgd->mtx);
2287 return -EALREADY;
2288 }
2289
2290 /* Trying to reassociate - clear previous association state */
Johannes Berg53f73c02010-10-05 19:37:40 +02002291 ieee80211_set_disassoc(sdata, true, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002292 }
2293 mutex_unlock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002294
Johannes Berg63f170e2009-12-23 13:15:33 +01002295 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002296 if (!wk)
2297 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002298
Johannes Berg77fdaa12009-07-07 03:45:17 +02002299 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
Vivek Natarajan375177b2010-02-09 14:50:28 +05302300 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002301
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002302 ifmgd->beacon_crc_valid = false;
2303
Johannes Berg77fdaa12009-07-07 03:45:17 +02002304 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2305 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2306 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2307 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2308 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2309
Johannes Berg77fdaa12009-07-07 03:45:17 +02002310
2311 if (req->ie && req->ie_len) {
2312 memcpy(wk->ie, req->ie, req->ie_len);
2313 wk->ie_len = req->ie_len;
2314 } else
2315 wk->ie_len = 0;
2316
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002317 wk->assoc.bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01002318
Johannes Bergaf6b6372009-12-23 13:15:35 +01002319 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
Johannes Bergf679f652009-12-23 13:15:34 +01002320
Johannes Bergaf6b6372009-12-23 13:15:35 +01002321 /* new association always uses requested smps mode */
2322 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2323 if (ifmgd->powersave)
2324 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2325 else
2326 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2327 } else
2328 ifmgd->ap_smps = ifmgd->req_smps;
2329
2330 wk->assoc.smps = ifmgd->ap_smps;
Johannes Berg77c81442009-12-23 13:15:37 +01002331 /*
2332 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2333 * We still associate in non-HT mode (11a/b/g) if any one of these
2334 * ciphers is configured as pairwise.
2335 * We can set this to true for non-11n hardware, that'll be checked
2336 * separately along with the peer capabilities.
2337 */
Johannes Bergaf6b6372009-12-23 13:15:35 +01002338 wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
Johannes Bergf679f652009-12-23 13:15:34 +01002339 wk->assoc.capability = req->bss->capability;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002340 wk->assoc.wmm_used = bss->wmm_used;
2341 wk->assoc.supp_rates = bss->supp_rates;
2342 wk->assoc.supp_rates_len = bss->supp_rates_len;
Johannes Bergf679f652009-12-23 13:15:34 +01002343 wk->assoc.ht_information_ie =
2344 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
Johannes Berg63f170e2009-12-23 13:15:33 +01002345
Kalle Valoab133152010-01-12 10:42:31 +02002346 if (bss->wmm_used && bss->uapsd_supported &&
2347 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2348 wk->assoc.uapsd_used = true;
2349 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2350 } else {
2351 wk->assoc.uapsd_used = false;
2352 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2353 }
2354
Johannes Berg63f170e2009-12-23 13:15:33 +01002355 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Johannes Bergf679f652009-12-23 13:15:34 +01002356 memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2357 wk->assoc.ssid_len = ssid[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01002358
Johannes Berg77fdaa12009-07-07 03:45:17 +02002359 if (req->prev_bssid)
Johannes Bergf679f652009-12-23 13:15:34 +01002360 memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002361
Johannes Bergf679f652009-12-23 13:15:34 +01002362 wk->chan = req->bss->channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002363 wk->sdata = sdata;
2364 wk->done = ieee80211_assoc_done;
Johannes Berge5b900d2010-07-29 16:08:55 +02002365 if (!bss->dtim_period &&
2366 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2367 wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2368 else
2369 wk->type = IEEE80211_WORK_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002370
2371 if (req->use_mfp) {
2372 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2373 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2374 } else {
2375 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2376 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2377 }
2378
2379 if (req->crypto.control_port)
2380 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2381 else
2382 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2383
Johannes Berga621fa42010-08-27 14:26:54 +03002384 sdata->control_port_protocol = req->crypto.control_port_ethertype;
2385 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2386
Johannes Bergaf6b6372009-12-23 13:15:35 +01002387 ieee80211_add_work(wk);
2388 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002389}
2390
2391int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503d2009-07-07 03:56:11 +02002392 struct cfg80211_deauth_request *req,
2393 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002394{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002395 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002396 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergf679f652009-12-23 13:15:34 +01002397 struct ieee80211_work *wk;
Jouni Malinen05e48e82010-06-14 11:55:56 -07002398 u8 bssid[ETH_ALEN];
2399 bool assoc_bss = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002400
Johannes Berg77fdaa12009-07-07 03:45:17 +02002401 mutex_lock(&ifmgd->mtx);
2402
Jouni Malinen05e48e82010-06-14 11:55:56 -07002403 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002404 if (ifmgd->associated == req->bss) {
Johannes Berg53f73c02010-10-05 19:37:40 +02002405 ieee80211_set_disassoc(sdata, false, true);
Johannes Berga58ce432009-11-19 12:45:42 +01002406 mutex_unlock(&ifmgd->mtx);
Jouni Malinen05e48e82010-06-14 11:55:56 -07002407 assoc_bss = true;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002408 } else {
2409 bool not_auth_yet = false;
Johannes Berga58ce432009-11-19 12:45:42 +01002410
Johannes Bergaf6b6372009-12-23 13:15:35 +01002411 mutex_unlock(&ifmgd->mtx);
2412
Johannes Berga1699b72010-07-30 16:46:07 +02002413 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002414 list_for_each_entry(wk, &local->work_list, list) {
Johannes Berg29165e42010-02-06 15:20:13 +01002415 if (wk->sdata != sdata)
Johannes Bergaf6b6372009-12-23 13:15:35 +01002416 continue;
Johannes Berg29165e42010-02-06 15:20:13 +01002417
2418 if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
Reinette Chatre79733a82010-05-04 16:04:49 -07002419 wk->type != IEEE80211_WORK_AUTH &&
Johannes Berge5b900d2010-07-29 16:08:55 +02002420 wk->type != IEEE80211_WORK_ASSOC &&
2421 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
Johannes Berg29165e42010-02-06 15:20:13 +01002422 continue;
2423
Johannes Bergaf6b6372009-12-23 13:15:35 +01002424 if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2425 continue;
Johannes Berg29165e42010-02-06 15:20:13 +01002426
2427 not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2428 list_del_rcu(&wk->list);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002429 free_work(wk);
2430 break;
2431 }
Johannes Berga1699b72010-07-30 16:46:07 +02002432 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002433
2434 /*
2435 * If somebody requests authentication and we haven't
2436 * sent out an auth frame yet there's no need to send
2437 * out a deauth frame either. If the state was PROBE,
2438 * then this is the case. If it's AUTH we have sent a
2439 * frame, and if it's IDLE we have completed the auth
2440 * process already.
2441 */
2442 if (not_auth_yet) {
2443 __cfg80211_auth_canceled(sdata->dev, bssid);
2444 return 0;
2445 }
2446 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002447
Johannes Berg0ff71612009-09-26 14:45:41 +02002448 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002449 sdata->name, bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002450
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002451 ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2452 req->reason_code, cookie,
2453 !req->local_state_change);
Jouni Malinen05e48e82010-06-14 11:55:56 -07002454 if (assoc_bss)
2455 sta_info_destroy_addr(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002456
Johannes Berg7da7cc12010-08-05 17:02:38 +02002457 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002458 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002459 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002460
Johannes Berg77fdaa12009-07-07 03:45:17 +02002461 return 0;
2462}
2463
2464int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503d2009-07-07 03:56:11 +02002465 struct cfg80211_disassoc_request *req,
2466 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002467{
2468 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07002469 u8 bssid[ETH_ALEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002470
Johannes Berg77fdaa12009-07-07 03:45:17 +02002471 mutex_lock(&ifmgd->mtx);
2472
Johannes Berg8d8b2612009-07-25 11:58:36 +02002473 /*
2474 * cfg80211 should catch this ... but it's racy since
2475 * we can receive a disassoc frame, process it, hand it
2476 * to cfg80211 while that's in a locked section already
2477 * trying to tell us that the user wants to disconnect.
2478 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002479 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002480 mutex_unlock(&ifmgd->mtx);
2481 return -ENOLINK;
2482 }
2483
Johannes Berg0ff71612009-09-26 14:45:41 +02002484 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002485 sdata->name, req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002486
Jouni Malinene69e95d2010-03-29 23:29:31 -07002487 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg53f73c02010-10-05 19:37:40 +02002488 ieee80211_set_disassoc(sdata, false, true);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002489
2490 mutex_unlock(&ifmgd->mtx);
2491
2492 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +02002493 IEEE80211_STYPE_DISASSOC, req->reason_code,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002494 cookie, !req->local_state_change);
Jouni Malinene69e95d2010-03-29 23:29:31 -07002495 sta_info_destroy_addr(sdata, bssid);
Johannes Bergbc83b682009-11-29 12:19:06 +01002496
Johannes Berg7da7cc12010-08-05 17:02:38 +02002497 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002498 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002499 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002500
Johannes Berg77fdaa12009-07-07 03:45:17 +02002501 return 0;
2502}
Jouni Malinen026331c2010-02-15 12:53:10 +02002503
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002504void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2505 enum nl80211_cqm_rssi_threshold_event rssi_event,
2506 gfp_t gfp)
2507{
2508 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2509
Johannes Bergb5878a22010-04-07 16:48:40 +02002510 trace_api_cqm_rssi_notify(sdata, rssi_event);
2511
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002512 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2513}
2514EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);