blob: 0ca34137a3b5e1cddf2ab6a70fd3f616276aed8e [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 Berg77fdaa12009-07-07 03:45:17 +020091/*
92 * All cfg80211 functions have to be called outside a locked
93 * section so that they can acquire a lock themselves... This
94 * is much simpler than queuing up things in cfg80211, but we
95 * do need some indirection for that here.
96 */
97enum rx_mgmt_action {
98 /* no action required */
99 RX_MGMT_NONE,
100
Johannes Berg77fdaa12009-07-07 03:45:17 +0200101 /* caller must call cfg80211_send_deauth() */
102 RX_MGMT_CFG80211_DEAUTH,
103
104 /* caller must call cfg80211_send_disassoc() */
105 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100106
107 /* caller must call cfg80211_send_rx_auth() */
108 RX_MGMT_CFG80211_RX_AUTH,
109
110 /* caller must call cfg80211_send_rx_assoc() */
111 RX_MGMT_CFG80211_RX_ASSOC,
112
113 /* caller must call cfg80211_send_assoc_timeout() */
114 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200115};
116
Johannes Berg5484e232008-09-08 17:44:27 +0200117/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200118static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
119{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200120 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200121}
122
Johannes Bergca386f32009-07-10 02:39:48 +0200123/*
124 * We can have multiple work items (and connection probing)
125 * scheduling this timer, but we need to take care to only
126 * reschedule it when it should fire _earlier_ than it was
127 * asked for before, or if it's not pending right now. This
128 * function ensures that. Note that it then is required to
129 * run this function for all timeouts after the first one
130 * has happened -- the work that runs from this timer will
131 * do that.
132 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100133static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200134{
135 ASSERT_MGD_MTX(ifmgd);
136
137 if (!timer_pending(&ifmgd->timer) ||
138 time_before(timeout, ifmgd->timer.expires))
139 mod_timer(&ifmgd->timer, timeout);
140}
141
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400142void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200143{
Johannes Bergc1288b12012-01-19 09:29:57 +0100144 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145 return;
146
Johannes Berg7eeff742012-07-18 10:27:27 +0200147 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
148 return;
149
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100151 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200152}
153
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400154void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
155{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100158 if (unlikely(!sdata->u.mgd.associated))
159 return;
160
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400161 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
162 return;
163
164 mod_timer(&sdata->u.mgd.conn_mon_timer,
165 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400166
167 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400168}
169
Johannes Berg5484e232008-09-08 17:44:27 +0200170static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200171{
Johannes Berg5484e232008-09-08 17:44:27 +0200172 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200173}
174
Johannes Berg24398e32012-03-28 10:58:36 +0200175static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
176 struct ieee80211_ht_operation *ht_oper,
177 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200178{
179 struct ieee80211_local *local = sdata->local;
180 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200181 struct sta_info *sta;
182 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200183 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200184 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200185
Johannes Berg568d6e22012-07-23 14:29:21 +0200186 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200187
Johannes Berg64f68e52012-03-28 10:58:37 +0200188 switch (sdata->vif.bss_conf.channel_type) {
189 case NL80211_CHAN_HT40PLUS:
Johannes Berg568d6e22012-07-23 14:29:21 +0200190 if (local->oper_channel->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200191 disable_40 = true;
192 break;
193 case NL80211_CHAN_HT40MINUS:
Johannes Berg568d6e22012-07-23 14:29:21 +0200194 if (local->oper_channel->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200195 disable_40 = true;
196 break;
197 default:
198 break;
199 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200200
Johannes Berg24398e32012-03-28 10:58:36 +0200201 /* This can change during the lifetime of the BSS */
202 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200203 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200204
Johannes Berg64f68e52012-03-28 10:58:37 +0200205 mutex_lock(&local->sta_mtx);
206 sta = sta_info_get(sdata, bssid);
207
208 WARN_ON_ONCE(!sta);
209
210 if (sta && !sta->supports_40mhz)
211 disable_40 = true;
212
213 if (sta && (!reconfig ||
Rajkumar Manoharan91a00992012-04-25 20:24:24 +0530214 (disable_40 != !(sta->sta.ht_cap.cap &
Johannes Berg64f68e52012-03-28 10:58:37 +0200215 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530216
Johannes Berg64f68e52012-03-28 10:58:37 +0200217 if (disable_40)
218 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
219 else
220 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530221
Johannes Berg64f68e52012-03-28 10:58:37 +0200222 rate_control_rate_update(local, sband, sta,
Johannes Berg8f727ef2012-03-30 08:43:32 +0200223 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200224 }
Johannes Berg64f68e52012-03-28 10:58:37 +0200225 mutex_unlock(&local->sta_mtx);
Johannes Bergd5522e02009-03-30 13:23:35 +0200226
Johannes Berg074d46d2012-03-15 19:45:16 +0100227 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200228
229 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200230 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200231 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200232 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200233 }
234
235 return changed;
236}
237
Johannes Berg9c6bd792008-09-11 00:01:52 +0200238/* frame sending functions */
239
Johannes Berg66e67e42012-01-20 13:55:27 +0100240static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
241 struct ieee80211_supported_band *sband,
242 u32 *rates)
243{
244 int i, j, count;
245 *rates = 0;
246 count = 0;
247 for (i = 0; i < supp_rates_len; i++) {
248 int rate = (supp_rates[i] & 0x7F) * 5;
249
250 for (j = 0; j < sband->n_bitrates; j++)
251 if (sband->bitrates[j].bitrate == rate) {
252 *rates |= BIT(j);
253 count++;
254 break;
255 }
256 }
257
258 return count;
259}
260
261static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200262 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100263 struct ieee80211_supported_band *sband,
264 struct ieee80211_channel *channel,
265 enum ieee80211_smps_mode smps)
266{
Johannes Berg66e67e42012-01-20 13:55:27 +0100267 u8 *pos;
268 u32 flags = channel->flags;
269 u16 cap;
270 struct ieee80211_sta_ht_cap ht_cap;
271
272 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
273
Johannes Berg66e67e42012-01-20 13:55:27 +0100274 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
275 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
276
Johannes Berg66e67e42012-01-20 13:55:27 +0100277 /* determine capability flags */
278 cap = ht_cap.cap;
279
Johannes Berg9dde6422012-05-16 23:43:19 +0200280 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100281 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
282 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
283 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
284 cap &= ~IEEE80211_HT_CAP_SGI_40;
285 }
286 break;
287 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
288 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
289 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
290 cap &= ~IEEE80211_HT_CAP_SGI_40;
291 }
292 break;
293 }
294
Johannes Berg24398e32012-03-28 10:58:36 +0200295 /*
296 * If 40 MHz was disabled associate as though we weren't
297 * capable of 40 MHz -- some broken APs will never fall
298 * back to trying to transmit in 20 MHz.
299 */
300 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
301 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
302 cap &= ~IEEE80211_HT_CAP_SGI_40;
303 }
304
Johannes Berg66e67e42012-01-20 13:55:27 +0100305 /* set SM PS mode properly */
306 cap &= ~IEEE80211_HT_CAP_SM_PS;
307 switch (smps) {
308 case IEEE80211_SMPS_AUTOMATIC:
309 case IEEE80211_SMPS_NUM_MODES:
310 WARN_ON(1);
311 case IEEE80211_SMPS_OFF:
312 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
313 IEEE80211_HT_CAP_SM_PS_SHIFT;
314 break;
315 case IEEE80211_SMPS_STATIC:
316 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
317 IEEE80211_HT_CAP_SM_PS_SHIFT;
318 break;
319 case IEEE80211_SMPS_DYNAMIC:
320 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
321 IEEE80211_HT_CAP_SM_PS_SHIFT;
322 break;
323 }
324
325 /* reserve and fill IE */
326 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
327 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
328}
329
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000330static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
331 struct sk_buff *skb,
332 struct ieee80211_supported_band *sband)
333{
334 u8 *pos;
335 u32 cap;
336 struct ieee80211_sta_vht_cap vht_cap;
337
338 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
339
340 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
341
342 /* determine capability flags */
343 cap = vht_cap.cap;
344
345 /* reserve and fill IE */
346 pos = skb_put(skb, sizeof(struct ieee80211_vht_capabilities) + 2);
347 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
348}
349
Johannes Berg66e67e42012-01-20 13:55:27 +0100350static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
351{
352 struct ieee80211_local *local = sdata->local;
353 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
354 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
355 struct sk_buff *skb;
356 struct ieee80211_mgmt *mgmt;
357 u8 *pos, qos_info;
358 size_t offset = 0, noffset;
359 int i, count, rates_len, supp_rates_len;
360 u16 capab;
361 struct ieee80211_supported_band *sband;
362 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100363
364 lockdep_assert_held(&ifmgd->mtx);
365
366 sband = local->hw.wiphy->bands[local->oper_channel->band];
367
368 if (assoc_data->supp_rates_len) {
369 /*
370 * Get all rates supported by the device and the AP as
371 * some APs don't like getting a superset of their rates
372 * in the association request (e.g. D-Link DAP 1353 in
373 * b-only mode)...
374 */
375 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
376 assoc_data->supp_rates_len,
377 sband, &rates);
378 } else {
379 /*
380 * In case AP not provide any supported rates information
381 * before association, we send information element(s) with
382 * all rates that we support.
383 */
384 rates = ~0;
385 rates_len = sband->n_bitrates;
386 }
387
388 skb = alloc_skb(local->hw.extra_tx_headroom +
389 sizeof(*mgmt) + /* bit too much but doesn't matter */
390 2 + assoc_data->ssid_len + /* SSID */
391 4 + rates_len + /* (extended) rates */
392 4 + /* power capability */
393 2 + 2 * sband->n_channels + /* supported channels */
394 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000395 2 + sizeof(struct ieee80211_vht_capabilities) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100396 assoc_data->ie_len + /* extra IEs */
397 9, /* WMM */
398 GFP_KERNEL);
399 if (!skb)
400 return;
401
402 skb_reserve(skb, local->hw.extra_tx_headroom);
403
404 capab = WLAN_CAPABILITY_ESS;
405
406 if (sband->band == IEEE80211_BAND_2GHZ) {
407 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
408 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
409 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
410 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
411 }
412
413 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
414 capab |= WLAN_CAPABILITY_PRIVACY;
415
416 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
417 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
418 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
419
420 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
421 memset(mgmt, 0, 24);
422 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
423 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
424 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
425
426 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
427 skb_put(skb, 10);
428 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
429 IEEE80211_STYPE_REASSOC_REQ);
430 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
431 mgmt->u.reassoc_req.listen_interval =
432 cpu_to_le16(local->hw.conf.listen_interval);
433 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
434 ETH_ALEN);
435 } else {
436 skb_put(skb, 4);
437 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
438 IEEE80211_STYPE_ASSOC_REQ);
439 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
440 mgmt->u.assoc_req.listen_interval =
441 cpu_to_le16(local->hw.conf.listen_interval);
442 }
443
444 /* SSID */
445 pos = skb_put(skb, 2 + assoc_data->ssid_len);
446 *pos++ = WLAN_EID_SSID;
447 *pos++ = assoc_data->ssid_len;
448 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
449
450 /* add all rates which were marked to be used above */
451 supp_rates_len = rates_len;
452 if (supp_rates_len > 8)
453 supp_rates_len = 8;
454
455 pos = skb_put(skb, supp_rates_len + 2);
456 *pos++ = WLAN_EID_SUPP_RATES;
457 *pos++ = supp_rates_len;
458
459 count = 0;
460 for (i = 0; i < sband->n_bitrates; i++) {
461 if (BIT(i) & rates) {
462 int rate = sband->bitrates[i].bitrate;
463 *pos++ = (u8) (rate / 5);
464 if (++count == 8)
465 break;
466 }
467 }
468
469 if (rates_len > count) {
470 pos = skb_put(skb, rates_len - count + 2);
471 *pos++ = WLAN_EID_EXT_SUPP_RATES;
472 *pos++ = rates_len - count;
473
474 for (i++; i < sband->n_bitrates; i++) {
475 if (BIT(i) & rates) {
476 int rate = sband->bitrates[i].bitrate;
477 *pos++ = (u8) (rate / 5);
478 }
479 }
480 }
481
482 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
483 /* 1. power capabilities */
484 pos = skb_put(skb, 4);
485 *pos++ = WLAN_EID_PWR_CAPABILITY;
486 *pos++ = 2;
487 *pos++ = 0; /* min tx power */
488 *pos++ = local->oper_channel->max_power; /* max tx power */
489
490 /* 2. supported channels */
491 /* TODO: get this in reg domain format */
492 pos = skb_put(skb, 2 * sband->n_channels + 2);
493 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
494 *pos++ = 2 * sband->n_channels;
495 for (i = 0; i < sband->n_channels; i++) {
496 *pos++ = ieee80211_frequency_to_channel(
497 sband->channels[i].center_freq);
498 *pos++ = 1; /* one channel in the subband*/
499 }
500 }
501
502 /* if present, add any custom IEs that go before HT */
503 if (assoc_data->ie_len && assoc_data->ie) {
504 static const u8 before_ht[] = {
505 WLAN_EID_SSID,
506 WLAN_EID_SUPP_RATES,
507 WLAN_EID_EXT_SUPP_RATES,
508 WLAN_EID_PWR_CAPABILITY,
509 WLAN_EID_SUPPORTED_CHANNELS,
510 WLAN_EID_RSN,
511 WLAN_EID_QOS_CAPA,
512 WLAN_EID_RRM_ENABLED_CAPABILITIES,
513 WLAN_EID_MOBILITY_DOMAIN,
514 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
515 };
516 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
517 before_ht, ARRAY_SIZE(before_ht),
518 offset);
519 pos = skb_put(skb, noffset - offset);
520 memcpy(pos, assoc_data->ie + offset, noffset - offset);
521 offset = noffset;
522 }
523
Johannes Berg4e74bfd2012-03-08 15:02:04 +0100524 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg9dde6422012-05-16 23:43:19 +0200525 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100526 sband, local->oper_channel, ifmgd->ap_smps);
527
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000528 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
529 ieee80211_add_vht_ie(sdata, skb, sband);
530
Johannes Berg66e67e42012-01-20 13:55:27 +0100531 /* if present, add any custom non-vendor IEs that go after HT */
532 if (assoc_data->ie_len && assoc_data->ie) {
533 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
534 assoc_data->ie_len,
535 offset);
536 pos = skb_put(skb, noffset - offset);
537 memcpy(pos, assoc_data->ie + offset, noffset - offset);
538 offset = noffset;
539 }
540
Johannes Berg76f03032012-03-08 15:02:05 +0100541 if (assoc_data->wmm) {
542 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200543 qos_info = ifmgd->uapsd_queues;
544 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100545 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
546 } else {
547 qos_info = 0;
548 }
549
550 pos = skb_put(skb, 9);
551 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
552 *pos++ = 7; /* len */
553 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
554 *pos++ = 0x50;
555 *pos++ = 0xf2;
556 *pos++ = 2; /* WME */
557 *pos++ = 0; /* WME info */
558 *pos++ = 1; /* WME ver */
559 *pos++ = qos_info;
560 }
561
562 /* add any remaining custom (i.e. vendor specific here) IEs */
563 if (assoc_data->ie_len && assoc_data->ie) {
564 noffset = assoc_data->ie_len;
565 pos = skb_put(skb, noffset - offset);
566 memcpy(pos, assoc_data->ie + offset, noffset - offset);
567 }
568
Johannes Berga1845fc2012-06-27 13:18:36 +0200569 drv_mgd_prepare_tx(local, sdata);
570
Johannes Berg66e67e42012-01-20 13:55:27 +0100571 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
572 ieee80211_tx_skb(sdata, skb);
573}
574
Kalle Valo572e0012009-02-10 17:09:31 +0200575void ieee80211_send_pspoll(struct ieee80211_local *local,
576 struct ieee80211_sub_if_data *sdata)
577{
Kalle Valo572e0012009-02-10 17:09:31 +0200578 struct ieee80211_pspoll *pspoll;
579 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200580
Kalle Valod8cd1892010-01-05 20:16:26 +0200581 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
582 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200583 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200584
Kalle Valod8cd1892010-01-05 20:16:26 +0200585 pspoll = (struct ieee80211_pspoll *) skb->data;
586 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200587
Johannes Berg62ae67b2009-11-18 18:42:05 +0100588 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
589 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200590}
591
Johannes Berg965beda2009-04-16 13:17:24 +0200592void ieee80211_send_nullfunc(struct ieee80211_local *local,
593 struct ieee80211_sub_if_data *sdata,
594 int powersave)
595{
596 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200597 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530598 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200599
Kalle Valod8cd1892010-01-05 20:16:26 +0200600 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
601 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200602 return;
603
Kalle Valod8cd1892010-01-05 20:16:26 +0200604 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200605 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200606 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200607
Johannes Berg62ae67b2009-11-18 18:42:05 +0100608 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530609 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
610 IEEE80211_STA_CONNECTION_POLL))
611 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
612
Johannes Berg62ae67b2009-11-18 18:42:05 +0100613 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200614}
615
Felix Fietkaud5242152010-01-08 18:06:26 +0100616static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
617 struct ieee80211_sub_if_data *sdata)
618{
619 struct sk_buff *skb;
620 struct ieee80211_hdr *nullfunc;
621 __le16 fc;
622
623 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
624 return;
625
626 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700627 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100628 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700629
Felix Fietkaud5242152010-01-08 18:06:26 +0100630 skb_reserve(skb, local->hw.extra_tx_headroom);
631
632 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
633 memset(nullfunc, 0, 30);
634 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
635 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
636 nullfunc->frame_control = fc;
637 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
638 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
639 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
640 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
641
642 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
643 ieee80211_tx_skb(sdata, skb);
644}
645
Johannes Bergcc32abd2009-05-15 11:52:31 +0200646/* spectrum management related things */
647static void ieee80211_chswitch_work(struct work_struct *work)
648{
649 struct ieee80211_sub_if_data *sdata =
650 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200651 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
652
Johannes Berg9607e6b2009-12-23 13:15:31 +0100653 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200654 return;
655
Johannes Berg77fdaa12009-07-07 03:45:17 +0200656 mutex_lock(&ifmgd->mtx);
657 if (!ifmgd->associated)
658 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200659
660 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200661 if (!sdata->local->ops->channel_switch) {
662 /* call "hw_config" only if doing sw channel switch */
663 ieee80211_hw_config(sdata->local,
664 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300665 } else {
666 /* update the device channel directly */
667 sdata->local->hw.conf.channel = sdata->local->oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200668 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200669
Johannes Berg77fdaa12009-07-07 03:45:17 +0200670 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100671 ifmgd->associated->channel = sdata->local->oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200672
Johannes Berg57eebdf2012-08-01 15:50:46 +0200673 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200674 ieee80211_wake_queues_by_reason(&sdata->local->hw,
675 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200676 out:
677 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
678 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200679}
680
Johannes Berg5ce6e432010-05-11 16:20:57 +0200681void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
682{
683 struct ieee80211_sub_if_data *sdata;
684 struct ieee80211_if_managed *ifmgd;
685
686 sdata = vif_to_sdata(vif);
687 ifmgd = &sdata->u.mgd;
688
689 trace_api_chswitch_done(sdata, success);
690 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200691 sdata_info(sdata,
692 "driver channel switch failed, disconnecting\n");
693 ieee80211_queue_work(&sdata->local->hw,
694 &ifmgd->csa_connection_drop_work);
695 } else {
696 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200697 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200698}
699EXPORT_SYMBOL(ieee80211_chswitch_done);
700
Johannes Bergcc32abd2009-05-15 11:52:31 +0200701static void ieee80211_chswitch_timer(unsigned long data)
702{
703 struct ieee80211_sub_if_data *sdata =
704 (struct ieee80211_sub_if_data *) data;
705 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
706
Johannes Berg5bb644a2009-05-17 11:40:42 +0200707 if (sdata->local->quiescing) {
708 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
709 return;
710 }
711
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400712 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200713}
714
715void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
716 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200717 struct ieee80211_bss *bss,
718 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200719{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100720 struct cfg80211_bss *cbss =
721 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200722 struct ieee80211_channel *new_ch;
723 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900724 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
725 cbss->channel->band);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200726
Johannes Berg77fdaa12009-07-07 03:45:17 +0200727 ASSERT_MGD_MTX(ifmgd);
728
729 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200730 return;
731
Helmut Schaafbe9c422009-07-23 12:14:04 +0200732 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200733 return;
734
735 /* Disregard subsequent beacons if we are already running a timer
736 processing a CSA */
737
738 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
739 return;
740
741 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +0200742 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
743 sdata_info(sdata,
744 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
745 ifmgd->associated->bssid, new_freq);
746 ieee80211_queue_work(&sdata->local->hw,
747 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200748 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200749 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200750
751 sdata->local->csa_channel = new_ch;
752
Johannes Berg57eebdf2012-08-01 15:50:46 +0200753 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
754
755 if (sw_elem->mode)
756 ieee80211_stop_queues_by_reason(&sdata->local->hw,
757 IEEE80211_QUEUE_STOP_REASON_CSA);
758
Johannes Berg5ce6e432010-05-11 16:20:57 +0200759 if (sdata->local->ops->channel_switch) {
760 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200761 struct ieee80211_channel_switch ch_switch = {
762 .timestamp = timestamp,
763 .block_tx = sw_elem->mode,
764 .channel = new_ch,
765 .count = sw_elem->count,
766 };
767
Johannes Berg5ce6e432010-05-11 16:20:57 +0200768 drv_channel_switch(sdata->local, &ch_switch);
769 return;
770 }
771
772 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200773 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400774 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200775 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200776 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200777 TU_TO_EXP_TIME(sw_elem->count *
778 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200779}
780
781static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
Johannes Berg761a48d2012-09-05 13:07:00 +0200782 u16 capab_info, u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200783{
784 struct ieee80211_conf *conf = &sdata->local->hw.conf;
785
786 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
787 return;
788
Hong Wua48b13a2012-01-11 20:34:30 +0200789 if ((*pwr_constr_elem <= conf->channel->max_reg_power) &&
Johannes Bergcc32abd2009-05-15 11:52:31 +0200790 (*pwr_constr_elem != sdata->local->power_constr_level)) {
791 sdata->local->power_constr_level = *pwr_constr_elem;
792 ieee80211_hw_config(sdata->local, 0);
793 }
794}
795
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300796void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
797{
798 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
799 struct ieee80211_local *local = sdata->local;
800 struct ieee80211_conf *conf = &local->hw.conf;
801
802 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
803 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
804 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
805
806 local->disable_dynamic_ps = false;
807 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
808}
809EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
810
811void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
812{
813 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
814 struct ieee80211_local *local = sdata->local;
815 struct ieee80211_conf *conf = &local->hw.conf;
816
817 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
818 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
819 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
820
821 local->disable_dynamic_ps = true;
822 conf->dynamic_ps_timeout = 0;
823 del_timer_sync(&local->dynamic_ps_timer);
824 ieee80211_queue_work(&local->hw,
825 &local->dynamic_ps_enable_work);
826}
827EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
828
Johannes Berg965beda2009-04-16 13:17:24 +0200829/* powersave */
830static void ieee80211_enable_ps(struct ieee80211_local *local,
831 struct ieee80211_sub_if_data *sdata)
832{
833 struct ieee80211_conf *conf = &local->hw.conf;
834
Johannes Bergd5edaed2009-04-22 23:02:51 +0200835 /*
836 * If we are scanning right now then the parameters will
837 * take effect when scan finishes.
838 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200839 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200840 return;
841
Johannes Berg965beda2009-04-16 13:17:24 +0200842 if (conf->dynamic_ps_timeout > 0 &&
843 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
844 mod_timer(&local->dynamic_ps_timer, jiffies +
845 msecs_to_jiffies(conf->dynamic_ps_timeout));
846 } else {
847 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
848 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530849
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200850 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
851 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
852 return;
853
854 conf->flags |= IEEE80211_CONF_PS;
855 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200856 }
857}
858
859static void ieee80211_change_ps(struct ieee80211_local *local)
860{
861 struct ieee80211_conf *conf = &local->hw.conf;
862
863 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200864 ieee80211_enable_ps(local, local->ps_sdata);
865 } else if (conf->flags & IEEE80211_CONF_PS) {
866 conf->flags &= ~IEEE80211_CONF_PS;
867 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
868 del_timer_sync(&local->dynamic_ps_timer);
869 cancel_work_sync(&local->dynamic_ps_enable_work);
870 }
871}
872
Jason Young808118c2011-03-10 16:43:19 -0800873static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
874{
875 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
876 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200877 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800878
879 if (!mgd->powersave)
880 return false;
881
Johannes Berg05cb9102011-10-28 11:59:47 +0200882 if (mgd->broken_ap)
883 return false;
884
Jason Young808118c2011-03-10 16:43:19 -0800885 if (!mgd->associated)
886 return false;
887
Jason Young808118c2011-03-10 16:43:19 -0800888 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
889 IEEE80211_STA_CONNECTION_POLL))
890 return false;
891
892 rcu_read_lock();
893 sta = sta_info_get(sdata, mgd->bssid);
894 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200895 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -0800896 rcu_read_unlock();
897
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200898 return authorized;
Jason Young808118c2011-03-10 16:43:19 -0800899}
900
Johannes Berg965beda2009-04-16 13:17:24 +0200901/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200902void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200903{
904 struct ieee80211_sub_if_data *sdata, *found = NULL;
905 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300906 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +0200907
908 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
909 local->ps_sdata = NULL;
910 return;
911 }
912
913 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100914 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +0200915 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +0530916 if (sdata->vif.type == NL80211_IFTYPE_AP) {
917 /* If an AP vif is found, then disable PS
918 * by setting the count to zero thereby setting
919 * ps_sdata to NULL.
920 */
921 count = 0;
922 break;
923 }
Johannes Berg965beda2009-04-16 13:17:24 +0200924 if (sdata->vif.type != NL80211_IFTYPE_STATION)
925 continue;
926 found = sdata;
927 count++;
928 }
929
Jason Young808118c2011-03-10 16:43:19 -0800930 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300931 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +0200932 s32 beaconint_us;
933
934 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +0200935 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +0200936
937 beaconint_us = ieee80211_tu_to_usec(
938 found->vif.bss_conf.beacon_int);
939
Juuso Oikarinenff616382010-06-09 09:51:52 +0300940 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300941 if (timeout < 0) {
942 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +0300943 * Go to full PSM if the user configures a very low
944 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +0200945 * The 2000 second value is there for compatibility
946 * until the PM_QOS_NETWORK_LATENCY is configured
947 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300948 */
Eliad Peller0ab82b02010-12-03 02:16:23 +0200949 if (latency > (1900 * USEC_PER_MSEC) &&
950 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300951 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +0300952 else
953 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300954 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300955 local->dynamic_ps_user_timeout = timeout;
956 if (!local->disable_dynamic_ps)
957 conf->dynamic_ps_timeout =
958 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300959
Johannes Berg04fe2032009-04-22 18:44:37 +0200960 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +0200961 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +0200962 } else {
Johannes Berg56007a02010-01-26 14:19:52 +0100963 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +0200964 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +0100965 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +0200966
Johannes Berg56007a02010-01-26 14:19:52 +0100967 bss = (void *)found->u.mgd.associated->priv;
968 dtimper = bss->dtim_period;
969
970 /* If the TIM IE is invalid, pretend the value is 1 */
971 if (!dtimper)
972 dtimper = 1;
973 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +0200974 maxslp = min_t(int, dtimper,
975 latency / beaconint_us);
976
Johannes Berg9ccebe62009-04-23 10:32:36 +0200977 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +0100978 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +0200979 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +0200980 }
Johannes Berg10f644a2009-04-16 13:17:25 +0200981 } else {
Johannes Berg965beda2009-04-16 13:17:24 +0200982 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +0200983 }
Johannes Berg965beda2009-04-16 13:17:24 +0200984
985 ieee80211_change_ps(local);
986}
987
Eliad Pellerab095872012-07-27 12:33:22 +0300988void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
989{
990 bool ps_allowed = ieee80211_powersave_allowed(sdata);
991
992 if (sdata->vif.bss_conf.ps != ps_allowed) {
993 sdata->vif.bss_conf.ps = ps_allowed;
994 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
995 }
996}
997
Johannes Berg965beda2009-04-16 13:17:24 +0200998void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
999{
1000 struct ieee80211_local *local =
1001 container_of(work, struct ieee80211_local,
1002 dynamic_ps_disable_work);
1003
1004 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1005 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1006 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1007 }
1008
1009 ieee80211_wake_queues_by_reason(&local->hw,
1010 IEEE80211_QUEUE_STOP_REASON_PS);
1011}
1012
1013void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1014{
1015 struct ieee80211_local *local =
1016 container_of(work, struct ieee80211_local,
1017 dynamic_ps_enable_work);
1018 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001019 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301020 unsigned long flags;
1021 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001022
1023 /* can only happen when PS was just disabled anyway */
1024 if (!sdata)
1025 return;
1026
Christian Lamparter5e340692011-06-30 21:08:43 +02001027 ifmgd = &sdata->u.mgd;
1028
Johannes Berg965beda2009-04-16 13:17:24 +02001029 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1030 return;
1031
Arik Nemtsov77b70232011-06-26 12:06:54 +03001032 if (!local->disable_dynamic_ps &&
1033 local->hw.conf.dynamic_ps_timeout > 0) {
1034 /* don't enter PS if TX frames are pending */
1035 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301036 mod_timer(&local->dynamic_ps_timer, jiffies +
1037 msecs_to_jiffies(
1038 local->hw.conf.dynamic_ps_timeout));
1039 return;
1040 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001041
1042 /*
1043 * transmission can be stopped by others which leads to
1044 * dynamic_ps_timer expiry. Postpone the ps timer if it
1045 * is not the actual idle state.
1046 */
1047 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1048 for (q = 0; q < local->hw.queues; q++) {
1049 if (local->queue_stop_reasons[q]) {
1050 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1051 flags);
1052 mod_timer(&local->dynamic_ps_timer, jiffies +
1053 msecs_to_jiffies(
1054 local->hw.conf.dynamic_ps_timeout));
1055 return;
1056 }
1057 }
1058 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301059 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301060
Vivek Natarajan375177b2010-02-09 14:50:28 +05301061 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301062 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301063 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001064
Vivek Natarajane8306f92011-04-06 11:41:10 +05301065 if (drv_tx_frames_pending(local))
1066 mod_timer(&local->dynamic_ps_timer, jiffies +
1067 msecs_to_jiffies(
1068 local->hw.conf.dynamic_ps_timeout));
1069 else {
1070 ieee80211_send_nullfunc(local, sdata, 1);
1071 /* Flush to get the tx status of nullfunc frame */
1072 drv_flush(local, false);
1073 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301074 }
1075
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001076 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1077 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301078 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1079 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1080 local->hw.conf.flags |= IEEE80211_CONF_PS;
1081 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1082 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301083
Arik Nemtsov77b70232011-06-26 12:06:54 +03001084 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1085 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001086}
1087
1088void ieee80211_dynamic_ps_timer(unsigned long data)
1089{
1090 struct ieee80211_local *local = (void *) data;
1091
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001092 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001093 return;
1094
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001095 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001096}
1097
Johannes Berg60f8b392008-09-08 17:44:22 +02001098/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001099static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001100 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001101 u8 *wmm_param, size_t wmm_param_len)
1102{
Jiri Bencf0706e82007-05-05 11:45:53 -07001103 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001104 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001105 size_t left;
1106 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001107 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001108
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001109 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001110 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001111
Johannes Berg32c50572012-03-28 11:04:29 +02001112 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001113 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001114
1115 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001116 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001117
Jiri Bencf0706e82007-05-05 11:45:53 -07001118 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001119 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001120
1121 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001122 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001123
Jiri Bencf0706e82007-05-05 11:45:53 -07001124 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001125 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001126 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001127 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001128
1129 pos = wmm_param + 8;
1130 left = wmm_param_len - 8;
1131
1132 memset(&params, 0, sizeof(params));
1133
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001134 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001135 for (; left >= 4; left -= 4, pos += 4) {
1136 int aci = (pos[0] >> 5) & 0x03;
1137 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001138 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001139 int queue;
1140
1141 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001142 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001143 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001144 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001145 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001146 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1147 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001148 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001149 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001150 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001151 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001152 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001153 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1154 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001155 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001156 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001157 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001158 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001159 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001160 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1161 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001162 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001163 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001164 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001165 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001166 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001167 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001168 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1169 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001170 break;
1171 }
1172
1173 params.aifs = pos[0] & 0x0f;
1174 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1175 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001176 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001177 params.uapsd = uapsd;
1178
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001179 mlme_dbg(sdata,
1180 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1181 queue, aci, acm,
1182 params.aifs, params.cw_min, params.cw_max,
1183 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001184 sdata->tx_conf[queue] = params;
1185 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001186 sdata_err(sdata,
1187 "failed to set TX queue parameters for queue %d\n",
1188 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001189 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001190
1191 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001192 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001193 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001194}
1195
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001196static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1197{
1198 lockdep_assert_held(&sdata->local->mtx);
1199
1200 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1201 IEEE80211_STA_BEACON_POLL);
1202 ieee80211_run_deferred_scan(sdata->local);
1203}
1204
1205static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1206{
1207 mutex_lock(&sdata->local->mtx);
1208 __ieee80211_stop_poll(sdata);
1209 mutex_unlock(&sdata->local->mtx);
1210}
1211
Johannes Berg7a5158e2008-10-08 10:59:33 +02001212static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1213 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001214{
Johannes Bergbda39332008-10-11 01:51:51 +02001215 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001216 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001217 bool use_protection;
1218 bool use_short_preamble;
1219 bool use_short_slot;
1220
1221 if (erp_valid) {
1222 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1223 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1224 } else {
1225 use_protection = false;
1226 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1227 }
1228
1229 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg568d6e22012-07-23 14:29:21 +02001230 if (sdata->local->oper_channel->band == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001231 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001232
Johannes Berg471b3ef2007-12-28 14:32:58 +01001233 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001234 bss_conf->use_cts_prot = use_protection;
1235 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001236 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001237
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001238 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001239 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001240 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001241 }
Daniel Draked9430a32007-07-27 15:43:24 +02001242
Johannes Berg7a5158e2008-10-08 10:59:33 +02001243 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001244 bss_conf->use_short_slot = use_short_slot;
1245 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001246 }
1247
1248 return changed;
1249}
1250
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001251static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001252 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001253 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001254{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001255 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001256 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001257 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001258
Johannes Bergae5eb022008-10-14 16:58:37 +02001259 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001260 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001261 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001262
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001263 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1264 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1265
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001266 sdata->u.mgd.associated = cbss;
1267 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001268
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001269 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1270
Johannes Bergb291ba12009-07-10 15:29:03 +02001271 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001272 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001273
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001274 ieee80211_led_assoc(local, 1);
1275
Johannes Berge5b900d2010-07-29 16:08:55 +02001276 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1277 bss_conf->dtim_period = bss->dtim_period;
1278 else
1279 bss_conf->dtim_period = 0;
1280
Juuso Oikarinen68542962010-06-09 13:43:26 +03001281 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001282
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001283 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001284 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001285 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001286 bss_info_changed |= BSS_CHANGED_CQM;
1287
Juuso Oikarinen68542962010-06-09 13:43:26 +03001288 /* Enable ARP filtering */
1289 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1290 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1291 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1292 }
1293
Johannes Bergae5eb022008-10-14 16:58:37 +02001294 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001295
Johannes Berg056508d2009-07-30 21:43:55 +02001296 mutex_lock(&local->iflist_mtx);
1297 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +02001298 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02001299 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001300
Eliad Pellerab095872012-07-27 12:33:22 +03001301 ieee80211_recalc_ps_vif(sdata);
1302
John W. Linville8a5b33f2010-01-06 15:39:39 -05001303 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001304 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001305}
1306
Jouni Malinene69e95d2010-03-29 23:29:31 -07001307static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001308 u16 stype, u16 reason, bool tx,
1309 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001310{
Johannes Berg46900292009-02-15 12:44:28 +01001311 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001312 struct ieee80211_local *local = sdata->local;
1313 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001314 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001315
Johannes Berg77fdaa12009-07-07 03:45:17 +02001316 ASSERT_MGD_MTX(ifmgd);
1317
Johannes Berg37ad3882012-02-24 13:50:54 +01001318 if (WARN_ON_ONCE(tx && !frame_buf))
1319 return;
1320
Johannes Bergb291ba12009-07-10 15:29:03 +02001321 if (WARN_ON(!ifmgd->associated))
1322 return;
1323
David Spinadel79543d82012-06-12 09:59:45 +03001324 ieee80211_stop_poll(sdata);
1325
Johannes Berg77fdaa12009-07-07 03:45:17 +02001326 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001327
1328 /*
1329 * we need to commit the associated = NULL change because the
1330 * scan code uses that to determine whether this iface should
1331 * go to/wake up from powersave or not -- and could otherwise
1332 * wake the queues erroneously.
1333 */
1334 smp_mb();
1335
1336 /*
1337 * Thus, we can only afterwards stop the queues -- to account
1338 * for the case where another CPU is finishing a scan at this
1339 * time -- we don't want the scan code to enable queues.
1340 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001341
John W. Linville8a5b33f2010-01-06 15:39:39 -05001342 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001343 netif_carrier_off(sdata->dev);
1344
Johannes Berg2a419052010-06-10 10:21:29 +02001345 mutex_lock(&local->sta_mtx);
Eliad Peller88a9e312012-06-01 11:14:03 +03001346 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301347 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001348 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +02001349 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +05301350 }
Johannes Berg2a419052010-06-10 10:21:29 +02001351 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001352
Eliad Peller88bc40e2012-07-12 17:35:33 +03001353 /*
1354 * if we want to get out of ps before disassoc (why?) we have
1355 * to do it before sending disassoc, as otherwise the null-packet
1356 * won't be valid.
1357 */
1358 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1359 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1360 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1361 }
1362 local->ps_sdata = NULL;
1363
Eliad Pellerab095872012-07-27 12:33:22 +03001364 /* disable per-vif ps */
1365 ieee80211_recalc_ps_vif(sdata);
1366
Eliad Pellerf8239812012-06-27 14:18:22 +03001367 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1368 if (tx)
1369 drv_flush(local, false);
1370
Johannes Berg37ad3882012-02-24 13:50:54 +01001371 /* deauthenticate/disassociate now */
1372 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001373 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1374 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001375
1376 /* flush out frame */
1377 if (tx)
1378 drv_flush(local, false);
1379
Eliad Peller88a9e312012-06-01 11:14:03 +03001380 /* clear bssid only after building the needed mgmt frames */
1381 memset(ifmgd->bssid, 0, ETH_ALEN);
1382
Johannes Berg37ad3882012-02-24 13:50:54 +01001383 /* remove AP and TDLS peers */
1384 sta_info_flush(local, sdata);
1385
1386 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001387 changed |= ieee80211_reset_erp_info(sdata);
1388
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001389 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001390 changed |= BSS_CHANGED_ASSOC;
1391 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001392
Johannes Berg413ad502009-05-08 21:21:06 +02001393 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001394 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1395 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001396
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301397 local->power_constr_level = 0;
1398
Kalle Valo520eb822008-12-18 23:35:27 +02001399 del_timer_sync(&local->dynamic_ps_timer);
1400 cancel_work_sync(&local->dynamic_ps_enable_work);
1401
Juuso Oikarinen68542962010-06-09 13:43:26 +03001402 /* Disable ARP filtering */
1403 if (sdata->vif.bss_conf.arp_filter_enabled) {
1404 sdata->vif.bss_conf.arp_filter_enabled = false;
1405 changed |= BSS_CHANGED_ARP_FILTER;
1406 }
1407
Johannes Berg3abead52012-03-02 15:56:59 +01001408 sdata->vif.bss_conf.qos = false;
1409 changed |= BSS_CHANGED_QOS;
1410
Johannes Berg0aaffa92010-05-05 15:28:27 +02001411 /* The BSSID (not really interesting) and HT changed */
1412 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001413 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001414
Johannes Berg3cc52402012-03-09 13:12:35 +01001415 /* channel(_type) changes are handled by ieee80211_hw_config */
1416 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
1417 ieee80211_hw_config(local, 0);
1418
Johannes Berg3abead52012-03-02 15:56:59 +01001419 /* disassociated - set to defaults now */
1420 ieee80211_set_wmm_default(sdata, false);
1421
Johannes Bergb9dcf712010-08-27 12:35:54 +02001422 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1423 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1424 del_timer_sync(&sdata->u.mgd.timer);
1425 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001426
1427 sdata->u.mgd.timers_running = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001428}
Jiri Bencf0706e82007-05-05 11:45:53 -07001429
Kalle Valo3cf335d2009-03-22 21:57:06 +02001430void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1431 struct ieee80211_hdr *hdr)
1432{
1433 /*
1434 * We can postpone the mgd.timer whenever receiving unicast frames
1435 * from AP because we know that the connection is working both ways
1436 * at that time. But multicast frames (and hence also beacons) must
1437 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001438 * data idle periods for sending the periodic probe request to the
1439 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001440 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001441 if (is_multicast_ether_addr(hdr->addr1))
1442 return;
1443
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001444 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001445}
Jiri Bencf0706e82007-05-05 11:45:53 -07001446
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001447static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1448{
1449 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001450 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001451
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001452 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001453 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001454 IEEE80211_STA_CONNECTION_POLL))) {
1455 mutex_unlock(&local->mtx);
1456 return;
1457 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001458
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001459 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001460
1461 mutex_lock(&local->iflist_mtx);
1462 ieee80211_recalc_ps(local, -1);
1463 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001464
1465 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001466 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001467
1468 /*
1469 * We've received a probe response, but are not sure whether
1470 * we have or will be receiving any beacons or data, so let's
1471 * schedule the timers again, just in case.
1472 */
1473 ieee80211_sta_reset_beacon_monitor(sdata);
1474
1475 mod_timer(&ifmgd->conn_mon_timer,
1476 round_jiffies_up(jiffies +
1477 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001478out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001479 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001480}
1481
1482void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001483 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001484{
Felix Fietkau75706d02010-12-02 21:01:07 +01001485 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001486 return;
1487
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001488 if (ack)
1489 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001490
1491 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1492 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001493 if (ack)
1494 sdata->u.mgd.probe_send_count = 0;
1495 else
1496 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001497 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1498 }
1499}
1500
Maxim Levitskya43abf22009-07-31 18:54:12 +03001501static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1502{
1503 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1504 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001505 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001506 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001507
1508 /*
1509 * Try sending broadcast probe requests for the last three
1510 * probe requests after the first ones failed since some
1511 * buggy APs only support broadcast probe requests.
1512 */
1513 if (ifmgd->probe_send_count >= unicast_limit)
1514 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001515
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001516 /*
1517 * When the hardware reports an accurate Tx ACK status, it's
1518 * better to send a nullfunc frame instead of a probe request,
1519 * as it will kick us off the AP quickly if we aren't associated
1520 * anymore. The timeout will be reset if the frame is ACKed by
1521 * the AP.
1522 */
Soumik Das992e68b2012-05-20 15:31:13 +05301523 ifmgd->probe_send_count++;
1524
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001525 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1526 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001527 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001528 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001529 int ssid_len;
1530
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001531 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001532 if (WARN_ON_ONCE(ssid == NULL))
1533 ssid_len = 0;
1534 else
1535 ssid_len = ssid[1];
1536
1537 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Bergfe94fe02012-07-30 12:26:34 +02001538 0, (u32) -1, true, false,
1539 ifmgd->associated->channel);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001540 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001541
Ben Greear180205b2011-02-04 15:30:24 -08001542 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001543 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301544 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1545 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001546}
1547
Johannes Bergb291ba12009-07-10 15:29:03 +02001548static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1549 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001550{
Kalle Valo04de8382009-03-22 21:57:35 +02001551 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001552 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001553
Johannes Berg9607e6b2009-12-23 13:15:31 +01001554 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001555 return;
1556
Johannes Berg77fdaa12009-07-07 03:45:17 +02001557 mutex_lock(&ifmgd->mtx);
1558
1559 if (!ifmgd->associated)
1560 goto out;
1561
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001562 mutex_lock(&sdata->local->mtx);
1563
1564 if (sdata->local->tmp_channel || sdata->local->scanning) {
1565 mutex_unlock(&sdata->local->mtx);
1566 goto out;
1567 }
1568
Joe Perchese87cc472012-05-13 21:56:26 +00001569 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001570 mlme_dbg_ratelimited(sdata,
1571 "detected beacon loss from AP - sending probe request\n");
1572
Holger Schurig6efb71b2012-05-15 15:38:59 +02001573 ieee80211_cqm_rssi_notify(&sdata->vif,
1574 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001575
Johannes Bergb291ba12009-07-10 15:29:03 +02001576 /*
1577 * The driver/our work has already reported this event or the
1578 * connection monitoring has kicked in and we have already sent
1579 * a probe request. Or maybe the AP died and the driver keeps
1580 * reporting until we disassociate...
1581 *
1582 * In either case we have to ignore the current call to this
1583 * function (except for setting the correct probe reason bit)
1584 * because otherwise we would reset the timer every time and
1585 * never check whether we received a probe response!
1586 */
1587 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1588 IEEE80211_STA_CONNECTION_POLL))
1589 already = true;
1590
1591 if (beacon)
1592 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1593 else
1594 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1595
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001596 mutex_unlock(&sdata->local->mtx);
1597
Johannes Bergb291ba12009-07-10 15:29:03 +02001598 if (already)
1599 goto out;
1600
Johannes Berg4e751842009-06-10 15:16:52 +02001601 mutex_lock(&sdata->local->iflist_mtx);
1602 ieee80211_recalc_ps(sdata->local, -1);
1603 mutex_unlock(&sdata->local->iflist_mtx);
1604
Maxim Levitskya43abf22009-07-31 18:54:12 +03001605 ifmgd->probe_send_count = 0;
1606 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001607 out:
1608 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001609}
1610
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001611struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1612 struct ieee80211_vif *vif)
1613{
1614 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1615 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001616 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001617 struct sk_buff *skb;
1618 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001619 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001620
1621 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1622 return NULL;
1623
1624 ASSERT_MGD_MTX(ifmgd);
1625
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001626 if (ifmgd->associated)
1627 cbss = ifmgd->associated;
1628 else if (ifmgd->auth_data)
1629 cbss = ifmgd->auth_data->bss;
1630 else if (ifmgd->assoc_data)
1631 cbss = ifmgd->assoc_data->bss;
1632 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001633 return NULL;
1634
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001635 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001636 if (WARN_ON_ONCE(ssid == NULL))
1637 ssid_len = 0;
1638 else
1639 ssid_len = ssid[1];
1640
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001641 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg6b778632012-07-23 14:53:27 +02001642 (u32) -1,
1643 sdata->local->oper_channel,
1644 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001645 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001646
1647 return skb;
1648}
1649EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1650
Johannes Berg882a7c62012-08-01 22:32:45 +02001651static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1652 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001653{
1654 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1655 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001656 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001657
1658 mutex_lock(&ifmgd->mtx);
1659 if (!ifmgd->associated) {
1660 mutex_unlock(&ifmgd->mtx);
1661 return;
1662 }
1663
Johannes Berg37ad3882012-02-24 13:50:54 +01001664 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1665 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001666 transmit_frame, frame_buf);
1667 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001668 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001669
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001670 /*
1671 * must be outside lock due to cfg80211,
1672 * but that's not a problem.
1673 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001674 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001675
1676 mutex_lock(&local->mtx);
1677 ieee80211_recalc_idle(local);
1678 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001679}
1680
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001681static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001682{
1683 struct ieee80211_sub_if_data *sdata =
1684 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001685 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001686 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1687 struct sta_info *sta;
1688
1689 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301690 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001691 sta = sta_info_get(sdata, ifmgd->bssid);
1692 if (sta)
1693 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301694 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001695 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001696
Johannes Berg882a7c62012-08-01 22:32:45 +02001697 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1698 sdata_info(sdata, "Connection to AP %pM lost\n",
1699 ifmgd->bssid);
1700 __ieee80211_disconnect(sdata, false);
1701 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001702 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001703 }
1704}
1705
1706static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1707{
1708 struct ieee80211_sub_if_data *sdata =
1709 container_of(work, struct ieee80211_sub_if_data,
1710 u.mgd.csa_connection_drop_work);
1711
1712 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1713 IEEE80211_QUEUE_STOP_REASON_CSA);
1714 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001715}
1716
Kalle Valo04de8382009-03-22 21:57:35 +02001717void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1718{
1719 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001720 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001721
Johannes Bergb5878a22010-04-07 16:48:40 +02001722 trace_api_beacon_loss(sdata);
1723
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001724 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1725 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001726}
1727EXPORT_SYMBOL(ieee80211_beacon_loss);
1728
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001729void ieee80211_connection_loss(struct ieee80211_vif *vif)
1730{
1731 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1732 struct ieee80211_hw *hw = &sdata->local->hw;
1733
Johannes Bergb5878a22010-04-07 16:48:40 +02001734 trace_api_connection_loss(sdata);
1735
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001736 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1737 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1738}
1739EXPORT_SYMBOL(ieee80211_connection_loss);
1740
1741
Johannes Berg66e67e42012-01-20 13:55:27 +01001742static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1743 bool assoc)
1744{
1745 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1746
1747 lockdep_assert_held(&sdata->u.mgd.mtx);
1748
Johannes Berg66e67e42012-01-20 13:55:27 +01001749 if (!assoc) {
1750 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1751
1752 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1753 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1754 }
1755
1756 cfg80211_put_bss(auth_data->bss);
1757 kfree(auth_data);
1758 sdata->u.mgd.auth_data = NULL;
1759}
1760
1761static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1762 struct ieee80211_mgmt *mgmt, size_t len)
1763{
1764 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1765 u8 *pos;
1766 struct ieee802_11_elems elems;
1767
1768 pos = mgmt->u.auth.variable;
1769 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1770 if (!elems.challenge)
1771 return;
1772 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001773 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001774 ieee80211_send_auth(sdata, 3, auth_data->algorithm,
1775 elems.challenge - 2, elems.challenge_len + 2,
1776 auth_data->bss->bssid, auth_data->bss->bssid,
1777 auth_data->key, auth_data->key_len,
1778 auth_data->key_idx);
1779}
1780
1781static enum rx_mgmt_action __must_check
1782ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1783 struct ieee80211_mgmt *mgmt, size_t len)
1784{
1785 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1786 u8 bssid[ETH_ALEN];
1787 u16 auth_alg, auth_transaction, status_code;
1788 struct sta_info *sta;
1789
1790 lockdep_assert_held(&ifmgd->mtx);
1791
1792 if (len < 24 + 6)
1793 return RX_MGMT_NONE;
1794
1795 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1796 return RX_MGMT_NONE;
1797
1798 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1799
Joe Perchesb203ca32012-05-08 18:56:52 +00001800 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001801 return RX_MGMT_NONE;
1802
1803 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1804 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1805 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1806
1807 if (auth_alg != ifmgd->auth_data->algorithm ||
1808 auth_transaction != ifmgd->auth_data->expected_transaction)
1809 return RX_MGMT_NONE;
1810
1811 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001812 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1813 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001814 ieee80211_destroy_auth_data(sdata, false);
1815 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001816 }
1817
1818 switch (ifmgd->auth_data->algorithm) {
1819 case WLAN_AUTH_OPEN:
1820 case WLAN_AUTH_LEAP:
1821 case WLAN_AUTH_FT:
1822 break;
1823 case WLAN_AUTH_SHARED_KEY:
1824 if (ifmgd->auth_data->expected_transaction != 4) {
1825 ieee80211_auth_challenge(sdata, mgmt, len);
1826 /* need another frame */
1827 return RX_MGMT_NONE;
1828 }
1829 break;
1830 default:
1831 WARN_ONCE(1, "invalid auth alg %d",
1832 ifmgd->auth_data->algorithm);
1833 return RX_MGMT_NONE;
1834 }
1835
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001836 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001837 ifmgd->auth_data->done = true;
1838 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1839 run_again(ifmgd, ifmgd->auth_data->timeout);
1840
1841 /* move station state to auth */
1842 mutex_lock(&sdata->local->sta_mtx);
1843 sta = sta_info_get(sdata, bssid);
1844 if (!sta) {
1845 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1846 goto out_err;
1847 }
1848 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001849 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01001850 goto out_err;
1851 }
1852 mutex_unlock(&sdata->local->sta_mtx);
1853
1854 return RX_MGMT_CFG80211_RX_AUTH;
1855 out_err:
1856 mutex_unlock(&sdata->local->sta_mtx);
1857 /* ignore frame -- wait for timeout */
1858 return RX_MGMT_NONE;
1859}
1860
1861
Johannes Berg77fdaa12009-07-07 03:45:17 +02001862static enum rx_mgmt_action __must_check
1863ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001864 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001865{
Johannes Berg46900292009-02-15 12:44:28 +01001866 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001867 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001868 u16 reason_code;
1869
Johannes Berg66e67e42012-01-20 13:55:27 +01001870 lockdep_assert_held(&ifmgd->mtx);
1871
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001872 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001873 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001874
Johannes Berg66e67e42012-01-20 13:55:27 +01001875 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001876 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001877 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001878
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001879 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001880
1881 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1882
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001883 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
1884 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001885
Johannes Berg37ad3882012-02-24 13:50:54 +01001886 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1887
Johannes Berg7da7cc12010-08-05 17:02:38 +02001888 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001889 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001890 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001891
1892 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001893}
1894
1895
Johannes Berg77fdaa12009-07-07 03:45:17 +02001896static enum rx_mgmt_action __must_check
1897ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1898 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001899{
Johannes Berg46900292009-02-15 12:44:28 +01001900 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001901 u16 reason_code;
1902
Johannes Berg66e67e42012-01-20 13:55:27 +01001903 lockdep_assert_held(&ifmgd->mtx);
1904
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001905 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001906 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001907
Johannes Berg66e67e42012-01-20 13:55:27 +01001908 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001909 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001910 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001911
1912 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1913
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001914 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
1915 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001916
Johannes Berg37ad3882012-02-24 13:50:54 +01001917 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1918
Johannes Berg7da7cc12010-08-05 17:02:38 +02001919 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001920 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001921 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01001922
Johannes Berg77fdaa12009-07-07 03:45:17 +02001923 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001924}
1925
Christian Lamparterc74d0842011-10-15 00:14:49 +02001926static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
1927 u8 *supp_rates, unsigned int supp_rates_len,
1928 u32 *rates, u32 *basic_rates,
1929 bool *have_higher_than_11mbit,
1930 int *min_rate, int *min_rate_index)
1931{
1932 int i, j;
1933
1934 for (i = 0; i < supp_rates_len; i++) {
1935 int rate = (supp_rates[i] & 0x7f) * 5;
1936 bool is_basic = !!(supp_rates[i] & 0x80);
1937
1938 if (rate > 110)
1939 *have_higher_than_11mbit = true;
1940
1941 /*
1942 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
1943 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
1944 *
1945 * Note: Even through the membership selector and the basic
1946 * rate flag share the same bit, they are not exactly
1947 * the same.
1948 */
1949 if (!!(supp_rates[i] & 0x80) &&
1950 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1951 continue;
1952
1953 for (j = 0; j < sband->n_bitrates; j++) {
1954 if (sband->bitrates[j].bitrate == rate) {
1955 *rates |= BIT(j);
1956 if (is_basic)
1957 *basic_rates |= BIT(j);
1958 if (rate < *min_rate) {
1959 *min_rate = rate;
1960 *min_rate_index = j;
1961 }
1962 break;
1963 }
1964 }
1965 }
1966}
Jiri Bencf0706e82007-05-05 11:45:53 -07001967
Johannes Berg66e67e42012-01-20 13:55:27 +01001968static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
1969 bool assoc)
1970{
1971 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
1972
1973 lockdep_assert_held(&sdata->u.mgd.mtx);
1974
Johannes Berg66e67e42012-01-20 13:55:27 +01001975 if (!assoc) {
1976 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
1977
1978 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1979 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1980 }
1981
1982 kfree(assoc_data);
1983 sdata->u.mgd.assoc_data = NULL;
1984}
1985
1986static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
1987 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01001988 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001989{
Johannes Berg46900292009-02-15 12:44:28 +01001990 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001991 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001992 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001993 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001994 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001995 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001996 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02001997 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02001998 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02001999 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002000
Johannes Bergaf6b6372009-12-23 13:15:35 +01002001 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002002
Jiri Bencf0706e82007-05-05 11:45:53 -07002003 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002004 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002005
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002006 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002007 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2008 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002009 aid &= ~(BIT(15) | BIT(14));
2010
Johannes Berg05cb9102011-10-28 11:59:47 +02002011 ifmgd->broken_ap = false;
2012
2013 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002014 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2015 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002016 aid = 0;
2017 ifmgd->broken_ap = true;
2018 }
2019
Johannes Bergaf6b6372009-12-23 13:15:35 +01002020 pos = mgmt->u.assoc_resp.variable;
2021 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2022
Jiri Bencf0706e82007-05-05 11:45:53 -07002023 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002024 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002025 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002026 }
2027
Johannes Berg46900292009-02-15 12:44:28 +01002028 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002029
Guy Eilam2a33bee2011-08-17 15:18:15 +03002030 mutex_lock(&sdata->local->sta_mtx);
2031 /*
2032 * station info was already allocated and inserted before
2033 * the association and should be available to us
2034 */
Johannes Berg7852e362012-01-20 13:55:24 +01002035 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002036 if (WARN_ON(!sta)) {
2037 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002038 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002039 }
2040
Johannes Berg66e67e42012-01-20 13:55:27 +01002041 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +01002042
Johannes Bergab1faea2009-07-01 21:41:17 +02002043 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Ben Greearef96a8422011-11-18 11:32:00 -08002044 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002045 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002046
Johannes Berg64f68e52012-03-28 10:58:37 +02002047 sta->supports_40mhz =
2048 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2049
Johannes Berg4b7679a2008-09-18 18:14:18 +02002050 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002051
Johannes Berg46900292009-02-15 12:44:28 +01002052 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002053 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002054
Johannes Bergddf4ac52008-10-22 11:41:38 +02002055 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002056 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002057
Johannes Bergc8987872012-01-20 13:55:17 +01002058 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2059 if (!err)
2060 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2061 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2062 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2063 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002064 sdata_info(sdata,
2065 "failed to move station %pM to desired state\n",
2066 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002067 WARN_ON(__sta_info_destroy(sta));
2068 mutex_unlock(&sdata->local->sta_mtx);
2069 return false;
2070 }
2071
Johannes Berg7852e362012-01-20 13:55:24 +01002072 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002073
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002074 /*
2075 * Always handle WMM once after association regardless
2076 * of the first value the AP uses. Setting -1 here has
2077 * that effect because the AP values is an unsigned
2078 * 4-bit value.
2079 */
2080 ifmgd->wmm_last_param_set = -1;
2081
Johannes Bergddf4ac52008-10-22 11:41:38 +02002082 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002083 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002084 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002085 else
Johannes Berg3abead52012-03-02 15:56:59 +01002086 ieee80211_set_wmm_default(sdata, false);
2087 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002088
Johannes Berg074d46d2012-03-15 19:45:16 +01002089 if (elems.ht_operation && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002090 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg24398e32012-03-28 10:58:36 +02002091 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2092 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002093
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002094 /* set AID and assoc capability,
2095 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002096 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002097 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002098 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002099
Kalle Valo15b7b062009-03-22 21:57:14 +02002100 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002101 * If we're using 4-addr mode, let the AP know that we're
2102 * doing so, so that it can create the STA VLAN on its side
2103 */
2104 if (ifmgd->use_4addr)
2105 ieee80211_send_4addr_nullfunc(local, sdata);
2106
2107 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002108 * Start timer to probe the connection to the AP now.
2109 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002110 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002111 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002112 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002113
Johannes Bergaf6b6372009-12-23 13:15:35 +01002114 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002115}
2116
Johannes Berg66e67e42012-01-20 13:55:27 +01002117static enum rx_mgmt_action __must_check
2118ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2119 struct ieee80211_mgmt *mgmt, size_t len,
2120 struct cfg80211_bss **bss)
2121{
2122 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2123 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2124 u16 capab_info, status_code, aid;
2125 struct ieee802_11_elems elems;
2126 u8 *pos;
2127 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002128
Johannes Berg66e67e42012-01-20 13:55:27 +01002129 lockdep_assert_held(&ifmgd->mtx);
2130
2131 if (!assoc_data)
2132 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002133 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002134 return RX_MGMT_NONE;
2135
2136 /*
2137 * AssocResp and ReassocResp have identical structure, so process both
2138 * of them in this function.
2139 */
2140
2141 if (len < 24 + 6)
2142 return RX_MGMT_NONE;
2143
2144 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2145 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2146 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2147 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2148
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002149 sdata_info(sdata,
2150 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2151 reassoc ? "Rea" : "A", mgmt->sa,
2152 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002153
2154 pos = mgmt->u.assoc_resp.variable;
2155 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2156
2157 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2158 elems.timeout_int && elems.timeout_int_len == 5 &&
2159 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2160 u32 tu, ms;
2161 tu = get_unaligned_le32(elems.timeout_int + 1);
2162 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002163 sdata_info(sdata,
2164 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2165 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002166 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2167 if (ms > IEEE80211_ASSOC_TIMEOUT)
2168 run_again(ifmgd, assoc_data->timeout);
2169 return RX_MGMT_NONE;
2170 }
2171
2172 *bss = assoc_data->bss;
2173
2174 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002175 sdata_info(sdata, "%pM denied association (code=%d)\n",
2176 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002177 ieee80211_destroy_assoc_data(sdata, false);
2178 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002179 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2180 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002181 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002182 cfg80211_put_bss(*bss);
2183 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2184 }
John W. Linville635d9992012-07-09 16:34:34 -04002185 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002186
2187 /*
2188 * destroy assoc_data afterwards, as otherwise an idle
2189 * recalc after assoc_data is NULL but before associated
2190 * is set can cause the interface to go idle
2191 */
2192 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002193 }
2194
2195 return RX_MGMT_CFG80211_RX_ASSOC;
2196}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002197static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002198 struct ieee80211_mgmt *mgmt,
2199 size_t len,
2200 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002201 struct ieee802_11_elems *elems,
2202 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002203{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002204 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002205 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002206 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002207 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002208 bool need_ps = false;
2209
Johannes Berg66e67e42012-01-20 13:55:27 +01002210 if (sdata->u.mgd.associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002211 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
Johannes Berg56007a02010-01-26 14:19:52 +01002212 bss = (void *)sdata->u.mgd.associated->priv;
2213 /* not previously set so we may need to recalc */
2214 need_ps = !bss->dtim_period;
2215 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002216
Johannes Berg5bda6172008-09-08 11:05:10 +02002217 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002218 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2219 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002220 else
2221 freq = rx_status->freq;
2222
2223 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2224
2225 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2226 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002227
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002228 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002229 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002230 if (bss)
2231 ieee80211_rx_bss_put(local, bss);
2232
2233 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002234 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002235
Johannes Berg56007a02010-01-26 14:19:52 +01002236 if (need_ps) {
2237 mutex_lock(&local->iflist_mtx);
2238 ieee80211_recalc_ps(local, -1);
2239 mutex_unlock(&local->iflist_mtx);
2240 }
2241
Johannes Berg5bc14202012-08-01 16:13:02 +02002242 if (elems->ch_switch_ie &&
2243 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2244 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002245 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002246}
2247
2248
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002249static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002250 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002251{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002252 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002253 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002254 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2255 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002256 struct ieee802_11_elems elems;
2257
Kalle Valo15b7b062009-03-22 21:57:14 +02002258 ifmgd = &sdata->u.mgd;
2259
Johannes Berg77fdaa12009-07-07 03:45:17 +02002260 ASSERT_MGD_MTX(ifmgd);
2261
Joe Perchesb203ca32012-05-08 18:56:52 +00002262 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002263 return; /* ignore ProbeResp to foreign address */
2264
Ester Kummerae6a44e2008-06-27 18:54:48 +03002265 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2266 if (baselen > len)
2267 return;
2268
2269 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2270 &elems);
2271
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002272 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002273
Johannes Berg77fdaa12009-07-07 03:45:17 +02002274 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002275 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002276 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002277
2278 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002279 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002280 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002281 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002282 ifmgd->auth_data->tries = 0;
2283 ifmgd->auth_data->timeout = jiffies;
2284 run_again(ifmgd, ifmgd->auth_data->timeout);
2285 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002286}
2287
Johannes Bergd91f36d2009-04-16 13:17:26 +02002288/*
2289 * This is the canonical list of information elements we care about,
2290 * the filter code also gives us all changes to the Microsoft OUI
2291 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2292 *
2293 * We implement beacon filtering in software since that means we can
2294 * avoid processing the frame here and in cfg80211, and userspace
2295 * will not be able to tell whether the hardware supports it or not.
2296 *
2297 * XXX: This list needs to be dynamic -- userspace needs to be able to
2298 * add items it requires. It also needs to be able to tell us to
2299 * look out for other vendor IEs.
2300 */
2301static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002302 (1ULL << WLAN_EID_COUNTRY) |
2303 (1ULL << WLAN_EID_ERP_INFO) |
2304 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2305 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2306 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002307 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002308
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002309static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002310 struct ieee80211_mgmt *mgmt,
2311 size_t len,
2312 struct ieee80211_rx_status *rx_status)
2313{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002314 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002315 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002316 size_t baselen;
2317 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002318 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002319 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002320 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002321 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002322 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002323 u8 *bssid;
2324
Johannes Berg66e67e42012-01-20 13:55:27 +01002325 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002326
Ester Kummerae6a44e2008-06-27 18:54:48 +03002327 /* Process beacon from the current BSS */
2328 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2329 if (baselen > len)
2330 return;
2331
Johannes Berg568d6e22012-07-23 14:29:21 +02002332 if (rx_status->freq != local->oper_channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07002333 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002334
Johannes Berg66e67e42012-01-20 13:55:27 +01002335 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002336 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002337 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2338 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002339
Johannes Berg66e67e42012-01-20 13:55:27 +01002340 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2341 false);
2342 ifmgd->assoc_data->have_beacon = true;
2343 ifmgd->assoc_data->sent_assoc = false;
2344 /* continue assoc process */
2345 ifmgd->assoc_data->timeout = jiffies;
2346 run_again(ifmgd, ifmgd->assoc_data->timeout);
2347 return;
2348 }
2349
2350 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002351 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002352 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002353 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002354
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002355 /* Track average RSSI from the Beacon frames of the current AP */
2356 ifmgd->last_beacon_signal = rx_status->signal;
2357 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2358 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002359 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002360 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002361 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002362 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002363 } else {
2364 ifmgd->ave_beacon_signal =
2365 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2366 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2367 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002368 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002369 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002370
2371 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2372 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2373 int sig = ifmgd->ave_beacon_signal;
2374 int last_sig = ifmgd->last_ave_beacon_signal;
2375
2376 /*
2377 * if signal crosses either of the boundaries, invoke callback
2378 * with appropriate parameters
2379 */
2380 if (sig > ifmgd->rssi_max_thold &&
2381 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2382 ifmgd->last_ave_beacon_signal = sig;
2383 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2384 } else if (sig < ifmgd->rssi_min_thold &&
2385 (last_sig >= ifmgd->rssi_max_thold ||
2386 last_sig == 0)) {
2387 ifmgd->last_ave_beacon_signal = sig;
2388 drv_rssi_callback(local, RSSI_EVENT_LOW);
2389 }
2390 }
2391
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002392 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002393 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002394 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002395 int sig = ifmgd->ave_beacon_signal / 16;
2396 int last_event = ifmgd->last_cqm_event_signal;
2397 int thold = bss_conf->cqm_rssi_thold;
2398 int hyst = bss_conf->cqm_rssi_hyst;
2399 if (sig < thold &&
2400 (last_event == 0 || sig < last_event - hyst)) {
2401 ifmgd->last_cqm_event_signal = sig;
2402 ieee80211_cqm_rssi_notify(
2403 &sdata->vif,
2404 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2405 GFP_KERNEL);
2406 } else if (sig > thold &&
2407 (last_event == 0 || sig > last_event + hyst)) {
2408 ifmgd->last_cqm_event_signal = sig;
2409 ieee80211_cqm_rssi_notify(
2410 &sdata->vif,
2411 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2412 GFP_KERNEL);
2413 }
2414 }
2415
Johannes Bergb291ba12009-07-10 15:29:03 +02002416 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002417 mlme_dbg_ratelimited(sdata,
2418 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002419 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002420 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002421 ieee80211_run_deferred_scan(local);
2422 mutex_unlock(&local->mtx);
2423
Johannes Berg4e751842009-06-10 15:16:52 +02002424 mutex_lock(&local->iflist_mtx);
2425 ieee80211_recalc_ps(local, -1);
2426 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002427 }
2428
Johannes Bergb291ba12009-07-10 15:29:03 +02002429 /*
2430 * Push the beacon loss detection into the future since
2431 * we are processing a beacon from the AP just now.
2432 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002433 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002434
Johannes Bergd91f36d2009-04-16 13:17:26 +02002435 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2436 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2437 len - baselen, &elems,
2438 care_about_ies, ncrc);
2439
2440 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02002441 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
2442 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002443
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302444 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02002445 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002446 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002447 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2448 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2449 ieee80211_hw_config(local,
2450 IEEE80211_CONF_CHANGE_PS);
2451 }
Kalle Valo572e0012009-02-10 17:09:31 +02002452 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302453 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002454 local->pspolling = true;
2455
2456 /*
2457 * Here is assumed that the driver will be
2458 * able to send ps-poll frame and receive a
2459 * response even though power save mode is
2460 * enabled, but some drivers might require
2461 * to disable power save here. This needs
2462 * to be investigated.
2463 */
2464 ieee80211_send_pspoll(local, sdata);
2465 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002466 }
2467 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002468
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002469 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002470 return;
2471 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002472 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002473
Johannes Berg7d257452012-07-06 17:37:43 +02002474 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2475 true);
2476
2477 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2478 elems.wmm_param_len))
2479 changed |= BSS_CHANGED_QOS;
2480
Johannes Berg7a5158e2008-10-08 10:59:33 +02002481 if (elems.erp_info && elems.erp_info_len >= 1) {
2482 erp_valid = true;
2483 erp_value = elems.erp_info[0];
2484 } else {
2485 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002486 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002487 changed |= ieee80211_handle_bss_capability(sdata,
2488 le16_to_cpu(mgmt->u.beacon.capab_info),
2489 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002490
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002491
Johannes Berg074d46d2012-03-15 19:45:16 +01002492 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002493 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02002494 struct ieee80211_supported_band *sband;
Johannes Bergae5eb022008-10-14 16:58:37 +02002495
Johannes Berg568d6e22012-07-23 14:29:21 +02002496 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Bergae5eb022008-10-14 16:58:37 +02002497
Johannes Berg24398e32012-03-28 10:58:36 +02002498 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2499 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002500 }
2501
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002502 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002503 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302504 /* TODO: IBSS also needs this */
2505 if (elems.pwr_constr_elem)
2506 ieee80211_handle_pwr_constr(sdata,
2507 le16_to_cpu(mgmt->u.probe_resp.capab_info),
Johannes Berg761a48d2012-09-05 13:07:00 +02002508 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002509 }
2510
Johannes Berg471b3ef2007-12-28 14:32:58 +01002511 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002512}
2513
Johannes Berg1fa57d02010-06-10 10:21:32 +02002514void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2515 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002516{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002517 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002518 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002519 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002520 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002521 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002522 u16 fc;
2523
Jiri Bencf0706e82007-05-05 11:45:53 -07002524 rx_status = (struct ieee80211_rx_status *) skb->cb;
2525 mgmt = (struct ieee80211_mgmt *) skb->data;
2526 fc = le16_to_cpu(mgmt->frame_control);
2527
Johannes Berg77fdaa12009-07-07 03:45:17 +02002528 mutex_lock(&ifmgd->mtx);
2529
Johannes Berg66e67e42012-01-20 13:55:27 +01002530 switch (fc & IEEE80211_FCTL_STYPE) {
2531 case IEEE80211_STYPE_BEACON:
2532 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2533 break;
2534 case IEEE80211_STYPE_PROBE_RESP:
2535 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2536 break;
2537 case IEEE80211_STYPE_AUTH:
2538 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2539 break;
2540 case IEEE80211_STYPE_DEAUTH:
2541 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2542 break;
2543 case IEEE80211_STYPE_DISASSOC:
2544 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2545 break;
2546 case IEEE80211_STYPE_ASSOC_RESP:
2547 case IEEE80211_STYPE_REASSOC_RESP:
2548 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2549 break;
2550 case IEEE80211_STYPE_ACTION:
2551 switch (mgmt->u.action.category) {
2552 case WLAN_CATEGORY_SPECTRUM_MGMT:
2553 ieee80211_sta_process_chanswitch(sdata,
2554 &mgmt->u.action.u.chan_switch.sw_elem,
2555 (void *)ifmgd->associated->priv,
2556 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002557 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002558 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002559 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002560 mutex_unlock(&ifmgd->mtx);
2561
Johannes Berg66e67e42012-01-20 13:55:27 +01002562 switch (rma) {
2563 case RX_MGMT_NONE:
2564 /* no action */
2565 break;
2566 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002567 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002568 break;
2569 case RX_MGMT_CFG80211_DISASSOC:
2570 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2571 break;
2572 case RX_MGMT_CFG80211_RX_AUTH:
2573 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2574 break;
2575 case RX_MGMT_CFG80211_RX_ASSOC:
2576 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2577 break;
2578 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2579 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2580 break;
2581 default:
2582 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002583 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002584}
2585
Johannes Berg9c6bd792008-09-11 00:01:52 +02002586static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002587{
2588 struct ieee80211_sub_if_data *sdata =
2589 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002590 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002591 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002592
Johannes Berg5bb644a2009-05-17 11:40:42 +02002593 if (local->quiescing) {
2594 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2595 return;
2596 }
2597
Johannes Berg64592c82010-06-10 10:21:31 +02002598 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002599}
2600
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002601static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002602 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002603{
2604 struct ieee80211_local *local = sdata->local;
2605 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002606 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002607
Johannes Berg37ad3882012-02-24 13:50:54 +01002608 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2609 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002610 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002611
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002612 /*
2613 * must be outside lock due to cfg80211,
2614 * but that's not a problem.
2615 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002616 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002617
2618 mutex_lock(&local->mtx);
2619 ieee80211_recalc_idle(local);
2620 mutex_unlock(&local->mtx);
2621
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002622 mutex_lock(&ifmgd->mtx);
2623}
2624
Johannes Berg66e67e42012-01-20 13:55:27 +01002625static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2626{
2627 struct ieee80211_local *local = sdata->local;
2628 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2629 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2630
2631 lockdep_assert_held(&ifmgd->mtx);
2632
2633 if (WARN_ON_ONCE(!auth_data))
2634 return -EINVAL;
2635
Johannes Berg66e67e42012-01-20 13:55:27 +01002636 auth_data->tries++;
2637
2638 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002639 sdata_info(sdata, "authentication with %pM timed out\n",
2640 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002641
2642 /*
2643 * Most likely AP is not in the range so remove the
2644 * bss struct for that AP.
2645 */
2646 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2647
2648 return -ETIMEDOUT;
2649 }
2650
Johannes Berga1845fc2012-06-27 13:18:36 +02002651 drv_mgd_prepare_tx(local, sdata);
2652
Johannes Berg66e67e42012-01-20 13:55:27 +01002653 if (auth_data->bss->proberesp_ies) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002654 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2655 auth_data->bss->bssid, auth_data->tries,
2656 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002657
2658 auth_data->expected_transaction = 2;
2659 ieee80211_send_auth(sdata, 1, auth_data->algorithm,
2660 auth_data->ie, auth_data->ie_len,
2661 auth_data->bss->bssid,
2662 auth_data->bss->bssid, NULL, 0, 0);
2663 } else {
2664 const u8 *ssidie;
2665
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002666 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2667 auth_data->bss->bssid, auth_data->tries,
2668 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002669
2670 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2671 if (!ssidie)
2672 return -EINVAL;
2673 /*
2674 * Direct probe is sent to broadcast address as some APs
2675 * will not answer to direct packet in unassociated state.
2676 */
2677 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Bergfe94fe02012-07-30 12:26:34 +02002678 NULL, 0, (u32) -1, true, false,
2679 auth_data->bss->channel);
Johannes Berg66e67e42012-01-20 13:55:27 +01002680 }
2681
2682 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2683 run_again(ifmgd, auth_data->timeout);
2684
2685 return 0;
2686}
2687
2688static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2689{
2690 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2691 struct ieee80211_local *local = sdata->local;
2692
2693 lockdep_assert_held(&sdata->u.mgd.mtx);
2694
Johannes Berg66e67e42012-01-20 13:55:27 +01002695 assoc_data->tries++;
2696 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002697 sdata_info(sdata, "association with %pM timed out\n",
2698 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002699
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, assoc_data->bss);
2705
2706 return -ETIMEDOUT;
2707 }
2708
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002709 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2710 assoc_data->bss->bssid, assoc_data->tries,
2711 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002712 ieee80211_send_assoc(sdata);
2713
2714 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2715 run_again(&sdata->u.mgd, assoc_data->timeout);
2716
2717 return 0;
2718}
2719
Johannes Berg1fa57d02010-06-10 10:21:32 +02002720void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002721{
Johannes Berg60f8b392008-09-08 17:44:22 +02002722 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002723 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002724
Johannes Berg77fdaa12009-07-07 03:45:17 +02002725 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002726
Johannes Berg66e67e42012-01-20 13:55:27 +01002727 if (ifmgd->auth_data &&
2728 time_after(jiffies, ifmgd->auth_data->timeout)) {
2729 if (ifmgd->auth_data->done) {
2730 /*
2731 * ok ... we waited for assoc but userspace didn't,
2732 * so let's just kill the auth data
2733 */
2734 ieee80211_destroy_auth_data(sdata, false);
2735 } else if (ieee80211_probe_auth(sdata)) {
2736 u8 bssid[ETH_ALEN];
2737
2738 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2739
2740 ieee80211_destroy_auth_data(sdata, false);
2741
2742 mutex_unlock(&ifmgd->mtx);
2743 cfg80211_send_auth_timeout(sdata->dev, bssid);
2744 mutex_lock(&ifmgd->mtx);
2745 }
2746 } else if (ifmgd->auth_data)
2747 run_again(ifmgd, ifmgd->auth_data->timeout);
2748
2749 if (ifmgd->assoc_data &&
2750 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2751 if (!ifmgd->assoc_data->have_beacon ||
2752 ieee80211_do_assoc(sdata)) {
2753 u8 bssid[ETH_ALEN];
2754
2755 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2756
2757 ieee80211_destroy_assoc_data(sdata, false);
2758
2759 mutex_unlock(&ifmgd->mtx);
2760 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2761 mutex_lock(&ifmgd->mtx);
2762 }
2763 } else if (ifmgd->assoc_data)
2764 run_again(ifmgd, ifmgd->assoc_data->timeout);
2765
Johannes Bergb291ba12009-07-10 15:29:03 +02002766 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2767 IEEE80211_STA_CONNECTION_POLL) &&
2768 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002769 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002770 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002771
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002772 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002773
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002774 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002775 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002776 else
Ben Greear180205b2011-02-04 15:30:24 -08002777 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002778
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002779 /* ACK received for nullfunc probing frame */
2780 if (!ifmgd->probe_send_count)
2781 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002782 else if (ifmgd->nullfunc_failed) {
2783 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002784 mlme_dbg(sdata,
2785 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
2786 bssid, ifmgd->probe_send_count,
2787 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002788 ieee80211_mgd_probe_ap_send(sdata);
2789 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002790 mlme_dbg(sdata,
2791 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
2792 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02002793 ieee80211_sta_connection_lost(sdata, bssid,
2794 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002795 }
2796 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002797 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002798 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002799 mlme_dbg(sdata,
2800 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
2801 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02002802 ieee80211_sta_connection_lost(sdata, bssid,
2803 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002804 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002805 mlme_dbg(sdata,
2806 "No probe response from AP %pM after %dms, try %d/%i\n",
2807 bssid, probe_wait_ms,
2808 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002809 ieee80211_mgd_probe_ap_send(sdata);
2810 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002811 /*
2812 * We actually lost the connection ... or did we?
2813 * Let's make sure!
2814 */
Ben Greearb38afa82010-10-07 16:12:06 -07002815 wiphy_debug(local->hw.wiphy,
2816 "%s: No probe response from AP %pM"
2817 " after %dms, disconnecting.\n",
2818 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002819 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002820
Johannes Berg95acac62011-07-12 12:30:59 +02002821 ieee80211_sta_connection_lost(sdata, bssid,
2822 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02002823 }
2824 }
2825
Johannes Berg77fdaa12009-07-07 03:45:17 +02002826 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002827
2828 mutex_lock(&local->mtx);
2829 ieee80211_recalc_idle(local);
2830 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002831}
Johannes Berg0a51b272008-09-08 17:44:25 +02002832
Johannes Bergb291ba12009-07-10 15:29:03 +02002833static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2834{
2835 struct ieee80211_sub_if_data *sdata =
2836 (struct ieee80211_sub_if_data *) data;
2837 struct ieee80211_local *local = sdata->local;
2838
2839 if (local->quiescing)
2840 return;
2841
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002842 ieee80211_queue_work(&sdata->local->hw,
2843 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002844}
2845
2846static void ieee80211_sta_conn_mon_timer(unsigned long data)
2847{
2848 struct ieee80211_sub_if_data *sdata =
2849 (struct ieee80211_sub_if_data *) data;
2850 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2851 struct ieee80211_local *local = sdata->local;
2852
2853 if (local->quiescing)
2854 return;
2855
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002856 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002857}
2858
2859static void ieee80211_sta_monitor_work(struct work_struct *work)
2860{
2861 struct ieee80211_sub_if_data *sdata =
2862 container_of(work, struct ieee80211_sub_if_data,
2863 u.mgd.monitor_work);
2864
2865 ieee80211_mgd_probe_ap(sdata, false);
2866}
2867
Johannes Berga1678f82008-09-11 00:01:47 +02002868static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2869{
Eliad Peller494f1fe2012-02-19 15:26:09 +02002870 u32 flags;
2871
Kalle Vaload935682009-04-19 08:47:19 +03002872 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002873 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03002874
Johannes Bergb291ba12009-07-10 15:29:03 +02002875 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02002876 flags = sdata->local->hw.flags;
2877 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
2878 ieee80211_queue_work(&sdata->local->hw,
2879 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002880 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02002881 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03002882 }
Johannes Berga1678f82008-09-11 00:01:47 +02002883}
2884
Johannes Berg5bb644a2009-05-17 11:40:42 +02002885#ifdef CONFIG_PM
2886void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2887{
2888 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2889
2890 /*
2891 * we need to use atomic bitops for the running bits
2892 * only because both timers might fire at the same
2893 * time -- the code here is properly synchronised.
2894 */
2895
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002896 cancel_work_sync(&ifmgd->request_smps_work);
2897
Johannes Berg0ecfe8062011-11-09 12:14:16 +01002898 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002899 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02002900 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002901 if (del_timer_sync(&ifmgd->timer))
2902 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2903
2904 cancel_work_sync(&ifmgd->chswitch_work);
2905 if (del_timer_sync(&ifmgd->chswitch_timer))
2906 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002907
Johannes Bergb291ba12009-07-10 15:29:03 +02002908 /* these will just be re-established on connection */
2909 del_timer_sync(&ifmgd->conn_mon_timer);
2910 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002911}
2912
2913void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2914{
2915 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2916
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05302917 if (!ifmgd->associated)
2918 return;
2919
Johannes Berg95acac62011-07-12 12:30:59 +02002920 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
2921 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
2922 mutex_lock(&ifmgd->mtx);
2923 if (ifmgd->associated) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002924 mlme_dbg(sdata,
2925 "driver requested disconnect after resume\n");
Johannes Berg95acac62011-07-12 12:30:59 +02002926 ieee80211_sta_connection_lost(sdata,
2927 ifmgd->associated->bssid,
2928 WLAN_REASON_UNSPECIFIED);
2929 mutex_unlock(&ifmgd->mtx);
2930 return;
2931 }
2932 mutex_unlock(&ifmgd->mtx);
2933 }
2934
Johannes Berg5bb644a2009-05-17 11:40:42 +02002935 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2936 add_timer(&ifmgd->timer);
2937 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2938 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01002939 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002940
2941 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01002942 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002943 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002944}
2945#endif
2946
Johannes Berg9c6bd792008-09-11 00:01:52 +02002947/* interface setup */
2948void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2949{
Johannes Berg46900292009-02-15 12:44:28 +01002950 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002951
Johannes Berg46900292009-02-15 12:44:28 +01002952 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002953 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01002954 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002955 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2956 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02002957 INIT_WORK(&ifmgd->csa_connection_drop_work,
2958 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002959 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01002960 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02002961 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002962 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2963 (unsigned long) sdata);
2964 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2965 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002966 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05302967 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002968
Johannes Bergab1faea2009-07-01 21:41:17 +02002969 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05302970 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02002971 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
2972 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02002973
Johannes Berg77fdaa12009-07-07 03:45:17 +02002974 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01002975
2976 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2977 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2978 else
2979 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002980}
2981
2982/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002983void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2984{
Johannes Berg31ee67a2012-07-06 21:18:24 +02002985 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02002986
2987 /* Restart STA timers */
2988 rcu_read_lock();
2989 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2990 ieee80211_restart_sta_timer(sdata);
2991 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02002992}
Johannes Berg10f644a2009-04-16 13:17:25 +02002993
2994int ieee80211_max_network_latency(struct notifier_block *nb,
2995 unsigned long data, void *dummy)
2996{
2997 s32 latency_usec = (s32) data;
2998 struct ieee80211_local *local =
2999 container_of(nb, struct ieee80211_local,
3000 network_latency_notifier);
3001
3002 mutex_lock(&local->iflist_mtx);
3003 ieee80211_recalc_ps(local, latency_usec);
3004 mutex_unlock(&local->iflist_mtx);
3005
3006 return 0;
3007}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003008
Johannes Bergb17166a2012-07-27 11:41:27 +02003009static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3010 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003011{
3012 struct ieee80211_local *local = sdata->local;
3013 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003014 int ht_cfreq;
3015 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3016 const u8 *ht_oper_ie;
3017 const struct ieee80211_ht_operation *ht_oper = NULL;
3018 struct ieee80211_supported_band *sband;
Johannes Berga1cf7752012-03-08 15:02:07 +01003019
Johannes Berg24398e32012-03-28 10:58:36 +02003020 sband = local->hw.wiphy->bands[cbss->channel->band];
3021
3022 ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
3023
3024 if (sband->ht_cap.ht_supported) {
3025 ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
3026 cbss->information_elements,
3027 cbss->len_information_elements);
3028 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3029 ht_oper = (void *)(ht_oper_ie + 2);
3030 }
3031
3032 if (ht_oper) {
3033 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3034 cbss->channel->band);
3035 /* check that channel matches the right operating channel */
3036 if (cbss->channel->center_freq != ht_cfreq) {
3037 /*
3038 * It's possible that some APs are confused here;
3039 * Netgear WNDR3700 sometimes reports 4 higher than
3040 * the actual channel in association responses, but
3041 * since we look at probe response/beacon data here
3042 * it should be OK.
3043 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003044 sdata_info(sdata,
3045 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3046 cbss->channel->center_freq,
3047 ht_cfreq, ht_oper->primary_chan,
3048 cbss->channel->band);
Johannes Berg24398e32012-03-28 10:58:36 +02003049 ht_oper = NULL;
3050 }
3051 }
3052
3053 if (ht_oper) {
3054 channel_type = NL80211_CHAN_HT20;
3055
3056 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3057 switch (ht_oper->ht_param &
3058 IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3059 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3060 channel_type = NL80211_CHAN_HT40PLUS;
3061 break;
3062 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3063 channel_type = NL80211_CHAN_HT40MINUS;
3064 break;
3065 }
3066 }
3067 }
3068
3069 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
3070 /* can only fail due to HT40+/- mismatch */
3071 channel_type = NL80211_CHAN_HT20;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003072 sdata_info(sdata,
3073 "disabling 40 MHz due to multi-vif mismatch\n");
Johannes Berg24398e32012-03-28 10:58:36 +02003074 ifmgd->flags |= IEEE80211_STA_DISABLE_40MHZ;
3075 WARN_ON(!ieee80211_set_channel_type(local, sdata,
3076 channel_type));
3077 }
3078
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003079 local->oper_channel = cbss->channel;
Stanislaw Gruszka6aee4ca2012-06-07 14:47:21 +02003080 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berga1cf7752012-03-08 15:02:07 +01003081
Johannes Bergb17166a2012-07-27 11:41:27 +02003082 return 0;
3083}
3084
3085static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3086 struct cfg80211_bss *cbss, bool assoc)
3087{
3088 struct ieee80211_local *local = sdata->local;
3089 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3090 struct ieee80211_bss *bss = (void *)cbss->priv;
3091 struct sta_info *new_sta = NULL;
3092 bool have_sta = false;
3093 int err;
3094
3095 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3096 return -EINVAL;
3097
3098 if (assoc) {
3099 rcu_read_lock();
3100 have_sta = sta_info_get(sdata, cbss->bssid);
3101 rcu_read_unlock();
3102 }
3103
3104 if (!have_sta) {
3105 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3106 if (!new_sta)
3107 return -ENOMEM;
3108 }
3109
3110 mutex_lock(&local->mtx);
3111 ieee80211_recalc_idle(sdata->local);
3112 mutex_unlock(&local->mtx);
3113
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003114 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003115 u32 rates = 0, basic_rates = 0;
3116 bool have_higher_than_11mbit;
3117 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003118 struct ieee80211_supported_band *sband;
3119
3120 sband = local->hw.wiphy->bands[cbss->channel->band];
3121
3122 err = ieee80211_prep_channel(sdata, cbss);
3123 if (err) {
3124 sta_info_free(local, new_sta);
3125 return err;
3126 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003127
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003128 ieee80211_get_rates(sband, bss->supp_rates,
3129 bss->supp_rates_len,
3130 &rates, &basic_rates,
3131 &have_higher_than_11mbit,
3132 &min_rate, &min_rate_index);
3133
3134 /*
3135 * This used to be a workaround for basic rates missing
3136 * in the association response frame. Now that we no
3137 * longer use the basic rates from there, it probably
3138 * doesn't happen any more, but keep the workaround so
3139 * in case some *other* APs are buggy in different ways
3140 * we can connect -- with a warning.
3141 */
3142 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003143 sdata_info(sdata,
3144 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003145 basic_rates = BIT(min_rate_index);
3146 }
3147
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003148 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003149 sdata->vif.bss_conf.basic_rates = basic_rates;
3150
3151 /* cf. IEEE 802.11 9.2.12 */
3152 if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
3153 have_higher_than_11mbit)
3154 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3155 else
3156 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3157
3158 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3159
Johannes Berg8c358bc2012-05-22 22:13:05 +02003160 /* set timing information */
3161 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3162 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3163 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3164
3165 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003166 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003167 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3168 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003169
3170 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003171 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003172
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003173 err = sta_info_insert(new_sta);
3174 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003175 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003176 sdata_info(sdata,
3177 "failed to insert STA entry for the AP (error %d)\n",
3178 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003179 return err;
3180 }
3181 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003182 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003183
3184 return 0;
3185}
3186
Johannes Berg77fdaa12009-07-07 03:45:17 +02003187/* config hooks */
3188int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3189 struct cfg80211_auth_request *req)
3190{
Johannes Berg66e67e42012-01-20 13:55:27 +01003191 struct ieee80211_local *local = sdata->local;
3192 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3193 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003194 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003195 int err;
3196
3197 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003198
3199 switch (req->auth_type) {
3200 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3201 auth_alg = WLAN_AUTH_OPEN;
3202 break;
3203 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003204 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003205 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003206 auth_alg = WLAN_AUTH_SHARED_KEY;
3207 break;
3208 case NL80211_AUTHTYPE_FT:
3209 auth_alg = WLAN_AUTH_FT;
3210 break;
3211 case NL80211_AUTHTYPE_NETWORK_EAP:
3212 auth_alg = WLAN_AUTH_LEAP;
3213 break;
3214 default:
3215 return -EOPNOTSUPP;
3216 }
3217
Johannes Berg66e67e42012-01-20 13:55:27 +01003218 auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
3219 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003220 return -ENOMEM;
3221
Johannes Berg66e67e42012-01-20 13:55:27 +01003222 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003223
3224 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003225 memcpy(auth_data->ie, req->ie, req->ie_len);
3226 auth_data->ie_len = req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003227 }
3228
Johannes Bergfffd0932009-07-08 14:22:54 +02003229 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003230 auth_data->key_len = req->key_len;
3231 auth_data->key_idx = req->key_idx;
3232 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003233 }
3234
Johannes Berg66e67e42012-01-20 13:55:27 +01003235 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003236
Johannes Berg66e67e42012-01-20 13:55:27 +01003237 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003238
Johannes Berg66e67e42012-01-20 13:55:27 +01003239 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003240
Johannes Berg66e67e42012-01-20 13:55:27 +01003241 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3242 ifmgd->assoc_data) {
3243 err = -EBUSY;
3244 goto err_free;
3245 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003246
Johannes Berg66e67e42012-01-20 13:55:27 +01003247 if (ifmgd->auth_data)
3248 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003249
Johannes Berg66e67e42012-01-20 13:55:27 +01003250 /* prep auth_data so we don't go into idle on disassoc */
3251 ifmgd->auth_data = auth_data;
3252
3253 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003254 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003255
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003256 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003257
Johannes Berga1cf7752012-03-08 15:02:07 +01003258 err = ieee80211_prep_connection(sdata, req->bss, false);
3259 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003260 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003261
Johannes Berg66e67e42012-01-20 13:55:27 +01003262 err = ieee80211_probe_auth(sdata);
3263 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003264 sta_info_destroy_addr(sdata, req->bss->bssid);
3265 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003266 }
3267
Johannes Berg66e67e42012-01-20 13:55:27 +01003268 /* hold our own reference */
3269 cfg80211_ref_bss(auth_data->bss);
3270 err = 0;
3271 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003272
Johannes Berg66e67e42012-01-20 13:55:27 +01003273 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003274 memset(ifmgd->bssid, 0, ETH_ALEN);
3275 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003276 ifmgd->auth_data = NULL;
3277 err_free:
3278 kfree(auth_data);
3279 out_unlock:
3280 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003281
Johannes Berg66e67e42012-01-20 13:55:27 +01003282 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003283}
3284
Johannes Berg77fdaa12009-07-07 03:45:17 +02003285int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3286 struct cfg80211_assoc_request *req)
3287{
Johannes Berg66e67e42012-01-20 13:55:27 +01003288 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003289 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003290 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003291 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003292 struct ieee80211_supported_band *sband;
Johannes Berg9dde6422012-05-16 23:43:19 +02003293 const u8 *ssidie, *ht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003294 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003295
Johannes Berg66e67e42012-01-20 13:55:27 +01003296 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3297 if (!ssidie)
3298 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003299
Johannes Berg66e67e42012-01-20 13:55:27 +01003300 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3301 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003302 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003303
Johannes Berg66e67e42012-01-20 13:55:27 +01003304 mutex_lock(&ifmgd->mtx);
3305
3306 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003307 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003308
3309 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3310 err = -EBUSY;
3311 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003312 }
3313
Johannes Berg66e67e42012-01-20 13:55:27 +01003314 if (ifmgd->assoc_data) {
3315 err = -EBUSY;
3316 goto err_free;
3317 }
3318
3319 if (ifmgd->auth_data) {
3320 bool match;
3321
3322 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003323 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003324 ieee80211_destroy_auth_data(sdata, match);
3325 }
3326
3327 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003328
3329 /*
3330 * keep only the 40 MHz disable bit set as it might have
3331 * been set during authentication already, all other bits
3332 * should be reset for a new connection
3333 */
3334 ifmgd->flags &= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003335
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003336 ifmgd->beacon_crc_valid = false;
3337
Johannes Bergde5036a2012-03-08 15:02:03 +01003338 /*
3339 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3340 * We still associate in non-HT mode (11a/b/g) if any one of these
3341 * ciphers is configured as pairwise.
3342 * We can set this to true for non-11n hardware, that'll be checked
3343 * separately along with the peer capabilities.
3344 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003345 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003346 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3347 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003348 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003349 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003350 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003351 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003352 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003353 }
3354 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003355
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003356 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Ben Greearef96a8422011-11-18 11:32:00 -08003357 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003358 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3359 }
Ben Greearef96a8422011-11-18 11:32:00 -08003360
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003361 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3362 sband = local->hw.wiphy->bands[req->bss->channel->band];
3363 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003364 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003365 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg1b49de22012-07-27 10:29:14 +02003366 if (!bss->wmm_used)
3367 netdev_info(sdata->dev,
3368 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003369 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003370
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003371 /* disable VHT if we don't support it or the AP doesn't use WMM */
3372 if (!sband->vht_cap.vht_supported ||
3373 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3374 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003375 if (!bss->wmm_used)
3376 netdev_info(sdata->dev,
3377 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003378 }
3379
Ben Greearef96a8422011-11-18 11:32:00 -08003380 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3381 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3382 sizeof(ifmgd->ht_capa_mask));
3383
Johannes Berg77fdaa12009-07-07 03:45:17 +02003384 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003385 memcpy(assoc_data->ie, req->ie, req->ie_len);
3386 assoc_data->ie_len = req->ie_len;
3387 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003388
Johannes Berg66e67e42012-01-20 13:55:27 +01003389 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003390
Johannes Bergaf6b6372009-12-23 13:15:35 +01003391 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3392 if (ifmgd->powersave)
3393 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
3394 else
3395 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
3396 } else
3397 ifmgd->ap_smps = ifmgd->req_smps;
3398
Johannes Berg66e67e42012-01-20 13:55:27 +01003399 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003400 assoc_data->wmm = bss->wmm_used &&
3401 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003402 assoc_data->supp_rates = bss->supp_rates;
3403 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003404
3405 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3406 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3407 assoc_data->ap_ht_param =
3408 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3409 else
3410 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg63f170e2009-12-23 13:15:33 +01003411
Kalle Valoab133152010-01-12 10:42:31 +02003412 if (bss->wmm_used && bss->uapsd_supported &&
3413 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003414 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003415 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3416 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003417 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003418 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3419 }
3420
Johannes Berg66e67e42012-01-20 13:55:27 +01003421 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3422 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003423
Johannes Berg77fdaa12009-07-07 03:45:17 +02003424 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003425 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003426
3427 if (req->use_mfp) {
3428 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3429 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3430 } else {
3431 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3432 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3433 }
3434
3435 if (req->crypto.control_port)
3436 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3437 else
3438 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3439
Johannes Berga621fa42010-08-27 14:26:54 +03003440 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3441 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3442
Johannes Berg66e67e42012-01-20 13:55:27 +01003443 /* kick off associate process */
3444
3445 ifmgd->assoc_data = assoc_data;
3446
Johannes Berga1cf7752012-03-08 15:02:07 +01003447 err = ieee80211_prep_connection(sdata, req->bss, true);
3448 if (err)
3449 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003450
3451 if (!bss->dtim_period &&
3452 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3453 /*
3454 * Wait up to one beacon interval ...
3455 * should this be more if we miss one?
3456 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003457 sdata_info(sdata, "waiting for beacon from %pM\n",
3458 ifmgd->bssid);
Johannes Berg3f9768a2012-03-28 21:02:46 +02003459 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +01003460 } else {
3461 assoc_data->have_beacon = true;
3462 assoc_data->sent_assoc = false;
3463 assoc_data->timeout = jiffies;
3464 }
3465 run_again(ifmgd, assoc_data->timeout);
3466
Paul Stewartfcff4f12012-02-23 17:59:53 -08003467 if (bss->corrupt_data) {
3468 char *corrupt_type = "data";
3469 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3470 if (bss->corrupt_data &
3471 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3472 corrupt_type = "beacon and probe response";
3473 else
3474 corrupt_type = "beacon";
3475 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3476 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003477 sdata_info(sdata, "associating with AP with corrupt %s\n",
3478 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08003479 }
3480
Johannes Berg66e67e42012-01-20 13:55:27 +01003481 err = 0;
3482 goto out;
3483 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003484 memset(ifmgd->bssid, 0, ETH_ALEN);
3485 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003486 ifmgd->assoc_data = NULL;
3487 err_free:
3488 kfree(assoc_data);
3489 out:
3490 mutex_unlock(&ifmgd->mtx);
3491
3492 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003493}
3494
3495int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003496 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003497{
3498 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003499 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003500
Johannes Berg77fdaa12009-07-07 03:45:17 +02003501 mutex_lock(&ifmgd->mtx);
3502
Johannes Berg37ad3882012-02-24 13:50:54 +01003503 if (ifmgd->auth_data) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003504 ieee80211_destroy_auth_data(sdata, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003505 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003506 return 0;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003507 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003508
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003509 sdata_info(sdata,
3510 "deauthenticating from %pM by local choice (reason=%d)\n",
3511 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003512
Johannes Berg37ad3882012-02-24 13:50:54 +01003513 if (ifmgd->associated &&
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003514 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Berg37ad3882012-02-24 13:50:54 +01003515 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
Johannes Berg5fef7db2012-02-24 13:50:52 +01003516 req->reason_code, true, frame_buf);
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003517 } else {
3518 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01003519 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3520 IEEE80211_STYPE_DEAUTH,
3521 req->reason_code, true,
3522 frame_buf);
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003523 }
3524
Johannes Berg37ad3882012-02-24 13:50:54 +01003525 mutex_unlock(&ifmgd->mtx);
3526
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003527 __cfg80211_send_deauth(sdata->dev, frame_buf,
3528 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003529
Johannes Berg7da7cc12010-08-05 17:02:38 +02003530 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003531 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003532 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003533
Johannes Berg77fdaa12009-07-07 03:45:17 +02003534 return 0;
3535}
3536
3537int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003538 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003539{
3540 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003541 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003542 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003543
Johannes Berg77fdaa12009-07-07 03:45:17 +02003544 mutex_lock(&ifmgd->mtx);
3545
Johannes Berg8d8b2612009-07-25 11:58:36 +02003546 /*
3547 * cfg80211 should catch this ... but it's racy since
3548 * we can receive a disassoc frame, process it, hand it
3549 * to cfg80211 while that's in a locked section already
3550 * trying to tell us that the user wants to disconnect.
3551 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003552 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003553 mutex_unlock(&ifmgd->mtx);
3554 return -ENOLINK;
3555 }
3556
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003557 sdata_info(sdata,
3558 "disassociating from %pM by local choice (reason=%d)\n",
3559 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003560
Jouni Malinene69e95d2010-03-29 23:29:31 -07003561 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01003562 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
3563 req->reason_code, !req->local_state_change,
3564 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003565 mutex_unlock(&ifmgd->mtx);
3566
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003567 __cfg80211_send_disassoc(sdata->dev, frame_buf,
3568 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01003569
Johannes Berg7da7cc12010-08-05 17:02:38 +02003570 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003571 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003572 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003573
Johannes Berg77fdaa12009-07-07 03:45:17 +02003574 return 0;
3575}
Jouni Malinen026331c2010-02-15 12:53:10 +02003576
Eliad Pellerafa762f2012-04-23 14:45:15 +03003577void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01003578{
3579 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3580
3581 mutex_lock(&ifmgd->mtx);
3582 if (ifmgd->assoc_data)
3583 ieee80211_destroy_assoc_data(sdata, false);
3584 if (ifmgd->auth_data)
3585 ieee80211_destroy_auth_data(sdata, false);
3586 del_timer_sync(&ifmgd->timer);
3587 mutex_unlock(&ifmgd->mtx);
3588}
3589
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003590void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3591 enum nl80211_cqm_rssi_threshold_event rssi_event,
3592 gfp_t gfp)
3593{
3594 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3595
Johannes Bergb5878a22010-04-07 16:48:40 +02003596 trace_api_cqm_rssi_notify(sdata, rssi_event);
3597
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003598 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3599}
3600EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);