blob: c9ceb4d57ab0e62ab3c250ad1e0d959ce1de06ef [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.
Eliad Peller0ab82b02010-12-03 02:16:23 +0200628 * The 2000 second value is there for compatibility
629 * until the PM_QOS_NETWORK_LATENCY is configured
630 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300631 */
Eliad Peller0ab82b02010-12-03 02:16:23 +0200632 if (latency > (1900 * USEC_PER_MSEC) &&
633 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300634 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +0300635 else
636 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300637 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300638 local->dynamic_ps_user_timeout = timeout;
639 if (!local->disable_dynamic_ps)
640 conf->dynamic_ps_timeout =
641 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300642
Johannes Berg04fe2032009-04-22 18:44:37 +0200643 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +0200644 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +0200645 } else {
Johannes Berg56007a02010-01-26 14:19:52 +0100646 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +0200647 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +0100648 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +0200649
Johannes Berg56007a02010-01-26 14:19:52 +0100650 bss = (void *)found->u.mgd.associated->priv;
651 dtimper = bss->dtim_period;
652
653 /* If the TIM IE is invalid, pretend the value is 1 */
654 if (!dtimper)
655 dtimper = 1;
656 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +0200657 maxslp = min_t(int, dtimper,
658 latency / beaconint_us);
659
Johannes Berg9ccebe62009-04-23 10:32:36 +0200660 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +0100661 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +0200662 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +0200663 }
Johannes Berg10f644a2009-04-16 13:17:25 +0200664 } else {
Johannes Berg965beda2009-04-16 13:17:24 +0200665 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +0200666 }
Johannes Berg965beda2009-04-16 13:17:24 +0200667
Johannes Bergaf6b6372009-12-23 13:15:35 +0100668 change:
Johannes Berg965beda2009-04-16 13:17:24 +0200669 ieee80211_change_ps(local);
670}
671
672void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
673{
674 struct ieee80211_local *local =
675 container_of(work, struct ieee80211_local,
676 dynamic_ps_disable_work);
677
678 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
679 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
680 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
681 }
682
683 ieee80211_wake_queues_by_reason(&local->hw,
684 IEEE80211_QUEUE_STOP_REASON_PS);
685}
686
687void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
688{
689 struct ieee80211_local *local =
690 container_of(work, struct ieee80211_local,
691 dynamic_ps_enable_work);
692 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Vivek Natarajan375177b2010-02-09 14:50:28 +0530693 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200694
695 /* can only happen when PS was just disabled anyway */
696 if (!sdata)
697 return;
698
699 if (local->hw.conf.flags & IEEE80211_CONF_PS)
700 return;
701
Vivek Natarajan375177b2010-02-09 14:50:28 +0530702 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
703 (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
Johannes Berg965beda2009-04-16 13:17:24 +0200704 ieee80211_send_nullfunc(local, sdata, 1);
705
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200706 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
707 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +0530708 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
709 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
710 local->hw.conf.flags |= IEEE80211_CONF_PS;
711 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
712 }
Johannes Berg965beda2009-04-16 13:17:24 +0200713}
714
715void ieee80211_dynamic_ps_timer(unsigned long data)
716{
717 struct ieee80211_local *local = (void *) data;
718
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -0700719 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +0200720 return;
721
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400722 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +0200723}
724
Johannes Berg60f8b392008-09-08 17:44:22 +0200725/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200726static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +0200727 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700728 u8 *wmm_param, size_t wmm_param_len)
729{
Jiri Bencf0706e82007-05-05 11:45:53 -0700730 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +0200731 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -0700732 size_t left;
733 int count;
Kalle Valoab133152010-01-12 10:42:31 +0200734 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700735
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200736 if (!local->ops->conf_tx)
737 return;
738
Johannes Bergaf6b6372009-12-23 13:15:35 +0100739 if (local->hw.queues < 4)
Johannes Berg3434fbd2008-05-03 00:59:37 +0200740 return;
741
742 if (!wmm_param)
743 return;
744
Jiri Bencf0706e82007-05-05 11:45:53 -0700745 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
746 return;
Kalle Valoab133152010-01-12 10:42:31 +0200747
748 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Kalle Valo50ae0cf2010-01-12 10:42:39 +0200749 uapsd_queues = local->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +0200750
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +0100752 if (count == ifmgd->wmm_last_param_set)
Jiri Bencf0706e82007-05-05 11:45:53 -0700753 return;
Johannes Berg46900292009-02-15 12:44:28 +0100754 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -0700755
756 pos = wmm_param + 8;
757 left = wmm_param_len - 8;
758
759 memset(&params, 0, sizeof(params));
760
Jiri Bencf0706e82007-05-05 11:45:53 -0700761 local->wmm_acm = 0;
762 for (; left >= 4; left -= 4, pos += 4) {
763 int aci = (pos[0] >> 5) & 0x03;
764 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +0200765 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700766 int queue;
767
768 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200769 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +0200770 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200771 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200772 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +0200773 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
774 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700775 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200776 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +0200777 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200778 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200779 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +0200780 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
781 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700782 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200783 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +0200784 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200785 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200786 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +0200787 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
788 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700789 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200790 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -0700791 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200792 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200793 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +0200794 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +0200795 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
796 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -0700797 break;
798 }
799
800 params.aifs = pos[0] & 0x0f;
801 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
802 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200803 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +0200804 params.uapsd = uapsd;
805
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200806#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700807 wiphy_debug(local->hw.wiphy,
808 "WMM queue=%d aci=%d acm=%d aifs=%d "
809 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
810 queue, aci, acm,
811 params.aifs, params.cw_min, params.cw_max,
812 params.txop, params.uapsd);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100813#endif
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200814 if (drv_conf_tx(local, queue, &params))
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700815 wiphy_debug(local->hw.wiphy,
816 "failed to set TX queue parameters for queue %d\n",
817 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700818 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200819
820 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +0200821 sdata->vif.bss_conf.qos = true;
822 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
Jiri Bencf0706e82007-05-05 11:45:53 -0700823}
824
Johannes Berg7a5158e2008-10-08 10:59:33 +0200825static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
826 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +0200827{
Johannes Bergbda39332008-10-11 01:51:51 +0200828 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100829 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +0200830 bool use_protection;
831 bool use_short_preamble;
832 bool use_short_slot;
833
834 if (erp_valid) {
835 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
836 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
837 } else {
838 use_protection = false;
839 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
840 }
841
842 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Felix Fietkau43d35342010-01-15 03:00:48 +0100843 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
844 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +0200845
Johannes Berg471b3ef2007-12-28 14:32:58 +0100846 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +0100847 bss_conf->use_cts_prot = use_protection;
848 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200849 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200850
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200851 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200852 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100853 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200854 }
Daniel Draked9430a32007-07-27 15:43:24 +0200855
Johannes Berg7a5158e2008-10-08 10:59:33 +0200856 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +0200857 bss_conf->use_short_slot = use_short_slot;
858 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -0400859 }
860
861 return changed;
862}
863
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200864static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100865 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +0200866 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700867{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100868 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100869 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +0300870 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400871
Johannes Bergae5eb022008-10-14 16:58:37 +0200872 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200873 /* set timing information */
Juuso Oikarinen68542962010-06-09 13:43:26 +0300874 bss_conf->beacon_int = cbss->beacon_interval;
875 bss_conf->timestamp = cbss->tsf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400876
Johannes Berg77fdaa12009-07-07 03:45:17 +0200877 bss_info_changed |= BSS_CHANGED_BEACON_INT;
878 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100879 cbss->capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700880
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100881 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
882 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
883
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100884 sdata->u.mgd.associated = cbss;
885 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100886
Jouni Malinen17e4ec12010-03-29 23:28:30 -0700887 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
888
Johannes Bergb291ba12009-07-10 15:29:03 +0200889 /* just to be sure */
890 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
891 IEEE80211_STA_BEACON_POLL);
892
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200893 ieee80211_led_assoc(local, 1);
894
Johannes Berge5b900d2010-07-29 16:08:55 +0200895 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
896 bss_conf->dtim_period = bss->dtim_period;
897 else
898 bss_conf->dtim_period = 0;
899
Juuso Oikarinen68542962010-06-09 13:43:26 +0300900 bss_conf->assoc = 1;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200901 /*
902 * For now just always ask the driver to update the basic rateset
903 * when we have associated, we aren't checking whether it actually
904 * changed or not.
905 */
Johannes Bergae5eb022008-10-14 16:58:37 +0200906 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
Johannes Berg9cef8732009-05-14 13:10:14 +0200907
908 /* And the BSSID changed - we're associated now */
909 bss_info_changed |= BSS_CHANGED_BSSID;
910
Juuso Oikarinena97c13c2010-03-23 09:02:34 +0200911 /* Tell the driver to monitor connection quality (if supported) */
912 if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
Juuso Oikarinen68542962010-06-09 13:43:26 +0300913 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +0200914 bss_info_changed |= BSS_CHANGED_CQM;
915
Juuso Oikarinen68542962010-06-09 13:43:26 +0300916 /* Enable ARP filtering */
917 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
918 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
919 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
920 }
921
Johannes Bergae5eb022008-10-14 16:58:37 +0200922 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300923
Johannes Berg056508d2009-07-30 21:43:55 +0200924 mutex_lock(&local->iflist_mtx);
925 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +0200926 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +0200927 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +0200928
John W. Linville8a5b33f2010-01-06 15:39:39 -0500929 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200930 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700931}
932
Jouni Malinene69e95d2010-03-29 23:29:31 -0700933static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg53f73c02010-10-05 19:37:40 +0200934 bool remove_sta, bool tx)
Tomas Winkleraa458d12008-09-09 00:32:12 +0300935{
Johannes Berg46900292009-02-15 12:44:28 +0100936 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +0300937 struct ieee80211_local *local = sdata->local;
938 struct sta_info *sta;
Kalle Valoe0cb6862008-12-18 23:35:13 +0200939 u32 changed = 0, config_changed = 0;
Johannes Bergb291ba12009-07-10 15:29:03 +0200940 u8 bssid[ETH_ALEN];
Tomas Winkleraa458d12008-09-09 00:32:12 +0300941
Johannes Berg77fdaa12009-07-07 03:45:17 +0200942 ASSERT_MGD_MTX(ifmgd);
943
Johannes Bergb291ba12009-07-10 15:29:03 +0200944 if (WARN_ON(!ifmgd->associated))
945 return;
946
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100947 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Johannes Bergb291ba12009-07-10 15:29:03 +0200948
Johannes Berg77fdaa12009-07-07 03:45:17 +0200949 ifmgd->associated = NULL;
950 memset(ifmgd->bssid, 0, ETH_ALEN);
951
952 /*
953 * we need to commit the associated = NULL change because the
954 * scan code uses that to determine whether this iface should
955 * go to/wake up from powersave or not -- and could otherwise
956 * wake the queues erroneously.
957 */
958 smp_mb();
959
960 /*
961 * Thus, we can only afterwards stop the queues -- to account
962 * for the case where another CPU is finishing a scan at this
963 * time -- we don't want the scan code to enable queues.
964 */
Tomas Winkleraa458d12008-09-09 00:32:12 +0300965
John W. Linville8a5b33f2010-01-06 15:39:39 -0500966 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300967 netif_carrier_off(sdata->dev);
968
Johannes Berg2a419052010-06-10 10:21:29 +0200969 mutex_lock(&local->sta_mtx);
Johannes Bergabe60632009-11-25 17:46:18 +0100970 sta = sta_info_get(sdata, bssid);
Sujith4cad6c72010-02-10 14:52:21 +0530971 if (sta) {
Johannes Berg2a419052010-06-10 10:21:29 +0200972 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +0200973 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +0530974 }
Johannes Berg2a419052010-06-10 10:21:29 +0200975 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300976
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200977 changed |= ieee80211_reset_erp_info(sdata);
978
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200979 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +0200980 changed |= BSS_CHANGED_ASSOC;
981 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200982
Johannes Bergaa837e12009-05-07 16:16:24 +0200983 ieee80211_set_wmm_default(sdata);
984
Johannes Berg47979382009-01-07 10:13:27 +0100985 /* channel(_type) changes are handled by ieee80211_hw_config */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200986 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergae5eb022008-10-14 16:58:37 +0200987
Johannes Berg413ad502009-05-08 21:21:06 +0200988 /* on the next assoc, re-program HT parameters */
989 sdata->ht_opmode_valid = false;
990
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +0530991 local->power_constr_level = 0;
992
Kalle Valo520eb822008-12-18 23:35:27 +0200993 del_timer_sync(&local->dynamic_ps_timer);
994 cancel_work_sync(&local->dynamic_ps_enable_work);
995
Kalle Valoe0cb6862008-12-18 23:35:13 +0200996 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
997 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
998 config_changed |= IEEE80211_CONF_CHANGE_PS;
999 }
1000
1001 ieee80211_hw_config(local, config_changed);
Johannes Berg9cef8732009-05-14 13:10:14 +02001002
Juuso Oikarinen68542962010-06-09 13:43:26 +03001003 /* Disable ARP filtering */
1004 if (sdata->vif.bss_conf.arp_filter_enabled) {
1005 sdata->vif.bss_conf.arp_filter_enabled = false;
1006 changed |= BSS_CHANGED_ARP_FILTER;
1007 }
1008
Johannes Berg0aaffa92010-05-05 15:28:27 +02001009 /* The BSSID (not really interesting) and HT changed */
1010 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001011 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001012
Jouni Malinene69e95d2010-03-29 23:29:31 -07001013 if (remove_sta)
1014 sta_info_destroy_addr(sdata, bssid);
Johannes Bergb9dcf712010-08-27 12:35:54 +02001015
1016 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1017 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1018 del_timer_sync(&sdata->u.mgd.timer);
1019 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001020}
Jiri Bencf0706e82007-05-05 11:45:53 -07001021
Kalle Valo3cf335d2009-03-22 21:57:06 +02001022void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1023 struct ieee80211_hdr *hdr)
1024{
1025 /*
1026 * We can postpone the mgd.timer whenever receiving unicast frames
1027 * from AP because we know that the connection is working both ways
1028 * at that time. But multicast frames (and hence also beacons) must
1029 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001030 * data idle periods for sending the periodic probe request to the
1031 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001032 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001033 if (is_multicast_ether_addr(hdr->addr1))
1034 return;
1035
Stanislaw Gruszka05e7c992011-02-18 09:05:08 +01001036 /*
1037 * In case we receive frames after disassociation.
1038 */
1039 if (!sdata->u.mgd.associated)
1040 return;
1041
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001042 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001043}
Jiri Bencf0706e82007-05-05 11:45:53 -07001044
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001045static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1046{
1047 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1048
1049 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1050 IEEE80211_STA_CONNECTION_POLL)))
1051 return;
1052
1053 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1054 IEEE80211_STA_BEACON_POLL);
1055 mutex_lock(&sdata->local->iflist_mtx);
1056 ieee80211_recalc_ps(sdata->local, -1);
1057 mutex_unlock(&sdata->local->iflist_mtx);
1058
1059 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1060 return;
1061
1062 /*
1063 * We've received a probe response, but are not sure whether
1064 * we have or will be receiving any beacons or data, so let's
1065 * schedule the timers again, just in case.
1066 */
1067 ieee80211_sta_reset_beacon_monitor(sdata);
1068
1069 mod_timer(&ifmgd->conn_mon_timer,
1070 round_jiffies_up(jiffies +
1071 IEEE80211_CONNECTION_IDLE_TIME));
1072}
1073
1074void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001075 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001076{
Felix Fietkau75706d02010-12-02 21:01:07 +01001077 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001078 return;
1079
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001080 if (ack)
1081 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001082
1083 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1084 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001085 if (ack)
1086 sdata->u.mgd.probe_send_count = 0;
1087 else
1088 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001089 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1090 }
1091}
1092
Maxim Levitskya43abf22009-07-31 18:54:12 +03001093static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1094{
1095 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1096 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001097 u8 *dst = ifmgd->associated->bssid;
1098 u8 unicast_limit = max(1, IEEE80211_MAX_PROBE_TRIES - 3);
1099
1100 /*
1101 * Try sending broadcast probe requests for the last three
1102 * probe requests after the first ones failed since some
1103 * buggy APs only support broadcast probe requests.
1104 */
1105 if (ifmgd->probe_send_count >= unicast_limit)
1106 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001107
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001108 /*
1109 * When the hardware reports an accurate Tx ACK status, it's
1110 * better to send a nullfunc frame instead of a probe request,
1111 * as it will kick us off the AP quickly if we aren't associated
1112 * anymore. The timeout will be reset if the frame is ACKed by
1113 * the AP.
1114 */
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001115 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1116 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001117 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001118 } else {
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001119 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1120 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
1121 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001122
1123 ifmgd->probe_send_count++;
1124 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1125 run_again(ifmgd, ifmgd->probe_timeout);
1126}
1127
Johannes Bergb291ba12009-07-10 15:29:03 +02001128static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1129 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001130{
Kalle Valo04de8382009-03-22 21:57:35 +02001131 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001132 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001133
Johannes Berg9607e6b2009-12-23 13:15:31 +01001134 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001135 return;
1136
Luis R. Rodriguez91a3bd72009-07-23 16:37:47 -07001137 if (sdata->local->scanning)
1138 return;
1139
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001140 if (sdata->local->tmp_channel)
1141 return;
1142
Johannes Berg77fdaa12009-07-07 03:45:17 +02001143 mutex_lock(&ifmgd->mtx);
1144
1145 if (!ifmgd->associated)
1146 goto out;
1147
Michael Buescha8604022009-04-15 14:41:22 -04001148#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergb291ba12009-07-10 15:29:03 +02001149 if (beacon && net_ratelimit())
1150 printk(KERN_DEBUG "%s: detected beacon loss from AP "
Johannes Berg47846c92009-11-25 17:46:19 +01001151 "- sending probe request\n", sdata->name);
Michael Buescha8604022009-04-15 14:41:22 -04001152#endif
Kalle Valo04de8382009-03-22 21:57:35 +02001153
Johannes Bergb291ba12009-07-10 15:29:03 +02001154 /*
1155 * The driver/our work has already reported this event or the
1156 * connection monitoring has kicked in and we have already sent
1157 * a probe request. Or maybe the AP died and the driver keeps
1158 * reporting until we disassociate...
1159 *
1160 * In either case we have to ignore the current call to this
1161 * function (except for setting the correct probe reason bit)
1162 * because otherwise we would reset the timer every time and
1163 * never check whether we received a probe response!
1164 */
1165 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1166 IEEE80211_STA_CONNECTION_POLL))
1167 already = true;
1168
1169 if (beacon)
1170 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1171 else
1172 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1173
1174 if (already)
1175 goto out;
1176
Johannes Berg4e751842009-06-10 15:16:52 +02001177 mutex_lock(&sdata->local->iflist_mtx);
1178 ieee80211_recalc_ps(sdata->local, -1);
1179 mutex_unlock(&sdata->local->iflist_mtx);
1180
Maxim Levitskya43abf22009-07-31 18:54:12 +03001181 ifmgd->probe_send_count = 0;
1182 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001183 out:
1184 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001185}
1186
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001187struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1188 struct ieee80211_vif *vif)
1189{
1190 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1191 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1192 struct sk_buff *skb;
1193 const u8 *ssid;
1194
1195 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1196 return NULL;
1197
1198 ASSERT_MGD_MTX(ifmgd);
1199
1200 if (!ifmgd->associated)
1201 return NULL;
1202
1203 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1204 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
1205 ssid + 2, ssid[1], NULL, 0);
1206
1207 return skb;
1208}
1209EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1210
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001211static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1212{
1213 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1214 struct ieee80211_local *local = sdata->local;
1215 u8 bssid[ETH_ALEN];
1216
1217 mutex_lock(&ifmgd->mtx);
1218 if (!ifmgd->associated) {
1219 mutex_unlock(&ifmgd->mtx);
1220 return;
1221 }
1222
1223 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1224
1225 printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
1226
Johannes Berg53f73c02010-10-05 19:37:40 +02001227 ieee80211_set_disassoc(sdata, true, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001228 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001229
1230 mutex_lock(&local->mtx);
1231 ieee80211_recalc_idle(local);
1232 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001233 /*
1234 * must be outside lock due to cfg80211,
1235 * but that's not a problem.
1236 */
1237 ieee80211_send_deauth_disassoc(sdata, bssid,
1238 IEEE80211_STYPE_DEAUTH,
1239 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001240 NULL, true);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001241}
1242
1243void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001244{
1245 struct ieee80211_sub_if_data *sdata =
1246 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001247 u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02001248
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001249 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1250 __ieee80211_connection_loss(sdata);
1251 else
1252 ieee80211_mgd_probe_ap(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001253}
1254
Kalle Valo04de8382009-03-22 21:57:35 +02001255void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1256{
1257 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001258 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001259
Johannes Bergb5878a22010-04-07 16:48:40 +02001260 trace_api_beacon_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);
Kalle Valo04de8382009-03-22 21:57:35 +02001264}
1265EXPORT_SYMBOL(ieee80211_beacon_loss);
1266
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001267void ieee80211_connection_loss(struct ieee80211_vif *vif)
1268{
1269 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1270 struct ieee80211_hw *hw = &sdata->local->hw;
1271
Johannes Bergb5878a22010-04-07 16:48:40 +02001272 trace_api_connection_loss(sdata);
1273
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001274 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1275 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1276}
1277EXPORT_SYMBOL(ieee80211_connection_loss);
1278
1279
Johannes Berg77fdaa12009-07-07 03:45:17 +02001280static enum rx_mgmt_action __must_check
1281ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001282 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001283{
Johannes Berg46900292009-02-15 12:44:28 +01001284 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001285 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001286 u16 reason_code;
1287
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001288 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001289 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001290
Johannes Berg77fdaa12009-07-07 03:45:17 +02001291 ASSERT_MGD_MTX(ifmgd);
1292
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001293 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001294
1295 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1296
Johannes Berg77fdaa12009-07-07 03:45:17 +02001297 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001298 sdata->name, bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001299
Johannes Berg53f73c02010-10-05 19:37:40 +02001300 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001301 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001302 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001303 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001304
1305 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001306}
1307
1308
Johannes Berg77fdaa12009-07-07 03:45:17 +02001309static enum rx_mgmt_action __must_check
1310ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1311 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001312{
Johannes Berg46900292009-02-15 12:44:28 +01001313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001314 u16 reason_code;
1315
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001316 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001317 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001318
Johannes Berg77fdaa12009-07-07 03:45:17 +02001319 ASSERT_MGD_MTX(ifmgd);
1320
1321 if (WARN_ON(!ifmgd->associated))
1322 return RX_MGMT_NONE;
1323
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001324 if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001325 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001326
1327 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1328
Johannes Berg0ff71612009-09-26 14:45:41 +02001329 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001330 sdata->name, mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001331
Johannes Berg53f73c02010-10-05 19:37:40 +02001332 ieee80211_set_disassoc(sdata, true, false);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001333 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001334 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001335 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001336 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001337}
1338
1339
Johannes Bergaf6b6372009-12-23 13:15:35 +01001340static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1341 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001342{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001343 struct ieee80211_sub_if_data *sdata = wk->sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001344 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001345 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001346 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001347 struct sta_info *sta;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001348 struct cfg80211_bss *cbss = wk->assoc.bss;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001349 u8 *pos;
Johannes Berg881d9482009-01-21 15:13:48 +01001350 u32 rates, basic_rates;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001351 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001352 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02001353 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02001354 u32 changed = 0;
Johannes Berg5d1ec852009-12-02 12:43:43 +01001355 int i, j, err;
1356 bool have_higher_than_11mbit = false;
Johannes Bergae5eb022008-10-14 16:58:37 +02001357 u16 ap_ht_cap_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07001358
Johannes Bergaf6b6372009-12-23 13:15:35 +01001359 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07001360
Jiri Bencf0706e82007-05-05 11:45:53 -07001361 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001362 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07001363
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001364 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1365 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Johannes Berg47846c92009-11-25 17:46:19 +01001366 "set\n", sdata->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001367 aid &= ~(BIT(15) | BIT(14));
1368
Johannes Bergaf6b6372009-12-23 13:15:35 +01001369 pos = mgmt->u.assoc_resp.variable;
1370 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1371
Jiri Bencf0706e82007-05-05 11:45:53 -07001372 if (!elems.supp_rates) {
1373 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001374 sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001375 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001376 }
1377
Johannes Berg46900292009-02-15 12:44:28 +01001378 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001379
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001380 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001381 if (!sta) {
Johannes Berg5d1ec852009-12-02 12:43:43 +01001382 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1383 " the AP\n", sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001384 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001385 }
1386
Johannes Berg5d1ec852009-12-02 12:43:43 +01001387 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1388 WLAN_STA_ASSOC_AP);
1389 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1390 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1391
Jiri Bencf0706e82007-05-05 11:45:53 -07001392 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001393 basic_rates = 0;
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001394 sband = local->hw.wiphy->bands[wk->chan->band];
Johannes Berg8318d782008-01-24 19:38:38 +01001395
Jiri Bencf0706e82007-05-05 11:45:53 -07001396 for (i = 0; i < elems.supp_rates_len; i++) {
1397 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Tomas Winklerd61272c2008-10-30 17:08:08 +02001398 bool is_basic = !!(elems.supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001399
1400 if (rate > 110)
1401 have_higher_than_11mbit = true;
1402
1403 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001404 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001405 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001406 if (is_basic)
1407 basic_rates |= BIT(j);
1408 break;
1409 }
Johannes Berg8318d782008-01-24 19:38:38 +01001410 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001411 }
Johannes Berg8318d782008-01-24 19:38:38 +01001412
Jiri Bencf0706e82007-05-05 11:45:53 -07001413 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1414 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg7e0986c2009-04-19 13:22:11 +02001415 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001416
1417 if (rate > 110)
1418 have_higher_than_11mbit = true;
1419
1420 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001421 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001422 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001423 if (is_basic)
1424 basic_rates |= BIT(j);
1425 break;
1426 }
Johannes Berg8318d782008-01-24 19:38:38 +01001427 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001428 }
Johannes Berg8318d782008-01-24 19:38:38 +01001429
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001430 sta->sta.supp_rates[wk->chan->band] = rates;
Johannes Bergbda39332008-10-11 01:51:51 +02001431 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01001432
1433 /* cf. IEEE 802.11 9.2.12 */
Luis R. Rodrigueze7480bb2010-10-01 17:05:19 -04001434 if (wk->chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg8318d782008-01-24 19:38:38 +01001435 have_higher_than_11mbit)
1436 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1437 else
1438 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001439
Johannes Bergab1faea2009-07-01 21:41:17 +02001440 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001441 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001442 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02001443
1444 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001445
Johannes Berg4b7679a2008-09-18 18:14:18 +02001446 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001447
Johannes Berg46900292009-02-15 12:44:28 +01001448 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Jouni Malinen5394af42009-01-08 13:31:59 +02001449 set_sta_flags(sta, WLAN_STA_MFP);
1450
Johannes Bergddf4ac52008-10-22 11:41:38 +02001451 if (elems.wmm_param)
Johannes Berg07346f812008-05-03 01:02:02 +02001452 set_sta_flags(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02001453
Johannes Berg5d1ec852009-12-02 12:43:43 +01001454 err = sta_info_insert(sta);
1455 sta = NULL;
1456 if (err) {
1457 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1458 " the AP (error %d)\n", sdata->name, err);
Johannes Berg90be5612010-01-08 19:01:07 +01001459 return false;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001460 }
1461
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03001462 /*
1463 * Always handle WMM once after association regardless
1464 * of the first value the AP uses. Setting -1 here has
1465 * that effect because the AP values is an unsigned
1466 * 4-bit value.
1467 */
1468 ifmgd->wmm_last_param_set = -1;
1469
Johannes Bergddf4ac52008-10-22 11:41:38 +02001470 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02001471 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07001472 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02001473 else
1474 ieee80211_set_wmm_default(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07001475
Johannes Berge4da8c32009-12-23 13:15:43 +01001476 local->oper_channel = wk->chan;
1477
Johannes Bergae5eb022008-10-14 16:58:37 +02001478 if (elems.ht_info_elem && elems.wmm_param &&
Johannes Bergaf6b6372009-12-23 13:15:35 +01001479 (sdata->local->hw.queues >= 4) &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001480 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02001481 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001482 cbss->bssid, ap_ht_cap_flags);
Johannes Bergae5eb022008-10-14 16:58:37 +02001483
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001484 /* set AID and assoc capability,
1485 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01001486 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001487 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001488 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001489
Kalle Valo15b7b062009-03-22 21:57:14 +02001490 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01001491 * If we're using 4-addr mode, let the AP know that we're
1492 * doing so, so that it can create the STA VLAN on its side
1493 */
1494 if (ifmgd->use_4addr)
1495 ieee80211_send_4addr_nullfunc(local, sdata);
1496
1497 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02001498 * Start timer to probe the connection to the AP now.
1499 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02001500 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001501 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001502 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02001503
Johannes Bergaf6b6372009-12-23 13:15:35 +01001504 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001505}
1506
1507
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001508static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001509 struct ieee80211_mgmt *mgmt,
1510 size_t len,
1511 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001512 struct ieee802_11_elems *elems,
1513 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07001514{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001515 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001516 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02001517 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01001518 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01001519 bool need_ps = false;
1520
1521 if (sdata->u.mgd.associated) {
1522 bss = (void *)sdata->u.mgd.associated->priv;
1523 /* not previously set so we may need to recalc */
1524 need_ps = !bss->dtim_period;
1525 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001526
Johannes Berg5bda6172008-09-08 11:05:10 +02001527 if (elems->ds_params && elems->ds_params_len == 1)
1528 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1529 else
1530 freq = rx_status->freq;
1531
1532 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1533
1534 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1535 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001536
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001537 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01001538 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001539 if (bss)
1540 ieee80211_rx_bss_put(local, bss);
1541
1542 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001543 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001544
Johannes Berg56007a02010-01-26 14:19:52 +01001545 if (need_ps) {
1546 mutex_lock(&local->iflist_mtx);
1547 ieee80211_recalc_ps(local, -1);
1548 mutex_unlock(&local->iflist_mtx);
1549 }
1550
Sujithc481ec92009-01-06 09:28:37 +05301551 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001552 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001553 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05301554 struct ieee80211_channel_sw_ie *sw_elem =
1555 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Berg5ce6e432010-05-11 16:20:57 +02001556 ieee80211_sta_process_chanswitch(sdata, sw_elem,
1557 bss, rx_status->mactime);
Sujithc481ec92009-01-06 09:28:37 +05301558 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001559}
1560
1561
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001562static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01001563 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001564{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001565 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02001566 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001567 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1568 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03001569 struct ieee802_11_elems elems;
1570
Kalle Valo15b7b062009-03-22 21:57:14 +02001571 ifmgd = &sdata->u.mgd;
1572
Johannes Berg77fdaa12009-07-07 03:45:17 +02001573 ASSERT_MGD_MTX(ifmgd);
1574
Johannes Berg47846c92009-11-25 17:46:19 +01001575 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03001576 return; /* ignore ProbeResp to foreign address */
1577
Ester Kummerae6a44e2008-06-27 18:54:48 +03001578 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1579 if (baselen > len)
1580 return;
1581
1582 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1583 &elems);
1584
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001585 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001586
Johannes Berg77fdaa12009-07-07 03:45:17 +02001587 if (ifmgd->associated &&
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001588 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
1589 ieee80211_reset_ap_probe(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07001590}
1591
Johannes Bergd91f36d2009-04-16 13:17:26 +02001592/*
1593 * This is the canonical list of information elements we care about,
1594 * the filter code also gives us all changes to the Microsoft OUI
1595 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1596 *
1597 * We implement beacon filtering in software since that means we can
1598 * avoid processing the frame here and in cfg80211, and userspace
1599 * will not be able to tell whether the hardware supports it or not.
1600 *
1601 * XXX: This list needs to be dynamic -- userspace needs to be able to
1602 * add items it requires. It also needs to be able to tell us to
1603 * look out for other vendor IEs.
1604 */
1605static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02001606 (1ULL << WLAN_EID_COUNTRY) |
1607 (1ULL << WLAN_EID_ERP_INFO) |
1608 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1609 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1610 (1ULL << WLAN_EID_HT_CAPABILITY) |
1611 (1ULL << WLAN_EID_HT_INFORMATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001612
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001613static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001614 struct ieee80211_mgmt *mgmt,
1615 size_t len,
1616 struct ieee80211_rx_status *rx_status)
1617{
Johannes Bergd91f36d2009-04-16 13:17:26 +02001618 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001619 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001620 size_t baselen;
1621 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001622 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001623 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001624 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001625 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02001626 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001627 u8 *bssid;
1628
1629 ASSERT_MGD_MTX(ifmgd);
Jiri Bencf0706e82007-05-05 11:45:53 -07001630
Ester Kummerae6a44e2008-06-27 18:54:48 +03001631 /* Process beacon from the current BSS */
1632 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1633 if (baselen > len)
1634 return;
1635
Johannes Bergd91f36d2009-04-16 13:17:26 +02001636 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07001637 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001638
Johannes Bergb291ba12009-07-10 15:29:03 +02001639 /*
1640 * We might have received a number of frames, among them a
1641 * disassoc frame and a beacon...
1642 */
1643 if (!ifmgd->associated)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001644 return;
1645
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001646 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001647
Johannes Bergb291ba12009-07-10 15:29:03 +02001648 /*
1649 * And in theory even frames from a different AP we were just
1650 * associated to a split-second ago!
1651 */
1652 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001653 return;
1654
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001655 /* Track average RSSI from the Beacon frames of the current AP */
1656 ifmgd->last_beacon_signal = rx_status->signal;
1657 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1658 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03001659 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001660 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03001661 ifmgd->count_beacon_signal = 1;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001662 } else {
1663 ifmgd->ave_beacon_signal =
1664 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1665 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1666 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03001667 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001668 }
1669 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03001670 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001671 !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1672 int sig = ifmgd->ave_beacon_signal / 16;
1673 int last_event = ifmgd->last_cqm_event_signal;
1674 int thold = bss_conf->cqm_rssi_thold;
1675 int hyst = bss_conf->cqm_rssi_hyst;
1676 if (sig < thold &&
1677 (last_event == 0 || sig < last_event - hyst)) {
1678 ifmgd->last_cqm_event_signal = sig;
1679 ieee80211_cqm_rssi_notify(
1680 &sdata->vif,
1681 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1682 GFP_KERNEL);
1683 } else if (sig > thold &&
1684 (last_event == 0 || sig > last_event + hyst)) {
1685 ifmgd->last_cqm_event_signal = sig;
1686 ieee80211_cqm_rssi_notify(
1687 &sdata->vif,
1688 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1689 GFP_KERNEL);
1690 }
1691 }
1692
Johannes Bergb291ba12009-07-10 15:29:03 +02001693 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Jouni Malinen92778182009-05-14 21:15:36 +03001694#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1695 if (net_ratelimit()) {
1696 printk(KERN_DEBUG "%s: cancelling probereq poll due "
Johannes Berg47846c92009-11-25 17:46:19 +01001697 "to a received beacon\n", sdata->name);
Jouni Malinen92778182009-05-14 21:15:36 +03001698 }
1699#endif
Johannes Bergb291ba12009-07-10 15:29:03 +02001700 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Johannes Berg4e751842009-06-10 15:16:52 +02001701 mutex_lock(&local->iflist_mtx);
1702 ieee80211_recalc_ps(local, -1);
1703 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03001704 }
1705
Johannes Bergb291ba12009-07-10 15:29:03 +02001706 /*
1707 * Push the beacon loss detection into the future since
1708 * we are processing a beacon from the AP just now.
1709 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04001710 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001711
Johannes Bergd91f36d2009-04-16 13:17:26 +02001712 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1713 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1714 len - baselen, &elems,
1715 care_about_ies, ncrc);
1716
1717 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02001718 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1719 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001720
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001721 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
Jouni Malinen30196672009-05-19 17:01:43 +03001722 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1723 true);
Johannes Bergd91f36d2009-04-16 13:17:26 +02001724
Johannes Berg4ced3f72010-07-19 16:39:04 +02001725 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jouni Malinen30196672009-05-19 17:01:43 +03001726 elems.wmm_param_len);
1727 }
Johannes Bergfe3fa822008-09-08 11:05:09 +02001728
Vivek Natarajan25c9c872009-03-02 20:20:30 +05301729 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02001730 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02001731 if (local->hw.conf.dynamic_ps_timeout > 0) {
1732 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1733 ieee80211_hw_config(local,
1734 IEEE80211_CONF_CHANGE_PS);
1735 ieee80211_send_nullfunc(local, sdata, 0);
1736 } else {
1737 local->pspolling = true;
1738
1739 /*
1740 * Here is assumed that the driver will be
1741 * able to send ps-poll frame and receive a
1742 * response even though power save mode is
1743 * enabled, but some drivers might require
1744 * to disable power save here. This needs
1745 * to be investigated.
1746 */
1747 ieee80211_send_pspoll(local, sdata);
1748 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08001749 }
1750 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001751
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001752 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03001753 return;
1754 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03001755 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03001756
Johannes Berg7a5158e2008-10-08 10:59:33 +02001757 if (elems.erp_info && elems.erp_info_len >= 1) {
1758 erp_valid = true;
1759 erp_value = elems.erp_info[0];
1760 } else {
1761 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04001762 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001763 changed |= ieee80211_handle_bss_capability(sdata,
1764 le16_to_cpu(mgmt->u.beacon.capab_info),
1765 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07001766
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001767
Vasanthakumar Thiagarajan53d6f81c2009-02-11 22:18:49 +05301768 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02001769 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001770 struct sta_info *sta;
1771 struct ieee80211_supported_band *sband;
1772 u16 ap_ht_cap_flags;
1773
1774 rcu_read_lock();
1775
Johannes Bergabe60632009-11-25 17:46:18 +01001776 sta = sta_info_get(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001777 if (WARN_ON(!sta)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001778 rcu_read_unlock();
1779 return;
1780 }
1781
1782 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1783
1784 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1785 elems.ht_cap_elem, &sta->sta.ht_cap);
1786
1787 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1788
1789 rcu_read_unlock();
1790
1791 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001792 bssid, ap_ht_cap_flags);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001793 }
1794
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001795 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001796 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301797 /* TODO: IBSS also needs this */
1798 if (elems.pwr_constr_elem)
1799 ieee80211_handle_pwr_constr(sdata,
1800 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1801 elems.pwr_constr_elem,
1802 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001803 }
1804
Johannes Berg471b3ef2007-12-28 14:32:58 +01001805 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001806}
1807
Johannes Berg1fa57d02010-06-10 10:21:32 +02001808void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1809 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07001810{
Johannes Berg77fdaa12009-07-07 03:45:17 +02001811 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001812 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07001813 struct ieee80211_mgmt *mgmt;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001814 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001815 u16 fc;
1816
Jiri Bencf0706e82007-05-05 11:45:53 -07001817 rx_status = (struct ieee80211_rx_status *) skb->cb;
1818 mgmt = (struct ieee80211_mgmt *) skb->data;
1819 fc = le16_to_cpu(mgmt->frame_control);
1820
Johannes Berg77fdaa12009-07-07 03:45:17 +02001821 mutex_lock(&ifmgd->mtx);
1822
1823 if (ifmgd->associated &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001824 memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001825 switch (fc & IEEE80211_FCTL_STYPE) {
1826 case IEEE80211_STYPE_BEACON:
1827 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1828 rx_status);
1829 break;
1830 case IEEE80211_STYPE_PROBE_RESP:
Johannes Bergaf6b6372009-12-23 13:15:35 +01001831 ieee80211_rx_mgmt_probe_resp(sdata, skb);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001832 break;
1833 case IEEE80211_STYPE_DEAUTH:
Johannes Berg63f170e2009-12-23 13:15:33 +01001834 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001835 break;
1836 case IEEE80211_STYPE_DISASSOC:
1837 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1838 break;
1839 case IEEE80211_STYPE_ACTION:
Johannes Berg8b9a4e62010-05-28 15:22:58 +02001840 switch (mgmt->u.action.category) {
Johannes Berg8b9a4e62010-05-28 15:22:58 +02001841 case WLAN_CATEGORY_SPECTRUM_MGMT:
1842 ieee80211_sta_process_chanswitch(sdata,
1843 &mgmt->u.action.u.chan_switch.sw_elem,
1844 (void *)ifmgd->associated->priv,
1845 rx_status->mactime);
1846 break;
1847 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02001848 }
1849 mutex_unlock(&ifmgd->mtx);
1850
1851 switch (rma) {
1852 case RX_MGMT_NONE:
1853 /* no action */
1854 break;
1855 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02001856 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001857 break;
1858 case RX_MGMT_CFG80211_DISASSOC:
Holger Schurigce470612009-10-13 13:28:13 +02001859 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001860 break;
1861 default:
1862 WARN(1, "unexpected: %d", rma);
1863 }
Johannes Berg36b3a622010-06-10 10:21:33 +02001864 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001865 }
1866
Johannes Berg77fdaa12009-07-07 03:45:17 +02001867 mutex_unlock(&ifmgd->mtx);
1868
Johannes Berg63f170e2009-12-23 13:15:33 +01001869 if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
Johannes Bergb054b742010-06-07 21:50:07 +02001870 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
1871 struct ieee80211_local *local = sdata->local;
1872 struct ieee80211_work *wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001873
Johannes Berga1699b72010-07-30 16:46:07 +02001874 mutex_lock(&local->mtx);
Johannes Bergb054b742010-06-07 21:50:07 +02001875 list_for_each_entry(wk, &local->work_list, list) {
1876 if (wk->sdata != sdata)
1877 continue;
1878
Johannes Berge5b900d2010-07-29 16:08:55 +02001879 if (wk->type != IEEE80211_WORK_ASSOC &&
1880 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
Johannes Bergb054b742010-06-07 21:50:07 +02001881 continue;
1882
1883 if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
1884 continue;
1885 if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
1886 continue;
1887
1888 /*
1889 * Printing the message only here means we can't
1890 * spuriously print it, but it also means that it
1891 * won't be printed when the frame comes in before
1892 * we even tried to associate or in similar cases.
1893 *
1894 * Ultimately, I suspect cfg80211 should print the
1895 * messages instead.
1896 */
1897 printk(KERN_DEBUG
1898 "%s: deauthenticated from %pM (Reason: %u)\n",
1899 sdata->name, mgmt->bssid,
1900 le16_to_cpu(mgmt->u.deauth.reason_code));
1901
1902 list_del_rcu(&wk->list);
1903 free_work(wk);
1904 break;
1905 }
Johannes Berga1699b72010-07-30 16:46:07 +02001906 mutex_unlock(&local->mtx);
Johannes Bergb054b742010-06-07 21:50:07 +02001907
Johannes Berg77fdaa12009-07-07 03:45:17 +02001908 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Bergb054b742010-06-07 21:50:07 +02001909 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001910}
1911
Johannes Berg9c6bd792008-09-11 00:01:52 +02001912static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07001913{
1914 struct ieee80211_sub_if_data *sdata =
1915 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01001916 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001917 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001918
Johannes Berg5bb644a2009-05-17 11:40:42 +02001919 if (local->quiescing) {
1920 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
1921 return;
1922 }
1923
Johannes Berg64592c82010-06-10 10:21:31 +02001924 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001925}
1926
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001927static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
1928 u8 *bssid)
1929{
1930 struct ieee80211_local *local = sdata->local;
1931 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1932
1933 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1934 IEEE80211_STA_BEACON_POLL);
1935
1936 ieee80211_set_disassoc(sdata, true, true);
1937 mutex_unlock(&ifmgd->mtx);
1938 mutex_lock(&local->mtx);
1939 ieee80211_recalc_idle(local);
1940 mutex_unlock(&local->mtx);
1941 /*
1942 * must be outside lock due to cfg80211,
1943 * but that's not a problem.
1944 */
1945 ieee80211_send_deauth_disassoc(sdata, bssid,
1946 IEEE80211_STYPE_DEAUTH,
1947 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1948 NULL, true);
1949 mutex_lock(&ifmgd->mtx);
1950}
1951
Johannes Berg1fa57d02010-06-10 10:21:32 +02001952void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02001953{
Johannes Berg60f8b392008-09-08 17:44:22 +02001954 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02001955 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02001956
Johannes Berg77fdaa12009-07-07 03:45:17 +02001957 /* then process the rest of the work */
1958 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02001959
Johannes Bergb291ba12009-07-10 15:29:03 +02001960 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1961 IEEE80211_STA_CONNECTION_POLL) &&
1962 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03001963 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01001964 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001965
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001966 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001967
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01001968 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1969 max_tries = IEEE80211_MAX_NULLFUNC_TRIES;
1970 else
1971 max_tries = IEEE80211_MAX_PROBE_TRIES;
1972
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001973 /* ACK received for nullfunc probing frame */
1974 if (!ifmgd->probe_send_count)
1975 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001976 else if (ifmgd->nullfunc_failed) {
1977 if (ifmgd->probe_send_count < max_tries) {
1978#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1979 wiphy_debug(local->hw.wiphy,
1980 "%s: No ack for nullfunc frame to"
1981 " AP %pM, try %d\n",
1982 sdata->name, bssid,
1983 ifmgd->probe_send_count);
1984#endif
1985 ieee80211_mgd_probe_ap_send(sdata);
1986 } else {
1987#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1988 wiphy_debug(local->hw.wiphy,
1989 "%s: No ack for nullfunc frame to"
1990 " AP %pM, disconnecting.\n",
Felix Fietkauc658e5d2010-12-07 04:40:18 +01001991 sdata->name, bssid);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001992#endif
1993 ieee80211_sta_connection_lost(sdata, bssid);
1994 }
1995 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02001996 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001997 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1998#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1999 wiphy_debug(local->hw.wiphy,
2000 "%s: Failed to send nullfunc to AP %pM"
2001 " after %dms, disconnecting.\n",
2002 sdata->name,
2003 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
2004#endif
2005 ieee80211_sta_connection_lost(sdata, bssid);
2006 } else if (ifmgd->probe_send_count < max_tries) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002007#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Ben Greearb38afa82010-10-07 16:12:06 -07002008 wiphy_debug(local->hw.wiphy,
2009 "%s: No probe response from AP %pM"
2010 " after %dms, try %d\n",
2011 sdata->name,
2012 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ,
2013 ifmgd->probe_send_count);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002014#endif
2015 ieee80211_mgd_probe_ap_send(sdata);
2016 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002017 /*
2018 * We actually lost the connection ... or did we?
2019 * Let's make sure!
2020 */
Ben Greearb38afa82010-10-07 16:12:06 -07002021 wiphy_debug(local->hw.wiphy,
2022 "%s: No probe response from AP %pM"
2023 " after %dms, disconnecting.\n",
2024 sdata->name,
2025 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002026
2027 ieee80211_sta_connection_lost(sdata, bssid);
Johannes Bergb291ba12009-07-10 15:29:03 +02002028 }
2029 }
2030
Johannes Berg77fdaa12009-07-07 03:45:17 +02002031 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002032}
Johannes Berg0a51b272008-09-08 17:44:25 +02002033
Johannes Bergb291ba12009-07-10 15:29:03 +02002034static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2035{
2036 struct ieee80211_sub_if_data *sdata =
2037 (struct ieee80211_sub_if_data *) data;
2038 struct ieee80211_local *local = sdata->local;
2039
2040 if (local->quiescing)
2041 return;
2042
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002043 ieee80211_queue_work(&sdata->local->hw,
2044 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002045}
2046
2047static void ieee80211_sta_conn_mon_timer(unsigned long data)
2048{
2049 struct ieee80211_sub_if_data *sdata =
2050 (struct ieee80211_sub_if_data *) data;
2051 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2052 struct ieee80211_local *local = sdata->local;
2053
2054 if (local->quiescing)
2055 return;
2056
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002057 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002058}
2059
2060static void ieee80211_sta_monitor_work(struct work_struct *work)
2061{
2062 struct ieee80211_sub_if_data *sdata =
2063 container_of(work, struct ieee80211_sub_if_data,
2064 u.mgd.monitor_work);
2065
2066 ieee80211_mgd_probe_ap(sdata, false);
2067}
2068
Johannes Berga1678f82008-09-11 00:01:47 +02002069static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2070{
Kalle Vaload935682009-04-19 08:47:19 +03002071 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Bergb291ba12009-07-10 15:29:03 +02002072 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2073 IEEE80211_STA_CONNECTION_POLL);
Kalle Vaload935682009-04-19 08:47:19 +03002074
Johannes Bergb291ba12009-07-10 15:29:03 +02002075 /* let's probe the connection once */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002076 ieee80211_queue_work(&sdata->local->hw,
Johannes Bergb291ba12009-07-10 15:29:03 +02002077 &sdata->u.mgd.monitor_work);
2078 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02002079 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03002080 }
Johannes Berga1678f82008-09-11 00:01:47 +02002081}
2082
Johannes Berg5bb644a2009-05-17 11:40:42 +02002083#ifdef CONFIG_PM
2084void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2085{
2086 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2087
2088 /*
2089 * we need to use atomic bitops for the running bits
2090 * only because both timers might fire at the same
2091 * time -- the code here is properly synchronised.
2092 */
2093
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002094 cancel_work_sync(&ifmgd->request_smps_work);
2095
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002096 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002097 if (del_timer_sync(&ifmgd->timer))
2098 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2099
2100 cancel_work_sync(&ifmgd->chswitch_work);
2101 if (del_timer_sync(&ifmgd->chswitch_timer))
2102 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002103
2104 cancel_work_sync(&ifmgd->monitor_work);
2105 /* these will just be re-established on connection */
2106 del_timer_sync(&ifmgd->conn_mon_timer);
2107 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002108}
2109
2110void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2111{
2112 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2113
2114 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2115 add_timer(&ifmgd->timer);
2116 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2117 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01002118 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau46090972010-11-23 20:28:03 +01002119 ieee80211_restart_sta_timer(sdata);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002120}
2121#endif
2122
Johannes Berg9c6bd792008-09-11 00:01:52 +02002123/* interface setup */
2124void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2125{
Johannes Berg46900292009-02-15 12:44:28 +01002126 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002127
Johannes Berg46900292009-02-15 12:44:28 +01002128 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002129 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01002130 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002131 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2132 ieee80211_beacon_connection_loss_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002133 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01002134 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02002135 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002136 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2137 (unsigned long) sdata);
2138 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2139 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002140 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05302141 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002142
Johannes Bergab1faea2009-07-01 21:41:17 +02002143 ifmgd->flags = 0;
Johannes Bergbbbdff92009-04-16 13:27:42 +02002144
Johannes Berg77fdaa12009-07-07 03:45:17 +02002145 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01002146
2147 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2148 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2149 else
2150 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002151}
2152
2153/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002154void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2155{
2156 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02002157
2158 /* Restart STA timers */
2159 rcu_read_lock();
2160 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2161 ieee80211_restart_sta_timer(sdata);
2162 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02002163}
Johannes Berg10f644a2009-04-16 13:17:25 +02002164
2165int ieee80211_max_network_latency(struct notifier_block *nb,
2166 unsigned long data, void *dummy)
2167{
2168 s32 latency_usec = (s32) data;
2169 struct ieee80211_local *local =
2170 container_of(nb, struct ieee80211_local,
2171 network_latency_notifier);
2172
2173 mutex_lock(&local->iflist_mtx);
2174 ieee80211_recalc_ps(local, latency_usec);
2175 mutex_unlock(&local->iflist_mtx);
2176
2177 return 0;
2178}
Johannes Berg77fdaa12009-07-07 03:45:17 +02002179
2180/* config hooks */
Johannes Bergaf6b6372009-12-23 13:15:35 +01002181static enum work_done_result
2182ieee80211_probe_auth_done(struct ieee80211_work *wk,
2183 struct sk_buff *skb)
2184{
2185 if (!skb) {
2186 cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
2187 return WORK_DONE_DESTROY;
2188 }
2189
2190 if (wk->type == IEEE80211_WORK_AUTH) {
2191 cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
2192 return WORK_DONE_DESTROY;
2193 }
2194
2195 mutex_lock(&wk->sdata->u.mgd.mtx);
2196 ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2197 mutex_unlock(&wk->sdata->u.mgd.mtx);
2198
2199 wk->type = IEEE80211_WORK_AUTH;
2200 wk->probe_auth.tries = 0;
2201 return WORK_DONE_REQUEUE;
2202}
2203
Johannes Berg77fdaa12009-07-07 03:45:17 +02002204int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2205 struct cfg80211_auth_request *req)
2206{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002207 const u8 *ssid;
Johannes Bergf679f652009-12-23 13:15:34 +01002208 struct ieee80211_work *wk;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002209 u16 auth_alg;
2210
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002211 if (req->local_state_change)
2212 return 0; /* no need to update mac80211 state */
2213
Johannes Berg77fdaa12009-07-07 03:45:17 +02002214 switch (req->auth_type) {
2215 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2216 auth_alg = WLAN_AUTH_OPEN;
2217 break;
2218 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg9dca9c42010-07-21 10:09:25 +02002219 if (IS_ERR(sdata->local->wep_tx_tfm))
2220 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002221 auth_alg = WLAN_AUTH_SHARED_KEY;
2222 break;
2223 case NL80211_AUTHTYPE_FT:
2224 auth_alg = WLAN_AUTH_FT;
2225 break;
2226 case NL80211_AUTHTYPE_NETWORK_EAP:
2227 auth_alg = WLAN_AUTH_LEAP;
2228 break;
2229 default:
2230 return -EOPNOTSUPP;
2231 }
2232
2233 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2234 if (!wk)
2235 return -ENOMEM;
2236
Joe Perches5e124bd2010-01-08 22:33:38 -08002237 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002238
2239 if (req->ie && req->ie_len) {
2240 memcpy(wk->ie, req->ie, req->ie_len);
2241 wk->ie_len = req->ie_len;
2242 }
2243
Johannes Bergfffd0932009-07-08 14:22:54 +02002244 if (req->key && req->key_len) {
Johannes Bergaf6b6372009-12-23 13:15:35 +01002245 wk->probe_auth.key_len = req->key_len;
2246 wk->probe_auth.key_idx = req->key_idx;
2247 memcpy(wk->probe_auth.key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02002248 }
2249
Johannes Berg77fdaa12009-07-07 03:45:17 +02002250 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002251 memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2252 wk->probe_auth.ssid_len = ssid[1];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002253
Johannes Bergaf6b6372009-12-23 13:15:35 +01002254 wk->probe_auth.algorithm = auth_alg;
2255 wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
Johannes Bergf679f652009-12-23 13:15:34 +01002256
Johannes Berg070bb542010-02-03 13:57:46 +01002257 /* if we already have a probe, don't probe again */
2258 if (req->bss->proberesp_ies)
2259 wk->type = IEEE80211_WORK_AUTH;
2260 else
2261 wk->type = IEEE80211_WORK_DIRECT_PROBE;
Johannes Bergf679f652009-12-23 13:15:34 +01002262 wk->chan = req->bss->channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002263 wk->sdata = sdata;
2264 wk->done = ieee80211_probe_auth_done;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002265
Johannes Bergaf6b6372009-12-23 13:15:35 +01002266 ieee80211_add_work(wk);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002267 return 0;
2268}
2269
Johannes Bergaf6b6372009-12-23 13:15:35 +01002270static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2271 struct sk_buff *skb)
2272{
2273 struct ieee80211_mgmt *mgmt;
Johannes Berge5b900d2010-07-29 16:08:55 +02002274 struct ieee80211_rx_status *rx_status;
2275 struct ieee802_11_elems elems;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002276 u16 status;
2277
2278 if (!skb) {
2279 cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
2280 return WORK_DONE_DESTROY;
2281 }
2282
Johannes Berge5b900d2010-07-29 16:08:55 +02002283 if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2284 mutex_lock(&wk->sdata->u.mgd.mtx);
2285 rx_status = (void *) skb->cb;
2286 ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2287 ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2288 &elems, true);
2289 mutex_unlock(&wk->sdata->u.mgd.mtx);
2290
2291 wk->type = IEEE80211_WORK_ASSOC;
2292 /* not really done yet */
2293 return WORK_DONE_REQUEUE;
2294 }
2295
Johannes Bergaf6b6372009-12-23 13:15:35 +01002296 mgmt = (void *)skb->data;
2297 status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2298
2299 if (status == WLAN_STATUS_SUCCESS) {
2300 mutex_lock(&wk->sdata->u.mgd.mtx);
2301 if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2302 mutex_unlock(&wk->sdata->u.mgd.mtx);
2303 /* oops -- internal error -- send timeout for now */
2304 cfg80211_send_assoc_timeout(wk->sdata->dev,
2305 wk->filter_ta);
2306 return WORK_DONE_DESTROY;
2307 }
Juuso Oikarinen68542962010-06-09 13:43:26 +03002308
2309 mutex_unlock(&wk->sdata->u.mgd.mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002310 }
2311
2312 cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
2313 return WORK_DONE_DESTROY;
2314}
2315
Johannes Berg77fdaa12009-07-07 03:45:17 +02002316int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2317 struct cfg80211_assoc_request *req)
2318{
2319 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002320 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Bergf679f652009-12-23 13:15:34 +01002321 struct ieee80211_work *wk;
Johannes Berg63f170e2009-12-23 13:15:33 +01002322 const u8 *ssid;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002323 int i;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002324
2325 mutex_lock(&ifmgd->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002326 if (ifmgd->associated) {
Jouni Malinen9c87ba62010-02-28 12:13:46 +02002327 if (!req->prev_bssid ||
2328 memcmp(req->prev_bssid, ifmgd->associated->bssid,
2329 ETH_ALEN)) {
2330 /*
2331 * We are already associated and the request was not a
2332 * reassociation request from the current BSS, so
2333 * reject it.
2334 */
2335 mutex_unlock(&ifmgd->mtx);
2336 return -EALREADY;
2337 }
2338
2339 /* Trying to reassociate - clear previous association state */
Johannes Berg53f73c02010-10-05 19:37:40 +02002340 ieee80211_set_disassoc(sdata, true, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002341 }
2342 mutex_unlock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002343
Johannes Berg63f170e2009-12-23 13:15:33 +01002344 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002345 if (!wk)
2346 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002347
Johannes Berg77fdaa12009-07-07 03:45:17 +02002348 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
Vivek Natarajan375177b2010-02-09 14:50:28 +05302349 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002350
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002351 ifmgd->beacon_crc_valid = false;
2352
Johannes Berg77fdaa12009-07-07 03:45:17 +02002353 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2354 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2355 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2356 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2357 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2358
Johannes Berg77fdaa12009-07-07 03:45:17 +02002359
2360 if (req->ie && req->ie_len) {
2361 memcpy(wk->ie, req->ie, req->ie_len);
2362 wk->ie_len = req->ie_len;
2363 } else
2364 wk->ie_len = 0;
2365
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002366 wk->assoc.bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01002367
Johannes Bergaf6b6372009-12-23 13:15:35 +01002368 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
Johannes Bergf679f652009-12-23 13:15:34 +01002369
Johannes Bergaf6b6372009-12-23 13:15:35 +01002370 /* new association always uses requested smps mode */
2371 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2372 if (ifmgd->powersave)
2373 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2374 else
2375 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2376 } else
2377 ifmgd->ap_smps = ifmgd->req_smps;
2378
2379 wk->assoc.smps = ifmgd->ap_smps;
Johannes Berg77c81442009-12-23 13:15:37 +01002380 /*
2381 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2382 * We still associate in non-HT mode (11a/b/g) if any one of these
2383 * ciphers is configured as pairwise.
2384 * We can set this to true for non-11n hardware, that'll be checked
2385 * separately along with the peer capabilities.
2386 */
Johannes Bergaf6b6372009-12-23 13:15:35 +01002387 wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
Johannes Bergf679f652009-12-23 13:15:34 +01002388 wk->assoc.capability = req->bss->capability;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002389 wk->assoc.wmm_used = bss->wmm_used;
2390 wk->assoc.supp_rates = bss->supp_rates;
2391 wk->assoc.supp_rates_len = bss->supp_rates_len;
Johannes Bergf679f652009-12-23 13:15:34 +01002392 wk->assoc.ht_information_ie =
2393 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
Johannes Berg63f170e2009-12-23 13:15:33 +01002394
Kalle Valoab133152010-01-12 10:42:31 +02002395 if (bss->wmm_used && bss->uapsd_supported &&
2396 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2397 wk->assoc.uapsd_used = true;
2398 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2399 } else {
2400 wk->assoc.uapsd_used = false;
2401 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2402 }
2403
Johannes Berg63f170e2009-12-23 13:15:33 +01002404 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
Johannes Bergf679f652009-12-23 13:15:34 +01002405 memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2406 wk->assoc.ssid_len = ssid[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01002407
Johannes Berg77fdaa12009-07-07 03:45:17 +02002408 if (req->prev_bssid)
Johannes Bergf679f652009-12-23 13:15:34 +01002409 memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002410
Johannes Bergf679f652009-12-23 13:15:34 +01002411 wk->chan = req->bss->channel;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002412 wk->sdata = sdata;
2413 wk->done = ieee80211_assoc_done;
Johannes Berge5b900d2010-07-29 16:08:55 +02002414 if (!bss->dtim_period &&
2415 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2416 wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2417 else
2418 wk->type = IEEE80211_WORK_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002419
2420 if (req->use_mfp) {
2421 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2422 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2423 } else {
2424 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2425 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2426 }
2427
2428 if (req->crypto.control_port)
2429 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2430 else
2431 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2432
Johannes Berga621fa42010-08-27 14:26:54 +03002433 sdata->control_port_protocol = req->crypto.control_port_ethertype;
2434 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2435
Johannes Bergaf6b6372009-12-23 13:15:35 +01002436 ieee80211_add_work(wk);
2437 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002438}
2439
2440int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503d2009-07-07 03:56:11 +02002441 struct cfg80211_deauth_request *req,
2442 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002443{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002444 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002445 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergf679f652009-12-23 13:15:34 +01002446 struct ieee80211_work *wk;
Jouni Malinen05e48e82010-06-14 11:55:56 -07002447 u8 bssid[ETH_ALEN];
2448 bool assoc_bss = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002449
Johannes Berg77fdaa12009-07-07 03:45:17 +02002450 mutex_lock(&ifmgd->mtx);
2451
Jouni Malinen05e48e82010-06-14 11:55:56 -07002452 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002453 if (ifmgd->associated == req->bss) {
Johannes Berg53f73c02010-10-05 19:37:40 +02002454 ieee80211_set_disassoc(sdata, false, true);
Johannes Berga58ce432009-11-19 12:45:42 +01002455 mutex_unlock(&ifmgd->mtx);
Jouni Malinen05e48e82010-06-14 11:55:56 -07002456 assoc_bss = true;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002457 } else {
2458 bool not_auth_yet = false;
Johannes Berga58ce432009-11-19 12:45:42 +01002459
Johannes Bergaf6b6372009-12-23 13:15:35 +01002460 mutex_unlock(&ifmgd->mtx);
2461
Johannes Berga1699b72010-07-30 16:46:07 +02002462 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002463 list_for_each_entry(wk, &local->work_list, list) {
Johannes Berg29165e42010-02-06 15:20:13 +01002464 if (wk->sdata != sdata)
Johannes Bergaf6b6372009-12-23 13:15:35 +01002465 continue;
Johannes Berg29165e42010-02-06 15:20:13 +01002466
2467 if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
Reinette Chatre79733a82010-05-04 16:04:49 -07002468 wk->type != IEEE80211_WORK_AUTH &&
Johannes Berge5b900d2010-07-29 16:08:55 +02002469 wk->type != IEEE80211_WORK_ASSOC &&
2470 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
Johannes Berg29165e42010-02-06 15:20:13 +01002471 continue;
2472
Johannes Bergaf6b6372009-12-23 13:15:35 +01002473 if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2474 continue;
Johannes Berg29165e42010-02-06 15:20:13 +01002475
2476 not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2477 list_del_rcu(&wk->list);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002478 free_work(wk);
2479 break;
2480 }
Johannes Berga1699b72010-07-30 16:46:07 +02002481 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002482
2483 /*
2484 * If somebody requests authentication and we haven't
2485 * sent out an auth frame yet there's no need to send
2486 * out a deauth frame either. If the state was PROBE,
2487 * then this is the case. If it's AUTH we have sent a
2488 * frame, and if it's IDLE we have completed the auth
2489 * process already.
2490 */
2491 if (not_auth_yet) {
2492 __cfg80211_auth_canceled(sdata->dev, bssid);
2493 return 0;
2494 }
2495 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002496
Johannes Berg0ff71612009-09-26 14:45:41 +02002497 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002498 sdata->name, bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002499
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002500 ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2501 req->reason_code, cookie,
2502 !req->local_state_change);
Jouni Malinen05e48e82010-06-14 11:55:56 -07002503 if (assoc_bss)
2504 sta_info_destroy_addr(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002505
Johannes Berg7da7cc12010-08-05 17:02:38 +02002506 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002507 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002508 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002509
Johannes Berg77fdaa12009-07-07 03:45:17 +02002510 return 0;
2511}
2512
2513int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg667503d2009-07-07 03:56:11 +02002514 struct cfg80211_disassoc_request *req,
2515 void *cookie)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002516{
2517 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07002518 u8 bssid[ETH_ALEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002519
Johannes Berg77fdaa12009-07-07 03:45:17 +02002520 mutex_lock(&ifmgd->mtx);
2521
Johannes Berg8d8b2612009-07-25 11:58:36 +02002522 /*
2523 * cfg80211 should catch this ... but it's racy since
2524 * we can receive a disassoc frame, process it, hand it
2525 * to cfg80211 while that's in a locked section already
2526 * trying to tell us that the user wants to disconnect.
2527 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002528 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002529 mutex_unlock(&ifmgd->mtx);
2530 return -ENOLINK;
2531 }
2532
Johannes Berg0ff71612009-09-26 14:45:41 +02002533 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002534 sdata->name, req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02002535
Jouni Malinene69e95d2010-03-29 23:29:31 -07002536 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg53f73c02010-10-05 19:37:40 +02002537 ieee80211_set_disassoc(sdata, false, true);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002538
2539 mutex_unlock(&ifmgd->mtx);
2540
2541 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +02002542 IEEE80211_STYPE_DISASSOC, req->reason_code,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03002543 cookie, !req->local_state_change);
Jouni Malinene69e95d2010-03-29 23:29:31 -07002544 sta_info_destroy_addr(sdata, bssid);
Johannes Bergbc83b682009-11-29 12:19:06 +01002545
Johannes Berg7da7cc12010-08-05 17:02:38 +02002546 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002547 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002548 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002549
Johannes Berg77fdaa12009-07-07 03:45:17 +02002550 return 0;
2551}
Jouni Malinen026331c2010-02-15 12:53:10 +02002552
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002553void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2554 enum nl80211_cqm_rssi_threshold_event rssi_event,
2555 gfp_t gfp)
2556{
2557 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2558
Johannes Bergb5878a22010-04-07 16:48:40 +02002559 trace_api_cqm_rssi_notify(sdata, rssi_event);
2560
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02002561 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2562}
2563EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);