blob: a382a91d74c38000d0b530bbda1118b1bfe616a6 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg66e67e42012-01-20 13:55:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_MAX_TRIES 3
35#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37#define IEEE80211_ASSOC_MAX_TRIES 3
38
Ben Greear180205b2011-02-04 15:30:24 -080039static int max_nullfunc_tries = 2;
40module_param(max_nullfunc_tries, int, 0644);
41MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44static int max_probe_tries = 5;
45module_param(max_probe_tries, int, 0644);
46MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020048
49/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010050 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020056 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010057#define IEEE80211_BEACON_LOSS_COUNT 7
58
Johannes Bergb291ba12009-07-10 15:29:03 +020059/*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030063#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
Ben Greear180205b2011-02-04 15:30:24 -080069static int probe_wait_ms = 500;
70module_param(probe_wait_ms, int, 0644);
71MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070074
Jouni Malinen17e4ec12010-03-29 23:28:30 -070075/*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80#define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
Jouni Malinen391a2002010-08-27 22:22:00 +030082/*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
Johannes Berg5bb644a2009-05-17 11:40:42 +020088#define TMR_RUNNING_TIMER 0
89#define TMR_RUNNING_CHANSW 1
90
Johannes Berg5fef7db2012-02-24 13:50:52 +010091#define DEAUTH_DISASSOC_LEN (24 /* hdr */ + 2 /* reason */)
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200117};
118
Johannes Berg5484e232008-09-08 17:44:27 +0200119/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200122 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123}
124
Johannes Bergca386f32009-07-10 02:39:48 +0200125/*
126 * We can have multiple work items (and connection probing)
127 * scheduling this timer, but we need to take care to only
128 * reschedule it when it should fire _earlier_ than it was
129 * asked for before, or if it's not pending right now. This
130 * function ensures that. Note that it then is required to
131 * run this function for all timeouts after the first one
132 * has happened -- the work that runs from this timer will
133 * do that.
134 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100135static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200136{
137 ASSERT_MGD_MTX(ifmgd);
138
139 if (!timer_pending(&ifmgd->timer) ||
140 time_before(timeout, ifmgd->timer.expires))
141 mod_timer(&ifmgd->timer, timeout);
142}
143
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400144void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145{
Johannes Bergc1288b12012-01-19 09:29:57 +0100146 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200147 return;
148
149 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100150 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200151}
152
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400153void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
154{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
156
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100157 if (unlikely(!sdata->u.mgd.associated))
158 return;
159
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400160 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
161 return;
162
163 mod_timer(&sdata->u.mgd.conn_mon_timer,
164 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400165
166 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400167}
168
Johannes Berg5484e232008-09-08 17:44:27 +0200169static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200170{
Johannes Berg5484e232008-09-08 17:44:27 +0200171 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200172}
173
Johannes Bergd5522e02009-03-30 13:23:35 +0200174/*
175 * ieee80211_enable_ht should be called only after the operating band
176 * has been determined as ht configuration depends on the hw's
177 * HT abilities for a specific band.
178 */
179static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
180 struct ieee80211_ht_info *hti,
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530181 const u8 *bssid, u16 ap_ht_cap_flags,
182 bool beacon_htcap_ie)
Johannes Bergd5522e02009-03-30 13:23:35 +0200183{
184 struct ieee80211_local *local = sdata->local;
185 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200186 struct sta_info *sta;
187 u32 changed = 0;
Ben Greear172710b2011-01-28 17:05:43 -0800188 int hti_cfreq;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200189 u16 ht_opmode;
Johannes Bergbf2370f2012-06-27 18:11:56 +0200190 bool enable_ht = true, queues_stopped = false;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200191 enum nl80211_channel_type prev_chantype;
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700192 enum nl80211_channel_type rx_channel_type = NL80211_CHAN_NO_HT;
193 enum nl80211_channel_type tx_channel_type;
Johannes Bergd5522e02009-03-30 13:23:35 +0200194
195 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200196 prev_chantype = sdata->vif.bss_conf.channel_type;
197
Johannes Bergd5522e02009-03-30 13:23:35 +0200198
Johannes Berg4e74bfd2012-03-08 15:02:04 +0100199 hti_cfreq = ieee80211_channel_to_frequency(hti->control_chan,
200 sband->band);
201 /* check that channel matches the right operating channel */
202 if (local->hw.conf.channel->center_freq != hti_cfreq) {
203 /* Some APs mess this up, evidently.
204 * Netgear WNDR3700 sometimes reports 4 higher than
205 * the actual channel, for instance.
206 */
207 printk(KERN_DEBUG
208 "%s: Wrong control channel in association"
209 " response: configured center-freq: %d"
210 " hti-cfreq: %d hti->control_chan: %d"
211 " band: %d. Disabling HT.\n",
212 sdata->name,
213 local->hw.conf.channel->center_freq,
214 hti_cfreq, hti->control_chan,
215 sband->band);
216 enable_ht = false;
Ben Greear172710b2011-01-28 17:05:43 -0800217 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200218
219 if (enable_ht) {
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700220 rx_channel_type = NL80211_CHAN_HT20;
Johannes Bergd5522e02009-03-30 13:23:35 +0200221
222 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
Ben Greearef96a8422011-11-18 11:32:00 -0800223 !ieee80111_cfg_override_disables_ht40(sdata) &&
Johannes Bergd5522e02009-03-30 13:23:35 +0200224 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
225 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
226 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
227 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700228 rx_channel_type = NL80211_CHAN_HT40PLUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200229 break;
230 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700231 rx_channel_type = NL80211_CHAN_HT40MINUS;
Johannes Bergd5522e02009-03-30 13:23:35 +0200232 break;
233 }
234 }
235 }
236
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700237 tx_channel_type = ieee80211_get_tx_channel_type(local, rx_channel_type);
Johannes Bergd5522e02009-03-30 13:23:35 +0200238
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700239 if (local->tmp_channel)
240 local->tmp_channel_type = rx_channel_type;
241
242 if (!ieee80211_set_channel_type(local, sdata, rx_channel_type)) {
Johannes Berg0aaffa92010-05-05 15:28:27 +0200243 /* can only fail due to HT40+/- mismatch */
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700244 rx_channel_type = NL80211_CHAN_HT20;
245 WARN_ON(!ieee80211_set_channel_type(local, sdata,
246 rx_channel_type));
Johannes Berg0aaffa92010-05-05 15:28:27 +0200247 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200248
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700249 if (beacon_htcap_ie && (prev_chantype != rx_channel_type)) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530250 /*
251 * Whenever the AP announces the HT mode change that can be
252 * 40MHz intolerant or etc., it would be safer to stop tx
253 * queues before doing hw config to avoid buffer overflow.
254 */
255 ieee80211_stop_queues_by_reason(&sdata->local->hw,
256 IEEE80211_QUEUE_STOP_REASON_CHTYPE_CHANGE);
Johannes Bergbf2370f2012-06-27 18:11:56 +0200257 queues_stopped = true;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530258
259 /* flush out all packets */
260 synchronize_net();
261
262 drv_flush(local, false);
263 }
264
Johannes Berg0aaffa92010-05-05 15:28:27 +0200265 /* channel_type change automatically detected */
266 ieee80211_hw_config(local, 0);
267
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700268 if (prev_chantype != tx_channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200269 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100270 sta = sta_info_get(sdata, bssid);
Johannes Bergd5522e02009-03-30 13:23:35 +0200271 if (sta)
272 rate_control_rate_update(local, sband, sta,
Sujith4fa00432010-03-01 14:42:57 +0530273 IEEE80211_RC_HT_CHANGED,
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700274 tx_channel_type);
Johannes Bergd5522e02009-03-30 13:23:35 +0200275 rcu_read_unlock();
Johannes Berg0aaffa92010-05-05 15:28:27 +0200276 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200277
Johannes Bergbf2370f2012-06-27 18:11:56 +0200278 if (queues_stopped)
279 ieee80211_wake_queues_by_reason(&sdata->local->hw,
280 IEEE80211_QUEUE_STOP_REASON_CHTYPE_CHANGE);
281
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200282 ht_opmode = le16_to_cpu(hti->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200283
284 /* if bss configuration changed store the new one */
Johannes Berg0aaffa92010-05-05 15:28:27 +0200285 if (sdata->ht_opmode_valid != enable_ht ||
286 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
Paul Stewart3117bbdb2012-03-13 07:46:18 -0700287 prev_chantype != rx_channel_type) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200288 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200289 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200290 sdata->ht_opmode_valid = enable_ht;
Johannes Bergd5522e02009-03-30 13:23:35 +0200291 }
292
293 return changed;
294}
295
Johannes Berg9c6bd792008-09-11 00:01:52 +0200296/* frame sending functions */
297
Johannes Berg66e67e42012-01-20 13:55:27 +0100298static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
299 struct ieee80211_supported_band *sband,
300 u32 *rates)
301{
302 int i, j, count;
303 *rates = 0;
304 count = 0;
305 for (i = 0; i < supp_rates_len; i++) {
306 int rate = (supp_rates[i] & 0x7F) * 5;
307
308 for (j = 0; j < sband->n_bitrates; j++)
309 if (sband->bitrates[j].bitrate == rate) {
310 *rates |= BIT(j);
311 count++;
312 break;
313 }
314 }
315
316 return count;
317}
318
319static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
320 struct sk_buff *skb, const u8 *ht_info_ie,
321 struct ieee80211_supported_band *sband,
322 struct ieee80211_channel *channel,
323 enum ieee80211_smps_mode smps)
324{
325 struct ieee80211_ht_info *ht_info;
326 u8 *pos;
327 u32 flags = channel->flags;
328 u16 cap;
329 struct ieee80211_sta_ht_cap ht_cap;
330
331 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
332
Johannes Berg66e67e42012-01-20 13:55:27 +0100333 if (!ht_info_ie)
334 return;
335
336 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
337 return;
338
339 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
340 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
341
342 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
343
344 /* determine capability flags */
345 cap = ht_cap.cap;
346
347 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
348 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
349 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
350 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
351 cap &= ~IEEE80211_HT_CAP_SGI_40;
352 }
353 break;
354 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
355 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
356 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
357 cap &= ~IEEE80211_HT_CAP_SGI_40;
358 }
359 break;
360 }
361
362 /* set SM PS mode properly */
363 cap &= ~IEEE80211_HT_CAP_SM_PS;
364 switch (smps) {
365 case IEEE80211_SMPS_AUTOMATIC:
366 case IEEE80211_SMPS_NUM_MODES:
367 WARN_ON(1);
368 case IEEE80211_SMPS_OFF:
369 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
370 IEEE80211_HT_CAP_SM_PS_SHIFT;
371 break;
372 case IEEE80211_SMPS_STATIC:
373 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
374 IEEE80211_HT_CAP_SM_PS_SHIFT;
375 break;
376 case IEEE80211_SMPS_DYNAMIC:
377 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
378 IEEE80211_HT_CAP_SM_PS_SHIFT;
379 break;
380 }
381
382 /* reserve and fill IE */
383 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
384 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
385}
386
Mahesh Palivelad488b6a2013-02-21 07:04:14 +0530387static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
388 struct sk_buff *skb,
389 struct ieee80211_supported_band *sband)
390{
391 u8 *pos;
392 u32 cap;
393 struct ieee80211_sta_vht_cap vht_cap;
394
395 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
396
397 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
398
399 /* determine capability flags */
400 cap = vht_cap.cap;
401
402 /* reserve and fill IE */
Mahesh Paliveladd293952012-10-10 11:25:40 +0000403 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad488b6a2013-02-21 07:04:14 +0530404 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
405}
406
Johannes Berg66e67e42012-01-20 13:55:27 +0100407static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
408{
409 struct ieee80211_local *local = sdata->local;
410 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
411 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
412 struct sk_buff *skb;
413 struct ieee80211_mgmt *mgmt;
414 u8 *pos, qos_info;
415 size_t offset = 0, noffset;
416 int i, count, rates_len, supp_rates_len;
417 u16 capab;
418 struct ieee80211_supported_band *sband;
419 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100420
421 lockdep_assert_held(&ifmgd->mtx);
422
423 sband = local->hw.wiphy->bands[local->oper_channel->band];
424
425 if (assoc_data->supp_rates_len) {
426 /*
427 * Get all rates supported by the device and the AP as
428 * some APs don't like getting a superset of their rates
429 * in the association request (e.g. D-Link DAP 1353 in
430 * b-only mode)...
431 */
432 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
433 assoc_data->supp_rates_len,
434 sband, &rates);
435 } else {
436 /*
437 * In case AP not provide any supported rates information
438 * before association, we send information element(s) with
439 * all rates that we support.
440 */
441 rates = ~0;
442 rates_len = sband->n_bitrates;
443 }
444
445 skb = alloc_skb(local->hw.extra_tx_headroom +
446 sizeof(*mgmt) + /* bit too much but doesn't matter */
447 2 + assoc_data->ssid_len + /* SSID */
448 4 + rates_len + /* (extended) rates */
449 4 + /* power capability */
450 2 + 2 * sband->n_channels + /* supported channels */
451 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Paliveladd293952012-10-10 11:25:40 +0000452 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100453 assoc_data->ie_len + /* extra IEs */
454 9, /* WMM */
455 GFP_KERNEL);
456 if (!skb)
457 return;
458
459 skb_reserve(skb, local->hw.extra_tx_headroom);
460
461 capab = WLAN_CAPABILITY_ESS;
462
463 if (sband->band == IEEE80211_BAND_2GHZ) {
464 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
465 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
466 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
467 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
468 }
469
470 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
471 capab |= WLAN_CAPABILITY_PRIVACY;
472
473 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
474 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
475 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
476
477 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
478 memset(mgmt, 0, 24);
479 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
480 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
481 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
482
483 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
484 skb_put(skb, 10);
485 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
486 IEEE80211_STYPE_REASSOC_REQ);
487 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
488 mgmt->u.reassoc_req.listen_interval =
489 cpu_to_le16(local->hw.conf.listen_interval);
490 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
491 ETH_ALEN);
492 } else {
493 skb_put(skb, 4);
494 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
495 IEEE80211_STYPE_ASSOC_REQ);
496 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
497 mgmt->u.assoc_req.listen_interval =
498 cpu_to_le16(local->hw.conf.listen_interval);
499 }
500
501 /* SSID */
502 pos = skb_put(skb, 2 + assoc_data->ssid_len);
503 *pos++ = WLAN_EID_SSID;
504 *pos++ = assoc_data->ssid_len;
505 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
506
507 /* add all rates which were marked to be used above */
508 supp_rates_len = rates_len;
509 if (supp_rates_len > 8)
510 supp_rates_len = 8;
511
512 pos = skb_put(skb, supp_rates_len + 2);
513 *pos++ = WLAN_EID_SUPP_RATES;
514 *pos++ = supp_rates_len;
515
516 count = 0;
517 for (i = 0; i < sband->n_bitrates; i++) {
518 if (BIT(i) & rates) {
519 int rate = sband->bitrates[i].bitrate;
520 *pos++ = (u8) (rate / 5);
521 if (++count == 8)
522 break;
523 }
524 }
525
526 if (rates_len > count) {
527 pos = skb_put(skb, rates_len - count + 2);
528 *pos++ = WLAN_EID_EXT_SUPP_RATES;
529 *pos++ = rates_len - count;
530
531 for (i++; i < sband->n_bitrates; i++) {
532 if (BIT(i) & rates) {
533 int rate = sband->bitrates[i].bitrate;
534 *pos++ = (u8) (rate / 5);
535 }
536 }
537 }
538
539 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
540 /* 1. power capabilities */
541 pos = skb_put(skb, 4);
542 *pos++ = WLAN_EID_PWR_CAPABILITY;
543 *pos++ = 2;
544 *pos++ = 0; /* min tx power */
545 *pos++ = local->oper_channel->max_power; /* max tx power */
546
547 /* 2. supported channels */
548 /* TODO: get this in reg domain format */
549 pos = skb_put(skb, 2 * sband->n_channels + 2);
550 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
551 *pos++ = 2 * sband->n_channels;
552 for (i = 0; i < sband->n_channels; i++) {
553 *pos++ = ieee80211_frequency_to_channel(
554 sband->channels[i].center_freq);
555 *pos++ = 1; /* one channel in the subband*/
556 }
557 }
558
559 /* if present, add any custom IEs that go before HT */
560 if (assoc_data->ie_len && assoc_data->ie) {
561 static const u8 before_ht[] = {
562 WLAN_EID_SSID,
563 WLAN_EID_SUPP_RATES,
564 WLAN_EID_EXT_SUPP_RATES,
565 WLAN_EID_PWR_CAPABILITY,
566 WLAN_EID_SUPPORTED_CHANNELS,
567 WLAN_EID_RSN,
568 WLAN_EID_QOS_CAPA,
569 WLAN_EID_RRM_ENABLED_CAPABILITIES,
570 WLAN_EID_MOBILITY_DOMAIN,
571 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
572 };
573 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
574 before_ht, ARRAY_SIZE(before_ht),
575 offset);
576 pos = skb_put(skb, noffset - offset);
577 memcpy(pos, assoc_data->ie + offset, noffset - offset);
578 offset = noffset;
579 }
580
Johannes Berg4e74bfd2012-03-08 15:02:04 +0100581 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg66e67e42012-01-20 13:55:27 +0100582 ieee80211_add_ht_ie(sdata, skb, assoc_data->ht_information_ie,
583 sband, local->oper_channel, ifmgd->ap_smps);
584
Mahesh Palivelad488b6a2013-02-21 07:04:14 +0530585 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
586 ieee80211_add_vht_ie(sdata, skb, sband);
587
Johannes Berg66e67e42012-01-20 13:55:27 +0100588 /* if present, add any custom non-vendor IEs that go after HT */
589 if (assoc_data->ie_len && assoc_data->ie) {
590 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
591 assoc_data->ie_len,
592 offset);
593 pos = skb_put(skb, noffset - offset);
594 memcpy(pos, assoc_data->ie + offset, noffset - offset);
595 offset = noffset;
596 }
597
Johannes Berg76f03032012-03-08 15:02:05 +0100598 if (assoc_data->wmm) {
599 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200600 qos_info = ifmgd->uapsd_queues;
601 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100602 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
603 } else {
604 qos_info = 0;
605 }
606
607 pos = skb_put(skb, 9);
608 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
609 *pos++ = 7; /* len */
610 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
611 *pos++ = 0x50;
612 *pos++ = 0xf2;
613 *pos++ = 2; /* WME */
614 *pos++ = 0; /* WME info */
615 *pos++ = 1; /* WME ver */
616 *pos++ = qos_info;
617 }
618
619 /* add any remaining custom (i.e. vendor specific here) IEs */
620 if (assoc_data->ie_len && assoc_data->ie) {
621 noffset = assoc_data->ie_len;
622 pos = skb_put(skb, noffset - offset);
623 memcpy(pos, assoc_data->ie + offset, noffset - offset);
624 }
625
626 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
627 ieee80211_tx_skb(sdata, skb);
628}
629
Johannes Bergef422bc2008-09-09 10:58:25 +0200630static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100631 const u8 *bssid, u16 stype,
Johannes Berg5fef7db2012-02-24 13:50:52 +0100632 u16 reason, bool send_frame,
633 u8 *frame_buf)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200634{
635 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100636 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200637 struct sk_buff *skb;
Johannes Berg5fef7db2012-02-24 13:50:52 +0100638 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200639
Johannes Berg5fef7db2012-02-24 13:50:52 +0100640 /* build frame */
641 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
642 mgmt->duration = 0; /* initialize only */
643 mgmt->seq_ctrl = 0; /* initialize only */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200644 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100645 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200646 memcpy(mgmt->bssid, bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200647 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200648 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
649
Johannes Berg5fef7db2012-02-24 13:50:52 +0100650 if (send_frame) {
651 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
652 DEAUTH_DISASSOC_LEN);
653 if (!skb)
654 return;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300655
Johannes Berg5fef7db2012-02-24 13:50:52 +0100656 skb_reserve(skb, local->hw.extra_tx_headroom);
657
658 /* copy in frame */
659 memcpy(skb_put(skb, DEAUTH_DISASSOC_LEN),
660 mgmt, DEAUTH_DISASSOC_LEN);
661
662 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
663 IEEE80211_SKB_CB(skb)->flags |=
664 IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300665 ieee80211_tx_skb(sdata, skb);
Johannes Berg5fef7db2012-02-24 13:50:52 +0100666 }
Johannes Berg9ac19a92008-09-09 10:57:09 +0200667}
668
Kalle Valo572e0012009-02-10 17:09:31 +0200669void ieee80211_send_pspoll(struct ieee80211_local *local,
670 struct ieee80211_sub_if_data *sdata)
671{
Kalle Valo572e0012009-02-10 17:09:31 +0200672 struct ieee80211_pspoll *pspoll;
673 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200674
Kalle Valod8cd1892010-01-05 20:16:26 +0200675 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
676 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200677 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200678
Kalle Valod8cd1892010-01-05 20:16:26 +0200679 pspoll = (struct ieee80211_pspoll *) skb->data;
680 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200681
Johannes Berg62ae67b2009-11-18 18:42:05 +0100682 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
683 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200684}
685
Johannes Berg965beda2009-04-16 13:17:24 +0200686void ieee80211_send_nullfunc(struct ieee80211_local *local,
687 struct ieee80211_sub_if_data *sdata,
688 int powersave)
689{
690 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200691 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530692 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200693
Kalle Valod8cd1892010-01-05 20:16:26 +0200694 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
695 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200696 return;
697
Kalle Valod8cd1892010-01-05 20:16:26 +0200698 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200699 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200700 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200701
Johannes Berg62ae67b2009-11-18 18:42:05 +0100702 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530703 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
704 IEEE80211_STA_CONNECTION_POLL))
705 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
706
Johannes Berg62ae67b2009-11-18 18:42:05 +0100707 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200708}
709
Felix Fietkaud5242152010-01-08 18:06:26 +0100710static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
711 struct ieee80211_sub_if_data *sdata)
712{
713 struct sk_buff *skb;
714 struct ieee80211_hdr *nullfunc;
715 __le16 fc;
716
717 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
718 return;
719
720 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700721 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100722 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700723
Felix Fietkaud5242152010-01-08 18:06:26 +0100724 skb_reserve(skb, local->hw.extra_tx_headroom);
725
726 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
727 memset(nullfunc, 0, 30);
728 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
729 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
730 nullfunc->frame_control = fc;
731 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
732 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
733 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
734 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
735
736 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
737 ieee80211_tx_skb(sdata, skb);
738}
739
Johannes Bergcc32abd2009-05-15 11:52:31 +0200740/* spectrum management related things */
741static void ieee80211_chswitch_work(struct work_struct *work)
742{
743 struct ieee80211_sub_if_data *sdata =
744 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200745 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
746
Johannes Berg9607e6b2009-12-23 13:15:31 +0100747 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200748 return;
749
Johannes Berg77fdaa12009-07-07 03:45:17 +0200750 mutex_lock(&ifmgd->mtx);
751 if (!ifmgd->associated)
752 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200753
754 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200755 if (!sdata->local->ops->channel_switch) {
756 /* call "hw_config" only if doing sw channel switch */
757 ieee80211_hw_config(sdata->local,
758 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300759 } else {
760 /* update the device channel directly */
761 sdata->local->hw.conf.channel = sdata->local->oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200762 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200763
Johannes Berg77fdaa12009-07-07 03:45:17 +0200764 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100765 ifmgd->associated->channel = sdata->local->oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200766
Johannes Bergcc32abd2009-05-15 11:52:31 +0200767 ieee80211_wake_queues_by_reason(&sdata->local->hw,
768 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200769 out:
770 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
771 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200772}
773
Johannes Berg5ce6e432010-05-11 16:20:57 +0200774void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
775{
776 struct ieee80211_sub_if_data *sdata;
777 struct ieee80211_if_managed *ifmgd;
778
779 sdata = vif_to_sdata(vif);
780 ifmgd = &sdata->u.mgd;
781
782 trace_api_chswitch_done(sdata, success);
783 if (!success) {
784 /*
785 * If the channel switch was not successful, stay
786 * around on the old channel. We currently lack
787 * good handling of this situation, possibly we
788 * should just drop the association.
789 */
790 sdata->local->csa_channel = sdata->local->oper_channel;
791 }
792
793 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
794}
795EXPORT_SYMBOL(ieee80211_chswitch_done);
796
Johannes Bergcc32abd2009-05-15 11:52:31 +0200797static void ieee80211_chswitch_timer(unsigned long data)
798{
799 struct ieee80211_sub_if_data *sdata =
800 (struct ieee80211_sub_if_data *) data;
801 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
802
Johannes Berg5bb644a2009-05-17 11:40:42 +0200803 if (sdata->local->quiescing) {
804 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
805 return;
806 }
807
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400808 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200809}
810
811void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
812 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200813 struct ieee80211_bss *bss,
814 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200815{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100816 struct cfg80211_bss *cbss =
817 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200818 struct ieee80211_channel *new_ch;
819 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900820 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
821 cbss->channel->band);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200822
Johannes Berg77fdaa12009-07-07 03:45:17 +0200823 ASSERT_MGD_MTX(ifmgd);
824
825 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200826 return;
827
Helmut Schaafbe9c422009-07-23 12:14:04 +0200828 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200829 return;
830
831 /* Disregard subsequent beacons if we are already running a timer
832 processing a CSA */
833
834 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
835 return;
836
837 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
838 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
839 return;
840
841 sdata->local->csa_channel = new_ch;
842
Johannes Berg5ce6e432010-05-11 16:20:57 +0200843 if (sdata->local->ops->channel_switch) {
844 /* use driver's channel switch callback */
845 struct ieee80211_channel_switch ch_switch;
846 memset(&ch_switch, 0, sizeof(ch_switch));
847 ch_switch.timestamp = timestamp;
848 if (sw_elem->mode) {
849 ch_switch.block_tx = true;
850 ieee80211_stop_queues_by_reason(&sdata->local->hw,
851 IEEE80211_QUEUE_STOP_REASON_CSA);
852 }
853 ch_switch.channel = new_ch;
854 ch_switch.count = sw_elem->count;
855 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
856 drv_channel_switch(sdata->local, &ch_switch);
857 return;
858 }
859
860 /* channel switch handled in software */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200861 if (sw_elem->count <= 1) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400862 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200863 } else {
Wey-Yi Guy9feaddc2010-05-05 20:34:02 -0700864 if (sw_elem->mode)
865 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200866 IEEE80211_QUEUE_STOP_REASON_CSA);
867 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
868 mod_timer(&ifmgd->chswitch_timer,
869 jiffies +
870 msecs_to_jiffies(sw_elem->count *
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100871 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200872 }
873}
874
875static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
876 u16 capab_info, u8 *pwr_constr_elem,
877 u8 pwr_constr_elem_len)
878{
879 struct ieee80211_conf *conf = &sdata->local->hw.conf;
880
881 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
882 return;
883
884 /* Power constraint IE length should be 1 octet */
885 if (pwr_constr_elem_len != 1)
886 return;
887
Hong Wua48b13a2012-01-11 20:34:30 +0200888 if ((*pwr_constr_elem <= conf->channel->max_reg_power) &&
Johannes Bergcc32abd2009-05-15 11:52:31 +0200889 (*pwr_constr_elem != sdata->local->power_constr_level)) {
890 sdata->local->power_constr_level = *pwr_constr_elem;
891 ieee80211_hw_config(sdata->local, 0);
892 }
893}
894
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300895void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
896{
897 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
898 struct ieee80211_local *local = sdata->local;
899 struct ieee80211_conf *conf = &local->hw.conf;
900
901 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
902 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
903 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
904
905 local->disable_dynamic_ps = false;
906 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
907}
908EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
909
910void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
911{
912 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
913 struct ieee80211_local *local = sdata->local;
914 struct ieee80211_conf *conf = &local->hw.conf;
915
916 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
917 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
918 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
919
920 local->disable_dynamic_ps = true;
921 conf->dynamic_ps_timeout = 0;
922 del_timer_sync(&local->dynamic_ps_timer);
923 ieee80211_queue_work(&local->hw,
924 &local->dynamic_ps_enable_work);
925}
926EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
927
Johannes Berg965beda2009-04-16 13:17:24 +0200928/* powersave */
929static void ieee80211_enable_ps(struct ieee80211_local *local,
930 struct ieee80211_sub_if_data *sdata)
931{
932 struct ieee80211_conf *conf = &local->hw.conf;
933
Johannes Bergd5edaed2009-04-22 23:02:51 +0200934 /*
935 * If we are scanning right now then the parameters will
936 * take effect when scan finishes.
937 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200938 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200939 return;
940
Johannes Berg965beda2009-04-16 13:17:24 +0200941 if (conf->dynamic_ps_timeout > 0 &&
942 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
943 mod_timer(&local->dynamic_ps_timer, jiffies +
944 msecs_to_jiffies(conf->dynamic_ps_timeout));
945 } else {
946 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
947 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530948
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200949 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
950 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
951 return;
952
953 conf->flags |= IEEE80211_CONF_PS;
954 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200955 }
956}
957
958static void ieee80211_change_ps(struct ieee80211_local *local)
959{
960 struct ieee80211_conf *conf = &local->hw.conf;
961
962 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200963 ieee80211_enable_ps(local, local->ps_sdata);
964 } else if (conf->flags & IEEE80211_CONF_PS) {
965 conf->flags &= ~IEEE80211_CONF_PS;
966 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
967 del_timer_sync(&local->dynamic_ps_timer);
968 cancel_work_sync(&local->dynamic_ps_enable_work);
969 }
970}
971
Jason Young808118c2011-03-10 16:43:19 -0800972static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
973{
974 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
975 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200976 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800977
978 if (!mgd->powersave)
979 return false;
980
Johannes Berg05cb9102011-10-28 11:59:47 +0200981 if (mgd->broken_ap)
982 return false;
983
Jason Young808118c2011-03-10 16:43:19 -0800984 if (!mgd->associated)
985 return false;
986
987 if (!mgd->associated->beacon_ies)
988 return false;
989
990 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
991 IEEE80211_STA_CONNECTION_POLL))
992 return false;
993
994 rcu_read_lock();
995 sta = sta_info_get(sdata, mgd->bssid);
996 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200997 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -0800998 rcu_read_unlock();
999
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001000 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001001}
1002
Johannes Berg965beda2009-04-16 13:17:24 +02001003/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001004void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001005{
1006 struct ieee80211_sub_if_data *sdata, *found = NULL;
1007 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001008 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001009
1010 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1011 local->ps_sdata = NULL;
1012 return;
1013 }
1014
Johannes Bergaf6b6372009-12-23 13:15:35 +01001015 if (!list_empty(&local->work_list)) {
1016 local->ps_sdata = NULL;
1017 goto change;
1018 }
1019
Johannes Berg965beda2009-04-16 13:17:24 +02001020 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001021 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001022 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301023 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1024 /* If an AP vif is found, then disable PS
1025 * by setting the count to zero thereby setting
1026 * ps_sdata to NULL.
1027 */
1028 count = 0;
1029 break;
1030 }
Johannes Berg965beda2009-04-16 13:17:24 +02001031 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1032 continue;
1033 found = sdata;
1034 count++;
1035 }
1036
Jason Young808118c2011-03-10 16:43:19 -08001037 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001038 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001039 s32 beaconint_us;
1040
1041 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001042 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001043
1044 beaconint_us = ieee80211_tu_to_usec(
1045 found->vif.bss_conf.beacon_int);
1046
Juuso Oikarinenff616382010-06-09 09:51:52 +03001047 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001048 if (timeout < 0) {
1049 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001050 * Go to full PSM if the user configures a very low
1051 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001052 * The 2000 second value is there for compatibility
1053 * until the PM_QOS_NETWORK_LATENCY is configured
1054 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001055 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001056 if (latency > (1900 * USEC_PER_MSEC) &&
1057 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001058 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001059 else
1060 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001061 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001062 local->dynamic_ps_user_timeout = timeout;
1063 if (!local->disable_dynamic_ps)
1064 conf->dynamic_ps_timeout =
1065 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001066
Johannes Berg04fe2032009-04-22 18:44:37 +02001067 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001068 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001069 } else {
Johannes Berg56007a02010-01-26 14:19:52 +01001070 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +02001071 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001072 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +02001073
Johannes Berg56007a02010-01-26 14:19:52 +01001074 bss = (void *)found->u.mgd.associated->priv;
1075 dtimper = bss->dtim_period;
1076
1077 /* If the TIM IE is invalid, pretend the value is 1 */
1078 if (!dtimper)
1079 dtimper = 1;
1080 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001081 maxslp = min_t(int, dtimper,
1082 latency / beaconint_us);
1083
Johannes Berg9ccebe62009-04-23 10:32:36 +02001084 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001085 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001086 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001087 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001088 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001089 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001090 }
Johannes Berg965beda2009-04-16 13:17:24 +02001091
Johannes Bergaf6b6372009-12-23 13:15:35 +01001092 change:
Johannes Berg965beda2009-04-16 13:17:24 +02001093 ieee80211_change_ps(local);
1094}
1095
1096void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1097{
1098 struct ieee80211_local *local =
1099 container_of(work, struct ieee80211_local,
1100 dynamic_ps_disable_work);
1101
1102 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1103 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1104 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1105 }
1106
1107 ieee80211_wake_queues_by_reason(&local->hw,
1108 IEEE80211_QUEUE_STOP_REASON_PS);
1109}
1110
1111void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1112{
1113 struct ieee80211_local *local =
1114 container_of(work, struct ieee80211_local,
1115 dynamic_ps_enable_work);
1116 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001117 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301118 unsigned long flags;
1119 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001120
1121 /* can only happen when PS was just disabled anyway */
1122 if (!sdata)
1123 return;
1124
Christian Lamparter5e340692011-06-30 21:08:43 +02001125 ifmgd = &sdata->u.mgd;
1126
Johannes Berg965beda2009-04-16 13:17:24 +02001127 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1128 return;
1129
Arik Nemtsov77b70232011-06-26 12:06:54 +03001130 if (!local->disable_dynamic_ps &&
1131 local->hw.conf.dynamic_ps_timeout > 0) {
1132 /* don't enter PS if TX frames are pending */
1133 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301134 mod_timer(&local->dynamic_ps_timer, jiffies +
1135 msecs_to_jiffies(
1136 local->hw.conf.dynamic_ps_timeout));
1137 return;
1138 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001139
1140 /*
1141 * transmission can be stopped by others which leads to
1142 * dynamic_ps_timer expiry. Postpone the ps timer if it
1143 * is not the actual idle state.
1144 */
1145 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1146 for (q = 0; q < local->hw.queues; q++) {
1147 if (local->queue_stop_reasons[q]) {
1148 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1149 flags);
1150 mod_timer(&local->dynamic_ps_timer, jiffies +
1151 msecs_to_jiffies(
1152 local->hw.conf.dynamic_ps_timeout));
1153 return;
1154 }
1155 }
1156 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301157 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301158
Vivek Natarajan375177b2010-02-09 14:50:28 +05301159 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301160 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301161 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001162
Vivek Natarajane8306f92011-04-06 11:41:10 +05301163 if (drv_tx_frames_pending(local))
1164 mod_timer(&local->dynamic_ps_timer, jiffies +
1165 msecs_to_jiffies(
1166 local->hw.conf.dynamic_ps_timeout));
1167 else {
1168 ieee80211_send_nullfunc(local, sdata, 1);
1169 /* Flush to get the tx status of nullfunc frame */
1170 drv_flush(local, false);
1171 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301172 }
1173
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001174 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1175 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301176 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1177 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1178 local->hw.conf.flags |= IEEE80211_CONF_PS;
1179 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1180 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301181
Arik Nemtsov77b70232011-06-26 12:06:54 +03001182 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1183 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001184}
1185
1186void ieee80211_dynamic_ps_timer(unsigned long data)
1187{
1188 struct ieee80211_local *local = (void *) data;
1189
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001190 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001191 return;
1192
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001193 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001194}
1195
Johannes Berg60f8b392008-09-08 17:44:22 +02001196/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001197static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001198 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001199 u8 *wmm_param, size_t wmm_param_len)
1200{
Jiri Bencf0706e82007-05-05 11:45:53 -07001201 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001202 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001203 size_t left;
1204 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001205 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001206
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001207 if (!local->ops->conf_tx)
1208 return;
1209
Johannes Bergaf6b6372009-12-23 13:15:35 +01001210 if (local->hw.queues < 4)
Johannes Berg3434fbd2008-05-03 00:59:37 +02001211 return;
1212
1213 if (!wmm_param)
1214 return;
1215
Jiri Bencf0706e82007-05-05 11:45:53 -07001216 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1217 return;
Kalle Valoab133152010-01-12 10:42:31 +02001218
1219 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001220 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001221
Jiri Bencf0706e82007-05-05 11:45:53 -07001222 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001223 if (count == ifmgd->wmm_last_param_set)
Jiri Bencf0706e82007-05-05 11:45:53 -07001224 return;
Johannes Berg46900292009-02-15 12:44:28 +01001225 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001226
1227 pos = wmm_param + 8;
1228 left = wmm_param_len - 8;
1229
1230 memset(&params, 0, sizeof(params));
1231
Jiri Bencf0706e82007-05-05 11:45:53 -07001232 local->wmm_acm = 0;
1233 for (; left >= 4; left -= 4, pos += 4) {
1234 int aci = (pos[0] >> 5) & 0x03;
1235 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001236 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001237 int queue;
1238
1239 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001240 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001241 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001242 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001243 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001244 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1245 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001246 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001247 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001248 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001249 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001250 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001251 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1252 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001253 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001254 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001255 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001256 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001257 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001258 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1259 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001260 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001261 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001262 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001263 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001264 if (acm)
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001265 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001266 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1267 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001268 break;
1269 }
1270
1271 params.aifs = pos[0] & 0x0f;
1272 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1273 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001274 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001275 params.uapsd = uapsd;
1276
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001277#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001278 wiphy_debug(local->hw.wiphy,
1279 "WMM queue=%d aci=%d acm=%d aifs=%d "
1280 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1281 queue, aci, acm,
1282 params.aifs, params.cw_min, params.cw_max,
1283 params.txop, params.uapsd);
Johannes Berg3330d7be2008-02-10 16:49:38 +01001284#endif
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001285 sdata->tx_conf[queue] = params;
1286 if (drv_conf_tx(local, sdata, queue, &params))
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001287 wiphy_debug(local->hw.wiphy,
1288 "failed to set TX queue parameters for queue %d\n",
1289 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001290 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001291
1292 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001293 sdata->vif.bss_conf.qos = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001294}
1295
Johannes Berg7a5158e2008-10-08 10:59:33 +02001296static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1297 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001298{
Johannes Bergbda39332008-10-11 01:51:51 +02001299 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001300 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001301 bool use_protection;
1302 bool use_short_preamble;
1303 bool use_short_slot;
1304
1305 if (erp_valid) {
1306 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1307 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1308 } else {
1309 use_protection = false;
1310 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1311 }
1312
1313 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Felix Fietkau43d35342010-01-15 03:00:48 +01001314 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
1315 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001316
Johannes Berg471b3ef2007-12-28 14:32:58 +01001317 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001318 bss_conf->use_cts_prot = use_protection;
1319 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001320 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001321
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001322 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001323 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001324 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001325 }
Daniel Draked9430a32007-07-27 15:43:24 +02001326
Johannes Berg7a5158e2008-10-08 10:59:33 +02001327 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001328 bss_conf->use_short_slot = use_short_slot;
1329 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001330 }
1331
1332 return changed;
1333}
1334
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001335static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001336 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001337 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001338{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001339 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001340 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001341 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001342
Johannes Bergae5eb022008-10-14 16:58:37 +02001343 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001344 /* set timing information */
Juuso Oikarinen68542962010-06-09 13:43:26 +03001345 bss_conf->beacon_int = cbss->beacon_interval;
Johannes Berge9ac0742012-03-13 14:29:30 +01001346 bss_conf->last_tsf = cbss->tsf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001347
Johannes Berg77fdaa12009-07-07 03:45:17 +02001348 bss_info_changed |= BSS_CHANGED_BEACON_INT;
1349 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001350 cbss->capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001351
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001352 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1353 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1354
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001355 sdata->u.mgd.associated = cbss;
1356 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001357
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001358 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1359
Johannes Bergb291ba12009-07-10 15:29:03 +02001360 /* just to be sure */
1361 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1362 IEEE80211_STA_BEACON_POLL);
1363
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001364 ieee80211_led_assoc(local, 1);
1365
Johannes Berge5b900d2010-07-29 16:08:55 +02001366 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1367 bss_conf->dtim_period = bss->dtim_period;
1368 else
1369 bss_conf->dtim_period = 0;
1370
Juuso Oikarinen68542962010-06-09 13:43:26 +03001371 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001372
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001373 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001374 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001375 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001376 bss_info_changed |= BSS_CHANGED_CQM;
1377
Juuso Oikarinen68542962010-06-09 13:43:26 +03001378 /* Enable ARP filtering */
1379 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1380 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1381 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1382 }
1383
Johannes Bergae5eb022008-10-14 16:58:37 +02001384 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001385
Johannes Berg056508d2009-07-30 21:43:55 +02001386 mutex_lock(&local->iflist_mtx);
1387 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +02001388 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02001389 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001390
John W. Linville8a5b33f2010-01-06 15:39:39 -05001391 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001392 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001393}
1394
Jouni Malinene69e95d2010-03-29 23:29:31 -07001395static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001396 u16 stype, u16 reason, bool tx,
1397 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001398{
Johannes Berg46900292009-02-15 12:44:28 +01001399 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001400 struct ieee80211_local *local = sdata->local;
1401 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001402 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001403
Johannes Berg77fdaa12009-07-07 03:45:17 +02001404 ASSERT_MGD_MTX(ifmgd);
1405
Johannes Berg37ad3882012-02-24 13:50:54 +01001406 if (WARN_ON_ONCE(tx && !frame_buf))
1407 return;
1408
Johannes Bergb291ba12009-07-10 15:29:03 +02001409 if (WARN_ON(!ifmgd->associated))
1410 return;
1411
Johannes Berg77fdaa12009-07-07 03:45:17 +02001412 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001413
1414 /*
1415 * we need to commit the associated = NULL change because the
1416 * scan code uses that to determine whether this iface should
1417 * go to/wake up from powersave or not -- and could otherwise
1418 * wake the queues erroneously.
1419 */
1420 smp_mb();
1421
1422 /*
1423 * Thus, we can only afterwards stop the queues -- to account
1424 * for the case where another CPU is finishing a scan at this
1425 * time -- we don't want the scan code to enable queues.
1426 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001427
John W. Linville8a5b33f2010-01-06 15:39:39 -05001428 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001429 netif_carrier_off(sdata->dev);
1430
Johannes Berg2a419052010-06-10 10:21:29 +02001431 mutex_lock(&local->sta_mtx);
Eliad Peller576f0802012-06-01 11:14:03 +03001432 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301433 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001434 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +02001435 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +05301436 }
Johannes Berg2a419052010-06-10 10:21:29 +02001437 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001438
Johannes Berg37ad3882012-02-24 13:50:54 +01001439 /* deauthenticate/disassociate now */
1440 if (tx || frame_buf)
Eliad Peller576f0802012-06-01 11:14:03 +03001441 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1442 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001443
1444 /* flush out frame */
1445 if (tx)
1446 drv_flush(local, false);
1447
Eliad Peller576f0802012-06-01 11:14:03 +03001448 /* clear bssid only after building the needed mgmt frames */
1449 memset(ifmgd->bssid, 0, ETH_ALEN);
1450
Johannes Berg37ad3882012-02-24 13:50:54 +01001451 /* remove AP and TDLS peers */
1452 sta_info_flush(local, sdata);
1453
1454 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001455 changed |= ieee80211_reset_erp_info(sdata);
1456
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001457 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001458 changed |= BSS_CHANGED_ASSOC;
1459 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001460
Johannes Berg413ad502009-05-08 21:21:06 +02001461 /* on the next assoc, re-program HT parameters */
1462 sdata->ht_opmode_valid = false;
Ben Greearef96a8422011-11-18 11:32:00 -08001463 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1464 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001465
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301466 local->power_constr_level = 0;
1467
Kalle Valo520eb822008-12-18 23:35:27 +02001468 del_timer_sync(&local->dynamic_ps_timer);
1469 cancel_work_sync(&local->dynamic_ps_enable_work);
1470
Kalle Valoe0cb6862008-12-18 23:35:13 +02001471 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1472 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
Johannes Berg3cc52402012-03-09 13:12:35 +01001473 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001474 }
Eliad Pellera3314002011-05-26 11:46:37 +03001475 local->ps_sdata = NULL;
Kalle Valoe0cb6862008-12-18 23:35:13 +02001476
Juuso Oikarinen68542962010-06-09 13:43:26 +03001477 /* Disable ARP filtering */
1478 if (sdata->vif.bss_conf.arp_filter_enabled) {
1479 sdata->vif.bss_conf.arp_filter_enabled = false;
1480 changed |= BSS_CHANGED_ARP_FILTER;
1481 }
1482
Johannes Berg3abead52012-03-02 15:56:59 +01001483 sdata->vif.bss_conf.qos = false;
1484 changed |= BSS_CHANGED_QOS;
1485
Johannes Berg0aaffa92010-05-05 15:28:27 +02001486 /* The BSSID (not really interesting) and HT changed */
1487 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001488 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001489
Johannes Berg3cc52402012-03-09 13:12:35 +01001490 /* channel(_type) changes are handled by ieee80211_hw_config */
1491 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
1492 ieee80211_hw_config(local, 0);
1493
Johannes Berg3abead52012-03-02 15:56:59 +01001494 /* disassociated - set to defaults now */
1495 ieee80211_set_wmm_default(sdata, false);
1496
Johannes Bergb9dcf712010-08-27 12:35:54 +02001497 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1498 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1499 del_timer_sync(&sdata->u.mgd.timer);
1500 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001501}
Jiri Bencf0706e82007-05-05 11:45:53 -07001502
Kalle Valo3cf335d2009-03-22 21:57:06 +02001503void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1504 struct ieee80211_hdr *hdr)
1505{
1506 /*
1507 * We can postpone the mgd.timer whenever receiving unicast frames
1508 * from AP because we know that the connection is working both ways
1509 * at that time. But multicast frames (and hence also beacons) must
1510 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001511 * data idle periods for sending the periodic probe request to the
1512 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001513 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001514 if (is_multicast_ether_addr(hdr->addr1))
1515 return;
1516
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001517 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001518}
Jiri Bencf0706e82007-05-05 11:45:53 -07001519
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001520static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1521{
1522 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1523
1524 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1525 IEEE80211_STA_CONNECTION_POLL)))
1526 return;
1527
1528 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1529 IEEE80211_STA_BEACON_POLL);
1530 mutex_lock(&sdata->local->iflist_mtx);
1531 ieee80211_recalc_ps(sdata->local, -1);
1532 mutex_unlock(&sdata->local->iflist_mtx);
1533
1534 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1535 return;
1536
1537 /*
1538 * We've received a probe response, but are not sure whether
1539 * we have or will be receiving any beacons or data, so let's
1540 * schedule the timers again, just in case.
1541 */
1542 ieee80211_sta_reset_beacon_monitor(sdata);
1543
1544 mod_timer(&ifmgd->conn_mon_timer,
1545 round_jiffies_up(jiffies +
1546 IEEE80211_CONNECTION_IDLE_TIME));
1547}
1548
1549void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001550 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001551{
Felix Fietkau75706d02010-12-02 21:01:07 +01001552 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001553 return;
1554
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001555 if (ack)
1556 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001557
1558 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1559 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001560 if (ack)
1561 sdata->u.mgd.probe_send_count = 0;
1562 else
1563 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001564 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1565 }
1566}
1567
Maxim Levitskya43abf22009-07-31 18:54:12 +03001568static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1569{
1570 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1571 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001572 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001573 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001574
1575 /*
1576 * Try sending broadcast probe requests for the last three
1577 * probe requests after the first ones failed since some
1578 * buggy APs only support broadcast probe requests.
1579 */
1580 if (ifmgd->probe_send_count >= unicast_limit)
1581 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001582
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001583 /*
1584 * When the hardware reports an accurate Tx ACK status, it's
1585 * better to send a nullfunc frame instead of a probe request,
1586 * as it will kick us off the AP quickly if we aren't associated
1587 * anymore. The timeout will be reset if the frame is ACKed by
1588 * the AP.
1589 */
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001590 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1591 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001592 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001593 } else {
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001594 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Paul Stewarta806c552011-06-23 09:00:11 -08001595 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0,
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +05301596 (u32) -1, true, false);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001597 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001598
1599 ifmgd->probe_send_count++;
Ben Greear180205b2011-02-04 15:30:24 -08001600 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001601 run_again(ifmgd, ifmgd->probe_timeout);
1602}
1603
Johannes Bergb291ba12009-07-10 15:29:03 +02001604static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1605 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001606{
Kalle Valo04de8382009-03-22 21:57:35 +02001607 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001608 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001609
Johannes Berg9607e6b2009-12-23 13:15:31 +01001610 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001611 return;
1612
Luis R. Rodriguez91a3bd72009-07-23 16:37:47 -07001613 if (sdata->local->scanning)
1614 return;
1615
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001616 if (sdata->local->tmp_channel)
1617 return;
1618
Johannes Berg77fdaa12009-07-07 03:45:17 +02001619 mutex_lock(&ifmgd->mtx);
1620
1621 if (!ifmgd->associated)
1622 goto out;
1623
Michael Buescha8604022009-04-15 14:41:22 -04001624#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergb291ba12009-07-10 15:29:03 +02001625 if (beacon && net_ratelimit())
1626 printk(KERN_DEBUG "%s: detected beacon loss from AP "
Johannes Berg47846c92009-11-25 17:46:19 +01001627 "- sending probe request\n", sdata->name);
Michael Buescha8604022009-04-15 14:41:22 -04001628#endif
Kalle Valo04de8382009-03-22 21:57:35 +02001629
Johannes Bergb291ba12009-07-10 15:29:03 +02001630 /*
1631 * The driver/our work has already reported this event or the
1632 * connection monitoring has kicked in and we have already sent
1633 * a probe request. Or maybe the AP died and the driver keeps
1634 * reporting until we disassociate...
1635 *
1636 * In either case we have to ignore the current call to this
1637 * function (except for setting the correct probe reason bit)
1638 * because otherwise we would reset the timer every time and
1639 * never check whether we received a probe response!
1640 */
1641 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1642 IEEE80211_STA_CONNECTION_POLL))
1643 already = true;
1644
1645 if (beacon)
1646 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1647 else
1648 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1649
1650 if (already)
1651 goto out;
1652
Johannes Berg4e751842009-06-10 15:16:52 +02001653 mutex_lock(&sdata->local->iflist_mtx);
1654 ieee80211_recalc_ps(sdata->local, -1);
1655 mutex_unlock(&sdata->local->iflist_mtx);
1656
Maxim Levitskya43abf22009-07-31 18:54:12 +03001657 ifmgd->probe_send_count = 0;
1658 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001659 out:
1660 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001661}
1662
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001663struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1664 struct ieee80211_vif *vif)
1665{
1666 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1667 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1668 struct sk_buff *skb;
1669 const u8 *ssid;
1670
1671 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1672 return NULL;
1673
1674 ASSERT_MGD_MTX(ifmgd);
1675
1676 if (!ifmgd->associated)
1677 return NULL;
1678
1679 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1680 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
Johannes Berg85a237f2011-07-18 18:08:36 +02001681 (u32) -1, ssid + 2, ssid[1],
1682 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001683
1684 return skb;
1685}
1686EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1687
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001688static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1689{
1690 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1691 struct ieee80211_local *local = sdata->local;
1692 u8 bssid[ETH_ALEN];
Johannes Berg5fef7db2012-02-24 13:50:52 +01001693 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001694
1695 mutex_lock(&ifmgd->mtx);
1696 if (!ifmgd->associated) {
1697 mutex_unlock(&ifmgd->mtx);
1698 return;
1699 }
1700
1701 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1702
Ben Greear180205b2011-02-04 15:30:24 -08001703 printk(KERN_DEBUG "%s: Connection to AP %pM lost.\n",
1704 sdata->name, bssid);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001705
Johannes Berg37ad3882012-02-24 13:50:54 +01001706 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1707 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1708 false, frame_buf);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001709 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001710
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001711 /*
1712 * must be outside lock due to cfg80211,
1713 * but that's not a problem.
1714 */
Johannes Berg5fef7db2012-02-24 13:50:52 +01001715 cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001716
1717 mutex_lock(&local->mtx);
1718 ieee80211_recalc_idle(local);
1719 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001720}
1721
1722void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001723{
1724 struct ieee80211_sub_if_data *sdata =
1725 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001726 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001727 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1728 struct sta_info *sta;
1729
1730 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301731 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001732 sta = sta_info_get(sdata, ifmgd->bssid);
1733 if (sta)
1734 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301735 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001736 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001737
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001738 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1739 __ieee80211_connection_loss(sdata);
1740 else
1741 ieee80211_mgd_probe_ap(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001742}
1743
Kalle Valo04de8382009-03-22 21:57:35 +02001744void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1745{
1746 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001747 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001748
Johannes Bergb5878a22010-04-07 16:48:40 +02001749 trace_api_beacon_loss(sdata);
1750
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001751 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1752 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001753}
1754EXPORT_SYMBOL(ieee80211_beacon_loss);
1755
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001756void ieee80211_connection_loss(struct ieee80211_vif *vif)
1757{
1758 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1759 struct ieee80211_hw *hw = &sdata->local->hw;
1760
Johannes Bergb5878a22010-04-07 16:48:40 +02001761 trace_api_connection_loss(sdata);
1762
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001763 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1764 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1765}
1766EXPORT_SYMBOL(ieee80211_connection_loss);
1767
1768
Johannes Berg66e67e42012-01-20 13:55:27 +01001769static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1770 bool assoc)
1771{
1772 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1773
1774 lockdep_assert_held(&sdata->u.mgd.mtx);
1775
Johannes Berg66e67e42012-01-20 13:55:27 +01001776 if (!assoc) {
1777 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1778
1779 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1780 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1781 }
1782
1783 cfg80211_put_bss(auth_data->bss);
1784 kfree(auth_data);
1785 sdata->u.mgd.auth_data = NULL;
1786}
1787
1788static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1789 struct ieee80211_mgmt *mgmt, size_t len)
1790{
1791 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1792 u8 *pos;
1793 struct ieee802_11_elems elems;
1794
1795 pos = mgmt->u.auth.variable;
1796 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1797 if (!elems.challenge)
1798 return;
1799 auth_data->expected_transaction = 4;
1800 ieee80211_send_auth(sdata, 3, auth_data->algorithm,
1801 elems.challenge - 2, elems.challenge_len + 2,
1802 auth_data->bss->bssid, auth_data->bss->bssid,
1803 auth_data->key, auth_data->key_len,
1804 auth_data->key_idx);
1805}
1806
1807static enum rx_mgmt_action __must_check
1808ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1809 struct ieee80211_mgmt *mgmt, size_t len)
1810{
1811 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1812 u8 bssid[ETH_ALEN];
1813 u16 auth_alg, auth_transaction, status_code;
1814 struct sta_info *sta;
1815
1816 lockdep_assert_held(&ifmgd->mtx);
1817
1818 if (len < 24 + 6)
1819 return RX_MGMT_NONE;
1820
1821 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1822 return RX_MGMT_NONE;
1823
1824 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1825
Felix Fietkau888d04d2012-03-01 15:22:09 +01001826 if (compare_ether_addr(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001827 return RX_MGMT_NONE;
1828
1829 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1830 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1831 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1832
1833 if (auth_alg != ifmgd->auth_data->algorithm ||
1834 auth_transaction != ifmgd->auth_data->expected_transaction)
1835 return RX_MGMT_NONE;
1836
1837 if (status_code != WLAN_STATUS_SUCCESS) {
1838 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
1839 sdata->name, mgmt->sa, status_code);
Eliad Peller79746482012-05-13 18:07:04 +03001840 ieee80211_destroy_auth_data(sdata, false);
1841 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001842 }
1843
1844 switch (ifmgd->auth_data->algorithm) {
1845 case WLAN_AUTH_OPEN:
1846 case WLAN_AUTH_LEAP:
1847 case WLAN_AUTH_FT:
1848 break;
1849 case WLAN_AUTH_SHARED_KEY:
1850 if (ifmgd->auth_data->expected_transaction != 4) {
1851 ieee80211_auth_challenge(sdata, mgmt, len);
1852 /* need another frame */
1853 return RX_MGMT_NONE;
1854 }
1855 break;
1856 default:
1857 WARN_ONCE(1, "invalid auth alg %d",
1858 ifmgd->auth_data->algorithm);
1859 return RX_MGMT_NONE;
1860 }
1861
1862 printk(KERN_DEBUG "%s: authenticated\n", sdata->name);
Johannes Berg66e67e42012-01-20 13:55:27 +01001863 ifmgd->auth_data->done = true;
1864 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1865 run_again(ifmgd, ifmgd->auth_data->timeout);
1866
1867 /* move station state to auth */
1868 mutex_lock(&sdata->local->sta_mtx);
1869 sta = sta_info_get(sdata, bssid);
1870 if (!sta) {
1871 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1872 goto out_err;
1873 }
1874 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
1875 printk(KERN_DEBUG "%s: failed moving %pM to auth\n",
1876 sdata->name, bssid);
1877 goto out_err;
1878 }
1879 mutex_unlock(&sdata->local->sta_mtx);
1880
1881 return RX_MGMT_CFG80211_RX_AUTH;
1882 out_err:
1883 mutex_unlock(&sdata->local->sta_mtx);
1884 /* ignore frame -- wait for timeout */
1885 return RX_MGMT_NONE;
1886}
1887
1888
Johannes Berg77fdaa12009-07-07 03:45:17 +02001889static enum rx_mgmt_action __must_check
1890ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001891 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001892{
Johannes Berg46900292009-02-15 12:44:28 +01001893 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001894 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001895 u16 reason_code;
1896
Johannes Berg66e67e42012-01-20 13:55:27 +01001897 lockdep_assert_held(&ifmgd->mtx);
1898
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001899 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001900 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001901
Johannes Berg66e67e42012-01-20 13:55:27 +01001902 if (!ifmgd->associated ||
Felix Fietkau888d04d2012-03-01 15:22:09 +01001903 compare_ether_addr(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001904 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001905
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001906 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001907
1908 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1909
Johannes Berg77fdaa12009-07-07 03:45:17 +02001910 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001911 sdata->name, bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001912
Johannes Berg37ad3882012-02-24 13:50:54 +01001913 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1914
Johannes Berg7da7cc12010-08-05 17:02:38 +02001915 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001916 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001917 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001918
1919 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001920}
1921
1922
Johannes Berg77fdaa12009-07-07 03:45:17 +02001923static enum rx_mgmt_action __must_check
1924ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1925 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001926{
Johannes Berg46900292009-02-15 12:44:28 +01001927 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001928 u16 reason_code;
1929
Johannes Berg66e67e42012-01-20 13:55:27 +01001930 lockdep_assert_held(&ifmgd->mtx);
1931
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001932 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001933 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001934
Johannes Berg66e67e42012-01-20 13:55:27 +01001935 if (!ifmgd->associated ||
Felix Fietkau888d04d2012-03-01 15:22:09 +01001936 compare_ether_addr(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001937 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001938
1939 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1940
Johannes Berg0ff71612009-09-26 14:45:41 +02001941 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001942 sdata->name, mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001943
Johannes Berg37ad3882012-02-24 13:50:54 +01001944 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1945
Johannes Berg7da7cc12010-08-05 17:02:38 +02001946 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001947 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001948 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01001949
Johannes Berg77fdaa12009-07-07 03:45:17 +02001950 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001951}
1952
Christian Lamparterc74d0842011-10-15 00:14:49 +02001953static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
1954 u8 *supp_rates, unsigned int supp_rates_len,
1955 u32 *rates, u32 *basic_rates,
1956 bool *have_higher_than_11mbit,
1957 int *min_rate, int *min_rate_index)
1958{
1959 int i, j;
1960
1961 for (i = 0; i < supp_rates_len; i++) {
1962 int rate = (supp_rates[i] & 0x7f) * 5;
1963 bool is_basic = !!(supp_rates[i] & 0x80);
1964
1965 if (rate > 110)
1966 *have_higher_than_11mbit = true;
1967
1968 /*
1969 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
1970 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
1971 *
1972 * Note: Even through the membership selector and the basic
1973 * rate flag share the same bit, they are not exactly
1974 * the same.
1975 */
1976 if (!!(supp_rates[i] & 0x80) &&
1977 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1978 continue;
1979
1980 for (j = 0; j < sband->n_bitrates; j++) {
1981 if (sband->bitrates[j].bitrate == rate) {
1982 *rates |= BIT(j);
1983 if (is_basic)
1984 *basic_rates |= BIT(j);
1985 if (rate < *min_rate) {
1986 *min_rate = rate;
1987 *min_rate_index = j;
1988 }
1989 break;
1990 }
1991 }
1992 }
1993}
Jiri Bencf0706e82007-05-05 11:45:53 -07001994
Johannes Berg66e67e42012-01-20 13:55:27 +01001995static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
1996 bool assoc)
1997{
1998 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
1999
2000 lockdep_assert_held(&sdata->u.mgd.mtx);
2001
Johannes Berg66e67e42012-01-20 13:55:27 +01002002 if (!assoc) {
2003 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2004
2005 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2006 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2007 }
2008
2009 kfree(assoc_data);
2010 sdata->u.mgd.assoc_data = NULL;
2011}
2012
2013static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2014 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002015 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002016{
Johannes Berg46900292009-02-15 12:44:28 +01002017 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002018 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002019 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002020 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002021 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002022 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002023 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002024 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002025 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002026 int err;
Johannes Bergae5eb022008-10-14 16:58:37 +02002027 u16 ap_ht_cap_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07002028
Johannes Bergaf6b6372009-12-23 13:15:35 +01002029 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002030
Jiri Bencf0706e82007-05-05 11:45:53 -07002031 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002032 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002033
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002034 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Berg05cb9102011-10-28 11:59:47 +02002035 printk(KERN_DEBUG
2036 "%s: invalid AID value 0x%x; bits 15:14 not set\n",
2037 sdata->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002038 aid &= ~(BIT(15) | BIT(14));
2039
Johannes Berg05cb9102011-10-28 11:59:47 +02002040 ifmgd->broken_ap = false;
2041
2042 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2043 printk(KERN_DEBUG
2044 "%s: invalid AID value %d (out of range), turn off PS\n",
2045 sdata->name, aid);
2046 aid = 0;
2047 ifmgd->broken_ap = true;
2048 }
2049
Johannes Bergaf6b6372009-12-23 13:15:35 +01002050 pos = mgmt->u.assoc_resp.variable;
2051 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2052
Jiri Bencf0706e82007-05-05 11:45:53 -07002053 if (!elems.supp_rates) {
2054 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Johannes Berg47846c92009-11-25 17:46:19 +01002055 sdata->name);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002056 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002057 }
2058
Johannes Berg46900292009-02-15 12:44:28 +01002059 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002060
Guy Eilam2a33bee2011-08-17 15:18:15 +03002061 mutex_lock(&sdata->local->sta_mtx);
2062 /*
2063 * station info was already allocated and inserted before
2064 * the association and should be available to us
2065 */
Johannes Berg7852e362012-01-20 13:55:24 +01002066 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002067 if (WARN_ON(!sta)) {
2068 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002069 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002070 }
2071
Johannes Berg66e67e42012-01-20 13:55:27 +01002072 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +01002073
Johannes Bergab1faea2009-07-01 21:41:17 +02002074 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Ben Greearef96a8422011-11-18 11:32:00 -08002075 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002076 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002077
2078 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002079
Johannes Berg4b7679a2008-09-18 18:14:18 +02002080 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002081
Johannes Berg46900292009-02-15 12:44:28 +01002082 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002083 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002084
Johannes Bergddf4ac52008-10-22 11:41:38 +02002085 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002086 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002087
Johannes Bergc8987872012-01-20 13:55:17 +01002088 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2089 if (!err)
2090 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2091 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2092 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2093 if (err) {
2094 printk(KERN_DEBUG
2095 "%s: failed to move station %pM to desired state\n",
2096 sdata->name, sta->sta.addr);
2097 WARN_ON(__sta_info_destroy(sta));
2098 mutex_unlock(&sdata->local->sta_mtx);
2099 return false;
2100 }
2101
Johannes Berg7852e362012-01-20 13:55:24 +01002102 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002103
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002104 /*
2105 * Always handle WMM once after association regardless
2106 * of the first value the AP uses. Setting -1 here has
2107 * that effect because the AP values is an unsigned
2108 * 4-bit value.
2109 */
2110 ifmgd->wmm_last_param_set = -1;
2111
Johannes Bergddf4ac52008-10-22 11:41:38 +02002112 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002113 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002114 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002115 else
Johannes Berg3abead52012-03-02 15:56:59 +01002116 ieee80211_set_wmm_default(sdata, false);
2117 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002118
Johannes Bergae5eb022008-10-14 16:58:37 +02002119 if (elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002120 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Bergae5eb022008-10-14 16:58:37 +02002121 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +05302122 cbss->bssid, ap_ht_cap_flags,
2123 false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002124
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002125 /* set AID and assoc capability,
2126 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002127 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002128 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002129 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002130
Kalle Valo15b7b062009-03-22 21:57:14 +02002131 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002132 * If we're using 4-addr mode, let the AP know that we're
2133 * doing so, so that it can create the STA VLAN on its side
2134 */
2135 if (ifmgd->use_4addr)
2136 ieee80211_send_4addr_nullfunc(local, sdata);
2137
2138 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002139 * Start timer to probe the connection to the AP now.
2140 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002141 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002142 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002143 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002144
Johannes Bergaf6b6372009-12-23 13:15:35 +01002145 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002146}
2147
Johannes Berg66e67e42012-01-20 13:55:27 +01002148static enum rx_mgmt_action __must_check
2149ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2150 struct ieee80211_mgmt *mgmt, size_t len,
2151 struct cfg80211_bss **bss)
2152{
2153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2154 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2155 u16 capab_info, status_code, aid;
2156 struct ieee802_11_elems elems;
2157 u8 *pos;
2158 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002159
Johannes Berg66e67e42012-01-20 13:55:27 +01002160 lockdep_assert_held(&ifmgd->mtx);
2161
2162 if (!assoc_data)
2163 return RX_MGMT_NONE;
Felix Fietkau888d04d2012-03-01 15:22:09 +01002164 if (compare_ether_addr(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002165 return RX_MGMT_NONE;
2166
2167 /*
2168 * AssocResp and ReassocResp have identical structure, so process both
2169 * of them in this function.
2170 */
2171
2172 if (len < 24 + 6)
2173 return RX_MGMT_NONE;
2174
2175 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2176 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2177 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2178 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2179
2180 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
2181 "status=%d aid=%d)\n",
2182 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
2183 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2184
2185 pos = mgmt->u.assoc_resp.variable;
2186 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2187
2188 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2189 elems.timeout_int && elems.timeout_int_len == 5 &&
2190 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2191 u32 tu, ms;
2192 tu = get_unaligned_le32(elems.timeout_int + 1);
2193 ms = tu * 1024 / 1000;
2194 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
2195 "comeback duration %u TU (%u ms)\n",
2196 sdata->name, mgmt->sa, tu, ms);
2197 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2198 if (ms > IEEE80211_ASSOC_TIMEOUT)
2199 run_again(ifmgd, assoc_data->timeout);
2200 return RX_MGMT_NONE;
2201 }
2202
2203 *bss = assoc_data->bss;
2204
2205 if (status_code != WLAN_STATUS_SUCCESS) {
2206 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
2207 sdata->name, mgmt->sa, status_code);
2208 ieee80211_destroy_assoc_data(sdata, false);
2209 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002210 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2211 /* oops -- internal error -- send timeout for now */
Eliad Peller3cf16f72012-07-02 14:42:03 +03002212 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002213 cfg80211_put_bss(*bss);
2214 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2215 }
Eliad Peller3cf16f72012-07-02 14:42:03 +03002216 printk(KERN_DEBUG "%s: associated\n", sdata->name);
Johannes Berg79ebfb82012-02-20 14:19:58 +01002217
2218 /*
2219 * destroy assoc_data afterwards, as otherwise an idle
2220 * recalc after assoc_data is NULL but before associated
2221 * is set can cause the interface to go idle
2222 */
2223 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002224 }
2225
2226 return RX_MGMT_CFG80211_RX_ASSOC;
2227}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002228static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002229 struct ieee80211_mgmt *mgmt,
2230 size_t len,
2231 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002232 struct ieee802_11_elems *elems,
2233 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002234{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002235 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002236 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002237 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002238 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002239 bool need_ps = false;
2240
Johannes Berg66e67e42012-01-20 13:55:27 +01002241 if (sdata->u.mgd.associated &&
Felix Fietkau888d04d2012-03-01 15:22:09 +01002242 compare_ether_addr(mgmt->bssid, sdata->u.mgd.associated->bssid)
2243 == 0) {
Johannes Berg56007a02010-01-26 14:19:52 +01002244 bss = (void *)sdata->u.mgd.associated->priv;
2245 /* not previously set so we may need to recalc */
2246 need_ps = !bss->dtim_period;
2247 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002248
Johannes Berg5bda6172008-09-08 11:05:10 +02002249 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002250 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2251 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002252 else
2253 freq = rx_status->freq;
2254
2255 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2256
2257 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2258 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002259
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002260 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002261 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002262 if (bss)
2263 ieee80211_rx_bss_put(local, bss);
2264
2265 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002266 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002267
Johannes Berg56007a02010-01-26 14:19:52 +01002268 if (need_ps) {
2269 mutex_lock(&local->iflist_mtx);
2270 ieee80211_recalc_ps(local, -1);
2271 mutex_unlock(&local->iflist_mtx);
2272 }
2273
Sujithc481ec92009-01-06 09:28:37 +05302274 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002275 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002276 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05302277 struct ieee80211_channel_sw_ie *sw_elem =
2278 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Berg5ce6e432010-05-11 16:20:57 +02002279 ieee80211_sta_process_chanswitch(sdata, sw_elem,
2280 bss, rx_status->mactime);
Sujithc481ec92009-01-06 09:28:37 +05302281 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002282}
2283
2284
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002285static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002286 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002287{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002288 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002289 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002290 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2291 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002292 struct ieee802_11_elems elems;
2293
Kalle Valo15b7b062009-03-22 21:57:14 +02002294 ifmgd = &sdata->u.mgd;
2295
Johannes Berg77fdaa12009-07-07 03:45:17 +02002296 ASSERT_MGD_MTX(ifmgd);
2297
Felix Fietkau888d04d2012-03-01 15:22:09 +01002298 if (compare_ether_addr(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002299 return; /* ignore ProbeResp to foreign address */
2300
Ester Kummerae6a44e2008-06-27 18:54:48 +03002301 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2302 if (baselen > len)
2303 return;
2304
2305 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2306 &elems);
2307
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002308 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002309
Johannes Berg77fdaa12009-07-07 03:45:17 +02002310 if (ifmgd->associated &&
Felix Fietkau888d04d2012-03-01 15:22:09 +01002311 compare_ether_addr(mgmt->bssid, ifmgd->associated->bssid) == 0)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002312 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002313
2314 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Felix Fietkau888d04d2012-03-01 15:22:09 +01002315 compare_ether_addr(mgmt->bssid, ifmgd->auth_data->bss->bssid)
2316 == 0) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002317 /* got probe response, continue with auth */
2318 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
2319 ifmgd->auth_data->tries = 0;
2320 ifmgd->auth_data->timeout = jiffies;
2321 run_again(ifmgd, ifmgd->auth_data->timeout);
2322 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002323}
2324
Johannes Bergd91f36d2009-04-16 13:17:26 +02002325/*
2326 * This is the canonical list of information elements we care about,
2327 * the filter code also gives us all changes to the Microsoft OUI
2328 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2329 *
2330 * We implement beacon filtering in software since that means we can
2331 * avoid processing the frame here and in cfg80211, and userspace
2332 * will not be able to tell whether the hardware supports it or not.
2333 *
2334 * XXX: This list needs to be dynamic -- userspace needs to be able to
2335 * add items it requires. It also needs to be able to tell us to
2336 * look out for other vendor IEs.
2337 */
2338static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002339 (1ULL << WLAN_EID_COUNTRY) |
2340 (1ULL << WLAN_EID_ERP_INFO) |
2341 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2342 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2343 (1ULL << WLAN_EID_HT_CAPABILITY) |
2344 (1ULL << WLAN_EID_HT_INFORMATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002345
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002346static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002347 struct ieee80211_mgmt *mgmt,
2348 size_t len,
2349 struct ieee80211_rx_status *rx_status)
2350{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002351 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002352 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002353 size_t baselen;
2354 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002355 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002356 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002357 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002358 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002359 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002360 u8 *bssid;
2361
Johannes Berg66e67e42012-01-20 13:55:27 +01002362 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002363
Ester Kummerae6a44e2008-06-27 18:54:48 +03002364 /* Process beacon from the current BSS */
2365 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2366 if (baselen > len)
2367 return;
2368
Johannes Bergd91f36d2009-04-16 13:17:26 +02002369 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07002370 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002371
Johannes Berg66e67e42012-01-20 13:55:27 +01002372 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Felix Fietkau888d04d2012-03-01 15:22:09 +01002373 compare_ether_addr(mgmt->bssid, ifmgd->assoc_data->bss->bssid)
2374 == 0) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002375 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2376 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002377
Johannes Berg66e67e42012-01-20 13:55:27 +01002378 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2379 false);
2380 ifmgd->assoc_data->have_beacon = true;
2381 ifmgd->assoc_data->sent_assoc = false;
2382 /* continue assoc process */
2383 ifmgd->assoc_data->timeout = jiffies;
2384 run_again(ifmgd, ifmgd->assoc_data->timeout);
2385 return;
2386 }
2387
2388 if (!ifmgd->associated ||
Felix Fietkau888d04d2012-03-01 15:22:09 +01002389 compare_ether_addr(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002390 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002391 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002392
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002393 /* Track average RSSI from the Beacon frames of the current AP */
2394 ifmgd->last_beacon_signal = rx_status->signal;
2395 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2396 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002397 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002398 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002399 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002400 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002401 } else {
2402 ifmgd->ave_beacon_signal =
2403 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2404 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2405 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002406 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002407 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002408
2409 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2410 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2411 int sig = ifmgd->ave_beacon_signal;
2412 int last_sig = ifmgd->last_ave_beacon_signal;
2413
2414 /*
2415 * if signal crosses either of the boundaries, invoke callback
2416 * with appropriate parameters
2417 */
2418 if (sig > ifmgd->rssi_max_thold &&
2419 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2420 ifmgd->last_ave_beacon_signal = sig;
2421 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2422 } else if (sig < ifmgd->rssi_min_thold &&
2423 (last_sig >= ifmgd->rssi_max_thold ||
2424 last_sig == 0)) {
2425 ifmgd->last_ave_beacon_signal = sig;
2426 drv_rssi_callback(local, RSSI_EVENT_LOW);
2427 }
2428 }
2429
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002430 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002431 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002432 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002433 int sig = ifmgd->ave_beacon_signal / 16;
2434 int last_event = ifmgd->last_cqm_event_signal;
2435 int thold = bss_conf->cqm_rssi_thold;
2436 int hyst = bss_conf->cqm_rssi_hyst;
2437 if (sig < thold &&
2438 (last_event == 0 || sig < last_event - hyst)) {
2439 ifmgd->last_cqm_event_signal = sig;
2440 ieee80211_cqm_rssi_notify(
2441 &sdata->vif,
2442 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2443 GFP_KERNEL);
2444 } else if (sig > thold &&
2445 (last_event == 0 || sig > last_event + hyst)) {
2446 ifmgd->last_cqm_event_signal = sig;
2447 ieee80211_cqm_rssi_notify(
2448 &sdata->vif,
2449 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2450 GFP_KERNEL);
2451 }
2452 }
2453
Johannes Bergb291ba12009-07-10 15:29:03 +02002454 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Jouni Malinen92778182009-05-14 21:15:36 +03002455#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2456 if (net_ratelimit()) {
2457 printk(KERN_DEBUG "%s: cancelling probereq poll due "
Johannes Berg47846c92009-11-25 17:46:19 +01002458 "to a received beacon\n", sdata->name);
Jouni Malinen92778182009-05-14 21:15:36 +03002459 }
2460#endif
Johannes Bergb291ba12009-07-10 15:29:03 +02002461 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Johannes Berg4e751842009-06-10 15:16:52 +02002462 mutex_lock(&local->iflist_mtx);
2463 ieee80211_recalc_ps(local, -1);
2464 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002465 }
2466
Johannes Bergb291ba12009-07-10 15:29:03 +02002467 /*
2468 * Push the beacon loss detection into the future since
2469 * we are processing a beacon from the AP just now.
2470 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002471 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002472
Johannes Bergd91f36d2009-04-16 13:17:26 +02002473 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2474 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2475 len - baselen, &elems,
2476 care_about_ies, ncrc);
2477
2478 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02002479 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
2480 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002481
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002482 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
Jouni Malinen30196672009-05-19 17:01:43 +03002483 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2484 true);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002485
Johannes Berg4ced3f72010-07-19 16:39:04 +02002486 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jouni Malinen30196672009-05-19 17:01:43 +03002487 elems.wmm_param_len);
2488 }
Johannes Bergfe3fa822008-09-08 11:05:09 +02002489
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302490 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02002491 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002492 if (local->hw.conf.dynamic_ps_timeout > 0) {
2493 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2494 ieee80211_hw_config(local,
2495 IEEE80211_CONF_CHANGE_PS);
2496 ieee80211_send_nullfunc(local, sdata, 0);
2497 } else {
2498 local->pspolling = true;
2499
2500 /*
2501 * Here is assumed that the driver will be
2502 * able to send ps-poll frame and receive a
2503 * response even though power save mode is
2504 * enabled, but some drivers might require
2505 * to disable power save here. This needs
2506 * to be investigated.
2507 */
2508 ieee80211_send_pspoll(local, sdata);
2509 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002510 }
2511 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002512
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002513 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002514 return;
2515 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002516 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002517
Johannes Berg7a5158e2008-10-08 10:59:33 +02002518 if (elems.erp_info && elems.erp_info_len >= 1) {
2519 erp_valid = true;
2520 erp_value = elems.erp_info[0];
2521 } else {
2522 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002523 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002524 changed |= ieee80211_handle_bss_capability(sdata,
2525 le16_to_cpu(mgmt->u.beacon.capab_info),
2526 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002527
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002528
Vasanthakumar Thiagarajan53d6f812009-02-11 22:18:49 +05302529 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002530 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02002531 struct sta_info *sta;
2532 struct ieee80211_supported_band *sband;
2533 u16 ap_ht_cap_flags;
2534
2535 rcu_read_lock();
2536
Johannes Bergabe60632009-11-25 17:46:18 +01002537 sta = sta_info_get(sdata, bssid);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002538 if (WARN_ON(!sta)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02002539 rcu_read_unlock();
2540 return;
2541 }
2542
2543 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2544
Ben Greearef96a8422011-11-18 11:32:00 -08002545 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergae5eb022008-10-14 16:58:37 +02002546 elems.ht_cap_elem, &sta->sta.ht_cap);
2547
2548 ap_ht_cap_flags = sta->sta.ht_cap.cap;
2549
2550 rcu_read_unlock();
2551
2552 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +05302553 bssid, ap_ht_cap_flags, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002554 }
2555
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002556 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002557 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302558 /* TODO: IBSS also needs this */
2559 if (elems.pwr_constr_elem)
2560 ieee80211_handle_pwr_constr(sdata,
2561 le16_to_cpu(mgmt->u.probe_resp.capab_info),
2562 elems.pwr_constr_elem,
2563 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002564 }
2565
Johannes Berg471b3ef2007-12-28 14:32:58 +01002566 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002567}
2568
Johannes Berg1fa57d02010-06-10 10:21:32 +02002569void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2570 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002571{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002572 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002573 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002574 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002575 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002576 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002577 u16 fc;
2578
Jiri Bencf0706e82007-05-05 11:45:53 -07002579 rx_status = (struct ieee80211_rx_status *) skb->cb;
2580 mgmt = (struct ieee80211_mgmt *) skb->data;
2581 fc = le16_to_cpu(mgmt->frame_control);
2582
Johannes Berg77fdaa12009-07-07 03:45:17 +02002583 mutex_lock(&ifmgd->mtx);
2584
Johannes Berg66e67e42012-01-20 13:55:27 +01002585 switch (fc & IEEE80211_FCTL_STYPE) {
2586 case IEEE80211_STYPE_BEACON:
2587 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2588 break;
2589 case IEEE80211_STYPE_PROBE_RESP:
2590 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2591 break;
2592 case IEEE80211_STYPE_AUTH:
2593 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2594 break;
2595 case IEEE80211_STYPE_DEAUTH:
2596 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2597 break;
2598 case IEEE80211_STYPE_DISASSOC:
2599 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2600 break;
2601 case IEEE80211_STYPE_ASSOC_RESP:
2602 case IEEE80211_STYPE_REASSOC_RESP:
2603 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2604 break;
2605 case IEEE80211_STYPE_ACTION:
2606 switch (mgmt->u.action.category) {
2607 case WLAN_CATEGORY_SPECTRUM_MGMT:
2608 ieee80211_sta_process_chanswitch(sdata,
2609 &mgmt->u.action.u.chan_switch.sw_elem,
2610 (void *)ifmgd->associated->priv,
2611 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002612 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002613 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002614 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002615 mutex_unlock(&ifmgd->mtx);
2616
Johannes Berg66e67e42012-01-20 13:55:27 +01002617 switch (rma) {
2618 case RX_MGMT_NONE:
2619 /* no action */
2620 break;
2621 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002622 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002623 break;
2624 case RX_MGMT_CFG80211_DISASSOC:
2625 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2626 break;
2627 case RX_MGMT_CFG80211_RX_AUTH:
2628 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2629 break;
2630 case RX_MGMT_CFG80211_RX_ASSOC:
2631 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2632 break;
2633 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2634 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2635 break;
2636 default:
2637 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002638 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002639}
2640
Johannes Berg9c6bd792008-09-11 00:01:52 +02002641static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002642{
2643 struct ieee80211_sub_if_data *sdata =
2644 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002645 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002646 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002647
Johannes Berg5bb644a2009-05-17 11:40:42 +02002648 if (local->quiescing) {
2649 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2650 return;
2651 }
2652
Johannes Berg64592c82010-06-10 10:21:31 +02002653 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002654}
2655
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002656static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002657 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002658{
2659 struct ieee80211_local *local = sdata->local;
2660 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5fef7db2012-02-24 13:50:52 +01002661 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002662
2663 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2664 IEEE80211_STA_BEACON_POLL);
2665
Johannes Berg37ad3882012-02-24 13:50:54 +01002666 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2667 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002668 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002669
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002670 /*
2671 * must be outside lock due to cfg80211,
2672 * but that's not a problem.
2673 */
Johannes Berg5fef7db2012-02-24 13:50:52 +01002674 cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002675
2676 mutex_lock(&local->mtx);
2677 ieee80211_recalc_idle(local);
2678 mutex_unlock(&local->mtx);
2679
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002680 mutex_lock(&ifmgd->mtx);
2681}
2682
Johannes Berg66e67e42012-01-20 13:55:27 +01002683static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2684{
2685 struct ieee80211_local *local = sdata->local;
2686 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2687 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2688
2689 lockdep_assert_held(&ifmgd->mtx);
2690
2691 if (WARN_ON_ONCE(!auth_data))
2692 return -EINVAL;
2693
Johannes Berg66e67e42012-01-20 13:55:27 +01002694 auth_data->tries++;
2695
2696 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
2697 printk(KERN_DEBUG "%s: authentication with %pM timed out\n",
2698 sdata->name, auth_data->bss->bssid);
2699
2700 /*
2701 * Most likely AP is not in the range so remove the
2702 * bss struct for that AP.
2703 */
2704 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2705
2706 return -ETIMEDOUT;
2707 }
2708
2709 if (auth_data->bss->proberesp_ies) {
2710 printk(KERN_DEBUG "%s: send auth to %pM (try %d/%d)\n",
2711 sdata->name, auth_data->bss->bssid, auth_data->tries,
2712 IEEE80211_AUTH_MAX_TRIES);
2713
2714 auth_data->expected_transaction = 2;
2715 ieee80211_send_auth(sdata, 1, auth_data->algorithm,
2716 auth_data->ie, auth_data->ie_len,
2717 auth_data->bss->bssid,
2718 auth_data->bss->bssid, NULL, 0, 0);
2719 } else {
2720 const u8 *ssidie;
2721
2722 printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
2723 sdata->name, auth_data->bss->bssid, auth_data->tries,
2724 IEEE80211_AUTH_MAX_TRIES);
2725
2726 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2727 if (!ssidie)
2728 return -EINVAL;
2729 /*
2730 * Direct probe is sent to broadcast address as some APs
2731 * will not answer to direct packet in unassociated state.
2732 */
2733 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
2734 NULL, 0, (u32) -1, true, false);
2735 }
2736
2737 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2738 run_again(ifmgd, auth_data->timeout);
2739
2740 return 0;
2741}
2742
2743static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2744{
2745 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2746 struct ieee80211_local *local = sdata->local;
2747
2748 lockdep_assert_held(&sdata->u.mgd.mtx);
2749
Johannes Berg66e67e42012-01-20 13:55:27 +01002750 assoc_data->tries++;
2751 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
2752 printk(KERN_DEBUG "%s: association with %pM timed out\n",
2753 sdata->name, assoc_data->bss->bssid);
2754
2755 /*
2756 * Most likely AP is not in the range so remove the
2757 * bss struct for that AP.
2758 */
2759 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2760
2761 return -ETIMEDOUT;
2762 }
2763
2764 printk(KERN_DEBUG "%s: associate with %pM (try %d/%d)\n",
2765 sdata->name, assoc_data->bss->bssid, assoc_data->tries,
2766 IEEE80211_ASSOC_MAX_TRIES);
2767 ieee80211_send_assoc(sdata);
2768
2769 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2770 run_again(&sdata->u.mgd, assoc_data->timeout);
2771
2772 return 0;
2773}
2774
Johannes Berg1fa57d02010-06-10 10:21:32 +02002775void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002776{
Johannes Berg60f8b392008-09-08 17:44:22 +02002777 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002778 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002779
Johannes Berg77fdaa12009-07-07 03:45:17 +02002780 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002781
Johannes Berg66e67e42012-01-20 13:55:27 +01002782 if (ifmgd->auth_data &&
2783 time_after(jiffies, ifmgd->auth_data->timeout)) {
2784 if (ifmgd->auth_data->done) {
2785 /*
2786 * ok ... we waited for assoc but userspace didn't,
2787 * so let's just kill the auth data
2788 */
2789 ieee80211_destroy_auth_data(sdata, false);
2790 } else if (ieee80211_probe_auth(sdata)) {
2791 u8 bssid[ETH_ALEN];
2792
2793 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2794
2795 ieee80211_destroy_auth_data(sdata, false);
2796
2797 mutex_unlock(&ifmgd->mtx);
2798 cfg80211_send_auth_timeout(sdata->dev, bssid);
2799 mutex_lock(&ifmgd->mtx);
2800 }
2801 } else if (ifmgd->auth_data)
2802 run_again(ifmgd, ifmgd->auth_data->timeout);
2803
2804 if (ifmgd->assoc_data &&
2805 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2806 if (!ifmgd->assoc_data->have_beacon ||
2807 ieee80211_do_assoc(sdata)) {
2808 u8 bssid[ETH_ALEN];
2809
2810 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2811
2812 ieee80211_destroy_assoc_data(sdata, false);
2813
2814 mutex_unlock(&ifmgd->mtx);
2815 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2816 mutex_lock(&ifmgd->mtx);
2817 }
2818 } else if (ifmgd->assoc_data)
2819 run_again(ifmgd, ifmgd->assoc_data->timeout);
2820
Johannes Bergb291ba12009-07-10 15:29:03 +02002821 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2822 IEEE80211_STA_CONNECTION_POLL) &&
2823 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002824 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002825 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002826
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002827 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002828
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002829 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002830 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002831 else
Ben Greear180205b2011-02-04 15:30:24 -08002832 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002833
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002834 /* ACK received for nullfunc probing frame */
2835 if (!ifmgd->probe_send_count)
2836 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002837 else if (ifmgd->nullfunc_failed) {
2838 if (ifmgd->probe_send_count < max_tries) {
2839#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2840 wiphy_debug(local->hw.wiphy,
2841 "%s: No ack for nullfunc frame to"
Ben Greearbfc31df2011-01-14 09:32:18 -08002842 " AP %pM, try %d/%i\n",
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002843 sdata->name, bssid,
Ben Greearbfc31df2011-01-14 09:32:18 -08002844 ifmgd->probe_send_count, max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002845#endif
2846 ieee80211_mgd_probe_ap_send(sdata);
2847 } else {
2848#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2849 wiphy_debug(local->hw.wiphy,
2850 "%s: No ack for nullfunc frame to"
2851 " AP %pM, disconnecting.\n",
Felix Fietkauc658e5d2010-12-07 04:40:18 +01002852 sdata->name, bssid);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002853#endif
Johannes Berg95acac62011-07-12 12:30:59 +02002854 ieee80211_sta_connection_lost(sdata, bssid,
2855 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002856 }
2857 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002858 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002859 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2860#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2861 wiphy_debug(local->hw.wiphy,
2862 "%s: Failed to send nullfunc to AP %pM"
2863 " after %dms, disconnecting.\n",
2864 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002865 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002866#endif
Johannes Berg95acac62011-07-12 12:30:59 +02002867 ieee80211_sta_connection_lost(sdata, bssid,
2868 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002869 } else if (ifmgd->probe_send_count < max_tries) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002870#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Ben Greearb38afa82010-10-07 16:12:06 -07002871 wiphy_debug(local->hw.wiphy,
2872 "%s: No probe response from AP %pM"
Ben Greearbfc31df2011-01-14 09:32:18 -08002873 " after %dms, try %d/%i\n",
Ben Greearb38afa82010-10-07 16:12:06 -07002874 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002875 bssid, probe_wait_ms,
Ben Greearbfc31df2011-01-14 09:32:18 -08002876 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002877#endif
2878 ieee80211_mgd_probe_ap_send(sdata);
2879 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002880 /*
2881 * We actually lost the connection ... or did we?
2882 * Let's make sure!
2883 */
Ben Greearb38afa82010-10-07 16:12:06 -07002884 wiphy_debug(local->hw.wiphy,
2885 "%s: No probe response from AP %pM"
2886 " after %dms, disconnecting.\n",
2887 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002888 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002889
Johannes Berg95acac62011-07-12 12:30:59 +02002890 ieee80211_sta_connection_lost(sdata, bssid,
2891 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02002892 }
2893 }
2894
Johannes Berg77fdaa12009-07-07 03:45:17 +02002895 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002896
2897 mutex_lock(&local->mtx);
2898 ieee80211_recalc_idle(local);
2899 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002900}
Johannes Berg0a51b272008-09-08 17:44:25 +02002901
Johannes Bergb291ba12009-07-10 15:29:03 +02002902static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2903{
2904 struct ieee80211_sub_if_data *sdata =
2905 (struct ieee80211_sub_if_data *) data;
2906 struct ieee80211_local *local = sdata->local;
2907
2908 if (local->quiescing)
2909 return;
2910
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002911 ieee80211_queue_work(&sdata->local->hw,
2912 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002913}
2914
2915static void ieee80211_sta_conn_mon_timer(unsigned long data)
2916{
2917 struct ieee80211_sub_if_data *sdata =
2918 (struct ieee80211_sub_if_data *) data;
2919 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2920 struct ieee80211_local *local = sdata->local;
2921
2922 if (local->quiescing)
2923 return;
2924
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002925 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002926}
2927
2928static void ieee80211_sta_monitor_work(struct work_struct *work)
2929{
2930 struct ieee80211_sub_if_data *sdata =
2931 container_of(work, struct ieee80211_sub_if_data,
2932 u.mgd.monitor_work);
2933
2934 ieee80211_mgd_probe_ap(sdata, false);
2935}
2936
Johannes Berga1678f82008-09-11 00:01:47 +02002937static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2938{
Eliad Peller494f1fe2012-02-19 15:26:09 +02002939 u32 flags;
2940
Kalle Vaload935682009-04-19 08:47:19 +03002941 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Bergb291ba12009-07-10 15:29:03 +02002942 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2943 IEEE80211_STA_CONNECTION_POLL);
Kalle Vaload935682009-04-19 08:47:19 +03002944
Johannes Bergb291ba12009-07-10 15:29:03 +02002945 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02002946 flags = sdata->local->hw.flags;
2947 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
2948 ieee80211_queue_work(&sdata->local->hw,
2949 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002950 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02002951 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03002952 }
Johannes Berga1678f82008-09-11 00:01:47 +02002953}
2954
Johannes Berg5bb644a2009-05-17 11:40:42 +02002955#ifdef CONFIG_PM
2956void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2957{
2958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2959
2960 /*
2961 * we need to use atomic bitops for the running bits
2962 * only because both timers might fire at the same
2963 * time -- the code here is properly synchronised.
2964 */
2965
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002966 cancel_work_sync(&ifmgd->request_smps_work);
2967
Johannes Berg0ecfe802011-11-09 12:14:16 +01002968 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002969 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002970 if (del_timer_sync(&ifmgd->timer))
2971 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2972
2973 cancel_work_sync(&ifmgd->chswitch_work);
2974 if (del_timer_sync(&ifmgd->chswitch_timer))
2975 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002976
Johannes Bergb291ba12009-07-10 15:29:03 +02002977 /* these will just be re-established on connection */
2978 del_timer_sync(&ifmgd->conn_mon_timer);
2979 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002980}
2981
2982void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2983{
2984 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2985
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05302986 if (!ifmgd->associated)
2987 return;
2988
Johannes Berg95acac62011-07-12 12:30:59 +02002989 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
2990 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
2991 mutex_lock(&ifmgd->mtx);
2992 if (ifmgd->associated) {
2993#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2994 wiphy_debug(sdata->local->hw.wiphy,
2995 "%s: driver requested disconnect after resume.\n",
2996 sdata->name);
2997#endif
2998 ieee80211_sta_connection_lost(sdata,
2999 ifmgd->associated->bssid,
3000 WLAN_REASON_UNSPECIFIED);
3001 mutex_unlock(&ifmgd->mtx);
3002 return;
3003 }
3004 mutex_unlock(&ifmgd->mtx);
3005 }
3006
Johannes Berg5bb644a2009-05-17 11:40:42 +02003007 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3008 add_timer(&ifmgd->timer);
3009 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3010 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003011 ieee80211_sta_reset_beacon_monitor(sdata);
Felix Fietkau46090972010-11-23 20:28:03 +01003012 ieee80211_restart_sta_timer(sdata);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003013}
3014#endif
3015
Johannes Berg9c6bd792008-09-11 00:01:52 +02003016/* interface setup */
3017void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3018{
Johannes Berg46900292009-02-15 12:44:28 +01003019 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003020
Johannes Berg46900292009-02-15 12:44:28 +01003021 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003022 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003023 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003024 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3025 ieee80211_beacon_connection_loss_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003026 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003027 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003028 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003029 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3030 (unsigned long) sdata);
3031 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3032 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003033 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303034 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003035
Johannes Bergab1faea2009-07-01 21:41:17 +02003036 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303037 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003038 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3039 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003040
Johannes Berg77fdaa12009-07-07 03:45:17 +02003041 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003042
3043 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3044 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3045 else
3046 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003047}
3048
3049/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003050void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3051{
3052 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003053
3054 /* Restart STA timers */
3055 rcu_read_lock();
3056 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3057 ieee80211_restart_sta_timer(sdata);
3058 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003059}
Johannes Berg10f644a2009-04-16 13:17:25 +02003060
3061int ieee80211_max_network_latency(struct notifier_block *nb,
3062 unsigned long data, void *dummy)
3063{
3064 s32 latency_usec = (s32) data;
3065 struct ieee80211_local *local =
3066 container_of(nb, struct ieee80211_local,
3067 network_latency_notifier);
3068
3069 mutex_lock(&local->iflist_mtx);
3070 ieee80211_recalc_ps(local, latency_usec);
3071 mutex_unlock(&local->iflist_mtx);
3072
3073 return 0;
3074}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003075
Johannes Berga1cf7752012-03-08 15:02:07 +01003076static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003077 struct cfg80211_bss *cbss, bool assoc)
Johannes Berga1cf7752012-03-08 15:02:07 +01003078{
3079 struct ieee80211_local *local = sdata->local;
3080 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003081 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berga1cf7752012-03-08 15:02:07 +01003082 struct sta_info *sta;
3083 bool have_sta = false;
3084 int err;
3085
3086 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3087 return -EINVAL;
3088
3089 if (assoc) {
3090 rcu_read_lock();
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003091 have_sta = sta_info_get(sdata, cbss->bssid);
Johannes Berga1cf7752012-03-08 15:02:07 +01003092 rcu_read_unlock();
3093 }
3094
3095 if (!have_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003096 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
Johannes Berga1cf7752012-03-08 15:02:07 +01003097 if (!sta)
3098 return -ENOMEM;
3099 }
3100
3101 mutex_lock(&local->mtx);
3102 ieee80211_recalc_idle(sdata->local);
3103 mutex_unlock(&local->mtx);
3104
3105 /* switch to the right channel */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003106 local->oper_channel = cbss->channel;
Johannes Berga1cf7752012-03-08 15:02:07 +01003107 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
3108
3109 if (!have_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003110 struct ieee80211_supported_band *sband;
3111 u32 rates = 0, basic_rates = 0;
3112 bool have_higher_than_11mbit;
3113 int min_rate = INT_MAX, min_rate_index = -1;
3114
3115 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
3116
3117 ieee80211_get_rates(sband, bss->supp_rates,
3118 bss->supp_rates_len,
3119 &rates, &basic_rates,
3120 &have_higher_than_11mbit,
3121 &min_rate, &min_rate_index);
3122
3123 /*
3124 * This used to be a workaround for basic rates missing
3125 * in the association response frame. Now that we no
3126 * longer use the basic rates from there, it probably
3127 * doesn't happen any more, but keep the workaround so
3128 * in case some *other* APs are buggy in different ways
3129 * we can connect -- with a warning.
3130 */
3131 if (!basic_rates && min_rate_index >= 0) {
3132 printk(KERN_DEBUG
3133 "%s: No basic rates, using min rate instead.\n",
3134 sdata->name);
3135 basic_rates = BIT(min_rate_index);
3136 }
3137
3138 sta->sta.supp_rates[cbss->channel->band] = rates;
3139 sdata->vif.bss_conf.basic_rates = basic_rates;
3140
3141 /* cf. IEEE 802.11 9.2.12 */
3142 if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
3143 have_higher_than_11mbit)
3144 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3145 else
3146 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3147
3148 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3149
3150 /* tell driver about BSSID and basic rates */
3151 ieee80211_bss_info_change_notify(sdata,
3152 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES);
Johannes Berga1cf7752012-03-08 15:02:07 +01003153
3154 if (assoc)
3155 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
3156
3157 err = sta_info_insert(sta);
3158 sta = NULL;
3159 if (err) {
3160 printk(KERN_DEBUG
3161 "%s: failed to insert STA entry for the AP (error %d)\n",
3162 sdata->name, err);
3163 return err;
3164 }
3165 } else
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003166 WARN_ON_ONCE(compare_ether_addr(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003167
3168 return 0;
3169}
3170
Johannes Berg77fdaa12009-07-07 03:45:17 +02003171/* config hooks */
3172int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3173 struct cfg80211_auth_request *req)
3174{
Johannes Berg66e67e42012-01-20 13:55:27 +01003175 struct ieee80211_local *local = sdata->local;
3176 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3177 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003178 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003179 int err;
3180
3181 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003182
3183 switch (req->auth_type) {
3184 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3185 auth_alg = WLAN_AUTH_OPEN;
3186 break;
3187 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003188 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003189 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003190 auth_alg = WLAN_AUTH_SHARED_KEY;
3191 break;
3192 case NL80211_AUTHTYPE_FT:
3193 auth_alg = WLAN_AUTH_FT;
3194 break;
3195 case NL80211_AUTHTYPE_NETWORK_EAP:
3196 auth_alg = WLAN_AUTH_LEAP;
3197 break;
3198 default:
3199 return -EOPNOTSUPP;
3200 }
3201
Johannes Berg66e67e42012-01-20 13:55:27 +01003202 auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
3203 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003204 return -ENOMEM;
3205
Johannes Berg66e67e42012-01-20 13:55:27 +01003206 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003207
3208 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003209 memcpy(auth_data->ie, req->ie, req->ie_len);
3210 auth_data->ie_len = req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003211 }
3212
Johannes Bergfffd0932009-07-08 14:22:54 +02003213 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003214 auth_data->key_len = req->key_len;
3215 auth_data->key_idx = req->key_idx;
3216 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003217 }
3218
Johannes Berg66e67e42012-01-20 13:55:27 +01003219 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003220
Johannes Berg66e67e42012-01-20 13:55:27 +01003221 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003222
Johannes Berg66e67e42012-01-20 13:55:27 +01003223 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003224
Johannes Berg66e67e42012-01-20 13:55:27 +01003225 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3226 ifmgd->assoc_data) {
3227 err = -EBUSY;
3228 goto err_free;
3229 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003230
Johannes Berg66e67e42012-01-20 13:55:27 +01003231 if (ifmgd->auth_data)
3232 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003233
Johannes Berg66e67e42012-01-20 13:55:27 +01003234 /* prep auth_data so we don't go into idle on disassoc */
3235 ifmgd->auth_data = auth_data;
3236
3237 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003238 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003239
3240 printk(KERN_DEBUG "%s: authenticate with %pM\n",
3241 sdata->name, req->bss->bssid);
3242
Johannes Berga1cf7752012-03-08 15:02:07 +01003243 err = ieee80211_prep_connection(sdata, req->bss, false);
3244 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003245 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003246
Johannes Berg66e67e42012-01-20 13:55:27 +01003247 err = ieee80211_probe_auth(sdata);
3248 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003249 sta_info_destroy_addr(sdata, req->bss->bssid);
3250 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003251 }
3252
Johannes Berg66e67e42012-01-20 13:55:27 +01003253 /* hold our own reference */
3254 cfg80211_ref_bss(auth_data->bss);
3255 err = 0;
3256 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003257
Johannes Berg66e67e42012-01-20 13:55:27 +01003258 err_clear:
3259 ifmgd->auth_data = NULL;
3260 err_free:
3261 kfree(auth_data);
3262 out_unlock:
3263 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003264
Johannes Berg66e67e42012-01-20 13:55:27 +01003265 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003266}
3267
Johannes Berg77fdaa12009-07-07 03:45:17 +02003268int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3269 struct cfg80211_assoc_request *req)
3270{
Johannes Berg66e67e42012-01-20 13:55:27 +01003271 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003272 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003273 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003274 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003275 struct ieee80211_supported_band *sband;
Johannes Berg66e67e42012-01-20 13:55:27 +01003276 const u8 *ssidie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003277 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003278
Johannes Berg66e67e42012-01-20 13:55:27 +01003279 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3280 if (!ssidie)
3281 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003282
Johannes Berg66e67e42012-01-20 13:55:27 +01003283 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3284 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003285 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003286
Johannes Berg66e67e42012-01-20 13:55:27 +01003287 mutex_lock(&ifmgd->mtx);
3288
3289 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003290 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003291
3292 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3293 err = -EBUSY;
3294 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003295 }
3296
Johannes Berg66e67e42012-01-20 13:55:27 +01003297 if (ifmgd->assoc_data) {
3298 err = -EBUSY;
3299 goto err_free;
3300 }
3301
3302 if (ifmgd->auth_data) {
3303 bool match;
3304
3305 /* keep sta info, bssid if matching */
Felix Fietkau888d04d2012-03-01 15:22:09 +01003306 match = compare_ether_addr(ifmgd->bssid, req->bss->bssid) == 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003307 ieee80211_destroy_auth_data(sdata, match);
3308 }
3309
3310 /* prepare assoc data */
3311
Johannes Berg77fdaa12009-07-07 03:45:17 +02003312 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
Vivek Natarajan375177b2010-02-09 14:50:28 +05303313 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303314 ifmgd->flags &= ~IEEE80211_STA_DISABLE_VHT;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003315
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003316 ifmgd->beacon_crc_valid = false;
3317
Johannes Bergde5036a2012-03-08 15:02:03 +01003318 /*
3319 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3320 * We still associate in non-HT mode (11a/b/g) if any one of these
3321 * ciphers is configured as pairwise.
3322 * We can set this to true for non-11n hardware, that'll be checked
3323 * separately along with the peer capabilities.
3324 */
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303325 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003326 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3327 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303328 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003329 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303330 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3331 netdev_info(sdata->dev,
3332 "disabling HT/VHT due to WEP/TKIP use\n");
3333 }
3334 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003335
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303336 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Ben Greearef96a8422011-11-18 11:32:00 -08003337 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303338 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3339 }
Ben Greearef96a8422011-11-18 11:32:00 -08003340
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003341 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3342 sband = local->hw.wiphy->bands[req->bss->channel->band];
3343 if (!sband->ht_cap.ht_supported ||
3344 local->hw.queues < 4 || !bss->wmm_used)
3345 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3346
Mahesh Palivelad488b6a2013-02-21 07:04:14 +05303347 /* disable VHT if we don't support it or the AP doesn't use WMM */
3348 if (!sband->vht_cap.vht_supported ||
3349 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3350 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3351 netdev_info(sdata->dev,
3352 "disabling VHT as WMM/QoS is not supported\n");
3353 }
3354
Ben Greearef96a8422011-11-18 11:32:00 -08003355 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3356 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3357 sizeof(ifmgd->ht_capa_mask));
3358
Johannes Berg77fdaa12009-07-07 03:45:17 +02003359 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003360 memcpy(assoc_data->ie, req->ie, req->ie_len);
3361 assoc_data->ie_len = req->ie_len;
3362 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003363
Johannes Berg66e67e42012-01-20 13:55:27 +01003364 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003365
Johannes Bergaf6b6372009-12-23 13:15:35 +01003366 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3367 if (ifmgd->powersave)
3368 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
3369 else
3370 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
3371 } else
3372 ifmgd->ap_smps = ifmgd->req_smps;
3373
Johannes Berg66e67e42012-01-20 13:55:27 +01003374 assoc_data->capability = req->bss->capability;
Johannes Berg76f03032012-03-08 15:02:05 +01003375 assoc_data->wmm = bss->wmm_used && (local->hw.queues >= 4);
Johannes Berg66e67e42012-01-20 13:55:27 +01003376 assoc_data->supp_rates = bss->supp_rates;
3377 assoc_data->supp_rates_len = bss->supp_rates_len;
3378 assoc_data->ht_information_ie =
Johannes Bergf679f652009-12-23 13:15:34 +01003379 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
Johannes Berg63f170e2009-12-23 13:15:33 +01003380
Kalle Valoab133152010-01-12 10:42:31 +02003381 if (bss->wmm_used && bss->uapsd_supported &&
3382 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003383 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003384 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3385 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003386 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003387 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3388 }
3389
Johannes Berg66e67e42012-01-20 13:55:27 +01003390 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3391 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003392
Johannes Berg77fdaa12009-07-07 03:45:17 +02003393 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003394 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003395
3396 if (req->use_mfp) {
3397 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3398 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3399 } else {
3400 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3401 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3402 }
3403
3404 if (req->crypto.control_port)
3405 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3406 else
3407 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3408
Johannes Berga621fa42010-08-27 14:26:54 +03003409 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3410 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3411
Johannes Berg66e67e42012-01-20 13:55:27 +01003412 /* kick off associate process */
3413
3414 ifmgd->assoc_data = assoc_data;
3415
Johannes Berga1cf7752012-03-08 15:02:07 +01003416 err = ieee80211_prep_connection(sdata, req->bss, true);
3417 if (err)
3418 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003419
3420 if (!bss->dtim_period &&
3421 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3422 /*
3423 * Wait up to one beacon interval ...
3424 * should this be more if we miss one?
3425 */
3426 printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
3427 sdata->name, ifmgd->bssid);
Johannes Berg3f9768a2012-03-28 21:02:46 +02003428 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +01003429 } else {
3430 assoc_data->have_beacon = true;
3431 assoc_data->sent_assoc = false;
3432 assoc_data->timeout = jiffies;
3433 }
3434 run_again(ifmgd, assoc_data->timeout);
3435
Paul Stewartfcff4f12012-02-23 17:59:53 -08003436 if (bss->corrupt_data) {
3437 char *corrupt_type = "data";
3438 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3439 if (bss->corrupt_data &
3440 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3441 corrupt_type = "beacon and probe response";
3442 else
3443 corrupt_type = "beacon";
3444 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3445 corrupt_type = "probe response";
3446 printk(KERN_DEBUG "%s: associating with AP with corrupt %s\n",
3447 sdata->name, corrupt_type);
3448 }
3449
Johannes Berg66e67e42012-01-20 13:55:27 +01003450 err = 0;
3451 goto out;
3452 err_clear:
3453 ifmgd->assoc_data = NULL;
3454 err_free:
3455 kfree(assoc_data);
3456 out:
3457 mutex_unlock(&ifmgd->mtx);
3458
3459 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003460}
3461
3462int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003463 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003464{
3465 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5fef7db2012-02-24 13:50:52 +01003466 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003467
Johannes Berg77fdaa12009-07-07 03:45:17 +02003468 mutex_lock(&ifmgd->mtx);
3469
Johannes Berg37ad3882012-02-24 13:50:54 +01003470 if (ifmgd->auth_data) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003471 ieee80211_destroy_auth_data(sdata, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003472 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003473 return 0;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003474 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003475
Johannes Berg37ad3882012-02-24 13:50:54 +01003476 printk(KERN_DEBUG
3477 "%s: deauthenticating from %pM by local choice (reason=%d)\n",
Johannes Berg95de8172012-01-20 13:55:25 +01003478 sdata->name, req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003479
Johannes Berg37ad3882012-02-24 13:50:54 +01003480 if (ifmgd->associated &&
Felix Fietkau888d04d2012-03-01 15:22:09 +01003481 compare_ether_addr(ifmgd->associated->bssid, req->bssid) == 0)
Johannes Berg37ad3882012-02-24 13:50:54 +01003482 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
Johannes Berg5fef7db2012-02-24 13:50:52 +01003483 req->reason_code, true, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01003484 else
3485 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3486 IEEE80211_STYPE_DEAUTH,
3487 req->reason_code, true,
3488 frame_buf);
3489 mutex_unlock(&ifmgd->mtx);
3490
Johannes Berg5fef7db2012-02-24 13:50:52 +01003491 __cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003492
Johannes Berg7da7cc12010-08-05 17:02:38 +02003493 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003494 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003495 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003496
Johannes Berg77fdaa12009-07-07 03:45:17 +02003497 return 0;
3498}
3499
3500int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003501 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003502{
3503 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003504 u8 bssid[ETH_ALEN];
Johannes Berg5fef7db2012-02-24 13:50:52 +01003505 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003506
Johannes Berg77fdaa12009-07-07 03:45:17 +02003507 mutex_lock(&ifmgd->mtx);
3508
Johannes Berg8d8b2612009-07-25 11:58:36 +02003509 /*
3510 * cfg80211 should catch this ... but it's racy since
3511 * we can receive a disassoc frame, process it, hand it
3512 * to cfg80211 while that's in a locked section already
3513 * trying to tell us that the user wants to disconnect.
3514 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003515 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003516 mutex_unlock(&ifmgd->mtx);
3517 return -ENOLINK;
3518 }
3519
Johannes Berg0ff71612009-09-26 14:45:41 +02003520 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +01003521 sdata->name, req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003522
Jouni Malinene69e95d2010-03-29 23:29:31 -07003523 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01003524 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
3525 req->reason_code, !req->local_state_change,
3526 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003527 mutex_unlock(&ifmgd->mtx);
3528
Johannes Berg5fef7db2012-02-24 13:50:52 +01003529 __cfg80211_send_disassoc(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01003530
Johannes Berg7da7cc12010-08-05 17:02:38 +02003531 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003532 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003533 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003534
Johannes Berg77fdaa12009-07-07 03:45:17 +02003535 return 0;
3536}
Jouni Malinen026331c2010-02-15 12:53:10 +02003537
Eliad Pellerafa762f2012-04-23 14:45:15 +03003538void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01003539{
3540 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3541
3542 mutex_lock(&ifmgd->mtx);
3543 if (ifmgd->assoc_data)
3544 ieee80211_destroy_assoc_data(sdata, false);
3545 if (ifmgd->auth_data)
3546 ieee80211_destroy_auth_data(sdata, false);
3547 del_timer_sync(&ifmgd->timer);
3548 mutex_unlock(&ifmgd->mtx);
3549}
3550
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003551void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3552 enum nl80211_cqm_rssi_threshold_event rssi_event,
3553 gfp_t gfp)
3554{
3555 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3556
Johannes Bergb5878a22010-04-07 16:48:40 +02003557 trace_api_cqm_rssi_notify(sdata, rssi_event);
3558
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003559 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3560}
3561EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
Eliad Peller1d34d102011-06-06 12:59:29 +03003562
3563unsigned char ieee80211_get_operstate(struct ieee80211_vif *vif)
3564{
3565 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3566 return sdata->dev->operstate;
3567}
3568EXPORT_SYMBOL(ieee80211_get_operstate);